gdialogs.py 89 KB

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