dialogs.py 83 KB

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