dialogs.py 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. """
  2. @package gui_core.dialogs
  3. @brief Various dialogs used in wxGUI.
  4. List of classes:
  5. - :class:`SimpleDialog`
  6. - :class:`LocationDialog`
  7. - :class:`MapsetDialog`
  8. - :class:`VectorDialog`
  9. - :class:`NewVectorDialog`
  10. - :class:`SavedRegion`
  11. - :class:`GroupDialog`
  12. - :class:`MapLayersDialog`
  13. - :class:`SetOpacityDialog`
  14. - :class:`ImageSizeDialog`
  15. - :class:`SqlQueryFrame`
  16. - :class:`SymbolDialog`
  17. - :class:`QuitDialog`
  18. (C) 2008-2016 by the GRASS Development Team
  19. This program is free software under the GNU General Public License
  20. (>=v2). Read the file COPYING that comes with GRASS for details.
  21. @author Martin Landa <landa.martin gmail.com>
  22. @author Anna Kratochvilova <kratochanna gmail.com> (GroupDialog, SymbolDialog)
  23. """
  24. import os
  25. import sys
  26. import re
  27. import wx
  28. from grass.script import core as grass
  29. from grass.script.utils import natural_sort
  30. from grass.pydispatch.signal import Signal
  31. from core import globalvar
  32. from core.gcmd import GError, RunCommand, GMessage
  33. from gui_core.gselect import LocationSelect, MapsetSelect, Select, \
  34. OgrTypeSelect, SubGroupSelect
  35. from gui_core.widgets import SingleSymbolPanel, GListCtrl, SimpleValidator, MapValidator
  36. from core.utils import _
  37. from core.settings import UserSettings
  38. from core.debug import Debug
  39. class SimpleDialog(wx.Dialog):
  40. def __init__(self, parent, title, id = wx.ID_ANY,
  41. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  42. **kwargs):
  43. """General dialog to choose given element (location, mapset, vector map, etc.)
  44. :param parent: window
  45. :param title: window title
  46. """
  47. wx.Dialog.__init__(self, parent, id, title, style = style, **kwargs)
  48. self.SetExtraStyle(wx.WS_EX_VALIDATE_RECURSIVELY)
  49. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  50. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  51. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  52. self.btnOK.SetDefault()
  53. self.__layout()
  54. self.warning = _("Required item is not set.")
  55. def __layout(self):
  56. """Do layout"""
  57. self.sizer = wx.BoxSizer(wx.VERTICAL)
  58. self.dataSizer = wx.BoxSizer(wx.VERTICAL)
  59. # self.informLabel = wx.StaticText(self.panel, id = wx.ID_ANY)
  60. # buttons
  61. btnSizer = wx.StdDialogButtonSizer()
  62. btnSizer.AddButton(self.btnCancel)
  63. btnSizer.AddButton(self.btnOK)
  64. btnSizer.Realize()
  65. self.sizer.Add(item = self.dataSizer, proportion = 1,
  66. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  67. # self.sizer.Add(item = self.informLabel, proportion = 0, flag = wx.ALL, border = 5)
  68. self.sizer.Add(item = btnSizer, proportion = 0,
  69. flag = wx.EXPAND | wx.ALL, border = 5)
  70. def ValidatorCallback(self, win):
  71. GMessage(parent = self, message = self.warning)
  72. # self.informLabel.SetForegroundColour(wx.Colour(255, 0, 0))
  73. # self.informLabel.SetLabel(self.warning)
  74. class LocationDialog(SimpleDialog):
  75. """Dialog used to select location"""
  76. def __init__(self, parent, title = _("Select GRASS location and mapset")):
  77. SimpleDialog.__init__(self, parent, title)
  78. self.element1 = LocationSelect(parent = self.panel, id = wx.ID_ANY,
  79. size = globalvar.DIALOG_GSELECT_SIZE,
  80. validator = SimpleValidator(callback = self.ValidatorCallback))
  81. self.element1.Bind(wx.EVT_TEXT, self.OnLocation)
  82. self.element2 = MapsetSelect(parent = self.panel, id = wx.ID_ANY,
  83. size = globalvar.DIALOG_GSELECT_SIZE,
  84. setItems = False, skipCurrent = True,
  85. validator = SimpleValidator(callback = self.ValidatorCallback))
  86. self.element1.SetFocus()
  87. self.warning = _("Location or mapset is not defined.")
  88. self._layout()
  89. self.SetMinSize(self.GetSize())
  90. def _layout(self):
  91. """Do layout"""
  92. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  93. label = _("Name of GRASS location:")),
  94. proportion = 0, flag = wx.ALL, border = 1)
  95. self.dataSizer.Add(self.element1, proportion = 0,
  96. flag = wx.EXPAND | wx.ALL, border = 1)
  97. self.dataSizer.Add(wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  98. label = _("Name of mapset:")), proportion = 0,
  99. flag = wx.EXPAND | wx.ALL, border = 1)
  100. self.dataSizer.Add(self.element2, proportion = 0,
  101. flag = wx.EXPAND | wx.ALL, border = 1)
  102. self.panel.SetSizer(self.sizer)
  103. self.sizer.Fit(self)
  104. def OnLocation(self, event):
  105. """Select mapset given location name"""
  106. location = event.GetString()
  107. if location:
  108. dbase = grass.gisenv()['GISDBASE']
  109. self.element2.UpdateItems(dbase = dbase, location = location)
  110. self.element2.SetSelection(0)
  111. mapset = self.element2.GetStringSelection()
  112. def GetValues(self):
  113. """Get location, mapset"""
  114. return (self.element1.GetValue(), self.element2.GetValue())
  115. class MapsetDialog(SimpleDialog):
  116. """Dialog used to select mapset"""
  117. def __init__(self, parent, title = _("Select mapset in GRASS location"),
  118. location = None):
  119. SimpleDialog.__init__(self, parent, title)
  120. if location:
  121. self.SetTitle(self.GetTitle() + ' <%s>' % location)
  122. else:
  123. self.SetTitle(self.GetTitle() + ' <%s>' % grass.gisenv()['LOCATION_NAME'])
  124. self.element = MapsetSelect(parent = self.panel, id = wx.ID_ANY, skipCurrent = True,
  125. size = globalvar.DIALOG_GSELECT_SIZE,
  126. validator = SimpleValidator(callback = self.ValidatorCallback))
  127. self.element.SetFocus()
  128. self.warning = _("Name of mapset is missing.")
  129. self._layout()
  130. self.SetMinSize(self.GetSize())
  131. def _layout(self):
  132. """Do layout"""
  133. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  134. label = _("Name of mapset:")),
  135. proportion = 0, flag = wx.ALL, border = 1)
  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.element.GetValue()
  142. class VectorDialog(SimpleDialog):
  143. def __init__(self, parent, title = _("Select vector map"), layerTree = None):
  144. """Dialog for selecting existing vector map
  145. :param parent: parent window
  146. :param title: window title
  147. :param layerTree: show only vector maps in given layer tree if not None
  148. :return: dialog instance
  149. """
  150. SimpleDialog.__init__(self, parent, title)
  151. self.element = Select(parent = self.panel, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  152. type = 'vector', layerTree = layerTree,
  153. validator = MapValidator())
  154. self.element.SetFocus()
  155. self.warning = _("Name of vector map is missing.")
  156. wx.CallAfter(self._layout)
  157. def _layout(self):
  158. """Do layout"""
  159. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  160. label = _("Name of vector map:")),
  161. proportion = 0, flag = wx.ALL, border = 1)
  162. self.dataSizer.Add(item = self.element, proportion = 0,
  163. flag = wx.EXPAND | wx.ALL, border = 1)
  164. self.panel.SetSizer(self.sizer)
  165. self.sizer.Fit(self)
  166. def GetName(self, full = False):
  167. """Get name of vector map to be created
  168. :param full: True to get fully qualified name
  169. """
  170. name = self.element.GetValue()
  171. if full:
  172. if '@' in name:
  173. return name
  174. else:
  175. return name + '@' + grass.gisenv()['MAPSET']
  176. return name.split('@', 1)[0]
  177. class NewVectorDialog(VectorDialog):
  178. def __init__(self, parent, title = _("Create new vector map"),
  179. disableAdd = False, disableTable = False, showType = False):
  180. """Dialog for creating new vector map
  181. :param parent: parent window
  182. :param title: window title
  183. :param disableAdd: disable 'add layer' checkbox
  184. :param disableTable: disable 'create table' checkbox
  185. :param showType: True to show feature type selector (used for creating new empty OGR layers)
  186. :return: dialog instance
  187. """
  188. VectorDialog.__init__(self, parent, title)
  189. # determine output format
  190. if showType:
  191. self.ftype = OgrTypeSelect(parent = self, panel = self.panel)
  192. else:
  193. self.ftype = None
  194. # create attribute table
  195. self.table = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  196. label = _("Create attribute table"))
  197. self.table.SetValue(True)
  198. if disableTable:
  199. self.table.Enable(False)
  200. if showType:
  201. self.keycol = None
  202. else:
  203. self.keycol = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  204. size = globalvar.DIALOG_SPIN_SIZE)
  205. self.keycol.SetValue(UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value'))
  206. if disableTable:
  207. self.keycol.Enable(False)
  208. self.addbox = wx.CheckBox(parent = self.panel,
  209. label = _('Add created map into layer tree'), style = wx.NO_BORDER)
  210. if disableAdd:
  211. self.addbox.SetValue(True)
  212. self.addbox.Enable(False)
  213. else:
  214. self.addbox.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  215. self.table.Bind(wx.EVT_CHECKBOX, self.OnTable)
  216. self.warning = _("Name of new vector map is missing.")
  217. def OnTable(self, event):
  218. if self.keycol:
  219. self.keycol.Enable(event.IsChecked())
  220. def _layout(self):
  221. """Do layout"""
  222. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  223. label = _("Name for new vector map:")),
  224. proportion = 0, flag = wx.ALL, border = 1)
  225. self.dataSizer.Add(item = self.element, proportion = 0,
  226. flag = wx.EXPAND | wx.ALL, border = 1)
  227. if self.ftype:
  228. self.dataSizer.AddSpacer(1)
  229. self.dataSizer.Add(item = self.ftype, proportion = 0,
  230. flag = wx.EXPAND | wx.ALL, border = 1)
  231. self.dataSizer.Add(item = self.table, proportion = 0,
  232. flag = wx.EXPAND | wx.ALL, border = 1)
  233. if self.keycol:
  234. keySizer = wx.BoxSizer(wx.HORIZONTAL)
  235. keySizer.Add(item = wx.StaticText(parent = self.panel, label = _("Key column:")),
  236. proportion = 0,
  237. flag = wx.ALIGN_CENTER_VERTICAL)
  238. keySizer.AddSpacer(10)
  239. keySizer.Add(item = self.keycol, proportion = 0,
  240. flag = wx.ALIGN_RIGHT)
  241. self.dataSizer.Add(item = keySizer, proportion = 1,
  242. flag = wx.EXPAND | wx.ALL, border = 1)
  243. self.dataSizer.AddSpacer(5)
  244. self.dataSizer.Add(item = self.addbox, proportion = 0,
  245. flag = wx.EXPAND | wx.ALL, border = 1)
  246. self.panel.SetSizer(self.sizer)
  247. self.sizer.Fit(self)
  248. self.SetMinSize(self.GetSize())
  249. def GetKey(self):
  250. """Get key column name"""
  251. if self.keycol:
  252. return self.keycol.GetValue()
  253. return UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value')
  254. def IsChecked(self, key):
  255. """Get dialog properties
  256. :param key: window key ('add', 'table')
  257. :return: True/False
  258. :return: None on error
  259. """
  260. if key == 'add':
  261. return self.addbox.IsChecked()
  262. elif key == 'table':
  263. return self.table.IsChecked()
  264. return None
  265. def GetFeatureType(self):
  266. """Get feature type for OGR
  267. :return: feature type as string
  268. :return: None for native format
  269. """
  270. if self.ftype:
  271. return self.ftype.GetType()
  272. return None
  273. def CreateNewVector(parent, cmd, title = _('Create new vector map'),
  274. exceptMap=None, giface=None,
  275. disableAdd = False, disableTable = False):
  276. """Create new vector map layer
  277. :param cmd: (prog, \*\*kwargs)
  278. :param title: window title
  279. :param exceptMap: list of maps to be excepted
  280. :param log:
  281. :param disableAdd: disable 'add layer' checkbox
  282. :param disableTable: disable 'create table' checkbox
  283. :return: dialog instance
  284. :return: None on error
  285. """
  286. vExternalOut = grass.parse_command('v.external.out', flags='g')
  287. isNative = vExternalOut['format'] == 'native'
  288. if cmd[0] == 'v.edit' and not isNative:
  289. showType = True
  290. else:
  291. showType = False
  292. dlg = NewVectorDialog(parent, title = title,
  293. disableAdd = disableAdd, disableTable = disableTable,
  294. showType = showType)
  295. if dlg.ShowModal() != wx.ID_OK:
  296. dlg.Destroy()
  297. return None
  298. outmap = dlg.GetName()
  299. key = dlg.GetKey()
  300. if outmap == exceptMap:
  301. GError(parent = parent,
  302. message = _("Unable to create vector map <%s>.") % outmap)
  303. dlg.Destroy()
  304. return None
  305. if dlg.table.IsEnabled() and not key:
  306. GError(parent = parent,
  307. message = _("Invalid or empty key column.\n"
  308. "Unable to create vector map <%s>.") % outmap)
  309. dlg.Destroy()
  310. return
  311. if outmap == '': # should not happen
  312. dlg.Destroy()
  313. return None
  314. # update cmd -> output name defined
  315. cmd[1][cmd[2]] = outmap
  316. if showType:
  317. cmd[1]['type'] = dlg.GetFeatureType()
  318. curMapset = grass.gisenv()['MAPSET']
  319. if isNative:
  320. listOfVectors = grass.list_grouped('vector')[curMapset]
  321. else:
  322. listOfVectors = RunCommand('v.external',
  323. quiet = True,
  324. parent = parent,
  325. read = True,
  326. flags = 'l',
  327. input = vExternalOut['dsn']).splitlines()
  328. overwrite = False
  329. if not UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled') and \
  330. outmap in listOfVectors:
  331. dlgOw = wx.MessageDialog(parent, message = _("Vector map <%s> already exists "
  332. "in the current mapset. "
  333. "Do you want to overwrite it?") % outmap,
  334. caption = _("Overwrite?"),
  335. style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  336. if dlgOw.ShowModal() == wx.ID_YES:
  337. overwrite = True
  338. else:
  339. dlgOw.Destroy()
  340. dlg.Destroy()
  341. return None
  342. if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  343. overwrite = True
  344. ret = RunCommand(prog = cmd[0],
  345. parent = parent,
  346. overwrite = overwrite,
  347. **cmd[1])
  348. if ret != 0:
  349. dlg.Destroy()
  350. return None
  351. if not isNative and \
  352. not grass.find_file(outmap, element = 'vector', mapset = curMapset)['fullname']:
  353. # create link for OGR layers
  354. RunCommand('v.external',
  355. overwrite = overwrite,
  356. parent = parent,
  357. input = vExternalOut['dsn'],
  358. layer = outmap)
  359. # create attribute table
  360. if dlg.table.IsEnabled() and dlg.table.IsChecked():
  361. if isNative:
  362. sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
  363. RunCommand('db.connect',
  364. flags = 'c')
  365. Debug.msg(1, "SQL: %s" % sql)
  366. RunCommand('db.execute',
  367. quiet = True,
  368. parent = parent,
  369. input = '-',
  370. stdin = sql)
  371. RunCommand('v.db.connect',
  372. quiet = True,
  373. parent = parent,
  374. map = outmap,
  375. table = outmap,
  376. key = key,
  377. layer = '1')
  378. # TODO: how to deal with attribute tables for OGR layers?
  379. # return fully qualified map name
  380. if '@' not in outmap:
  381. outmap += '@' + grass.gisenv()['MAPSET']
  382. # if giface:
  383. # giface.WriteLog(_("New vector map <%s> created") % outmap)
  384. return dlg
  385. class SavedRegion(wx.Dialog):
  386. def __init__(self, parent, title, id = wx.ID_ANY, loadsave = 'load',
  387. **kwargs):
  388. """Loading or saving of display extents to saved region file
  389. :param loadsave: load or save region?
  390. """
  391. wx.Dialog.__init__(self, parent, id, title, **kwargs)
  392. self.loadsave = loadsave
  393. self.wind = ''
  394. sizer = wx.BoxSizer(wx.VERTICAL)
  395. box = wx.BoxSizer(wx.HORIZONTAL)
  396. label = wx.StaticText(parent = self, id = wx.ID_ANY)
  397. box.Add(item = label, proportion = 0, flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  398. if loadsave == 'load':
  399. label.SetLabel(_("Load region:"))
  400. self._selection = Select(parent=self, size=globalvar.DIALOG_GSELECT_SIZE,
  401. type='windows')
  402. elif loadsave == 'save':
  403. label.SetLabel(_("Save region:"))
  404. self._selection = Select(parent=self, size=globalvar.DIALOG_GSELECT_SIZE,
  405. type='windows', mapsets=[grass.gisenv()['MAPSET']], fullyQualified = False)
  406. box.Add(item=self._selection, proportion=0, flag=wx.ALIGN_CENTRE | wx.ALL, border=5)
  407. self._selection.SetFocus()
  408. self._selection.Bind(wx.EVT_TEXT, self.OnRegion)
  409. sizer.Add(item = box, proportion = 0, flag = wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  410. border = 5)
  411. line = wx.StaticLine(parent = self, id = wx.ID_ANY, size = (20, -1), style = wx.LI_HORIZONTAL)
  412. sizer.Add(item = line, proportion = 0,
  413. flag = wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, border = 5)
  414. btnsizer = wx.StdDialogButtonSizer()
  415. btn = wx.Button(parent = self, id = wx.ID_OK)
  416. btn.SetDefault()
  417. btnsizer.AddButton(btn)
  418. btn = wx.Button(parent = self, id = wx.ID_CANCEL)
  419. btnsizer.AddButton(btn)
  420. btnsizer.Realize()
  421. sizer.Add(item = btnsizer, proportion = 0, flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  422. self.SetSizer(sizer)
  423. sizer.Fit(self)
  424. self.Layout()
  425. def OnRegion(self, event):
  426. value = self._selection.GetValue()
  427. if '@' in value:
  428. value = value.rsplit('@', 1)[0]
  429. if not grass.legal_name(value):
  430. GMessage(parent=self,
  431. message=_("Name cannot begin with '.' "
  432. "and must not contain space, quotes, "
  433. "'/', '\'', '@', ',', '=', '*', "
  434. "and all other non-alphanumeric characters."))
  435. else:
  436. self.wind = value
  437. def GetName(self):
  438. """Return region name"""
  439. return self.wind
  440. class GroupDialog(wx.Dialog):
  441. """Dialog for creating/editing groups"""
  442. def __init__(self, parent = None, defaultGroup = None, defaultSubgroup = None,
  443. title = _("Create or edit imagery groups"),
  444. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  445. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  446. style = style, **kwargs)
  447. self.parent = parent
  448. self.defaultGroup = defaultGroup
  449. self.defaultSubgroup = defaultSubgroup
  450. self.currentGroup = self.defaultGroup
  451. self.currentSubgroup = self.defaultGroup
  452. self.dataChanged = False
  453. # signaling edit subgroup / group mode
  454. self.edit_subg = False
  455. # sungroup maps dict value - ischecked
  456. self.subgmaps = {}
  457. # list of group maps
  458. self.gmaps = []
  459. # pattern chosen for filtering
  460. self.flt_pattern = ''
  461. self.bodySizer = self._createDialogBody()
  462. # buttons
  463. btnOk = wx.Button(parent = self, id = wx.ID_OK)
  464. btnApply = wx.Button(parent = self, id = wx.ID_APPLY)
  465. btnClose = wx.Button(parent = self, id = wx.ID_CANCEL)
  466. btnOk.SetToolTipString(_("Apply changes to selected group and close dialog"))
  467. btnApply.SetToolTipString(_("Apply changes to selected group"))
  468. btnClose.SetToolTipString(_("Close dialog, changes are not applied"))
  469. #btnOk.SetDefault()
  470. # sizers & do layout
  471. # btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  472. # btnSizer.Add(item = btnClose, proportion = 0,
  473. # flag = wx.RIGHT | wx.ALIGN_RIGHT | wx.EXPAND, border = 5)
  474. # btnSizer.Add(item = btnApply, proportion = 0,
  475. # flag = wx.LEFT, border = 5)
  476. btnSizer = wx.StdDialogButtonSizer()
  477. btnSizer.AddButton(btnOk)
  478. btnSizer.AddButton(btnApply)
  479. btnSizer.AddButton(btnClose)
  480. btnSizer.Realize()
  481. mainSizer = wx.BoxSizer(wx.VERTICAL)
  482. mainSizer.Add(item = self.bodySizer, proportion = 1,
  483. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 10)
  484. mainSizer.Add(item = wx.StaticLine(parent = self, id = wx.ID_ANY,
  485. style = wx.LI_HORIZONTAL), proportion = 0,
  486. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 10)
  487. mainSizer.Add(item = btnSizer, proportion = 0,
  488. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.ALIGN_RIGHT, border = 10)
  489. self.SetSizer(mainSizer)
  490. mainSizer.Fit(self)
  491. btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  492. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  493. btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  494. # set dialog min size
  495. self.SetMinSize(self.GetSize())
  496. self.SetSize((-1, 400))
  497. def _createDialogBody(self):
  498. bodySizer = wx.BoxSizer(wx.VERTICAL)
  499. #TODO same text in MapLayersDialogBase
  500. filter_tooltip = _("Put here a regular expression."
  501. " Characters '.*' stand for anything,"
  502. " character '^' stands for the beginning"
  503. " and '$' for the end.")
  504. # group selection
  505. bodySizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
  506. label = _("Select existing group or "
  507. "enter name of new group:")),
  508. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP, border = 10)
  509. self.groupSelect = Select(parent = self, type = 'group',
  510. mapsets = [grass.gisenv()['MAPSET']],
  511. size = globalvar.DIALOG_GSELECT_SIZE,
  512. fullyQualified = False) # searchpath?
  513. bodySizer.Add(item = self.groupSelect, flag = wx.TOP | wx.EXPAND, border = 5)
  514. self.subg_chbox = wx.CheckBox(parent = self, id = wx.ID_ANY,
  515. label = _("Edit/create subgroup"))
  516. bodySizer.Add(item = self.subg_chbox,
  517. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP, border = 10)
  518. self.subg_panel = wx.Panel(self)
  519. subg_sizer = wx.BoxSizer(wx.VERTICAL)
  520. subg_sizer.Add(item = wx.StaticText(parent=self.subg_panel, id = wx.ID_ANY,
  521. label = _("Select existing subgroup or "
  522. "enter name of new subgroup:")),
  523. flag = wx.ALIGN_CENTER_VERTICAL)
  524. self.subGroupSelect = SubGroupSelect(parent = self.subg_panel)
  525. subg_sizer.Add(item=self.subGroupSelect, flag=wx.EXPAND | wx.TOP, border = 5)
  526. self.subg_panel.SetSizer(subg_sizer)
  527. bodySizer.Add(item = self.subg_panel, flag = wx.TOP | wx.EXPAND, border = 5)
  528. bodySizer.AddSpacer(10)
  529. buttonSizer = wx.BoxSizer(wx.VERTICAL)
  530. # layers in group
  531. self.gListPanel = wx.Panel(self)
  532. gListSizer = wx.GridBagSizer(vgap=3, hgap=2)
  533. self.g_sel_all = wx.CheckBox(parent=self.gListPanel, id=wx.ID_ANY,
  534. label=_("Select all"))
  535. gListSizer.Add(item=self.g_sel_all,
  536. flag=wx.ALIGN_CENTER_VERTICAL,
  537. pos=(0,1))
  538. gListSizer.Add(item = wx.StaticText(parent = self.gListPanel, label = _("Pattern:")),
  539. flag = wx.ALIGN_CENTER_VERTICAL,
  540. pos = (1,0))
  541. self.gfilter = wx.TextCtrl(parent=self.gListPanel, id=wx.ID_ANY,
  542. value="",
  543. size=(250,-1))
  544. self.gfilter.SetToolTipString(filter_tooltip)
  545. gListSizer.Add(item=self.gfilter,
  546. flag=wx.EXPAND,
  547. pos=(1,1))
  548. gListSizer.Add(item = wx.StaticText(parent = self.gListPanel,
  549. label = _("List of maps:")),
  550. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM, border = 5, pos=(2,0))
  551. sizer = wx.BoxSizer(wx.HORIZONTAL)
  552. self.gLayerBox = wx.ListBox(parent = self.gListPanel, id = wx.ID_ANY, size = (-1, 150),
  553. style = wx.LB_MULTIPLE | wx.LB_NEEDED_SB)
  554. sizer.Add(item = self.gLayerBox, proportion = 1, flag = wx.EXPAND)
  555. self.addLayer = wx.Button(self.gListPanel, id = wx.ID_ADD)
  556. self.addLayer.SetToolTipString(_("Select map layers and add them to the list."))
  557. buttonSizer.Add(item = self.addLayer, flag = wx.BOTTOM, border = 10)
  558. self.removeLayer = wx.Button(self.gListPanel, id = wx.ID_REMOVE)
  559. self.removeLayer.SetToolTipString(_("Remove selected layer(s) from list."))
  560. buttonSizer.Add(item = self.removeLayer)
  561. sizer.Add(item = buttonSizer, flag = wx.LEFT, border = 5)
  562. gListSizer.Add(item=sizer, flag=wx.EXPAND, pos=(2,1))
  563. gListSizer.AddGrowableCol(1)
  564. gListSizer.AddGrowableRow(2)
  565. self.gListPanel.SetSizer(gListSizer)
  566. bodySizer.Add(item=self.gListPanel, proportion=1, flag=wx.EXPAND)
  567. # layers in subgroup
  568. self.subgListPanel = wx.Panel(self)
  569. subgListSizer = wx.GridBagSizer(vgap=3, hgap=2)
  570. # select toggle
  571. self.subg_sel_all = wx.CheckBox(parent=self.subgListPanel, id=wx.ID_ANY,
  572. label=_("Select all"))
  573. subgListSizer.Add(item=self.subg_sel_all,
  574. flag=wx.ALIGN_CENTER_VERTICAL,
  575. pos=(0,1))
  576. subgListSizer.Add(item = wx.StaticText(parent=self.subgListPanel, label=_("Pattern:")),
  577. flag = wx.ALIGN_CENTER_VERTICAL,
  578. pos = (1,0))
  579. self.subgfilter = wx.TextCtrl(parent=self.subgListPanel, id=wx.ID_ANY,
  580. value="",
  581. size=(250,-1))
  582. self.subgfilter.SetToolTipString(filter_tooltip)
  583. subgListSizer.Add(item=self.subgfilter,
  584. flag=wx.EXPAND,
  585. pos=(1,1))
  586. subgListSizer.Add(item = wx.StaticText(parent = self.subgListPanel,
  587. label = _("List of maps:")),
  588. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM, border = 5, pos=(2,0))
  589. self.subgListBox = wx.CheckListBox(parent = self.subgListPanel, id = wx.ID_ANY,
  590. size = (250, 100))
  591. self.subgListBox.SetToolTipString(_("Check maps from group to be included into subgroup."))
  592. subgListSizer.Add(item=self.subgListBox, flag=wx.EXPAND, pos=(2,1))
  593. subgListSizer.AddGrowableCol(1)
  594. subgListSizer.AddGrowableRow(2)
  595. self.subgListPanel.SetSizer(subgListSizer)
  596. bodySizer.Add(item=self.subgListPanel, proportion=1, flag=wx.EXPAND)
  597. self.infoLabel = wx.StaticText(parent=self, id=wx.ID_ANY)
  598. bodySizer.Add(item = self.infoLabel,
  599. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM, border = 5)
  600. # bindings
  601. self.gfilter.Bind(wx.EVT_TEXT, self.OnGroupFilter)
  602. self.subgfilter.Bind(wx.EVT_TEXT, self.OnSubgroupFilter)
  603. self.gLayerBox.Bind(wx.EVT_LISTBOX, self.OnGLayerCheck)
  604. self.subgListBox.Bind(wx.EVT_CHECKLISTBOX, self.OnSubgLayerCheck)
  605. self.groupSelect.GetTextCtrl().Bind(wx.EVT_TEXT, self.OnGroupSelected)
  606. self.addLayer.Bind(wx.EVT_BUTTON, self.OnAddLayer)
  607. self.removeLayer.Bind(wx.EVT_BUTTON, self.OnRemoveLayer)
  608. self.subg_chbox.Bind(wx.EVT_CHECKBOX, self.OnSubgChbox)
  609. self.subGroupSelect.Bind(wx.EVT_TEXT, lambda event : self.SubGroupSelected())
  610. self.subg_sel_all.Bind(wx.EVT_CHECKBOX, self.OnSubgSelAll)
  611. self.g_sel_all.Bind(wx.EVT_CHECKBOX, self.OnGSelAll)
  612. if self.defaultGroup:
  613. self.groupSelect.SetValue(self.defaultGroup)
  614. if self.defaultSubgroup is not None:
  615. self.subGroupSelect.SetValue(self.defaultSubgroup)
  616. self.subg_chbox.SetValue(1)
  617. self.SubgChbox(True)
  618. else:
  619. self.subg_chbox.SetValue(0)
  620. self.SubgChbox(False)
  621. return bodySizer
  622. def OnGLayerCheck(self, event):
  623. self._checkGSellAll()
  624. def OnSubgSelAll(self, event):
  625. check = event.Checked()
  626. for item in range(self.subgListBox.GetCount()):
  627. self.CheckSubgItem(item, check)
  628. self.dataChanged = True
  629. event.Skip()
  630. def OnGSelAll(self, event):
  631. check = event.Checked()
  632. if not check:
  633. self.gLayerBox.DeselectAll()
  634. else:
  635. for item in range(self.subgListBox.GetCount()):
  636. self.gLayerBox.Select(item)
  637. event.Skip()
  638. def _checkGSellAll(self):
  639. check = False
  640. nsel = len(self.gLayerBox.GetSelections())
  641. if self.gLayerBox.GetCount() == nsel and \
  642. self.gLayerBox.GetCount() != 0:
  643. check = True
  644. self.g_sel_all.SetValue(check)
  645. def _checkSubGSellAll(self):
  646. not_all_checked = False
  647. if self.subgListBox.GetCount() == 0:
  648. not_all_checked = True
  649. else:
  650. for item in range(self.subgListBox.GetCount()):
  651. if not self.subgListBox.IsChecked(item):
  652. not_all_checked = True
  653. self.subg_sel_all.SetValue(not not_all_checked)
  654. def OnSubgroupFilter(self, event):
  655. text = event.GetString()
  656. self.gfilter.ChangeValue(text)
  657. self.flt_pattern = text
  658. self.FilterGroup()
  659. self.FilterSubgroup()
  660. event.Skip()
  661. def OnGroupFilter(self, event):
  662. text = event.GetString()
  663. self.subgfilter.ChangeValue(text)
  664. self.flt_pattern = text
  665. self.FilterGroup()
  666. self.FilterSubgroup()
  667. event.Skip()
  668. def OnSubgLayerCheck(self, event):
  669. idx = event.GetInt()
  670. m = self.subgListBox.GetString(idx)
  671. self.subgmaps[m] = self.subgListBox.IsChecked(idx)
  672. self.dataChanged = True
  673. self._checkSubGSellAll()
  674. def CheckSubgItem(self, idx, val):
  675. m = self.subgListBox.GetString(idx)
  676. self.subgListBox.Check(idx, val)
  677. self.subgmaps[m] = val
  678. self.dataChanged = val
  679. def DisableSubgroupEdit(self):
  680. """Disable editation of subgroups in the dialog
  681. .. todo::
  682. used by gcp manager, maybe the gcp m should also support subgroups
  683. """
  684. self.edit_subg = False
  685. self.subg_panel.Hide()
  686. self.subg_chbox.Hide()
  687. self.subgListBox.Hide()
  688. self.Layout()
  689. def OnSubgChbox(self, event):
  690. edit_subg = self.subg_chbox.GetValue()
  691. self.SubgChbox(edit_subg)
  692. def SubgChbox(self, edit_subg):
  693. self._checkChange()
  694. if edit_subg:
  695. self.edit_subg = edit_subg
  696. self.SubGroupSelected()
  697. self._subgroupLayout()
  698. else:
  699. self.edit_subg = edit_subg
  700. self.GroupSelected()
  701. self._groupLayout()
  702. self.SetMinSize(self.GetBestSize())
  703. def _groupLayout(self):
  704. self.subg_panel.Hide()
  705. self.subgListPanel.Hide()
  706. self.gListPanel.Show()
  707. self.Layout()
  708. def _subgroupLayout(self):
  709. self.subg_panel.Show()
  710. self.subgListPanel.Show()
  711. self.gListPanel.Hide()
  712. self.Layout()
  713. def OnAddLayer(self, event):
  714. """Add new layer to listbox"""
  715. dlg = MapLayersDialogForGroups(parent = self, title = _("Add selected map layers into group"))
  716. if dlg.ShowModal() != wx.ID_OK:
  717. dlg.Destroy()
  718. return
  719. layers = dlg.GetMapLayers()
  720. for layer in layers:
  721. if layer not in self.gmaps:
  722. self.gLayerBox.Append(layer)
  723. self.gmaps.append(layer)
  724. self.dataChanged = True
  725. def OnRemoveLayer(self, event):
  726. """Remove layer from listbox"""
  727. while self.gLayerBox.GetSelections():
  728. sel = self.gLayerBox.GetSelections()[0]
  729. m = self.gLayerBox.GetString(sel)
  730. self.gLayerBox.Delete(sel)
  731. self.gmaps.remove(m)
  732. self.dataChanged = True
  733. def GetLayers(self):
  734. """Get layers"""
  735. if self.edit_subg:
  736. layers = []
  737. for maps, sel in self.subgmaps.iteritems():
  738. if sel:
  739. layers.append(maps)
  740. else:
  741. layers = self.gmaps[:]
  742. return layers
  743. def OnGroupSelected(self, event):
  744. """Text changed in group selector"""
  745. # callAfter must be called to close popup before other actions
  746. wx.CallAfter(self.GroupSelected)
  747. def GroupSelected(self):
  748. """Group was selected, check if changes were apllied"""
  749. self._checkChange()
  750. group, s = self.GetSelectedGroup()
  751. maps = list()
  752. groups = self.GetExistGroups()
  753. if group in groups:
  754. maps = self.GetGroupLayers(group)
  755. self.subGroupSelect.Insert(group)
  756. self.gmaps = maps
  757. maps = self._filter(maps)
  758. self.ShowGroupLayers(maps)
  759. self.currentGroup = group
  760. self.SubGroupSelected()
  761. self.ClearNotification()
  762. self._checkGSellAll()
  763. def FilterGroup(self):
  764. maps = self._filter(self.gmaps)
  765. self.ShowGroupLayers(maps)
  766. self._checkGSellAll()
  767. def FilterSubgroup(self):
  768. maps = self._filter(self.gmaps)
  769. self.subgListBox.Set(maps)
  770. for i, m in enumerate(maps):
  771. if m in self.subgmaps.iterkeys() and self.subgmaps[m]:
  772. self.subgListBox.Check(i)
  773. self._checkSubGSellAll()
  774. def SubGroupSelected(self):
  775. """Subgroup was selected, check if changes were apllied"""
  776. self._checkChange()
  777. subgroup = self.subGroupSelect.GetValue().strip()
  778. group = self.currentGroup
  779. gmaps = list()
  780. groups = self.GetExistGroups()
  781. self.subgmaps = {}
  782. if group in groups:
  783. gmaps = self.GetGroupLayers(group)
  784. if subgroup:
  785. maps = self.GetGroupLayers(group, subgroup)
  786. for m in maps:
  787. if m in gmaps:
  788. self.subgmaps[m] = True
  789. else:
  790. self.subgmaps[m] = False
  791. gmaps = self._filter(gmaps)
  792. self.subgListBox.Set(gmaps)
  793. for i, m in enumerate(gmaps):
  794. if self.subgmaps.has_key(m):
  795. self.subgListBox.Check(i)
  796. else:
  797. self.subgListBox.Check(i, False)
  798. self._checkSubGSellAll()
  799. self.currentSubgroup = subgroup
  800. self.ClearNotification()
  801. def _filter(self, data):
  802. """Apply filter for strings in data list"""
  803. flt_data = []
  804. if len(self.flt_pattern) == 0:
  805. flt_data = data[:]
  806. return flt_data
  807. for dt in data:
  808. try:
  809. if re.compile(self.flt_pattern).search(dt):
  810. flt_data.append(dt)
  811. except:
  812. pass
  813. return flt_data
  814. def _checkChange(self):
  815. if self.edit_subg:
  816. self._checkSubgroupChange()
  817. else:
  818. self._checkGroupChange()
  819. def _checkGroupChange(self):
  820. if self.currentGroup and self.dataChanged:
  821. dlg = wx.MessageDialog(self, message = _("Group <%s> was changed, "
  822. "do you want to apply changes?") % self.currentGroup,
  823. caption = _("Unapplied changes"),
  824. style = wx.YES_NO | wx.ICON_QUESTION | wx.YES_DEFAULT)
  825. if dlg.ShowModal() == wx.ID_YES:
  826. self.ApplyChanges()
  827. dlg.Destroy()
  828. self.dataChanged = False
  829. def _checkSubgroupChange(self):
  830. if self.currentSubgroup and self.dataChanged:
  831. dlg = wx.MessageDialog(self, message = _("Subgroup <%s> was changed, "
  832. "do you want to apply changes?") % self.currentSubgroup,
  833. caption = _("Unapplied changes"),
  834. style = wx.YES_NO | wx.ICON_QUESTION | wx.YES_DEFAULT)
  835. if dlg.ShowModal() == wx.ID_YES:
  836. self.ApplyChanges()
  837. dlg.Destroy()
  838. self.dataChanged = False
  839. def ShowGroupLayers(self, mapList):
  840. """Show map layers in currently selected group"""
  841. self.gLayerBox.Set(mapList)
  842. def EditGroup(self, group, subgroup=None):
  843. """Edit selected group"""
  844. layersNew = self.GetLayers()
  845. layersOld = self.GetGroupLayers(group, subgroup)
  846. add = []
  847. remove = []
  848. for layerNew in layersNew:
  849. if layerNew not in layersOld:
  850. add.append(layerNew)
  851. for layerOld in layersOld:
  852. if layerOld not in layersNew:
  853. remove.append(layerOld)
  854. kwargs = {}
  855. if subgroup:
  856. kwargs["subgroup"] = subgroup
  857. ret = None
  858. if remove:
  859. ret = RunCommand('i.group',
  860. parent = self,
  861. group = group,
  862. flags = 'r',
  863. input = ','.join(remove),
  864. **kwargs)
  865. if add:
  866. ret = RunCommand('i.group',
  867. parent = self,
  868. group = group,
  869. input = ','.join(add),
  870. **kwargs)
  871. return ret
  872. def CreateNewGroup(self, group, subgroup):
  873. """Create new group"""
  874. layers = self.GetLayers()
  875. if not layers:
  876. GMessage(parent = self,
  877. message = _("No raster maps selected."))
  878. return 1
  879. kwargs = {}
  880. if subgroup:
  881. kwargs["subgroup"] = subgroup
  882. ret = RunCommand('i.group',
  883. parent = self,
  884. group = group,
  885. input = layers,
  886. **kwargs)
  887. #update subgroup select
  888. self.SubGroupSelected()
  889. return ret
  890. def GetExistGroups(self):
  891. """Returns existing groups in current mapset"""
  892. return grass.list_grouped('group')[grass.gisenv()['MAPSET']]
  893. def GetExistSubgroups(self, group):
  894. """Returns existing subgroups in a group"""
  895. return RunCommand('i.group', group=group,
  896. read=True, flags='sg').splitlines()
  897. def ShowResult(self, group, returnCode, create):
  898. """Show if operation was successfull."""
  899. group += '@' + grass.gisenv()['MAPSET']
  900. if returnCode is None:
  901. label = _("No changes to apply in group <%s>.") % group
  902. elif returnCode == 0:
  903. if create:
  904. label = _("Group <%s> was successfully created.") % group
  905. else:
  906. label = _("Group <%s> was successfully changed.") % group
  907. else:
  908. if create:
  909. label = _("Creating of new group <%s> failed.") % group
  910. else:
  911. label = _("Changing of group <%s> failed.") % group
  912. self.infoLabel.SetLabel(label)
  913. wx.FutureCall(4000, self.ClearNotification)
  914. def GetSelectedGroup(self):
  915. """Return currently selected group (without mapset)"""
  916. g = self.groupSelect.GetValue().split('@')[0]
  917. if self.edit_subg:
  918. s = self.subGroupSelect.GetValue()
  919. else:
  920. s = None
  921. return g, s
  922. def GetGroupLayers(self, group, subgroup=None):
  923. """Get layers in group"""
  924. kwargs = dict()
  925. kwargs['group'] = group
  926. if subgroup:
  927. kwargs['subgroup'] = subgroup
  928. res = RunCommand('i.group',
  929. parent = self,
  930. flags = 'g',
  931. read = True, **kwargs)
  932. if not res:
  933. return []
  934. return res.splitlines()
  935. def ClearNotification(self):
  936. """Clear notification string"""
  937. self.infoLabel.SetLabel("")
  938. def ApplyChanges(self):
  939. """Create or edit group"""
  940. group = self.currentGroup
  941. if not group:
  942. GMessage(parent = self,
  943. message = _("No group selected."))
  944. return False
  945. if self.edit_subg and not self.currentSubgroup:
  946. GMessage(parent = self,
  947. message = _("No subgroup selected."))
  948. return 0
  949. if self.edit_subg:
  950. subgroup = self.currentSubgroup
  951. else:
  952. subgroup = None
  953. groups = self.GetExistGroups()
  954. if group in groups:
  955. ret = self.EditGroup(group, subgroup)
  956. self.ShowResult(group = group, returnCode = ret, create = False)
  957. else:
  958. ret = self.CreateNewGroup(group, subgroup)
  959. self.ShowResult(group = group, returnCode = ret, create = True)
  960. self.dataChanged = False
  961. return True
  962. def OnApply(self, event):
  963. """Apply changes"""
  964. self.ApplyChanges()
  965. def OnOk(self, event):
  966. """Apply changes and close dialog"""
  967. if self.ApplyChanges():
  968. self.OnClose(event)
  969. def OnClose(self, event):
  970. """Close dialog"""
  971. if not self.IsModal():
  972. self.Destroy()
  973. event.Skip()
  974. class MapLayersDialogBase(wx.Dialog):
  975. """Base dialog for selecting map layers (raster, vector).
  976. There are 3 subclasses: MapLayersDialogForGroups, MapLayersDialogForModeler,
  977. MapLayersDialog. Base class contains core functionality.
  978. """
  979. def __init__(self, parent, title,
  980. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  981. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  982. style = style, **kwargs)
  983. self.parent = parent # GMFrame or ?
  984. self.applyAddingMapLayers = Signal('MapLayersDialogBase.applyAddingMapLayers')
  985. self.mainSizer = wx.BoxSizer(wx.VERTICAL)
  986. # dialog body
  987. self.bodySizer = self._createDialogBody()
  988. self.mainSizer.Add(item = self.bodySizer, proportion = 1,
  989. flag = wx.EXPAND | wx.ALL, border = 5)
  990. # update list of layer to be loaded
  991. self.map_layers = [] # list of map layers (full list type/mapset)
  992. self.LoadMapLayers(self.GetLayerType(cmd = True),
  993. self.mapset.GetStringSelection())
  994. self._fullyQualifiedNames()
  995. self._modelerDSeries()
  996. # buttons
  997. btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
  998. btnOk = wx.Button(parent = self, id = wx.ID_OK)
  999. btnOk.SetDefault()
  1000. # sizers & do layout
  1001. self.btnSizer = wx.StdDialogButtonSizer()
  1002. self.btnSizer.AddButton(btnCancel)
  1003. self.btnSizer.AddButton(btnOk)
  1004. self._addApplyButton()
  1005. self.btnSizer.Realize()
  1006. self.mainSizer.Add(item = self.btnSizer, proportion = 0,
  1007. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  1008. self.SetSizer(self.mainSizer)
  1009. self.mainSizer.Fit(self)
  1010. # set dialog min size
  1011. self.SetMinSize(self.GetSize())
  1012. def _modelerDSeries(self):
  1013. """Method used only by MapLayersDialogForModeler,
  1014. for other subclasses does nothing.
  1015. """
  1016. pass
  1017. def _addApplyButton(self):
  1018. """Method used only by MapLayersDialog,
  1019. for other subclasses does nothing.
  1020. """
  1021. pass
  1022. def _fullyQualifiedNames(self):
  1023. """Adds CheckBox which determines is fully qualified names are retuned.
  1024. """
  1025. self.fullyQualified = wx.CheckBox(parent = self, id = wx.ID_ANY,
  1026. label = _("Use fully-qualified map names"))
  1027. self.fullyQualified.SetValue(True)
  1028. self.mainSizer.Add(item = self.fullyQualified, proportion = 0,
  1029. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1030. def _useFullyQualifiedNames(self):
  1031. return self.fullyQualified.IsChecked()
  1032. def _layerTypes(self):
  1033. """Determines which layer types can be chosen.
  1034. Valid values:
  1035. - raster
  1036. - raster3d
  1037. - vector
  1038. """
  1039. return [_('raster'), _('3D raster'), _('vector')]
  1040. def _selectAll(self):
  1041. """Check all layers by default"""
  1042. return True
  1043. def _createDialogBody(self):
  1044. bodySizer = wx.GridBagSizer(vgap = 3, hgap = 3)
  1045. # layer type
  1046. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Map type:")),
  1047. flag = wx.ALIGN_CENTER_VERTICAL,
  1048. pos = (0,0))
  1049. self.layerType = wx.Choice(parent = self, id = wx.ID_ANY,
  1050. choices = self._layerTypes(), size = (100,-1))
  1051. self.layerType.SetSelection(0)
  1052. bodySizer.Add(item = self.layerType,
  1053. pos = (0,1))
  1054. self.layerType.Bind(wx.EVT_CHOICE, self.OnChangeParams)
  1055. # select toggle
  1056. self.toggle = wx.CheckBox(parent = self, id = wx.ID_ANY,
  1057. label = _("Select toggle"))
  1058. self.toggle.SetValue(self._selectAll())
  1059. bodySizer.Add(item = self.toggle,
  1060. flag = wx.ALIGN_CENTER_VERTICAL,
  1061. pos = (0,2))
  1062. # mapset filter
  1063. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Mapset:")),
  1064. flag = wx.ALIGN_CENTER_VERTICAL,
  1065. pos = (1,0))
  1066. self.mapset = MapsetSelect(parent = self, searchPath = True)
  1067. self.mapset.SetStringSelection(grass.gisenv()['MAPSET'])
  1068. bodySizer.Add(item = self.mapset,
  1069. pos = (1,1), span = (1, 2))
  1070. # map name filter
  1071. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Pattern:")),
  1072. flag = wx.ALIGN_CENTER_VERTICAL,
  1073. pos = (2,0))
  1074. self.filter = wx.TextCtrl(parent = self, id = wx.ID_ANY,
  1075. value = "",
  1076. size = (250,-1))
  1077. bodySizer.Add(item = self.filter,
  1078. flag = wx.EXPAND,
  1079. pos = (2,1), span = (1, 2))
  1080. self.filter.SetFocus()
  1081. #TODO same text in GroupDialog
  1082. self.filter.SetToolTipString(_("Put here a regular expression."
  1083. " Characters '.*' stand for anything,"
  1084. " character '^' stands for the beginning"
  1085. " and '$' for the end."))
  1086. # layer list
  1087. bodySizer.Add(item = wx.StaticText(parent = self, label = _("List of maps:")),
  1088. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_TOP,
  1089. pos = (3,0))
  1090. self.layers = wx.CheckListBox(parent = self, id = wx.ID_ANY,
  1091. size = (250, 100),
  1092. choices = [])
  1093. bodySizer.Add(item = self.layers,
  1094. flag = wx.EXPAND,
  1095. pos = (3,1), span = (1, 2))
  1096. bodySizer.AddGrowableCol(1)
  1097. bodySizer.AddGrowableRow(3)
  1098. # bindings
  1099. self.mapset.Bind(wx.EVT_TEXT, self.OnChangeParams)
  1100. self.layers.Bind(wx.EVT_RIGHT_DOWN, self.OnMenu)
  1101. self.filter.Bind(wx.EVT_TEXT, self.OnFilter)
  1102. self.toggle.Bind(wx.EVT_CHECKBOX, self.OnToggle)
  1103. return bodySizer
  1104. def LoadMapLayers(self, type, mapset):
  1105. """Load list of map layers
  1106. :param str type: layer type ('raster' or 'vector')
  1107. :param str mapset: mapset name
  1108. """
  1109. self.map_layers = grass.list_grouped(type = type)[mapset]
  1110. self.layers.Set(natural_sort(self.map_layers))
  1111. # check all items by default
  1112. for item in range(self.layers.GetCount()):
  1113. self.layers.Check(item, check = self._selectAll())
  1114. def OnChangeParams(self, event):
  1115. """Filter parameters changed by user"""
  1116. # update list of layer to be loaded
  1117. self.LoadMapLayers(self.GetLayerType(cmd = True),
  1118. self.mapset.GetStringSelection())
  1119. event.Skip()
  1120. def OnMenu(self, event):
  1121. """Table description area, context menu"""
  1122. if not hasattr(self, "popupID1"):
  1123. self.popupDataID1 = wx.NewId()
  1124. self.popupDataID2 = wx.NewId()
  1125. self.popupDataID3 = wx.NewId()
  1126. self.Bind(wx.EVT_MENU, self.OnSelectAll, id = self.popupDataID1)
  1127. self.Bind(wx.EVT_MENU, self.OnSelectInvert, id = self.popupDataID2)
  1128. self.Bind(wx.EVT_MENU, self.OnDeselectAll, id = self.popupDataID3)
  1129. # generate popup-menu
  1130. menu = wx.Menu()
  1131. menu.Append(self.popupDataID1, _("Select all"))
  1132. menu.Append(self.popupDataID2, _("Invert selection"))
  1133. menu.Append(self.popupDataID3, _("Deselect all"))
  1134. self.PopupMenu(menu)
  1135. menu.Destroy()
  1136. def OnSelectAll(self, event):
  1137. """Select all map layer from list"""
  1138. for item in range(self.layers.GetCount()):
  1139. self.layers.Check(item, True)
  1140. def OnSelectInvert(self, event):
  1141. """Invert current selection"""
  1142. for item in range(self.layers.GetCount()):
  1143. if self.layers.IsChecked(item):
  1144. self.layers.Check(item, False)
  1145. else:
  1146. self.layers.Check(item, True)
  1147. def OnDeselectAll(self, event):
  1148. """Select all map layer from list"""
  1149. for item in range(self.layers.GetCount()):
  1150. self.layers.Check(item, False)
  1151. def OnFilter(self, event):
  1152. """Apply filter for map names"""
  1153. if len(event.GetString()) == 0:
  1154. self.layers.Set(self.map_layers)
  1155. return
  1156. list = []
  1157. for layer in self.map_layers:
  1158. try:
  1159. if re.compile(event.GetString()).search(layer):
  1160. list.append(layer)
  1161. except:
  1162. pass
  1163. self.layers.Set(list)
  1164. self.OnSelectAll(None)
  1165. event.Skip()
  1166. def OnToggle(self, event):
  1167. """Select toggle (check or uncheck all layers)"""
  1168. check = event.Checked()
  1169. for item in range(self.layers.GetCount()):
  1170. self.layers.Check(item, check)
  1171. event.Skip()
  1172. def GetMapLayers(self):
  1173. """Return list of checked map layers"""
  1174. layerNames = []
  1175. for indx in self.layers.GetSelections():
  1176. # layers.append(self.layers.GetStringSelec(indx))
  1177. pass
  1178. mapset = self.mapset.GetStringSelection()
  1179. for item in range(self.layers.GetCount()):
  1180. if not self.layers.IsChecked(item):
  1181. continue
  1182. if self._useFullyQualifiedNames():
  1183. layerNames.append(self.layers.GetString(item) + '@' + mapset)
  1184. else:
  1185. layerNames.append(self.layers.GetString(item))
  1186. return layerNames
  1187. def GetLayerType(self, cmd = False):
  1188. """Get selected layer type
  1189. :param bool cmd: True for g.list
  1190. """
  1191. if not cmd:
  1192. return self.layerType.GetStringSelection()
  1193. sel = self.layerType.GetSelection()
  1194. if sel == 0:
  1195. ltype = 'raster'
  1196. elif sel == 1:
  1197. ltype = 'raster_3d'
  1198. else:
  1199. ltype = 'vector'
  1200. return ltype
  1201. class MapLayersDialog(MapLayersDialogBase):
  1202. """Subclass of MapLayersDialogBase used in Layer Manager.
  1203. Contains apply button, which sends wxApplyMapLayers event.
  1204. """
  1205. def __init__(self, parent, title, **kwargs):
  1206. MapLayersDialogBase.__init__(self, parent = parent, title = title, **kwargs)
  1207. def _addApplyButton(self):
  1208. btnApply = wx.Button(parent = self, id = wx.ID_APPLY)
  1209. self.btnSizer.AddButton(btnApply)
  1210. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1211. def OnApply(self, event):
  1212. self.applyAddingMapLayers.emit(mapLayers = self.GetMapLayers(),
  1213. ltype = self.GetLayerType(cmd = True))
  1214. class MapLayersDialogForGroups(MapLayersDialogBase):
  1215. """Subclass of MapLayersDialogBase used for specyfying maps in an imagery group.
  1216. Shows only raster maps.
  1217. """
  1218. def __init__(self, parent, title, **kwargs):
  1219. MapLayersDialogBase.__init__(self, parent = parent, title = title, **kwargs)
  1220. def _layerTypes(self):
  1221. return [_('raster'),]
  1222. def _selectAll(self):
  1223. """Could be overriden"""
  1224. return False
  1225. def _fullyQualifiedNames(self):
  1226. pass
  1227. def _useFullyQualifiedNames(self):
  1228. return True
  1229. class MapLayersDialogForModeler(MapLayersDialogBase):
  1230. """Subclass of MapLayersDialogBase used in Modeler.
  1231. """
  1232. def __init__(self, parent, title, **kwargs):
  1233. MapLayersDialogBase.__init__(self, parent = parent, title = title, **kwargs)
  1234. def _modelerDSeries(self):
  1235. self.dseries = wx.CheckBox(parent = self, id = wx.ID_ANY,
  1236. label = _("Dynamic series (%s)") % 'g.list')
  1237. self.dseries.SetValue(False)
  1238. self.mainSizer.Add(item = self.dseries, proportion = 0,
  1239. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1240. def GetDSeries(self):
  1241. """Used by modeler only
  1242. :return: g.list command
  1243. """
  1244. if not self.dseries or not self.dseries.IsChecked():
  1245. return ''
  1246. cond = 'map in `g.list type=%s ' % self.GetLayerType(cmd = True)
  1247. patt = self.filter.GetValue()
  1248. if patt:
  1249. cond += 'pattern=%s ' % patt
  1250. cond += 'mapset=%s`' % self.mapset.GetStringSelection()
  1251. return cond
  1252. class SetOpacityDialog(wx.Dialog):
  1253. """Set opacity of map layers.
  1254. Dialog expects opacity between 0 and 1 and returns this range, too.
  1255. """
  1256. def __init__(self, parent, id = wx.ID_ANY, title = _("Set Map Layer Opacity"),
  1257. size = wx.DefaultSize, pos = wx.DefaultPosition,
  1258. style = wx.DEFAULT_DIALOG_STYLE, opacity = 1):
  1259. self.parent = parent # GMFrame
  1260. self.opacity = opacity # current opacity
  1261. super(SetOpacityDialog, self).__init__(parent, id = id, pos = pos,
  1262. size = size, style = style, title = title)
  1263. self.applyOpacity = Signal('SetOpacityDialog.applyOpacity')
  1264. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1265. sizer = wx.BoxSizer(wx.VERTICAL)
  1266. box = wx.GridBagSizer(vgap = 5, hgap = 5)
  1267. self.value = wx.Slider(panel, id = wx.ID_ANY, value = int(self.opacity * 100),
  1268. style = wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | \
  1269. wx.SL_TOP | wx.SL_LABELS,
  1270. minValue = 0, maxValue = 100,
  1271. size = (350, -1))
  1272. box.Add(item = self.value,
  1273. flag = wx.ALIGN_CENTRE, pos = (0, 0), span = (1, 2))
  1274. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1275. label = _("transparent")),
  1276. pos = (1, 0))
  1277. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1278. label = _("opaque")),
  1279. flag = wx.ALIGN_RIGHT,
  1280. pos = (1, 1))
  1281. sizer.Add(item = box, proportion = 0,
  1282. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1283. line = wx.StaticLine(parent = panel, id = wx.ID_ANY,
  1284. style = wx.LI_HORIZONTAL)
  1285. sizer.Add(item = line, proportion = 0,
  1286. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1287. # buttons
  1288. btnsizer = wx.StdDialogButtonSizer()
  1289. btnOK = wx.Button(parent = panel, id = wx.ID_OK)
  1290. btnOK.SetDefault()
  1291. btnsizer.AddButton(btnOK)
  1292. btnCancel = wx.Button(parent = panel, id = wx.ID_CANCEL)
  1293. btnsizer.AddButton(btnCancel)
  1294. btnApply = wx.Button(parent = panel, id = wx.ID_APPLY)
  1295. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1296. btnsizer.AddButton(btnApply)
  1297. btnsizer.Realize()
  1298. sizer.Add(item = btnsizer, proportion = 0,
  1299. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1300. panel.SetSizer(sizer)
  1301. sizer.Fit(panel)
  1302. self.SetSize(self.GetBestSize())
  1303. self.Layout()
  1304. def GetOpacity(self):
  1305. """Button 'OK' pressed"""
  1306. # return opacity value
  1307. opacity = float(self.value.GetValue()) / 100
  1308. return opacity
  1309. def OnApply(self, event):
  1310. self.applyOpacity.emit(value = self.GetOpacity())
  1311. def GetImageHandlers(image):
  1312. """Get list of supported image handlers"""
  1313. lext = list()
  1314. ltype = list()
  1315. for h in image.GetHandlers():
  1316. lext.append(h.GetExtension())
  1317. filetype = ''
  1318. if 'png' in lext:
  1319. filetype += "PNG file (*.png)|*.png|"
  1320. ltype.append({ 'type' : wx.BITMAP_TYPE_PNG,
  1321. 'ext' : 'png' })
  1322. filetype += "BMP file (*.bmp)|*.bmp|"
  1323. ltype.append({ 'type' : wx.BITMAP_TYPE_BMP,
  1324. 'ext' : 'bmp' })
  1325. if 'gif' in lext:
  1326. filetype += "GIF file (*.gif)|*.gif|"
  1327. ltype.append({ 'type' : wx.BITMAP_TYPE_GIF,
  1328. 'ext' : 'gif' })
  1329. if 'jpg' in lext:
  1330. filetype += "JPG file (*.jpg)|*.jpg|"
  1331. ltype.append({ 'type' : wx.BITMAP_TYPE_JPEG,
  1332. 'ext' : 'jpg' })
  1333. if 'pcx' in lext:
  1334. filetype += "PCX file (*.pcx)|*.pcx|"
  1335. ltype.append({ 'type' : wx.BITMAP_TYPE_PCX,
  1336. 'ext' : 'pcx' })
  1337. if 'pnm' in lext:
  1338. filetype += "PNM file (*.pnm)|*.pnm|"
  1339. ltype.append({ 'type' : wx.BITMAP_TYPE_PNM,
  1340. 'ext' : 'pnm' })
  1341. if 'tif' in lext:
  1342. filetype += "TIF file (*.tif)|*.tif|"
  1343. ltype.append({ 'type' : wx.BITMAP_TYPE_TIF,
  1344. 'ext' : 'tif' })
  1345. if 'xpm' in lext:
  1346. filetype += "XPM file (*.xpm)|*.xpm"
  1347. ltype.append({ 'type' : wx.BITMAP_TYPE_XPM,
  1348. 'ext' : 'xpm' })
  1349. return filetype, ltype
  1350. class ImageSizeDialog(wx.Dialog):
  1351. """Set size for saved graphic file"""
  1352. def __init__(self, parent, id = wx.ID_ANY, title = _("Set image size"),
  1353. style = wx.DEFAULT_DIALOG_STYLE, **kwargs):
  1354. self.parent = parent
  1355. wx.Dialog.__init__(self, parent, id = id, style = style, title = title, **kwargs)
  1356. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1357. self.box = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1358. label = ' % s' % _("Image size"))
  1359. size = self.parent.GetWindow().GetClientSize()
  1360. self.width = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1361. style = wx.SP_ARROW_KEYS)
  1362. self.width.SetRange(20, 1e6)
  1363. self.width.SetValue(size.width)
  1364. wx.CallAfter(self.width.SetFocus)
  1365. self.height = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1366. style = wx.SP_ARROW_KEYS)
  1367. self.height.SetRange(20, 1e6)
  1368. self.height.SetValue(size.height)
  1369. self.template = wx.Choice(parent = self.panel, id = wx.ID_ANY,
  1370. size = (125, -1),
  1371. choices = [ "",
  1372. "640x480",
  1373. "800x600",
  1374. "1024x768",
  1375. "1280x960",
  1376. "1600x1200",
  1377. "1920x1440" ])
  1378. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  1379. self.btnOK.SetDefault()
  1380. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1381. self.template.Bind(wx.EVT_CHOICE, self.OnTemplate)
  1382. self._layout()
  1383. self.SetSize(self.GetBestSize())
  1384. def _layout(self):
  1385. """Do layout"""
  1386. sizer = wx.BoxSizer(wx.VERTICAL)
  1387. # body
  1388. box = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  1389. fbox = wx.FlexGridSizer(cols = 2, vgap = 5, hgap = 5)
  1390. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1391. label = _("Width:")),
  1392. flag = wx.ALIGN_CENTER_VERTICAL)
  1393. fbox.Add(item = self.width)
  1394. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1395. label = _("Height:")),
  1396. flag = wx.ALIGN_CENTER_VERTICAL)
  1397. fbox.Add(item = self.height)
  1398. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1399. label = _("Template:")),
  1400. flag = wx.ALIGN_CENTER_VERTICAL)
  1401. fbox.Add(item = self.template)
  1402. box.Add(item = fbox, proportion = 1,
  1403. flag = wx.EXPAND | wx.ALL, border = 5)
  1404. sizer.Add(item = box, proportion = 1,
  1405. flag=wx.EXPAND | wx.ALL, border = 3)
  1406. # buttons
  1407. btnsizer = wx.StdDialogButtonSizer()
  1408. btnsizer.AddButton(self.btnOK)
  1409. btnsizer.AddButton(self.btnCancel)
  1410. btnsizer.Realize()
  1411. sizer.Add(item = btnsizer, proportion = 0,
  1412. flag = wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
  1413. self.panel.SetSizer(sizer)
  1414. sizer.Fit(self.panel)
  1415. self.Layout()
  1416. def GetValues(self):
  1417. """Get width/height values"""
  1418. return self.width.GetValue(), self.height.GetValue()
  1419. def OnTemplate(self, event):
  1420. """Template selected"""
  1421. sel = event.GetString()
  1422. if not sel:
  1423. width, height = self.parent.GetWindow().GetClientSize()
  1424. else:
  1425. width, height = map(int, sel.split('x'))
  1426. self.width.SetValue(width)
  1427. self.height.SetValue(height)
  1428. class SqlQueryFrame(wx.Frame):
  1429. def __init__(self, parent, id = wx.ID_ANY,
  1430. title = _("GRASS GIS SQL Query Utility"),
  1431. *kwargs):
  1432. """SQL Query Utility window
  1433. """
  1434. self.parent = parent
  1435. wx.Frame.__init__(self, parent = parent, id = id, title = title, *kwargs)
  1436. self.SetIcon(wx.Icon(os.path.join(globalvar.ICONDIR, 'grass_sql.ico'), wx.BITMAP_TYPE_ICO))
  1437. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1438. self.sqlBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1439. label = _(" SQL statement "))
  1440. self.sql = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  1441. style = wx.TE_MULTILINE)
  1442. self.btnApply = wx.Button(parent = self.panel, id = wx.ID_APPLY)
  1443. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1444. self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, self.btnCancel)
  1445. self._layout()
  1446. self.SetMinSize(wx.Size(300, 150))
  1447. self.SetSize(wx.Size(500, 200))
  1448. def _layout(self):
  1449. """Do layout"""
  1450. sizer = wx.BoxSizer(wx.VERTICAL)
  1451. sqlSizer = wx.StaticBoxSizer(self.sqlBox, wx.HORIZONTAL)
  1452. sqlSizer.Add(item = self.sql, proportion = 1,
  1453. flag = wx.EXPAND)
  1454. btnSizer = wx.StdDialogButtonSizer()
  1455. btnSizer.AddButton(self.btnApply)
  1456. btnSizer.AddButton(self.btnCancel)
  1457. btnSizer.Realize()
  1458. sizer.Add(item = sqlSizer, proportion = 1,
  1459. flag = wx.EXPAND | wx.ALL, border = 5)
  1460. sizer.Add(item = btnSizer, proportion = 0,
  1461. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1462. self.panel.SetSizer(sizer)
  1463. self.Layout()
  1464. def OnCloseWindow(self, event):
  1465. """Close window
  1466. """
  1467. self.Close()
  1468. class SymbolDialog(wx.Dialog):
  1469. """Dialog for GRASS symbols selection.
  1470. Dialog is called in gui_core::forms module.
  1471. """
  1472. def __init__(self, parent, symbolPath, currentSymbol = None, title = _("Symbols")):
  1473. """Dialog constructor.
  1474. It is assumed that symbolPath contains folders with symbols.
  1475. :param parent: dialog parent
  1476. :param symbolPath: absolute path to symbols
  1477. :param currentSymbol: currently selected symbol (e.g. 'basic/x')
  1478. :param title: dialog title
  1479. """
  1480. wx.Dialog.__init__(self, parent = parent, title = title, id = wx.ID_ANY)
  1481. self.symbolPath = symbolPath
  1482. self.currentSymbol = currentSymbol # default basic/x
  1483. self.selected = None
  1484. self.selectedDir = None
  1485. self._layout()
  1486. def _layout(self):
  1487. mainPanel = wx.Panel(self, id = wx.ID_ANY)
  1488. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1489. vSizer = wx.BoxSizer( wx.VERTICAL)
  1490. fgSizer = wx.FlexGridSizer(rows = 2, vgap = 5, hgap = 5)
  1491. self.folderChoice = wx.Choice(mainPanel, id = wx.ID_ANY, choices = os.listdir(self.symbolPath))
  1492. self.folderChoice.Bind(wx.EVT_CHOICE, self.OnFolderSelect)
  1493. fgSizer.Add(item = wx.StaticText(mainPanel, id = wx.ID_ANY, label = _("Symbol directory:")),
  1494. proportion = 0,
  1495. flag = wx.ALIGN_CENTER_VERTICAL)
  1496. fgSizer.Add(item = self.folderChoice, proportion = 0,
  1497. flag = wx.ALIGN_CENTER, border = 0)
  1498. self.infoLabel = wx.StaticText(mainPanel, id = wx.ID_ANY)
  1499. fgSizer.Add(wx.StaticText(mainPanel, id = wx.ID_ANY, label = _("Symbol name:")),
  1500. flag = wx.ALIGN_CENTRE_VERTICAL)
  1501. fgSizer.Add(self.infoLabel, proportion = 0,
  1502. flag = wx.ALIGN_CENTRE_VERTICAL)
  1503. vSizer.Add(fgSizer, proportion = 0, flag = wx.ALL, border = 5)
  1504. self.panels = self._createSymbolPanels(mainPanel)
  1505. for panel in self.panels:
  1506. vSizer.Add(panel, proportion = 0, flag = wx.ALL | wx.EXPAND, border = 5)
  1507. mainSizer.Add(vSizer, proportion = 1, flag = wx.ALL| wx.EXPAND, border = 5)
  1508. self.btnCancel = wx.Button(parent = mainPanel, id = wx.ID_CANCEL)
  1509. self.btnOK = wx.Button(parent = mainPanel, id = wx.ID_OK)
  1510. self.btnOK.SetDefault()
  1511. self.btnOK.Enable(False)
  1512. # buttons
  1513. btnSizer = wx.StdDialogButtonSizer()
  1514. btnSizer.AddButton(self.btnCancel)
  1515. btnSizer.AddButton(self.btnOK)
  1516. btnSizer.Realize()
  1517. mainSizer.Add(item = btnSizer, proportion = 0,
  1518. flag = wx.EXPAND | wx.ALL, border = 5)
  1519. # show panel with the largest number of images and fit size
  1520. count = []
  1521. for folder in os.listdir(self.symbolPath):
  1522. count.append(len(os.listdir(os.path.join(self.symbolPath, folder))))
  1523. index = count.index(max(count))
  1524. self.folderChoice.SetSelection(index)
  1525. self.OnFolderSelect(None)
  1526. self.infoLabel.Show()
  1527. mainPanel.SetSizerAndFit(mainSizer)
  1528. self.SetSize(self.GetBestSize())
  1529. # show currently selected symbol
  1530. if self.currentSymbol:
  1531. # set directory
  1532. self.selectedDir, self.selected = os.path.split(self.currentSymbol)
  1533. self.folderChoice.SetStringSelection(self.selectedDir)
  1534. # select symbol
  1535. panelIdx = self.folderChoice.GetSelection()
  1536. for panel in self.symbolPanels[panelIdx]:
  1537. if panel.GetName() == self.selected:
  1538. panel.Select()
  1539. else:
  1540. self.folderChoice.SetSelection(0)
  1541. self.OnFolderSelect(None)
  1542. def _createSymbolPanels(self, parent):
  1543. """Creates multiple panels with symbols.
  1544. Panels are shown/hidden according to selected folder."""
  1545. folders = os.listdir(self.symbolPath)
  1546. panels = []
  1547. self.symbolPanels = []
  1548. for folder in folders:
  1549. panel = wx.Panel(parent, style = wx.BORDER_RAISED)
  1550. sizer = wx.GridSizer(cols = 6, vgap = 3, hgap = 3)
  1551. images = self._getSymbols(path = os.path.join(self.symbolPath, folder))
  1552. symbolPanels = []
  1553. for img in images:
  1554. iP = SingleSymbolPanel(parent = panel, symbolPath = img)
  1555. iP.symbolSelectionChanged.connect(self.SelectionChanged)
  1556. sizer.Add(item = iP, proportion = 0, flag = wx.ALIGN_CENTER)
  1557. symbolPanels.append(iP)
  1558. panel.SetSizerAndFit(sizer)
  1559. panel.Hide()
  1560. panels.append(panel)
  1561. self.symbolPanels.append(symbolPanels)
  1562. return panels
  1563. def _getSymbols(self, path):
  1564. # we assume that images are in subfolders (1 level only)
  1565. imageList = []
  1566. for image in os.listdir(path):
  1567. imageList.append(os.path.join(path, image))
  1568. return sorted(imageList)
  1569. def OnFolderSelect(self, event):
  1570. """Selected folder with symbols changed."""
  1571. idx = self.folderChoice.GetSelection()
  1572. for i in range(len(self.panels)):
  1573. sizer = self.panels[i].GetContainingSizer()
  1574. sizer.Show(self.panels[i], i == idx, recursive = True)
  1575. sizer.Layout()
  1576. if self.selectedDir == self.folderChoice.GetStringSelection():
  1577. self.btnOK.Enable()
  1578. self.infoLabel.SetLabel(self.selected)
  1579. else:
  1580. self.btnOK.Disable()
  1581. self.infoLabel.SetLabel('')
  1582. def SelectionChanged(self, name, doubleClick):
  1583. """Selected symbol changed."""
  1584. if doubleClick:
  1585. self.EndModal(wx.ID_OK)
  1586. # deselect all
  1587. for i in range(len(self.panels)):
  1588. for panel in self.symbolPanels[i]:
  1589. if panel.GetName() != name:
  1590. panel.Deselect()
  1591. self.btnOK.Enable()
  1592. self.selected = name
  1593. self.selectedDir = self.folderChoice.GetStringSelection()
  1594. self.infoLabel.SetLabel(name)
  1595. def GetSelectedSymbolName(self):
  1596. """Returns currently selected symbol name (e.g. 'basic/x').
  1597. """
  1598. # separator must be '/' and not dependent on OS
  1599. return self.selectedDir + '/' + self.selected
  1600. def GetSelectedSymbolPath(self):
  1601. """Returns currently selected symbol full path.
  1602. """
  1603. return os.path.join(self.symbolPath, self.selectedDir, self.selected)
  1604. class TextEntryDialog(wx.Dialog):
  1605. """Simple dialog with text field.
  1606. It differs from wx.TextEntryDialog because it allows adding validator.
  1607. """
  1608. def __init__(self, parent, message, caption='',
  1609. defaultValue='', validator=wx.DefaultValidator,
  1610. style=wx.OK | wx.CANCEL | wx.CENTRE, textStyle=0, textSize=(300, -1),
  1611. **kwargs):
  1612. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=caption, **kwargs)
  1613. vbox = wx.BoxSizer(wx.VERTICAL)
  1614. stline = wx.StaticText(self, id=wx.ID_ANY, label=message)
  1615. vbox.Add(item=stline, proportion=0, flag=wx.EXPAND | wx.ALL, border=10)
  1616. self._textCtrl = wx.TextCtrl(self, id=wx.ID_ANY,
  1617. value=defaultValue, validator=validator, style=textStyle)
  1618. self._textCtrl.SetInitialSize(textSize)
  1619. vbox.Add(item=self._textCtrl, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=10)
  1620. self._textCtrl.SetFocus()
  1621. sizer = self.CreateSeparatedButtonSizer(style)
  1622. vbox.Add(item=sizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  1623. self.SetSizerAndFit(vbox)
  1624. def GetValue(self):
  1625. return self._textCtrl.GetValue()
  1626. def SetValue(self, value):
  1627. self._textCtrl.SetValue(value)
  1628. class HyperlinkDialog(wx.Dialog):
  1629. """Dialog for displaying message with hyperlink."""
  1630. def __init__(self, parent, title, message, hyperlink,
  1631. hyperlinkLabel=None, style=wx.OK):
  1632. """Constructor
  1633. :param parent: gui parent
  1634. :param title: dialog title
  1635. :param message: message
  1636. :param hyperlink: url
  1637. :param hyperlinkLabel: label shown instead of url
  1638. :param style: button style
  1639. """
  1640. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title,
  1641. style=wx.DEFAULT_DIALOG_STYLE)
  1642. sizer = wx.BoxSizer(wx.VERTICAL)
  1643. label = wx.StaticText(self, label=message)
  1644. sizer.Add(item=label, proportion=0, flag=wx.ALIGN_CENTRE|wx.ALL, border=10)
  1645. hyperlinkLabel = hyperlinkLabel if hyperlinkLabel else hyperlink
  1646. hyperlinkCtrl = wx.HyperlinkCtrl(self, id=wx.ID_ANY,
  1647. label=hyperlinkLabel, url=hyperlink,
  1648. style=wx.HL_ALIGN_LEFT|wx.HL_CONTEXTMENU)
  1649. sizer.Add(item=hyperlinkCtrl, proportion=0, flag=wx.EXPAND|wx.ALL, border=10)
  1650. btnsizer = self.CreateSeparatedButtonSizer(style)
  1651. sizer.Add(item=btnsizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  1652. self.SetSizer(sizer)
  1653. sizer.Fit(self)
  1654. class QuitDialog(wx.Dialog):
  1655. def __init__(self, parent, title=_("Quit GRASS GIS"), id=wx.ID_ANY,
  1656. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  1657. """Dialog to quit GRASS
  1658. :param parent: window
  1659. """
  1660. wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)
  1661. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1662. self._icon = wx.StaticBitmap(parent=self.panel, id=wx.ID_ANY,
  1663. bitmap=wx.ArtProvider().GetBitmap(wx.ART_QUESTION, client=wx.ART_MESSAGE_BOX))
  1664. self.informLabel = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  1665. label=_("Do you want to quit GRASS including shell "
  1666. "prompt or just close the GUI?"))
  1667. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1668. self.btnClose = wx.Button(parent = self.panel, id = wx.ID_NO,
  1669. label=_("Close GUI"))
  1670. self.btnClose.SetFocus()
  1671. self.btnQuit = wx.Button(parent = self.panel, id = wx.ID_YES,
  1672. label=_("Quit GRASS GIS"))
  1673. self.btnQuit.SetMinSize((130, self.btnQuit.GetSize()[1]))
  1674. self.btnQuit.SetForegroundColour(wx.Colour(35, 142, 35))
  1675. self.btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  1676. self.btnQuit.Bind(wx.EVT_BUTTON, self.OnQuit)
  1677. self.__layout()
  1678. def __layout(self):
  1679. """Do layout"""
  1680. sizer = wx.BoxSizer(wx.VERTICAL)
  1681. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  1682. btnSizer.Add(item=self.btnCancel, flag=wx.RIGHT, border=5)
  1683. btnSizer.Add(item=self.btnClose, flag=wx.RIGHT, border=5)
  1684. btnSizer.Add(item=self.btnQuit, flag=wx.RIGHT, border=5)
  1685. bodySizer = wx.BoxSizer(wx.HORIZONTAL)
  1686. bodySizer.Add(item=self._icon, flag=wx.RIGHT, border=10)
  1687. bodySizer.Add(item=self.informLabel, proportion=1, flag=wx.EXPAND)
  1688. sizer.Add(item = bodySizer, proportion = 1,
  1689. flag = wx.EXPAND | wx.ALL, border = 15)
  1690. sizer.Add(item = btnSizer, proportion = 0,
  1691. flag = wx.ALL | wx.ALIGN_RIGHT, border = 5)
  1692. self.panel.SetSizer(sizer)
  1693. sizer.Fit(self)
  1694. self.Layout()
  1695. def OnClose(self, event):
  1696. self.EndModal(wx.ID_NO)
  1697. def OnQuit(self, event):
  1698. self.EndModal(wx.ID_YES)