gdialogs.py 70 KB

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