toolbars.py 36 KB

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