widgets.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. """
  2. @package gui_core.widgets
  3. @brief Core GUI widgets
  4. Classes:
  5. - widgets::GNotebook
  6. - widgets::ScrolledPanel
  7. - widgets::NumTextCtrl
  8. - widgets::FloatSlider
  9. - widgets::SymbolButton
  10. - widgets::StaticWrapText
  11. - widgets::BaseValidator
  12. - widgets::CoordinatesValidator
  13. - widgets::IntegerValidator
  14. - widgets::FloatValidator
  15. - widgets::EmailValidator
  16. - widgets::TimeISOValidator
  17. - widgets::MapValidator
  18. - widgets::NTCValidator
  19. - widgets::SimpleValidator
  20. - widgets::GenericValidator
  21. - widgets::GenericMultiValidator
  22. - widgets::LayersListValidator
  23. - widgets::PlacementValidator
  24. - widgets::GListCtrl
  25. - widgets::SearchModuleWidget
  26. - widgets::ManageSettingsWidget
  27. - widgets::PictureComboBox
  28. - widgets::ColorTablesComboBox
  29. - widgets::BarscalesComboBox
  30. - widgets::NArrowsComboBox
  31. - widgets::LayersList
  32. @todo:
  33. - move validators to a separate file gui_core/validators.py
  34. (C) 2008-2014 by the GRASS Development Team
  35. This program is free software under the GNU General Public License
  36. (>=v2). Read the file COPYING that comes with GRASS for details.
  37. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  38. @author Enhancements by Michael Barton <michael.barton asu.edu>
  39. @author Anna Kratochvilova <kratochanna gmail.com> (Google SoC 2011)
  40. @author Stepan Turek <stepan.turek seznam.cz> (ManageSettingsWidget - created from GdalSelect)
  41. @author Matej Krejci <matejkrejci gmail.com> (Google GSoC 2014; EmailValidator, TimeISOValidator)
  42. @author Tomas Zigo <tomas.zigo slovanet.sk> (LayersListValidator,
  43. PlacementValidator)
  44. """
  45. import os
  46. import sys
  47. import string
  48. import re
  49. import six
  50. from bisect import bisect
  51. from datetime import datetime
  52. from core.globalvar import wxPythonPhoenix
  53. import wx
  54. import wx.lib.mixins.listctrl as listmix
  55. import wx.lib.scrolledpanel as SP
  56. from wx.lib.stattext import GenStaticText
  57. from wx.lib.wordwrap import wordwrap
  58. if wxPythonPhoenix:
  59. import wx.adv
  60. from wx.adv import OwnerDrawnComboBox
  61. else:
  62. import wx.combo
  63. from wx.combo import OwnerDrawnComboBox
  64. try:
  65. import wx.lib.agw.flatnotebook as FN
  66. except ImportError:
  67. import wx.lib.flatnotebook as FN
  68. try:
  69. from wx.lib.buttons import ThemedGenBitmapTextButton as BitmapTextButton
  70. except ImportError: # not sure about TGBTButton version
  71. from wx.lib.buttons import GenBitmapTextButton as BitmapTextButton
  72. if wxPythonPhoenix:
  73. from wx import Validator as Validator
  74. else:
  75. from wx import PyValidator as Validator
  76. from grass.script import core as grass
  77. from grass.pydispatch.signal import Signal
  78. from core import globalvar
  79. from core.gcmd import GMessage, GError
  80. from core.debug import Debug
  81. from gui_core.wrap import (
  82. Button,
  83. SearchCtrl,
  84. Slider,
  85. StaticText,
  86. StaticBox,
  87. TextCtrl,
  88. Menu,
  89. Rect,
  90. EmptyBitmap,
  91. ListCtrl,
  92. NewId,
  93. CheckListCtrlMixin,
  94. )
  95. class NotebookController:
  96. """Provides handling of notebook page names.
  97. Translates page names to page indices.
  98. Class is aggregated in notebook subclasses.
  99. Notebook subclasses must delegate methods to controller.
  100. Methods inherited from notebook class must be delegated explicitly
  101. and other methods can be delegated by @c __getattr__.
  102. """
  103. def __init__(self, classObject, widget):
  104. """
  105. :param classObject: notebook class name (object, i.e. FlatNotebook)
  106. :param widget: notebook instance
  107. """
  108. self.notebookPages = {}
  109. self.classObject = classObject
  110. self.widget = widget
  111. self.highlightedTextEnd = _(" (...)")
  112. self.BindPageChanged()
  113. def BindPageChanged(self):
  114. """Binds page changed event."""
  115. self.widget.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnRemoveHighlight)
  116. def AddPage(self, *args, **kwargs):
  117. """Add a new page"""
  118. if "name" in kwargs:
  119. self.notebookPages[kwargs["name"]] = kwargs["page"]
  120. del kwargs["name"]
  121. self.classObject.AddPage(self.widget, *args, **kwargs)
  122. def InsertPage(self, *args, **kwargs):
  123. """Insert a new page"""
  124. if "name" in kwargs:
  125. self.notebookPages[kwargs["name"]] = kwargs["page"]
  126. del kwargs["name"]
  127. try:
  128. self.classObject.InsertPage(self.widget, *args, **kwargs)
  129. except TypeError as e: # documentation says 'index', but certain versions of wx require 'n'
  130. kwargs["n"] = kwargs["index"]
  131. del kwargs["index"]
  132. self.classObject.InsertPage(self.widget, *args, **kwargs)
  133. def DeletePage(self, page):
  134. """Delete page
  135. :param page: name
  136. :return: True if page was deleted, False if not exists
  137. """
  138. delPageIndex = self.GetPageIndexByName(page)
  139. if delPageIndex != -1:
  140. ret = self.classObject.DeletePage(self.widget, delPageIndex)
  141. if ret:
  142. del self.notebookPages[page]
  143. return ret
  144. else:
  145. return False
  146. def RemovePage(self, page):
  147. """Delete page without deleting the associated window.
  148. :param page: name
  149. :return: True if page was deleted, False if not exists
  150. """
  151. delPageIndex = self.GetPageIndexByName(page)
  152. if delPageIndex != -1:
  153. ret = self.classObject.RemovePage(self.widget, delPageIndex)
  154. if ret:
  155. del self.notebookPages[page]
  156. return ret
  157. else:
  158. return False
  159. def SetSelectionByName(self, page):
  160. """Set active notebook page.
  161. :param page: name, eg. 'layers', 'output', 'search', 'pyshell', 'nviz'
  162. (depends on concrete notebook instance)
  163. """
  164. idx = self.GetPageIndexByName(page)
  165. if self.classObject.GetSelection(self.widget) != idx:
  166. self.classObject.SetSelection(self.widget, idx)
  167. self.RemoveHighlight(idx)
  168. def OnRemoveHighlight(self, event):
  169. """Highlighted tab name should be removed."""
  170. page = event.GetSelection()
  171. self.RemoveHighlight(page)
  172. event.Skip()
  173. def RemoveHighlight(self, page):
  174. """Removes highlight string from notebook tab name if necessary.
  175. :param page: index
  176. """
  177. text = self.classObject.GetPageText(self.widget, page)
  178. if text.endswith(self.highlightedTextEnd):
  179. text = text.replace(self.highlightedTextEnd, "")
  180. self.classObject.SetPageText(self.widget, page, text)
  181. def GetPageIndexByName(self, page):
  182. """Get notebook page index
  183. :param page: name
  184. """
  185. if page not in self.notebookPages:
  186. return -1
  187. for pageIndex in range(self.classObject.GetPageCount(self.widget)):
  188. if self.notebookPages[page] == self.classObject.GetPage(
  189. self.widget, pageIndex
  190. ):
  191. break
  192. return pageIndex
  193. def HighlightPageByName(self, page):
  194. pageIndex = self.GetPageIndexByName(page)
  195. self.HighlightPage(pageIndex)
  196. def HighlightPage(self, index):
  197. if self.classObject.GetSelection(self.widget) != index:
  198. text = self.classObject.GetPageText(self.widget, index)
  199. if not text.endswith(self.highlightedTextEnd):
  200. text += self.highlightedTextEnd
  201. self.classObject.SetPageText(self.widget, index, text)
  202. def SetPageImage(self, page, index):
  203. """Sets image index for page
  204. :param page: page name
  205. :param index: image index (in wx.ImageList)
  206. """
  207. pageIndex = self.GetPageIndexByName(page)
  208. self.classObject.SetPageImage(self.widget, pageIndex, index)
  209. class FlatNotebookController(NotebookController):
  210. """Controller specialized for FN.FlatNotebook subclasses"""
  211. def __init__(self, classObject, widget):
  212. NotebookController.__init__(self, classObject, widget)
  213. def BindPageChanged(self):
  214. self.widget.Bind(FN.EVT_FLATNOTEBOOK_PAGE_CHANGED, self.OnRemoveHighlight)
  215. def GetPageIndexByName(self, page):
  216. """Get notebook page index
  217. :param page: name
  218. """
  219. if page not in self.notebookPages:
  220. return -1
  221. return self.classObject.GetPageIndex(self.widget, self.notebookPages[page])
  222. def InsertPage(self, *args, **kwargs):
  223. """Insert a new page"""
  224. if "name" in kwargs:
  225. self.notebookPages[kwargs["name"]] = kwargs["page"]
  226. del kwargs["name"]
  227. kwargs["indx"] = kwargs["index"]
  228. del kwargs["index"]
  229. self.classObject.InsertPage(self.widget, *args, **kwargs)
  230. class GNotebook(FN.FlatNotebook):
  231. """Generic notebook widget.
  232. Enables advanced style settings.
  233. Problems with hidden tabs. Uses system colours for active tabs.
  234. """
  235. def __init__(self, parent, style, **kwargs):
  236. if globalvar.hasAgw:
  237. FN.FlatNotebook.__init__(
  238. self, parent, id=wx.ID_ANY, agwStyle=style, **kwargs
  239. )
  240. else:
  241. FN.FlatNotebook.__init__(self, parent, id=wx.ID_ANY, style=style, **kwargs)
  242. self.controller = FlatNotebookController(
  243. classObject=FN.FlatNotebook, widget=self
  244. )
  245. self.SetActiveTabColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
  246. self.SetActiveTabTextColour(
  247. wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
  248. )
  249. def AddPage(self, *args, **kwargs):
  250. """@copydoc NotebookController::AddPage()"""
  251. self.controller.AddPage(*args, **kwargs)
  252. def InsertNBPage(self, *args, **kwargs):
  253. """@copydoc NotebookController::InsertPage()"""
  254. self.controller.InsertPage(*args, **kwargs)
  255. def DeleteNBPage(self, page):
  256. """@copydoc NotebookController::DeletePage()"""
  257. return self.controller.DeletePage(page)
  258. def RemoveNBPage(self, page):
  259. """@copydoc NotebookController::RemovePage()"""
  260. return self.controller.RemovePage(page)
  261. def SetPageImage(self, page, index):
  262. """Does nothing because we don't want images for this style"""
  263. pass
  264. def __getattr__(self, name):
  265. return getattr(self.controller, name)
  266. class FormNotebook(wx.Notebook):
  267. """Notebook widget.
  268. Respects native look.
  269. """
  270. def __init__(self, parent, style):
  271. wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=style)
  272. self.controller = NotebookController(classObject=wx.Notebook, widget=self)
  273. def AddPage(self, *args, **kwargs):
  274. """@copydoc NotebookController::AddPage()"""
  275. self.controller.AddPage(*args, **kwargs)
  276. def InsertNBPage(self, *args, **kwargs):
  277. """@copydoc NotebookController::InsertPage()"""
  278. self.controller.InsertPage(*args, **kwargs)
  279. def DeleteNBPage(self, page):
  280. """@copydoc NotebookController::DeletePage()"""
  281. return self.controller.DeletePage(page)
  282. def RemoveNBPage(self, page):
  283. """@copydoc NotebookController::RemovePage()"""
  284. return self.controller.RemovePage(page)
  285. def SetPageImage(self, page, index):
  286. """@copydoc NotebookController::SetPageImage()"""
  287. return self.controller.SetPageImage(page, index)
  288. def __getattr__(self, name):
  289. return getattr(self.controller, name)
  290. class FormListbook(wx.Listbook):
  291. """Notebook widget.
  292. Respects native look.
  293. """
  294. def __init__(self, parent, style):
  295. wx.Listbook.__init__(self, parent, id=wx.ID_ANY, style=style)
  296. self.controller = NotebookController(classObject=wx.Listbook, widget=self)
  297. def AddPage(self, *args, **kwargs):
  298. """@copydoc NotebookController::AddPage()"""
  299. self.controller.AddPage(*args, **kwargs)
  300. def InsertPage_(self, *args, **kwargs):
  301. """@copydoc NotebookController::InsertPage()"""
  302. self.controller.InsertPage(*args, **kwargs)
  303. def DeletePage(self, page):
  304. """@copydoc NotebookController::DeletePage()"""
  305. return self.controller.DeletePage(page)
  306. def RemovePage(self, page):
  307. """@copydoc NotebookController::RemovePage()"""
  308. return self.controller.RemovePage(page)
  309. def SetPageImage(self, page, index):
  310. """@copydoc NotebookController::SetPageImage()"""
  311. return self.controller.SetPageImage(page, index)
  312. def __getattr__(self, name):
  313. return getattr(self.controller, name)
  314. class ScrolledPanel(SP.ScrolledPanel):
  315. """Custom ScrolledPanel to avoid strange behaviour concerning focus"""
  316. def __init__(self, parent, style=wx.TAB_TRAVERSAL):
  317. SP.ScrolledPanel.__init__(self, parent=parent, id=wx.ID_ANY, style=style)
  318. def OnChildFocus(self, event):
  319. pass
  320. class NumTextCtrl(TextCtrl):
  321. """Class derived from wx.TextCtrl for numerical values only"""
  322. def __init__(self, parent, **kwargs):
  323. ## self.precision = kwargs.pop('prec')
  324. TextCtrl.__init__(
  325. self, parent=parent, validator=NTCValidator(flag="DIGIT_ONLY"), **kwargs
  326. )
  327. def SetValue(self, value):
  328. super(NumTextCtrl, self).SetValue(str(value))
  329. def GetValue(self):
  330. val = super(NumTextCtrl, self).GetValue()
  331. if val == "":
  332. val = "0"
  333. try:
  334. return float(val)
  335. except ValueError:
  336. val = "".join("".join(val.split("-")).split("."))
  337. return float(val)
  338. def SetRange(self, min, max):
  339. pass
  340. class FloatSlider(Slider):
  341. """Class derived from wx.Slider for floats"""
  342. def __init__(self, **kwargs):
  343. Debug.msg(1, "FloatSlider.__init__()")
  344. Slider.__init__(self, **kwargs)
  345. self.coef = 1.0
  346. # init range
  347. self.minValueOrig = 0
  348. self.maxValueOrig = 1
  349. def SetValue(self, value):
  350. value *= self.coef
  351. if abs(value) < 1 and value != 0:
  352. while abs(value) < 1:
  353. value *= 100
  354. self.coef *= 100
  355. super(FloatSlider, self).SetRange(
  356. self.minValueOrig * self.coef, self.maxValueOrig * self.coef
  357. )
  358. super(FloatSlider, self).SetValue(value)
  359. Debug.msg(4, "FloatSlider.SetValue(): value = %f" % value)
  360. def SetRange(self, minValue, maxValue):
  361. self.coef = 1.0
  362. self.minValueOrig = minValue
  363. self.maxValueOrig = maxValue
  364. if abs(minValue) < 1 or abs(maxValue) < 1:
  365. while (abs(minValue) < 1 and minValue != 0) or (
  366. abs(maxValue) < 1 and maxValue != 0
  367. ):
  368. minValue *= 100
  369. maxValue *= 100
  370. self.coef *= 100
  371. super(FloatSlider, self).SetValue(
  372. super(FloatSlider, self).GetValue() * self.coef
  373. )
  374. super(FloatSlider, self).SetRange(minValue, maxValue)
  375. Debug.msg(
  376. 4,
  377. "FloatSlider.SetRange(): minValue = %f, maxValue = %f"
  378. % (minValue, maxValue),
  379. )
  380. def GetValue(self):
  381. val = super(FloatSlider, self).GetValue()
  382. Debug.msg(4, "FloatSlider.GetValue(): value = %f" % (val / self.coef))
  383. return val / self.coef
  384. class SymbolButton(BitmapTextButton):
  385. """Button with symbol and label."""
  386. def __init__(self, parent, usage, label, **kwargs):
  387. """Constructor
  388. :param parent: parent (usually wx.Panel)
  389. :param usage: determines usage and picture
  390. :param label: displayed label
  391. """
  392. size = (15, 15)
  393. buffer = EmptyBitmap(*size)
  394. BitmapTextButton.__init__(
  395. self, parent=parent, label=" " + label, bitmap=buffer, **kwargs
  396. )
  397. dc = wx.MemoryDC()
  398. dc.SelectObject(buffer)
  399. maskColor = wx.Colour(255, 255, 255)
  400. dc.SetBrush(wx.Brush(maskColor))
  401. dc.Clear()
  402. if usage == "record":
  403. self.DrawRecord(dc, size)
  404. elif usage == "stop":
  405. self.DrawStop(dc, size)
  406. elif usage == "play":
  407. self.DrawPlay(dc, size)
  408. elif usage == "pause":
  409. self.DrawPause(dc, size)
  410. if sys.platform not in ("win32", "darwin"):
  411. buffer.SetMaskColour(maskColor)
  412. self.SetBitmapLabel(buffer)
  413. dc.SelectObject(wx.NullBitmap)
  414. def DrawRecord(self, dc, size):
  415. """Draw record symbol"""
  416. dc.SetBrush(wx.Brush(wx.Colour(255, 0, 0)))
  417. dc.DrawCircle(size[0] // 2, size[1] // 2, size[0] // 2)
  418. def DrawStop(self, dc, size):
  419. """Draw stop symbol"""
  420. dc.SetBrush(wx.Brush(wx.Colour(50, 50, 50)))
  421. dc.DrawRectangle(0, 0, size[0], size[1])
  422. def DrawPlay(self, dc, size):
  423. """Draw play symbol"""
  424. dc.SetBrush(wx.Brush(wx.Colour(0, 255, 0)))
  425. points = (wx.Point(0, 0), wx.Point(0, size[1]), wx.Point(size[0], size[1] // 2))
  426. dc.DrawPolygon(points)
  427. def DrawPause(self, dc, size):
  428. """Draw pause symbol"""
  429. dc.SetBrush(wx.Brush(wx.Colour(50, 50, 50)))
  430. dc.DrawRectangle(0, 0, 2 * size[0] // 5, size[1])
  431. dc.DrawRectangle(3 * size[0] // 5, 0, 2 * size[0] // 5, size[1])
  432. class StaticWrapText(GenStaticText):
  433. """A Static Text widget that wraps its text to fit parents width,
  434. enlarging its height if necessary."""
  435. def __init__(self, parent, id=wx.ID_ANY, label="", margin=0, *args, **kwds):
  436. self._margin = margin
  437. self._initialLabel = label
  438. self.init = False
  439. GenStaticText.__init__(self, parent, id, label, *args, **kwds)
  440. self.Bind(wx.EVT_SIZE, self.OnSize)
  441. def DoGetBestSize(self):
  442. """Overridden method which reports widget's best size."""
  443. if not self.init:
  444. self.init = True
  445. self._updateLabel()
  446. parent = self.GetParent()
  447. newExtent = wx.ClientDC(parent).GetMultiLineTextExtent(self.GetLabel())
  448. # when starting, width is very small and height is big which creates
  449. # very high windows
  450. if newExtent[0] < newExtent[1]:
  451. return (0, 0)
  452. return newExtent[:2]
  453. def OnSize(self, event):
  454. self._updateLabel()
  455. event.Skip()
  456. def _updateLabel(self):
  457. """Calculates size of wrapped label"""
  458. parent = self.GetParent()
  459. newLabel = wordwrap(
  460. text=self._initialLabel,
  461. width=parent.GetSize()[0],
  462. dc=wx.ClientDC(parent),
  463. breakLongWords=True,
  464. margin=self._margin,
  465. )
  466. GenStaticText.SetLabel(self, newLabel)
  467. def SetLabel(self, label):
  468. self._initialLabel = label
  469. self._updateLabel()
  470. class BaseValidator(Validator):
  471. def __init__(self):
  472. Validator.__init__(self)
  473. self.Bind(wx.EVT_TEXT, self.OnText)
  474. def OnText(self, event):
  475. """Do validation"""
  476. self._validate(win=event.GetEventObject())
  477. event.Skip()
  478. def Validate(self, parent):
  479. """Is called upon closing wx.Dialog"""
  480. win = self.GetWindow()
  481. return self._validate(win)
  482. def _validate(self, win):
  483. """Validate input"""
  484. text = win.GetValue()
  485. if text:
  486. try:
  487. self.type(text)
  488. except ValueError:
  489. self._notvalid()
  490. return False
  491. self._valid()
  492. return True
  493. def _notvalid(self):
  494. textCtrl = self.GetWindow()
  495. textCtrl.SetBackgroundColour("grey")
  496. textCtrl.Refresh()
  497. def _valid(self):
  498. textCtrl = self.GetWindow()
  499. sysColor = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW)
  500. textCtrl.SetBackgroundColour(sysColor)
  501. textCtrl.Refresh()
  502. return True
  503. def TransferToWindow(self):
  504. return True # Prevent wxDialog from complaining.
  505. def TransferFromWindow(self):
  506. return True # Prevent wxDialog from complaining.
  507. class CoordinatesValidator(BaseValidator):
  508. """Validator for coordinates input (list of floats separated by comma)"""
  509. def __init__(self):
  510. BaseValidator.__init__(self)
  511. def _validate(self, win):
  512. """Validate input"""
  513. text = win.GetValue()
  514. if text:
  515. try:
  516. text = text.split(",")
  517. for t in text:
  518. float(t)
  519. if len(text) % 2 != 0:
  520. return False
  521. except ValueError:
  522. self._notvalid()
  523. return False
  524. self._valid()
  525. return True
  526. def Clone(self):
  527. """Clone validator"""
  528. return CoordinatesValidator()
  529. class IntegerValidator(BaseValidator):
  530. """Validator for floating-point input"""
  531. def __init__(self):
  532. BaseValidator.__init__(self)
  533. self.type = int
  534. def Clone(self):
  535. """Clone validator"""
  536. return IntegerValidator()
  537. class FloatValidator(BaseValidator):
  538. """Validator for floating-point input"""
  539. def __init__(self):
  540. BaseValidator.__init__(self)
  541. self.type = float
  542. def Clone(self):
  543. """Clone validator"""
  544. return FloatValidator()
  545. class EmailValidator(BaseValidator):
  546. """Validator for email input"""
  547. def __init__(self):
  548. BaseValidator.__init__(self)
  549. def _validate(self, win):
  550. """Validate input"""
  551. text = win.GetValue()
  552. if text:
  553. if re.match(r"\b[\w.-]+@[\w.-]+.\w{2,4}\b", text) is None:
  554. self._notvalid()
  555. return False
  556. self._valid()
  557. return True
  558. def Clone(self):
  559. """Clone validator"""
  560. return EmailValidator()
  561. class TimeISOValidator(BaseValidator):
  562. """Validator for time ISO format (YYYY-MM-DD) input"""
  563. def __init__(self):
  564. BaseValidator.__init__(self)
  565. def _validate(self, win):
  566. """Validate input"""
  567. text = win.GetValue()
  568. if text:
  569. try:
  570. datetime.strptime(text, "%Y-%m-%d")
  571. except:
  572. self._notvalid()
  573. return False
  574. self._valid()
  575. return True
  576. def Clone(self):
  577. """Clone validator"""
  578. return TimeISOValidator()
  579. class NTCValidator(Validator):
  580. """validates input in textctrls, taken from wxpython demo"""
  581. def __init__(self, flag=None):
  582. Validator.__init__(self)
  583. self.flag = flag
  584. self.Bind(wx.EVT_CHAR, self.OnChar)
  585. def Clone(self):
  586. return NTCValidator(self.flag)
  587. def OnChar(self, event):
  588. key = event.GetKeyCode()
  589. if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
  590. event.Skip()
  591. return
  592. if self.flag == "DIGIT_ONLY" and chr(key) in string.digits + ".-":
  593. event.Skip()
  594. return
  595. if not wx.Validator_IsSilent():
  596. wx.Bell()
  597. # Returning without calling even.Skip eats the event before it
  598. # gets to the text control
  599. return
  600. class SimpleValidator(Validator):
  601. """This validator is used to ensure that the user has entered something
  602. into the text object editor dialog's text field.
  603. """
  604. def __init__(self, callback):
  605. """Standard constructor."""
  606. Validator.__init__(self)
  607. self.callback = callback
  608. def Clone(self):
  609. """Standard cloner.
  610. Note that every validator must implement the Clone() method.
  611. """
  612. return SimpleValidator(self.callback)
  613. def Validate(self, win):
  614. """Validate the contents of the given text control."""
  615. ctrl = self.GetWindow()
  616. text = ctrl.GetValue()
  617. if len(text) == 0:
  618. self.callback(ctrl)
  619. return False
  620. else:
  621. return True
  622. def TransferToWindow(self):
  623. """Transfer data from validator to window.
  624. The default implementation returns False, indicating that an
  625. error occurred. We simply return True, as we don't do any data
  626. transfer.
  627. """
  628. return True # Prevent wxDialog from complaining.
  629. def TransferFromWindow(self):
  630. """Transfer data from window to validator.
  631. The default implementation returns False, indicating that an
  632. error occurred. We simply return True, as we don't do any data
  633. transfer.
  634. """
  635. return True # Prevent wxDialog from complaining.
  636. class GenericValidator(Validator):
  637. """This validator checks condition and calls callback
  638. in case the condition is not fulfilled.
  639. """
  640. def __init__(self, condition, callback):
  641. """Standard constructor.
  642. :param condition: function which accepts string value and returns T/F
  643. :param callback: function which is called when condition is not fulfilled
  644. """
  645. Validator.__init__(self)
  646. self._condition = condition
  647. self._callback = callback
  648. def Clone(self):
  649. """Standard cloner.
  650. Note that every validator must implement the Clone() method.
  651. """
  652. return GenericValidator(self._condition, self._callback)
  653. def Validate(self, win):
  654. """Validate the contents of the given text control."""
  655. ctrl = self.GetWindow()
  656. text = ctrl.GetValue()
  657. if not self._condition(text):
  658. self._callback(ctrl)
  659. return False
  660. else:
  661. return True
  662. def TransferToWindow(self):
  663. """Transfer data from validator to window."""
  664. return True # Prevent wxDialog from complaining.
  665. def TransferFromWindow(self):
  666. """Transfer data from window to validator."""
  667. return True # Prevent wxDialog from complaining.
  668. class MapValidator(GenericValidator):
  669. """Validator for map name input
  670. See G_legal_filename()
  671. """
  672. def __init__(self):
  673. def _mapNameValidationFailed(ctrl):
  674. message = _(
  675. "Name <%(name)s> is not a valid name for GRASS map. "
  676. "Please use only ASCII characters excluding %(chars)s "
  677. "and space."
  678. ) % {"name": ctrl.GetValue(), "chars": "/\"'@,=*~"}
  679. GError(message, caption=_("Invalid name"))
  680. GenericValidator.__init__(self, grass.legal_name, _mapNameValidationFailed)
  681. class GenericMultiValidator(Validator):
  682. """This validator checks conditions and calls callbacks
  683. in case the condition is not fulfilled.
  684. """
  685. def __init__(self, checks):
  686. """Standard constructor.
  687. :param checks: list of tuples consisting of conditions (list of
  688. functions which accepts string value and returns T/F) and callbacks (
  689. list of functions which is called when condition is not fulfilled)
  690. """
  691. Validator.__init__(self)
  692. self._checks = checks
  693. def Clone(self):
  694. """Standard cloner.
  695. Note that every validator must implement the Clone() method.
  696. """
  697. return GenericMultiValidator(self._checks)
  698. def Validate(self, win):
  699. """Validate the contents of the given text control."""
  700. ctrl = self.GetWindow()
  701. text = ctrl.GetValue()
  702. for condition, callback in self._checks:
  703. if not condition(text):
  704. callback(ctrl)
  705. return False
  706. return True
  707. def TransferToWindow(self):
  708. """Transfer data from validator to window."""
  709. return True # Prevent wxDialog from complaining.
  710. def TransferFromWindow(self):
  711. """Transfer data from window to validator."""
  712. return True # Prevent wxDialog from complaining.
  713. class SingleSymbolPanel(wx.Panel):
  714. """Panel for displaying one symbol.
  715. Changes background when selected. Assumes that parent will catch
  716. events emitted on mouse click. Used in gui_core::dialog::SymbolDialog.
  717. """
  718. def __init__(self, parent, symbolPath):
  719. """Panel constructor
  720. Signal symbolSelectionChanged - symbol selected
  721. - attribute 'name' (symbol name)
  722. - attribute 'doubleClick' (underlying cause)
  723. :param parent: parent (gui_core::dialog::SymbolDialog)
  724. :param symbolPath: absolute path to symbol
  725. """
  726. self.symbolSelectionChanged = Signal("SingleSymbolPanel.symbolSelectionChanged")
  727. wx.Panel.__init__(self, parent, id=wx.ID_ANY, style=wx.BORDER_RAISED)
  728. self.SetName(os.path.splitext(os.path.basename(symbolPath))[0])
  729. self.sBmp = wx.StaticBitmap(self, wx.ID_ANY, wx.Bitmap(symbolPath))
  730. self.selected = False
  731. self.selectColor = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT)
  732. self.deselectColor = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW)
  733. sizer = wx.BoxSizer()
  734. sizer.Add(self.sBmp, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  735. self.SetBackgroundColour(self.deselectColor)
  736. self.SetMinSize(self.GetBestSize())
  737. self.SetSizerAndFit(sizer)
  738. # binding to both (staticBitmap, Panel) necessary
  739. self.sBmp.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  740. self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  741. self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
  742. self.sBmp.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
  743. def OnLeftDown(self, event):
  744. """Panel selected, background changes"""
  745. self.selected = True
  746. self.SetBackgroundColour(self.selectColor)
  747. self.Refresh()
  748. event.Skip()
  749. self.symbolSelectionChanged.emit(name=self.GetName(), doubleClick=False)
  750. def OnDoubleClick(self, event):
  751. self.symbolSelectionChanged.emit(name=self.GetName(), doubleClick=True)
  752. def Deselect(self):
  753. """Panel deselected, background changes back to default"""
  754. self.selected = False
  755. self.SetBackgroundColour(self.deselectColor)
  756. self.Refresh()
  757. def Select(self):
  758. """Select panel, no event emitted"""
  759. self.selected = True
  760. self.SetBackgroundColour(self.selectColor)
  761. self.Refresh()
  762. class LayersListValidator(GenericValidator):
  763. """This validator check output map existence"""
  764. def __init__(self, condition, callback):
  765. """Standard constructor.
  766. :param condition: function which accepts string value and returns T/F
  767. :param callback: function which is called when condition is not fulfilled
  768. """
  769. GenericValidator.__init__(self, condition, callback)
  770. def Clone(self):
  771. """Standard cloner.
  772. Note that every validator must implement the Clone() method.
  773. """
  774. return LayersListValidator(self._condition, self._callback)
  775. def Validate(self, win, validate_all=False):
  776. """Validate output map existence"""
  777. mapset = grass.gisenv()["MAPSET"]
  778. maps = grass.list_grouped(type=self._condition)[mapset]
  779. # Check all selected layers
  780. if validate_all:
  781. outputs = []
  782. data = win.GetLayers()
  783. if data is None:
  784. return False
  785. for layer, output, list_id in data:
  786. if output in maps:
  787. outputs.append(output)
  788. if outputs:
  789. win.output_map = outputs
  790. self._callback(layers_list=win)
  791. return False
  792. else:
  793. output_map = win.GetItemText(win.col, win.row)
  794. if output_map in maps:
  795. win.output_map = output_map
  796. self._callback(layers_list=win)
  797. return False
  798. return True
  799. class PlacementValidator(BaseValidator):
  800. """Validator for placement input (list of floats separated by comma)"""
  801. def __init__(self, num_of_params):
  802. self._num_of_params = num_of_params
  803. super().__init__()
  804. def _enableDisableBtn(self, enable):
  805. """Enable/Disable buttomn
  806. :param bool enable: Enable/Disable btn
  807. """
  808. win = self.GetWindow().GetTopLevelParent()
  809. for btn_id in (wx.ID_OK, wx.ID_APPLY):
  810. btn = win.FindWindow(id=btn_id)
  811. if btn:
  812. btn.Enable(enable)
  813. def _valid(self):
  814. super()._valid()
  815. self._enableDisableBtn(enable=True)
  816. def _notvalid(self):
  817. super()._notvalid()
  818. self._enableDisableBtn(enable=False)
  819. def _validate(self, win):
  820. """Validate input"""
  821. text = win.GetValue()
  822. if text:
  823. try:
  824. text = text.split(",")
  825. for t in text:
  826. float(t)
  827. if len(text) % self._num_of_params != 0:
  828. self._notvalid()
  829. return False
  830. except ValueError:
  831. self._notvalid()
  832. return False
  833. self._valid()
  834. return True
  835. def Clone(self):
  836. """Clone validator"""
  837. return PlacementValidator(num_of_params=self._num_of_params)
  838. class GListCtrl(ListCtrl, listmix.ListCtrlAutoWidthMixin, CheckListCtrlMixin):
  839. """Generic ListCtrl with popup menu to select/deselect all
  840. items"""
  841. def __init__(self, parent):
  842. self.parent = parent
  843. ListCtrl.__init__(self, parent, id=wx.ID_ANY, style=wx.LC_REPORT)
  844. CheckListCtrlMixin.__init__(self)
  845. # setup mixins
  846. listmix.ListCtrlAutoWidthMixin.__init__(self)
  847. self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnPopupMenu) # wxMSW
  848. self.Bind(wx.EVT_RIGHT_UP, self.OnPopupMenu) # wxGTK
  849. def OnPopupMenu(self, event):
  850. """Show popup menu"""
  851. if self.GetItemCount() < 1:
  852. return
  853. if not hasattr(self, "popupDataID1"):
  854. self.popupDataID1 = NewId()
  855. self.popupDataID2 = NewId()
  856. self.Bind(wx.EVT_MENU, self.OnSelectAll, id=self.popupDataID1)
  857. self.Bind(wx.EVT_MENU, self.OnSelectNone, id=self.popupDataID2)
  858. # generate popup-menu
  859. menu = Menu()
  860. menu.Append(self.popupDataID1, _("Select all"))
  861. menu.Append(self.popupDataID2, _("Deselect all"))
  862. self.PopupMenu(menu)
  863. menu.Destroy()
  864. def SelectAll(self, select=True):
  865. """Check or uncheck all items"""
  866. item = -1
  867. while True:
  868. item = self.GetNextItem(item)
  869. if item == -1:
  870. break
  871. self.CheckItem(item, select)
  872. def OnSelectAll(self, event):
  873. """Check all items"""
  874. self.SelectAll(select=True)
  875. event.Skip()
  876. def OnSelectNone(self, event):
  877. """Uncheck items"""
  878. self.SelectAll(select=False)
  879. event.Skip()
  880. def GetData(self, checked=None):
  881. """Get list data"""
  882. data = []
  883. checkedList = []
  884. item = -1
  885. while True:
  886. row = []
  887. item = self.GetNextItem(item)
  888. if item == -1:
  889. break
  890. isChecked = self.IsItemChecked(item)
  891. if checked is not None and checked != isChecked:
  892. continue
  893. checkedList.append(isChecked)
  894. for i in range(self.GetColumnCount()):
  895. row.append(self.GetItem(item, i).GetText())
  896. row.append(item)
  897. data.append(tuple(row))
  898. if checked is not None:
  899. return tuple(data)
  900. else:
  901. return (tuple(data), tuple(checkedList))
  902. def LoadData(self, data=None, selectOne=True):
  903. """Load data into list"""
  904. self.DeleteAllItems()
  905. if data is None:
  906. return
  907. idx = 0
  908. for item in data:
  909. index = self.InsertItem(idx, str(item[0]))
  910. for i in range(1, self.GetColumnCount()):
  911. self.SetItem(index, i, item[i])
  912. idx += 1
  913. # check by default only on one item
  914. if len(data) == 1 and selectOne:
  915. self.CheckItem(index, True)
  916. class SearchModuleWidget(wx.Panel):
  917. """Search module widget (used e.g. in SearchModuleWindow)
  918. Signals:
  919. moduleSelected - attribute 'name' is module name
  920. showSearchResult - attribute 'result' is a node (representing module)
  921. showNotification - attribute 'message'
  922. """
  923. def __init__(self, parent, model, showChoice=True, showTip=False, **kwargs):
  924. self._showTip = showTip
  925. self._showChoice = showChoice
  926. self._model = model
  927. self._results = [] # list of found nodes
  928. self._resultIndex = -1
  929. self._searchKeys = ["description", "keywords", "command"]
  930. self._oldValue = ""
  931. self.moduleSelected = Signal("SearchModuleWidget.moduleSelected")
  932. self.showSearchResult = Signal("SearchModuleWidget.showSearchResult")
  933. self.showNotification = Signal("SearchModuleWidget.showNotification")
  934. wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY, **kwargs)
  935. # self._box = wx.StaticBox(parent = self, id = wx.ID_ANY,
  936. # label = " %s " % _("Find tool - (press Enter for next match)"))
  937. if sys.platform == "win32":
  938. self._search = TextCtrl(
  939. parent=self, id=wx.ID_ANY, size=(-1, 25), style=wx.TE_PROCESS_ENTER
  940. )
  941. else:
  942. self._search = SearchCtrl(
  943. parent=self, id=wx.ID_ANY, size=(-1, 25), style=wx.TE_PROCESS_ENTER
  944. )
  945. self._search.SetDescriptiveText(_("Fulltext search"))
  946. self._search.SetToolTip(
  947. _("Type to search in all tools. Press Enter for next match.")
  948. )
  949. self._search.Bind(wx.EVT_TEXT, self.OnSearchModule)
  950. self._search.Bind(wx.EVT_TEXT_ENTER, self.OnEnter)
  951. if self._showTip:
  952. self._searchTip = StaticWrapText(
  953. parent=self, id=wx.ID_ANY, label="Choose a tool", size=(-1, 35)
  954. )
  955. if self._showChoice:
  956. self._searchChoice = wx.Choice(parent=self, id=wx.ID_ANY)
  957. self._searchChoice.SetItems(self._searchModule(keys=["command"], value=""))
  958. self._searchChoice.Bind(wx.EVT_CHOICE, self.OnSelectModule)
  959. self._layout()
  960. def _layout(self):
  961. """Do layout"""
  962. sizer = wx.BoxSizer(wx.HORIZONTAL)
  963. boxSizer = wx.BoxSizer(wx.VERTICAL)
  964. boxSizer.Add(self._search, flag=wx.EXPAND | wx.BOTTOM, border=5)
  965. if self._showChoice:
  966. hSizer = wx.BoxSizer(wx.HORIZONTAL)
  967. hSizer.Add(self._searchChoice, flag=wx.EXPAND | wx.BOTTOM, border=5)
  968. hSizer.AddStretchSpacer()
  969. boxSizer.Add(hSizer, flag=wx.EXPAND)
  970. if self._showTip:
  971. boxSizer.Add(self._searchTip, flag=wx.EXPAND)
  972. sizer.Add(boxSizer, proportion=1)
  973. self.SetSizer(sizer)
  974. sizer.Fit(self)
  975. def OnEnter(self, event):
  976. """Process EVT_TEXT_ENTER to show search results"""
  977. self._showSearchResult()
  978. event.Skip()
  979. def _showSearchResult(self):
  980. if self._results:
  981. self._resultIndex += 1
  982. if self._resultIndex == len(self._results):
  983. self._resultIndex = 0
  984. self.showSearchResult.emit(result=self._results[self._resultIndex])
  985. def OnSearchModule(self, event):
  986. """Search module by keywords or description"""
  987. value = self._search.GetValue()
  988. if value == self._oldValue:
  989. event.Skip()
  990. return
  991. self._oldValue = value
  992. if len(value) <= 2:
  993. if len(value) == 0: # reset
  994. commands = self._searchModule(keys=["command"], value="")
  995. else:
  996. self.showNotification.emit(
  997. message=_("Searching, please type more characters.")
  998. )
  999. return
  1000. else:
  1001. commands = self._searchModule(keys=self._searchKeys, value=value)
  1002. if self._showChoice:
  1003. self._searchChoice.SetItems(commands)
  1004. if commands:
  1005. self._searchChoice.SetSelection(0)
  1006. self.OnSelectModule()
  1007. label = _("%d tools match") % len(commands)
  1008. if self._showTip:
  1009. self._searchTip.SetLabel(label)
  1010. self.showNotification.emit(message=label)
  1011. event.Skip()
  1012. def _searchModule(self, keys, value):
  1013. """Search modules by keys
  1014. :param keys: list of keys
  1015. :param value: patter to match
  1016. """
  1017. nodes = set()
  1018. for key in keys:
  1019. nodes.update(self._model.SearchNodes(key=key, value=value))
  1020. nodes = list(nodes)
  1021. nodes.sort(key=lambda node: self._model.GetIndexOfNode(node))
  1022. self._results = nodes
  1023. self._resultIndex = -1
  1024. commands = sorted(
  1025. [node.data["command"] for node in nodes if node.data["command"]]
  1026. )
  1027. return commands
  1028. def OnSelectModule(self, event=None):
  1029. """Module selected from choice, update command prompt"""
  1030. cmd = self._searchChoice.GetStringSelection()
  1031. self.moduleSelected.emit(name=cmd)
  1032. if self._showTip:
  1033. for module in self._results:
  1034. if cmd == module.data["command"]:
  1035. self._searchTip.SetLabel(module.data["description"])
  1036. break
  1037. def Reset(self):
  1038. """Reset widget"""
  1039. self._search.SetValue("")
  1040. if self._showTip:
  1041. self._searchTip.SetLabel("Choose a tool")
  1042. class ManageSettingsWidget(wx.Panel):
  1043. """Widget which allows loading and saving settings into file."""
  1044. def __init__(self, parent, settingsFile):
  1045. """
  1046. Signals:
  1047. settingsChanged - called when users changes setting
  1048. - attribute 'data' with chosen setting data
  1049. settingsSaving - called when settings are saving
  1050. - attribute 'name' with chosen settings name
  1051. settingsLoaded - called when settings are loaded
  1052. - attribute 'settings' is dict with loaded settings
  1053. {nameofsetting : settingdata, ....}
  1054. :param settingsFile: path to file, where settings will be saved and loaded from
  1055. """
  1056. self.settingsFile = settingsFile
  1057. self.settingsChanged = Signal("ManageSettingsWidget.settingsChanged")
  1058. self.settingsSaving = Signal("ManageSettingsWidget.settingsSaving")
  1059. self.settingsLoaded = Signal("ManageSettingsWidget.settingsLoaded")
  1060. wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
  1061. self.settingsBox = StaticBox(
  1062. parent=self, id=wx.ID_ANY, label=" %s " % _("Profiles")
  1063. )
  1064. self.settingsChoice = wx.Choice(parent=self, id=wx.ID_ANY)
  1065. self.settingsChoice.Bind(wx.EVT_CHOICE, self.OnSettingsChanged)
  1066. self.btnSettingsSave = Button(parent=self, id=wx.ID_SAVE)
  1067. self.btnSettingsSave.Bind(wx.EVT_BUTTON, self.OnSettingsSave)
  1068. self.btnSettingsSave.SetToolTip(_("Save current settings"))
  1069. self.btnSettingsDel = Button(parent=self, id=wx.ID_REMOVE)
  1070. self.btnSettingsDel.Bind(wx.EVT_BUTTON, self.OnSettingsDelete)
  1071. self.btnSettingsSave.SetToolTip(_("Delete currently selected settings"))
  1072. # escaping with '$' character - index in self.esc_chars
  1073. self.e_char_i = 0
  1074. self.esc_chars = ["$", ";"]
  1075. self._settings = self._loadSettings() # -> self.settingsChoice.SetItems()
  1076. self.settingsLoaded.emit(settings=self._settings)
  1077. self.data_to_save = []
  1078. self._layout()
  1079. self.SetSizer(self.settingsSizer)
  1080. self.settingsSizer.Fit(self)
  1081. def _layout(self):
  1082. self.settingsSizer = wx.StaticBoxSizer(self.settingsBox, wx.HORIZONTAL)
  1083. self.settingsSizer.Add(
  1084. StaticText(parent=self, id=wx.ID_ANY, label=_("Load:")),
  1085. flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.LEFT,
  1086. border=5,
  1087. )
  1088. self.settingsSizer.Add(
  1089. self.settingsChoice, proportion=1, flag=wx.EXPAND | wx.BOTTOM, border=3
  1090. )
  1091. self.settingsSizer.Add(
  1092. self.btnSettingsSave, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM, border=3
  1093. )
  1094. self.settingsSizer.Add(self.btnSettingsDel, flag=wx.RIGHT | wx.BOTTOM, border=3)
  1095. def OnSettingsChanged(self, event):
  1096. """Load named settings"""
  1097. name = event.GetString()
  1098. if name not in self._settings:
  1099. GError(parent=self, message=_("Settings <%s> not found") % name)
  1100. return
  1101. data = self._settings[name]
  1102. self.settingsChanged.emit(data=data)
  1103. def GetSettings(self):
  1104. """Load named settings"""
  1105. return self._settings.copy()
  1106. def OnSettingsSave(self, event):
  1107. """Save settings"""
  1108. dlg = wx.TextEntryDialog(
  1109. parent=self, message=_("Name:"), caption=_("Save settings")
  1110. )
  1111. if dlg.ShowModal() == wx.ID_OK:
  1112. name = dlg.GetValue()
  1113. if not name:
  1114. GMessage(
  1115. parent=self, message=_("Name not given, settings is not saved.")
  1116. )
  1117. else:
  1118. self.settingsSaving.emit(name=name)
  1119. dlg.Destroy()
  1120. def SaveSettings(self, name):
  1121. # check if settings item already exists
  1122. if name in self._settings:
  1123. dlgOwt = wx.MessageDialog(
  1124. self,
  1125. message=_(
  1126. "Settings <%s> already exists. "
  1127. "Do you want to overwrite the settings?"
  1128. )
  1129. % name,
  1130. caption=_("Save settings"),
  1131. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
  1132. )
  1133. if dlgOwt.ShowModal() != wx.ID_YES:
  1134. dlgOwt.Destroy()
  1135. return
  1136. if self.data_to_save:
  1137. self._settings[name] = self.data_to_save
  1138. self._saveSettings()
  1139. self.settingsChoice.SetStringSelection(name)
  1140. self.data_to_save = []
  1141. def _saveSettings(self):
  1142. """Save settings and reload if successful"""
  1143. if self._writeSettings() == 0:
  1144. self._settings = self._loadSettings()
  1145. def SetDataToSave(self, data):
  1146. """Set data for setting, which will be saved.
  1147. :param data: - list of strings, which will be saved
  1148. """
  1149. self.data_to_save = data
  1150. def SetSettings(self, settings):
  1151. """Set settings
  1152. :param settings: - dict with all settigs {nameofsetting : settingdata, ....}
  1153. """
  1154. self._settings = settings
  1155. self._saveSettings()
  1156. def AddSettings(self, settings):
  1157. """Add settings
  1158. :param settings: - dict with all settigs {nameofsetting : settingdata, ....}
  1159. """
  1160. self._settings.update(settings)
  1161. self._saveSettings()
  1162. def OnSettingsDelete(self, event):
  1163. """Save settings"""
  1164. name = self.settingsChoice.GetStringSelection()
  1165. if not name:
  1166. GMessage(
  1167. parent=self, message=_("No settings is defined. Operation canceled.")
  1168. )
  1169. return
  1170. self._settings.pop(name)
  1171. if self._writeSettings() == 0:
  1172. self._settings = self._loadSettings()
  1173. def _writeSettings(self):
  1174. """Save settings into the file
  1175. :return: 0 on success
  1176. :return: -1 on failure
  1177. """
  1178. try:
  1179. fd = open(self.settingsFile, "w")
  1180. fd.write("format_version=2.0\n")
  1181. for key, values in six.iteritems(self._settings):
  1182. first = True
  1183. for v in values:
  1184. # escaping characters
  1185. for e_ch in self.esc_chars:
  1186. v = v.replace(e_ch, self.esc_chars[self.e_char_i] + e_ch)
  1187. if first:
  1188. # escaping characters
  1189. for e_ch in self.esc_chars:
  1190. key = key.replace(
  1191. e_ch, self.esc_chars[self.e_char_i] + e_ch
  1192. )
  1193. fd.write("%s;%s;" % (key, v))
  1194. first = False
  1195. else:
  1196. fd.write("%s;" % (v))
  1197. fd.write("\n")
  1198. except IOError:
  1199. GError(parent=self, message=_("Unable to save settings"))
  1200. return -1
  1201. fd.close()
  1202. return 0
  1203. def _loadSettings(self):
  1204. """Load settings from the file
  1205. The file is defined by self.SettingsFile.
  1206. :return: parsed dict
  1207. :return: empty dict on error
  1208. """
  1209. data = dict()
  1210. if not os.path.exists(self.settingsFile):
  1211. return data
  1212. try:
  1213. fd = open(self.settingsFile, "r")
  1214. except IOError:
  1215. return data
  1216. fd_lines = fd.readlines()
  1217. if not fd_lines:
  1218. fd.close()
  1219. return data
  1220. if fd_lines[0].strip() == "format_version=2.0":
  1221. data = self._loadSettings_v2(fd_lines)
  1222. else:
  1223. data = self._loadSettings_v1(fd_lines)
  1224. self.settingsChoice.SetItems(sorted(data.keys()))
  1225. fd.close()
  1226. self.settingsLoaded.emit(settings=data)
  1227. return data
  1228. def _loadSettings_v2(self, fd_lines):
  1229. """Load settings from the file in format version 2.0
  1230. The file is defined by self.SettingsFile.
  1231. :return: parsed dict
  1232. :return: empty dict on error
  1233. """
  1234. data = dict()
  1235. for line in fd_lines[1:]:
  1236. try:
  1237. lineData = []
  1238. line = line.rstrip("\n")
  1239. i_last_found = i_last = 0
  1240. key = ""
  1241. while True:
  1242. idx = line.find(";", i_last)
  1243. if idx < 0:
  1244. break
  1245. elif idx != 0:
  1246. # find out whether it is separator
  1247. # $$$$; - it is separator
  1248. # $$$$$; - it is not separator
  1249. i_esc_chars = 0
  1250. while True:
  1251. if (
  1252. line[idx - (i_esc_chars + 1)]
  1253. == self.esc_chars[self.e_char_i]
  1254. ):
  1255. i_esc_chars += 1
  1256. else:
  1257. break
  1258. if i_esc_chars % 2 != 0:
  1259. i_last = idx + 1
  1260. continue
  1261. lineItem = line[i_last_found:idx]
  1262. # unescape characters
  1263. for e_ch in self.esc_chars:
  1264. lineItem = lineItem.replace(
  1265. self.esc_chars[self.e_char_i] + e_ch, e_ch
  1266. )
  1267. if i_last_found == 0:
  1268. key = lineItem
  1269. else:
  1270. lineData.append(lineItem)
  1271. i_last_found = i_last = idx + 1
  1272. if key and lineData:
  1273. data[key] = lineData
  1274. except ValueError:
  1275. pass
  1276. return data
  1277. def _loadSettings_v1(self, fd_lines):
  1278. """Load settings from the file in format version 1.0 (backward compatibility)
  1279. The file is defined by self.SettingsFile.
  1280. :return: parsed dict
  1281. :return: empty dict on error
  1282. """
  1283. data = dict()
  1284. for line in fd_lines:
  1285. try:
  1286. lineData = line.rstrip("\n").split(";")
  1287. if len(lineData) > 4:
  1288. # type, dsn, format, options
  1289. data[lineData[0]] = (
  1290. lineData[1],
  1291. lineData[2],
  1292. lineData[3],
  1293. lineData[4],
  1294. )
  1295. else:
  1296. data[lineData[0]] = (lineData[1], lineData[2], lineData[3], "")
  1297. except ValueError:
  1298. pass
  1299. return data
  1300. class PictureComboBox(OwnerDrawnComboBox):
  1301. """Abstract class of ComboBox with pictures.
  1302. Derived class has to specify has to specify _getPath method.
  1303. """
  1304. def OnDrawItem(self, dc, rect, item, flags):
  1305. """Overridden from OwnerDrawnComboBox.
  1306. Called to draw each item in the list.
  1307. """
  1308. if item == wx.NOT_FOUND:
  1309. # painting the control, but there is no valid item selected yet
  1310. return
  1311. r = Rect(*rect) # make a copy
  1312. r.Deflate(3, 5)
  1313. # for painting the items in the popup
  1314. bitmap = self.GetPictureBitmap(self.GetString(item))
  1315. if bitmap:
  1316. dc.DrawBitmap(bitmap, r.x, r.y + (r.height - bitmap.GetHeight()) // 2)
  1317. width = bitmap.GetWidth() + 10
  1318. else:
  1319. width = 0
  1320. dc.DrawText(
  1321. self.GetString(item),
  1322. r.x + width,
  1323. (r.y + 0) + (r.height - dc.GetCharHeight()) // 2,
  1324. )
  1325. def OnMeasureItem(self, item):
  1326. """Overridden from OwnerDrawnComboBox, should return the height.
  1327. Needed to display an item in the popup, or -1 for default.
  1328. """
  1329. return 24
  1330. def GetPictureBitmap(self, name):
  1331. """Returns bitmap for given picture name.
  1332. :param str colorTable: name of color table
  1333. """
  1334. if not hasattr(self, "bitmaps"):
  1335. self.bitmaps = {}
  1336. if name in self.bitmaps:
  1337. return self.bitmaps[name]
  1338. path = self._getPath(name)
  1339. if os.path.exists(path):
  1340. bitmap = wx.Bitmap(path)
  1341. self.bitmaps[name] = bitmap
  1342. return bitmap
  1343. return None
  1344. class ColorTablesComboBox(PictureComboBox):
  1345. """ComboBox with drawn color tables (created by thumbnails.py).
  1346. Used in r(3).colors dialog."""
  1347. def _getPath(self, name):
  1348. return os.path.join(
  1349. os.getenv("GISBASE"), "docs", "html", "colortables", "%s.png" % name
  1350. )
  1351. class BarscalesComboBox(PictureComboBox):
  1352. """ComboBox with barscales for d.barscale."""
  1353. def _getPath(self, name):
  1354. return os.path.join(
  1355. os.getenv("GISBASE"), "docs", "html", "barscales", name + ".png"
  1356. )
  1357. class NArrowsComboBox(PictureComboBox):
  1358. """ComboBox with north arrows for d.barscale."""
  1359. def _getPath(self, name):
  1360. return os.path.join(
  1361. os.getenv("GISBASE"), "docs", "html", "northarrows", "%s.png" % name
  1362. )
  1363. class LayersList(GListCtrl, listmix.TextEditMixin):
  1364. """List of layers to be imported (dxf, shp...)"""
  1365. def __init__(self, parent, columns, log=None):
  1366. GListCtrl.__init__(self, parent)
  1367. self.log = log
  1368. self.row = None
  1369. self.col = None
  1370. self.output_map = None
  1371. self.validate = True
  1372. # setup mixins
  1373. listmix.TextEditMixin.__init__(self)
  1374. for i in range(len(columns)):
  1375. self.InsertColumn(i, columns[i])
  1376. width = []
  1377. if len(columns) == 3:
  1378. width = (65, 200)
  1379. elif len(columns) == 4:
  1380. width = (65, 200, 90)
  1381. elif len(columns) == 5:
  1382. width = (65, 180, 90, 70)
  1383. for i in range(len(width)):
  1384. self.SetColumnWidth(col=i, width=width[i])
  1385. def OnLeftDown(self, event):
  1386. """Allow editing only output name
  1387. Code taken from TextEditMixin class.
  1388. """
  1389. x, y = event.GetPosition()
  1390. colLocs = [0]
  1391. loc = 0
  1392. for n in range(self.GetColumnCount()):
  1393. loc = loc + self.GetColumnWidth(n)
  1394. colLocs.append(loc)
  1395. col = bisect(colLocs, x + self.GetScrollPos(wx.HORIZONTAL)) - 1
  1396. if col == self.GetColumnCount() - 1:
  1397. listmix.TextEditMixin.OnLeftDown(self, event)
  1398. else:
  1399. event.Skip()
  1400. def GetLayers(self):
  1401. """Get list of layers (layer name, output name, list id)"""
  1402. layers = []
  1403. data = self.GetData(checked=True)
  1404. for itm in data:
  1405. layer = itm[1]
  1406. ftype = itm[2]
  1407. if "/" in ftype:
  1408. layer += "|%s" % ftype.split("/", 1)[0]
  1409. output = itm[self.GetColumnCount() - 1]
  1410. layers.append((layer, output, itm[-1]))
  1411. return layers
  1412. def ValidateOutputMapName(self):
  1413. """Validate output map name"""
  1414. wx.CallAfter(self.GetValidator().Validate, self)
  1415. def OpenEditor(self, row, col):
  1416. """Open editor"""
  1417. self.col = col
  1418. self.row = row
  1419. super().OpenEditor(row, col)
  1420. def CloseEditor(self, event=None):
  1421. """Close editor"""
  1422. if event:
  1423. if event.IsCommandEvent():
  1424. listmix.TextEditMixin.CloseEditor(self, event)
  1425. if self.validate:
  1426. self.ValidateOutputMapName()
  1427. event.Skip()