toolbars.py 37 KB

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