dialogs.py 83 KB

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