dialogs.py 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  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. list = natural_sort(list)
  1164. self.layers.Set(list)
  1165. self.OnSelectAll(None)
  1166. event.Skip()
  1167. def OnToggle(self, event):
  1168. """Select toggle (check or uncheck all layers)"""
  1169. check = event.Checked()
  1170. for item in range(self.layers.GetCount()):
  1171. self.layers.Check(item, check)
  1172. event.Skip()
  1173. def GetMapLayers(self):
  1174. """Return list of checked map layers"""
  1175. layerNames = []
  1176. for indx in self.layers.GetSelections():
  1177. # layers.append(self.layers.GetStringSelec(indx))
  1178. pass
  1179. mapset = self.mapset.GetStringSelection()
  1180. for item in range(self.layers.GetCount()):
  1181. if not self.layers.IsChecked(item):
  1182. continue
  1183. if self._useFullyQualifiedNames():
  1184. layerNames.append(self.layers.GetString(item) + '@' + mapset)
  1185. else:
  1186. layerNames.append(self.layers.GetString(item))
  1187. return layerNames
  1188. def GetLayerType(self, cmd = False):
  1189. """Get selected layer type
  1190. :param bool cmd: True for g.list
  1191. """
  1192. if not cmd:
  1193. return self.layerType.GetStringSelection()
  1194. sel = self.layerType.GetSelection()
  1195. if sel == 0:
  1196. ltype = 'raster'
  1197. elif sel == 1:
  1198. ltype = 'raster_3d'
  1199. else:
  1200. ltype = 'vector'
  1201. return ltype
  1202. class MapLayersDialog(MapLayersDialogBase):
  1203. """Subclass of MapLayersDialogBase used in Layer Manager.
  1204. Contains apply button, which sends wxApplyMapLayers event.
  1205. """
  1206. def __init__(self, parent, title, **kwargs):
  1207. MapLayersDialogBase.__init__(self, parent = parent, title = title, **kwargs)
  1208. def _addApplyButton(self):
  1209. btnApply = wx.Button(parent = self, id = wx.ID_APPLY)
  1210. self.btnSizer.AddButton(btnApply)
  1211. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1212. def OnApply(self, event):
  1213. self.applyAddingMapLayers.emit(mapLayers = self.GetMapLayers(),
  1214. ltype = self.GetLayerType(cmd = True))
  1215. class MapLayersDialogForGroups(MapLayersDialogBase):
  1216. """Subclass of MapLayersDialogBase used for specyfying maps in an imagery group.
  1217. Shows only raster maps.
  1218. """
  1219. def __init__(self, parent, title, **kwargs):
  1220. MapLayersDialogBase.__init__(self, parent = parent, title = title, **kwargs)
  1221. def _layerTypes(self):
  1222. return [_('raster'),]
  1223. def _selectAll(self):
  1224. """Could be overriden"""
  1225. return False
  1226. def _fullyQualifiedNames(self):
  1227. pass
  1228. def _useFullyQualifiedNames(self):
  1229. return True
  1230. class MapLayersDialogForModeler(MapLayersDialogBase):
  1231. """Subclass of MapLayersDialogBase used in Modeler.
  1232. """
  1233. def __init__(self, parent, title, **kwargs):
  1234. MapLayersDialogBase.__init__(self, parent = parent, title = title, **kwargs)
  1235. def _modelerDSeries(self):
  1236. self.dseries = wx.CheckBox(parent = self, id = wx.ID_ANY,
  1237. label = _("Dynamic series (%s)") % 'g.list')
  1238. self.dseries.SetValue(False)
  1239. self.mainSizer.Add(item = self.dseries, proportion = 0,
  1240. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1241. def GetDSeries(self):
  1242. """Used by modeler only
  1243. :return: g.list command
  1244. """
  1245. if not self.dseries or not self.dseries.IsChecked():
  1246. return ''
  1247. cond = 'map in `g.list type=%s ' % self.GetLayerType(cmd = True)
  1248. patt = self.filter.GetValue()
  1249. if patt:
  1250. cond += 'pattern=%s ' % patt
  1251. cond += 'mapset=%s`' % self.mapset.GetStringSelection()
  1252. return cond
  1253. class SetOpacityDialog(wx.Dialog):
  1254. """Set opacity of map layers.
  1255. Dialog expects opacity between 0 and 1 and returns this range, too.
  1256. """
  1257. def __init__(self, parent, id = wx.ID_ANY, title = _("Set Map Layer Opacity"),
  1258. size = wx.DefaultSize, pos = wx.DefaultPosition,
  1259. style = wx.DEFAULT_DIALOG_STYLE, opacity = 1):
  1260. self.parent = parent # GMFrame
  1261. self.opacity = opacity # current opacity
  1262. super(SetOpacityDialog, self).__init__(parent, id = id, pos = pos,
  1263. size = size, style = style, title = title)
  1264. self.applyOpacity = Signal('SetOpacityDialog.applyOpacity')
  1265. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1266. sizer = wx.BoxSizer(wx.VERTICAL)
  1267. box = wx.GridBagSizer(vgap = 5, hgap = 5)
  1268. self.value = wx.Slider(panel, id = wx.ID_ANY, value = int(self.opacity * 100),
  1269. style = wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | \
  1270. wx.SL_TOP | wx.SL_LABELS,
  1271. minValue = 0, maxValue = 100,
  1272. size = (350, -1))
  1273. box.Add(item = self.value,
  1274. flag = wx.ALIGN_CENTRE, pos = (0, 0), span = (1, 2))
  1275. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1276. label = _("transparent")),
  1277. pos = (1, 0))
  1278. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1279. label = _("opaque")),
  1280. flag = wx.ALIGN_RIGHT,
  1281. pos = (1, 1))
  1282. sizer.Add(item = box, proportion = 0,
  1283. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1284. line = wx.StaticLine(parent = panel, id = wx.ID_ANY,
  1285. style = wx.LI_HORIZONTAL)
  1286. sizer.Add(item = line, proportion = 0,
  1287. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1288. # buttons
  1289. btnsizer = wx.StdDialogButtonSizer()
  1290. btnOK = wx.Button(parent = panel, id = wx.ID_OK)
  1291. btnOK.SetDefault()
  1292. btnsizer.AddButton(btnOK)
  1293. btnCancel = wx.Button(parent = panel, id = wx.ID_CANCEL)
  1294. btnsizer.AddButton(btnCancel)
  1295. btnApply = wx.Button(parent = panel, id = wx.ID_APPLY)
  1296. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1297. btnsizer.AddButton(btnApply)
  1298. btnsizer.Realize()
  1299. sizer.Add(item = btnsizer, proportion = 0,
  1300. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1301. panel.SetSizer(sizer)
  1302. sizer.Fit(panel)
  1303. self.SetSize(self.GetBestSize())
  1304. self.Layout()
  1305. def GetOpacity(self):
  1306. """Button 'OK' pressed"""
  1307. # return opacity value
  1308. opacity = float(self.value.GetValue()) / 100
  1309. return opacity
  1310. def OnApply(self, event):
  1311. self.applyOpacity.emit(value = self.GetOpacity())
  1312. def GetImageHandlers(image):
  1313. """Get list of supported image handlers"""
  1314. lext = list()
  1315. ltype = list()
  1316. for h in image.GetHandlers():
  1317. lext.append(h.GetExtension())
  1318. filetype = ''
  1319. if 'png' in lext:
  1320. filetype += "PNG file (*.png)|*.png|"
  1321. ltype.append({ 'type' : wx.BITMAP_TYPE_PNG,
  1322. 'ext' : 'png' })
  1323. filetype += "BMP file (*.bmp)|*.bmp|"
  1324. ltype.append({ 'type' : wx.BITMAP_TYPE_BMP,
  1325. 'ext' : 'bmp' })
  1326. if 'gif' in lext:
  1327. filetype += "GIF file (*.gif)|*.gif|"
  1328. ltype.append({ 'type' : wx.BITMAP_TYPE_GIF,
  1329. 'ext' : 'gif' })
  1330. if 'jpg' in lext:
  1331. filetype += "JPG file (*.jpg)|*.jpg|"
  1332. ltype.append({ 'type' : wx.BITMAP_TYPE_JPEG,
  1333. 'ext' : 'jpg' })
  1334. if 'pcx' in lext:
  1335. filetype += "PCX file (*.pcx)|*.pcx|"
  1336. ltype.append({ 'type' : wx.BITMAP_TYPE_PCX,
  1337. 'ext' : 'pcx' })
  1338. if 'pnm' in lext:
  1339. filetype += "PNM file (*.pnm)|*.pnm|"
  1340. ltype.append({ 'type' : wx.BITMAP_TYPE_PNM,
  1341. 'ext' : 'pnm' })
  1342. if 'tif' in lext:
  1343. filetype += "TIF file (*.tif)|*.tif|"
  1344. ltype.append({ 'type' : wx.BITMAP_TYPE_TIF,
  1345. 'ext' : 'tif' })
  1346. if 'xpm' in lext:
  1347. filetype += "XPM file (*.xpm)|*.xpm"
  1348. ltype.append({ 'type' : wx.BITMAP_TYPE_XPM,
  1349. 'ext' : 'xpm' })
  1350. return filetype, ltype
  1351. class ImageSizeDialog(wx.Dialog):
  1352. """Set size for saved graphic file"""
  1353. def __init__(self, parent, id = wx.ID_ANY, title = _("Set image size"),
  1354. style = wx.DEFAULT_DIALOG_STYLE, **kwargs):
  1355. self.parent = parent
  1356. wx.Dialog.__init__(self, parent, id = id, style = style, title = title, **kwargs)
  1357. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1358. self.box = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1359. label = ' % s' % _("Image size"))
  1360. size = self.parent.GetWindow().GetClientSize()
  1361. self.width = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1362. style = wx.SP_ARROW_KEYS)
  1363. self.width.SetRange(20, 1e6)
  1364. self.width.SetValue(size.width)
  1365. wx.CallAfter(self.width.SetFocus)
  1366. self.height = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1367. style = wx.SP_ARROW_KEYS)
  1368. self.height.SetRange(20, 1e6)
  1369. self.height.SetValue(size.height)
  1370. self.template = wx.Choice(parent = self.panel, id = wx.ID_ANY,
  1371. size = (125, -1),
  1372. choices = [ "",
  1373. "640x480",
  1374. "800x600",
  1375. "1024x768",
  1376. "1280x960",
  1377. "1600x1200",
  1378. "1920x1440" ])
  1379. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  1380. self.btnOK.SetDefault()
  1381. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1382. self.template.Bind(wx.EVT_CHOICE, self.OnTemplate)
  1383. self._layout()
  1384. self.SetSize(self.GetBestSize())
  1385. def _layout(self):
  1386. """Do layout"""
  1387. sizer = wx.BoxSizer(wx.VERTICAL)
  1388. # body
  1389. box = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  1390. fbox = wx.FlexGridSizer(cols = 2, vgap = 5, hgap = 5)
  1391. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1392. label = _("Width:")),
  1393. flag = wx.ALIGN_CENTER_VERTICAL)
  1394. fbox.Add(item = self.width)
  1395. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1396. label = _("Height:")),
  1397. flag = wx.ALIGN_CENTER_VERTICAL)
  1398. fbox.Add(item = self.height)
  1399. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1400. label = _("Template:")),
  1401. flag = wx.ALIGN_CENTER_VERTICAL)
  1402. fbox.Add(item = self.template)
  1403. box.Add(item = fbox, proportion = 1,
  1404. flag = wx.EXPAND | wx.ALL, border = 5)
  1405. sizer.Add(item = box, proportion = 1,
  1406. flag=wx.EXPAND | wx.ALL, border = 3)
  1407. # buttons
  1408. btnsizer = wx.StdDialogButtonSizer()
  1409. btnsizer.AddButton(self.btnOK)
  1410. btnsizer.AddButton(self.btnCancel)
  1411. btnsizer.Realize()
  1412. sizer.Add(item = btnsizer, proportion = 0,
  1413. flag = wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
  1414. self.panel.SetSizer(sizer)
  1415. sizer.Fit(self.panel)
  1416. self.Layout()
  1417. def GetValues(self):
  1418. """Get width/height values"""
  1419. return self.width.GetValue(), self.height.GetValue()
  1420. def OnTemplate(self, event):
  1421. """Template selected"""
  1422. sel = event.GetString()
  1423. if not sel:
  1424. width, height = self.parent.GetWindow().GetClientSize()
  1425. else:
  1426. width, height = map(int, sel.split('x'))
  1427. self.width.SetValue(width)
  1428. self.height.SetValue(height)
  1429. class SqlQueryFrame(wx.Frame):
  1430. def __init__(self, parent, id = wx.ID_ANY,
  1431. title = _("GRASS GIS SQL Query Utility"),
  1432. *kwargs):
  1433. """SQL Query Utility window
  1434. """
  1435. self.parent = parent
  1436. wx.Frame.__init__(self, parent = parent, id = id, title = title, *kwargs)
  1437. self.SetIcon(wx.Icon(os.path.join(globalvar.ICONDIR, 'grass_sql.ico'), wx.BITMAP_TYPE_ICO))
  1438. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1439. self.sqlBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1440. label = _(" SQL statement "))
  1441. self.sql = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  1442. style = wx.TE_MULTILINE)
  1443. self.btnApply = wx.Button(parent = self.panel, id = wx.ID_APPLY)
  1444. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1445. self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, self.btnCancel)
  1446. self._layout()
  1447. self.SetMinSize(wx.Size(300, 150))
  1448. self.SetSize(wx.Size(500, 200))
  1449. def _layout(self):
  1450. """Do layout"""
  1451. sizer = wx.BoxSizer(wx.VERTICAL)
  1452. sqlSizer = wx.StaticBoxSizer(self.sqlBox, wx.HORIZONTAL)
  1453. sqlSizer.Add(item = self.sql, proportion = 1,
  1454. flag = wx.EXPAND)
  1455. btnSizer = wx.StdDialogButtonSizer()
  1456. btnSizer.AddButton(self.btnApply)
  1457. btnSizer.AddButton(self.btnCancel)
  1458. btnSizer.Realize()
  1459. sizer.Add(item = sqlSizer, proportion = 1,
  1460. flag = wx.EXPAND | wx.ALL, border = 5)
  1461. sizer.Add(item = btnSizer, proportion = 0,
  1462. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1463. self.panel.SetSizer(sizer)
  1464. self.Layout()
  1465. def OnCloseWindow(self, event):
  1466. """Close window
  1467. """
  1468. self.Close()
  1469. class SymbolDialog(wx.Dialog):
  1470. """Dialog for GRASS symbols selection.
  1471. Dialog is called in gui_core::forms module.
  1472. """
  1473. def __init__(self, parent, symbolPath, currentSymbol = None, title = _("Symbols")):
  1474. """Dialog constructor.
  1475. It is assumed that symbolPath contains folders with symbols.
  1476. :param parent: dialog parent
  1477. :param symbolPath: absolute path to symbols
  1478. :param currentSymbol: currently selected symbol (e.g. 'basic/x')
  1479. :param title: dialog title
  1480. """
  1481. wx.Dialog.__init__(self, parent = parent, title = title, id = wx.ID_ANY)
  1482. self.symbolPath = symbolPath
  1483. self.currentSymbol = currentSymbol # default basic/x
  1484. self.selected = None
  1485. self.selectedDir = None
  1486. self._layout()
  1487. def _layout(self):
  1488. mainPanel = wx.Panel(self, id = wx.ID_ANY)
  1489. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1490. vSizer = wx.BoxSizer( wx.VERTICAL)
  1491. fgSizer = wx.FlexGridSizer(rows = 2, vgap = 5, hgap = 5)
  1492. self.folderChoice = wx.Choice(mainPanel, id = wx.ID_ANY, choices = os.listdir(self.symbolPath))
  1493. self.folderChoice.Bind(wx.EVT_CHOICE, self.OnFolderSelect)
  1494. fgSizer.Add(item = wx.StaticText(mainPanel, id = wx.ID_ANY, label = _("Symbol directory:")),
  1495. proportion = 0,
  1496. flag = wx.ALIGN_CENTER_VERTICAL)
  1497. fgSizer.Add(item = self.folderChoice, proportion = 0,
  1498. flag = wx.ALIGN_CENTER, border = 0)
  1499. self.infoLabel = wx.StaticText(mainPanel, id = wx.ID_ANY)
  1500. fgSizer.Add(wx.StaticText(mainPanel, id = wx.ID_ANY, label = _("Symbol name:")),
  1501. flag = wx.ALIGN_CENTRE_VERTICAL)
  1502. fgSizer.Add(self.infoLabel, proportion = 0,
  1503. flag = wx.ALIGN_CENTRE_VERTICAL)
  1504. vSizer.Add(fgSizer, proportion = 0, flag = wx.ALL, border = 5)
  1505. self.panels = self._createSymbolPanels(mainPanel)
  1506. for panel in self.panels:
  1507. vSizer.Add(panel, proportion = 0, flag = wx.ALL | wx.EXPAND, border = 5)
  1508. mainSizer.Add(vSizer, proportion = 1, flag = wx.ALL| wx.EXPAND, border = 5)
  1509. self.btnCancel = wx.Button(parent = mainPanel, id = wx.ID_CANCEL)
  1510. self.btnOK = wx.Button(parent = mainPanel, id = wx.ID_OK)
  1511. self.btnOK.SetDefault()
  1512. self.btnOK.Enable(False)
  1513. # buttons
  1514. btnSizer = wx.StdDialogButtonSizer()
  1515. btnSizer.AddButton(self.btnCancel)
  1516. btnSizer.AddButton(self.btnOK)
  1517. btnSizer.Realize()
  1518. mainSizer.Add(item = btnSizer, proportion = 0,
  1519. flag = wx.EXPAND | wx.ALL, border = 5)
  1520. # show panel with the largest number of images and fit size
  1521. count = []
  1522. for folder in os.listdir(self.symbolPath):
  1523. count.append(len(os.listdir(os.path.join(self.symbolPath, folder))))
  1524. index = count.index(max(count))
  1525. self.folderChoice.SetSelection(index)
  1526. self.OnFolderSelect(None)
  1527. self.infoLabel.Show()
  1528. mainPanel.SetSizerAndFit(mainSizer)
  1529. self.SetSize(self.GetBestSize())
  1530. # show currently selected symbol
  1531. if self.currentSymbol:
  1532. # set directory
  1533. self.selectedDir, self.selected = os.path.split(self.currentSymbol)
  1534. self.folderChoice.SetStringSelection(self.selectedDir)
  1535. # select symbol
  1536. panelIdx = self.folderChoice.GetSelection()
  1537. for panel in self.symbolPanels[panelIdx]:
  1538. if panel.GetName() == self.selected:
  1539. panel.Select()
  1540. else:
  1541. self.folderChoice.SetSelection(0)
  1542. self.OnFolderSelect(None)
  1543. def _createSymbolPanels(self, parent):
  1544. """Creates multiple panels with symbols.
  1545. Panels are shown/hidden according to selected folder."""
  1546. folders = os.listdir(self.symbolPath)
  1547. panels = []
  1548. self.symbolPanels = []
  1549. for folder in folders:
  1550. panel = wx.Panel(parent, style = wx.BORDER_RAISED)
  1551. sizer = wx.GridSizer(cols = 6, vgap = 3, hgap = 3)
  1552. images = self._getSymbols(path = os.path.join(self.symbolPath, folder))
  1553. symbolPanels = []
  1554. for img in images:
  1555. iP = SingleSymbolPanel(parent = panel, symbolPath = img)
  1556. iP.symbolSelectionChanged.connect(self.SelectionChanged)
  1557. sizer.Add(item = iP, proportion = 0, flag = wx.ALIGN_CENTER)
  1558. symbolPanels.append(iP)
  1559. panel.SetSizerAndFit(sizer)
  1560. panel.Hide()
  1561. panels.append(panel)
  1562. self.symbolPanels.append(symbolPanels)
  1563. return panels
  1564. def _getSymbols(self, path):
  1565. # we assume that images are in subfolders (1 level only)
  1566. imageList = []
  1567. for image in os.listdir(path):
  1568. imageList.append(os.path.join(path, image))
  1569. return sorted(imageList)
  1570. def OnFolderSelect(self, event):
  1571. """Selected folder with symbols changed."""
  1572. idx = self.folderChoice.GetSelection()
  1573. for i in range(len(self.panels)):
  1574. sizer = self.panels[i].GetContainingSizer()
  1575. sizer.Show(self.panels[i], i == idx, recursive = True)
  1576. sizer.Layout()
  1577. if self.selectedDir == self.folderChoice.GetStringSelection():
  1578. self.btnOK.Enable()
  1579. self.infoLabel.SetLabel(self.selected)
  1580. else:
  1581. self.btnOK.Disable()
  1582. self.infoLabel.SetLabel('')
  1583. def SelectionChanged(self, name, doubleClick):
  1584. """Selected symbol changed."""
  1585. if doubleClick:
  1586. self.EndModal(wx.ID_OK)
  1587. # deselect all
  1588. for i in range(len(self.panels)):
  1589. for panel in self.symbolPanels[i]:
  1590. if panel.GetName() != name:
  1591. panel.Deselect()
  1592. self.btnOK.Enable()
  1593. self.selected = name
  1594. self.selectedDir = self.folderChoice.GetStringSelection()
  1595. self.infoLabel.SetLabel(name)
  1596. def GetSelectedSymbolName(self):
  1597. """Returns currently selected symbol name (e.g. 'basic/x').
  1598. """
  1599. # separator must be '/' and not dependent on OS
  1600. return self.selectedDir + '/' + self.selected
  1601. def GetSelectedSymbolPath(self):
  1602. """Returns currently selected symbol full path.
  1603. """
  1604. return os.path.join(self.symbolPath, self.selectedDir, self.selected)
  1605. class TextEntryDialog(wx.Dialog):
  1606. """Simple dialog with text field.
  1607. It differs from wx.TextEntryDialog because it allows adding validator.
  1608. """
  1609. def __init__(self, parent, message, caption='',
  1610. defaultValue='', validator=wx.DefaultValidator,
  1611. style=wx.OK | wx.CANCEL | wx.CENTRE, textStyle=0, textSize=(300, -1),
  1612. **kwargs):
  1613. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=caption, **kwargs)
  1614. vbox = wx.BoxSizer(wx.VERTICAL)
  1615. stline = wx.StaticText(self, id=wx.ID_ANY, label=message)
  1616. vbox.Add(item=stline, proportion=0, flag=wx.EXPAND | wx.ALL, border=10)
  1617. self._textCtrl = wx.TextCtrl(self, id=wx.ID_ANY,
  1618. value=defaultValue, validator=validator, style=textStyle)
  1619. self._textCtrl.SetInitialSize(textSize)
  1620. wx.CallAfter(self._textCtrl.SetFocus)
  1621. vbox.Add(item=self._textCtrl, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=10)
  1622. self._textCtrl.SetFocus()
  1623. sizer = self.CreateSeparatedButtonSizer(style)
  1624. vbox.Add(item=sizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  1625. self.SetSizerAndFit(vbox)
  1626. def GetValue(self):
  1627. return self._textCtrl.GetValue()
  1628. def SetValue(self, value):
  1629. self._textCtrl.SetValue(value)
  1630. class HyperlinkDialog(wx.Dialog):
  1631. """Dialog for displaying message with hyperlink."""
  1632. def __init__(self, parent, title, message, hyperlink,
  1633. hyperlinkLabel=None, style=wx.OK):
  1634. """Constructor
  1635. :param parent: gui parent
  1636. :param title: dialog title
  1637. :param message: message
  1638. :param hyperlink: url
  1639. :param hyperlinkLabel: label shown instead of url
  1640. :param style: button style
  1641. """
  1642. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title,
  1643. style=wx.DEFAULT_DIALOG_STYLE)
  1644. sizer = wx.BoxSizer(wx.VERTICAL)
  1645. label = wx.StaticText(self, label=message)
  1646. sizer.Add(item=label, proportion=0, flag=wx.ALIGN_CENTRE|wx.ALL, border=10)
  1647. hyperlinkLabel = hyperlinkLabel if hyperlinkLabel else hyperlink
  1648. hyperlinkCtrl = wx.HyperlinkCtrl(self, id=wx.ID_ANY,
  1649. label=hyperlinkLabel, url=hyperlink,
  1650. style=wx.HL_ALIGN_LEFT|wx.HL_CONTEXTMENU)
  1651. sizer.Add(item=hyperlinkCtrl, proportion=0, flag=wx.EXPAND|wx.ALL, border=10)
  1652. btnsizer = self.CreateSeparatedButtonSizer(style)
  1653. sizer.Add(item=btnsizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  1654. self.SetSizer(sizer)
  1655. sizer.Fit(self)
  1656. class QuitDialog(wx.Dialog):
  1657. def __init__(self, parent, title=_("Quit GRASS GIS"), id=wx.ID_ANY,
  1658. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  1659. """Dialog to quit GRASS
  1660. :param parent: window
  1661. """
  1662. wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)
  1663. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1664. self._icon = wx.StaticBitmap(parent=self.panel, id=wx.ID_ANY,
  1665. bitmap=wx.ArtProvider().GetBitmap(wx.ART_QUESTION, client=wx.ART_MESSAGE_BOX))
  1666. self.informLabel = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  1667. label=_("Do you want to quit GRASS including shell "
  1668. "prompt or just close the GUI?"))
  1669. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1670. self.btnClose = wx.Button(parent = self.panel, id = wx.ID_NO,
  1671. label=_("Close GUI"))
  1672. self.btnClose.SetFocus()
  1673. self.btnQuit = wx.Button(parent = self.panel, id = wx.ID_YES,
  1674. label=_("Quit GRASS GIS"))
  1675. self.btnQuit.SetMinSize((130, self.btnQuit.GetSize()[1]))
  1676. self.btnQuit.SetForegroundColour(wx.Colour(35, 142, 35))
  1677. self.btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  1678. self.btnQuit.Bind(wx.EVT_BUTTON, self.OnQuit)
  1679. self.__layout()
  1680. def __layout(self):
  1681. """Do layout"""
  1682. sizer = wx.BoxSizer(wx.VERTICAL)
  1683. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  1684. btnSizer.Add(item=self.btnCancel, flag=wx.RIGHT, border=5)
  1685. btnSizer.Add(item=self.btnClose, flag=wx.RIGHT, border=5)
  1686. btnSizer.Add(item=self.btnQuit, flag=wx.RIGHT, border=5)
  1687. bodySizer = wx.BoxSizer(wx.HORIZONTAL)
  1688. bodySizer.Add(item=self._icon, flag=wx.RIGHT, border=10)
  1689. bodySizer.Add(item=self.informLabel, proportion=1, flag=wx.EXPAND)
  1690. sizer.Add(item = bodySizer, proportion = 1,
  1691. flag = wx.EXPAND | wx.ALL, border = 15)
  1692. sizer.Add(item = btnSizer, proportion = 0,
  1693. flag = wx.ALL | wx.ALIGN_RIGHT, border = 5)
  1694. self.panel.SetSizer(sizer)
  1695. sizer.Fit(self)
  1696. self.Layout()
  1697. def OnClose(self, event):
  1698. self.EndModal(wx.ID_NO)
  1699. def OnQuit(self, event):
  1700. self.EndModal(wx.ID_YES)