gdialogs.py 88 KB

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