gdialogs.py 70 KB

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