toolbars.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. """!
  2. @package vdigit.toolbars
  3. @brief wxGUI vector digitizer toolbars
  4. List of classes:
  5. - VDigitToolbar
  6. (C) 2007-2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Martin Landa <landa.martin gmail.com>
  10. """
  11. from grass.script import core as grass
  12. from gui_core.toolbars import BaseToolbar
  13. from gui_core.dialogs import CreateNewVector
  14. from vdigit.preferences import VDigitSettingsDialog
  15. from vdigit.main import VDigit
  16. from core.debug import Debug
  17. from core.settings import UserSettings
  18. from core.gcmd import GError
  19. class VDigitToolbar(BaseToolbar):
  20. """!Toolbar for digitization
  21. """
  22. def __init__(self, parent, MapWindow, tools = [], layerTree = None, log = None):
  23. self.MapWindow = MapWindow
  24. self.Map = MapWindow.GetMap() # Map class instance
  25. self.layerTree = layerTree # reference to layer tree associated to map display
  26. self.log = log # log area
  27. self.tools = tools
  28. BaseToolbar.__init__(self, parent)
  29. self.digit = None
  30. # currently selected map layer for editing (reference to MapLayer instance)
  31. self.mapLayer = None
  32. # list of vector layers from Layer Manager (only in the current mapset)
  33. self.layers = []
  34. self.comboid = self.combo = None
  35. self.undo = -1
  36. # only one dialog can be open
  37. self.settingsDialog = None
  38. # create toolbars (two rows optionally)
  39. self.InitToolbar(self._toolbarData())
  40. self.Bind(wx.EVT_TOOL, self.OnTool)
  41. # default action (digitize new point, line, etc.)
  42. self.action = { 'desc' : '',
  43. 'type' : '',
  44. 'id' : -1 }
  45. # list of available vector maps
  46. self.UpdateListOfLayers(updateTool = True)
  47. # realize toolbar
  48. self.Realize()
  49. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  50. if self.combo:
  51. self.combo.Hide()
  52. self.combo.Show()
  53. # disable undo/redo
  54. if self.undo:
  55. self.EnableTool(self.undo, False)
  56. # toogle to pointer by default
  57. self.OnTool(None)
  58. self.FixSize(width = 105)
  59. def _toolbarData(self):
  60. """!Toolbar data
  61. """
  62. data = []
  63. icons = Icons['vdigit']
  64. if not self.tools or 'selector' in self.tools:
  65. data.append((None, ))
  66. if not self.tools or 'addPoint' in self.tools:
  67. data.append(("addPoint", icons["addPoint"],
  68. self.OnAddPoint,
  69. wx.ITEM_CHECK))
  70. if not self.tools or 'addLine' in self.tools:
  71. data.append(("addLine", icons["addLine"],
  72. self.OnAddLine,
  73. wx.ITEM_CHECK))
  74. if not self.tools or 'addBoundary' in self.tools:
  75. data.append(("addBoundary", icons["addBoundary"],
  76. self.OnAddBoundary,
  77. wx.ITEM_CHECK))
  78. if not self.tools or 'addCentroid' in self.tools:
  79. data.append(("addCentroid", icons["addCentroid"],
  80. self.OnAddCentroid,
  81. wx.ITEM_CHECK))
  82. if not self.tools or 'addArea' in self.tools:
  83. data.append(("addArea", icons["addArea"],
  84. self.OnAddArea,
  85. wx.ITEM_CHECK))
  86. if not self.tools or 'moveVertex' in self.tools:
  87. data.append(("moveVertex", icons["moveVertex"],
  88. self.OnMoveVertex,
  89. wx.ITEM_CHECK))
  90. if not self.tools or 'addVertex' in self.tools:
  91. data.append(("addVertex", icons["addVertex"],
  92. self.OnAddVertex,
  93. wx.ITEM_CHECK))
  94. if not self.tools or 'removeVertex' in self.tools:
  95. data.append(("removeVertex", icons["removeVertex"],
  96. self.OnRemoveVertex,
  97. wx.ITEM_CHECK))
  98. if not self.tools or 'editLine' in self.tools:
  99. data.append(("editLine", icons["editLine"],
  100. self.OnEditLine,
  101. wx.ITEM_CHECK))
  102. if not self.tools or 'moveLine' in self.tools:
  103. data.append(("moveLine", icons["moveLine"],
  104. self.OnMoveLine,
  105. wx.ITEM_CHECK))
  106. if not self.tools or 'deleteLine' in self.tools:
  107. data.append(("deleteLine", icons["deleteLine"],
  108. self.OnDeleteLine,
  109. wx.ITEM_CHECK))
  110. if not self.tools or 'displayCats' in self.tools:
  111. data.append(("displayCats", icons["displayCats"],
  112. self.OnDisplayCats,
  113. wx.ITEM_CHECK))
  114. if not self.tools or 'displayAttr' in self.tools:
  115. data.append(("displayAttr", icons["displayAttr"],
  116. self.OnDisplayAttr,
  117. wx.ITEM_CHECK))
  118. if not self.tools or 'additionalSelf.Tools' in self.tools:
  119. data.append(("additionalTools", icons["additionalTools"],
  120. self.OnAdditionalToolMenu,
  121. wx.ITEM_CHECK))
  122. if not self.tools or 'undo' in self.tools or \
  123. 'settings' in self.tools or \
  124. 'quit' in self.tools:
  125. data.append((None, ))
  126. if not self.tools or 'undo' in self.tools:
  127. data.append(("undo", icons["undo"],
  128. self.OnUndo))
  129. if not self.tools or 'settings' in self.tools:
  130. data.append(("settings", icons["settings"],
  131. self.OnSettings))
  132. if not self.tools or 'quit' in self.tools:
  133. data.append(("quit", icons["quit"],
  134. self.OnExit))
  135. return self._getToolbarData(data)
  136. def OnTool(self, event):
  137. """!Tool selected -> disable selected tool in map toolbar"""
  138. if self.parent.GetName() == 'IClassWindow':
  139. toolbarName = 'iClassMap'
  140. else:
  141. toolbarName = 'map'
  142. aId = self.parent.toolbars[toolbarName].GetAction(type = 'id')
  143. self.parent.toolbars[toolbarName].ToggleTool(aId, False)
  144. # set cursor
  145. cursor = self.parent.cursors["cross"]
  146. self.MapWindow.SetCursor(cursor)
  147. # pointer
  148. self.parent.OnPointer(None)
  149. if event:
  150. # deselect previously selected tool
  151. aId = self.action.get('id', -1)
  152. if aId != event.GetId() and \
  153. self.action['id'] != -1:
  154. self.ToggleTool(self.action['id'], False)
  155. else:
  156. self.ToggleTool(self.action['id'], True)
  157. self.action['id'] = event.GetId()
  158. event.Skip()
  159. if self.action['id'] != -1:
  160. self.ToggleTool(self.action['id'], True)
  161. # clear tmp canvas
  162. if self.action['id'] != aId:
  163. self.MapWindow.ClearLines(pdc = self.MapWindow.pdcTmp)
  164. if self.digit and \
  165. len(self.MapWindow.digit.GetDisplay().GetSelected()) > 0:
  166. # cancel action
  167. self.MapWindow.OnMiddleDown(None)
  168. # set focus
  169. self.MapWindow.SetFocus()
  170. def OnAddPoint(self, event):
  171. """!Add point to the vector map Laier"""
  172. Debug.msg (2, "VDigitToolbar.OnAddPoint()")
  173. self.action = { 'desc' : "addLine",
  174. 'type' : "point",
  175. 'id' : self.addPoint }
  176. self.MapWindow.mouse['box'] = 'point'
  177. def OnAddLine(self, event):
  178. """!Add line to the vector map layer"""
  179. Debug.msg (2, "VDigitToolbar.OnAddLine()")
  180. self.action = { 'desc' : "addLine",
  181. 'type' : "line",
  182. 'id' : self.addLine }
  183. self.MapWindow.mouse['box'] = 'line'
  184. ### self.MapWindow.polycoords = [] # reset temp line
  185. def OnAddBoundary(self, event):
  186. """!Add boundary to the vector map layer"""
  187. Debug.msg (2, "VDigitToolbar.OnAddBoundary()")
  188. if self.action['desc'] != 'addLine' or \
  189. self.action['type'] != 'boundary':
  190. self.MapWindow.polycoords = [] # reset temp line
  191. self.action = { 'desc' : "addLine",
  192. 'type' : "boundary",
  193. 'id' : self.addBoundary }
  194. self.MapWindow.mouse['box'] = 'line'
  195. def OnAddCentroid(self, event):
  196. """!Add centroid to the vector map layer"""
  197. Debug.msg (2, "VDigitToolbar.OnAddCentroid()")
  198. self.action = { 'desc' : "addLine",
  199. 'type' : "centroid",
  200. 'id' : self.addCentroid }
  201. self.MapWindow.mouse['box'] = 'point'
  202. def OnAddArea(self, event):
  203. """!Add area to the vector map layer"""
  204. Debug.msg (2, "VDigitToolbar.OnAddCentroid()")
  205. self.action = { 'desc' : "addLine",
  206. 'type' : "area",
  207. 'id' : self.addArea }
  208. self.MapWindow.mouse['box'] = 'line'
  209. def OnExit (self, event=None):
  210. """!Quit digitization tool"""
  211. # stop editing of the currently selected map layer
  212. if self.mapLayer:
  213. self.StopEditing()
  214. # close dialogs if still open
  215. if self.settingsDialog:
  216. self.settingsDialog.OnCancel(None)
  217. # set default mouse settings
  218. self.MapWindow.mouse['use'] = "pointer"
  219. self.MapWindow.mouse['box'] = "point"
  220. self.MapWindow.polycoords = []
  221. # disable the toolbar
  222. self.parent.RemoveToolbar("vdigit")
  223. def OnMoveVertex(self, event):
  224. """!Move line vertex"""
  225. Debug.msg(2, "Digittoolbar.OnMoveVertex():")
  226. self.action = { 'desc' : "moveVertex",
  227. 'id' : self.moveVertex }
  228. self.MapWindow.mouse['box'] = 'point'
  229. def OnAddVertex(self, event):
  230. """!Add line vertex"""
  231. Debug.msg(2, "Digittoolbar.OnAddVertex():")
  232. self.action = { 'desc' : "addVertex",
  233. 'id' : self.addVertex }
  234. self.MapWindow.mouse['box'] = 'point'
  235. def OnRemoveVertex(self, event):
  236. """!Remove line vertex"""
  237. Debug.msg(2, "Digittoolbar.OnRemoveVertex():")
  238. self.action = { 'desc' : "removeVertex",
  239. 'id' : self.removeVertex }
  240. self.MapWindow.mouse['box'] = 'point'
  241. def OnEditLine(self, event):
  242. """!Edit line"""
  243. Debug.msg(2, "Digittoolbar.OnEditLine():")
  244. self.action = { 'desc' : "editLine",
  245. 'id' : self.editLine }
  246. self.MapWindow.mouse['box'] = 'line'
  247. def OnMoveLine(self, event):
  248. """!Move line"""
  249. Debug.msg(2, "Digittoolbar.OnMoveLine():")
  250. self.action = { 'desc' : "moveLine",
  251. 'id' : self.moveLine }
  252. self.MapWindow.mouse['box'] = 'box'
  253. def OnDeleteLine(self, event):
  254. """!Delete line"""
  255. Debug.msg(2, "Digittoolbar.OnDeleteLine():")
  256. self.action = { 'desc' : "deleteLine",
  257. 'id' : self.deleteLine }
  258. self.MapWindow.mouse['box'] = 'box'
  259. def OnDisplayCats(self, event):
  260. """!Display/update categories"""
  261. Debug.msg(2, "Digittoolbar.OnDisplayCats():")
  262. self.action = { 'desc' : "displayCats",
  263. 'id' : self.displayCats }
  264. self.MapWindow.mouse['box'] = 'point'
  265. def OnDisplayAttr(self, event):
  266. """!Display/update attributes"""
  267. Debug.msg(2, "Digittoolbar.OnDisplayAttr():")
  268. self.action = { 'desc' : "displayAttrs",
  269. 'id' : self.displayAttr }
  270. self.MapWindow.mouse['box'] = 'point'
  271. def OnUndo(self, event):
  272. """!Undo previous changes"""
  273. self.digit.Undo()
  274. event.Skip()
  275. def EnableUndo(self, enable = True):
  276. """!Enable 'Undo' in toolbar
  277. @param enable False for disable
  278. """
  279. if enable:
  280. if self.GetToolEnabled(self.undo) is False:
  281. self.EnableTool(self.undo, True)
  282. else:
  283. if self.GetToolEnabled(self.undo) is True:
  284. self.EnableTool(self.undo, False)
  285. def OnSettings(self, event):
  286. """!Show settings dialog"""
  287. if self.digit is None:
  288. try:
  289. self.digit = self.MapWindow.digit = VDigit(mapwindow = self.MapWindow)
  290. except SystemExit:
  291. self.digit = self.MapWindow.digit = None
  292. if not self.settingsDialog:
  293. self.settingsDialog = VDigitSettingsDialog(parent = self.parent, title = _("Digitization settings"),
  294. style = wx.DEFAULT_DIALOG_STYLE)
  295. self.settingsDialog.Show()
  296. def OnAdditionalToolMenu(self, event):
  297. """!Menu for additional tools"""
  298. point = wx.GetMousePosition()
  299. toolMenu = wx.Menu()
  300. for label, itype, handler, desc in (
  301. (_('Break selected lines/boundaries at intersection'),
  302. wx.ITEM_CHECK, self.OnBreak, "breakLine"),
  303. (_('Connect selected lines/boundaries'),
  304. wx.ITEM_CHECK, self.OnConnect, "connectLine"),
  305. (_('Copy categories'),
  306. wx.ITEM_CHECK, self.OnCopyCats, "copyCats"),
  307. (_('Copy features from (background) vector map'),
  308. wx.ITEM_CHECK, self.OnCopy, "copyLine"),
  309. (_('Copy attributes'),
  310. wx.ITEM_CHECK, self.OnCopyAttrb, "copyAttrs"),
  311. (_('Feature type conversion'),
  312. wx.ITEM_CHECK, self.OnTypeConversion, "typeConv"),
  313. (_('Flip selected lines/boundaries'),
  314. wx.ITEM_CHECK, self.OnFlip, "flipLine"),
  315. (_('Merge selected lines/boundaries'),
  316. wx.ITEM_CHECK, self.OnMerge, "mergeLine"),
  317. (_('Snap selected lines/boundaries (only to nodes)'),
  318. wx.ITEM_CHECK, self.OnSnap, "snapLine"),
  319. (_('Split line/boundary'),
  320. wx.ITEM_CHECK, self.OnSplitLine, "splitLine"),
  321. (_('Query features'),
  322. wx.ITEM_CHECK, self.OnQuery, "queryLine"),
  323. (_('Z bulk-labeling of 3D lines'),
  324. wx.ITEM_CHECK, self.OnZBulk, "zbulkLine")):
  325. # Add items to the menu
  326. item = wx.MenuItem(parentMenu = toolMenu, id = wx.ID_ANY,
  327. text = label,
  328. kind = itype)
  329. toolMenu.AppendItem(item)
  330. self.MapWindow.Bind(wx.EVT_MENU, handler, item)
  331. if self.action['desc'] == desc:
  332. item.Check(True)
  333. # Popup the menu. If an item is selected then its handler
  334. # will be called before PopupMenu returns.
  335. self.MapWindow.PopupMenu(toolMenu)
  336. toolMenu.Destroy()
  337. if self.action['desc'] == 'addPoint':
  338. self.ToggleTool(self.additionalTools, False)
  339. def OnCopy(self, event):
  340. """!Copy selected features from (background) vector map"""
  341. if self.action['desc'] == 'copyLine': # select previous action
  342. self.ToggleTool(self.addPoint, True)
  343. self.ToggleTool(self.additionalTools, False)
  344. self.OnAddPoint(event)
  345. return
  346. Debug.msg(2, "Digittoolbar.OnCopy():")
  347. self.action = { 'desc' : "copyLine",
  348. 'id' : self.additionalTools }
  349. self.MapWindow.mouse['box'] = 'box'
  350. def OnSplitLine(self, event):
  351. """!Split line"""
  352. if self.action['desc'] == 'splitLine': # select previous action
  353. self.ToggleTool(self.addPoint, True)
  354. self.ToggleTool(self.additionalTools, False)
  355. self.OnAddPoint(event)
  356. return
  357. Debug.msg(2, "Digittoolbar.OnSplitLine():")
  358. self.action = { 'desc' : "splitLine",
  359. 'id' : self.additionalTools }
  360. self.MapWindow.mouse['box'] = 'point'
  361. def OnCopyCats(self, event):
  362. """!Copy categories"""
  363. if self.action['desc'] == 'copyCats': # select previous action
  364. self.ToggleTool(self.addPoint, True)
  365. self.ToggleTool(self.copyCats, False)
  366. self.OnAddPoint(event)
  367. return
  368. Debug.msg(2, "Digittoolbar.OnCopyCats():")
  369. self.action = { 'desc' : "copyCats",
  370. 'id' : self.additionalTools }
  371. self.MapWindow.mouse['box'] = 'point'
  372. def OnCopyAttrb(self, event):
  373. """!Copy attributes"""
  374. if self.action['desc'] == 'copyAttrs': # select previous action
  375. self.ToggleTool(self.addPoint, True)
  376. self.ToggleTool(self.copyCats, False)
  377. self.OnAddPoint(event)
  378. return
  379. Debug.msg(2, "Digittoolbar.OnCopyAttrb():")
  380. self.action = { 'desc' : "copyAttrs",
  381. 'id' : self.additionalTools }
  382. self.MapWindow.mouse['box'] = 'point'
  383. def OnFlip(self, event):
  384. """!Flip selected lines/boundaries"""
  385. if self.action['desc'] == 'flipLine': # select previous action
  386. self.ToggleTool(self.addPoint, True)
  387. self.ToggleTool(self.additionalTools, False)
  388. self.OnAddPoint(event)
  389. return
  390. Debug.msg(2, "Digittoolbar.OnFlip():")
  391. self.action = { 'desc' : "flipLine",
  392. 'id' : self.additionalTools }
  393. self.MapWindow.mouse['box'] = 'box'
  394. def OnMerge(self, event):
  395. """!Merge selected lines/boundaries"""
  396. if self.action['desc'] == 'mergeLine': # select previous action
  397. self.ToggleTool(self.addPoint, True)
  398. self.ToggleTool(self.additionalTools, False)
  399. self.OnAddPoint(event)
  400. return
  401. Debug.msg(2, "Digittoolbar.OnMerge():")
  402. self.action = { 'desc' : "mergeLine",
  403. 'id' : self.additionalTools }
  404. self.MapWindow.mouse['box'] = 'box'
  405. def OnBreak(self, event):
  406. """!Break selected lines/boundaries"""
  407. if self.action['desc'] == 'breakLine': # select previous action
  408. self.ToggleTool(self.addPoint, True)
  409. self.ToggleTool(self.additionalTools, False)
  410. self.OnAddPoint(event)
  411. return
  412. Debug.msg(2, "Digittoolbar.OnBreak():")
  413. self.action = { 'desc' : "breakLine",
  414. 'id' : self.additionalTools }
  415. self.MapWindow.mouse['box'] = 'box'
  416. def OnSnap(self, event):
  417. """!Snap selected features"""
  418. if self.action['desc'] == 'snapLine': # select previous action
  419. self.ToggleTool(self.addPoint, True)
  420. self.ToggleTool(self.additionalTools, False)
  421. self.OnAddPoint(event)
  422. return
  423. Debug.msg(2, "Digittoolbar.OnSnap():")
  424. self.action = { 'desc' : "snapLine",
  425. 'id' : self.additionalTools }
  426. self.MapWindow.mouse['box'] = 'box'
  427. def OnConnect(self, event):
  428. """!Connect selected lines/boundaries"""
  429. if self.action['desc'] == 'connectLine': # select previous action
  430. self.ToggleTool(self.addPoint, True)
  431. self.ToggleTool(self.additionalTools, False)
  432. self.OnAddPoint(event)
  433. return
  434. Debug.msg(2, "Digittoolbar.OnConnect():")
  435. self.action = { 'desc' : "connectLine",
  436. 'id' : self.additionalTools }
  437. self.MapWindow.mouse['box'] = 'box'
  438. def OnQuery(self, event):
  439. """!Query selected lines/boundaries"""
  440. if self.action['desc'] == 'queryLine': # select previous action
  441. self.ToggleTool(self.addPoint, True)
  442. self.ToggleTool(self.additionalTools, False)
  443. self.OnAddPoint(event)
  444. return
  445. Debug.msg(2, "Digittoolbar.OnQuery(): %s" % \
  446. UserSettings.Get(group = 'vdigit', key = 'query', subkey = 'selection'))
  447. self.action = { 'desc' : "queryLine",
  448. 'id' : self.additionalTools }
  449. self.MapWindow.mouse['box'] = 'box'
  450. def OnZBulk(self, event):
  451. """!Z bulk-labeling selected lines/boundaries"""
  452. if not self.digit.IsVector3D():
  453. GError(parent = self.parent,
  454. message = _("Vector map is not 3D. Operation canceled."))
  455. return
  456. if self.action['desc'] == 'zbulkLine': # select previous action
  457. self.ToggleTool(self.addPoint, True)
  458. self.ToggleTool(self.additionalTools, False)
  459. self.OnAddPoint(event)
  460. return
  461. Debug.msg(2, "Digittoolbar.OnZBulk():")
  462. self.action = { 'desc' : "zbulkLine",
  463. 'id' : self.additionalTools }
  464. self.MapWindow.mouse['box'] = 'line'
  465. def OnTypeConversion(self, event):
  466. """!Feature type conversion
  467. Supported conversions:
  468. - point <-> centroid
  469. - line <-> boundary
  470. """
  471. if self.action['desc'] == 'typeConv': # select previous action
  472. self.ToggleTool(self.addPoint, True)
  473. self.ToggleTool(self.additionalTools, False)
  474. self.OnAddPoint(event)
  475. return
  476. Debug.msg(2, "Digittoolbar.OnTypeConversion():")
  477. self.action = { 'desc' : "typeConv",
  478. 'id' : self.additionalTools }
  479. self.MapWindow.mouse['box'] = 'box'
  480. def OnSelectMap (self, event):
  481. """!Select vector map layer for editing
  482. If there is a vector map layer already edited, this action is
  483. firstly terminated. The map layer is closed. After this the
  484. selected map layer activated for editing.
  485. """
  486. if event.GetSelection() == 0: # create new vector map layer
  487. if self.mapLayer:
  488. openVectorMap = self.mapLayer.GetName(fullyQualified = False)['name']
  489. else:
  490. openVectorMap = None
  491. dlg = CreateNewVector(self.parent,
  492. exceptMap = openVectorMap, log = self.log,
  493. cmd = (('v.edit',
  494. { 'flags' : 'b', 'tool' : 'create' },
  495. 'map')),
  496. disableAdd = True)
  497. if dlg and dlg.GetName():
  498. # add layer to map layer tree
  499. if self.layerTree:
  500. mapName = dlg.GetName() + '@' + grass.gisenv()['MAPSET']
  501. self.layerTree.AddLayer(ltype = 'vector',
  502. lname = mapName,
  503. lcmd = ['d.vect', 'map=%s' % mapName])
  504. vectLayers = self.UpdateListOfLayers(updateTool = True)
  505. selection = vectLayers.index(mapName)
  506. # create table ?
  507. if dlg.IsChecked('table'):
  508. lmgr = self.parent.GetLayerManager()
  509. if lmgr:
  510. lmgr.OnShowAttributeTable(None, selection = 'table')
  511. dlg.Destroy()
  512. else:
  513. self.combo.SetValue(_('Select vector map'))
  514. if dlg:
  515. dlg.Destroy()
  516. return
  517. else:
  518. selection = event.GetSelection() - 1 # first option is 'New vector map'
  519. # skip currently selected map
  520. if self.layers[selection] == self.mapLayer:
  521. return
  522. if self.mapLayer:
  523. # deactive map layer for editing
  524. self.StopEditing()
  525. # select the given map layer for editing
  526. self.StartEditing(self.layers[selection])
  527. event.Skip()
  528. def StartEditing (self, mapLayer):
  529. """!Start editing selected vector map layer.
  530. @param mapLayer MapLayer to be edited
  531. """
  532. # deactive layer
  533. self.Map.ChangeLayerActive(mapLayer, False)
  534. # clean map canvas
  535. self.MapWindow.EraseMap()
  536. # unset background map if needed
  537. if mapLayer:
  538. if UserSettings.Get(group = 'vdigit', key = 'bgmap',
  539. subkey = 'value', internal = True) == mapLayer.GetName():
  540. UserSettings.Set(group = 'vdigit', key = 'bgmap',
  541. subkey = 'value', value = '', internal = True)
  542. self.parent.SetStatusText(_("Please wait, "
  543. "opening vector map <%s> for editing...") % mapLayer.GetName(),
  544. 0)
  545. self.MapWindow.pdcVector = wx.PseudoDC()
  546. self.digit = self.MapWindow.digit = VDigit(mapwindow = self.MapWindow)
  547. self.mapLayer = mapLayer
  548. # open vector map
  549. if self.digit.OpenMap(mapLayer.GetName()) is None:
  550. self.mapLayer = None
  551. self.StopEditing()
  552. return False
  553. # check feature type (only for OGR layers)
  554. fType = self.digit.GetFeatureType()
  555. self.EnableAll()
  556. self.EnableUndo(False)
  557. if fType == 'Point':
  558. for tool in (self.addLine, self.addBoundary, self.addCentroid,
  559. self.addArea, self.moveVertex, self.addVertex,
  560. self.removeVertex, self.editLine):
  561. self.EnableTool(tool, False)
  562. elif fType == 'Line String':
  563. for tool in (self.addPoint, self.addBoundary, self.addCentroid,
  564. self.addArea):
  565. self.EnableTool(tool, False)
  566. elif fType == 'Polygon':
  567. for tool in (self.addPoint, self.addLine, self.addBoundary, self.addCentroid):
  568. self.EnableTool(tool, False)
  569. elif fType:
  570. GError(parent = self,
  571. message = _("Unsupported feature type '%s'. Unable to edit "
  572. "OGR layer <%s>.") % (fType, mapLayer.GetName()))
  573. self.digit.CloseMap()
  574. self.mapLayer = None
  575. self.StopEditing()
  576. return False
  577. # update toolbar
  578. if self.combo:
  579. self.combo.SetValue(mapLayer.GetName())
  580. if 'map' in self.parent.toolbars:
  581. self.parent.toolbars['map'].combo.SetValue (_('Digitize'))
  582. if self.parent.GetName() != "IClassWindow":
  583. lmgr = self.parent.GetLayerManager()
  584. if lmgr:
  585. lmgr.toolbars['tools'].Enable('vdigit', enable = False)
  586. Debug.msg (4, "VDigitToolbar.StartEditing(): layer=%s" % mapLayer.GetName())
  587. # change cursor
  588. if self.MapWindow.mouse['use'] == 'pointer':
  589. self.MapWindow.SetCursor(self.parent.cursors["cross"])
  590. if not self.MapWindow.resize:
  591. self.MapWindow.UpdateMap(render = True)
  592. # respect opacity
  593. opacity = mapLayer.GetOpacity(float = True)
  594. if opacity < 1.0:
  595. alpha = int(opacity * 255)
  596. self.digit.GetDisplay().UpdateSettings(alpha = alpha)
  597. return True
  598. def StopEditing(self):
  599. """!Stop editing of selected vector map layer.
  600. @return True on success
  601. @return False on failure
  602. """
  603. if self.combo:
  604. self.combo.SetValue (_('Select vector map'))
  605. # save changes
  606. if self.mapLayer:
  607. Debug.msg (4, "VDigitToolbar.StopEditing(): layer=%s" % self.mapLayer.GetName())
  608. if UserSettings.Get(group = 'vdigit', key = 'saveOnExit', subkey = 'enabled') is False:
  609. if self.digit.GetUndoLevel() > -1:
  610. dlg = wx.MessageDialog(parent = self.parent,
  611. message = _("Do you want to save changes "
  612. "in vector map <%s>?") % self.mapLayer.GetName(),
  613. caption = _("Save changes?"),
  614. style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  615. if dlg.ShowModal() == wx.ID_NO:
  616. # revert changes
  617. self.digit.Undo(0)
  618. dlg.Destroy()
  619. self.parent.SetStatusText(_("Please wait, "
  620. "closing and rebuilding topology of "
  621. "vector map <%s>...") % self.mapLayer.GetName(),
  622. 0)
  623. self.digit.CloseMap()
  624. lmgr = self.parent.GetLayerManager()
  625. if lmgr:
  626. lmgr.toolbars['tools'].Enable('vdigit', enable = True)
  627. lmgr.GetLogWindow().GetProgressBar().SetValue(0)
  628. lmgr.GetLogWindow().WriteCmdLog(_("Editing of vector map <%s> successfully finished") % \
  629. self.mapLayer.GetName(),
  630. switchPage = False)
  631. # re-active layer
  632. item = self.parent.tree.FindItemByData('maplayer', self.mapLayer)
  633. if item and self.parent.tree.IsItemChecked(item):
  634. self.Map.ChangeLayerActive(self.mapLayer, True)
  635. # change cursor
  636. self.MapWindow.SetCursor(self.parent.cursors["default"])
  637. self.MapWindow.pdcVector = None
  638. # close dialogs
  639. for dialog in ('attributes', 'category'):
  640. if self.parent.dialogs[dialog]:
  641. self.parent.dialogs[dialog].Close()
  642. self.parent.dialogs[dialog] = None
  643. del self.digit
  644. del self.MapWindow.digit
  645. self.mapLayer = None
  646. self.MapWindow.redrawAll = True
  647. return True
  648. def UpdateListOfLayers (self, updateTool = False):
  649. """!Update list of available vector map layers.
  650. This list consists only editable layers (in the current mapset)
  651. @param updateTool True to update also toolbar
  652. """
  653. Debug.msg (4, "VDigitToolbar.UpdateListOfLayers(): updateTool=%d" % \
  654. updateTool)
  655. layerNameSelected = None
  656. # name of currently selected layer
  657. if self.mapLayer:
  658. layerNameSelected = self.mapLayer.GetName()
  659. # select vector map layer in the current mapset
  660. layerNameList = []
  661. self.layers = self.Map.GetListOfLayers(l_type = "vector",
  662. l_mapset = grass.gisenv()['MAPSET'])
  663. for layer in self.layers:
  664. if not layer.name in layerNameList: # do not duplicate layer
  665. layerNameList.append (layer.GetName())
  666. if updateTool: # update toolbar
  667. if not self.mapLayer:
  668. value = _('Select vector map')
  669. else:
  670. value = layerNameSelected
  671. if not self.comboid:
  672. if not self.tools or 'selector' in self.tools:
  673. self.combo = wx.ComboBox(self, id = wx.ID_ANY, value = value,
  674. choices = [_('New vector map'), ] + layerNameList, size = (80, -1),
  675. style = wx.CB_READONLY)
  676. self.comboid = self.InsertControl(0, self.combo)
  677. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectMap, self.comboid)
  678. else:
  679. self.combo.SetItems([_('New vector map'), ] + layerNameList)
  680. self.Realize()
  681. return layerNameList
  682. def GetLayer(self):
  683. """!Get selected layer for editing -- MapLayer instance"""
  684. return self.mapLayer