toolbars.py 36 KB

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