toolbars.py 38 KB

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