gdialogs.py 73 KB

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