dialogs.py 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. """!
  2. @package gui_core.dialogs
  3. @brief Various dialogs used in wxGUI.
  4. List of classes:
  5. - dialogs::ElementDialog
  6. - dialogs::LocationDialog
  7. - dialogs::MapsetDialog
  8. - dialogs::NewVectorDialog
  9. - dialogs::SavedRegion
  10. - dialogs::DecorationDialog
  11. - dialogs::TextLayerDialog
  12. - dialogs::GroupDialog
  13. - dialogs::MapLayersDialog
  14. - dialogs::ImportDialog
  15. - dialogs::GdalImportDialog
  16. - dialogs::GdalOutputDialog
  17. - dialogs::DxfImportDialog
  18. - dialogs::LayersList (used by MultiImport)
  19. - dialogs::SetOpacityDialog
  20. - dialogs::ImageSizeDialog
  21. - dialogs::SqlQueryFrame
  22. (C) 2008-2011 by the GRASS Development Team
  23. This program is free software under the GNU General Public License
  24. (>=v2). Read the file COPYING that comes with GRASS for details.
  25. @author Martin Landa <landa.martin gmail.com>
  26. @author Anna Kratochvilova <kratochanna gmail.com> (GroupDialog)
  27. """
  28. import os
  29. import sys
  30. import re
  31. from bisect import bisect
  32. import wx
  33. import wx.lib.filebrowsebutton as filebrowse
  34. import wx.lib.mixins.listctrl as listmix
  35. from grass.script import core as grass
  36. from grass.script import task as gtask
  37. from core import globalvar
  38. from core.gcmd import GError, RunCommand, GMessage
  39. from gui_core.gselect import ElementSelect, LocationSelect, MapsetSelect, Select, OgrTypeSelect, GdalSelect, MapsetSelect
  40. from gui_core.forms import GUI
  41. from core.utils import GetListOfMapsets, GetLayerNameFromCmd, GetValidLayerName
  42. from core.settings import UserSettings
  43. from core.debug import Debug
  44. class ElementDialog(wx.Dialog):
  45. def __init__(self, parent, title, label, id = wx.ID_ANY,
  46. etype = False, style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  47. **kwargs):
  48. """!General dialog to choose given element (location, mapset, vector map, etc.)
  49. @param parent window
  50. @param title window title
  51. @param label element label
  52. @param etype show also ElementSelect
  53. """
  54. wx.Dialog.__init__(self, parent, id, title, style = style, **kwargs)
  55. self.etype = etype
  56. self.label = label
  57. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  58. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  59. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  60. self.btnOK.SetDefault()
  61. self.btnOK.Enable(False)
  62. if self.etype:
  63. self.typeSelect = ElementSelect(parent = self.panel,
  64. size = globalvar.DIALOG_GSELECT_SIZE)
  65. self.typeSelect.Bind(wx.EVT_CHOICE, self.OnType)
  66. self.element = None # must be defined
  67. self.__layout()
  68. def PostInit(self):
  69. self.element.SetFocus()
  70. self.element.Bind(wx.EVT_TEXT, self.OnElement)
  71. def OnType(self, event):
  72. """!Select element type"""
  73. if not self.etype:
  74. return
  75. evalue = self.typeSelect.GetValue(event.GetString())
  76. self.element.SetType(evalue)
  77. def OnElement(self, event):
  78. """!Name for vector map layer given"""
  79. if len(event.GetString()) > 0:
  80. self.btnOK.Enable(True)
  81. else:
  82. self.btnOK.Enable(False)
  83. def __layout(self):
  84. """!Do layout"""
  85. self.sizer = wx.BoxSizer(wx.VERTICAL)
  86. self.dataSizer = wx.BoxSizer(wx.VERTICAL)
  87. if self.etype:
  88. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  89. label = _("Type of element:")),
  90. proportion = 0, flag = wx.ALL, border = 1)
  91. self.dataSizer.Add(item = self.typeSelect,
  92. proportion = 0, flag = wx.ALL, border = 1)
  93. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  94. label = self.label),
  95. proportion = 0, flag = wx.ALL, border = 1)
  96. # buttons
  97. btnSizer = wx.StdDialogButtonSizer()
  98. btnSizer.AddButton(self.btnCancel)
  99. btnSizer.AddButton(self.btnOK)
  100. btnSizer.Realize()
  101. self.sizer.Add(item = self.dataSizer, proportion = 1,
  102. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  103. self.sizer.Add(item = btnSizer, proportion = 0,
  104. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  105. def GetElement(self):
  106. """!Return (mapName, overwrite)"""
  107. return self.element.GetValue()
  108. def GetType(self):
  109. """!Get element type"""
  110. return self.element.tcp.GetType()
  111. class LocationDialog(ElementDialog):
  112. """!Dialog used to select location"""
  113. def __init__(self, parent, title = _("Select GRASS location and mapset"), id = wx.ID_ANY):
  114. ElementDialog.__init__(self, parent, title, label = _("Name of GRASS location:"))
  115. self.element = LocationSelect(parent = self.panel, id = wx.ID_ANY,
  116. size = globalvar.DIALOG_GSELECT_SIZE)
  117. self.element1 = MapsetSelect(parent = self.panel, id = wx.ID_ANY,
  118. size = globalvar.DIALOG_GSELECT_SIZE,
  119. setItems = False, skipCurrent = True)
  120. self.PostInit()
  121. self._layout()
  122. self.SetMinSize(self.GetSize())
  123. def _layout(self):
  124. """!Do layout"""
  125. self.dataSizer.Add(self.element, proportion = 0,
  126. flag = wx.EXPAND | wx.ALL, border = 1)
  127. self.dataSizer.Add(wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  128. label = _("Name of mapset:")), proportion = 0,
  129. flag = wx.EXPAND | wx.ALL, border = 1)
  130. self.dataSizer.Add(self.element1, proportion = 0,
  131. flag = wx.EXPAND | wx.ALL, border = 1)
  132. self.panel.SetSizer(self.sizer)
  133. self.sizer.Fit(self)
  134. def OnElement(self, event):
  135. """!Select mapset given location name"""
  136. location = event.GetString()
  137. if location:
  138. dbase = grass.gisenv()['GISDBASE']
  139. self.element1.UpdateItems(dbase = dbase, location = location)
  140. self.element1.SetSelection(0)
  141. mapset = self.element1.GetStringSelection()
  142. if location and mapset:
  143. self.btnOK.Enable(True)
  144. else:
  145. self.btnOK.Enable(False)
  146. def GetValues(self):
  147. """!Get location, mapset"""
  148. return (self.GetElement(), self.element1.GetStringSelection())
  149. class MapsetDialog(ElementDialog):
  150. """!Dialog used to select mapset"""
  151. def __init__(self, parent, title = _("Select mapset in GRASS location"),
  152. location = None, id = wx.ID_ANY):
  153. ElementDialog.__init__(self, parent, title, label = _("Name of mapset:"))
  154. if location:
  155. self.SetTitle(self.GetTitle() + ' <%s>' % location)
  156. else:
  157. self.SetTitle(self.GetTitle() + ' <%s>' % grass.gisenv()['LOCATION_NAME'])
  158. self.element = MapsetSelect(parent = self.panel, id = wx.ID_ANY, skipCurrent = True,
  159. size = globalvar.DIALOG_GSELECT_SIZE)
  160. self.PostInit()
  161. self.__Layout()
  162. self.SetMinSize(self.GetSize())
  163. def __Layout(self):
  164. """!Do layout"""
  165. self.dataSizer.Add(self.element, proportion = 0,
  166. flag = wx.EXPAND | wx.ALL, border = 1)
  167. self.panel.SetSizer(self.sizer)
  168. self.sizer.Fit(self)
  169. def GetMapset(self):
  170. return self.GetElement()
  171. class NewVectorDialog(ElementDialog):
  172. def __init__(self, parent, id = wx.ID_ANY, title = _('Create new vector map'),
  173. disableAdd = False, disableTable = False, showType = False,
  174. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, *kwargs):
  175. """!Dialog for creating new vector map
  176. @param parent parent window
  177. @param id window id
  178. @param title window title
  179. @param disableAdd disable 'add layer' checkbox
  180. @param disableTable disable 'create table' checkbox
  181. @param showType True to show feature type selector (used for creating new empty OGR layers)
  182. @param style window style
  183. @param kwargs other argumentes for ElementDialog
  184. @return dialog instance
  185. """
  186. ElementDialog.__init__(self, parent, title, label = _("Name for new vector map:"))
  187. self.element = Select(parent = self.panel, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  188. type = 'vector', mapsets = [grass.gisenv()['MAPSET'],])
  189. # determine output format
  190. if showType:
  191. self.ftype = OgrTypeSelect(parent = self, panel = self.panel)
  192. else:
  193. self.ftype = None
  194. self.table = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  195. label = _("Create attribute table"))
  196. self.table.SetValue(True)
  197. if disableTable:
  198. self.table.Enable(False)
  199. if showType:
  200. self.keycol = None
  201. else:
  202. self.keycol = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  203. size = globalvar.DIALOG_SPIN_SIZE)
  204. self.keycol.SetValue(UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value'))
  205. if disableTable:
  206. self.keycol.Enable(False)
  207. self.addbox = wx.CheckBox(parent = self.panel,
  208. label = _('Add created map into layer tree'), style = wx.NO_BORDER)
  209. if disableAdd:
  210. self.addbox.SetValue(True)
  211. self.addbox.Enable(False)
  212. else:
  213. self.addbox.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  214. self.table.Bind(wx.EVT_CHECKBOX, self.OnTable)
  215. self.PostInit()
  216. self._layout()
  217. self.SetMinSize(self.GetSize())
  218. def OnMapName(self, event):
  219. """!Name for vector map layer given"""
  220. self.OnElement(event)
  221. def OnTable(self, event):
  222. if self.keycol:
  223. self.keycol.Enable(event.IsChecked())
  224. def _layout(self):
  225. """!Do layout"""
  226. self.dataSizer.Add(item = self.element, proportion = 0,
  227. flag = wx.EXPAND | wx.ALL, border = 1)
  228. if self.ftype:
  229. self.dataSizer.AddSpacer(1)
  230. self.dataSizer.Add(item = self.ftype, proportion = 0,
  231. flag = wx.EXPAND | wx.ALL, border = 1)
  232. self.dataSizer.Add(item = self.table, proportion = 0,
  233. flag = wx.EXPAND | wx.ALL, border = 1)
  234. if self.keycol:
  235. keySizer = wx.BoxSizer(wx.HORIZONTAL)
  236. keySizer.Add(item = wx.StaticText(parent = self.panel, label = _("Key column:")),
  237. proportion = 0,
  238. flag = wx.ALIGN_CENTER_VERTICAL)
  239. keySizer.AddSpacer(10)
  240. keySizer.Add(item = self.keycol, proportion = 0,
  241. flag = wx.ALIGN_RIGHT)
  242. self.dataSizer.Add(item = keySizer, proportion = 1,
  243. flag = wx.EXPAND | wx.ALL, border = 1)
  244. self.dataSizer.AddSpacer(5)
  245. self.dataSizer.Add(item = self.addbox, proportion = 0,
  246. flag = wx.EXPAND | wx.ALL, border = 1)
  247. self.panel.SetSizer(self.sizer)
  248. self.sizer.Fit(self)
  249. def GetName(self, full = False):
  250. """!Get name of vector map to be created
  251. @param full True to get fully qualified name
  252. """
  253. name = self.GetElement()
  254. if full:
  255. if '@' in name:
  256. return name
  257. else:
  258. return name + '@' + grass.gisenv()['MAPSET']
  259. return name.split('@', 1)[0]
  260. def GetKey(self):
  261. """!Get key column name"""
  262. if self.keycol:
  263. return self.keycol.GetValue()
  264. return UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value')
  265. def IsChecked(self, key):
  266. """!Get dialog properties
  267. @param key window key ('add', 'table')
  268. @return True/False
  269. @return None on error
  270. """
  271. if key == 'add':
  272. return self.addbox.IsChecked()
  273. elif key == 'table':
  274. return self.table.IsChecked()
  275. return None
  276. def GetFeatureType(self):
  277. """!Get feature type for OGR
  278. @return feature type as string
  279. @return None for native format
  280. """
  281. if self.ftype:
  282. return self.ftype.GetType()
  283. return None
  284. def CreateNewVector(parent, cmd, title = _('Create new vector map'),
  285. exceptMap = None, log = None,
  286. disableAdd = False, disableTable = False):
  287. """!Create new vector map layer
  288. @param cmd (prog, **kwargs)
  289. @param title window title
  290. @param exceptMap list of maps to be excepted
  291. @param log
  292. @param disableAdd disable 'add layer' checkbox
  293. @param disableTable disable 'create table' checkbox
  294. @return dialog instance
  295. @return None on error
  296. """
  297. vExternalOut = grass.parse_command('v.external.out', flags = 'g')
  298. isNative = vExternalOut['format'] == 'native'
  299. if cmd[0] == 'v.edit' and not isNative:
  300. showType = True
  301. else:
  302. showType = False
  303. dlg = NewVectorDialog(parent, title = title,
  304. disableAdd = disableAdd, disableTable = disableTable,
  305. showType = showType)
  306. if dlg.ShowModal() != wx.ID_OK:
  307. dlg.Destroy()
  308. return None
  309. outmap = dlg.GetName()
  310. key = dlg.GetKey()
  311. if outmap == exceptMap:
  312. GError(parent = parent,
  313. message = _("Unable to create vector map <%s>.") % outmap)
  314. dlg.Destroy()
  315. return None
  316. if dlg.table.IsEnabled() and not key:
  317. GError(parent = parent,
  318. message = _("Invalid or empty key column.\n"
  319. "Unable to create vector map <%s>.") % outmap)
  320. dlg.Destroy()
  321. return
  322. if outmap == '': # should not happen
  323. dlg.Destroy()
  324. return None
  325. # update cmd -> output name defined
  326. cmd[1][cmd[2]] = outmap
  327. if showType:
  328. cmd[1]['type'] = dlg.GetFeatureType()
  329. if isNative:
  330. listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
  331. else:
  332. listOfVectors = RunCommand('v.external',
  333. quiet = True,
  334. parent = parent,
  335. read = True,
  336. flags = 'l',
  337. dsn = vExternalOut['dsn']).splitlines()
  338. overwrite = False
  339. if not UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled') and \
  340. outmap in listOfVectors:
  341. dlgOw = wx.MessageDialog(parent, message = _("Vector map <%s> already exists "
  342. "in the current mapset. "
  343. "Do you want to overwrite it?") % outmap,
  344. caption = _("Overwrite?"),
  345. style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  346. if dlgOw.ShowModal() == wx.ID_YES:
  347. overwrite = True
  348. else:
  349. dlgOw.Destroy()
  350. dlg.Destroy()
  351. return None
  352. if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  353. overwrite = True
  354. ret = RunCommand(prog = cmd[0],
  355. parent = parent,
  356. overwrite = overwrite,
  357. **cmd[1])
  358. if ret != 0:
  359. dlg.Destroy()
  360. return None
  361. if not isNative:
  362. # create link for OGR layers
  363. RunCommand('v.external',
  364. overwrite = overwrite,
  365. parent = parent,
  366. dsn = vExternalOut['dsn'],
  367. layer = outmap)
  368. # create attribute table
  369. if dlg.table.IsEnabled() and dlg.table.IsChecked():
  370. if isNative:
  371. sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
  372. RunCommand('db.connect',
  373. flags = 'c')
  374. Debug.msg(1, "SQL: %s" % sql)
  375. RunCommand('db.execute',
  376. quiet = True,
  377. parent = parent,
  378. input = '-',
  379. stdin = sql)
  380. RunCommand('v.db.connect',
  381. quiet = True,
  382. parent = parent,
  383. map = outmap,
  384. table = outmap,
  385. key = key,
  386. layer = '1')
  387. # TODO: how to deal with attribute tables for OGR layers?
  388. # return fully qualified map name
  389. if '@' not in outmap:
  390. outmap += '@' + grass.gisenv()['MAPSET']
  391. if log:
  392. log.WriteLog(_("New vector map <%s> created") % outmap)
  393. return dlg
  394. class SavedRegion(wx.Dialog):
  395. def __init__(self, parent, id = wx.ID_ANY, title = "", loadsave = 'load',
  396. **kwargs):
  397. """!Loading and saving of display extents to saved region file
  398. @param loadsave load or save region?
  399. """
  400. wx.Dialog.__init__(self, parent, id, title, **kwargs)
  401. self.loadsave = loadsave
  402. self.wind = ''
  403. sizer = wx.BoxSizer(wx.VERTICAL)
  404. box = wx.BoxSizer(wx.HORIZONTAL)
  405. label = wx.StaticText(parent = self, id = wx.ID_ANY)
  406. box.Add(item = label, proportion = 0, flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  407. if loadsave == 'load':
  408. label.SetLabel(_("Load region:"))
  409. selection = Select(parent = self, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  410. type = 'windows')
  411. elif loadsave == 'save':
  412. label.SetLabel(_("Save region:"))
  413. selection = Select(parent = self, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  414. type = 'windows', mapsets = [grass.gisenv()['MAPSET']])
  415. box.Add(item = selection, proportion = 0, flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  416. selection.SetFocus()
  417. selection.Bind(wx.EVT_TEXT, self.OnRegion)
  418. sizer.Add(item = box, proportion = 0, flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
  419. border = 5)
  420. line = wx.StaticLine(parent = self, id = wx.ID_ANY, size = (20, -1), style = wx.LI_HORIZONTAL)
  421. sizer.Add(item = line, proportion = 0,
  422. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, border = 5)
  423. btnsizer = wx.StdDialogButtonSizer()
  424. btn = wx.Button(parent = self, id = wx.ID_OK)
  425. btn.SetDefault()
  426. btnsizer.AddButton(btn)
  427. btn = wx.Button(parent = self, id = wx.ID_CANCEL)
  428. btnsizer.AddButton(btn)
  429. btnsizer.Realize()
  430. sizer.Add(item = btnsizer, proportion = 0, flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  431. self.SetSizer(sizer)
  432. sizer.Fit(self)
  433. self.Layout()
  434. def OnRegion(self, event):
  435. self.wind = event.GetString()
  436. class DecorationDialog(wx.Dialog):
  437. """!Controls setting options and displaying/hiding map overlay
  438. decorations
  439. """
  440. def __init__(self, parent, ovlId, title, cmd, name = None,
  441. pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE,
  442. checktxt = '', ctrltxt = ''):
  443. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  444. self.ovlId = ovlId # PseudoDC id
  445. self.cmd = cmd
  446. self.name = name # overlay name
  447. self.parent = parent # MapFrame
  448. sizer = wx.BoxSizer(wx.VERTICAL)
  449. box = wx.BoxSizer(wx.HORIZONTAL)
  450. self.chkbox = wx.CheckBox(parent = self, id = wx.ID_ANY, label = checktxt)
  451. if self.parent.Map.GetOverlay(self.ovlId) is None:
  452. self.chkbox.SetValue(True)
  453. else:
  454. self.chkbox.SetValue(self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive())
  455. box.Add(item = self.chkbox, proportion = 0,
  456. flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  457. sizer.Add(item = box, proportion = 0,
  458. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  459. box = wx.BoxSizer(wx.HORIZONTAL)
  460. optnbtn = wx.Button(parent = self, id = wx.ID_ANY, label = _("Set options"))
  461. box.Add(item = optnbtn, proportion = 0, flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  462. sizer.Add(item = box, proportion = 0,
  463. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  464. if self.name == 'legend':
  465. box = wx.BoxSizer(wx.HORIZONTAL)
  466. resize = wx.ToggleButton(parent = self, id = wx.ID_ANY, label = _("Set size and position"))
  467. resize.SetToolTipString(_("Click and drag on the map display to set legend"
  468. " size and position and then press OK"))
  469. resize.SetName('resize')
  470. if self.parent.IsPaneShown('3d'):
  471. resize.Disable()
  472. box.Add(item = resize, proportion = 0, flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  473. sizer.Add(item = box, proportion = 0,
  474. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  475. box = wx.BoxSizer(wx.HORIZONTAL)
  476. label = wx.StaticText(parent = self, id = wx.ID_ANY,
  477. label = _("Drag %s with mouse in pointer mode to position.\n"
  478. "Double-click to change options." % ctrltxt))
  479. if self.name == 'legend':
  480. label.SetLabel(label.GetLabel() + _('\nDefine raster map name for legend in '
  481. 'properties dialog.'))
  482. box.Add(item = label, proportion = 0,
  483. flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  484. sizer.Add(item = box, proportion = 0,
  485. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  486. line = wx.StaticLine(parent = self, id = wx.ID_ANY, size = (20,-1), style = wx.LI_HORIZONTAL)
  487. sizer.Add(item = line, proportion = 0,
  488. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  489. # buttons
  490. btnsizer = wx.StdDialogButtonSizer()
  491. self.btnOK = wx.Button(parent = self, id = wx.ID_OK)
  492. self.btnOK.SetDefault()
  493. if self.name == 'legend':
  494. self.btnOK.Enable(False)
  495. btnsizer.AddButton(self.btnOK)
  496. btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
  497. btnsizer.AddButton(btnCancel)
  498. btnsizer.Realize()
  499. sizer.Add(item = btnsizer, proportion = 0,
  500. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  501. #
  502. # bindings
  503. #
  504. self.Bind(wx.EVT_BUTTON, self.OnOptions, optnbtn)
  505. if self.name == 'legend':
  506. self.Bind(wx.EVT_TOGGLEBUTTON, self.OnResize, resize)
  507. self.Bind(wx.EVT_BUTTON, self.OnCancel, btnCancel)
  508. self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOK)
  509. self.SetSizer(sizer)
  510. sizer.Fit(self)
  511. # create overlay if doesn't exist
  512. self._createOverlay()
  513. if len(self.parent.MapWindow.overlays[self.ovlId]['cmd']) > 1:
  514. if name == 'legend':
  515. mapName, found = GetLayerNameFromCmd(self.parent.MapWindow.overlays[self.ovlId]['cmd'])
  516. if found:
  517. # enable 'OK' button
  518. self.btnOK.Enable()
  519. # set title
  520. self.SetTitle(_('Legend of raster map <%s>') % \
  521. mapName)
  522. def _createOverlay(self):
  523. """!Creates overlay"""
  524. if not self.parent.GetMap().GetOverlay(self.ovlId):
  525. self.newOverlay = self.parent.Map.AddOverlay(id = self.ovlId, type = self.name,
  526. command = self.cmd,
  527. l_active = False, l_render = False, l_hidden = True)
  528. prop = { 'layer' : self.newOverlay,
  529. 'params' : None,
  530. 'propwin' : None,
  531. 'cmd' : self.cmd,
  532. 'coords': (0, 0),
  533. 'pdcType': 'image' }
  534. self.parent.MapWindow2D.overlays[self.ovlId] = prop
  535. if self.parent.MapWindow3D:
  536. self.parent.MapWindow3D.overlays[self.ovlId] = prop
  537. else:
  538. if self.parent.MapWindow.overlays[self.ovlId]['propwin'] == None:
  539. return
  540. self.parent.MapWindow.overlays[self.ovlId]['propwin'].get_dcmd = self.GetOptData
  541. def OnOptions(self, event):
  542. """!Sets option for decoration map overlays
  543. """
  544. if self.parent.MapWindow.overlays[self.ovlId]['propwin'] is None:
  545. # build properties dialog
  546. GUI(parent = self.parent).ParseCommand(cmd = self.cmd,
  547. completed = (self.GetOptData, self.name, ''))
  548. else:
  549. if self.parent.MapWindow.overlays[self.ovlId]['propwin'].IsShown():
  550. self.parent.MapWindow.overlays[self.ovlId]['propwin'].SetFocus()
  551. else:
  552. self.parent.MapWindow.overlays[self.ovlId]['propwin'].Show()
  553. def OnResize(self, event):
  554. if self.FindWindowByName('resize').GetValue():
  555. self.parent.MapWindow.SetCursor(self.parent.cursors["cross"])
  556. self.parent.MapWindow.mouse['use'] = 'legend'
  557. self.parent.MapWindow.mouse['box'] = 'box'
  558. self.parent.MapWindow.pen = wx.Pen(colour = 'Black', width = 2, style = wx.SHORT_DASH)
  559. else:
  560. self.parent.MapWindow.SetCursor(self.parent.cursors["default"])
  561. self.parent.MapWindow.mouse['use'] = 'pointer'
  562. def OnCancel(self, event):
  563. """!Cancel dialog"""
  564. if self.name == 'legend' and self.FindWindowByName('resize').GetValue():
  565. self.FindWindowByName('resize').SetValue(False)
  566. self.OnResize(None)
  567. self.parent.dialogs['barscale'] = None
  568. if event and hasattr(self, 'newOverlay'):
  569. self.parent.Map.DeleteOverlay(self.newOverlay)
  570. self.Destroy()
  571. def OnOK(self, event):
  572. """!Button 'OK' pressed"""
  573. # enable or disable overlay
  574. self.parent.Map.GetOverlay(self.ovlId).SetActive(self.chkbox.IsChecked())
  575. # update map
  576. if self.parent.IsPaneShown('3d'):
  577. self.parent.MapWindow.UpdateOverlays()
  578. self.parent.MapWindow.UpdateMap()
  579. # close dialog
  580. self.OnCancel(None)
  581. def GetOptData(self, dcmd, layer, params, propwin):
  582. """!Process decoration layer data"""
  583. # update layer data
  584. if params:
  585. self.parent.MapWindow.overlays[self.ovlId]['params'] = params
  586. if dcmd:
  587. self.parent.MapWindow.overlays[self.ovlId]['cmd'] = dcmd
  588. self.parent.MapWindow.overlays[self.ovlId]['propwin'] = propwin
  589. # change parameters for item in layers list in render.Map
  590. # "Use mouse..." (-m) flag causes GUI freeze and is pointless here, trac #119
  591. try:
  592. self.parent.MapWindow.overlays[self.ovlId]['cmd'].remove('-m')
  593. except ValueError:
  594. pass
  595. self.parent.Map.ChangeOverlay(id = self.ovlId, type = self.name,
  596. command = self.parent.MapWindow.overlays[self.ovlId]['cmd'],
  597. l_active = self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive(),
  598. l_render = False, l_hidden = True)
  599. if self.name == 'legend':
  600. if params and not self.btnOK.IsEnabled():
  601. self.btnOK.Enable()
  602. class TextLayerDialog(wx.Dialog):
  603. """
  604. Controls setting options and displaying/hiding map overlay decorations
  605. """
  606. def __init__(self, parent, ovlId, title, name = 'text',
  607. pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE):
  608. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  609. from wx.lib.expando import ExpandoTextCtrl, EVT_ETC_LAYOUT_NEEDED
  610. self.ovlId = ovlId
  611. self.parent = parent
  612. if self.ovlId in self.parent.MapWindow.textdict.keys():
  613. self.currText = self.parent.MapWindow.textdict[self.ovlId]['text']
  614. self.currFont = self.parent.MapWindow.textdict[self.ovlId]['font']
  615. self.currClr = self.parent.MapWindow.textdict[self.ovlId]['color']
  616. self.currRot = self.parent.MapWindow.textdict[self.ovlId]['rotation']
  617. self.currCoords = self.parent.MapWindow.textdict[self.ovlId]['coords']
  618. self.currBB = self.parent.MapWindow.textdict[self.ovlId]['bbox']
  619. else:
  620. self.currClr = wx.BLACK
  621. self.currText = ''
  622. self.currFont = self.GetFont()
  623. self.currRot = 0.0
  624. self.currCoords = [10, 10]
  625. self.currBB = wx.Rect()
  626. self.sizer = wx.BoxSizer(wx.VERTICAL)
  627. box = wx.GridBagSizer(vgap = 5, hgap = 5)
  628. # show/hide
  629. self.chkbox = wx.CheckBox(parent = self, id = wx.ID_ANY,
  630. label = _('Show text object'))
  631. if self.parent.Map.GetOverlay(self.ovlId) is None:
  632. self.chkbox.SetValue(True)
  633. else:
  634. self.chkbox.SetValue(self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive())
  635. box.Add(item = self.chkbox, span = (1,2),
  636. flag = wx.ALIGN_LEFT|wx.ALL, border = 5,
  637. pos = (0, 0))
  638. # text entry
  639. label = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Enter text:"))
  640. box.Add(item = label,
  641. flag = wx.ALIGN_CENTER_VERTICAL,
  642. pos = (1, 0))
  643. self.textentry = ExpandoTextCtrl(parent = self, id = wx.ID_ANY, value = "", size = (300,-1))
  644. self.textentry.SetFont(self.currFont)
  645. self.textentry.SetForegroundColour(self.currClr)
  646. self.textentry.SetValue(self.currText)
  647. # get rid of unneeded scrollbar when text box first opened
  648. self.textentry.SetClientSize((300,-1))
  649. box.Add(item = self.textentry,
  650. pos = (1, 1))
  651. # rotation
  652. label = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Rotation:"))
  653. box.Add(item = label,
  654. flag = wx.ALIGN_CENTER_VERTICAL,
  655. pos = (2, 0))
  656. self.rotation = wx.SpinCtrl(parent = self, id = wx.ID_ANY, value = "", pos = (30, 50),
  657. size = (75,-1), style = wx.SP_ARROW_KEYS)
  658. self.rotation.SetRange(-360, 360)
  659. self.rotation.SetValue(int(self.currRot))
  660. box.Add(item = self.rotation,
  661. flag = wx.ALIGN_RIGHT,
  662. pos = (2, 1))
  663. # font
  664. fontbtn = wx.Button(parent = self, id = wx.ID_ANY, label = _("Set font"))
  665. box.Add(item = fontbtn,
  666. flag = wx.ALIGN_RIGHT,
  667. pos = (3, 1))
  668. self.sizer.Add(item = box, proportion = 1,
  669. flag = wx.ALL, border = 10)
  670. # note
  671. box = wx.BoxSizer(wx.HORIZONTAL)
  672. label = wx.StaticText(parent = self, id = wx.ID_ANY,
  673. label = _("Drag text with mouse in pointer mode "
  674. "to position.\nDouble-click to change options"))
  675. box.Add(item = label, proportion = 0,
  676. flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  677. self.sizer.Add(item = box, proportion = 0,
  678. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER | wx.ALL, border = 5)
  679. line = wx.StaticLine(parent = self, id = wx.ID_ANY,
  680. size = (20,-1), style = wx.LI_HORIZONTAL)
  681. self.sizer.Add(item = line, proportion = 0,
  682. flag = wx.EXPAND | wx.ALIGN_CENTRE | wx.ALL, border = 5)
  683. btnsizer = wx.StdDialogButtonSizer()
  684. btn = wx.Button(parent = self, id = wx.ID_OK)
  685. btn.SetDefault()
  686. btnsizer.AddButton(btn)
  687. btn = wx.Button(parent = self, id = wx.ID_CANCEL)
  688. btnsizer.AddButton(btn)
  689. btnsizer.Realize()
  690. self.sizer.Add(item = btnsizer, proportion = 0,
  691. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  692. self.SetSizer(self.sizer)
  693. self.sizer.Fit(self)
  694. # bindings
  695. self.Bind(EVT_ETC_LAYOUT_NEEDED, self.OnRefit, self.textentry)
  696. self.Bind(wx.EVT_BUTTON, self.OnSelectFont, fontbtn)
  697. self.Bind(wx.EVT_TEXT, self.OnText, self.textentry)
  698. self.Bind(wx.EVT_SPINCTRL, self.OnRotation, self.rotation)
  699. def OnRefit(self, event):
  700. """!Resize text entry to match text"""
  701. self.sizer.Fit(self)
  702. def OnText(self, event):
  703. """!Change text string"""
  704. self.currText = event.GetString()
  705. def OnRotation(self, event):
  706. """!Change rotation"""
  707. self.currRot = event.GetInt()
  708. event.Skip()
  709. def OnSelectFont(self, event):
  710. """!Change font"""
  711. data = wx.FontData()
  712. data.EnableEffects(True)
  713. data.SetColour(self.currClr) # set colour
  714. data.SetInitialFont(self.currFont)
  715. dlg = wx.FontDialog(self, data)
  716. if dlg.ShowModal() == wx.ID_OK:
  717. data = dlg.GetFontData()
  718. self.currFont = data.GetChosenFont()
  719. self.currClr = data.GetColour()
  720. self.textentry.SetFont(self.currFont)
  721. self.textentry.SetForegroundColour(self.currClr)
  722. self.Layout()
  723. dlg.Destroy()
  724. def GetValues(self):
  725. """!Get text properties"""
  726. return { 'text' : self.currText,
  727. 'font' : self.currFont,
  728. 'color' : self.currClr,
  729. 'rotation' : self.currRot,
  730. 'coords' : self.currCoords,
  731. 'active' : self.chkbox.IsChecked() }
  732. class GroupDialog(wx.Dialog):
  733. """!Dialog for creating/editing groups"""
  734. def __init__(self, parent = None, defaultGroup = None,
  735. title = _("Create or edit imagery groups"),
  736. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  737. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  738. style = style, **kwargs)
  739. self.parent = parent
  740. self.defaultGroup = defaultGroup
  741. self.currentGroup = self.defaultGroup
  742. self.groupChanged = False
  743. self.bodySizer = self._createDialogBody()
  744. # buttons
  745. btnOk = wx.Button(parent = self, id = wx.ID_OK)
  746. btnApply = wx.Button(parent = self, id = wx.ID_APPLY)
  747. btnClose = wx.Button(parent = self, id = wx.ID_CANCEL)
  748. btnOk.SetToolTipString(_("Apply changes to selected group and close dialog"))
  749. btnApply.SetToolTipString(_("Apply changes to selected group"))
  750. btnClose.SetToolTipString(_("Close dialog, changes are not applied"))
  751. btnOk.SetDefault()
  752. # sizers & do layout
  753. # btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  754. # btnSizer.Add(item = btnClose, proportion = 0,
  755. # flag = wx.RIGHT | wx.ALIGN_RIGHT | wx.EXPAND, border = 5)
  756. # btnSizer.Add(item = btnApply, proportion = 0,
  757. # flag = wx.LEFT, border = 5)
  758. btnSizer = wx.StdDialogButtonSizer()
  759. btnSizer.AddButton(btnOk)
  760. btnSizer.AddButton(btnApply)
  761. btnSizer.AddButton(btnClose)
  762. btnSizer.Realize()
  763. mainSizer = wx.BoxSizer(wx.VERTICAL)
  764. mainSizer.Add(item = self.bodySizer, proportion = 1,
  765. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 10)
  766. mainSizer.Add(item = wx.StaticLine(parent = self, id = wx.ID_ANY,
  767. style = wx.LI_HORIZONTAL), proportion = 0,
  768. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 10)
  769. mainSizer.Add(item = btnSizer, proportion = 0,
  770. flag = wx.ALL | wx.ALIGN_RIGHT, border = 10)
  771. self.SetSizer(mainSizer)
  772. mainSizer.Fit(self)
  773. btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  774. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  775. btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  776. # set dialog min size
  777. self.SetMinSize(self.GetSize())
  778. def _createDialogBody(self):
  779. bodySizer = wx.BoxSizer(wx.VERTICAL)
  780. # group selection
  781. bodySizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
  782. label = _("Select the group you want to edit or "
  783. "enter name of new group:")),
  784. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP, border = 10)
  785. self.groupSelect = Select(parent = self, type = 'group',
  786. mapsets = [grass.gisenv()['MAPSET']],
  787. size = globalvar.DIALOG_GSELECT_SIZE) # searchpath?
  788. bodySizer.Add(item = self.groupSelect, flag = wx.TOP | wx.EXPAND, border = 5)
  789. bodySizer.AddSpacer(10)
  790. # layers in group
  791. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Layers in selected group:")),
  792. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM, border = 5)
  793. gridSizer = wx.GridBagSizer(vgap = 5, hgap = 5)
  794. gridSizer.AddGrowableCol(0)
  795. self.layerBox = wx.ListBox(parent = self, id = wx.ID_ANY, size = (-1, 150),
  796. style = wx.LB_MULTIPLE | wx.LB_NEEDED_SB)
  797. gridSizer.Add(item = self.layerBox, pos = (0, 0), span = (2, 1), flag = wx.EXPAND)
  798. self.addLayer = wx.Button(self, id = wx.ID_ADD)
  799. self.addLayer.SetToolTipString(_("Select map layers and add them to the list."))
  800. gridSizer.Add(item = self.addLayer, pos = (0, 1))
  801. self.removeLayer = wx.Button(self, id = wx.ID_REMOVE)
  802. self.removeLayer.SetToolTipString(_("Remove selected layer(s) from list."))
  803. gridSizer.Add(item = self.removeLayer, pos = (1, 1))
  804. bodySizer.Add(item = gridSizer, proportion = 1, flag = wx.EXPAND)
  805. self.infoLabel = wx.StaticText(parent = self, id = wx.ID_ANY)
  806. bodySizer.Add(item = self.infoLabel,
  807. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM, border = 5)
  808. self.subGroup = wx.CheckBox(parent = self, id = wx.ID_ANY,
  809. label = _("Define also sub-group (same name as group)"))
  810. bodySizer.Add(item = self.subGroup, flag = wx.BOTTOM | wx.EXPAND, border = 5)
  811. # bindings
  812. self.groupSelect.GetTextCtrl().Bind(wx.EVT_TEXT, self.OnGroupSelected)
  813. self.addLayer.Bind(wx.EVT_BUTTON, self.OnAddLayer)
  814. self.removeLayer.Bind(wx.EVT_BUTTON, self.OnRemoveLayer)
  815. if self.defaultGroup:
  816. self.groupSelect.SetValue(self.defaultGroup)
  817. return bodySizer
  818. def OnAddLayer(self, event):
  819. """!Add new layer to listbox"""
  820. dlg = MapLayersDialog(parent = self, title = _("Add selected map layers into group"),
  821. mapType = 'raster', selectAll = False,
  822. fullyQualified = True, showFullyQualified = False)
  823. if dlg.ShowModal() != wx.ID_OK:
  824. dlg.Destroy()
  825. return
  826. layers = dlg.GetMapLayers()
  827. for layer in layers:
  828. if layer not in self.GetLayers():
  829. self.layerBox.Append(layer)
  830. self.groupChanged = True
  831. def OnRemoveLayer(self, event):
  832. """!Remove layer from listbox"""
  833. while self.layerBox.GetSelections():
  834. sel = self.layerBox.GetSelections()[0]
  835. self.layerBox.Delete(sel)
  836. self.groupChanged = True
  837. def GetLayers(self):
  838. """!Get layers"""
  839. return self.layerBox.GetItems()
  840. def OnGroupSelected(self, event):
  841. """!Text changed in group selector"""
  842. # callAfter must be called to close popup before other actions
  843. wx.CallAfter(self.GroupSelected)
  844. def GroupSelected(self):
  845. """!Group was selected, check if changes were apllied"""
  846. group = self.GetSelectedGroup()
  847. if self.groupChanged:
  848. dlg = wx.MessageDialog(self, message = _("Group <%s> was changed, "
  849. "do you want to apply changes?") % self.currentGroup,
  850. caption = _("Unapplied changes"),
  851. style = wx.YES_NO | wx.ICON_QUESTION | wx.YES_DEFAULT)
  852. if dlg.ShowModal() == wx.ID_YES:
  853. self.ApplyChanges(showResult = True)
  854. dlg.Destroy()
  855. groups = self.GetExistGroups()
  856. if group in groups:
  857. self.ShowGroupLayers(self.GetGroupLayers(group))
  858. self.currentGroup = group
  859. self.groupChanged = False
  860. self.ClearNotification()
  861. def ShowGroupLayers(self, mapList):
  862. """!Show map layers in currently selected group"""
  863. self.layerBox.Set(mapList)
  864. def EditGroup(self, group):
  865. """!Edit selected group"""
  866. layersNew = self.GetLayers()
  867. layersOld = self.GetGroupLayers(group)
  868. add = []
  869. remove = []
  870. for layerNew in layersNew:
  871. if layerNew not in layersOld:
  872. add.append(layerNew)
  873. for layerOld in layersOld:
  874. if layerOld not in layersNew:
  875. remove.append(layerOld)
  876. kwargs = {}
  877. if self.subGroup.IsChecked():
  878. kwargs['subgroup'] = group
  879. ret = None
  880. if remove:
  881. ret = RunCommand('i.group',
  882. parent = self,
  883. group = group,
  884. flags = 'r',
  885. input = ','.join(remove),
  886. **kwargs)
  887. if add:
  888. ret = RunCommand('i.group',
  889. parent = self,
  890. group = group,
  891. input = ','.join(add),
  892. **kwargs)
  893. return ret
  894. def CreateNewGroup(self, group):
  895. """!Create new group"""
  896. layers = self.GetLayers()
  897. kwargs = {}
  898. if self.subGroup.IsChecked():
  899. kwargs['subgroup'] = group
  900. return RunCommand('i.group',
  901. parent = self,
  902. group = group,
  903. input = layers,
  904. **kwargs)
  905. def GetExistGroups(self):
  906. """!Returns existing groups in current mapset"""
  907. return grass.list_grouped('group')[grass.gisenv()['MAPSET']]
  908. def ShowResult(self, group, returnCode, create):
  909. """!Show if operation was successfull."""
  910. group += '@' + grass.gisenv()['MAPSET']
  911. if returnCode is None:
  912. label = _("No changes to apply in group <%s>.") % group
  913. elif returnCode == 0:
  914. if create:
  915. label = _("Group <%s> was successfully created.") % group
  916. else:
  917. label = _("Group <%s> was successfully changed.") % group
  918. else:
  919. if create:
  920. label = _("Creating of new group <%s> failed.") % group
  921. else:
  922. label = _("Changing of group <%s> failed.") % group
  923. self.infoLabel.SetLabel(label)
  924. wx.FutureCall(4000, self.ClearNotification)
  925. def GetSelectedGroup(self):
  926. """!Return currently selected group (without mapset)"""
  927. return self.groupSelect.GetValue().split('@')[0]
  928. def GetGroupLayers(self, group):
  929. """!Get layers in group"""
  930. res = RunCommand('i.group',
  931. parent = self,
  932. flags = 'g',
  933. group = group,
  934. read = True).strip()
  935. if res.split('\n')[0]:
  936. return res.split('\n')
  937. return []
  938. def ClearNotification(self):
  939. """!Clear notification string"""
  940. self.infoLabel.SetLabel("")
  941. def ApplyChanges(self, showResult):
  942. """!Create or edit group"""
  943. group = self.currentGroup
  944. if not group:
  945. GMessage(parent = self,
  946. message = _("No group selected."))
  947. return False
  948. groups = self.GetExistGroups()
  949. if group in groups:
  950. ret = self.EditGroup(group)
  951. self.ShowResult(group = group, returnCode = ret, create = False)
  952. else:
  953. ret = self.CreateNewGroup(group)
  954. self.ShowResult(group = group, returnCode = ret, create = True)
  955. self.groupChanged = False
  956. return True
  957. def OnApply(self, event):
  958. """!Apply changes"""
  959. self.ApplyChanges(showResult = True)
  960. def OnOk(self, event):
  961. """!Apply changes and close dialog"""
  962. if self.ApplyChanges(showResult = False):
  963. self.OnClose(event)
  964. def OnClose(self, event):
  965. """!Close dialog"""
  966. if not self.IsModal():
  967. self.Destroy()
  968. event.Skip()
  969. class MapLayersDialog(wx.Dialog):
  970. def __init__(self, parent, title, modeler = False,
  971. mapType = None, selectAll = True, fullyQualified = True, showFullyQualified = True,
  972. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  973. """!Dialog for selecting map layers (raster, vector)
  974. @param mapType type of map (if None: raster, vector, 3d raster, if one only: selects it and disables selection)
  975. @param selectAll all/none maps should be selected by default
  976. @param fullyQualified True if dialog should return full map names by default
  977. @param showFullyQualified True to show 'fullyQualified' checkbox, otherwise hide it
  978. """
  979. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  980. style = style, **kwargs)
  981. self.parent = parent # GMFrame or ?
  982. self.mapType = mapType
  983. self.selectAll = selectAll
  984. # dialog body
  985. self.bodySizer = self._createDialogBody()
  986. # update list of layer to be loaded
  987. self.map_layers = [] # list of map layers (full list type/mapset)
  988. self.LoadMapLayers(self.GetLayerType(cmd = True),
  989. self.mapset.GetStringSelection())
  990. self.fullyQualified = wx.CheckBox(parent = self, id = wx.ID_ANY,
  991. label = _("Use fully-qualified map names"))
  992. self.fullyQualified.SetValue(fullyQualified)
  993. self.fullyQualified.Show(showFullyQualified)
  994. self.dseries = None
  995. if modeler:
  996. self.dseries = wx.CheckBox(parent = self, id = wx.ID_ANY,
  997. label = _("Dynamic series (%s)") % 'g.mlist')
  998. self.dseries.SetValue(False)
  999. # buttons
  1000. btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
  1001. btnOk = wx.Button(parent = self, id = wx.ID_OK)
  1002. btnOk.SetDefault()
  1003. # sizers & do layout
  1004. btnSizer = wx.StdDialogButtonSizer()
  1005. btnSizer.AddButton(btnCancel)
  1006. btnSizer.AddButton(btnOk)
  1007. btnSizer.Realize()
  1008. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1009. mainSizer.Add(item = self.bodySizer, proportion = 1,
  1010. flag = wx.EXPAND | wx.ALL, border = 5)
  1011. mainSizer.Add(item = self.fullyQualified, proportion = 0,
  1012. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1013. if self.dseries:
  1014. mainSizer.Add(item = self.dseries, proportion = 0,
  1015. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1016. mainSizer.Add(item = btnSizer, proportion = 0,
  1017. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  1018. self.SetSizer(mainSizer)
  1019. mainSizer.Fit(self)
  1020. # set dialog min size
  1021. self.SetMinSize(self.GetSize())
  1022. def _createDialogBody(self):
  1023. bodySizer = wx.GridBagSizer(vgap = 3, hgap = 3)
  1024. bodySizer.AddGrowableCol(1)
  1025. bodySizer.AddGrowableRow(3)
  1026. # layer type
  1027. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Map type:")),
  1028. flag = wx.ALIGN_CENTER_VERTICAL,
  1029. pos = (0,0))
  1030. self.layerType = wx.Choice(parent = self, id = wx.ID_ANY,
  1031. choices = [_('raster'), _('3D raster'), _('vector')], size = (100,-1))
  1032. if self.mapType:
  1033. self.layerType.SetStringSelection(self.mapType)
  1034. self.layerType.Disable()
  1035. else:
  1036. self.layerType.SetSelection(0)
  1037. bodySizer.Add(item = self.layerType,
  1038. pos = (0,1))
  1039. # select toggle
  1040. self.toggle = wx.CheckBox(parent = self, id = wx.ID_ANY,
  1041. label = _("Select toggle"))
  1042. self.toggle.SetValue(self.selectAll)
  1043. bodySizer.Add(item = self.toggle,
  1044. flag = wx.ALIGN_CENTER_VERTICAL,
  1045. pos = (0,2))
  1046. # mapset filter
  1047. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Mapset:")),
  1048. flag = wx.ALIGN_CENTER_VERTICAL,
  1049. pos = (1,0))
  1050. self.mapset = MapsetSelect(parent = self, searchPath = True)
  1051. self.mapset.SetStringSelection(grass.gisenv()['MAPSET'])
  1052. bodySizer.Add(item = self.mapset,
  1053. pos = (1,1), span = (1, 2))
  1054. # map name filter
  1055. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Pattern:")),
  1056. flag = wx.ALIGN_CENTER_VERTICAL,
  1057. pos = (2,0))
  1058. self.filter = wx.TextCtrl(parent = self, id = wx.ID_ANY,
  1059. value = "",
  1060. size = (250,-1))
  1061. bodySizer.Add(item = self.filter,
  1062. flag = wx.EXPAND,
  1063. pos = (2,1), span = (1, 2))
  1064. # layer list
  1065. bodySizer.Add(item = wx.StaticText(parent = self, label = _("List of maps:")),
  1066. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_TOP,
  1067. pos = (3,0))
  1068. self.layers = wx.CheckListBox(parent = self, id = wx.ID_ANY,
  1069. size = (250, 100),
  1070. choices = [])
  1071. bodySizer.Add(item = self.layers,
  1072. flag = wx.EXPAND,
  1073. pos = (3,1), span = (1, 2))
  1074. # bindings
  1075. self.layerType.Bind(wx.EVT_CHOICE, self.OnChangeParams)
  1076. self.mapset.Bind(wx.EVT_COMBOBOX, self.OnChangeParams)
  1077. self.layers.Bind(wx.EVT_RIGHT_DOWN, self.OnMenu)
  1078. self.filter.Bind(wx.EVT_TEXT, self.OnFilter)
  1079. self.toggle.Bind(wx.EVT_CHECKBOX, self.OnToggle)
  1080. return bodySizer
  1081. def LoadMapLayers(self, type, mapset):
  1082. """!Load list of map layers
  1083. @param type layer type ('raster' or 'vector')
  1084. @param mapset mapset name
  1085. """
  1086. self.map_layers = grass.mlist_grouped(type = type)[mapset]
  1087. self.layers.Set(self.map_layers)
  1088. # check all items by default
  1089. for item in range(self.layers.GetCount()):
  1090. self.layers.Check(item, check = self.selectAll)
  1091. def OnChangeParams(self, event):
  1092. """!Filter parameters changed by user"""
  1093. # update list of layer to be loaded
  1094. self.LoadMapLayers(self.GetLayerType(cmd = True),
  1095. self.mapset.GetStringSelection())
  1096. event.Skip()
  1097. def OnMenu(self, event):
  1098. """!Table description area, context menu"""
  1099. if not hasattr(self, "popupID1"):
  1100. self.popupDataID1 = wx.NewId()
  1101. self.popupDataID2 = wx.NewId()
  1102. self.popupDataID3 = wx.NewId()
  1103. self.Bind(wx.EVT_MENU, self.OnSelectAll, id = self.popupDataID1)
  1104. self.Bind(wx.EVT_MENU, self.OnSelectInvert, id = self.popupDataID2)
  1105. self.Bind(wx.EVT_MENU, self.OnDeselectAll, id = self.popupDataID3)
  1106. # generate popup-menu
  1107. menu = wx.Menu()
  1108. menu.Append(self.popupDataID1, _("Select all"))
  1109. menu.Append(self.popupDataID2, _("Invert selection"))
  1110. menu.Append(self.popupDataID3, _("Deselect all"))
  1111. self.PopupMenu(menu)
  1112. menu.Destroy()
  1113. def OnSelectAll(self, event):
  1114. """!Select all map layer from list"""
  1115. for item in range(self.layers.GetCount()):
  1116. self.layers.Check(item, True)
  1117. def OnSelectInvert(self, event):
  1118. """!Invert current selection"""
  1119. for item in range(self.layers.GetCount()):
  1120. if self.layers.IsChecked(item):
  1121. self.layers.Check(item, False)
  1122. else:
  1123. self.layers.Check(item, True)
  1124. def OnDeselectAll(self, event):
  1125. """!Select all map layer from list"""
  1126. for item in range(self.layers.GetCount()):
  1127. self.layers.Check(item, False)
  1128. def OnFilter(self, event):
  1129. """!Apply filter for map names"""
  1130. if len(event.GetString()) == 0:
  1131. self.layers.Set(self.map_layers)
  1132. return
  1133. list = []
  1134. for layer in self.map_layers:
  1135. try:
  1136. if re.compile('^' + event.GetString()).search(layer):
  1137. list.append(layer)
  1138. except:
  1139. pass
  1140. self.layers.Set(list)
  1141. self.OnSelectAll(None)
  1142. event.Skip()
  1143. def OnToggle(self, event):
  1144. """!Select toggle (check or uncheck all layers)"""
  1145. check = event.Checked()
  1146. for item in range(self.layers.GetCount()):
  1147. self.layers.Check(item, check)
  1148. event.Skip()
  1149. def GetMapLayers(self):
  1150. """!Return list of checked map layers"""
  1151. layerNames = []
  1152. for indx in self.layers.GetSelections():
  1153. # layers.append(self.layers.GetStringSelec(indx))
  1154. pass
  1155. fullyQualified = self.fullyQualified.IsChecked()
  1156. mapset = self.mapset.GetStringSelection()
  1157. for item in range(self.layers.GetCount()):
  1158. if not self.layers.IsChecked(item):
  1159. continue
  1160. if fullyQualified:
  1161. layerNames.append(self.layers.GetString(item) + '@' + mapset)
  1162. else:
  1163. layerNames.append(self.layers.GetString(item))
  1164. return layerNames
  1165. def GetLayerType(self, cmd = False):
  1166. """!Get selected layer type
  1167. @param cmd True for g.mlist
  1168. """
  1169. if not cmd:
  1170. return self.layerType.GetStringSelection()
  1171. sel = self.layerType.GetSelection()
  1172. if sel == 0:
  1173. ltype = 'rast'
  1174. elif sel == 1:
  1175. ltype = 'rast3d'
  1176. else:
  1177. ltype = 'vect'
  1178. return ltype
  1179. def GetDSeries(self):
  1180. """!Used by modeler only
  1181. @return g.mlist command
  1182. """
  1183. if not self.dseries or not self.dseries.IsChecked():
  1184. return ''
  1185. cond = 'map in `g.mlist type=%s ' % self.GetLayerType(cmd = True)
  1186. patt = self.filter.GetValue()
  1187. if patt:
  1188. cond += 'pattern=%s ' % patt
  1189. cond += 'mapset=%s`' % self.mapset.GetStringSelection()
  1190. return cond
  1191. class ImportDialog(wx.Dialog):
  1192. """!Dialog for bulk import of various data (base class)"""
  1193. def __init__(self, parent, itype,
  1194. id = wx.ID_ANY, title = _("Multiple import"),
  1195. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
  1196. self.parent = parent # GMFrame
  1197. self.importType = itype
  1198. self.options = dict() # list of options
  1199. self.commandId = -1 # id of running command
  1200. wx.Dialog.__init__(self, parent, id, title, style = style,
  1201. name = "MultiImportDialog")
  1202. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1203. self.layerBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1204. label = _(" List of %s layers ") % self.importType.upper())
  1205. #
  1206. # list of layers
  1207. #
  1208. columns = [_('Layer id'),
  1209. _('Layer name'),
  1210. _('Name for GRASS map (editable)')]
  1211. if itype == 'ogr':
  1212. columns.insert(2, _('Feature type'))
  1213. self.list = LayersList(parent = self.panel, columns = columns)
  1214. self.list.LoadData()
  1215. self.optionBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1216. label = "%s" % _("Options"))
  1217. cmd = self._getCommand()
  1218. task = gtask.parse_interface(cmd)
  1219. for f in task.get_options()['flags']:
  1220. name = f.get('name', '')
  1221. desc = f.get('label', '')
  1222. if not desc:
  1223. desc = f.get('description', '')
  1224. if not name and not desc:
  1225. continue
  1226. if cmd == 'r.in.gdal' and name not in ('o', 'e', 'l', 'k'):
  1227. continue
  1228. elif cmd == 'r.external' and name not in ('o', 'e', 'r', 'h', 'v'):
  1229. continue
  1230. elif cmd == 'v.in.ogr' and name not in ('c', 'z', 't', 'o', 'r', 'e', 'w'):
  1231. continue
  1232. elif cmd == 'v.external' and name not in ('b'):
  1233. continue
  1234. elif cmd == 'v.in.dxf' and name not in ('e', 't', 'b', 'f', 'i'):
  1235. continue
  1236. self.options[name] = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  1237. label = desc)
  1238. self.overwrite = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  1239. label = _("Allow output files to overwrite existing files"))
  1240. self.overwrite.SetValue(UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'))
  1241. self.add = wx.CheckBox(parent = self.panel, id = wx.ID_ANY)
  1242. #
  1243. # buttons
  1244. #
  1245. # cancel
  1246. self.btn_cancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1247. self.btn_cancel.SetToolTipString(_("Close dialog"))
  1248. self.btn_cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  1249. # run
  1250. self.btn_run = wx.Button(parent = self.panel, id = wx.ID_OK, label = _("&Import"))
  1251. self.btn_run.SetToolTipString(_("Import selected layers"))
  1252. self.btn_run.SetDefault()
  1253. self.btn_run.Enable(False)
  1254. self.btn_run.Bind(wx.EVT_BUTTON, self.OnRun)
  1255. # run command dialog
  1256. self.btn_cmd = wx.Button(parent = self.panel, id = wx.ID_ANY,
  1257. label = _("Command dialog"))
  1258. self.btn_cmd.Bind(wx.EVT_BUTTON, self.OnCmdDialog)
  1259. def doLayout(self):
  1260. """!Do layout"""
  1261. dialogSizer = wx.BoxSizer(wx.VERTICAL)
  1262. # dsn input
  1263. dialogSizer.Add(item = self.dsnInput, proportion = 0,
  1264. flag = wx.EXPAND)
  1265. #
  1266. # list of DXF layers
  1267. #
  1268. layerSizer = wx.StaticBoxSizer(self.layerBox, wx.HORIZONTAL)
  1269. layerSizer.Add(item = self.list, proportion = 1,
  1270. flag = wx.ALL | wx.EXPAND, border = 5)
  1271. dialogSizer.Add(item = layerSizer, proportion = 1,
  1272. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border = 5)
  1273. # options
  1274. optionSizer = wx.StaticBoxSizer(self.optionBox, wx.VERTICAL)
  1275. for key in self.options.keys():
  1276. optionSizer.Add(item = self.options[key], proportion = 0)
  1277. dialogSizer.Add(item = optionSizer, proportion = 0,
  1278. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border = 5)
  1279. dialogSizer.Add(item = self.overwrite, proportion = 0,
  1280. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1281. dialogSizer.Add(item = self.add, proportion = 0,
  1282. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1283. #
  1284. # buttons
  1285. #
  1286. btnsizer = wx.BoxSizer(orient = wx.HORIZONTAL)
  1287. btnsizer.Add(item = self.btn_cmd, proportion = 0,
  1288. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1289. border = 10)
  1290. btnsizer.Add(item = self.btn_cancel, proportion = 0,
  1291. flag = wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,
  1292. border = 10)
  1293. btnsizer.Add(item = self.btn_run, proportion = 0,
  1294. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1295. border = 10)
  1296. dialogSizer.Add(item = btnsizer, proportion = 0,
  1297. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM | wx.ALIGN_RIGHT,
  1298. border = 10)
  1299. # dialogSizer.SetSizeHints(self.panel)
  1300. self.panel.SetAutoLayout(True)
  1301. self.panel.SetSizer(dialogSizer)
  1302. dialogSizer.Fit(self.panel)
  1303. # auto-layout seems not work here - FIXME
  1304. size = wx.Size(globalvar.DIALOG_GSELECT_SIZE[0] + 225, 550)
  1305. self.SetMinSize(size)
  1306. self.SetSize((size.width, size.height + 100))
  1307. # width = self.GetSize()[0]
  1308. # self.list.SetColumnWidth(col = 1, width = width / 2 - 50)
  1309. self.Layout()
  1310. def _getCommand(self):
  1311. """!Get command"""
  1312. return ''
  1313. def OnCancel(self, event = None):
  1314. """!Close dialog"""
  1315. self.Close()
  1316. def OnRun(self, event):
  1317. """!Import/Link data (each layes as separate vector map)"""
  1318. pass
  1319. def OnCmdDialog(self, event):
  1320. """!Show command dialog"""
  1321. pass
  1322. def AddLayers(self, returncode, cmd = None):
  1323. """!Add imported/linked layers into layer tree"""
  1324. self.commandId += 1
  1325. if not self.add.IsChecked() or returncode != 0:
  1326. return
  1327. maptree = self.parent.curr_page.maptree
  1328. layer, output = self.list.GetLayers()[self.commandId]
  1329. if '@' not in output:
  1330. name = output + '@' + grass.gisenv()['MAPSET']
  1331. else:
  1332. name = output
  1333. # add imported layers into layer tree
  1334. if self.importType == 'gdal':
  1335. cmd = ['d.rast',
  1336. 'map=%s' % name]
  1337. if UserSettings.Get(group = 'cmd', key = 'rasterOverlay', subkey = 'enabled'):
  1338. cmd.append('-o')
  1339. item = maptree.AddLayer(ltype = 'raster',
  1340. lname = name, lchecked = False,
  1341. lcmd = cmd)
  1342. else:
  1343. item = maptree.AddLayer(ltype = 'vector',
  1344. lname = name, lchecked = False,
  1345. lcmd = ['d.vect',
  1346. 'map=%s' % name])
  1347. maptree.mapdisplay.MapWindow.ZoomToMap()
  1348. def OnAbort(self, event):
  1349. """!Abort running import
  1350. @todo not yet implemented
  1351. """
  1352. pass
  1353. class GdalImportDialog(ImportDialog):
  1354. def __init__(self, parent, ogr = False, link = False):
  1355. """!Dialog for bulk import of various raster/vector data
  1356. @param parent parent window
  1357. @param ogr True for OGR (vector) otherwise GDAL (raster)
  1358. @param link True for linking data otherwise importing data
  1359. """
  1360. self.link = link
  1361. self.ogr = ogr
  1362. if ogr:
  1363. ImportDialog.__init__(self, parent, itype = 'ogr')
  1364. if link:
  1365. self.SetTitle(_("Link external vector data"))
  1366. else:
  1367. self.SetTitle(_("Import vector data"))
  1368. else:
  1369. ImportDialog.__init__(self, parent, itype = 'gdal')
  1370. if link:
  1371. self.SetTitle(_("Link external raster data"))
  1372. else:
  1373. self.SetTitle(_("Import raster data"))
  1374. self.dsnInput = GdalSelect(parent = self, panel = self.panel, ogr = ogr)
  1375. if link:
  1376. self.add.SetLabel(_("Add linked layers into layer tree"))
  1377. else:
  1378. self.add.SetLabel(_("Add imported layers into layer tree"))
  1379. self.add.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  1380. if link:
  1381. self.btn_run.SetLabel(_("&Link"))
  1382. self.btn_run.SetToolTipString(_("Link selected layers"))
  1383. if ogr:
  1384. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'v.external')
  1385. else:
  1386. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'r.external')
  1387. else:
  1388. self.btn_run.SetLabel(_("&Import"))
  1389. self.btn_run.SetToolTipString(_("Import selected layers"))
  1390. if ogr:
  1391. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'v.in.ogr')
  1392. else:
  1393. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'r.in.gdal')
  1394. self.doLayout()
  1395. def OnRun(self, event):
  1396. """!Import/Link data (each layes as separate vector map)"""
  1397. data = self.list.GetLayers()
  1398. # hide dialog
  1399. self.Hide()
  1400. dsn = self.dsnInput.GetDsn()
  1401. ext = self.dsnInput.GetFormatExt()
  1402. for layer, output in data:
  1403. if self.importType == 'ogr':
  1404. if ext and layer.rfind(ext) > -1:
  1405. layer = layer.replace('.' + ext, '')
  1406. if self.link:
  1407. cmd = ['v.external',
  1408. 'dsn=%s' % dsn,
  1409. 'output=%s' % output,
  1410. 'layer=%s' % layer]
  1411. else:
  1412. cmd = ['v.in.ogr',
  1413. 'dsn=%s' % dsn,
  1414. 'layer=%s' % layer,
  1415. 'output=%s' % output]
  1416. else: # gdal
  1417. if self.dsnInput.GetType() == 'dir':
  1418. idsn = os.path.join(dsn, layer)
  1419. else:
  1420. idsn = dsn
  1421. if self.link:
  1422. cmd = ['r.external',
  1423. 'input=%s' % idsn,
  1424. 'output=%s' % output]
  1425. else:
  1426. cmd = ['r.in.gdal',
  1427. 'input=%s' % idsn,
  1428. 'output=%s' % output]
  1429. if self.overwrite.IsChecked():
  1430. cmd.append('--overwrite')
  1431. for key in self.options.keys():
  1432. if self.options[key].IsChecked():
  1433. cmd.append('-%s' % key)
  1434. if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  1435. cmd.append('--overwrite')
  1436. # run in Layer Manager
  1437. self.parent.goutput.RunCmd(cmd, switchPage = True,
  1438. onDone = self.AddLayers)
  1439. self.OnCancel()
  1440. def _getCommand(self):
  1441. """!Get command"""
  1442. if self.link:
  1443. if self.ogr:
  1444. return 'v.external'
  1445. else:
  1446. return 'r.external'
  1447. else:
  1448. if self.ogr:
  1449. return 'v.in.ogr'
  1450. else:
  1451. return 'r.in.gdal'
  1452. return ''
  1453. def OnCmdDialog(self, event):
  1454. """!Show command dialog"""
  1455. name = self._getCommand()
  1456. GUI(parent = self, modal = False).ParseCommand(cmd = [name])
  1457. class GdalOutputDialog(wx.Dialog):
  1458. def __init__(self, parent, id = wx.ID_ANY, ogr = False,
  1459. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, *kwargs):
  1460. """!Dialog for setting output format for rasters/vectors
  1461. @param parent parent window
  1462. @param id window id
  1463. @param ogr True for OGR (vector) otherwise GDAL (raster)
  1464. @param style window style
  1465. @param *kwargs other wx.Dialog's arguments
  1466. """
  1467. self.parent = parent # GMFrame
  1468. self.ogr = ogr
  1469. wx.Dialog.__init__(self, parent, id = id, style = style, *kwargs)
  1470. if self.ogr:
  1471. self.SetTitle(_("Define output format for vector data"))
  1472. else:
  1473. self.SetTitle(_("Define output format for raster data"))
  1474. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1475. # buttons
  1476. self.btnCmd = wx.Button(parent = self.panel, id = wx.ID_ANY,
  1477. label = _("Command dialog"))
  1478. self.btnCmd.Bind(wx.EVT_BUTTON, self.OnCmdDialog)
  1479. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1480. self.btnCancel.SetToolTipString(_("Close dialog"))
  1481. self.btnOk = wx.Button(parent = self.panel, id = wx.ID_OK)
  1482. self.btnOk.SetToolTipString(_("Set external format and close dialog"))
  1483. self.btnOk.SetDefault()
  1484. self.btnOk.Enable(False)
  1485. self.dsnInput = GdalSelect(parent = self, panel = self.panel,
  1486. ogr = ogr,
  1487. exclude = ['file', 'protocol'], dest = True)
  1488. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel)
  1489. self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOk)
  1490. self._layout()
  1491. def _layout(self):
  1492. dialogSizer = wx.BoxSizer(wx.VERTICAL)
  1493. dialogSizer.Add(item = self.dsnInput, proportion = 0,
  1494. flag = wx.EXPAND)
  1495. btnSizer = wx.BoxSizer(orient = wx.HORIZONTAL)
  1496. btnSizer.Add(item = self.btnCmd, proportion = 0,
  1497. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1498. border = 10)
  1499. btnSizer.Add(item = self.btnCancel, proportion = 0,
  1500. flag = wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,
  1501. border = 10)
  1502. btnSizer.Add(item = self.btnOk, proportion = 0,
  1503. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1504. border = 10)
  1505. dialogSizer.Add(item = btnSizer, proportion = 0,
  1506. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM | wx.TOP | wx.ALIGN_RIGHT,
  1507. border = 10)
  1508. self.panel.SetAutoLayout(True)
  1509. self.panel.SetSizer(dialogSizer)
  1510. dialogSizer.Fit(self.panel)
  1511. size = wx.Size(globalvar.DIALOG_GSELECT_SIZE[0] + 225, self.GetBestSize()[1])
  1512. self.SetMinSize(size)
  1513. self.SetSize((size.width, size.height))
  1514. self.Layout()
  1515. def OnCmdDialog(self, event):
  1516. GUI(parent = self, modal = True).ParseCommand(cmd = ['v.external.out'])
  1517. def OnCancel(self, event):
  1518. self.Destroy()
  1519. def OnOK(self, event):
  1520. if self.dsnInput.GetType() == 'native':
  1521. RunCommand('v.external.out',
  1522. parent = self,
  1523. flags = 'r')
  1524. else:
  1525. dsn = self.dsnInput.GetDsn()
  1526. frmt = self.dsnInput.GetFormat()
  1527. options = self.dsnInput.GetOptions()
  1528. RunCommand('v.external.out',
  1529. parent = self,
  1530. dsn = dsn, format = frmt,
  1531. options = options)
  1532. self.Close()
  1533. class DxfImportDialog(ImportDialog):
  1534. """!Dialog for bulk import of DXF layers"""
  1535. def __init__(self, parent):
  1536. ImportDialog.__init__(self, parent, itype = 'dxf',
  1537. title = _("Import DXF layers"))
  1538. self.dsnInput = filebrowse.FileBrowseButton(parent = self.panel, id = wx.ID_ANY,
  1539. size = globalvar.DIALOG_GSELECT_SIZE, labelText = '',
  1540. dialogTitle = _('Choose DXF file to import'),
  1541. buttonText = _('Browse'),
  1542. startDirectory = os.getcwd(), fileMode = 0,
  1543. changeCallback = self.OnSetDsn,
  1544. fileMask = "DXF File (*.dxf)|*.dxf")
  1545. self.add.SetLabel(_("Add imported layers into layer tree"))
  1546. self.add.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  1547. self.doLayout()
  1548. def _getCommand(self):
  1549. """!Get command"""
  1550. return 'v.in.dxf'
  1551. def OnRun(self, event):
  1552. """!Import/Link data (each layes as separate vector map)"""
  1553. data = self.list.GetLayers()
  1554. # hide dialog
  1555. self.Hide()
  1556. inputDxf = self.dsnInput.GetValue()
  1557. for layer, output in data:
  1558. cmd = ['v.in.dxf',
  1559. 'input=%s' % inputDxf,
  1560. 'layers=%s' % layer,
  1561. 'output=%s' % output]
  1562. for key in self.options.keys():
  1563. if self.options[key].IsChecked():
  1564. cmd.append('-%s' % key)
  1565. if self.overwrite.IsChecked() or \
  1566. UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  1567. cmd.append('--overwrite')
  1568. # run in Layer Manager
  1569. self.parent.goutput.RunCmd(cmd, switchPage = True,
  1570. onDone = self.AddLayers)
  1571. self.OnCancel()
  1572. def OnSetDsn(self, event):
  1573. """!Input DXF file defined, update list of layer widget"""
  1574. path = event.GetString()
  1575. if not path:
  1576. return
  1577. data = list()
  1578. ret = RunCommand('v.in.dxf',
  1579. quiet = True,
  1580. parent = self,
  1581. read = True,
  1582. flags = 'l',
  1583. input = path)
  1584. if not ret:
  1585. self.list.LoadData()
  1586. self.btn_run.Enable(False)
  1587. return
  1588. for line in ret.splitlines():
  1589. layerId = line.split(':')[0].split(' ')[1]
  1590. layerName = line.split(':')[1].strip()
  1591. grassName = GetValidLayerName(layerName)
  1592. data.append((layerId, layerName.strip(), grassName.strip()))
  1593. self.list.LoadData(data)
  1594. if len(data) > 0:
  1595. self.btn_run.Enable(True)
  1596. else:
  1597. self.btn_run.Enable(False)
  1598. def OnCmdDialog(self, event):
  1599. """!Show command dialog"""
  1600. GUI(parent = self, modal = True).ParseCommand(cmd = ['v.in.dxf'])
  1601. class LayersList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin,
  1602. listmix.CheckListCtrlMixin, listmix.TextEditMixin):
  1603. """!List of layers to be imported (dxf, shp...)"""
  1604. def __init__(self, parent, columns, log = None):
  1605. self.parent = parent
  1606. wx.ListCtrl.__init__(self, parent, wx.ID_ANY,
  1607. style = wx.LC_REPORT)
  1608. listmix.CheckListCtrlMixin.__init__(self)
  1609. self.log = log
  1610. # setup mixins
  1611. listmix.ListCtrlAutoWidthMixin.__init__(self)
  1612. listmix.TextEditMixin.__init__(self)
  1613. for i in range(len(columns)):
  1614. self.InsertColumn(i, columns[i])
  1615. if len(columns) == 3:
  1616. width = (65, 200)
  1617. else:
  1618. width = (65, 180, 110)
  1619. for i in range(len(width)):
  1620. self.SetColumnWidth(col = i, width = width[i])
  1621. self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnPopupMenu) #wxMSW
  1622. self.Bind(wx.EVT_RIGHT_UP, self.OnPopupMenu) #wxGTK
  1623. def LoadData(self, data = None):
  1624. """!Load data into list"""
  1625. if data is None:
  1626. return
  1627. self.DeleteAllItems()
  1628. for item in data:
  1629. index = self.InsertStringItem(sys.maxint, str(item[0]))
  1630. for i in range(1, len(item)):
  1631. self.SetStringItem(index, i, "%s" % str(item[i]))
  1632. # check by default only on one item
  1633. if len(data) == 1:
  1634. self.CheckItem(index, True)
  1635. def OnPopupMenu(self, event):
  1636. """!Show popup menu"""
  1637. if self.GetItemCount() < 1:
  1638. return
  1639. if not hasattr(self, "popupDataID1"):
  1640. self.popupDataID1 = wx.NewId()
  1641. self.popupDataID2 = wx.NewId()
  1642. self.Bind(wx.EVT_MENU, self.OnSelectAll, id = self.popupDataID1)
  1643. self.Bind(wx.EVT_MENU, self.OnSelectNone, id = self.popupDataID2)
  1644. # generate popup-menu
  1645. menu = wx.Menu()
  1646. menu.Append(self.popupDataID1, _("Select all"))
  1647. menu.Append(self.popupDataID2, _("Deselect all"))
  1648. self.PopupMenu(menu)
  1649. menu.Destroy()
  1650. def OnSelectAll(self, event):
  1651. """!Select all items"""
  1652. item = -1
  1653. while True:
  1654. item = self.GetNextItem(item)
  1655. if item == -1:
  1656. break
  1657. self.CheckItem(item, True)
  1658. event.Skip()
  1659. def OnSelectNone(self, event):
  1660. """!Deselect items"""
  1661. item = -1
  1662. while True:
  1663. item = self.GetNextItem(item, wx.LIST_STATE_SELECTED)
  1664. if item == -1:
  1665. break
  1666. self.CheckItem(item, False)
  1667. event.Skip()
  1668. def OnLeftDown(self, event):
  1669. """!Allow editing only output name
  1670. Code taken from TextEditMixin class.
  1671. """
  1672. x, y = event.GetPosition()
  1673. colLocs = [0]
  1674. loc = 0
  1675. for n in range(self.GetColumnCount()):
  1676. loc = loc + self.GetColumnWidth(n)
  1677. colLocs.append(loc)
  1678. col = bisect(colLocs, x + self.GetScrollPos(wx.HORIZONTAL)) - 1
  1679. if col == self.GetColumnCount() - 1:
  1680. listmix.TextEditMixin.OnLeftDown(self, event)
  1681. else:
  1682. event.Skip()
  1683. def GetLayers(self):
  1684. """!Get list of layers (layer name, output name)"""
  1685. data = []
  1686. item = -1
  1687. while True:
  1688. item = self.GetNextItem(item)
  1689. if item == -1:
  1690. break
  1691. if not self.IsChecked(item):
  1692. continue
  1693. # layer / output name
  1694. data.append((self.GetItem(item, 1).GetText(),
  1695. self.GetItem(item, self.GetColumnCount() - 1).GetText()))
  1696. return data
  1697. class SetOpacityDialog(wx.Dialog):
  1698. """!Set opacity of map layers"""
  1699. def __init__(self, parent, id = wx.ID_ANY, title = _("Set Map Layer Opacity"),
  1700. size = wx.DefaultSize, pos = wx.DefaultPosition,
  1701. style = wx.DEFAULT_DIALOG_STYLE, opacity = 100):
  1702. self.parent = parent # GMFrame
  1703. self.opacity = opacity # current opacity
  1704. super(SetOpacityDialog, self).__init__(parent, id = id, pos = pos,
  1705. size = size, style = style, title = title)
  1706. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1707. sizer = wx.BoxSizer(wx.VERTICAL)
  1708. box = wx.GridBagSizer(vgap = 5, hgap = 5)
  1709. self.value = wx.Slider(panel, id = wx.ID_ANY, value = self.opacity,
  1710. style = wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | \
  1711. wx.SL_TOP | wx.SL_LABELS,
  1712. minValue = 0, maxValue = 100,
  1713. size = (350, -1))
  1714. box.Add(item = self.value,
  1715. flag = wx.ALIGN_CENTRE, pos = (0, 0), span = (1, 2))
  1716. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1717. label = _("transparent")),
  1718. pos = (1, 0))
  1719. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1720. label = _("opaque")),
  1721. flag = wx.ALIGN_RIGHT,
  1722. pos = (1, 1))
  1723. sizer.Add(item = box, proportion = 0,
  1724. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1725. line = wx.StaticLine(parent = panel, id = wx.ID_ANY,
  1726. style = wx.LI_HORIZONTAL)
  1727. sizer.Add(item = line, proportion = 0,
  1728. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1729. # buttons
  1730. btnsizer = wx.StdDialogButtonSizer()
  1731. btnOK = wx.Button(parent = panel, id = wx.ID_OK)
  1732. btnOK.SetDefault()
  1733. btnsizer.AddButton(btnOK)
  1734. btnCancel = wx.Button(parent = panel, id = wx.ID_CANCEL)
  1735. btnsizer.AddButton(btnCancel)
  1736. btnsizer.Realize()
  1737. sizer.Add(item = btnsizer, proportion = 0,
  1738. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1739. panel.SetSizer(sizer)
  1740. sizer.Fit(panel)
  1741. self.SetSize(self.GetBestSize())
  1742. self.Layout()
  1743. def GetOpacity(self):
  1744. """!Button 'OK' pressed"""
  1745. # return opacity value
  1746. opacity = float(self.value.GetValue()) / 100
  1747. return opacity
  1748. def GetImageHandlers(image):
  1749. """!Get list of supported image handlers"""
  1750. lext = list()
  1751. ltype = list()
  1752. for h in image.GetHandlers():
  1753. lext.append(h.GetExtension())
  1754. filetype = ''
  1755. if 'png' in lext:
  1756. filetype += "PNG file (*.png)|*.png|"
  1757. ltype.append({ 'type' : wx.BITMAP_TYPE_PNG,
  1758. 'ext' : 'png' })
  1759. filetype += "BMP file (*.bmp)|*.bmp|"
  1760. ltype.append({ 'type' : wx.BITMAP_TYPE_BMP,
  1761. 'ext' : 'bmp' })
  1762. if 'gif' in lext:
  1763. filetype += "GIF file (*.gif)|*.gif|"
  1764. ltype.append({ 'type' : wx.BITMAP_TYPE_GIF,
  1765. 'ext' : 'gif' })
  1766. if 'jpg' in lext:
  1767. filetype += "JPG file (*.jpg)|*.jpg|"
  1768. ltype.append({ 'type' : wx.BITMAP_TYPE_JPEG,
  1769. 'ext' : 'jpg' })
  1770. if 'pcx' in lext:
  1771. filetype += "PCX file (*.pcx)|*.pcx|"
  1772. ltype.append({ 'type' : wx.BITMAP_TYPE_PCX,
  1773. 'ext' : 'pcx' })
  1774. if 'pnm' in lext:
  1775. filetype += "PNM file (*.pnm)|*.pnm|"
  1776. ltype.append({ 'type' : wx.BITMAP_TYPE_PNM,
  1777. 'ext' : 'pnm' })
  1778. if 'tif' in lext:
  1779. filetype += "TIF file (*.tif)|*.tif|"
  1780. ltype.append({ 'type' : wx.BITMAP_TYPE_TIF,
  1781. 'ext' : 'tif' })
  1782. if 'xpm' in lext:
  1783. filetype += "XPM file (*.xpm)|*.xpm"
  1784. ltype.append({ 'type' : wx.BITMAP_TYPE_XPM,
  1785. 'ext' : 'xpm' })
  1786. return filetype, ltype
  1787. class ImageSizeDialog(wx.Dialog):
  1788. """!Set size for saved graphic file"""
  1789. def __init__(self, parent, id = wx.ID_ANY, title = _("Set image size"),
  1790. style = wx.DEFAULT_DIALOG_STYLE, **kwargs):
  1791. self.parent = parent
  1792. wx.Dialog.__init__(self, parent, id = id, style = style, title = title, **kwargs)
  1793. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1794. self.box = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1795. label = ' % s' % _("Image size"))
  1796. size = self.parent.GetWindow().GetClientSize()
  1797. self.width = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1798. style = wx.SP_ARROW_KEYS)
  1799. self.width.SetRange(20, 1e6)
  1800. self.width.SetValue(size.width)
  1801. wx.CallAfter(self.width.SetFocus)
  1802. self.height = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1803. style = wx.SP_ARROW_KEYS)
  1804. self.height.SetRange(20, 1e6)
  1805. self.height.SetValue(size.height)
  1806. self.template = wx.Choice(parent = self.panel, id = wx.ID_ANY,
  1807. size = (125, -1),
  1808. choices = [ "",
  1809. "640x480",
  1810. "800x600",
  1811. "1024x768",
  1812. "1280x960",
  1813. "1600x1200",
  1814. "1920x1440" ])
  1815. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  1816. self.btnOK.SetDefault()
  1817. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1818. self.template.Bind(wx.EVT_CHOICE, self.OnTemplate)
  1819. self._layout()
  1820. self.SetSize(self.GetBestSize())
  1821. def _layout(self):
  1822. """!Do layout"""
  1823. sizer = wx.BoxSizer(wx.VERTICAL)
  1824. # body
  1825. box = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  1826. fbox = wx.FlexGridSizer(cols = 2, vgap = 5, hgap = 5)
  1827. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1828. label = _("Width:")),
  1829. flag = wx.ALIGN_CENTER_VERTICAL)
  1830. fbox.Add(item = self.width)
  1831. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1832. label = _("Height:")),
  1833. flag = wx.ALIGN_CENTER_VERTICAL)
  1834. fbox.Add(item = self.height)
  1835. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1836. label = _("Template:")),
  1837. flag = wx.ALIGN_CENTER_VERTICAL)
  1838. fbox.Add(item = self.template)
  1839. box.Add(item = fbox, proportion = 1,
  1840. flag = wx.EXPAND | wx.ALL, border = 5)
  1841. sizer.Add(item = box, proportion = 1,
  1842. flag=wx.EXPAND | wx.ALL, border = 3)
  1843. # buttons
  1844. btnsizer = wx.StdDialogButtonSizer()
  1845. btnsizer.AddButton(self.btnOK)
  1846. btnsizer.AddButton(self.btnCancel)
  1847. btnsizer.Realize()
  1848. sizer.Add(item = btnsizer, proportion = 0,
  1849. flag = wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
  1850. self.panel.SetSizer(sizer)
  1851. sizer.Fit(self.panel)
  1852. self.Layout()
  1853. def GetValues(self):
  1854. """!Get width/height values"""
  1855. return self.width.GetValue(), self.height.GetValue()
  1856. def OnTemplate(self, event):
  1857. """!Template selected"""
  1858. sel = event.GetString()
  1859. if not sel:
  1860. width, height = self.parent.GetWindow().GetClientSize()
  1861. else:
  1862. width, height = map(int, sel.split('x'))
  1863. self.width.SetValue(width)
  1864. self.height.SetValue(height)
  1865. class SqlQueryFrame(wx.Frame):
  1866. def __init__(self, parent, id = wx.ID_ANY,
  1867. title = _("GRASS GIS SQL Query Utility"),
  1868. *kwargs):
  1869. """!SQL Query Utility window
  1870. """
  1871. self.parent = parent
  1872. wx.Frame.__init__(self, parent = parent, id = id, title = title, *kwargs)
  1873. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass_sql.ico'), wx.BITMAP_TYPE_ICO))
  1874. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1875. self.sqlBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1876. label = _(" SQL statement "))
  1877. self.sql = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  1878. style = wx.TE_MULTILINE)
  1879. self.btnApply = wx.Button(parent = self.panel, id = wx.ID_APPLY)
  1880. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1881. self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, self.btnCancel)
  1882. self._layout()
  1883. self.SetMinSize(wx.Size(300, 150))
  1884. self.SetSize(wx.Size(500, 200))
  1885. def _layout(self):
  1886. """!Do layout"""
  1887. sizer = wx.BoxSizer(wx.VERTICAL)
  1888. sqlSizer = wx.StaticBoxSizer(self.sqlBox, wx.HORIZONTAL)
  1889. sqlSizer.Add(item = self.sql, proportion = 1,
  1890. flag = wx.EXPAND)
  1891. btnSizer = wx.StdDialogButtonSizer()
  1892. btnSizer.AddButton(self.btnApply)
  1893. btnSizer.AddButton(self.btnCancel)
  1894. btnSizer.Realize()
  1895. sizer.Add(item = sqlSizer, proportion = 1,
  1896. flag = wx.EXPAND | wx.ALL, border = 5)
  1897. sizer.Add(item = btnSizer, proportion = 0,
  1898. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1899. self.panel.SetSizer(sizer)
  1900. self.Layout()
  1901. def OnCloseWindow(self, event):
  1902. """!Close window
  1903. """
  1904. self.Close()