dialogs.py 83 KB

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