dialogs.py 83 KB

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