dialogs.py 83 KB

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