dialogs.py 79 KB

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