dialogs.py 76 KB

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