gdialogs.py 86 KB

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