dialogs.py 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  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. self.value = Slider(
  1441. panel,
  1442. id=wx.ID_ANY,
  1443. value=int(self.opacity * 100),
  1444. style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_TOP | wx.SL_LABELS,
  1445. minValue=0, maxValue=100, size=(350, -1))
  1446. box.Add(self.value,
  1447. flag=wx.ALIGN_CENTRE, pos=(0, 0), span=(1, 2))
  1448. box.Add(StaticText(parent=panel, id=wx.ID_ANY,
  1449. label=_("transparent")),
  1450. pos=(1, 0))
  1451. box.Add(StaticText(parent=panel, id=wx.ID_ANY,
  1452. label=_("opaque")),
  1453. flag=wx.ALIGN_RIGHT,
  1454. pos=(1, 1))
  1455. sizer.Add(box, proportion=0,
  1456. flag=wx.EXPAND | wx.ALL, border=5)
  1457. line = wx.StaticLine(parent=panel, id=wx.ID_ANY,
  1458. style=wx.LI_HORIZONTAL)
  1459. sizer.Add(line, proportion=0,
  1460. flag=wx.EXPAND | wx.ALL, border=5)
  1461. # buttons
  1462. btnsizer = wx.StdDialogButtonSizer()
  1463. btnOK = Button(parent=panel, id=wx.ID_OK)
  1464. btnOK.SetDefault()
  1465. btnsizer.AddButton(btnOK)
  1466. btnCancel = Button(parent=panel, id=wx.ID_CANCEL)
  1467. btnsizer.AddButton(btnCancel)
  1468. btnApply = Button(parent=panel, id=wx.ID_APPLY)
  1469. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1470. btnsizer.AddButton(btnApply)
  1471. btnsizer.Realize()
  1472. sizer.Add(btnsizer, proportion=0,
  1473. flag=wx.EXPAND | wx.ALL, border=5)
  1474. panel.SetSizer(sizer)
  1475. sizer.Fit(panel)
  1476. self.SetSize(self.GetBestSize())
  1477. self.Layout()
  1478. def GetOpacity(self):
  1479. """Button 'OK' pressed"""
  1480. # return opacity value
  1481. opacity = float(self.value.GetValue()) / 100
  1482. return opacity
  1483. def OnApply(self, event):
  1484. self.applyOpacity.emit(value=self.GetOpacity())
  1485. def GetImageHandlers(image):
  1486. """Get list of supported image handlers"""
  1487. lext = list()
  1488. ltype = list()
  1489. try:
  1490. for h in image.GetHandlers():
  1491. lext.append(h.GetExtension())
  1492. except AttributeError:
  1493. lext = {'png', 'gif', 'jpg', 'pcx', 'pnm', 'tif', 'xpm'}
  1494. filetype = ''
  1495. if 'png' in lext:
  1496. filetype += "PNG file (*.png)|*.png|"
  1497. ltype.append({'type': wx.BITMAP_TYPE_PNG,
  1498. 'ext': 'png'})
  1499. filetype += "BMP file (*.bmp)|*.bmp|"
  1500. ltype.append({'type': wx.BITMAP_TYPE_BMP,
  1501. 'ext': 'bmp'})
  1502. if 'gif' in lext:
  1503. filetype += "GIF file (*.gif)|*.gif|"
  1504. ltype.append({'type': wx.BITMAP_TYPE_GIF,
  1505. 'ext': 'gif'})
  1506. if 'jpg' in lext:
  1507. filetype += "JPG file (*.jpg)|*.jpg|"
  1508. ltype.append({'type': wx.BITMAP_TYPE_JPEG,
  1509. 'ext': 'jpg'})
  1510. if 'pcx' in lext:
  1511. filetype += "PCX file (*.pcx)|*.pcx|"
  1512. ltype.append({'type': wx.BITMAP_TYPE_PCX,
  1513. 'ext': 'pcx'})
  1514. if 'pnm' in lext:
  1515. filetype += "PNM file (*.pnm)|*.pnm|"
  1516. ltype.append({'type': wx.BITMAP_TYPE_PNM,
  1517. 'ext': 'pnm'})
  1518. if 'tif' in lext:
  1519. filetype += "TIF file (*.tif)|*.tif|"
  1520. ltype.append({'type': wx.BITMAP_TYPE_TIF,
  1521. 'ext': 'tif'})
  1522. if 'xpm' in lext:
  1523. filetype += "XPM file (*.xpm)|*.xpm"
  1524. ltype.append({'type': wx.BITMAP_TYPE_XPM,
  1525. 'ext': 'xpm'})
  1526. return filetype, ltype
  1527. class ImageSizeDialog(wx.Dialog):
  1528. """Set size for saved graphic file"""
  1529. def __init__(self, parent, id=wx.ID_ANY, title=_("Set image size"),
  1530. style=wx.DEFAULT_DIALOG_STYLE, **kwargs):
  1531. self.parent = parent
  1532. wx.Dialog.__init__(
  1533. self,
  1534. parent,
  1535. id=id,
  1536. style=style,
  1537. title=title,
  1538. **kwargs)
  1539. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  1540. self.box = StaticBox(parent=self.panel, id=wx.ID_ANY,
  1541. label=' % s' % _("Image size"))
  1542. size = self.parent.GetWindow().GetClientSize()
  1543. self.width = SpinCtrl(parent=self.panel, id=wx.ID_ANY,
  1544. style=wx.SP_ARROW_KEYS)
  1545. self.width.SetRange(20, 1e6)
  1546. self.width.SetValue(size.width)
  1547. wx.CallAfter(self.width.SetFocus)
  1548. self.height = SpinCtrl(parent=self.panel, id=wx.ID_ANY,
  1549. style=wx.SP_ARROW_KEYS)
  1550. self.height.SetRange(20, 1e6)
  1551. self.height.SetValue(size.height)
  1552. self.template = wx.Choice(parent=self.panel, id=wx.ID_ANY,
  1553. size=(125, -1),
  1554. choices=["",
  1555. "640x480",
  1556. "800x600",
  1557. "1024x768",
  1558. "1280x960",
  1559. "1600x1200",
  1560. "1920x1440"])
  1561. self.btnOK = Button(parent=self.panel, id=wx.ID_OK)
  1562. self.btnOK.SetDefault()
  1563. self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
  1564. self.template.Bind(wx.EVT_CHOICE, self.OnTemplate)
  1565. self._layout()
  1566. self.SetSize(self.GetBestSize())
  1567. def _layout(self):
  1568. """Do layout"""
  1569. sizer = wx.BoxSizer(wx.VERTICAL)
  1570. # body
  1571. box = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  1572. fbox = wx.FlexGridSizer(cols=2, vgap=5, hgap=5)
  1573. fbox.Add(StaticText(parent=self.panel, id=wx.ID_ANY,
  1574. label=_("Width:")),
  1575. flag=wx.ALIGN_CENTER_VERTICAL)
  1576. fbox.Add(self.width)
  1577. fbox.Add(StaticText(parent=self.panel, id=wx.ID_ANY,
  1578. label=_("Height:")),
  1579. flag=wx.ALIGN_CENTER_VERTICAL)
  1580. fbox.Add(self.height)
  1581. fbox.Add(StaticText(parent=self.panel, id=wx.ID_ANY,
  1582. label=_("Template:")),
  1583. flag=wx.ALIGN_CENTER_VERTICAL)
  1584. fbox.Add(self.template)
  1585. box.Add(fbox, proportion=1,
  1586. flag=wx.EXPAND | wx.ALL, border=5)
  1587. sizer.Add(box, proportion=1,
  1588. flag=wx.EXPAND | wx.ALL, border=3)
  1589. # buttons
  1590. btnsizer = wx.StdDialogButtonSizer()
  1591. btnsizer.AddButton(self.btnOK)
  1592. btnsizer.AddButton(self.btnCancel)
  1593. btnsizer.Realize()
  1594. sizer.Add(btnsizer, proportion=0,
  1595. flag=wx.EXPAND | wx.ALL, border=5)
  1596. self.panel.SetSizer(sizer)
  1597. sizer.Fit(self)
  1598. self.Layout()
  1599. def GetValues(self):
  1600. """Get width/height values"""
  1601. return self.width.GetValue(), self.height.GetValue()
  1602. def OnTemplate(self, event):
  1603. """Template selected"""
  1604. sel = event.GetString()
  1605. if not sel:
  1606. width, height = self.parent.GetWindow().GetClientSize()
  1607. else:
  1608. width, height = map(int, sel.split('x'))
  1609. self.width.SetValue(width)
  1610. self.height.SetValue(height)
  1611. class SqlQueryFrame(wx.Frame):
  1612. def __init__(self, parent, id=wx.ID_ANY,
  1613. title=_("GRASS GIS SQL Query Utility"),
  1614. *kwargs):
  1615. """SQL Query Utility window
  1616. """
  1617. self.parent = parent
  1618. wx.Frame.__init__(self, parent=parent, id=id, title=title, *kwargs)
  1619. self.SetIcon(
  1620. wx.Icon(
  1621. os.path.join(
  1622. globalvar.ICONDIR,
  1623. 'grass_sql.ico'),
  1624. wx.BITMAP_TYPE_ICO))
  1625. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  1626. self.sqlBox = StaticBox(parent=self.panel, id=wx.ID_ANY,
  1627. label=_(" SQL statement "))
  1628. self.sql = TextCtrl(parent=self.panel, id=wx.ID_ANY,
  1629. style=wx.TE_MULTILINE)
  1630. self.btnApply = Button(parent=self.panel, id=wx.ID_APPLY)
  1631. self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
  1632. self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, self.btnCancel)
  1633. self._layout()
  1634. self.SetMinSize(wx.Size(300, 150))
  1635. self.SetSize(wx.Size(500, 200))
  1636. def _layout(self):
  1637. """Do layout"""
  1638. sizer = wx.BoxSizer(wx.VERTICAL)
  1639. sqlSizer = wx.StaticBoxSizer(self.sqlBox, wx.HORIZONTAL)
  1640. sqlSizer.Add(self.sql, proportion=1,
  1641. flag=wx.EXPAND)
  1642. btnSizer = wx.StdDialogButtonSizer()
  1643. btnSizer.AddButton(self.btnApply)
  1644. btnSizer.AddButton(self.btnCancel)
  1645. btnSizer.Realize()
  1646. sizer.Add(sqlSizer, proportion=1,
  1647. flag=wx.EXPAND | wx.ALL, border=5)
  1648. sizer.Add(btnSizer, proportion=0,
  1649. flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  1650. self.panel.SetSizer(sizer)
  1651. self.Layout()
  1652. def OnCloseWindow(self, event):
  1653. """Close window
  1654. """
  1655. self.Close()
  1656. class SymbolDialog(wx.Dialog):
  1657. """Dialog for GRASS symbols selection.
  1658. Dialog is called in gui_core::forms module.
  1659. """
  1660. def __init__(self, parent, symbolPath,
  1661. currentSymbol=None, title=_("Symbols")):
  1662. """Dialog constructor.
  1663. It is assumed that symbolPath contains folders with symbols.
  1664. :param parent: dialog parent
  1665. :param symbolPath: absolute path to symbols
  1666. :param currentSymbol: currently selected symbol (e.g. 'basic/x')
  1667. :param title: dialog title
  1668. """
  1669. wx.Dialog.__init__(self, parent=parent, title=title, id=wx.ID_ANY)
  1670. self.symbolPath = symbolPath
  1671. self.currentSymbol = currentSymbol # default basic/x
  1672. self.selected = None
  1673. self.selectedDir = None
  1674. self._layout()
  1675. def _layout(self):
  1676. mainPanel = wx.Panel(self, id=wx.ID_ANY)
  1677. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1678. vSizer = wx.BoxSizer(wx.VERTICAL)
  1679. fgSizer = wx.FlexGridSizer(rows=2, cols=2, vgap=5, hgap=5)
  1680. self.folderChoice = wx.Choice(
  1681. mainPanel,
  1682. id=wx.ID_ANY,
  1683. choices=os.listdir(
  1684. self.symbolPath))
  1685. self.folderChoice.Bind(wx.EVT_CHOICE, self.OnFolderSelect)
  1686. fgSizer.Add(
  1687. StaticText(
  1688. mainPanel,
  1689. id=wx.ID_ANY,
  1690. label=_("Symbol directory:")),
  1691. proportion=0,
  1692. flag=wx.ALIGN_CENTER_VERTICAL)
  1693. fgSizer.Add(self.folderChoice, proportion=0,
  1694. flag=wx.ALIGN_CENTER, border=0)
  1695. self.infoLabel = StaticText(mainPanel, id=wx.ID_ANY)
  1696. fgSizer.Add(
  1697. StaticText(
  1698. mainPanel,
  1699. id=wx.ID_ANY,
  1700. label=_("Symbol name:")),
  1701. flag=wx.ALIGN_CENTRE_VERTICAL)
  1702. fgSizer.Add(self.infoLabel, proportion=0,
  1703. flag=wx.ALIGN_CENTRE_VERTICAL)
  1704. vSizer.Add(fgSizer, proportion=0, flag=wx.ALL, border=5)
  1705. self.panels = self._createSymbolPanels(mainPanel)
  1706. for panel in self.panels:
  1707. vSizer.Add(panel, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
  1708. mainSizer.Add(vSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
  1709. self.btnCancel = Button(parent=mainPanel, id=wx.ID_CANCEL)
  1710. self.btnOK = Button(parent=mainPanel, id=wx.ID_OK)
  1711. self.btnOK.SetDefault()
  1712. self.btnOK.Enable(False)
  1713. # buttons
  1714. btnSizer = wx.StdDialogButtonSizer()
  1715. btnSizer.AddButton(self.btnCancel)
  1716. btnSizer.AddButton(self.btnOK)
  1717. btnSizer.Realize()
  1718. mainSizer.Add(btnSizer, proportion=0,
  1719. flag=wx.EXPAND | wx.ALL, border=5)
  1720. # show panel with the largest number of images and fit size
  1721. count = []
  1722. for folder in os.listdir(self.symbolPath):
  1723. count.append(
  1724. len(os.listdir(os.path.join(self.symbolPath, folder))))
  1725. index = count.index(max(count))
  1726. self.folderChoice.SetSelection(index)
  1727. self.OnFolderSelect(None)
  1728. self.infoLabel.Show()
  1729. mainPanel.SetSizerAndFit(mainSizer)
  1730. self.SetSize(self.GetBestSize())
  1731. # show currently selected symbol
  1732. if self.currentSymbol:
  1733. # set directory
  1734. self.selectedDir, self.selected = os.path.split(self.currentSymbol)
  1735. self.folderChoice.SetStringSelection(self.selectedDir)
  1736. # select symbol
  1737. panelIdx = self.folderChoice.GetSelection()
  1738. for panel in self.symbolPanels[panelIdx]:
  1739. if panel.GetName() == self.selected:
  1740. panel.Select()
  1741. else:
  1742. self.folderChoice.SetSelection(0)
  1743. self.OnFolderSelect(None)
  1744. def _createSymbolPanels(self, parent):
  1745. """Creates multiple panels with symbols.
  1746. Panels are shown/hidden according to selected folder."""
  1747. folders = os.listdir(self.symbolPath)
  1748. panels = []
  1749. self.symbolPanels = []
  1750. for folder in folders:
  1751. panel = wx.Panel(parent, style=wx.BORDER_RAISED)
  1752. sizer = wx.GridSizer(cols=6, vgap=3, hgap=3)
  1753. images = self._getSymbols(
  1754. path=os.path.join(
  1755. self.symbolPath, folder))
  1756. symbolPanels = []
  1757. for img in images:
  1758. iP = SingleSymbolPanel(parent=panel, symbolPath=img)
  1759. iP.symbolSelectionChanged.connect(self.SelectionChanged)
  1760. sizer.Add(iP, proportion=0, flag=wx.ALIGN_CENTER)
  1761. symbolPanels.append(iP)
  1762. panel.SetSizerAndFit(sizer)
  1763. panel.Hide()
  1764. panels.append(panel)
  1765. self.symbolPanels.append(symbolPanels)
  1766. return panels
  1767. def _getSymbols(self, path):
  1768. # we assume that images are in subfolders (1 level only)
  1769. imageList = []
  1770. for image in os.listdir(path):
  1771. imageList.append(os.path.join(path, image))
  1772. return sorted(imageList)
  1773. def OnFolderSelect(self, event):
  1774. """Selected folder with symbols changed."""
  1775. idx = self.folderChoice.GetSelection()
  1776. for i in range(len(self.panels)):
  1777. sizer = self.panels[i].GetContainingSizer()
  1778. sizer.Show(self.panels[i], i == idx, recursive=True)
  1779. sizer.Layout()
  1780. if self.selectedDir == self.folderChoice.GetStringSelection():
  1781. self.btnOK.Enable()
  1782. self.infoLabel.SetLabel(self.selected)
  1783. else:
  1784. self.btnOK.Disable()
  1785. self.infoLabel.SetLabel('')
  1786. def SelectionChanged(self, name, doubleClick):
  1787. """Selected symbol changed."""
  1788. if doubleClick:
  1789. self.EndModal(wx.ID_OK)
  1790. # deselect all
  1791. for i in range(len(self.panels)):
  1792. for panel in self.symbolPanels[i]:
  1793. if panel.GetName() != name:
  1794. panel.Deselect()
  1795. self.btnOK.Enable()
  1796. self.selected = name
  1797. self.selectedDir = self.folderChoice.GetStringSelection()
  1798. self.infoLabel.SetLabel(name)
  1799. def GetSelectedSymbolName(self):
  1800. """Returns currently selected symbol name (e.g. 'basic/x').
  1801. """
  1802. # separator must be '/' and not dependent on OS
  1803. return self.selectedDir + '/' + self.selected
  1804. def GetSelectedSymbolPath(self):
  1805. """Returns currently selected symbol full path.
  1806. """
  1807. return os.path.join(self.symbolPath, self.selectedDir, self.selected)
  1808. class TextEntryDialog(wx.Dialog):
  1809. """Simple dialog with text field.
  1810. It differs from wx.TextEntryDialog because it allows adding validator.
  1811. """
  1812. def __init__(
  1813. self, parent, message, caption='', defaultValue='',
  1814. validator=wx.DefaultValidator, style=wx.OK | wx.CANCEL | wx.CENTRE,
  1815. textStyle=0, textSize=(300, -1),
  1816. **kwargs):
  1817. wx.Dialog.__init__(
  1818. self,
  1819. parent=parent,
  1820. id=wx.ID_ANY,
  1821. title=caption,
  1822. **kwargs)
  1823. vbox = wx.BoxSizer(wx.VERTICAL)
  1824. stline = StaticText(self, id=wx.ID_ANY, label=message)
  1825. vbox.Add(stline, proportion=0, flag=wx.EXPAND | wx.ALL, border=10)
  1826. self._textCtrl = TextCtrl(
  1827. self,
  1828. id=wx.ID_ANY,
  1829. value=defaultValue,
  1830. validator=validator,
  1831. style=textStyle)
  1832. self._textCtrl.SetInitialSize(textSize)
  1833. wx.CallAfter(self._textCtrl.SetFocus)
  1834. vbox.Add(
  1835. self._textCtrl,
  1836. proportion=0,
  1837. flag=wx.EXPAND | wx.LEFT | wx.RIGHT,
  1838. border=10)
  1839. self._textCtrl.SetFocus()
  1840. sizer = self.CreateSeparatedButtonSizer(style)
  1841. vbox.Add(sizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  1842. self.SetSizerAndFit(vbox)
  1843. def GetValue(self):
  1844. return self._textCtrl.GetValue()
  1845. def SetValue(self, value):
  1846. self._textCtrl.SetValue(value)
  1847. class HyperlinkDialog(wx.Dialog):
  1848. """Dialog for displaying message with hyperlink."""
  1849. def __init__(self, parent, title, message, hyperlink,
  1850. hyperlinkLabel=None, style=wx.OK):
  1851. """Constructor
  1852. :param parent: gui parent
  1853. :param title: dialog title
  1854. :param message: message
  1855. :param hyperlink: url
  1856. :param hyperlinkLabel: label shown instead of url
  1857. :param style: button style
  1858. """
  1859. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title,
  1860. style=wx.DEFAULT_DIALOG_STYLE)
  1861. sizer = wx.BoxSizer(wx.VERTICAL)
  1862. label = StaticText(self, label=message)
  1863. sizer.Add(
  1864. label,
  1865. proportion=0,
  1866. flag=wx.ALIGN_CENTRE | wx.ALL,
  1867. border=10)
  1868. hyperlinkLabel = hyperlinkLabel if hyperlinkLabel else hyperlink
  1869. hyperlinkCtrl = HyperlinkCtrl(
  1870. self, id=wx.ID_ANY, label=hyperlinkLabel, url=hyperlink,
  1871. style=HyperlinkCtrl.HL_ALIGN_LEFT | HyperlinkCtrl.HL_CONTEXTMENU)
  1872. sizer.Add(
  1873. hyperlinkCtrl,
  1874. proportion=0,
  1875. flag=wx.EXPAND | wx.ALL,
  1876. border=10)
  1877. btnsizer = self.CreateSeparatedButtonSizer(style)
  1878. sizer.Add(
  1879. btnsizer,
  1880. proportion=1,
  1881. flag=wx.EXPAND | wx.ALL,
  1882. border=10)
  1883. self.SetSizer(sizer)
  1884. sizer.Fit(self)
  1885. class QuitDialog(wx.Dialog):
  1886. def __init__(self, parent, title=_("Quit GRASS GIS"), id=wx.ID_ANY,
  1887. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  1888. """Dialog to quit GRASS
  1889. :param parent: window
  1890. """
  1891. wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)
  1892. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  1893. self._icon = wx.StaticBitmap(
  1894. self.panel, wx.ID_ANY,
  1895. wx.ArtProvider().GetBitmap(
  1896. wx.ART_QUESTION,
  1897. client=wx.ART_MESSAGE_BOX))
  1898. self.informLabel = StaticText(
  1899. parent=self.panel, id=wx.ID_ANY, label=_(
  1900. "Do you want to quit GRASS including shell "
  1901. "prompt or just close the GUI?"))
  1902. self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
  1903. self.btnClose = Button(parent=self.panel, id=wx.ID_NO,
  1904. label=_("Close GUI"))
  1905. self.btnClose.SetFocus()
  1906. self.btnQuit = Button(parent=self.panel, id=wx.ID_YES,
  1907. label=_("Quit GRASS GIS"))
  1908. self.btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  1909. self.btnQuit.Bind(wx.EVT_BUTTON, self.OnQuit)
  1910. self.__layout()
  1911. def __layout(self):
  1912. """Do layout"""
  1913. sizer = wx.BoxSizer(wx.VERTICAL)
  1914. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  1915. btnSizer.Add(self.btnCancel, flag=wx.RIGHT, border=5)
  1916. btnSizer.Add(self.btnClose, flag=wx.RIGHT, border=5)
  1917. btnSizer.Add(self.btnQuit, flag=wx.RIGHT, border=5)
  1918. bodySizer = wx.BoxSizer(wx.HORIZONTAL)
  1919. bodySizer.Add(self._icon, flag=wx.RIGHT, border=10)
  1920. bodySizer.Add(self.informLabel, proportion=1, flag=wx.EXPAND)
  1921. sizer.Add(bodySizer, proportion=1,
  1922. flag=wx.EXPAND | wx.ALL, border=15)
  1923. sizer.Add(btnSizer, proportion=0,
  1924. flag=wx.ALL | wx.ALIGN_RIGHT, border=5)
  1925. self.panel.SetSizer(sizer)
  1926. sizer.Fit(self)
  1927. self.Layout()
  1928. def OnClose(self, event):
  1929. self.EndModal(wx.ID_NO)
  1930. def OnQuit(self, event):
  1931. self.EndModal(wx.ID_YES)
  1932. class DefaultFontDialog(wx.Dialog):
  1933. """
  1934. Opens a file selection dialog to select default font
  1935. to use in all GRASS displays
  1936. """
  1937. def __init__(self, parent, title, id=wx.ID_ANY,
  1938. style=wx.DEFAULT_DIALOG_STYLE |
  1939. wx.RESIZE_BORDER,
  1940. settings=UserSettings,
  1941. type='font'):
  1942. self.settings = settings
  1943. self.type = type
  1944. wx.Dialog.__init__(self, parent, id, title, style=style)
  1945. panel = wx.Panel(parent=self, id=wx.ID_ANY)
  1946. self.tmp_file = grass.tempfile(False) + '.png'
  1947. self.fontdict, fontdict_reverse, self.fontlist = self.GetFonts()
  1948. border = wx.BoxSizer(wx.VERTICAL)
  1949. box = StaticBox(
  1950. parent=panel,
  1951. id=wx.ID_ANY,
  1952. label=" %s " %
  1953. _("Font settings"))
  1954. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1955. gridSizer = wx.GridBagSizer(hgap=5, vgap=5)
  1956. label = StaticText(parent=panel, id=wx.ID_ANY,
  1957. label=_("Select font:"))
  1958. gridSizer.Add(label,
  1959. flag=wx.ALIGN_TOP,
  1960. pos=(0, 0))
  1961. self.fontlb = wx.ListBox(
  1962. parent=panel,
  1963. id=wx.ID_ANY,
  1964. pos=wx.DefaultPosition,
  1965. choices=self.fontlist,
  1966. style=wx.LB_SINGLE)
  1967. self.Bind(wx.EVT_LISTBOX, self.EvtListBox, self.fontlb)
  1968. self.Bind(wx.EVT_LISTBOX_DCLICK, self.EvtListBoxDClick, self.fontlb)
  1969. gridSizer.Add(self.fontlb,
  1970. flag=wx.EXPAND, pos=(1, 0))
  1971. self.renderfont = wx.StaticBitmap(panel, -1, wx.Bitmap.FromRGBA(100, 50, 255, 255, 255))
  1972. gridSizer.Add(self.renderfont,
  1973. flag=wx.EXPAND, pos=(2, 0))
  1974. if self.type == 'font':
  1975. if "GRASS_FONT" in os.environ:
  1976. self.font = os.environ["GRASS_FONT"]
  1977. else:
  1978. self.font = self.settings.Get(group='display',
  1979. key='font', subkey='type')
  1980. self.encoding = self.settings.Get(group='display',
  1981. key='font', subkey='encoding')
  1982. label = StaticText(parent=panel, id=wx.ID_ANY,
  1983. label=_("Character encoding:"))
  1984. gridSizer.Add(label,
  1985. flag=wx.ALIGN_CENTER_VERTICAL,
  1986. pos=(3, 0))
  1987. self.textentry = TextCtrl(parent=panel, id=wx.ID_ANY,
  1988. value=self.encoding)
  1989. gridSizer.Add(self.textentry,
  1990. flag=wx.EXPAND, pos=(4, 0))
  1991. self.textentry.Bind(wx.EVT_TEXT, self.OnEncoding)
  1992. elif self.type == 'outputfont':
  1993. self.font = self.settings.Get(group='appearance',
  1994. key='outputfont', subkey='type')
  1995. self.fontsize = self.settings.Get(group='appearance',
  1996. key='outputfont', subkey='size')
  1997. label = StaticText(parent=panel, id=wx.ID_ANY,
  1998. label=_("Font size:"))
  1999. gridSizer.Add(label,
  2000. flag=wx.ALIGN_CENTER_VERTICAL,
  2001. pos=(3, 0))
  2002. self.spin = SpinCtrl(parent=panel, id=wx.ID_ANY)
  2003. if self.fontsize:
  2004. self.spin.SetValue(int(self.fontsize))
  2005. self.spin.Bind(wx.EVT_SPINCTRL, self.OnSizeSpin)
  2006. self.spin.Bind(wx.EVT_TEXT, self.OnSizeSpin)
  2007. gridSizer.Add(self.spin,
  2008. flag=wx.ALIGN_CENTER_VERTICAL,
  2009. pos=(4, 0))
  2010. else:
  2011. return
  2012. if self.font:
  2013. long_name = fontdict_reverse.get(self.font, None)
  2014. if long_name:
  2015. self.fontlb.SetStringSelection(long_name, True)
  2016. else:
  2017. # font is not in the list of GRASS recognized fonts
  2018. self.font = None
  2019. gridSizer.AddGrowableCol(0)
  2020. sizer.Add(gridSizer, proportion=1,
  2021. flag=wx.EXPAND | wx.ALL,
  2022. border=5)
  2023. border.Add(sizer, proportion=1,
  2024. flag=wx.ALL | wx.EXPAND, border=3)
  2025. btnsizer = wx.StdDialogButtonSizer()
  2026. btn = Button(parent=panel, id=wx.ID_OK)
  2027. btn.SetDefault()
  2028. btnsizer.AddButton(btn)
  2029. btn = Button(parent=panel, id=wx.ID_CANCEL)
  2030. btnsizer.AddButton(btn)
  2031. btnsizer.Realize()
  2032. border.Add(btnsizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
  2033. panel.SetAutoLayout(True)
  2034. panel.SetSizer(border)
  2035. border.Fit(self)
  2036. row, col = gridSizer.GetItemPosition(self.renderfont)
  2037. self.renderfont.SetSize(gridSizer.GetCellSize(row, col))
  2038. if self.font:
  2039. self.RenderText(self.font, _("Example"), size=self.renderfont.GetSize())
  2040. self.Layout()
  2041. def OnEncoding(self, event):
  2042. self.encoding = event.GetString()
  2043. def EvtListBox(self, event):
  2044. self.font = self.fontdict[event.GetString()]
  2045. self.RenderText(self.font, "Example", size=self.renderfont.GetSize())
  2046. event.Skip()
  2047. def EvtListBoxDClick(self, event):
  2048. self.font = self.fontdict[event.GetString()]
  2049. event.Skip()
  2050. def OnSizeSpin(self, event):
  2051. self.fontsize = self.spin.GetValue()
  2052. event.Skip()
  2053. def GetFonts(self):
  2054. """
  2055. parses fonts directory or fretypecap file to get a list of fonts
  2056. for the listbox
  2057. """
  2058. fontlist = []
  2059. fontdict = {}
  2060. fontdict_reverse = {}
  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. ret = RunCommand('d.fontlist', flags='v',
  2068. read=True,
  2069. env=env)
  2070. if not ret:
  2071. return fontlist
  2072. dfonts = ret.splitlines()
  2073. for line in dfonts:
  2074. shortname = line.split('|')[0]
  2075. longname = line.split('|')[1]
  2076. # not sure when this happens?
  2077. if shortname.startswith('#'):
  2078. continue
  2079. fontlist.append(longname)
  2080. fontdict[longname] = shortname
  2081. fontdict_reverse[shortname] = longname
  2082. fontlist = natural_sort(list(set(fontlist)))
  2083. return fontdict, fontdict_reverse, fontlist
  2084. def RenderText(self, font, text, size):
  2085. """Renders an example text with the selected font and resets the bitmap widget"""
  2086. env = os.environ.copy()
  2087. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  2088. if driver == 'png':
  2089. env['GRASS_RENDER_IMMEDIATE'] = 'png'
  2090. else:
  2091. env['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  2092. env['GRASS_RENDER_WIDTH'] = str(size[0])
  2093. env['GRASS_RENDER_HEIGHT'] = str(size[1])
  2094. env['GRASS_RENDER_FILE'] = self.tmp_file
  2095. env['GRASS_REGION'] = grass.region_env(s=0, n=size[1], w=0, e=size[0])
  2096. ret = RunCommand('d.text', text=text, font=font, align='cc', at='50,60',
  2097. size=80, color='black', env=env)
  2098. if ret == 0:
  2099. self.renderfont.SetBitmap(wx.Bitmap(self.tmp_file))
  2100. else:
  2101. self.renderfont.SetBitmap(EmptyBitmap(size[0], size[1]))
  2102. try_remove(self.tmp_file)