toolbars.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. """!
  2. @package toolbar
  3. @brief wxGUI toolbar widgets
  4. Classes:
  5. - AbstractToolbar
  6. - MapToolbar
  7. - GRToolbar
  8. - GCPToolbar
  9. - VDigitToolbar
  10. - ProfileToolbar
  11. - NvizToolbar
  12. (C) 2007-2009 by the GRASS Development Team
  13. This program is free software under the GNU General Public License
  14. (>=v2). Read the file COPYING that comes with GRASS for details.
  15. @author Michael Barton
  16. @author Jachym Cepicky
  17. @author Martin Landa <landa.martin gmail.com>
  18. """
  19. import os
  20. import sys
  21. import platform
  22. from grass.script import core as grass
  23. import wx
  24. import globalvar
  25. import gcmd
  26. import gdialogs
  27. import vdigit
  28. from vdigit import VDigitSettingsDialog as VDigitSettingsDialog
  29. from debug import Debug as Debug
  30. from icon import Icons as Icons
  31. from preferences import globalSettings as UserSettings
  32. gmpath = os.path.join(globalvar.ETCWXDIR, "icons")
  33. sys.path.append(gmpath)
  34. class AbstractToolbar(wx.ToolBar):
  35. """!Abstract toolbar class"""
  36. def __init__(self, parent):
  37. self.parent = parent
  38. wx.ToolBar.__init__(self, parent = self.parent, id = wx.ID_ANY)
  39. self.SetToolBitmapSize(globalvar.toolbarSize)
  40. self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
  41. def OnKeyDown(self, event):
  42. pass
  43. def InitToolbar(self, toolData):
  44. """!Initialize toolbar, add tools to the toolbar
  45. """
  46. for tool in toolData:
  47. self.CreateTool(*tool)
  48. self._data = toolData
  49. def ToolbarData(self):
  50. """!Toolbar data (virtual)"""
  51. return None
  52. def CreateTool(self, tool, label, bitmap, kind,
  53. shortHelp, longHelp, handler):
  54. """!Add tool to the toolbar
  55. @return id of tool
  56. """
  57. bmpDisabled=wx.NullBitmap
  58. if label:
  59. Debug.msg(3, "CreateTool(): tool=%d, label=%s bitmap=%s" % \
  60. (tool, label, bitmap))
  61. toolWin = self.AddLabelTool(tool, label, bitmap,
  62. bmpDisabled, kind,
  63. shortHelp, longHelp)
  64. self.Bind(wx.EVT_TOOL, handler, toolWin)
  65. else: # separator
  66. self.AddSeparator()
  67. return tool
  68. def EnableLongHelp(self, enable=True):
  69. """!Enable/disable long help
  70. @param enable True for enable otherwise disable
  71. """
  72. for tool in self._data:
  73. if tool[0] == '': # separator
  74. continue
  75. if enable:
  76. self.SetToolLongHelp(tool[0], tool[5])
  77. else:
  78. self.SetToolLongHelp(tool[0], "")
  79. def OnTool(self, event):
  80. """!Tool selected"""
  81. if self.parent.toolbars['vdigit']:
  82. # update vdigit toolbar (unselect currently selected tool)
  83. id = self.parent.toolbars['vdigit'].GetAction(type='id')
  84. self.parent.toolbars['vdigit'].ToggleTool(id, False)
  85. if event:
  86. # deselect previously selected tool
  87. id = self.action.get('id', -1)
  88. if id != event.GetId():
  89. self.ToggleTool(self.action['id'], False)
  90. else:
  91. self.ToggleTool(self.action['id'], True)
  92. self.action['id'] = event.GetId()
  93. event.Skip()
  94. else:
  95. # initialize toolbar
  96. self.ToggleTool(self.action['id'], True)
  97. def GetAction(self, type='desc'):
  98. """!Get current action info"""
  99. return self.action.get(type, '')
  100. def SelectDefault(self, event):
  101. """!Select default tool"""
  102. self.ToggleTool(self.defaultAction['id'], True)
  103. self.defaultAction['bind'](event)
  104. self.action = { 'id' : self.defaultAction['id'],
  105. 'desc' : self.defaultAction.get('desc', '') }
  106. def FixSize(self, width):
  107. """!Fix toolbar width on Windows
  108. @todo Determine why combobox causes problems here
  109. """
  110. if platform.system() == 'Windows':
  111. size = self.GetBestSize()
  112. self.SetSize((size[0] + width, size[1]))
  113. class MapToolbar(AbstractToolbar):
  114. """!Map Display toolbar
  115. """
  116. def __init__(self, parent, mapcontent):
  117. """!Map Display constructor
  118. @param parent reference to MapFrame
  119. @param mapcontent reference to render.Map (registred by MapFrame)
  120. """
  121. self.mapcontent = mapcontent # render.Map
  122. AbstractToolbar.__init__(self, parent = parent) # MapFrame
  123. self.InitToolbar(self.ToolbarData())
  124. # optional tools
  125. self.combo = wx.ComboBox(parent=self, id=wx.ID_ANY, value=_('2D view'),
  126. choices=[_('2D view'), _('3D view'), _('Digitize')],
  127. style=wx.CB_READONLY, size=(90, -1))
  128. self.comboid = self.AddControl(self.combo)
  129. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectTool, self.comboid)
  130. # realize the toolbar
  131. self.Realize()
  132. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  133. self.combo.Hide()
  134. self.combo.Show()
  135. # default action
  136. self.action = { 'id' : self.pointer }
  137. self.defaultAction = { 'id' : self.pointer,
  138. 'bind' : self.parent.OnPointer }
  139. self.OnTool(None)
  140. self.EnableTool(self.zoomback, False)
  141. self.FixSize(width = 90)
  142. def ToolbarData(self):
  143. """!Toolbar data"""
  144. self.displaymap = wx.NewId()
  145. self.rendermap = wx.NewId()
  146. self.erase = wx.NewId()
  147. self.pointer = wx.NewId()
  148. self.query = wx.NewId()
  149. self.pan = wx.NewId()
  150. self.zoomin = wx.NewId()
  151. self.zoomout = wx.NewId()
  152. self.zoomback = wx.NewId()
  153. self.zoommenu = wx.NewId()
  154. self.analyze = wx.NewId()
  155. self.dec = wx.NewId()
  156. self.savefile = wx.NewId()
  157. self.printmap = wx.NewId()
  158. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  159. return (
  160. (self.displaymap, "displaymap", Icons["displaymap"].GetBitmap(),
  161. wx.ITEM_NORMAL, Icons["displaymap"].GetLabel(), Icons["displaymap"].GetDesc(),
  162. self.parent.OnDraw),
  163. (self.rendermap, "rendermap", Icons["rendermap"].GetBitmap(),
  164. wx.ITEM_NORMAL, Icons["rendermap"].GetLabel(), Icons["rendermap"].GetDesc(),
  165. self.parent.OnRender),
  166. (self.erase, "erase", Icons["erase"].GetBitmap(),
  167. wx.ITEM_NORMAL, Icons["erase"].GetLabel(), Icons["erase"].GetDesc(),
  168. self.parent.OnErase),
  169. ("", "", "", "", "", "", ""),
  170. (self.pointer, "pointer", Icons["pointer"].GetBitmap(),
  171. wx.ITEM_CHECK, Icons["pointer"].GetLabel(), Icons["pointer"].GetDesc(),
  172. self.parent.OnPointer),
  173. (self.query, "query", Icons["query"].GetBitmap(),
  174. wx.ITEM_CHECK, Icons["query"].GetLabel(), Icons["query"].GetDesc(),
  175. self.parent.OnQuery),
  176. (self.pan, "pan", Icons["pan"].GetBitmap(),
  177. wx.ITEM_CHECK, Icons["pan"].GetLabel(), Icons["pan"].GetDesc(),
  178. self.parent.OnPan),
  179. (self.zoomin, "zoom_in", Icons["zoom_in"].GetBitmap(),
  180. wx.ITEM_CHECK, Icons["zoom_in"].GetLabel(), Icons["zoom_in"].GetDesc(),
  181. self.parent.OnZoomIn),
  182. (self.zoomout, "zoom_out", Icons["zoom_out"].GetBitmap(),
  183. wx.ITEM_CHECK, Icons["zoom_out"].GetLabel(), Icons["zoom_out"].GetDesc(),
  184. self.parent.OnZoomOut),
  185. (self.zoomback, "zoom_back", Icons["zoom_back"].GetBitmap(),
  186. wx.ITEM_NORMAL, Icons["zoom_back"].GetLabel(), Icons["zoom_back"].GetDesc(),
  187. self.parent.OnZoomBack),
  188. (self.zoommenu, "zoommenu", Icons["zoommenu"].GetBitmap(),
  189. wx.ITEM_NORMAL, Icons["zoommenu"].GetLabel(), Icons["zoommenu"].GetDesc(),
  190. self.parent.OnZoomMenu),
  191. ("", "", "", "", "", "", ""),
  192. (self.analyze, "analyze", Icons["analyze"].GetBitmap(),
  193. wx.ITEM_NORMAL, Icons["analyze"].GetLabel(), Icons["analyze"].GetDesc(),
  194. self.parent.OnAnalyze),
  195. ("", "", "", "", "", "", ""),
  196. (self.dec, "overlay", Icons["overlay"].GetBitmap(),
  197. wx.ITEM_NORMAL, Icons["overlay"].GetLabel(), Icons["overlay"].GetDesc(),
  198. self.parent.OnDecoration),
  199. ("", "", "", "", "", "", ""),
  200. (self.savefile, "savefile", Icons["savefile"].GetBitmap(),
  201. wx.ITEM_NORMAL, Icons["savefile"].GetLabel(), Icons["savefile"].GetDesc(),
  202. self.parent.SaveToFile),
  203. (self.printmap, "printmap", Icons["printmap"].GetBitmap(),
  204. wx.ITEM_NORMAL, Icons["printmap"].GetLabel(), Icons["printmap"].GetDesc(),
  205. self.parent.PrintMenu),
  206. ("", "", "", "", "", "", "")
  207. )
  208. def OnSelectTool(self, event):
  209. """!
  210. Select / enable tool available in tools list
  211. """
  212. tool = event.GetSelection()
  213. if tool == 0:
  214. self.ExitToolbars()
  215. self.Enable2D(True)
  216. elif tool == 1 and not self.parent.toolbars['nviz']:
  217. self.ExitToolbars()
  218. self.parent.AddToolbar("nviz")
  219. elif tool == 2 and not self.parent.toolbars['vdigit']:
  220. self.ExitToolbars()
  221. self.parent.AddToolbar("vdigit")
  222. self.parent.MapWindow.SetFocus()
  223. def ExitToolbars(self):
  224. if self.parent.toolbars['vdigit']:
  225. self.parent.toolbars['vdigit'].OnExit()
  226. if self.parent.toolbars['nviz']:
  227. self.parent.toolbars['nviz'].OnExit()
  228. def Enable2D(self, enabled):
  229. """!Enable/Disable 2D display mode specific tools"""
  230. for tool in (self.pointer,
  231. self.query,
  232. self.pan,
  233. self.zoomin,
  234. self.zoomout,
  235. self.zoomback,
  236. self.zoommenu,
  237. self.analyze,
  238. self.dec,
  239. self.savefile,
  240. self.printmap):
  241. self.EnableTool(tool, enabled)
  242. def Enable(self, tool, enable = True):
  243. """!Enable defined tool
  244. @param tool name
  245. @param enable True to enable otherwise disable tool
  246. """
  247. try:
  248. id = getattr(self, tool)
  249. except AttributeError:
  250. return
  251. self.EnableTool(self.zoomback, enable)
  252. class GRToolbar(AbstractToolbar):
  253. """
  254. Georectification toolbar
  255. """
  256. def __init__(self, parent, mapcontent):
  257. """!
  258. Georectification toolbar constructor
  259. @param parent reference to MapFrame
  260. @param mapcontent reference to render.Map (registred by MapFrame)
  261. """
  262. self.mapcontent = map
  263. AbstractToolbar.__init__(self, parent)
  264. self.InitToolbar(self.ToolbarData())
  265. # realize the toolbar
  266. self.Realize()
  267. def ToolbarData(self):
  268. """!Toolbar data"""
  269. self.displaymap = wx.NewId()
  270. self.rendermap = wx.NewId()
  271. self.erase = wx.NewId()
  272. self.gcpset = wx.NewId()
  273. self.pan = wx.NewId()
  274. self.zoomin = wx.NewId()
  275. self.zoomout = wx.NewId()
  276. self.zoomback = wx.NewId()
  277. self.zoomtomap = wx.NewId()
  278. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  279. return (
  280. (self.displaymap, "displaymap", Icons["displaymap"].GetBitmap(),
  281. wx.ITEM_NORMAL, Icons["displaymap"].GetLabel(), Icons["displaymap"].GetDesc(),
  282. self.parent.OnDraw),
  283. (self.rendermap, "rendermap", Icons["rendermap"].GetBitmap(),
  284. wx.ITEM_NORMAL, Icons["rendermap"].GetLabel(), Icons["rendermap"].GetDesc(),
  285. self.parent.OnRender),
  286. (self.erase, "erase", Icons["erase"].GetBitmap(),
  287. wx.ITEM_NORMAL, Icons["erase"].GetLabel(), Icons["erase"].GetDesc(),
  288. self.parent.OnErase),
  289. ("", "", "", "", "", "", ""),
  290. (self.gcpset, "grGcpSet", Icons["grGcpSet"].GetBitmap(),
  291. wx.ITEM_RADIO, Icons["grGcpSet"].GetLabel(), Icons["grGcpSet"].GetDesc(),
  292. self.parent.OnPointer),
  293. (self.pan, "pan", Icons["pan"].GetBitmap(),
  294. wx.ITEM_RADIO, Icons["pan"].GetLabel(), Icons["pan"].GetDesc(),
  295. self.parent.OnPan),
  296. (self.zoomin, "zoom_in", Icons["zoom_in"].GetBitmap(),
  297. wx.ITEM_RADIO, Icons["zoom_in"].GetLabel(), Icons["zoom_in"].GetDesc(),
  298. self.parent.OnZoomIn),
  299. (self.zoomout, "zoom_out", Icons["zoom_out"].GetBitmap(),
  300. wx.ITEM_RADIO, Icons["zoom_out"].GetLabel(), Icons["zoom_out"].GetDesc(),
  301. self.parent.OnZoomOut),
  302. (self.zoomback, "zoom_back", Icons["zoom_back"].GetBitmap(),
  303. wx.ITEM_NORMAL, Icons["zoom_back"].GetLabel(), Icons["zoom_back"].GetDesc(),
  304. self.parent.OnZoomBack),
  305. (self.zoomtomap, "zoomtomap", Icons["zoommenu"].GetBitmap(),
  306. wx.ITEM_NORMAL, _("Zoom to map"), _("Zoom to displayed map"),
  307. self.OnZoomMap),
  308. ("", "", "", "", "", "", ""),
  309. )
  310. def OnZoomMap(self, event):
  311. """!Zoom to selected map"""
  312. self.parent.MapWindow.ZoomToMap(layers = self.mapcontent.GetListOfLayers())
  313. event.Skip()
  314. class GCPToolbar(AbstractToolbar):
  315. """!
  316. Toolbar for managing ground control points during georectification
  317. @param parent reference to GCP widget
  318. """
  319. def __init__(self, parent):
  320. AbstractToolbar.__init__(self, parent)
  321. self.InitToolbar(self.ToolbarData())
  322. # realize the toolbar
  323. self.Realize()
  324. def ToolbarData(self):
  325. self.gcpSave = wx.NewId()
  326. self.gcpAdd = wx.NewId()
  327. self.gcpDelete = wx.NewId()
  328. self.gcpClear = wx.NewId()
  329. self.gcpReload = wx.NewId()
  330. self.rms = wx.NewId()
  331. self.georect = wx.NewId()
  332. self.settings = wx.NewId()
  333. self.quit = wx.NewId()
  334. return (
  335. (self.gcpSave, 'grGcpSave', Icons["grGcpSave"].GetBitmap(),
  336. wx.ITEM_NORMAL, Icons["grGcpSave"].GetLabel(), Icons["grGcpSave"].GetDesc(),
  337. self.parent.SaveGCPs),
  338. (self.gcpAdd, 'grGrGcpAdd', Icons["grGcpAdd"].GetBitmap(),
  339. wx.ITEM_NORMAL, Icons["grGcpAdd"].GetLabel(), Icons["grGcpAdd"].GetDesc(),
  340. self.parent.AddGCP),
  341. (self.gcpDelete, 'grGrGcpDelete', Icons["grGcpDelete"].GetBitmap(),
  342. wx.ITEM_NORMAL, Icons["grGcpDelete"].GetLabel(), Icons["grGcpDelete"].GetDesc(),
  343. self.parent.DeleteGCP),
  344. (self.gcpClear, 'grGcpClear', Icons["grGcpClear"].GetBitmap(),
  345. wx.ITEM_NORMAL, Icons["grGcpClear"].GetLabel(), Icons["grGcpClear"].GetDesc(),
  346. self.parent.ClearGCP),
  347. (self.gcpReload, 'grGcpReload', Icons["grGcpReload"].GetBitmap(),
  348. wx.ITEM_NORMAL, Icons["grGcpReload"].GetLabel(), Icons["grGcpReload"].GetDesc(),
  349. self.parent.ReloadGCPs),
  350. ("", "", "", "", "", "", ""),
  351. (self.rms, 'grGcpRms', Icons["grGcpRms"].GetBitmap(),
  352. wx.ITEM_NORMAL, Icons["grGcpRms"].GetLabel(), Icons["grGcpRms"].GetDesc(),
  353. self.parent.OnRMS),
  354. (self.georect, 'grGeorect', Icons["grGeorect"].GetBitmap(),
  355. wx.ITEM_NORMAL, Icons["grGeorect"].GetLabel(), Icons["grGeorect"].GetDesc(),
  356. self.parent.OnGeorect),
  357. ("", "", "", "", "", "", ""),
  358. (self.settings, 'grSettings', Icons["grSettings"].GetBitmap(),
  359. wx.ITEM_NORMAL, Icons["grSettings"].GetLabel(), Icons["grSettings"].GetDesc(),
  360. self.parent.OnSettings),
  361. (self.quit, 'grGcpQuit', Icons["grGcpQuit"].GetBitmap(),
  362. wx.ITEM_NORMAL, Icons["grGcpQuit"].GetLabel(), Icons["grGcpQuit"].GetDesc(),
  363. self.parent.OnQuit)
  364. )
  365. class VDigitToolbar(AbstractToolbar):
  366. """
  367. Toolbar for digitization
  368. """
  369. def __init__(self, parent, mapcontent, layerTree=None, log=None):
  370. self.mapcontent = mapcontent # Map class instance
  371. self.layerTree = layerTree # reference to layer tree associated to map display
  372. self.log = log # log area
  373. AbstractToolbar.__init__(self, parent)
  374. # currently selected map layer for editing (reference to MapLayer instance)
  375. self.mapLayer = None
  376. # list of vector layers from Layer Manager (only in the current mapset)
  377. self.layers = []
  378. self.comboid = None
  379. # only one dialog can be open
  380. self.settingsDialog = None
  381. # create toolbars (two rows optionally)
  382. self.Bind(wx.EVT_TOOL, self.OnTool)
  383. self.InitToolbar(self.ToolbarData())
  384. # default action (digitize new point, line, etc.)
  385. self.action = { 'desc' : 'addLine',
  386. 'type' : 'point',
  387. 'id' : self.addPoint }
  388. # list of available vector maps
  389. self.UpdateListOfLayers(updateTool=True)
  390. # realize toolbar
  391. self.Realize()
  392. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  393. self.combo.Hide()
  394. self.combo.Show()
  395. # disable undo/redo
  396. self.EnableTool(self.undo, False)
  397. # toogle to pointer by default
  398. self.OnTool(None)
  399. self.FixSize(width = 105)
  400. def ToolbarData(self):
  401. """!
  402. Toolbar data
  403. """
  404. data = []
  405. self.addPoint = wx.NewId()
  406. self.addLine = wx.NewId()
  407. self.addBoundary = wx.NewId()
  408. self.addCentroid = wx.NewId()
  409. self.moveVertex = wx.NewId()
  410. self.addVertex = wx.NewId()
  411. self.removeVertex = wx.NewId()
  412. self.splitLine = wx.NewId()
  413. self.editLine = wx.NewId()
  414. self.moveLine = wx.NewId()
  415. self.deleteLine = wx.NewId()
  416. self.additionalTools = wx.NewId()
  417. self.displayCats = wx.NewId()
  418. self.displayAttr = wx.NewId()
  419. self.copyCats = wx.NewId()
  420. self.undo = wx.NewId()
  421. self.settings = wx.NewId()
  422. self.exit = wx.NewId()
  423. data = [("", "", "", "", "", "", ""),
  424. (self.addPoint, "digAddPoint", Icons["digAddPoint"].GetBitmap(),
  425. wx.ITEM_CHECK, Icons["digAddPoint"].GetLabel(), Icons["digAddPoint"].GetDesc(),
  426. self.OnAddPoint),
  427. (self.addLine, "digAddLine", Icons["digAddLine"].GetBitmap(),
  428. wx.ITEM_CHECK, Icons["digAddLine"].GetLabel(), Icons["digAddLine"].GetDesc(),
  429. self.OnAddLine),
  430. (self.addBoundary, "digAddBoundary", Icons["digAddBoundary"].GetBitmap(),
  431. wx.ITEM_CHECK, Icons["digAddBoundary"].GetLabel(), Icons["digAddBoundary"].GetDesc(),
  432. self.OnAddBoundary),
  433. (self.addCentroid, "digAddCentroid", Icons["digAddCentroid"].GetBitmap(),
  434. wx.ITEM_CHECK, Icons["digAddCentroid"].GetLabel(), Icons["digAddCentroid"].GetDesc(),
  435. self.OnAddCentroid),
  436. (self.moveVertex, "digMoveVertex", Icons["digMoveVertex"].GetBitmap(),
  437. wx.ITEM_CHECK, Icons["digMoveVertex"].GetLabel(), Icons["digMoveVertex"].GetDesc(),
  438. self.OnMoveVertex),
  439. (self.addVertex, "digAddVertex", Icons["digAddVertex"].GetBitmap(),
  440. wx.ITEM_CHECK, Icons["digAddVertex"].GetLabel(), Icons["digAddVertex"].GetDesc(),
  441. self.OnAddVertex),
  442. (self.removeVertex, "digRemoveVertex", Icons["digRemoveVertex"].GetBitmap(),
  443. wx.ITEM_CHECK, Icons["digRemoveVertex"].GetLabel(), Icons["digRemoveVertex"].GetDesc(),
  444. self.OnRemoveVertex),
  445. (self.splitLine, "digSplitLine", Icons["digSplitLine"].GetBitmap(),
  446. wx.ITEM_CHECK, Icons["digSplitLine"].GetLabel(), Icons["digSplitLine"].GetDesc(),
  447. self.OnSplitLine),
  448. (self.editLine, "digEditLine", Icons["digEditLine"].GetBitmap(),
  449. wx.ITEM_CHECK, Icons["digEditLine"].GetLabel(), Icons["digEditLine"].GetDesc(),
  450. self.OnEditLine),
  451. (self.moveLine, "digMoveLine", Icons["digMoveLine"].GetBitmap(),
  452. wx.ITEM_CHECK, Icons["digMoveLine"].GetLabel(), Icons["digMoveLine"].GetDesc(),
  453. self.OnMoveLine),
  454. (self.deleteLine, "digDeleteLine", Icons["digDeleteLine"].GetBitmap(),
  455. wx.ITEM_CHECK, Icons["digDeleteLine"].GetLabel(), Icons["digDeleteLine"].GetDesc(),
  456. self.OnDeleteLine),
  457. (self.displayCats, "digDispCats", Icons["digDispCats"].GetBitmap(),
  458. wx.ITEM_CHECK, Icons["digDispCats"].GetLabel(), Icons["digDispCats"].GetDesc(),
  459. self.OnDisplayCats),
  460. (self.copyCats, "digCopyCats", Icons["digCopyCats"].GetBitmap(),
  461. wx.ITEM_CHECK, Icons["digCopyCats"].GetLabel(), Icons["digCopyCats"].GetDesc(),
  462. self.OnCopyCA),
  463. (self.displayAttr, "digDispAttr", Icons["digDispAttr"].GetBitmap(),
  464. wx.ITEM_CHECK, Icons["digDispAttr"].GetLabel(), Icons["digDispAttr"].GetDesc(),
  465. self.OnDisplayAttr),
  466. (self.additionalTools, "digAdditionalTools", Icons["digAdditionalTools"].GetBitmap(),
  467. wx.ITEM_CHECK, Icons["digAdditionalTools"].GetLabel(),
  468. Icons["digAdditionalTools"].GetDesc(),
  469. self.OnAdditionalToolMenu),
  470. ("", "", "", "", "", "", ""),
  471. (self.undo, "digUndo", Icons["digUndo"].GetBitmap(),
  472. wx.ITEM_NORMAL, Icons["digUndo"].GetLabel(), Icons["digUndo"].GetDesc(),
  473. self.OnUndo),
  474. # data.append((self.undo, "digRedo", Icons["digRedo"].GetBitmap(),
  475. # wx.ITEM_NORMAL, Icons["digRedo"].GetLabel(), Icons["digRedo"].GetDesc(),
  476. # self.OnRedo))
  477. (self.settings, "digSettings", Icons["digSettings"].GetBitmap(),
  478. wx.ITEM_NORMAL, Icons["digSettings"].GetLabel(), Icons["digSettings"].GetDesc(),
  479. self.OnSettings),
  480. (self.exit, "digExit", Icons["quit"].GetBitmap(),
  481. wx.ITEM_NORMAL, Icons["digExit"].GetLabel(), Icons["digExit"].GetDesc(),
  482. self.OnExit)]
  483. return data
  484. def OnTool(self, event):
  485. """!Tool selected -> disable selected tool in map toolbar"""
  486. # update map toolbar (unselect currently selected tool)
  487. id = self.parent.toolbars['map'].GetAction(type='id')
  488. self.parent.toolbars['map'].ToggleTool(id, False)
  489. # set cursor
  490. cursor = self.parent.cursors["cross"]
  491. self.parent.MapWindow.SetCursor(cursor)
  492. # pointer
  493. self.parent.OnPointer(None)
  494. if event:
  495. # deselect previously selected tool
  496. id = self.action.get('id', -1)
  497. if id != event.GetId():
  498. self.ToggleTool(self.action['id'], False)
  499. else:
  500. self.ToggleTool(self.action['id'], True)
  501. self.action['id'] = event.GetId()
  502. event.Skip()
  503. self.ToggleTool(self.action['id'], True)
  504. # clear tmp canvas
  505. if self.action['id'] != id:
  506. self.parent.MapWindow.ClearLines(pdc=self.parent.MapWindow.pdcTmp)
  507. if self.parent.digit and \
  508. len(self.parent.digit.driver.GetSelected()) > 0:
  509. # cancel action
  510. self.parent.MapWindow.OnMiddleDown(None)
  511. # set focus
  512. self.parent.MapWindow.SetFocus()
  513. def OnAddPoint(self, event):
  514. """!Add point to the vector map Laier"""
  515. Debug.msg (2, "VDigitToolbar.OnAddPoint()")
  516. self.action = { 'desc' : "addLine",
  517. 'type' : "point",
  518. 'id' : self.addPoint }
  519. self.parent.MapWindow.mouse['box'] = 'point'
  520. def OnAddLine(self, event):
  521. """!Add line to the vector map layer"""
  522. Debug.msg (2, "VDigitToolbar.OnAddLine()")
  523. self.action = { 'desc' : "addLine",
  524. 'type' : "line",
  525. 'id' : self.addLine }
  526. self.parent.MapWindow.mouse['box'] = 'line'
  527. ### self.parent.MapWindow.polycoords = [] # reset temp line
  528. def OnAddBoundary(self, event):
  529. """!Add boundary to the vector map layer"""
  530. Debug.msg (2, "VDigitToolbar.OnAddBoundary()")
  531. if self.action['desc'] != 'addLine' or \
  532. self.action['type'] != 'boundary':
  533. self.parent.MapWindow.polycoords = [] # reset temp line
  534. self.action = { 'desc' : "addLine",
  535. 'type' : "boundary",
  536. 'id' : self.addBoundary }
  537. self.parent.MapWindow.mouse['box'] = 'line'
  538. def OnAddCentroid(self, event):
  539. """!Add centroid to the vector map layer"""
  540. Debug.msg (2, "VDigitToolbar.OnAddCentroid()")
  541. self.action = { 'desc' : "addLine",
  542. 'type' : "centroid",
  543. 'id' : self.addCentroid }
  544. self.parent.MapWindow.mouse['box'] = 'point'
  545. def OnExit (self, event=None):
  546. """!Quit digitization tool"""
  547. # stop editing of the currently selected map layer
  548. if self.mapLayer:
  549. self.StopEditing()
  550. # close dialogs if still open
  551. if self.settingsDialog:
  552. self.settingsDialog.OnCancel(None)
  553. # set default mouse settings
  554. self.parent.MapWindow.mouse['use'] = "pointer"
  555. self.parent.MapWindow.mouse['box'] = "point"
  556. self.parent.MapWindow.polycoords = []
  557. # disable the toolbar
  558. self.parent.RemoveToolbar("vdigit")
  559. def OnMoveVertex(self, event):
  560. """!Move line vertex"""
  561. Debug.msg(2, "Digittoolbar.OnMoveVertex():")
  562. self.action = { 'desc' : "moveVertex",
  563. 'id' : self.moveVertex }
  564. self.parent.MapWindow.mouse['box'] = 'point'
  565. def OnAddVertex(self, event):
  566. """!Add line vertex"""
  567. Debug.msg(2, "Digittoolbar.OnAddVertex():")
  568. self.action = { 'desc' : "addVertex",
  569. 'id' : self.addVertex }
  570. self.parent.MapWindow.mouse['box'] = 'point'
  571. def OnRemoveVertex(self, event):
  572. """!Remove line vertex"""
  573. Debug.msg(2, "Digittoolbar.OnRemoveVertex():")
  574. self.action = { 'desc' : "removeVertex",
  575. 'id' : self.removeVertex }
  576. self.parent.MapWindow.mouse['box'] = 'point'
  577. def OnSplitLine(self, event):
  578. """!Split line"""
  579. Debug.msg(2, "Digittoolbar.OnSplitLine():")
  580. self.action = { 'desc' : "splitLine",
  581. 'id' : self.splitLine }
  582. self.parent.MapWindow.mouse['box'] = 'point'
  583. def OnEditLine(self, event):
  584. """!Edit line"""
  585. Debug.msg(2, "Digittoolbar.OnEditLine():")
  586. self.action = { 'desc' : "editLine",
  587. 'id' : self.editLine }
  588. self.parent.MapWindow.mouse['box'] = 'line'
  589. def OnMoveLine(self, event):
  590. """!Move line"""
  591. Debug.msg(2, "Digittoolbar.OnMoveLine():")
  592. self.action = { 'desc' : "moveLine",
  593. 'id' : self.moveLine }
  594. self.parent.MapWindow.mouse['box'] = 'box'
  595. def OnDeleteLine(self, event):
  596. """!Delete line"""
  597. Debug.msg(2, "Digittoolbar.OnDeleteLine():")
  598. self.action = { 'desc' : "deleteLine",
  599. 'id' : self.deleteLine }
  600. self.parent.MapWindow.mouse['box'] = 'box'
  601. def OnDisplayCats(self, event):
  602. """!Display/update categories"""
  603. Debug.msg(2, "Digittoolbar.OnDisplayCats():")
  604. self.action = { 'desc' : "displayCats",
  605. 'id' : self.displayCats }
  606. self.parent.MapWindow.mouse['box'] = 'point'
  607. def OnDisplayAttr(self, event):
  608. """!Display/update attributes"""
  609. Debug.msg(2, "Digittoolbar.OnDisplayAttr():")
  610. self.action = { 'desc' : "displayAttrs",
  611. 'id' : self.displayAttr }
  612. self.parent.MapWindow.mouse['box'] = 'point'
  613. def OnCopyCA(self, event):
  614. """!Copy categories/attributes menu"""
  615. point = wx.GetMousePosition()
  616. toolMenu = wx.Menu()
  617. # Add items to the menu
  618. cats = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  619. text=_('Copy categories'),
  620. kind=wx.ITEM_CHECK)
  621. toolMenu.AppendItem(cats)
  622. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnCopyCats, cats)
  623. if self.action['desc'] == "copyCats":
  624. cats.Check(True)
  625. attrb = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  626. text=_('Duplicate attributes'),
  627. kind=wx.ITEM_CHECK)
  628. toolMenu.AppendItem(attrb)
  629. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnCopyAttrb, attrb)
  630. if self.action['desc'] == "copyAttrs":
  631. attrb.Check(True)
  632. # Popup the menu. If an item is selected then its handler
  633. # will be called before PopupMenu returns.
  634. self.parent.MapWindow.PopupMenu(toolMenu)
  635. toolMenu.Destroy()
  636. if self.action['desc'] == "addPoint":
  637. self.ToggleTool(self.copyCats, False)
  638. def OnCopyCats(self, event):
  639. """!Copy categories"""
  640. if self.action['desc'] == 'copyCats': # select previous action
  641. self.ToggleTool(self.addPoint, True)
  642. self.ToggleTool(self.copyCats, False)
  643. self.OnAddPoint(event)
  644. return
  645. Debug.msg(2, "Digittoolbar.OnCopyCats():")
  646. self.action = { 'desc' : "copyCats",
  647. 'id' : self.copyCats }
  648. self.parent.MapWindow.mouse['box'] = 'point'
  649. def OnCopyAttrb(self, event):
  650. if self.action['desc'] == 'copyAttrs': # select previous action
  651. self.ToggleTool(self.addPoint, True)
  652. self.ToggleTool(self.copyCats, False)
  653. self.OnAddPoint(event)
  654. return
  655. Debug.msg(2, "Digittoolbar.OnCopyAttrb():")
  656. self.action = { 'desc' : "copyAttrs",
  657. 'id' : self.copyCats }
  658. self.parent.MapWindow.mouse['box'] = 'point'
  659. def OnUndo(self, event):
  660. """!Undo previous changes"""
  661. self.parent.digit.Undo()
  662. event.Skip()
  663. def EnableUndo(self, enable=True):
  664. """!Enable 'Undo' in toolbar
  665. @param enable False for disable
  666. """
  667. if enable:
  668. if self.GetToolEnabled(self.undo) is False:
  669. self.EnableTool(self.undo, True)
  670. else:
  671. if self.GetToolEnabled(self.undo) is True:
  672. self.EnableTool(self.undo, False)
  673. def OnSettings(self, event):
  674. """!Show settings dialog"""
  675. if self.parent.digit is None:
  676. reload(vdigit)
  677. from vdigit import VDigit as VDigit
  678. try:
  679. self.parent.digit = VDigit(mapwindow=self.parent.MapWindow)
  680. except SystemExit:
  681. self.parent.digit = None
  682. if not self.settingsDialog:
  683. self.settingsDialog = VDigitSettingsDialog(parent=self.parent, title=_("Digitization settings"),
  684. style=wx.DEFAULT_DIALOG_STYLE)
  685. self.settingsDialog.Show()
  686. def OnAdditionalToolMenu(self, event):
  687. """!Menu for additional tools"""
  688. point = wx.GetMousePosition()
  689. toolMenu = wx.Menu()
  690. # Add items to the menu
  691. copy = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  692. text=_('Copy features from (background) vector map'),
  693. kind=wx.ITEM_CHECK)
  694. toolMenu.AppendItem(copy)
  695. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnCopy, copy)
  696. if self.action['desc'] == "copyLine":
  697. copy.Check(True)
  698. flip = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  699. text=_('Flip selected lines/boundaries'),
  700. kind=wx.ITEM_CHECK)
  701. toolMenu.AppendItem(flip)
  702. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnFlip, flip)
  703. if self.action['desc'] == "flipLine":
  704. flip.Check(True)
  705. merge = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  706. text=_('Merge selected lines/boundaries'),
  707. kind=wx.ITEM_CHECK)
  708. toolMenu.AppendItem(merge)
  709. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnMerge, merge)
  710. if self.action['desc'] == "mergeLine":
  711. merge.Check(True)
  712. breakL = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  713. text=_('Break selected lines/boundaries at intersection'),
  714. kind=wx.ITEM_CHECK)
  715. toolMenu.AppendItem(breakL)
  716. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnBreak, breakL)
  717. if self.action['desc'] == "breakLine":
  718. breakL.Check(True)
  719. snap = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  720. text=_('Snap selected lines/boundaries (only to nodes)'),
  721. kind=wx.ITEM_CHECK)
  722. toolMenu.AppendItem(snap)
  723. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnSnap, snap)
  724. if self.action['desc'] == "snapLine":
  725. snap.Check(True)
  726. connect = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  727. text=_('Connect selected lines/boundaries'),
  728. kind=wx.ITEM_CHECK)
  729. toolMenu.AppendItem(connect)
  730. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnConnect, connect)
  731. if self.action['desc'] == "connectLine":
  732. connect.Check(True)
  733. query = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  734. text=_('Query features'),
  735. kind=wx.ITEM_CHECK)
  736. toolMenu.AppendItem(query)
  737. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnQuery, query)
  738. if self.action['desc'] == "queryLine":
  739. query.Check(True)
  740. zbulk = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  741. text=_('Z bulk-labeling of 3D lines'),
  742. kind=wx.ITEM_CHECK)
  743. toolMenu.AppendItem(zbulk)
  744. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnZBulk, zbulk)
  745. if self.action['desc'] == "zbulkLine":
  746. zbulk.Check(True)
  747. typeconv = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY,
  748. text=_('Feature type conversion'),
  749. kind=wx.ITEM_CHECK)
  750. toolMenu.AppendItem(typeconv)
  751. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnTypeConversion, typeconv)
  752. if self.action['desc'] == "typeConv":
  753. typeconv.Check(True)
  754. # Popup the menu. If an item is selected then its handler
  755. # will be called before PopupMenu returns.
  756. self.parent.MapWindow.PopupMenu(toolMenu)
  757. toolMenu.Destroy()
  758. if self.action['desc'] == 'addPoint':
  759. self.ToggleTool(self.additionalTools, False)
  760. def OnCopy(self, event):
  761. """!Copy selected features from (background) vector map"""
  762. if self.action['desc'] == 'copyLine': # select previous action
  763. self.ToggleTool(self.addPoint, True)
  764. self.ToggleTool(self.additionalTools, False)
  765. self.OnAddPoint(event)
  766. return
  767. Debug.msg(2, "Digittoolbar.OnCopy():")
  768. self.action = { 'desc' : "copyLine",
  769. 'id' : self.additionalTools }
  770. self.parent.MapWindow.mouse['box'] = 'box'
  771. def OnFlip(self, event):
  772. """!Flip selected lines/boundaries"""
  773. if self.action['desc'] == 'flipLine': # select previous action
  774. self.ToggleTool(self.addPoint, True)
  775. self.ToggleTool(self.additionalTools, False)
  776. self.OnAddPoint(event)
  777. return
  778. Debug.msg(2, "Digittoolbar.OnFlip():")
  779. self.action = { 'desc' : "flipLine",
  780. 'id' : self.additionalTools }
  781. self.parent.MapWindow.mouse['box'] = 'box'
  782. def OnMerge(self, event):
  783. """!Merge selected lines/boundaries"""
  784. if self.action['desc'] == 'mergeLine': # select previous action
  785. self.ToggleTool(self.addPoint, True)
  786. self.ToggleTool(self.additionalTools, False)
  787. self.OnAddPoint(event)
  788. return
  789. Debug.msg(2, "Digittoolbar.OnMerge():")
  790. self.action = { 'desc' : "mergeLine",
  791. 'id' : self.additionalTools }
  792. self.parent.MapWindow.mouse['box'] = 'box'
  793. def OnBreak(self, event):
  794. """!Break selected lines/boundaries"""
  795. if self.action['desc'] == 'breakLine': # select previous action
  796. self.ToggleTool(self.addPoint, True)
  797. self.ToggleTool(self.additionalTools, False)
  798. self.OnAddPoint(event)
  799. return
  800. Debug.msg(2, "Digittoolbar.OnBreak():")
  801. self.action = { 'desc' : "breakLine",
  802. 'id' : self.additionalTools }
  803. self.parent.MapWindow.mouse['box'] = 'box'
  804. def OnSnap(self, event):
  805. """!Snap selected features"""
  806. if self.action['desc'] == 'snapLine': # select previous action
  807. self.ToggleTool(self.addPoint, True)
  808. self.ToggleTool(self.additionalTools, False)
  809. self.OnAddPoint(event)
  810. return
  811. Debug.msg(2, "Digittoolbar.OnSnap():")
  812. self.action = { 'desc' : "snapLine",
  813. 'id' : self.additionalTools }
  814. self.parent.MapWindow.mouse['box'] = 'box'
  815. def OnConnect(self, event):
  816. """!Connect selected lines/boundaries"""
  817. if self.action['desc'] == 'connectLine': # select previous action
  818. self.ToggleTool(self.addPoint, True)
  819. self.ToggleTool(self.additionalTools, False)
  820. self.OnAddPoint(event)
  821. return
  822. Debug.msg(2, "Digittoolbar.OnConnect():")
  823. self.action = { 'desc' : "connectLine",
  824. 'id' : self.additionalTools }
  825. self.parent.MapWindow.mouse['box'] = 'box'
  826. def OnQuery(self, event):
  827. """!Query selected lines/boundaries"""
  828. if self.action['desc'] == 'queryLine': # select previous action
  829. self.ToggleTool(self.addPoint, True)
  830. self.ToggleTool(self.additionalTools, False)
  831. self.OnAddPoint(event)
  832. return
  833. Debug.msg(2, "Digittoolbar.OnQuery(): %s" % \
  834. UserSettings.Get(group='vdigit', key='query', subkey='selection'))
  835. self.action = { 'desc' : "queryLine",
  836. 'id' : self.additionalTools }
  837. self.parent.MapWindow.mouse['box'] = 'box'
  838. def OnZBulk(self, event):
  839. """!Z bulk-labeling selected lines/boundaries"""
  840. if not self.parent.digit.driver.Is3D():
  841. wx.MessageBox(parent=self.parent,
  842. message=_("Vector map is not 3D. Operation canceled."),
  843. caption=_("Error"), style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  844. return
  845. if self.action['desc'] == 'zbulkLine': # select previous action
  846. self.ToggleTool(self.addPoint, True)
  847. self.ToggleTool(self.additionalTools, False)
  848. self.OnAddPoint(event)
  849. return
  850. Debug.msg(2, "Digittoolbar.OnZBulk():")
  851. self.action = { 'desc' : "zbulkLine",
  852. 'id' : self.additionalTools }
  853. self.parent.MapWindow.mouse['box'] = 'line'
  854. def OnTypeConversion(self, event):
  855. """!Feature type conversion
  856. Supported conversions:
  857. - point <-> centroid
  858. - line <-> boundary
  859. """
  860. if self.action['desc'] == 'typeConv': # select previous action
  861. self.ToggleTool(self.addPoint, True)
  862. self.ToggleTool(self.additionalTools, False)
  863. self.OnAddPoint(event)
  864. return
  865. Debug.msg(2, "Digittoolbar.OnTypeConversion():")
  866. self.action = { 'desc' : "typeConv",
  867. 'id' : self.additionalTools }
  868. self.parent.MapWindow.mouse['box'] = 'box'
  869. def OnSelectMap (self, event):
  870. """
  871. Select vector map layer for editing
  872. If there is a vector map layer already edited, this action is
  873. firstly terminated. The map layer is closed. After this the
  874. selected map layer activated for editing.
  875. """
  876. if event.GetSelection() == 0: # create new vector map layer
  877. if self.mapLayer:
  878. openVectorMap = self.mapLayer.GetName(fullyQualified=False)['name']
  879. else:
  880. openVectorMap = None
  881. mapName = gdialogs.CreateNewVector(self.parent,
  882. exceptMap = openVectorMap, log = self.log,
  883. cmd = (('v.edit',
  884. { 'tool' : 'create' },
  885. 'map')),
  886. disableAdd=True)[0]
  887. if mapName:
  888. # add layer to map layer tree
  889. if self.layerTree:
  890. self.layerTree.AddLayer(ltype='vector',
  891. lname=mapName,
  892. lchecked=True,
  893. lopacity=1.0,
  894. lcmd=['d.vect', 'map=%s' % mapName])
  895. vectLayers = self.UpdateListOfLayers(updateTool=True)
  896. selection = vectLayers.index(mapName)
  897. else:
  898. pass # TODO (no Layer Manager)
  899. else:
  900. self.combo.SetValue(_('Select vector map'))
  901. return
  902. else:
  903. selection = event.GetSelection() - 1 # first option is 'New vector map'
  904. # skip currently selected map
  905. if self.layers[selection] == self.mapLayer:
  906. return False
  907. if self.mapLayer:
  908. # deactive map layer for editing
  909. self.StopEditing()
  910. # select the given map layer for editing
  911. self.StartEditing(self.layers[selection])
  912. event.Skip()
  913. return True
  914. def StartEditing (self, mapLayer):
  915. """
  916. Start editing selected vector map layer.
  917. @param mapLayer reference to MapLayer instance
  918. """
  919. # deactive layer
  920. self.mapcontent.ChangeLayerActive(mapLayer, False)
  921. # clean map canvas
  922. ### self.parent.MapWindow.EraseMap()
  923. # unset background map if needed
  924. if mapLayer:
  925. if UserSettings.Get(group='vdigit', key='bgmap',
  926. subkey='value', internal=True) == mapLayer.GetName():
  927. UserSettings.Set(group='vdigit', key='bgmap',
  928. subkey='value', value='', internal=True)
  929. self.parent.statusbar.SetStatusText(_("Please wait, "
  930. "opening vector map <%s> for editing...") % \
  931. mapLayer.GetName(),
  932. 0)
  933. # reload vdigit module
  934. reload(vdigit)
  935. from vdigit import VDigit as VDigit
  936. # use vdigit's PseudoDC
  937. self.parent.MapWindow.DefinePseudoDC(vdigit = True)
  938. self.parent.digit = VDigit(mapwindow=self.parent.MapWindow)
  939. self.mapLayer = mapLayer
  940. # open vector map
  941. try:
  942. if not self.parent.MapWindow.CheckPseudoDC():
  943. raise gcmd.DigitError(parent=self.parent,
  944. message=_("Unable to initialize display driver of vector "
  945. "digitizer. See 'Command output' for details."))
  946. self.parent.digit.SetMapName(mapLayer.GetName())
  947. except gcmd.DigitError, e:
  948. self.mapLayer = None
  949. self.StopEditing()
  950. print >> sys.stderr, e # wxMessageBox
  951. return False
  952. # update toolbar
  953. self.combo.SetValue(mapLayer.GetName())
  954. self.parent.toolbars['map'].combo.SetValue (_('Digitize'))
  955. Debug.msg (4, "VDigitToolbar.StartEditing(): layer=%s" % mapLayer.GetName())
  956. # change cursor
  957. if self.parent.MapWindow.mouse['use'] == 'pointer':
  958. self.parent.MapWindow.SetCursor(self.parent.cursors["cross"])
  959. # create pseudoDC for drawing the map
  960. self.parent.MapWindow.pdcVector = vdigit.PseudoDC()
  961. self.parent.digit.driver.SetDevice(self.parent.MapWindow.pdcVector)
  962. if not self.parent.MapWindow.resize:
  963. self.parent.MapWindow.UpdateMap(render=True)
  964. opacity = mapLayer.GetOpacity(float=True)
  965. if opacity < 1.0:
  966. alpha = int(opacity * 255)
  967. self.parent.digit.driver.UpdateSettings(alpha)
  968. return True
  969. def StopEditing(self):
  970. """!Stop editing of selected vector map layer.
  971. @return True on success
  972. @return False on failure
  973. """
  974. # use wx's PseudoDC
  975. self.parent.MapWindow.DefinePseudoDC(vdigit = False)
  976. self.combo.SetValue (_('Select vector map'))
  977. # save changes
  978. if self.mapLayer:
  979. Debug.msg (4, "VDigitToolbar.StopEditing(): layer=%s" % self.mapLayer.GetName())
  980. if UserSettings.Get(group='vdigit', key='saveOnExit', subkey='enabled') is False:
  981. if self.parent.digit.GetUndoLevel() > 0:
  982. dlg = wx.MessageDialog(parent=self.parent,
  983. message=_("Do you want to save changes "
  984. "in vector map <%s>?") % self.mapLayer.GetName(),
  985. caption=_("Save changes?"),
  986. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  987. if dlg.ShowModal() == wx.ID_NO:
  988. # revert changes
  989. self.parent.digit.Undo(0)
  990. dlg.Destroy()
  991. self.parent.statusbar.SetStatusText(_("Please wait, "
  992. "closing and rebuilding topology of "
  993. "vector map <%s>...") % self.mapLayer.GetName(),
  994. 0)
  995. self.parent.digit.SetMapName(None) # -> close map
  996. # re-active layer
  997. item = self.parent.tree.FindItemByData('maplayer', self.mapLayer)
  998. if item and self.parent.tree.IsItemChecked(item):
  999. self.mapcontent.ChangeLayerActive(self.mapLayer, True)
  1000. # change cursor
  1001. self.parent.MapWindow.SetCursor(self.parent.cursors["default"])
  1002. # disable pseudodc for vector map layer
  1003. self.parent.MapWindow.pdcVector = None
  1004. self.parent.digit.driver.SetDevice(None)
  1005. # close dialogs
  1006. for dialog in ('attributes', 'category'):
  1007. if self.parent.dialogs[dialog]:
  1008. self.parent.dialogs[dialog].Close()
  1009. self.parent.dialogs[dialog] = None
  1010. self.parent.digit.__del__() # FIXME: destructor is not called here (del)
  1011. self.parent.digit = None
  1012. self.mapLayer = None
  1013. self.parent.MapWindow.redrawAll = True
  1014. return True
  1015. def UpdateListOfLayers (self, updateTool=False):
  1016. """!
  1017. Update list of available vector map layers.
  1018. This list consists only editable layers (in the current mapset)
  1019. Optionally also update toolbar
  1020. """
  1021. Debug.msg (4, "VDigitToolbar.UpdateListOfLayers(): updateTool=%d" % \
  1022. updateTool)
  1023. layerNameSelected = None
  1024. # name of currently selected layer
  1025. if self.mapLayer:
  1026. layerNameSelected = self.mapLayer.GetName()
  1027. # select vector map layer in the current mapset
  1028. layerNameList = []
  1029. self.layers = self.mapcontent.GetListOfLayers(l_type="vector",
  1030. l_mapset=grass.gisenv()['MAPSET'])
  1031. for layer in self.layers:
  1032. if not layer.name in layerNameList: # do not duplicate layer
  1033. layerNameList.append (layer.GetName())
  1034. if updateTool: # update toolbar
  1035. if not self.mapLayer:
  1036. value = _('Select vector map')
  1037. else:
  1038. value = layerNameSelected
  1039. if not self.comboid:
  1040. self.combo = wx.ComboBox(self, id=wx.ID_ANY, value=value,
  1041. choices=[_('New vector map'), ] + layerNameList, size=(85, -1),
  1042. style=wx.CB_READONLY)
  1043. self.comboid = self.InsertControl(0, self.combo)
  1044. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectMap, self.comboid)
  1045. else:
  1046. self.combo.SetItems([_('New vector map'), ] + layerNameList)
  1047. self.Realize()
  1048. return layerNameList
  1049. def GetLayer(self):
  1050. """!Get selected layer for editing -- MapLayer instance"""
  1051. return self.mapLayer
  1052. class ProfileToolbar(AbstractToolbar):
  1053. """!
  1054. Toolbar for profiling raster map
  1055. """
  1056. def __init__(self, parent):
  1057. AbstractToolbar.__init__(self, parent)
  1058. self.InitToolbar(self.ToolbarData())
  1059. # realize the toolbar
  1060. self.Realize()
  1061. def ToolbarData(self):
  1062. """!Toolbar data"""
  1063. self.transect = wx.NewId()
  1064. self.addraster = wx.NewId()
  1065. self.draw = wx.NewId()
  1066. self.options = wx.NewId()
  1067. self.drag = wx.NewId()
  1068. self.zoom = wx.NewId()
  1069. self.unzoom = wx.NewId()
  1070. self.erase = wx.NewId()
  1071. self.save = wx.NewId()
  1072. self.printer = wx.NewId()
  1073. self.quit = wx.NewId()
  1074. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  1075. return (
  1076. (self.addraster, 'raster', Icons["addrast"].GetBitmap(),
  1077. wx.ITEM_NORMAL, Icons["addrast"].GetLabel(), Icons["addrast"].GetDesc(),
  1078. self.parent.OnSelectRaster),
  1079. (self.transect, 'transect', Icons["transect"].GetBitmap(),
  1080. wx.ITEM_NORMAL, Icons["transect"].GetLabel(), Icons["transect"].GetDesc(),
  1081. self.parent.OnDrawTransect),
  1082. (self.draw, 'profiledraw', Icons["profiledraw"].GetBitmap(),
  1083. wx.ITEM_NORMAL, Icons["profiledraw"].GetLabel(), Icons["profiledraw"].GetDesc(),
  1084. self.parent.OnCreateProfile),
  1085. (self.options, 'options', Icons["profileopt"].GetBitmap(),
  1086. wx.ITEM_NORMAL, Icons["profileopt"].GetLabel(), Icons["profileopt"].GetDesc(),
  1087. self.parent.ProfileOptionsMenu),
  1088. (self.drag, 'drag', Icons['pan'].GetBitmap(),
  1089. wx.ITEM_NORMAL, Icons["pan"].GetLabel(), Icons["pan"].GetDesc(),
  1090. self.parent.OnDrag),
  1091. (self.zoom, 'zoom', Icons['zoom_in'].GetBitmap(),
  1092. wx.ITEM_NORMAL, Icons["zoom_in"].GetLabel(), Icons["zoom_in"].GetDesc(),
  1093. self.parent.OnZoom),
  1094. (self.unzoom, 'unzoom', Icons['zoom_back'].GetBitmap(),
  1095. wx.ITEM_NORMAL, Icons["zoom_back"].GetLabel(), Icons["zoom_back"].GetDesc(),
  1096. self.parent.OnRedraw),
  1097. (self.erase, 'erase', Icons["erase"].GetBitmap(),
  1098. wx.ITEM_NORMAL, Icons["erase"].GetLabel(), Icons["erase"].GetDesc(),
  1099. self.parent.OnErase),
  1100. ("", "", "", "", "", "", ""),
  1101. (self.save, 'save', Icons["savefile"].GetBitmap(),
  1102. wx.ITEM_NORMAL, Icons["savefile"].GetLabel(), Icons["savefile"].GetDesc(),
  1103. self.parent.SaveToFile),
  1104. (self.printer, 'print', Icons["printmap"].GetBitmap(),
  1105. wx.ITEM_NORMAL, Icons["printmap"].GetLabel(), Icons["printmap"].GetDesc(),
  1106. self.parent.PrintMenu),
  1107. (self.quit, 'quit', Icons["quit"].GetBitmap(),
  1108. wx.ITEM_NORMAL, Icons["quit"].GetLabel(), Icons["quit"].GetDesc(),
  1109. self.parent.OnQuit),
  1110. )
  1111. class NvizToolbar(AbstractToolbar):
  1112. """!
  1113. Nviz toolbar
  1114. """
  1115. def __init__(self, parent, mapcontent):
  1116. self.mapcontent = mapcontent
  1117. AbstractToolbar.__init__(self, parent)
  1118. self.InitToolbar(self.ToolbarData())
  1119. # realize the toolbar
  1120. self.Realize()
  1121. def ToolbarData(self):
  1122. """!Toolbar data"""
  1123. self.settings = wx.NewId()
  1124. self.quit = wx.NewId()
  1125. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  1126. return (
  1127. (self.settings, "settings", Icons["nvizSettings"].GetBitmap(),
  1128. wx.ITEM_NORMAL, Icons["nvizSettings"].GetLabel(), Icons["nvizSettings"].GetDesc(),
  1129. self.OnSettings),
  1130. (self.quit, 'quit', Icons["quit"].GetBitmap(),
  1131. wx.ITEM_NORMAL, Icons["quit"].GetLabel(), Icons["quit"].GetDesc(),
  1132. self.OnExit),
  1133. )
  1134. def OnSettings(self, event):
  1135. win = self.parent.nvizToolWin
  1136. if not win.IsShown():
  1137. self.parent.nvizToolWin.Show()
  1138. else:
  1139. self.parent.nvizToolWin.Hide()
  1140. def OnExit (self, event=None):
  1141. """!Quit nviz tool (swith to 2D mode)"""
  1142. # hide dialogs if still open
  1143. if self.parent.nvizToolWin:
  1144. self.parent.nvizToolWin.Hide()
  1145. # set default mouse settings
  1146. self.parent.MapWindow.mouse['use'] = "pointer"
  1147. self.parent.MapWindow.mouse['box'] = "point"
  1148. self.parent.MapWindow.polycoords = []
  1149. # disable the toolbar
  1150. self.parent.RemoveToolbar("nviz")