toolbars.py 32 KB

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