dialogs.py 83 KB

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