gdialogs.py 63 KB

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