dialogs.py 80 KB

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