dialogs.py 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  1. """!
  2. @package gui_core.dialogs
  3. @brief Various dialogs used in wxGUI.
  4. List of classes:
  5. - dialogs::ElementDialog
  6. - dialogs::LocationDialog
  7. - dialogs::MapsetDialog
  8. - dialogs::NewVectorDialog
  9. - dialogs::SavedRegion
  10. - dialogs::DecorationDialog
  11. - dialogs::TextLayerDialog
  12. - dialogs::GroupDialog
  13. - dialogs::MapLayersDialog
  14. - dialogs::ImportDialog
  15. - dialogs::GdalImportDialog
  16. - dialogs::GdalOutputDialog
  17. - dialogs::DxfImportDialog
  18. - dialogs::LayersList (used by MultiImport)
  19. - dialogs::SetOpacityDialog
  20. - dialogs::StaticWrapText
  21. - dialogs::ImageSizeDialog
  22. - dialogs::SqlQueryFrame
  23. (C) 2008-2011 by the GRASS Development Team
  24. This program is free software under the GNU General Public License
  25. (>=v2). Read the file COPYING that comes with GRASS for details.
  26. @author Martin Landa <landa.martin gmail.com>
  27. @author Anna Kratochvilova <kratochanna gmail.com> (GroupDialog)
  28. """
  29. import os
  30. import sys
  31. import re
  32. from bisect import bisect
  33. import wx
  34. import wx.lib.filebrowsebutton as filebrowse
  35. import wx.lib.mixins.listctrl as listmix
  36. from grass.script import core as grass
  37. from grass.script import task as gtask
  38. from core import globalvar
  39. from core.gcmd import GError, RunCommand, GMessage
  40. from gui_core.gselect import ElementSelect, LocationSelect, MapsetSelect, Select, OgrTypeSelect, GdalSelect
  41. from gui_core.forms import GUI
  42. from core.utils import GetListOfMapsets, GetLayerNameFromCmd, GetValidLayerName
  43. from core.settings import UserSettings
  44. from core.debug import Debug
  45. class ElementDialog(wx.Dialog):
  46. def __init__(self, parent, title, label, id = wx.ID_ANY,
  47. etype = False, style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  48. **kwargs):
  49. """!General dialog to choose given element (location, mapset, vector map, etc.)
  50. @param parent window
  51. @param title window title
  52. @param label element label
  53. @param etype show also ElementSelect
  54. """
  55. wx.Dialog.__init__(self, parent, id, title, style = style, **kwargs)
  56. self.etype = etype
  57. self.label = label
  58. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  59. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  60. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  61. self.btnOK.SetDefault()
  62. self.btnOK.Enable(False)
  63. if self.etype:
  64. self.typeSelect = ElementSelect(parent = self.panel,
  65. size = globalvar.DIALOG_GSELECT_SIZE)
  66. self.typeSelect.Bind(wx.EVT_CHOICE, self.OnType)
  67. self.element = None # must be defined
  68. self.__layout()
  69. def PostInit(self):
  70. self.element.SetFocus()
  71. self.element.Bind(wx.EVT_TEXT, self.OnElement)
  72. def OnType(self, event):
  73. """!Select element type"""
  74. if not self.etype:
  75. return
  76. evalue = self.typeSelect.GetValue(event.GetString())
  77. self.element.SetType(evalue)
  78. def OnElement(self, event):
  79. """!Name for vector map layer given"""
  80. if len(event.GetString()) > 0:
  81. self.btnOK.Enable(True)
  82. else:
  83. self.btnOK.Enable(False)
  84. def __layout(self):
  85. """!Do layout"""
  86. self.sizer = wx.BoxSizer(wx.VERTICAL)
  87. self.dataSizer = wx.BoxSizer(wx.VERTICAL)
  88. if self.etype:
  89. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  90. label = _("Type of element:")),
  91. proportion = 0, flag = wx.ALL, border = 1)
  92. self.dataSizer.Add(item = self.typeSelect,
  93. proportion = 0, flag = wx.ALL, border = 1)
  94. self.dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  95. label = self.label),
  96. proportion = 0, flag = wx.ALL, border = 1)
  97. # buttons
  98. btnSizer = wx.StdDialogButtonSizer()
  99. btnSizer.AddButton(self.btnCancel)
  100. btnSizer.AddButton(self.btnOK)
  101. btnSizer.Realize()
  102. self.sizer.Add(item = self.dataSizer, proportion = 1,
  103. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  104. self.sizer.Add(item = btnSizer, proportion = 0,
  105. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  106. def GetElement(self):
  107. """!Return (mapName, overwrite)"""
  108. return self.element.GetValue()
  109. def GetType(self):
  110. """!Get element type"""
  111. return self.element.tcp.GetType()
  112. class LocationDialog(ElementDialog):
  113. """!Dialog used to select location"""
  114. def __init__(self, parent, title = _("Select GRASS location and mapset"), id = wx.ID_ANY):
  115. ElementDialog.__init__(self, parent, title, label = _("Name of GRASS location:"))
  116. self.element = LocationSelect(parent = self.panel, id = wx.ID_ANY,
  117. size = globalvar.DIALOG_GSELECT_SIZE)
  118. self.element1 = MapsetSelect(parent = self.panel, id = wx.ID_ANY,
  119. size = globalvar.DIALOG_GSELECT_SIZE,
  120. setItems = False)
  121. self.PostInit()
  122. self.__Layout()
  123. self.SetMinSize(self.GetSize())
  124. def __Layout(self):
  125. """!Do layout"""
  126. self.dataSizer.Add(self.element, proportion = 0,
  127. flag = wx.EXPAND | wx.ALL, border = 1)
  128. self.dataSizer.Add(wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  129. label = _("Name of mapset:")), proportion = 0,
  130. flag = wx.EXPAND | wx.ALL, border = 1)
  131. self.dataSizer.Add(self.element1, proportion = 0,
  132. flag = wx.EXPAND | wx.ALL, border = 1)
  133. self.panel.SetSizer(self.sizer)
  134. self.sizer.Fit(self)
  135. def OnElement(self, event):
  136. """!Select mapset given location name"""
  137. location = event.GetString()
  138. if location:
  139. dbase = grass.gisenv()['GISDBASE']
  140. self.element1.SetItems(GetListOfMapsets(dbase, location, selectable = True))
  141. self.element1.SetSelection(0)
  142. mapset = self.element1.GetStringSelection()
  143. if location and mapset:
  144. self.btnOK.Enable(True)
  145. else:
  146. self.btnOK.Enable(False)
  147. def GetValues(self):
  148. """!Get location, mapset"""
  149. return (self.GetElement(), self.element1.GetStringSelection())
  150. class MapsetDialog(ElementDialog):
  151. """!Dialog used to select mapset"""
  152. def __init__(self, parent, title = _("Select mapset in GRASS location"),
  153. location = None, id = wx.ID_ANY):
  154. ElementDialog.__init__(self, parent, title, label = _("Name of mapset:"))
  155. if location:
  156. self.SetTitle(self.GetTitle() + ' <%s>' % location)
  157. else:
  158. self.SetTitle(self.GetTitle() + ' <%s>' % grass.gisenv()['LOCATION_NAME'])
  159. self.element = gselect.MapsetSelect(parent = self.panel, id = wx.ID_ANY,
  160. size = globalvar.DIALOG_GSELECT_SIZE)
  161. self.PostInit()
  162. self.__Layout()
  163. self.SetMinSize(self.GetSize())
  164. def __Layout(self):
  165. """!Do layout"""
  166. self.dataSizer.Add(self.element, proportion = 0,
  167. flag = wx.EXPAND | wx.ALL, border = 1)
  168. self.panel.SetSizer(self.sizer)
  169. self.sizer.Fit(self)
  170. def GetMapset(self):
  171. return self.GetElement()
  172. class NewVectorDialog(ElementDialog):
  173. def __init__(self, parent, id = wx.ID_ANY, title = _('Create new vector map'),
  174. disableAdd = False, disableTable = False, showType = False,
  175. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, *kwargs):
  176. """!Dialog for creating new vector map
  177. @param parent parent window
  178. @param id window id
  179. @param title window title
  180. @param disableAdd disable 'add layer' checkbox
  181. @param disableTable disable 'create table' checkbox
  182. @param showType True to show feature type selector (used for creating new empty OGR layers)
  183. @param style window style
  184. @param kwargs other argumentes for ElementDialog
  185. @return dialog instance
  186. """
  187. ElementDialog.__init__(self, parent, title, label = _("Name for new vector map:"))
  188. self.element = Select(parent = self.panel, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  189. type = 'vector', mapsets = [grass.gisenv()['MAPSET'],])
  190. # determine output format
  191. if showType:
  192. self.ftype = OgrTypeSelect(parent = self, panel = self.panel)
  193. else:
  194. self.ftype = None
  195. self.table = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  196. label = _("Create attribute table"))
  197. self.table.SetValue(True)
  198. if disableTable:
  199. self.table.Enable(False)
  200. if showType:
  201. self.keycol = None
  202. else:
  203. self.keycol = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  204. size = globalvar.DIALOG_SPIN_SIZE)
  205. self.keycol.SetValue(UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value'))
  206. if disableTable:
  207. self.keycol.Enable(False)
  208. self.addbox = wx.CheckBox(parent = self.panel,
  209. label = _('Add created map into layer tree'), style = wx.NO_BORDER)
  210. if disableAdd:
  211. self.addbox.SetValue(True)
  212. self.addbox.Enable(False)
  213. else:
  214. self.addbox.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  215. self.table.Bind(wx.EVT_CHECKBOX, self.OnTable)
  216. self.PostInit()
  217. self._layout()
  218. self.SetMinSize(self.GetSize())
  219. def OnMapName(self, event):
  220. """!Name for vector map layer given"""
  221. self.OnElement(event)
  222. def OnTable(self, event):
  223. if self.keycol:
  224. self.keycol.Enable(event.IsChecked())
  225. def _layout(self):
  226. """!Do layout"""
  227. self.dataSizer.Add(item = self.element, proportion = 0,
  228. flag = wx.EXPAND | wx.ALL, border = 1)
  229. if self.ftype:
  230. self.dataSizer.AddSpacer(1)
  231. self.dataSizer.Add(item = self.ftype, proportion = 0,
  232. flag = wx.EXPAND | wx.ALL, border = 1)
  233. self.dataSizer.Add(item = self.table, proportion = 0,
  234. flag = wx.EXPAND | wx.ALL, border = 1)
  235. if self.keycol:
  236. keySizer = wx.BoxSizer(wx.HORIZONTAL)
  237. keySizer.Add(item = wx.StaticText(parent = self.panel, label = _("Key column:")),
  238. proportion = 0,
  239. flag = wx.ALIGN_CENTER_VERTICAL)
  240. keySizer.AddSpacer(10)
  241. keySizer.Add(item = self.keycol, proportion = 0,
  242. flag = wx.ALIGN_RIGHT)
  243. self.dataSizer.Add(item = keySizer, proportion = 1,
  244. flag = wx.EXPAND | wx.ALL, border = 1)
  245. self.dataSizer.AddSpacer(5)
  246. self.dataSizer.Add(item = self.addbox, proportion = 0,
  247. flag = wx.EXPAND | wx.ALL, border = 1)
  248. self.panel.SetSizer(self.sizer)
  249. self.sizer.Fit(self)
  250. def GetName(self, full = False):
  251. """!Get name of vector map to be created
  252. @param full True to get fully qualified name
  253. """
  254. name = self.GetElement()
  255. if full:
  256. if '@' in name:
  257. return name
  258. else:
  259. return name + '@' + grass.gisenv()['MAPSET']
  260. return name.split('@', 1)[0]
  261. def GetKey(self):
  262. """!Get key column name"""
  263. if self.keycol:
  264. return self.keycol.GetValue()
  265. return UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value')
  266. def IsChecked(self, key):
  267. """!Get dialog properties
  268. @param key window key ('add', 'table')
  269. @return True/False
  270. @return None on error
  271. """
  272. if key == 'add':
  273. return self.addbox.IsChecked()
  274. elif key == 'table':
  275. return self.table.IsChecked()
  276. return None
  277. def GetFeatureType(self):
  278. """!Get feature type for OGR
  279. @return feature type as string
  280. @return None for native format
  281. """
  282. if self.ftype:
  283. return self.ftype.GetType()
  284. return None
  285. def CreateNewVector(parent, cmd, title = _('Create new vector map'),
  286. exceptMap = None, log = None,
  287. disableAdd = False, disableTable = False):
  288. """!Create new vector map layer
  289. @param cmd (prog, **kwargs)
  290. @param title window title
  291. @param exceptMap list of maps to be excepted
  292. @param log
  293. @param disableAdd disable 'add layer' checkbox
  294. @param disableTable disable 'create table' checkbox
  295. @return dialog instance
  296. @return None on error
  297. """
  298. vExternalOut = grass.parse_command('v.external.out', flags = 'g')
  299. isNative = vExternalOut['format'] == 'native'
  300. if cmd[0] == 'v.edit' and not isNative:
  301. showType = True
  302. else:
  303. showType = False
  304. dlg = NewVectorDialog(parent, title = title,
  305. disableAdd = disableAdd, disableTable = disableTable,
  306. showType = showType)
  307. if dlg.ShowModal() != wx.ID_OK:
  308. dlg.Destroy()
  309. return None
  310. outmap = dlg.GetName()
  311. key = dlg.GetKey()
  312. if outmap == exceptMap:
  313. GError(parent = parent,
  314. message = _("Unable to create vector map <%s>.") % outmap)
  315. dlg.Destroy()
  316. return None
  317. if dlg.table.IsEnabled() and not key:
  318. GError(parent = parent,
  319. message = _("Invalid or empty key column.\n"
  320. "Unable to create vector map <%s>.") % outmap)
  321. dlg.Destroy()
  322. return
  323. if outmap == '': # should not happen
  324. dlg.Destroy()
  325. return None
  326. # update cmd -> output name defined
  327. cmd[1][cmd[2]] = outmap
  328. if showType:
  329. cmd[1]['type'] = dlg.GetFeatureType()
  330. if isNative:
  331. listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
  332. else:
  333. listOfVectors = RunCommand('v.external',
  334. quiet = True,
  335. parent = parent,
  336. read = True,
  337. flags = 'l',
  338. dsn = vExternalOut['dsn']).splitlines()
  339. overwrite = False
  340. if not UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled') and \
  341. outmap in listOfVectors:
  342. dlgOw = wx.MessageDialog(parent, message = _("Vector map <%s> already exists "
  343. "in the current mapset. "
  344. "Do you want to overwrite it?") % outmap,
  345. caption = _("Overwrite?"),
  346. style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  347. if dlgOw.ShowModal() == wx.ID_YES:
  348. overwrite = True
  349. else:
  350. dlgOw.Destroy()
  351. dlg.Destroy()
  352. return None
  353. if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  354. overwrite = True
  355. ret = RunCommand(prog = cmd[0],
  356. parent = parent,
  357. overwrite = overwrite,
  358. **cmd[1])
  359. if ret != 0:
  360. dlg.Destroy()
  361. return None
  362. if not isNative:
  363. # create link for OGR layers
  364. RunCommand('v.external',
  365. overwrite = overwrite,
  366. parent = parent,
  367. dsn = vExternalOut['dsn'],
  368. layer = outmap)
  369. # create attribute table
  370. if dlg.table.IsEnabled() and dlg.table.IsChecked():
  371. if isNative:
  372. sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
  373. RunCommand('db.connect',
  374. flags = 'c')
  375. Debug.msg(1, "SQL: %s" % sql)
  376. RunCommand('db.execute',
  377. quiet = True,
  378. parent = parent,
  379. input = '-',
  380. stdin = sql)
  381. RunCommand('v.db.connect',
  382. quiet = True,
  383. parent = parent,
  384. map = outmap,
  385. table = outmap,
  386. key = key,
  387. layer = '1')
  388. # TODO: how to deal with attribute tables for OGR layers?
  389. # return fully qualified map name
  390. if '@' not in outmap:
  391. outmap += '@' + grass.gisenv()['MAPSET']
  392. if log:
  393. log.WriteLog(_("New vector map <%s> created") % outmap)
  394. return dlg
  395. class SavedRegion(wx.Dialog):
  396. def __init__(self, parent, id = wx.ID_ANY, title = "", loadsave = 'load',
  397. **kwargs):
  398. """!Loading and saving of display extents to saved region file
  399. @param loadsave load or save region?
  400. """
  401. wx.Dialog.__init__(self, parent, id, title, **kwargs)
  402. self.loadsave = loadsave
  403. self.wind = ''
  404. sizer = wx.BoxSizer(wx.VERTICAL)
  405. box = wx.BoxSizer(wx.HORIZONTAL)
  406. label = wx.StaticText(parent = self, id = wx.ID_ANY)
  407. box.Add(item = label, proportion = 0, flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  408. if loadsave == 'load':
  409. label.SetLabel(_("Load region:"))
  410. selection = Select(parent = self, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  411. type = 'windows')
  412. elif loadsave == 'save':
  413. label.SetLabel(_("Save region:"))
  414. selection = Select(parent = self, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
  415. type = 'windows', mapsets = [grass.gisenv()['MAPSET']])
  416. box.Add(item = selection, proportion = 0, flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  417. selection.SetFocus()
  418. selection.Bind(wx.EVT_TEXT, self.OnRegion)
  419. sizer.Add(item = box, proportion = 0, flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
  420. border = 5)
  421. line = wx.StaticLine(parent = self, id = wx.ID_ANY, size = (20, -1), style = wx.LI_HORIZONTAL)
  422. sizer.Add(item = line, proportion = 0,
  423. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, border = 5)
  424. btnsizer = wx.StdDialogButtonSizer()
  425. btn = wx.Button(parent = self, id = wx.ID_OK)
  426. btn.SetDefault()
  427. btnsizer.AddButton(btn)
  428. btn = wx.Button(parent = self, id = wx.ID_CANCEL)
  429. btnsizer.AddButton(btn)
  430. btnsizer.Realize()
  431. sizer.Add(item = btnsizer, proportion = 0, flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  432. self.SetSizer(sizer)
  433. sizer.Fit(self)
  434. self.Layout()
  435. def OnRegion(self, event):
  436. self.wind = event.GetString()
  437. class DecorationDialog(wx.Dialog):
  438. """!Controls setting options and displaying/hiding map overlay
  439. decorations
  440. """
  441. def __init__(self, parent, ovlId, title, cmd, name = None,
  442. pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE,
  443. checktxt = '', ctrltxt = ''):
  444. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  445. self.ovlId = ovlId # PseudoDC id
  446. self.cmd = cmd
  447. self.name = name # overlay name
  448. self.parent = parent # MapFrame
  449. sizer = wx.BoxSizer(wx.VERTICAL)
  450. box = wx.BoxSizer(wx.HORIZONTAL)
  451. self.chkbox = wx.CheckBox(parent = self, id = wx.ID_ANY, label = checktxt)
  452. if self.parent.Map.GetOverlay(self.ovlId) is None:
  453. self.chkbox.SetValue(True)
  454. else:
  455. self.chkbox.SetValue(self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive())
  456. box.Add(item = self.chkbox, proportion = 0,
  457. flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  458. sizer.Add(item = box, proportion = 0,
  459. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  460. box = wx.BoxSizer(wx.HORIZONTAL)
  461. optnbtn = wx.Button(parent = self, id = wx.ID_ANY, label = _("Set options"))
  462. box.Add(item = optnbtn, proportion = 0, flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  463. sizer.Add(item = box, proportion = 0,
  464. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  465. if self.name == 'legend':
  466. box = wx.BoxSizer(wx.HORIZONTAL)
  467. resize = wx.ToggleButton(parent = self, id = wx.ID_ANY, label = _("Set size and position"))
  468. resize.SetToolTipString(_("Click and drag on the map display to set legend"
  469. " size and position and then press OK"))
  470. resize.SetName('resize')
  471. if self.parent.IsPaneShown('3d'):
  472. resize.Disable()
  473. box.Add(item = resize, proportion = 0, flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  474. sizer.Add(item = box, proportion = 0,
  475. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  476. box = wx.BoxSizer(wx.HORIZONTAL)
  477. label = wx.StaticText(parent = self, id = wx.ID_ANY,
  478. label = _("Drag %s with mouse in pointer mode to position.\n"
  479. "Double-click to change options." % ctrltxt))
  480. if self.name == 'legend':
  481. label.SetLabel(label.GetLabel() + _('\nDefine raster map name for legend in '
  482. 'properties dialog.'))
  483. box.Add(item = label, proportion = 0,
  484. flag = wx.ALIGN_CENTRE|wx.ALL, border = 5)
  485. sizer.Add(item = box, proportion = 0,
  486. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  487. line = wx.StaticLine(parent = self, id = wx.ID_ANY, size = (20,-1), style = wx.LI_HORIZONTAL)
  488. sizer.Add(item = line, proportion = 0,
  489. flag = wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, border = 5)
  490. # buttons
  491. btnsizer = wx.StdDialogButtonSizer()
  492. self.btnOK = wx.Button(parent = self, id = wx.ID_OK)
  493. self.btnOK.SetDefault()
  494. if self.name == 'legend':
  495. self.btnOK.Enable(False)
  496. btnsizer.AddButton(self.btnOK)
  497. btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
  498. btnsizer.AddButton(btnCancel)
  499. btnsizer.Realize()
  500. sizer.Add(item = btnsizer, proportion = 0,
  501. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  502. #
  503. # bindings
  504. #
  505. self.Bind(wx.EVT_BUTTON, self.OnOptions, optnbtn)
  506. if self.name == 'legend':
  507. self.Bind(wx.EVT_TOGGLEBUTTON, self.OnResize, resize)
  508. self.Bind(wx.EVT_BUTTON, self.OnCancel, btnCancel)
  509. self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOK)
  510. self.SetSizer(sizer)
  511. sizer.Fit(self)
  512. # create overlay if doesn't exist
  513. self._createOverlay()
  514. if len(self.parent.MapWindow.overlays[self.ovlId]['cmd']) > 1:
  515. if name == 'legend':
  516. mapName, found = GetLayerNameFromCmd(self.parent.MapWindow.overlays[self.ovlId]['cmd'])
  517. if found:
  518. # enable 'OK' button
  519. self.btnOK.Enable()
  520. # set title
  521. self.SetTitle(_('Legend of raster map <%s>') % \
  522. mapName)
  523. def _createOverlay(self):
  524. """!Creates overlay"""
  525. if not self.parent.GetMap().GetOverlay(self.ovlId):
  526. self.newOverlay = self.parent.Map.AddOverlay(id = self.ovlId, type = self.name,
  527. command = self.cmd,
  528. l_active = False, l_render = False, l_hidden = True)
  529. prop = { 'layer' : self.newOverlay,
  530. 'params' : None,
  531. 'propwin' : None,
  532. 'cmd' : self.cmd,
  533. 'coords': (0, 0),
  534. 'pdcType': 'image' }
  535. self.parent.MapWindow2D.overlays[self.ovlId] = prop
  536. if self.parent.MapWindow3D:
  537. self.parent.MapWindow3D.overlays[self.ovlId] = prop
  538. else:
  539. if self.parent.MapWindow.overlays[self.ovlId]['propwin'] == None:
  540. return
  541. self.parent.MapWindow.overlays[self.ovlId]['propwin'].get_dcmd = self.GetOptData
  542. def OnOptions(self, event):
  543. """!Sets option for decoration map overlays
  544. """
  545. if self.parent.MapWindow.overlays[self.ovlId]['propwin'] is None:
  546. # build properties dialog
  547. GUI(parent = self.parent).ParseCommand(cmd = self.cmd,
  548. completed = (self.GetOptData, self.name, ''))
  549. else:
  550. if self.parent.MapWindow.overlays[self.ovlId]['propwin'].IsShown():
  551. self.parent.MapWindow.overlays[self.ovlId]['propwin'].SetFocus()
  552. else:
  553. self.parent.MapWindow.overlays[self.ovlId]['propwin'].Show()
  554. def OnResize(self, event):
  555. if self.FindWindowByName('resize').GetValue():
  556. self.parent.MapWindow.SetCursor(self.parent.cursors["cross"])
  557. self.parent.MapWindow.mouse['use'] = 'legend'
  558. self.parent.MapWindow.mouse['box'] = 'box'
  559. self.parent.MapWindow.pen = wx.Pen(colour = 'Black', width = 2, style = wx.SHORT_DASH)
  560. else:
  561. self.parent.MapWindow.SetCursor(self.parent.cursors["default"])
  562. self.parent.MapWindow.mouse['use'] = 'pointer'
  563. def OnCancel(self, event):
  564. """!Cancel dialog"""
  565. if self.name == 'legend' and self.FindWindowByName('resize').GetValue():
  566. self.FindWindowByName('resize').SetValue(False)
  567. self.OnResize(None)
  568. self.parent.dialogs['barscale'] = None
  569. if event and hasattr(self, 'newOverlay'):
  570. self.parent.Map.DeleteOverlay(self.newOverlay)
  571. self.Destroy()
  572. def OnOK(self, event):
  573. """!Button 'OK' pressed"""
  574. # enable or disable overlay
  575. self.parent.Map.GetOverlay(self.ovlId).SetActive(self.chkbox.IsChecked())
  576. # update map
  577. if self.parent.IsPaneShown('3d'):
  578. self.parent.MapWindow.UpdateOverlays()
  579. self.parent.MapWindow.UpdateMap()
  580. # close dialog
  581. self.OnCancel(None)
  582. def GetOptData(self, dcmd, layer, params, propwin):
  583. """!Process decoration layer data"""
  584. # update layer data
  585. if params:
  586. self.parent.MapWindow.overlays[self.ovlId]['params'] = params
  587. if dcmd:
  588. self.parent.MapWindow.overlays[self.ovlId]['cmd'] = dcmd
  589. self.parent.MapWindow.overlays[self.ovlId]['propwin'] = propwin
  590. # change parameters for item in layers list in render.Map
  591. # "Use mouse..." (-m) flag causes GUI freeze and is pointless here, trac #119
  592. try:
  593. self.parent.MapWindow.overlays[self.ovlId]['cmd'].remove('-m')
  594. except ValueError:
  595. pass
  596. self.parent.Map.ChangeOverlay(id = self.ovlId, type = self.name,
  597. command = self.parent.MapWindow.overlays[self.ovlId]['cmd'],
  598. l_active = self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive(),
  599. l_render = False, l_hidden = True)
  600. if self.name == 'legend':
  601. if params and not self.btnOK.IsEnabled():
  602. self.btnOK.Enable()
  603. class TextLayerDialog(wx.Dialog):
  604. """
  605. Controls setting options and displaying/hiding map overlay decorations
  606. """
  607. def __init__(self, parent, ovlId, title, name = 'text',
  608. pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE):
  609. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  610. from wx.lib.expando import ExpandoTextCtrl, EVT_ETC_LAYOUT_NEEDED
  611. self.ovlId = ovlId
  612. self.parent = parent
  613. if self.ovlId in self.parent.MapWindow.textdict.keys():
  614. self.currText = self.parent.MapWindow.textdict[self.ovlId]['text']
  615. self.currFont = self.parent.MapWindow.textdict[self.ovlId]['font']
  616. self.currClr = self.parent.MapWindow.textdict[self.ovlId]['color']
  617. self.currRot = self.parent.MapWindow.textdict[self.ovlId]['rotation']
  618. self.currCoords = self.parent.MapWindow.textdict[self.ovlId]['coords']
  619. self.currBB = self.parent.MapWindow.textdict[self.ovlId]['bbox']
  620. else:
  621. self.currClr = wx.BLACK
  622. self.currText = ''
  623. self.currFont = self.GetFont()
  624. self.currRot = 0.0
  625. self.currCoords = [10, 10]
  626. self.currBB = wx.Rect()
  627. self.sizer = wx.BoxSizer(wx.VERTICAL)
  628. box = wx.GridBagSizer(vgap = 5, hgap = 5)
  629. # show/hide
  630. self.chkbox = wx.CheckBox(parent = self, id = wx.ID_ANY,
  631. label = _('Show text object'))
  632. if self.parent.Map.GetOverlay(self.ovlId) is None:
  633. self.chkbox.SetValue(True)
  634. else:
  635. self.chkbox.SetValue(self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive())
  636. box.Add(item = self.chkbox, span = (1,2),
  637. flag = wx.ALIGN_LEFT|wx.ALL, border = 5,
  638. pos = (0, 0))
  639. # text entry
  640. label = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Enter text:"))
  641. box.Add(item = label,
  642. flag = wx.ALIGN_CENTER_VERTICAL,
  643. pos = (1, 0))
  644. self.textentry = ExpandoTextCtrl(parent = self, id = wx.ID_ANY, value = "", size = (300,-1))
  645. self.textentry.SetFont(self.currFont)
  646. self.textentry.SetForegroundColour(self.currClr)
  647. self.textentry.SetValue(self.currText)
  648. # get rid of unneeded scrollbar when text box first opened
  649. self.textentry.SetClientSize((300,-1))
  650. box.Add(item = self.textentry,
  651. pos = (1, 1))
  652. # rotation
  653. label = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Rotation:"))
  654. box.Add(item = label,
  655. flag = wx.ALIGN_CENTER_VERTICAL,
  656. pos = (2, 0))
  657. self.rotation = wx.SpinCtrl(parent = self, id = wx.ID_ANY, value = "", pos = (30, 50),
  658. size = (75,-1), style = wx.SP_ARROW_KEYS)
  659. self.rotation.SetRange(-360, 360)
  660. self.rotation.SetValue(int(self.currRot))
  661. box.Add(item = self.rotation,
  662. flag = wx.ALIGN_RIGHT,
  663. pos = (2, 1))
  664. # font
  665. fontbtn = wx.Button(parent = self, id = wx.ID_ANY, label = _("Set font"))
  666. box.Add(item = fontbtn,
  667. flag = wx.ALIGN_RIGHT,
  668. pos = (3, 1))
  669. self.sizer.Add(item = box, proportion = 1,
  670. flag = wx.ALL, border = 10)
  671. # note
  672. box = wx.BoxSizer(wx.HORIZONTAL)
  673. label = wx.StaticText(parent = self, id = wx.ID_ANY,
  674. label = _("Drag text with mouse in pointer mode "
  675. "to position.\nDouble-click to change options"))
  676. box.Add(item = label, proportion = 0,
  677. flag = wx.ALIGN_CENTRE | wx.ALL, border = 5)
  678. self.sizer.Add(item = box, proportion = 0,
  679. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER | wx.ALL, border = 5)
  680. line = wx.StaticLine(parent = self, id = wx.ID_ANY,
  681. size = (20,-1), style = wx.LI_HORIZONTAL)
  682. self.sizer.Add(item = line, proportion = 0,
  683. flag = wx.EXPAND | wx.ALIGN_CENTRE | wx.ALL, border = 5)
  684. btnsizer = wx.StdDialogButtonSizer()
  685. btn = wx.Button(parent = self, id = wx.ID_OK)
  686. btn.SetDefault()
  687. btnsizer.AddButton(btn)
  688. btn = wx.Button(parent = self, id = wx.ID_CANCEL)
  689. btnsizer.AddButton(btn)
  690. btnsizer.Realize()
  691. self.sizer.Add(item = btnsizer, proportion = 0,
  692. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  693. self.SetSizer(self.sizer)
  694. self.sizer.Fit(self)
  695. # bindings
  696. self.Bind(EVT_ETC_LAYOUT_NEEDED, self.OnRefit, self.textentry)
  697. self.Bind(wx.EVT_BUTTON, self.OnSelectFont, fontbtn)
  698. self.Bind(wx.EVT_TEXT, self.OnText, self.textentry)
  699. self.Bind(wx.EVT_SPINCTRL, self.OnRotation, self.rotation)
  700. def OnRefit(self, event):
  701. """!Resize text entry to match text"""
  702. self.sizer.Fit(self)
  703. def OnText(self, event):
  704. """!Change text string"""
  705. self.currText = event.GetString()
  706. def OnRotation(self, event):
  707. """!Change rotation"""
  708. self.currRot = event.GetInt()
  709. event.Skip()
  710. def OnSelectFont(self, event):
  711. """!Change font"""
  712. data = wx.FontData()
  713. data.EnableEffects(True)
  714. data.SetColour(self.currClr) # set colour
  715. data.SetInitialFont(self.currFont)
  716. dlg = wx.FontDialog(self, data)
  717. if dlg.ShowModal() == wx.ID_OK:
  718. data = dlg.GetFontData()
  719. self.currFont = data.GetChosenFont()
  720. self.currClr = data.GetColour()
  721. self.textentry.SetFont(self.currFont)
  722. self.textentry.SetForegroundColour(self.currClr)
  723. self.Layout()
  724. dlg.Destroy()
  725. def GetValues(self):
  726. """!Get text properties"""
  727. return { 'text' : self.currText,
  728. 'font' : self.currFont,
  729. 'color' : self.currClr,
  730. 'rotation' : self.currRot,
  731. 'coords' : self.currCoords,
  732. 'active' : self.chkbox.IsChecked() }
  733. class GroupDialog(wx.Dialog):
  734. """!Dialog for creating/editing groups"""
  735. def __init__(self, parent = None, defaultGroup = None,
  736. title = _("Create or edit imagery groups"),
  737. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  738. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  739. style = style, **kwargs)
  740. self.parent = parent
  741. self.defaultGroup = defaultGroup
  742. self.currentGroup = self.defaultGroup
  743. self.groupChanged = False
  744. self.bodySizer = self._createDialogBody()
  745. # buttons
  746. btnOk = wx.Button(parent = self, id = wx.ID_OK)
  747. btnApply = wx.Button(parent = self, id = wx.ID_APPLY)
  748. btnClose = wx.Button(parent = self, id = wx.ID_CANCEL)
  749. btnOk.SetToolTipString(_("Apply changes to selected group and close dialog"))
  750. btnApply.SetToolTipString(_("Apply changes to selected group"))
  751. btnClose.SetToolTipString(_("Close dialog, changes are not applied"))
  752. btnOk.SetDefault()
  753. # sizers & do layout
  754. # btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  755. # btnSizer.Add(item = btnClose, proportion = 0,
  756. # flag = wx.RIGHT | wx.ALIGN_RIGHT | wx.EXPAND, border = 5)
  757. # btnSizer.Add(item = btnApply, proportion = 0,
  758. # flag = wx.LEFT, border = 5)
  759. btnSizer = wx.StdDialogButtonSizer()
  760. btnSizer.AddButton(btnOk)
  761. btnSizer.AddButton(btnApply)
  762. btnSizer.AddButton(btnClose)
  763. btnSizer.Realize()
  764. mainSizer = wx.BoxSizer(wx.VERTICAL)
  765. mainSizer.Add(item = self.bodySizer, proportion = 1,
  766. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 10)
  767. mainSizer.Add(item = wx.StaticLine(parent = self, id = wx.ID_ANY,
  768. style = wx.LI_HORIZONTAL), proportion = 0,
  769. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 10)
  770. mainSizer.Add(item = btnSizer, proportion = 0,
  771. flag = wx.ALL | wx.ALIGN_RIGHT, border = 10)
  772. self.SetSizer(mainSizer)
  773. mainSizer.Fit(self)
  774. btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  775. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  776. btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  777. # set dialog min size
  778. self.SetMinSize(self.GetSize())
  779. def _createDialogBody(self):
  780. bodySizer = wx.BoxSizer(wx.VERTICAL)
  781. # group selection
  782. bodySizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
  783. label = _("Select the group you want to edit or "
  784. "enter name of new group:")),
  785. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP, border = 10)
  786. self.groupSelect = Select(parent = self, type = 'group',
  787. mapsets = [grass.gisenv()['MAPSET']],
  788. size = globalvar.DIALOG_GSELECT_SIZE) # searchpath?
  789. bodySizer.Add(item = self.groupSelect, flag = wx.TOP | wx.EXPAND, border = 5)
  790. bodySizer.AddSpacer(10)
  791. # layers in group
  792. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Layers in selected group:")),
  793. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM, border = 5)
  794. gridSizer = wx.GridBagSizer(vgap = 5, hgap = 5)
  795. gridSizer.AddGrowableCol(0)
  796. self.layerBox = wx.ListBox(parent = self, id = wx.ID_ANY, size = (-1, 150),
  797. style = wx.LB_MULTIPLE | wx.LB_NEEDED_SB)
  798. gridSizer.Add(item = self.layerBox, pos = (0, 0), span = (2, 1), flag = wx.EXPAND)
  799. self.addLayer = wx.Button(self, id = wx.ID_ADD)
  800. self.addLayer.SetToolTipString(_("Select map layers and add them to the list."))
  801. gridSizer.Add(item = self.addLayer, pos = (0, 1))
  802. self.removeLayer = wx.Button(self, id = wx.ID_REMOVE)
  803. self.removeLayer.SetToolTipString(_("Remove selected layer(s) from list."))
  804. gridSizer.Add(item = self.removeLayer, pos = (1, 1))
  805. bodySizer.Add(item = gridSizer, proportion = 1, flag = wx.EXPAND)
  806. self.infoLabel = wx.StaticText(parent = self, id = wx.ID_ANY)
  807. bodySizer.Add(item = self.infoLabel,
  808. flag = wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM, border = 5)
  809. # bindings
  810. self.groupSelect.GetTextCtrl().Bind(wx.EVT_TEXT, self.OnGroupSelected)
  811. self.addLayer.Bind(wx.EVT_BUTTON, self.OnAddLayer)
  812. self.removeLayer.Bind(wx.EVT_BUTTON, self.OnRemoveLayer)
  813. if self.defaultGroup:
  814. self.groupSelect.SetValue(self.defaultGroup)
  815. return bodySizer
  816. def OnAddLayer(self, event):
  817. """!Add new layer to listbox"""
  818. dlg = MapLayersDialog(parent = self, title = _("Add selected map layers into group"),
  819. mapType = 'raster', selectAll = False,
  820. fullyQualified = True, showFullyQualified = False)
  821. if dlg.ShowModal() != wx.ID_OK:
  822. dlg.Destroy()
  823. return
  824. layers = dlg.GetMapLayers()
  825. for layer in layers:
  826. if layer not in self.GetLayers():
  827. self.layerBox.Append(layer)
  828. self.groupChanged = True
  829. def OnRemoveLayer(self, event):
  830. """!Remove layer from listbox"""
  831. while self.layerBox.GetSelections():
  832. sel = self.layerBox.GetSelections()[0]
  833. self.layerBox.Delete(sel)
  834. self.groupChanged = True
  835. def GetLayers(self):
  836. """!Get layers"""
  837. return self.layerBox.GetItems()
  838. def OnGroupSelected(self, event):
  839. """!Text changed in group selector"""
  840. # callAfter must be called to close popup before other actions
  841. wx.CallAfter(self.GroupSelected)
  842. def GroupSelected(self):
  843. """!Group was selected, check if changes were apllied"""
  844. group = self.GetSelectedGroup()
  845. if self.groupChanged:
  846. dlg = wx.MessageDialog(self, message = _("Group <%s> was changed, "
  847. "do you want to apply changes?") % self.currentGroup,
  848. caption = _("Unapplied changes"),
  849. style = wx.YES_NO | wx.ICON_QUESTION | wx.YES_DEFAULT)
  850. if dlg.ShowModal() == wx.ID_YES:
  851. self.ApplyChanges(showResult = True)
  852. dlg.Destroy()
  853. groups = self.GetExistGroups()
  854. if group in groups:
  855. self.ShowGroupLayers(self.GetGroupLayers(group))
  856. self.currentGroup = group
  857. self.groupChanged = False
  858. self.ClearNotification()
  859. def ShowGroupLayers(self, mapList):
  860. """!Show map layers in currently selected group"""
  861. self.layerBox.Set(mapList)
  862. def EditGroup(self, group):
  863. """!Edit selected group"""
  864. layersNew = self.GetLayers()
  865. layersOld = self.GetGroupLayers(group)
  866. add = []
  867. remove = []
  868. for layerNew in layersNew:
  869. if layerNew not in layersOld:
  870. add.append(layerNew)
  871. for layerOld in layersOld:
  872. if layerOld not in layersNew:
  873. remove.append(layerOld)
  874. ret = None
  875. if remove:
  876. ret = RunCommand('i.group',
  877. parent = self,
  878. group = group,
  879. flags = 'r',
  880. input = ','.join(remove))
  881. if add:
  882. ret = RunCommand('i.group',
  883. parent = self,
  884. group = group,
  885. input = ','.join(add))
  886. return ret
  887. def CreateNewGroup(self, group):
  888. """!Create new group"""
  889. layers = self.GetLayers()
  890. ret = RunCommand('i.group',
  891. parent = self,
  892. group = group,
  893. input = layers)
  894. return ret
  895. def GetExistGroups(self):
  896. """!Returns existing groups in current mapset"""
  897. return grass.list_grouped('group')[grass.gisenv()['MAPSET']]
  898. def ShowResult(self, group, returnCode, create):
  899. """!Show if operation was successfull."""
  900. group += '@' + grass.gisenv()['MAPSET']
  901. if returnCode is None:
  902. label = _("No changes to apply in group <%s>.") % group
  903. elif returnCode == 0:
  904. if create:
  905. label = _("Group <%s> was successfully created.") % group
  906. else:
  907. label = _("Group <%s> was successfully changed.") % group
  908. else:
  909. if create:
  910. label = _("Creating of new group <%s> failed.") % group
  911. else:
  912. label = _("Changing of group <%s> failed.") % group
  913. self.infoLabel.SetLabel(label)
  914. wx.FutureCall(4000, self.ClearNotification)
  915. def GetSelectedGroup(self):
  916. """!Return currently selected group (without mapset)"""
  917. return self.groupSelect.GetValue().split('@')[0]
  918. def GetGroupLayers(self, group):
  919. """!Get layers in group"""
  920. res = RunCommand('i.group',
  921. parent = self,
  922. flags = 'g',
  923. group = group,
  924. read = True).strip()
  925. if res.split('\n')[0]:
  926. return res.split('\n')
  927. return []
  928. def ClearNotification(self):
  929. """!Clear notification string"""
  930. self.infoLabel.SetLabel("")
  931. def ApplyChanges(self, showResult):
  932. """!Create or edit group"""
  933. group = self.currentGroup
  934. if not group:
  935. GMessage(parent = self,
  936. message = _("No group selected."))
  937. return False
  938. groups = self.GetExistGroups()
  939. if group in groups:
  940. ret = self.EditGroup(group)
  941. self.ShowResult(group = group, returnCode = ret, create = False)
  942. else:
  943. ret = self.CreateNewGroup(group)
  944. self.ShowResult(group = group, returnCode = ret, create = True)
  945. self.groupChanged = False
  946. return True
  947. def OnApply(self, event):
  948. """!Apply changes"""
  949. self.ApplyChanges(showResult = True)
  950. def OnOk(self, event):
  951. """!Apply changes and close dialog"""
  952. if self.ApplyChanges(showResult = False):
  953. self.OnClose(event)
  954. def OnClose(self, event):
  955. """!Close dialog"""
  956. if not self.IsModal():
  957. self.Destroy()
  958. event.Skip()
  959. class MapLayersDialog(wx.Dialog):
  960. def __init__(self, parent, title, modeler = False,
  961. mapType = None, selectAll = True, fullyQualified = True, showFullyQualified = True,
  962. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  963. """!Dialog for selecting map layers (raster, vector)
  964. @param mapType type of map (if None: raster, vector, 3d raster, if one only: selects it and disables selection)
  965. @param selectAll all/none maps should be selected by default
  966. @param fullyQualified True if dialog should return full map names by default
  967. @param showFullyQualified True to show 'fullyQualified' checkbox, otherwise hide it
  968. """
  969. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  970. style = style, **kwargs)
  971. self.parent = parent # GMFrame or ?
  972. self.mapType = mapType
  973. self.selectAll = selectAll
  974. # dialog body
  975. self.bodySizer = self._createDialogBody()
  976. # update list of layer to be loaded
  977. self.map_layers = [] # list of map layers (full list type/mapset)
  978. self.LoadMapLayers(self.GetLayerType(cmd = True),
  979. self.mapset.GetStringSelection())
  980. self.fullyQualified = wx.CheckBox(parent = self, id = wx.ID_ANY,
  981. label = _("Use fully-qualified map names"))
  982. self.fullyQualified.SetValue(fullyQualified)
  983. self.fullyQualified.Show(showFullyQualified)
  984. self.dseries = None
  985. if modeler:
  986. self.dseries = wx.CheckBox(parent = self, id = wx.ID_ANY,
  987. label = _("Dynamic series (%s)") % 'g.mlist')
  988. self.dseries.SetValue(False)
  989. # buttons
  990. btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
  991. btnOk = wx.Button(parent = self, id = wx.ID_OK)
  992. btnOk.SetDefault()
  993. # sizers & do layout
  994. btnSizer = wx.StdDialogButtonSizer()
  995. btnSizer.AddButton(btnCancel)
  996. btnSizer.AddButton(btnOk)
  997. btnSizer.Realize()
  998. mainSizer = wx.BoxSizer(wx.VERTICAL)
  999. mainSizer.Add(item = self.bodySizer, proportion = 1,
  1000. flag = wx.EXPAND | wx.ALL, border = 5)
  1001. mainSizer.Add(item = self.fullyQualified, proportion = 0,
  1002. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1003. if self.dseries:
  1004. mainSizer.Add(item = self.dseries, proportion = 0,
  1005. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  1006. mainSizer.Add(item = btnSizer, proportion = 0,
  1007. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  1008. self.SetSizer(mainSizer)
  1009. mainSizer.Fit(self)
  1010. # set dialog min size
  1011. self.SetMinSize(self.GetSize())
  1012. def _createDialogBody(self):
  1013. bodySizer = wx.GridBagSizer(vgap = 3, hgap = 3)
  1014. bodySizer.AddGrowableCol(1)
  1015. bodySizer.AddGrowableRow(3)
  1016. # layer type
  1017. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Map type:")),
  1018. flag = wx.ALIGN_CENTER_VERTICAL,
  1019. pos = (0,0))
  1020. self.layerType = wx.Choice(parent = self, id = wx.ID_ANY,
  1021. choices = [_('raster'), _('3D raster'), _('vector')], size = (100,-1))
  1022. if self.mapType:
  1023. self.layerType.SetStringSelection(self.mapType)
  1024. self.layerType.Disable()
  1025. else:
  1026. self.layerType.SetSelection(0)
  1027. bodySizer.Add(item = self.layerType,
  1028. pos = (0,1))
  1029. # select toggle
  1030. self.toggle = wx.CheckBox(parent = self, id = wx.ID_ANY,
  1031. label = _("Select toggle"))
  1032. self.toggle.SetValue(self.selectAll)
  1033. bodySizer.Add(item = self.toggle,
  1034. flag = wx.ALIGN_CENTER_VERTICAL,
  1035. pos = (0,2))
  1036. # mapset filter
  1037. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Mapset:")),
  1038. flag = wx.ALIGN_CENTER_VERTICAL,
  1039. pos = (1,0))
  1040. self.mapset = MapsetSelect(parent = self, searchPath = True)
  1041. self.mapset.SetStringSelection(grass.gisenv()['MAPSET'])
  1042. bodySizer.Add(item = self.mapset,
  1043. pos = (1,1), span = (1, 2))
  1044. # map name filter
  1045. bodySizer.Add(item = wx.StaticText(parent = self, label = _("Pattern:")),
  1046. flag = wx.ALIGN_CENTER_VERTICAL,
  1047. pos = (2,0))
  1048. self.filter = wx.TextCtrl(parent = self, id = wx.ID_ANY,
  1049. value = "",
  1050. size = (250,-1))
  1051. bodySizer.Add(item = self.filter,
  1052. flag = wx.EXPAND,
  1053. pos = (2,1), span = (1, 2))
  1054. # layer list
  1055. bodySizer.Add(item = wx.StaticText(parent = self, label = _("List of maps:")),
  1056. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_TOP,
  1057. pos = (3,0))
  1058. self.layers = wx.CheckListBox(parent = self, id = wx.ID_ANY,
  1059. size = (250, 100),
  1060. choices = [])
  1061. bodySizer.Add(item = self.layers,
  1062. flag = wx.EXPAND,
  1063. pos = (3,1), span = (1, 2))
  1064. # bindings
  1065. self.layerType.Bind(wx.EVT_CHOICE, self.OnChangeParams)
  1066. self.mapset.Bind(wx.EVT_COMBOBOX, self.OnChangeParams)
  1067. self.layers.Bind(wx.EVT_RIGHT_DOWN, self.OnMenu)
  1068. self.filter.Bind(wx.EVT_TEXT, self.OnFilter)
  1069. self.toggle.Bind(wx.EVT_CHECKBOX, self.OnToggle)
  1070. return bodySizer
  1071. def LoadMapLayers(self, type, mapset):
  1072. """!Load list of map layers
  1073. @param type layer type ('raster' or 'vector')
  1074. @param mapset mapset name
  1075. """
  1076. self.map_layers = grass.mlist_grouped(type = type)[mapset]
  1077. self.layers.Set(self.map_layers)
  1078. # check all items by default
  1079. for item in range(self.layers.GetCount()):
  1080. self.layers.Check(item, check = self.selectAll)
  1081. def OnChangeParams(self, event):
  1082. """!Filter parameters changed by user"""
  1083. # update list of layer to be loaded
  1084. self.LoadMapLayers(self.GetLayerType(cmd = True),
  1085. self.mapset.GetStringSelection())
  1086. event.Skip()
  1087. def OnMenu(self, event):
  1088. """!Table description area, context menu"""
  1089. if not hasattr(self, "popupID1"):
  1090. self.popupDataID1 = wx.NewId()
  1091. self.popupDataID2 = wx.NewId()
  1092. self.popupDataID3 = wx.NewId()
  1093. self.Bind(wx.EVT_MENU, self.OnSelectAll, id = self.popupDataID1)
  1094. self.Bind(wx.EVT_MENU, self.OnSelectInvert, id = self.popupDataID2)
  1095. self.Bind(wx.EVT_MENU, self.OnDeselectAll, id = self.popupDataID3)
  1096. # generate popup-menu
  1097. menu = wx.Menu()
  1098. menu.Append(self.popupDataID1, _("Select all"))
  1099. menu.Append(self.popupDataID2, _("Invert selection"))
  1100. menu.Append(self.popupDataID3, _("Deselect all"))
  1101. self.PopupMenu(menu)
  1102. menu.Destroy()
  1103. def OnSelectAll(self, event):
  1104. """!Select all map layer from list"""
  1105. for item in range(self.layers.GetCount()):
  1106. self.layers.Check(item, True)
  1107. def OnSelectInvert(self, event):
  1108. """!Invert current selection"""
  1109. for item in range(self.layers.GetCount()):
  1110. if self.layers.IsChecked(item):
  1111. self.layers.Check(item, False)
  1112. else:
  1113. self.layers.Check(item, True)
  1114. def OnDeselectAll(self, event):
  1115. """!Select all map layer from list"""
  1116. for item in range(self.layers.GetCount()):
  1117. self.layers.Check(item, False)
  1118. def OnFilter(self, event):
  1119. """!Apply filter for map names"""
  1120. if len(event.GetString()) == 0:
  1121. self.layers.Set(self.map_layers)
  1122. return
  1123. list = []
  1124. for layer in self.map_layers:
  1125. try:
  1126. if re.compile('^' + event.GetString()).search(layer):
  1127. list.append(layer)
  1128. except:
  1129. pass
  1130. self.layers.Set(list)
  1131. self.OnSelectAll(None)
  1132. event.Skip()
  1133. def OnToggle(self, event):
  1134. """!Select toggle (check or uncheck all layers)"""
  1135. check = event.Checked()
  1136. for item in range(self.layers.GetCount()):
  1137. self.layers.Check(item, check)
  1138. event.Skip()
  1139. def GetMapLayers(self):
  1140. """!Return list of checked map layers"""
  1141. layerNames = []
  1142. for indx in self.layers.GetSelections():
  1143. # layers.append(self.layers.GetStringSelec(indx))
  1144. pass
  1145. fullyQualified = self.fullyQualified.IsChecked()
  1146. mapset = self.mapset.GetStringSelection()
  1147. for item in range(self.layers.GetCount()):
  1148. if not self.layers.IsChecked(item):
  1149. continue
  1150. if fullyQualified:
  1151. layerNames.append(self.layers.GetString(item) + '@' + mapset)
  1152. else:
  1153. layerNames.append(self.layers.GetString(item))
  1154. return layerNames
  1155. def GetLayerType(self, cmd = False):
  1156. """!Get selected layer type
  1157. @param cmd True for g.mlist
  1158. """
  1159. if not cmd:
  1160. return self.layerType.GetStringSelection()
  1161. sel = self.layerType.GetSelection()
  1162. if sel == 0:
  1163. ltype = 'rast'
  1164. elif sel == 1:
  1165. ltype = 'rast3d'
  1166. else:
  1167. ltype = 'vect'
  1168. return ltype
  1169. def GetDSeries(self):
  1170. """!Used by modeler only
  1171. @return g.mlist command
  1172. """
  1173. if not self.dseries or not self.dseries.IsChecked():
  1174. return ''
  1175. cond = 'map in `g.mlist type=%s ' % self.GetLayerType(cmd = True)
  1176. patt = self.filter.GetValue()
  1177. if patt:
  1178. cond += 'pattern=%s ' % patt
  1179. cond += 'mapset=%s`' % self.mapset.GetStringSelection()
  1180. return cond
  1181. class ImportDialog(wx.Dialog):
  1182. """!Dialog for bulk import of various data (base class)"""
  1183. def __init__(self, parent, itype,
  1184. id = wx.ID_ANY, title = _("Multiple import"),
  1185. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
  1186. self.parent = parent # GMFrame
  1187. self.importType = itype
  1188. self.options = dict() # list of options
  1189. self.commandId = -1 # id of running command
  1190. wx.Dialog.__init__(self, parent, id, title, style = style,
  1191. name = "MultiImportDialog")
  1192. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1193. self.layerBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1194. label = _(" List of %s layers ") % self.importType.upper())
  1195. #
  1196. # list of layers
  1197. #
  1198. columns = [_('Layer id'),
  1199. _('Layer name'),
  1200. _('Name for GRASS map (editable)')]
  1201. if itype == 'ogr':
  1202. columns.insert(2, _('Feature type'))
  1203. self.list = LayersList(parent = self.panel, columns = columns)
  1204. self.list.LoadData()
  1205. self.optionBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1206. label = "%s" % _("Options"))
  1207. cmd = self._getCommand()
  1208. task = gtask.parse_interface(cmd)
  1209. for f in task.get_options()['flags']:
  1210. name = f.get('name', '')
  1211. desc = f.get('label', '')
  1212. if not desc:
  1213. desc = f.get('description', '')
  1214. if not name and not desc:
  1215. continue
  1216. if cmd == 'r.in.gdal' and name not in ('o', 'e', 'l', 'k'):
  1217. continue
  1218. elif cmd == 'r.external' and name not in ('o', 'e', 'r', 'h', 'v'):
  1219. continue
  1220. elif cmd == 'v.in.ogr' and name not in ('c', 'z', 't', 'o', 'r', 'e', 'w'):
  1221. continue
  1222. elif cmd == 'v.external' and name not in ('b'):
  1223. continue
  1224. elif cmd == 'v.in.dxf' and name not in ('e', 't', 'b', 'f', 'i'):
  1225. continue
  1226. self.options[name] = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  1227. label = desc)
  1228. self.overwrite = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  1229. label = _("Allow output files to overwrite existing files"))
  1230. self.overwrite.SetValue(UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'))
  1231. self.add = wx.CheckBox(parent = self.panel, id = wx.ID_ANY)
  1232. #
  1233. # buttons
  1234. #
  1235. # cancel
  1236. self.btn_cancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1237. self.btn_cancel.SetToolTipString(_("Close dialog"))
  1238. self.btn_cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  1239. # run
  1240. self.btn_run = wx.Button(parent = self.panel, id = wx.ID_OK, label = _("&Import"))
  1241. self.btn_run.SetToolTipString(_("Import selected layers"))
  1242. self.btn_run.SetDefault()
  1243. self.btn_run.Enable(False)
  1244. self.btn_run.Bind(wx.EVT_BUTTON, self.OnRun)
  1245. # run command dialog
  1246. self.btn_cmd = wx.Button(parent = self.panel, id = wx.ID_ANY,
  1247. label = _("Command dialog"))
  1248. self.btn_cmd.Bind(wx.EVT_BUTTON, self.OnCmdDialog)
  1249. def doLayout(self):
  1250. """!Do layout"""
  1251. dialogSizer = wx.BoxSizer(wx.VERTICAL)
  1252. # dsn input
  1253. dialogSizer.Add(item = self.dsnInput, proportion = 0,
  1254. flag = wx.EXPAND)
  1255. #
  1256. # list of DXF layers
  1257. #
  1258. layerSizer = wx.StaticBoxSizer(self.layerBox, wx.HORIZONTAL)
  1259. layerSizer.Add(item = self.list, proportion = 1,
  1260. flag = wx.ALL | wx.EXPAND, border = 5)
  1261. dialogSizer.Add(item = layerSizer, proportion = 1,
  1262. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border = 5)
  1263. # options
  1264. optionSizer = wx.StaticBoxSizer(self.optionBox, wx.VERTICAL)
  1265. for key in self.options.keys():
  1266. optionSizer.Add(item = self.options[key], proportion = 0)
  1267. dialogSizer.Add(item = optionSizer, proportion = 0,
  1268. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border = 5)
  1269. dialogSizer.Add(item = self.overwrite, proportion = 0,
  1270. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1271. dialogSizer.Add(item = self.add, proportion = 0,
  1272. flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1273. #
  1274. # buttons
  1275. #
  1276. btnsizer = wx.BoxSizer(orient = wx.HORIZONTAL)
  1277. btnsizer.Add(item = self.btn_cmd, proportion = 0,
  1278. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1279. border = 10)
  1280. btnsizer.Add(item = self.btn_cancel, proportion = 0,
  1281. flag = wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,
  1282. border = 10)
  1283. btnsizer.Add(item = self.btn_run, proportion = 0,
  1284. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1285. border = 10)
  1286. dialogSizer.Add(item = btnsizer, proportion = 0,
  1287. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM | wx.ALIGN_RIGHT,
  1288. border = 10)
  1289. # dialogSizer.SetSizeHints(self.panel)
  1290. self.panel.SetAutoLayout(True)
  1291. self.panel.SetSizer(dialogSizer)
  1292. dialogSizer.Fit(self.panel)
  1293. # auto-layout seems not work here - FIXME
  1294. size = wx.Size(globalvar.DIALOG_GSELECT_SIZE[0] + 225, 550)
  1295. self.SetMinSize(size)
  1296. self.SetSize((size.width, size.height + 100))
  1297. # width = self.GetSize()[0]
  1298. # self.list.SetColumnWidth(col = 1, width = width / 2 - 50)
  1299. self.Layout()
  1300. def _getCommand(self):
  1301. """!Get command"""
  1302. return ''
  1303. def OnCancel(self, event = None):
  1304. """!Close dialog"""
  1305. self.Close()
  1306. def OnRun(self, event):
  1307. """!Import/Link data (each layes as separate vector map)"""
  1308. pass
  1309. def OnCmdDialog(self, event):
  1310. """!Show command dialog"""
  1311. pass
  1312. def AddLayers(self, returncode, cmd = None):
  1313. """!Add imported/linked layers into layer tree"""
  1314. self.commandId += 1
  1315. if not self.add.IsChecked() or returncode != 0:
  1316. return
  1317. maptree = self.parent.curr_page.maptree
  1318. layer, output = self.list.GetLayers()[self.commandId]
  1319. if '@' not in output:
  1320. name = output + '@' + grass.gisenv()['MAPSET']
  1321. else:
  1322. name = output
  1323. # add imported layers into layer tree
  1324. if self.importType == 'gdal':
  1325. cmd = ['d.rast',
  1326. 'map=%s' % name]
  1327. if UserSettings.Get(group = 'cmd', key = 'rasterOverlay', subkey = 'enabled'):
  1328. cmd.append('-o')
  1329. item = maptree.AddLayer(ltype = 'raster',
  1330. lname = name, lchecked = False,
  1331. lcmd = cmd)
  1332. else:
  1333. item = maptree.AddLayer(ltype = 'vector',
  1334. lname = name, lchecked = False,
  1335. lcmd = ['d.vect',
  1336. 'map=%s' % name])
  1337. maptree.mapdisplay.MapWindow.ZoomToMap()
  1338. def OnAbort(self, event):
  1339. """!Abort running import
  1340. @todo not yet implemented
  1341. """
  1342. pass
  1343. class GdalImportDialog(ImportDialog):
  1344. def __init__(self, parent, ogr = False, link = False):
  1345. """!Dialog for bulk import of various raster/vector data
  1346. @param parent parent window
  1347. @param ogr True for OGR (vector) otherwise GDAL (raster)
  1348. @param link True for linking data otherwise importing data
  1349. """
  1350. self.link = link
  1351. self.ogr = ogr
  1352. if ogr:
  1353. ImportDialog.__init__(self, parent, itype = 'ogr')
  1354. if link:
  1355. self.SetTitle(_("Link external vector data"))
  1356. else:
  1357. self.SetTitle(_("Import vector data"))
  1358. else:
  1359. ImportDialog.__init__(self, parent, itype = 'gdal')
  1360. if link:
  1361. self.SetTitle(_("Link external raster data"))
  1362. else:
  1363. self.SetTitle(_("Import raster data"))
  1364. self.dsnInput = GdalSelect(parent = self, panel = self.panel, ogr = ogr)
  1365. if link:
  1366. self.add.SetLabel(_("Add linked layers into layer tree"))
  1367. else:
  1368. self.add.SetLabel(_("Add imported layers into layer tree"))
  1369. self.add.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  1370. if link:
  1371. self.btn_run.SetLabel(_("&Link"))
  1372. self.btn_run.SetToolTipString(_("Link selected layers"))
  1373. if ogr:
  1374. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'v.external')
  1375. else:
  1376. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'r.external')
  1377. else:
  1378. self.btn_run.SetLabel(_("&Import"))
  1379. self.btn_run.SetToolTipString(_("Import selected layers"))
  1380. if ogr:
  1381. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'v.in.ogr')
  1382. else:
  1383. self.btn_cmd.SetToolTipString(_('Open %s dialog') % 'r.in.gdal')
  1384. self.doLayout()
  1385. def OnRun(self, event):
  1386. """!Import/Link data (each layes as separate vector map)"""
  1387. data = self.list.GetLayers()
  1388. # hide dialog
  1389. self.Hide()
  1390. dsn = self.dsnInput.GetDsn()
  1391. ext = self.dsnInput.GetFormatExt()
  1392. for layer, output in data:
  1393. if self.importType == 'ogr':
  1394. if ext and layer.rfind(ext) > -1:
  1395. layer = layer.replace('.' + ext, '')
  1396. if self.link:
  1397. cmd = ['v.external',
  1398. 'dsn=%s' % dsn,
  1399. 'output=%s' % output,
  1400. 'layer=%s' % layer]
  1401. else:
  1402. cmd = ['v.in.ogr',
  1403. 'dsn=%s' % dsn,
  1404. 'layer=%s' % layer,
  1405. 'output=%s' % output]
  1406. else: # gdal
  1407. if self.dsnInput.GetType() == 'dir':
  1408. idsn = os.path.join(dsn, layer)
  1409. else:
  1410. idsn = dsn
  1411. if self.link:
  1412. cmd = ['r.external',
  1413. 'input=%s' % idsn,
  1414. 'output=%s' % output]
  1415. else:
  1416. cmd = ['r.in.gdal',
  1417. 'input=%s' % idsn,
  1418. 'output=%s' % output]
  1419. if self.overwrite.IsChecked():
  1420. cmd.append('--overwrite')
  1421. for key in self.options.keys():
  1422. if self.options[key].IsChecked():
  1423. cmd.append('-%s' % key)
  1424. if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  1425. cmd.append('--overwrite')
  1426. # run in Layer Manager
  1427. self.parent.goutput.RunCmd(cmd, switchPage = True,
  1428. onDone = self.AddLayers)
  1429. self.OnCancel()
  1430. def _getCommand(self):
  1431. """!Get command"""
  1432. if self.link:
  1433. if self.ogr:
  1434. return 'v.external'
  1435. else:
  1436. return 'r.external'
  1437. else:
  1438. if self.ogr:
  1439. return 'v.in.ogr'
  1440. else:
  1441. return 'r.in.gdal'
  1442. return ''
  1443. def OnCmdDialog(self, event):
  1444. """!Show command dialog"""
  1445. name = self._getCommand()
  1446. GUI(parent = self, modal = False).ParseCommand(cmd = [name])
  1447. class GdalOutputDialog(wx.Dialog):
  1448. def __init__(self, parent, id = wx.ID_ANY, ogr = False,
  1449. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, *kwargs):
  1450. """!Dialog for setting output format for rasters/vectors
  1451. @param parent parent window
  1452. @param id window id
  1453. @param ogr True for OGR (vector) otherwise GDAL (raster)
  1454. @param style window style
  1455. @param *kwargs other wx.Dialog's arguments
  1456. """
  1457. self.parent = parent # GMFrame
  1458. self.ogr = ogr
  1459. wx.Dialog.__init__(self, parent, id = id, style = style, *kwargs)
  1460. if self.ogr:
  1461. self.SetTitle(_("Define output format for vector data"))
  1462. else:
  1463. self.SetTitle(_("Define output format for raster data"))
  1464. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1465. # buttons
  1466. self.btnCmd = wx.Button(parent = self.panel, id = wx.ID_ANY,
  1467. label = _("Command dialog"))
  1468. self.btnCmd.Bind(wx.EVT_BUTTON, self.OnCmdDialog)
  1469. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1470. self.btnCancel.SetToolTipString(_("Close dialog"))
  1471. self.btnOk = wx.Button(parent = self.panel, id = wx.ID_OK)
  1472. self.btnOk.SetToolTipString(_("Set external format and close dialog"))
  1473. self.btnOk.SetDefault()
  1474. self.btnOk.Enable(False)
  1475. self.dsnInput = GdalSelect(parent = self, panel = self.panel,
  1476. ogr = ogr,
  1477. exclude = ['file', 'protocol'], dest = True)
  1478. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel)
  1479. self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOk)
  1480. self._layout()
  1481. def _layout(self):
  1482. dialogSizer = wx.BoxSizer(wx.VERTICAL)
  1483. dialogSizer.Add(item = self.dsnInput, proportion = 0,
  1484. flag = wx.EXPAND)
  1485. btnSizer = wx.BoxSizer(orient = wx.HORIZONTAL)
  1486. btnSizer.Add(item = self.btnCmd, proportion = 0,
  1487. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1488. border = 10)
  1489. btnSizer.Add(item = self.btnCancel, proportion = 0,
  1490. flag = wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,
  1491. border = 10)
  1492. btnSizer.Add(item = self.btnOk, proportion = 0,
  1493. flag = wx.RIGHT | wx.ALIGN_CENTER,
  1494. border = 10)
  1495. dialogSizer.Add(item = btnSizer, proportion = 0,
  1496. flag = wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM | wx.TOP | wx.ALIGN_RIGHT,
  1497. border = 10)
  1498. self.panel.SetAutoLayout(True)
  1499. self.panel.SetSizer(dialogSizer)
  1500. dialogSizer.Fit(self.panel)
  1501. size = wx.Size(globalvar.DIALOG_GSELECT_SIZE[0] + 225, self.GetBestSize()[1])
  1502. self.SetMinSize(size)
  1503. self.SetSize((size.width, size.height))
  1504. self.Layout()
  1505. def OnCmdDialog(self, event):
  1506. GUI(parent = self, modal = True).ParseCommand(cmd = ['v.external.out'])
  1507. def OnCancel(self, event):
  1508. self.Destroy()
  1509. def OnOK(self, event):
  1510. if self.dsnInput.GetType() == 'native':
  1511. RunCommand('v.external.out',
  1512. parent = self,
  1513. flags = 'r')
  1514. else:
  1515. dsn = self.dsnInput.GetDsn()
  1516. frmt = self.dsnInput.GetFormat()
  1517. options = self.dsnInput.GetOptions()
  1518. RunCommand('v.external.out',
  1519. parent = self,
  1520. dsn = dsn, format = frmt,
  1521. options = options)
  1522. self.Close()
  1523. class DxfImportDialog(ImportDialog):
  1524. """!Dialog for bulk import of DXF layers"""
  1525. def __init__(self, parent):
  1526. ImportDialog.__init__(self, parent, itype = 'dxf',
  1527. title = _("Import DXF layers"))
  1528. self.dsnInput = filebrowse.FileBrowseButton(parent = self.panel, id = wx.ID_ANY,
  1529. size = globalvar.DIALOG_GSELECT_SIZE, labelText = '',
  1530. dialogTitle = _('Choose DXF file to import'),
  1531. buttonText = _('Browse'),
  1532. startDirectory = os.getcwd(), fileMode = 0,
  1533. changeCallback = self.OnSetDsn,
  1534. fileMask = "DXF File (*.dxf)|*.dxf")
  1535. self.add.SetLabel(_("Add imported layers into layer tree"))
  1536. self.add.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
  1537. self.doLayout()
  1538. def _getCommand(self):
  1539. """!Get command"""
  1540. return 'v.in.dxf'
  1541. def OnRun(self, event):
  1542. """!Import/Link data (each layes as separate vector map)"""
  1543. data = self.list.GetLayers()
  1544. # hide dialog
  1545. self.Hide()
  1546. inputDxf = self.dsnInput.GetValue()
  1547. for layer, output in data:
  1548. cmd = ['v.in.dxf',
  1549. 'input=%s' % inputDxf,
  1550. 'layers=%s' % layer,
  1551. 'output=%s' % output]
  1552. for key in self.options.keys():
  1553. if self.options[key].IsChecked():
  1554. cmd.append('-%s' % key)
  1555. if self.overwrite.IsChecked() or \
  1556. UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
  1557. cmd.append('--overwrite')
  1558. # run in Layer Manager
  1559. self.parent.goutput.RunCmd(cmd, switchPage = True,
  1560. onDone = self.AddLayers)
  1561. self.OnCancel()
  1562. def OnSetDsn(self, event):
  1563. """!Input DXF file defined, update list of layer widget"""
  1564. path = event.GetString()
  1565. if not path:
  1566. return
  1567. data = list()
  1568. ret = RunCommand('v.in.dxf',
  1569. quiet = True,
  1570. parent = self,
  1571. read = True,
  1572. flags = 'l',
  1573. input = path)
  1574. if not ret:
  1575. self.list.LoadData()
  1576. self.btn_run.Enable(False)
  1577. return
  1578. for line in ret.splitlines():
  1579. layerId = line.split(':')[0].split(' ')[1]
  1580. layerName = line.split(':')[1].strip()
  1581. grassName = GetValidLayerName(layerName)
  1582. data.append((layerId, layerName.strip(), grassName.strip()))
  1583. self.list.LoadData(data)
  1584. if len(data) > 0:
  1585. self.btn_run.Enable(True)
  1586. else:
  1587. self.btn_run.Enable(False)
  1588. def OnCmdDialog(self, event):
  1589. """!Show command dialog"""
  1590. GUI(parent = self, modal = True).ParseCommand(cmd = ['v.in.dxf'])
  1591. class LayersList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin,
  1592. listmix.CheckListCtrlMixin, listmix.TextEditMixin):
  1593. """!List of layers to be imported (dxf, shp...)"""
  1594. def __init__(self, parent, columns, log = None):
  1595. self.parent = parent
  1596. wx.ListCtrl.__init__(self, parent, wx.ID_ANY,
  1597. style = wx.LC_REPORT)
  1598. listmix.CheckListCtrlMixin.__init__(self)
  1599. self.log = log
  1600. # setup mixins
  1601. listmix.ListCtrlAutoWidthMixin.__init__(self)
  1602. listmix.TextEditMixin.__init__(self)
  1603. for i in range(len(columns)):
  1604. self.InsertColumn(i, columns[i])
  1605. if len(columns) == 3:
  1606. width = (65, 200)
  1607. else:
  1608. width = (65, 180, 110)
  1609. for i in range(len(width)):
  1610. self.SetColumnWidth(col = i, width = width[i])
  1611. self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnPopupMenu) #wxMSW
  1612. self.Bind(wx.EVT_RIGHT_UP, self.OnPopupMenu) #wxGTK
  1613. def LoadData(self, data = None):
  1614. """!Load data into list"""
  1615. if data is None:
  1616. return
  1617. self.DeleteAllItems()
  1618. for item in data:
  1619. index = self.InsertStringItem(sys.maxint, str(item[0]))
  1620. for i in range(1, len(item)):
  1621. self.SetStringItem(index, i, "%s" % str(item[i]))
  1622. # check by default only on one item
  1623. if len(data) == 1:
  1624. self.CheckItem(index, True)
  1625. def OnPopupMenu(self, event):
  1626. """!Show popup menu"""
  1627. if self.GetItemCount() < 1:
  1628. return
  1629. if not hasattr(self, "popupDataID1"):
  1630. self.popupDataID1 = wx.NewId()
  1631. self.popupDataID2 = wx.NewId()
  1632. self.Bind(wx.EVT_MENU, self.OnSelectAll, id = self.popupDataID1)
  1633. self.Bind(wx.EVT_MENU, self.OnSelectNone, id = self.popupDataID2)
  1634. # generate popup-menu
  1635. menu = wx.Menu()
  1636. menu.Append(self.popupDataID1, _("Select all"))
  1637. menu.Append(self.popupDataID2, _("Deselect all"))
  1638. self.PopupMenu(menu)
  1639. menu.Destroy()
  1640. def OnSelectAll(self, event):
  1641. """!Select all items"""
  1642. item = -1
  1643. while True:
  1644. item = self.GetNextItem(item)
  1645. if item == -1:
  1646. break
  1647. self.CheckItem(item, True)
  1648. event.Skip()
  1649. def OnSelectNone(self, event):
  1650. """!Deselect items"""
  1651. item = -1
  1652. while True:
  1653. item = self.GetNextItem(item, wx.LIST_STATE_SELECTED)
  1654. if item == -1:
  1655. break
  1656. self.CheckItem(item, False)
  1657. event.Skip()
  1658. def OnLeftDown(self, event):
  1659. """!Allow editing only output name
  1660. Code taken from TextEditMixin class.
  1661. """
  1662. x, y = event.GetPosition()
  1663. colLocs = [0]
  1664. loc = 0
  1665. for n in range(self.GetColumnCount()):
  1666. loc = loc + self.GetColumnWidth(n)
  1667. colLocs.append(loc)
  1668. col = bisect(colLocs, x + self.GetScrollPos(wx.HORIZONTAL)) - 1
  1669. if col == self.GetColumnCount() - 1:
  1670. listmix.TextEditMixin.OnLeftDown(self, event)
  1671. else:
  1672. event.Skip()
  1673. def GetLayers(self):
  1674. """!Get list of layers (layer name, output name)"""
  1675. data = []
  1676. item = -1
  1677. while True:
  1678. item = self.GetNextItem(item)
  1679. if item == -1:
  1680. break
  1681. if not self.IsChecked(item):
  1682. continue
  1683. # layer / output name
  1684. data.append((self.GetItem(item, 1).GetText(),
  1685. self.GetItem(item, self.GetColumnCount() - 1).GetText()))
  1686. return data
  1687. class SetOpacityDialog(wx.Dialog):
  1688. """!Set opacity of map layers"""
  1689. def __init__(self, parent, id = wx.ID_ANY, title = _("Set Map Layer Opacity"),
  1690. size = wx.DefaultSize, pos = wx.DefaultPosition,
  1691. style = wx.DEFAULT_DIALOG_STYLE, opacity = 100):
  1692. self.parent = parent # GMFrame
  1693. self.opacity = opacity # current opacity
  1694. super(SetOpacityDialog, self).__init__(parent, id = id, pos = pos,
  1695. size = size, style = style, title = title)
  1696. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1697. sizer = wx.BoxSizer(wx.VERTICAL)
  1698. box = wx.GridBagSizer(vgap = 5, hgap = 5)
  1699. self.value = wx.Slider(panel, id = wx.ID_ANY, value = self.opacity,
  1700. style = wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | \
  1701. wx.SL_TOP | wx.SL_LABELS,
  1702. minValue = 0, maxValue = 100,
  1703. size = (350, -1))
  1704. box.Add(item = self.value,
  1705. flag = wx.ALIGN_CENTRE, pos = (0, 0), span = (1, 2))
  1706. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1707. label = _("transparent")),
  1708. pos = (1, 0))
  1709. box.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
  1710. label = _("opaque")),
  1711. flag = wx.ALIGN_RIGHT,
  1712. pos = (1, 1))
  1713. sizer.Add(item = box, proportion = 0,
  1714. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1715. line = wx.StaticLine(parent = panel, id = wx.ID_ANY,
  1716. style = wx.LI_HORIZONTAL)
  1717. sizer.Add(item = line, proportion = 0,
  1718. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1719. # buttons
  1720. btnsizer = wx.StdDialogButtonSizer()
  1721. btnOK = wx.Button(parent = panel, id = wx.ID_OK)
  1722. btnOK.SetDefault()
  1723. btnsizer.AddButton(btnOK)
  1724. btnCancel = wx.Button(parent = panel, id = wx.ID_CANCEL)
  1725. btnsizer.AddButton(btnCancel)
  1726. btnsizer.Realize()
  1727. sizer.Add(item = btnsizer, proportion = 0,
  1728. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  1729. panel.SetSizer(sizer)
  1730. sizer.Fit(panel)
  1731. self.SetSize(self.GetBestSize())
  1732. self.Layout()
  1733. def GetOpacity(self):
  1734. """!Button 'OK' pressed"""
  1735. # return opacity value
  1736. opacity = float(self.value.GetValue()) / 100
  1737. return opacity
  1738. def GetImageHandlers(image):
  1739. """!Get list of supported image handlers"""
  1740. lext = list()
  1741. ltype = list()
  1742. for h in image.GetHandlers():
  1743. lext.append(h.GetExtension())
  1744. filetype = ''
  1745. if 'png' in lext:
  1746. filetype += "PNG file (*.png)|*.png|"
  1747. ltype.append({ 'type' : wx.BITMAP_TYPE_PNG,
  1748. 'ext' : 'png' })
  1749. filetype += "BMP file (*.bmp)|*.bmp|"
  1750. ltype.append({ 'type' : wx.BITMAP_TYPE_BMP,
  1751. 'ext' : 'bmp' })
  1752. if 'gif' in lext:
  1753. filetype += "GIF file (*.gif)|*.gif|"
  1754. ltype.append({ 'type' : wx.BITMAP_TYPE_GIF,
  1755. 'ext' : 'gif' })
  1756. if 'jpg' in lext:
  1757. filetype += "JPG file (*.jpg)|*.jpg|"
  1758. ltype.append({ 'type' : wx.BITMAP_TYPE_JPEG,
  1759. 'ext' : 'jpg' })
  1760. if 'pcx' in lext:
  1761. filetype += "PCX file (*.pcx)|*.pcx|"
  1762. ltype.append({ 'type' : wx.BITMAP_TYPE_PCX,
  1763. 'ext' : 'pcx' })
  1764. if 'pnm' in lext:
  1765. filetype += "PNM file (*.pnm)|*.pnm|"
  1766. ltype.append({ 'type' : wx.BITMAP_TYPE_PNM,
  1767. 'ext' : 'pnm' })
  1768. if 'tif' in lext:
  1769. filetype += "TIF file (*.tif)|*.tif|"
  1770. ltype.append({ 'type' : wx.BITMAP_TYPE_TIF,
  1771. 'ext' : 'tif' })
  1772. if 'xpm' in lext:
  1773. filetype += "XPM file (*.xpm)|*.xpm"
  1774. ltype.append({ 'type' : wx.BITMAP_TYPE_XPM,
  1775. 'ext' : 'xpm' })
  1776. return filetype, ltype
  1777. class ImageSizeDialog(wx.Dialog):
  1778. """!Set size for saved graphic file"""
  1779. def __init__(self, parent, id = wx.ID_ANY, title = _("Set image size"),
  1780. style = wx.DEFAULT_DIALOG_STYLE, **kwargs):
  1781. self.parent = parent
  1782. wx.Dialog.__init__(self, parent, id = id, style = style, title = title, **kwargs)
  1783. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1784. self.box = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1785. label = ' % s' % _("Image size"))
  1786. size = self.parent.GetWindow().GetClientSize()
  1787. self.width = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1788. style = wx.SP_ARROW_KEYS)
  1789. self.width.SetRange(20, 1e6)
  1790. self.width.SetValue(size.width)
  1791. wx.CallAfter(self.width.SetFocus)
  1792. self.height = wx.SpinCtrl(parent = self.panel, id = wx.ID_ANY,
  1793. style = wx.SP_ARROW_KEYS)
  1794. self.height.SetRange(20, 1e6)
  1795. self.height.SetValue(size.height)
  1796. self.template = wx.Choice(parent = self.panel, id = wx.ID_ANY,
  1797. size = (125, -1),
  1798. choices = [ "",
  1799. "640x480",
  1800. "800x600",
  1801. "1024x768",
  1802. "1280x960",
  1803. "1600x1200",
  1804. "1920x1440" ])
  1805. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  1806. self.btnOK.SetDefault()
  1807. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1808. self.template.Bind(wx.EVT_CHOICE, self.OnTemplate)
  1809. self._layout()
  1810. self.SetSize(self.GetBestSize())
  1811. def _layout(self):
  1812. """!Do layout"""
  1813. sizer = wx.BoxSizer(wx.VERTICAL)
  1814. # body
  1815. box = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  1816. fbox = wx.FlexGridSizer(cols = 2, vgap = 5, hgap = 5)
  1817. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1818. label = _("Width:")),
  1819. flag = wx.ALIGN_CENTER_VERTICAL)
  1820. fbox.Add(item = self.width)
  1821. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1822. label = _("Height:")),
  1823. flag = wx.ALIGN_CENTER_VERTICAL)
  1824. fbox.Add(item = self.height)
  1825. fbox.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  1826. label = _("Template:")),
  1827. flag = wx.ALIGN_CENTER_VERTICAL)
  1828. fbox.Add(item = self.template)
  1829. box.Add(item = fbox, proportion = 1,
  1830. flag = wx.EXPAND | wx.ALL, border = 5)
  1831. sizer.Add(item = box, proportion = 1,
  1832. flag=wx.EXPAND | wx.ALL, border = 3)
  1833. # buttons
  1834. btnsizer = wx.StdDialogButtonSizer()
  1835. btnsizer.AddButton(self.btnOK)
  1836. btnsizer.AddButton(self.btnCancel)
  1837. btnsizer.Realize()
  1838. sizer.Add(item = btnsizer, proportion = 0,
  1839. flag = wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
  1840. self.panel.SetSizer(sizer)
  1841. sizer.Fit(self.panel)
  1842. self.Layout()
  1843. def GetValues(self):
  1844. """!Get width/height values"""
  1845. return self.width.GetValue(), self.height.GetValue()
  1846. def OnTemplate(self, event):
  1847. """!Template selected"""
  1848. sel = event.GetString()
  1849. if not sel:
  1850. width, height = self.parent.GetWindow().GetClientSize()
  1851. else:
  1852. width, height = map(int, sel.split('x'))
  1853. self.width.SetValue(width)
  1854. self.height.SetValue(height)
  1855. class SqlQueryFrame(wx.Frame):
  1856. def __init__(self, parent, id = wx.ID_ANY,
  1857. title = _("GRASS GIS SQL Query Utility"),
  1858. *kwargs):
  1859. """!SQL Query Utility window
  1860. """
  1861. self.parent = parent
  1862. wx.Frame.__init__(self, parent = parent, id = id, title = title, *kwargs)
  1863. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass_sql.ico'), wx.BITMAP_TYPE_ICO))
  1864. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  1865. self.sqlBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  1866. label = _(" SQL statement "))
  1867. self.sql = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
  1868. style = wx.TE_MULTILINE)
  1869. self.btnApply = wx.Button(parent = self.panel, id = wx.ID_APPLY)
  1870. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  1871. self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, self.btnCancel)
  1872. self._layout()
  1873. self.SetMinSize(wx.Size(300, 150))
  1874. self.SetSize(wx.Size(500, 200))
  1875. def _layout(self):
  1876. """!Do layout"""
  1877. sizer = wx.BoxSizer(wx.VERTICAL)
  1878. sqlSizer = wx.StaticBoxSizer(self.sqlBox, wx.HORIZONTAL)
  1879. sqlSizer.Add(item = self.sql, proportion = 1,
  1880. flag = wx.EXPAND)
  1881. btnSizer = wx.StdDialogButtonSizer()
  1882. btnSizer.AddButton(self.btnApply)
  1883. btnSizer.AddButton(self.btnCancel)
  1884. btnSizer.Realize()
  1885. sizer.Add(item = sqlSizer, proportion = 1,
  1886. flag = wx.EXPAND | wx.ALL, border = 5)
  1887. sizer.Add(item = btnSizer, proportion = 0,
  1888. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  1889. self.panel.SetSizer(sizer)
  1890. self.Layout()
  1891. def OnCloseWindow(self, event):
  1892. """!Close window
  1893. """
  1894. self.Close()