toolbars.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. """
  2. MODULE: toolbar
  3. CLASSES:
  4. * AbstractToolbar
  5. * MapToolbar
  6. * GRToolbar
  7. * VDigitToolbar
  8. * ProfileToolbar
  9. PURPOSE: Toolbars for Map Display window
  10. AUTHORS: The GRASS Development Team
  11. Michael Barton, Martin Landa, Jachym Cepicky
  12. COPYRIGHT: (C) 2007-2008 by the GRASS Development Team
  13. This program is free software under the GNU General Public
  14. License (>=v2). Read the file COPYING that comes with GRASS
  15. for details.
  16. """
  17. import wx
  18. import os, sys
  19. import globalvar
  20. import gcmd
  21. import grassenv
  22. import gdialogs
  23. import vdigit
  24. from vdigit import VDigitSettingsDialog as VDigitSettingsDialog
  25. from debug import Debug as Debug
  26. from icon import Icons as Icons
  27. from preferences import globalSettings as UserSettings
  28. gmpath = os.path.join(globalvar.ETCWXDIR, "icons")
  29. sys.path.append(gmpath)
  30. class AbstractToolbar(object):
  31. """Abstract toolbar class"""
  32. def __init__():
  33. pass
  34. def InitToolbar(self, parent, toolbar, toolData):
  35. """Initialize toolbar, i.e. add tools to the toolbar
  36. @return list of ids (of added tools)
  37. """
  38. for tool in toolData:
  39. self.CreateTool(parent, toolbar, *tool)
  40. self._toolbar = toolbar
  41. def ToolbarData(self):
  42. """Toolbar data"""
  43. return None
  44. def CreateTool(self, parent, toolbar, tool, label, bitmap, kind,
  45. shortHelp, longHelp, handler):
  46. """Add tool to the toolbar
  47. @return id of tool
  48. """
  49. bmpDisabled=wx.NullBitmap
  50. if label:
  51. toolWin = toolbar.AddLabelTool(tool, label, bitmap,
  52. bmpDisabled, kind,
  53. shortHelp, longHelp)
  54. parent.Bind(wx.EVT_TOOL, handler, toolWin)
  55. else: # add separator
  56. toolbar.AddSeparator()
  57. return tool
  58. def GetToolbar(self):
  59. """Get toolbar widget reference"""
  60. return self._toolbar
  61. class MapToolbar(AbstractToolbar):
  62. """
  63. Main Map Display toolbar
  64. """
  65. def __init__(self, mapdisplay, map):
  66. self.mapcontent = map
  67. self.mapdisplay = mapdisplay
  68. self.toolbar = wx.ToolBar(parent=self.mapdisplay, id=wx.ID_ANY)
  69. self.toolbar.SetToolBitmapSize(globalvar.toolbarSize)
  70. self.InitToolbar(self.mapdisplay, self.toolbar, self.ToolbarData())
  71. # optional tools
  72. self.combo = wx.ComboBox(parent=self.toolbar, id=wx.ID_ANY, value='Tools',
  73. choices=['Digitize'], style=wx.CB_READONLY, size=(90, -1))
  74. self.comboid = self.toolbar.AddControl(self.combo)
  75. self.mapdisplay.Bind(wx.EVT_COMBOBOX, self.OnSelect, self.comboid)
  76. # realize the toolbar
  77. self.toolbar.Realize()
  78. def ToolbarData(self):
  79. """Toolbar data"""
  80. self.displaymap = wx.NewId()
  81. self.rendermap = wx.NewId()
  82. self.erase = wx.NewId()
  83. self.pointer = wx.NewId()
  84. self.query = wx.NewId()
  85. self.pan = wx.NewId()
  86. self.zoomin = wx.NewId()
  87. self.zoomout = wx.NewId()
  88. self.zoomback = wx.NewId()
  89. self.zoommenu = wx.NewId()
  90. self.analyze = wx.NewId()
  91. self.dec = wx.NewId()
  92. self.savefile = wx.NewId()
  93. self.printmap = wx.NewId()
  94. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  95. return (
  96. (self.displaymap, "displaymap", Icons["displaymap"].GetBitmap(),
  97. wx.ITEM_NORMAL, Icons["displaymap"].GetLabel(), Icons["displaymap"].GetDesc(),
  98. self.mapdisplay.OnDraw),
  99. (self.rendermap, "rendermap", Icons["rendermap"].GetBitmap(),
  100. wx.ITEM_NORMAL, Icons["rendermap"].GetLabel(), Icons["rendermap"].GetDesc(),
  101. self.mapdisplay.OnRender),
  102. (self.erase, "erase", Icons["erase"].GetBitmap(),
  103. wx.ITEM_NORMAL, Icons["erase"].GetLabel(), Icons["erase"].GetDesc(),
  104. self.mapdisplay.OnErase),
  105. ("", "", "", "", "", "", ""),
  106. (self.pointer, "pointer", Icons["pointer"].GetBitmap(),
  107. wx.ITEM_RADIO, Icons["pointer"].GetLabel(), Icons["pointer"].GetDesc(),
  108. self.mapdisplay.OnPointer),
  109. (self.query, "queryDisplay", Icons["queryDisplay"].GetBitmap(),
  110. wx.ITEM_RADIO, Icons["queryDisplay"].GetLabel(), Icons["queryDisplay"].GetDesc(),
  111. self.mapdisplay.OnQuery),
  112. (self.pan, "pan", Icons["pan"].GetBitmap(),
  113. wx.ITEM_RADIO, Icons["pan"].GetLabel(), Icons["pan"].GetDesc(),
  114. self.mapdisplay.OnPan),
  115. (self.zoomin, "zoom_in", Icons["zoom_in"].GetBitmap(),
  116. wx.ITEM_RADIO, Icons["zoom_in"].GetLabel(), Icons["zoom_in"].GetDesc(),
  117. self.mapdisplay.OnZoomIn),
  118. (self.zoomout, "zoom_out", Icons["zoom_out"].GetBitmap(),
  119. wx.ITEM_RADIO, Icons["zoom_out"].GetLabel(), Icons["zoom_out"].GetDesc(),
  120. self.mapdisplay.OnZoomOut),
  121. (self.zoomback, "zoom_back", Icons["zoom_back"].GetBitmap(),
  122. wx.ITEM_NORMAL, Icons["zoom_back"].GetLabel(), Icons["zoom_back"].GetDesc(),
  123. self.mapdisplay.OnZoomBack),
  124. (self.zoommenu, "zoommenu", Icons["zoommenu"].GetBitmap(),
  125. wx.ITEM_NORMAL, Icons["zoommenu"].GetLabel(), Icons["zoommenu"].GetDesc(),
  126. self.mapdisplay.OnZoomMenu),
  127. ("", "", "", "", "", "", ""),
  128. (self.analyze, "analyze", Icons["analyze"].GetBitmap(),
  129. wx.ITEM_NORMAL, Icons["analyze"].GetLabel(), Icons["analyze"].GetDesc(),
  130. self.mapdisplay.OnAnalyze),
  131. ("", "", "", "", "", "", ""),
  132. (self.dec, "overlay", Icons["overlay"].GetBitmap(),
  133. wx.ITEM_NORMAL, Icons["overlay"].GetLabel(), Icons["overlay"].GetDesc(),
  134. self.mapdisplay.OnDecoration),
  135. ("", "", "", "", "", "", ""),
  136. (self.savefile, "savefile", Icons["savefile"].GetBitmap(),
  137. wx.ITEM_NORMAL, Icons["savefile"].GetLabel(), Icons["savefile"].GetDesc(),
  138. self.mapdisplay.SaveToFile),
  139. (self.printmap, "printmap", Icons["printmap"].GetBitmap(),
  140. wx.ITEM_NORMAL, Icons["printmap"].GetLabel(), Icons["printmap"].GetDesc(),
  141. self.mapdisplay.PrintMenu),
  142. ("", "", "", "", "", "", "")
  143. )
  144. def OnSelect(self, event):
  145. """
  146. Select / enable tool available in tools list
  147. """
  148. tool = event.GetString()
  149. if tool == "Digitize" and not self.mapdisplay.digittoolbar:
  150. self.mapdisplay.AddToolbar("digit")
  151. class GRToolbar(AbstractToolbar):
  152. """
  153. Georectify Display toolbar
  154. """
  155. def __init__(self, mapdisplay, map):
  156. self.mapcontent = map
  157. self.mapdisplay = mapdisplay
  158. self.toolbar = wx.ToolBar(parent=self.mapdisplay, id=wx.ID_ANY)
  159. # self.SetToolBar(self.toolbar)
  160. self.toolbar.SetToolBitmapSize(globalvar.toolbarSize)
  161. self.InitToolbar(self.mapdisplay, self.toolbar, self.ToolbarData())
  162. # realize the toolbar
  163. self.toolbar.Realize()
  164. def ToolbarData(self):
  165. """Toolbar data"""
  166. self.displaymap = wx.NewId()
  167. self.rendermap = wx.NewId()
  168. self.erase = wx.NewId()
  169. self.gcpset = wx.NewId()
  170. self.pan = wx.NewId()
  171. self.zoomin = wx.NewId()
  172. self.zoomout = wx.NewId()
  173. self.zoomback = wx.NewId()
  174. self.zoommenu = wx.NewId()
  175. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  176. return (
  177. (self.displaymap, "displaymap", Icons["displaymap"].GetBitmap(),
  178. wx.ITEM_NORMAL, Icons["displaymap"].GetLabel(), Icons["displaymap"].GetDesc(),
  179. self.mapdisplay.OnDraw),
  180. (self.rendermap, "rendermap", Icons["rendermap"].GetBitmap(),
  181. wx.ITEM_NORMAL, Icons["rendermap"].GetLabel(), Icons["rendermap"].GetDesc(),
  182. self.mapdisplay.OnRender),
  183. (self.erase, "erase", Icons["erase"].GetBitmap(),
  184. wx.ITEM_NORMAL, Icons["erase"].GetLabel(), Icons["erase"].GetDesc(),
  185. self.mapdisplay.OnErase),
  186. ("", "", "", "", "", "", ""),
  187. (self.gcpset, "gcpset", Icons["gcpset"].GetBitmap(),
  188. wx.ITEM_RADIO, Icons["gcpset"].GetLabel(), Icons["gcpset"].GetDesc(),
  189. self.mapdisplay.OnPointer),
  190. (self.pan, "pan", Icons["pan"].GetBitmap(),
  191. wx.ITEM_RADIO, Icons["pan"].GetLabel(), Icons["pan"].GetDesc(),
  192. self.mapdisplay.OnPan),
  193. (self.zoomin, "zoom_in", Icons["zoom_in"].GetBitmap(),
  194. wx.ITEM_RADIO, Icons["zoom_in"].GetLabel(), Icons["zoom_in"].GetDesc(),
  195. self.mapdisplay.OnZoomIn),
  196. (self.zoomout, "zoom_out", Icons["zoom_out"].GetBitmap(),
  197. wx.ITEM_RADIO, Icons["zoom_out"].GetLabel(), Icons["zoom_out"].GetDesc(),
  198. self.mapdisplay.OnZoomOut),
  199. (self.zoomback, "zoom_back", Icons["zoom_back"].GetBitmap(),
  200. wx.ITEM_NORMAL, Icons["zoom_back"].GetLabel(), Icons["zoom_back"].GetDesc(),
  201. self.mapdisplay.OnZoomBack),
  202. (self.zoommenu, "zoommenu", Icons["zoommenu"].GetBitmap(),
  203. wx.ITEM_NORMAL, Icons["zoommenu"].GetLabel(), Icons["zoommenu"].GetDesc(),
  204. self.mapdisplay.OnZoomMenu),
  205. ("", "", "", "", "", "", "")
  206. )
  207. def OnSelect(self, event):
  208. """
  209. Select / enable tool available in tools list
  210. """
  211. tool = event.GetString()
  212. if tool == "Digitize" and not self.mapdisplay.digittoolbar:
  213. self.mapdisplay.AddToolbar("digit")
  214. class VDigitToolbar(AbstractToolbar):
  215. """
  216. Toolbar for digitization
  217. """
  218. def __init__(self, parent, map, layerTree=None):
  219. self.mapcontent = map # Map class instance
  220. self.parent = parent # MapFrame
  221. self.layerTree = layerTree # reference to layer tree associated to map display
  222. # selected map to digitize
  223. self.layerSelectedID = None
  224. self.layers = []
  225. # default action (digitize new point, line, etc.)
  226. self.action = "addLine"
  227. self.type = "point"
  228. self.addString = ""
  229. self.comboid = None
  230. # only one dialog can be open
  231. self.settingsDialog = None
  232. # create toolbars (two rows optionaly)
  233. self.toolbar = []
  234. self.numOfRows = 1 # number of rows for toolbar
  235. for row in range(0, self.numOfRows):
  236. self.toolbar.append(wx.ToolBar(parent=self.parent, id=wx.ID_ANY))
  237. self.toolbar[row].SetToolBitmapSize(globalvar.toolbarSize)
  238. self.toolbar[row].Bind(wx.EVT_TOOL, self.OnTool)
  239. # create toolbar
  240. if self.numOfRows == 1:
  241. rowdata=None
  242. else:
  243. rowdata = row
  244. self.InitToolbar(self.parent, self.toolbar[row], self.ToolbarData(rowdata))
  245. # list of available vector maps
  246. self.UpdateListOfLayers(updateTool=True)
  247. # realize toolbar
  248. for row in range(0, self.numOfRows):
  249. self.toolbar[row].Realize()
  250. # disable undo/redo
  251. self.toolbar[0].EnableTool(self.undo, False)
  252. # toogle to pointer by default
  253. self.OnTool(None)
  254. def ToolbarData(self, row=None):
  255. """
  256. Toolbar data
  257. """
  258. data = []
  259. if row is None or row == 0:
  260. self.addPoint = wx.NewId()
  261. self.addLine = wx.NewId()
  262. self.addBoundary = wx.NewId()
  263. self.addCentroid = wx.NewId()
  264. self.moveVertex = wx.NewId()
  265. self.addVertex = wx.NewId()
  266. self.removeVertex = wx.NewId()
  267. self.splitLine = wx.NewId()
  268. self.editLine = wx.NewId()
  269. self.moveLine = wx.NewId()
  270. self.deleteLine = wx.NewId()
  271. self.additionalTools = wx.NewId()
  272. self.displayCats = wx.NewId()
  273. self.displayAttr = wx.NewId()
  274. self.copyCats = wx.NewId()
  275. data = [("", "", "", "", "", "", ""),
  276. (self.addPoint, "digAddPoint", Icons["digAddPoint"].GetBitmap(),
  277. wx.ITEM_RADIO, Icons["digAddPoint"].GetLabel(), Icons["digAddPoint"].GetDesc(),
  278. self.OnAddPoint),
  279. (self.addLine, "digAddLine", Icons["digAddLine"].GetBitmap(),
  280. wx.ITEM_RADIO, Icons["digAddLine"].GetLabel(), Icons["digAddLine"].GetDesc(),
  281. self.OnAddLine),
  282. (self.addBoundary, "digAddBoundary", Icons["digAddBoundary"].GetBitmap(),
  283. wx.ITEM_RADIO, Icons["digAddBoundary"].GetLabel(), Icons["digAddBoundary"].GetDesc(),
  284. self.OnAddBoundary),
  285. (self.addCentroid, "digAddCentroid", Icons["digAddCentroid"].GetBitmap(),
  286. wx.ITEM_RADIO, Icons["digAddCentroid"].GetLabel(), Icons["digAddCentroid"].GetDesc(),
  287. self.OnAddCentroid),
  288. (self.moveVertex, "digMoveVertex", Icons["digMoveVertex"].GetBitmap(),
  289. wx.ITEM_RADIO, Icons["digMoveVertex"].GetLabel(), Icons["digMoveVertex"].GetDesc(),
  290. self.OnMoveVertex),
  291. (self.addVertex, "digAddVertex", Icons["digAddVertex"].GetBitmap(),
  292. wx.ITEM_RADIO, Icons["digAddVertex"].GetLabel(), Icons["digAddVertex"].GetDesc(),
  293. self.OnAddVertex),
  294. (self.removeVertex, "digRemoveVertex", Icons["digRemoveVertex"].GetBitmap(),
  295. wx.ITEM_RADIO, Icons["digRemoveVertex"].GetLabel(), Icons["digRemoveVertex"].GetDesc(),
  296. self.OnRemoveVertex),
  297. (self.splitLine, "digSplitLine", Icons["digSplitLine"].GetBitmap(),
  298. wx.ITEM_RADIO, Icons["digSplitLine"].GetLabel(), Icons["digSplitLine"].GetDesc(),
  299. self.OnSplitLine),
  300. (self.editLine, "digEditLine", Icons["digEditLine"].GetBitmap(),
  301. wx.ITEM_RADIO, Icons["digEditLine"].GetLabel(), Icons["digEditLine"].GetDesc(),
  302. self.OnEditLine),
  303. (self.moveLine, "digMoveLine", Icons["digMoveLine"].GetBitmap(),
  304. wx.ITEM_RADIO, Icons["digMoveLine"].GetLabel(), Icons["digMoveLine"].GetDesc(),
  305. self.OnMoveLine),
  306. (self.deleteLine, "digDeleteLine", Icons["digDeleteLine"].GetBitmap(),
  307. wx.ITEM_RADIO, Icons["digDeleteLine"].GetLabel(), Icons["digDeleteLine"].GetDesc(),
  308. self.OnDeleteLine),
  309. (self.displayCats, "digDispCats", Icons["digDispCats"].GetBitmap(),
  310. wx.ITEM_RADIO, Icons["digDispCats"].GetLabel(), Icons["digDispCats"].GetDesc(),
  311. self.OnDisplayCats),
  312. (self.copyCats, "digCopyCats", Icons["digCopyCats"].GetBitmap(),
  313. wx.ITEM_RADIO, Icons["digCopyCats"].GetLabel(), Icons["digCopyCats"].GetDesc(),
  314. self.OnCopyCats),
  315. (self.displayAttr, "digDispAttr", Icons["digDispAttr"].GetBitmap(),
  316. wx.ITEM_RADIO, Icons["digDispAttr"].GetLabel(), Icons["digDispAttr"].GetDesc(),
  317. self.OnDisplayAttr),
  318. (self.additionalTools, "digAdditionalTools", Icons["digAdditionalTools"].GetBitmap(),
  319. wx.ITEM_RADIO, Icons["digAdditionalTools"].GetLabel(),
  320. Icons["digAdditionalTools"].GetDesc(),
  321. self.OnAdditionalToolMenu)]
  322. if row is None or row == 1:
  323. self.undo = wx.NewId()
  324. self.settings = wx.NewId()
  325. self.exit = wx.NewId()
  326. data.append(("", "", "", "", "", "", ""))
  327. data.append((self.undo, "digUndo", Icons["digUndo"].GetBitmap(),
  328. wx.ITEM_NORMAL, Icons["digUndo"].GetLabel(), Icons["digUndo"].GetDesc(),
  329. self.OnUndo))
  330. # data.append((self.undo, "digRedo", Icons["digRedo"].GetBitmap(),
  331. # wx.ITEM_NORMAL, Icons["digRedo"].GetLabel(), Icons["digRedo"].GetDesc(),
  332. # self.OnRedo))
  333. data.append((self.settings, "digSettings", Icons["digSettings"].GetBitmap(),
  334. wx.ITEM_NORMAL, Icons["digSettings"].GetLabel(), Icons["digSettings"].GetDesc(),
  335. self.OnSettings))
  336. data.append((self.exit, "digExit", Icons["quit"].GetBitmap(),
  337. wx.ITEM_NORMAL, Icons["digExit"].GetLabel(), Icons["digExit"].GetDesc(),
  338. self.OnExit))
  339. return data
  340. def OnTool(self, event):
  341. """Tool selected -> toggle tool to pointer"""
  342. id = self.parent.maptoolbar.pointer
  343. self.parent.maptoolbar.toolbar.ToggleTool(id, True)
  344. self.parent.maptoolbar.mapdisplay.OnPointer(event)
  345. if event:
  346. event.Skip()
  347. def OnAddPoint(self, event):
  348. """Add point to the vector map Laier"""
  349. Debug.msg (2, "VDigitToolbar.OnAddPoint()")
  350. self.action = "addLine"
  351. self.type = "point"
  352. self.parent.MapWindow.mouse['box'] = 'point'
  353. def OnAddLine(self, event):
  354. """Add line to the vector map layer"""
  355. Debug.msg (2, "VDigitToolbar.OnAddLine()")
  356. self.action = "addLine"
  357. self.type = "line"
  358. self.parent.MapWindow.mouse['box'] = 'line'
  359. self.parent.MapWindow.polycoords = [] # reset temp line
  360. def OnAddBoundary(self, event):
  361. """Add boundary to the vector map layer"""
  362. Debug.msg (2, "VDigitToolbar.OnAddBoundary()")
  363. self.action = "addLine"
  364. self.type = "boundary"
  365. self.parent.MapWindow.mouse['box'] = 'line'
  366. self.parent.MapWindow.polycoords = [] # reset temp line
  367. def OnAddCentroid(self, event):
  368. """Add centroid to the vector map layer"""
  369. Debug.msg (2, "VDigitToolbar.OnAddCentroid()")
  370. self.action = "addLine"
  371. self.type = "centroid"
  372. self.parent.MapWindow.mouse['box'] = 'point'
  373. def OnExit (self, event=None):
  374. """Quit digitization tool"""
  375. # stop editing of the currently selected map layer
  376. try:
  377. self.StopEditing(self.layers[self.layerSelectedID])
  378. except:
  379. pass
  380. # close dialogs if still open
  381. if self.settingsDialog:
  382. self.settingsDialog.OnCancel(None)
  383. if self.parent.dialogs['category']:
  384. self.parent.dialogs['category'].OnCancel(None)
  385. if self.parent.dialogs['attributes']:
  386. self.parent.dialogs['attributes'].OnCancel(None)
  387. # disable the toolbar
  388. self.parent.RemoveToolbar ("digit")
  389. def OnMoveVertex(self, event):
  390. """Move line vertex"""
  391. Debug.msg(2, "Digittoolbar.OnMoveVertex():")
  392. self.action = "moveVertex"
  393. self.parent.MapWindow.mouse['box'] = 'point'
  394. def OnAddVertex(self, event):
  395. """Add line vertex"""
  396. Debug.msg(2, "Digittoolbar.OnAddVertex():")
  397. self.action = "addVertex"
  398. self.parent.MapWindow.mouse['box'] = 'point'
  399. def OnRemoveVertex(self, event):
  400. """Remove line vertex"""
  401. Debug.msg(2, "Digittoolbar.OnRemoveVertex():")
  402. self.action = "removeVertex"
  403. self.parent.MapWindow.mouse['box'] = 'point'
  404. def OnSplitLine(self, event):
  405. """Split line"""
  406. Debug.msg(2, "Digittoolbar.OnSplitLine():")
  407. self.action = "splitLine"
  408. self.parent.MapWindow.mouse['box'] = 'point'
  409. def OnEditLine(self, event):
  410. """Edit line"""
  411. Debug.msg(2, "Digittoolbar.OnEditLine():")
  412. self.action="editLine"
  413. self.parent.MapWindow.mouse['box'] = 'line'
  414. def OnMoveLine(self, event):
  415. """Move line"""
  416. Debug.msg(2, "Digittoolbar.OnMoveLine():")
  417. self.action = "moveLine"
  418. self.parent.MapWindow.mouse['box'] = 'box'
  419. def OnDeleteLine(self, event):
  420. """Delete line"""
  421. Debug.msg(2, "Digittoolbar.OnDeleteLine():")
  422. self.action = "deleteLine"
  423. self.parent.MapWindow.mouse['box'] = 'box'
  424. def OnDisplayCats(self, event):
  425. """Display/update categories"""
  426. Debug.msg(2, "Digittoolbar.OnDisplayCats():")
  427. self.action="displayCats"
  428. self.parent.MapWindow.mouse['box'] = 'point'
  429. def OnDisplayAttr(self, event):
  430. """Display/update attributes"""
  431. Debug.msg(2, "Digittoolbar.OnDisplayAttr():")
  432. self.action="displayAttrs"
  433. self.parent.MapWindow.mouse['box'] = 'point'
  434. def OnCopyCats(self, event):
  435. """Copy categories"""
  436. Debug.msg(2, "Digittoolbar.OnCopyCats():")
  437. self.action="copyCats"
  438. self.parent.MapWindow.mouse['box'] = 'point'
  439. def OnUndo(self, event):
  440. """Undo previous changes"""
  441. self.parent.digit.Undo()
  442. event.Skip()
  443. def EnableUndo(self, enable=True):
  444. """Enable 'Undo' in toolbar
  445. @param enable False for disable
  446. """
  447. if enable:
  448. if self.toolbar[0].GetToolEnabled(self.undo) is False:
  449. self.toolbar[0].EnableTool(self.undo, True)
  450. else:
  451. if self.toolbar[0].GetToolEnabled(self.undo) is True:
  452. self.toolbar[0].EnableTool(self.undo, False)
  453. def OnSettings(self, event):
  454. """Show settings dialog"""
  455. if self.parent.digit is None:
  456. reload(vdigit)
  457. from vdigit import Digit as Digit
  458. self.parent.digit = Digit(mapwindow=self.parent.MapWindow)
  459. if not self.settingsDialog:
  460. self.settingsDialog = VDigitSettingsDialog(parent=self.parent, title=_("Digitization settings"),
  461. style=wx.DEFAULT_DIALOG_STYLE)
  462. self.settingsDialog.Show()
  463. def OnAdditionalToolMenu(self, event):
  464. """Menu for additional tools"""
  465. point = wx.GetMousePosition()
  466. toolMenu = wx.Menu()
  467. # Add items to the menu
  468. copy = wx.MenuItem(toolMenu, wx.ID_ANY, 'Copy features from (background) vector map')
  469. toolMenu.AppendItem(copy)
  470. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnCopy, copy)
  471. flip = wx.MenuItem(toolMenu, wx.ID_ANY, 'Flip selected lines')
  472. toolMenu.AppendItem(flip)
  473. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnFlip, flip)
  474. merge = wx.MenuItem(toolMenu, wx.ID_ANY, 'Merge selected lines')
  475. toolMenu.AppendItem(merge)
  476. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnMerge, merge)
  477. breakL = wx.MenuItem(toolMenu, wx.ID_ANY, 'Break selected lines at intersection')
  478. toolMenu.AppendItem(breakL)
  479. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnBreak, breakL)
  480. snap = wx.MenuItem(toolMenu, wx.ID_ANY, 'Snap selected lines (only to nodes)')
  481. toolMenu.AppendItem(snap)
  482. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnSnap, snap)
  483. connect = wx.MenuItem(toolMenu, wx.ID_ANY, 'Connect two selected lines')
  484. toolMenu.AppendItem(connect)
  485. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnConnect, connect)
  486. query = wx.MenuItem(toolMenu, wx.ID_ANY, 'Query tool')
  487. toolMenu.AppendItem(query)
  488. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnQuery, query)
  489. zbulk = wx.MenuItem(toolMenu, wx.ID_ANY, 'Z bulk-labeling of 3D lines')
  490. toolMenu.AppendItem(zbulk)
  491. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnZBulk, zbulk)
  492. typeconv = wx.MenuItem(toolMenu, wx.ID_ANY, 'Feature type conversion')
  493. toolMenu.AppendItem(typeconv)
  494. self.parent.MapWindow.Bind(wx.EVT_MENU, self.OnTypeConversion, typeconv)
  495. # Popup the menu. If an item is selected then its handler
  496. # will be called before PopupMenu returns.
  497. self.parent.MapWindow.PopupMenu(toolMenu)
  498. toolMenu.Destroy()
  499. def OnCopy(self, event):
  500. """Copy selected features from (background) vector map"""
  501. Debug.msg(2, "Digittoolbar.OnCopy():")
  502. self.action="copyLine"
  503. self.parent.MapWindow.mouse['box'] = 'box'
  504. def OnFlip(self, event):
  505. """Flip selected lines/boundaries"""
  506. Debug.msg(2, "Digittoolbar.OnFlip():")
  507. self.action="flipLine"
  508. self.parent.MapWindow.mouse['box'] = 'box'
  509. def OnMerge(self, event):
  510. """Merge selected lines/boundaries"""
  511. Debug.msg(2, "Digittoolbar.OnMerge():")
  512. self.action="mergeLine"
  513. self.parent.MapWindow.mouse['box'] = 'box'
  514. def OnBreak(self, event):
  515. """Break selected lines/boundaries"""
  516. Debug.msg(2, "Digittoolbar.OnBreak():")
  517. self.action="breakLine"
  518. self.parent.MapWindow.mouse['box'] = 'box'
  519. def OnSnap(self, event):
  520. """Snap selected features"""
  521. Debug.msg(2, "Digittoolbar.OnSnap():")
  522. self.action="snapLine"
  523. self.parent.MapWindow.mouse['box'] = 'box'
  524. def OnConnect(self, event):
  525. """Connect selected lines/boundaries"""
  526. Debug.msg(2, "Digittoolbar.OnConnect():")
  527. self.action="connectLine"
  528. self.parent.MapWindow.mouse['box'] = 'point'
  529. def OnQuery(self, event):
  530. """Query selected lines/boundaries"""
  531. Debug.msg(2, "Digittoolbar.OnQuery(): %s" % \
  532. UserSettings.Get(group='vdigit', key='query', subkey='type'))
  533. self.action="queryLine"
  534. self.parent.MapWindow.mouse['box'] = 'box'
  535. def OnZBulk(self, event):
  536. """Z bulk-labeling selected lines/boundaries"""
  537. Debug.msg(2, "Digittoolbar.OnZBulk():")
  538. self.action="zbulkLine"
  539. self.parent.MapWindow.mouse['box'] = 'line'
  540. def OnTypeConversion(self, event):
  541. """Feature type conversion
  542. Supported conversions:
  543. - point <-> centroid
  544. - line <-> boundary
  545. """
  546. Debug.msg(2, "Digittoolbar.OnTypeConversion():")
  547. self.action="typeConv"
  548. self.parent.MapWindow.mouse['box'] = 'box'
  549. def OnSelectMap (self, event):
  550. """
  551. Select vector map layer for editing
  552. If there is a vector map layer already edited, this action is
  553. firstly terminated. The map layer is closed. After this the
  554. selected map layer activated for editing.
  555. """
  556. if event.GetSelection() == 0: # create new vector map layer
  557. if self.layerSelectedID is not None:
  558. openVectorMap = self.layers[self.layerSelectedID].name.split('@')[0]
  559. else:
  560. openVectorMap = None
  561. mapName = gdialogs.CreateNewVector(self.parent,
  562. exceptMap=openVectorMap)
  563. if mapName:
  564. # add layer to map layer tree
  565. if self.layerTree:
  566. self.layerTree.AddLayer(ltype='vector',
  567. lname=mapName,
  568. lchecked=True,
  569. lopacity=1.0,
  570. lcmd=['d.vect', 'map=%s' % mapName])
  571. vectLayers = self.UpdateListOfLayers(updateTool=True)
  572. selection = vectLayers.index(mapName)
  573. else:
  574. pass # TODO (no Layer Manager)
  575. else:
  576. self.combo.SetValue(_('Select vector map'))
  577. return
  578. else:
  579. selection = event.GetSelection() - 1 # first option is 'New vector map'
  580. if self.layerSelectedID == selection:
  581. return False
  582. if self.layerSelectedID != None: # deactive map layer for editing
  583. self.StopEditing(self.layers[self.layerSelectedID])
  584. # select the given map layer for editing
  585. self.layerSelectedID = selection
  586. self.StartEditing(self.layers[self.layerSelectedID])
  587. event.Skip()
  588. return True
  589. def StartEditing (self, layerSelected):
  590. """
  591. Start editing of selected vector map layer.
  592. @param layerSelectedId id of layer to be edited
  593. @return True on success
  594. @return False on error
  595. """
  596. reload(vdigit)
  597. from vdigit import Digit as Digit
  598. self.parent.digit = Digit(mapwindow=self.parent.MapWindow)
  599. try:
  600. self.layerSelectedID = self.layers.index(layerSelected)
  601. mapLayer = self.layers[self.layerSelectedID]
  602. except:
  603. return False
  604. try:
  605. self.parent.digit.SetMapName(mapLayer.name)
  606. except gcmd.DigitError, e:
  607. self.layerSelectedID = None
  608. print >> sys.stderr, e # wxMessageBox
  609. return False
  610. # update toolbar
  611. self.combo.SetValue (layerSelected.name)
  612. self.parent.maptoolbar.combo.SetValue ('Digitize')
  613. # set initial category number for new features (layer=1), etc.
  614. Debug.msg (4, "VDigitToolbar.StartEditing(): layerSelectedID=%d layer=%s" % \
  615. (self.layerSelectedID, mapLayer.name))
  616. # deactive layer
  617. self.mapcontent.ChangeLayerActive(mapLayer, False)
  618. # change cursor
  619. if self.parent.MapWindow.mouse['use'] == 'pointer':
  620. self.parent.MapWindow.SetCursor(self.parent.cursors["cross"])
  621. # create pseudoDC for drawing the map
  622. self.parent.MapWindow.pdcVector = wx.PseudoDC()
  623. self.parent.digit.driver.SetDevice(self.parent.MapWindow.pdcVector)
  624. # self.parent.MapWindow.UpdateMap()
  625. if not self.parent.MapWindow.resize:
  626. self.parent.MapWindow.UpdateMap(render=True)
  627. return True
  628. def StopEditing (self, layerSelected):
  629. """
  630. Stop editing of selected vector map layer.
  631. """
  632. if self.layers[self.layerSelectedID] == layerSelected:
  633. self.layerSelectedID = None
  634. Debug.msg (4, "VDigitToolbar.StopEditing(): layer=%s" % \
  635. (layerSelected.name))
  636. self.combo.SetValue (_('Select vector map'))
  637. # save changes (only for vdigit)
  638. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vdigit':
  639. if UserSettings.Get(group='vdigit', key='saveOnExit', subkey='enabled') is False:
  640. dlg = wx.MessageDialog(parent=self.parent, message=_("Do you want to save changes "
  641. "in vector map <%s>?") % layerSelected.name,
  642. caption=_("Save changes?"),
  643. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  644. if dlg.ShowModal() == wx.ID_NO:
  645. # revert changes
  646. self.parent.digit.Undo(0)
  647. dlg.Destroy()
  648. self.parent.digit.SetMapName(None) # -> close map
  649. # re-active layer
  650. item = self.parent.tree.FindItemByData('maplayer', layerSelected)
  651. if item and self.parent.tree.IsItemChecked(item):
  652. self.mapcontent.ChangeLayerActive(layerSelected, True)
  653. # change cursor
  654. self.parent.MapWindow.SetCursor(self.parent.cursors["default"])
  655. # disable pseudodc for vector map layer
  656. self.parent.MapWindow.pdcVector = None
  657. self.parent.digit.driver.SetDevice(None)
  658. self.parent.digit.__del__() # FIXME: destructor is not called here (del)
  659. self.parent.digit = None
  660. return True
  661. return False
  662. def UpdateListOfLayers (self, updateTool=False):
  663. """
  664. Update list of available vector map layers.
  665. This list consists only editable layers (in the current mapset)
  666. Optionaly also update toolbar
  667. """
  668. Debug.msg (4, "VDigitToolbar.UpdateListOfLayers(): updateTool=%d" % \
  669. updateTool)
  670. layerNameSelected = None
  671. if self.layerSelectedID != None: # name of currently selected layer
  672. layerNameSelected = self.layers[self.layerSelectedID].name
  673. # select vector map layer in the current mapset
  674. layerNameList = []
  675. self.layers = self.mapcontent.GetListOfLayers(l_type="vector",
  676. l_mapset=grassenv.GetGRASSVariable('MAPSET'))
  677. for layer in self.layers:
  678. if not layer.name in layerNameList: # do not duplicate layer
  679. layerNameList.append (layer.name)
  680. if updateTool: # update toolbar
  681. if self.layerSelectedID == None:
  682. value = _('Select vector map')
  683. else:
  684. value = layerNameSelected
  685. if not self.comboid:
  686. self.combo = wx.ComboBox(self.toolbar[self.numOfRows-1], id=wx.ID_ANY, value=value,
  687. choices=[_('New vector map'), ] + layerNameList, size=(105, -1),
  688. style=wx.CB_READONLY)
  689. self.comboid = self.toolbar[self.numOfRows-1].InsertControl(0, self.combo)
  690. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectMap, self.comboid)
  691. else:
  692. self.combo.SetItems([_('New vector map'), ] + layerNameList)
  693. # update layer index
  694. try:
  695. self.layerSelectedID = layerNameList.index(value)
  696. except ValueError:
  697. self.layerSelectedID = None
  698. self.toolbar[self.numOfRows-1].Realize()
  699. return layerNameList
  700. class ProfileToolbar(AbstractToolbar):
  701. """
  702. Toolbar for digitization
  703. """
  704. def __init__(self, parent, mapdisplay, map):
  705. self.parent = parent
  706. self.mapcontent = map
  707. self.mapdisplay = mapdisplay
  708. self.toolbar = wx.ToolBar(parent=self.mapdisplay, id=wx.ID_ANY)
  709. # self.SetToolBar(self.toolbar)
  710. self.toolbar.SetToolBitmapSize(globalvar.toolbarSize)
  711. self.InitToolbar(self.mapdisplay, self.toolbar, self.ToolbarData())
  712. # realize the toolbar
  713. self.toolbar.Realize()
  714. def ToolbarData(self):
  715. """Toolbar data"""
  716. self.transect = wx.NewId()
  717. self.addraster = wx.NewId()
  718. self.draw = wx.NewId()
  719. self.options = wx.NewId()
  720. self.drag = wx.NewId()
  721. self.zoom = wx.NewId()
  722. self.unzoom = wx.NewId()
  723. self.erase = wx.NewId()
  724. self.save = wx.NewId()
  725. self.printer = wx.NewId()
  726. self.quit = wx.NewId()
  727. # tool, label, bitmap, kind, shortHelp, longHelp, handler
  728. return (
  729. (self.transect, 'transect', Icons["transect"].GetBitmap(),
  730. wx.ITEM_NORMAL, Icons["transect"].GetLabel(), Icons["transect"].GetDesc(),
  731. self.parent.OnDrawTransect),
  732. (self.addraster, 'raster', Icons["addrast"].GetBitmap(),
  733. wx.ITEM_NORMAL, Icons["addrast"].GetLabel(), Icons["addrast"].GetDesc(),
  734. self.parent.OnSelectRaster),
  735. (self.draw, 'profiledraw', Icons["profiledraw"].GetBitmap(),
  736. wx.ITEM_NORMAL, Icons["profiledraw"].GetLabel(), Icons["profiledraw"].GetDesc(),
  737. self.parent.OnCreateProfile),
  738. (self.options, 'options', Icons["profileopt"].GetBitmap(),
  739. wx.ITEM_NORMAL, Icons["profileopt"].GetLabel(), Icons["profileopt"].GetDesc(),
  740. self.parent.ProfileOptionsMenu),
  741. (self.drag, 'drag', Icons['pan'].GetBitmap(),
  742. wx.ITEM_NORMAL, Icons["pan"].GetLabel(), Icons["pan"].GetDesc(),
  743. self.parent.OnDrag),
  744. (self.zoom, 'zoom', Icons['zoom_in'].GetBitmap(),
  745. wx.ITEM_NORMAL, Icons["zoom_in"].GetLabel(), Icons["zoom_in"].GetDesc(),
  746. self.parent.OnZoom),
  747. (self.unzoom, 'unzoom', Icons['zoom_back'].GetBitmap(),
  748. wx.ITEM_NORMAL, Icons["zoom_back"].GetLabel(), Icons["zoom_back"].GetDesc(),
  749. self.parent.OnRedraw),
  750. (self.erase, 'erase', Icons["erase"].GetBitmap(),
  751. wx.ITEM_NORMAL, Icons["erase"].GetLabel(), Icons["erase"].GetDesc(),
  752. self.parent.OnErase),
  753. ("", "", "", "", "", "", ""),
  754. (self.save, 'save', Icons["savefile"].GetBitmap(),
  755. wx.ITEM_NORMAL, Icons["savefile"].GetLabel(), Icons["savefile"].GetDesc(),
  756. self.parent.SaveToFile),
  757. (self.printer, 'print', Icons["printmap"].GetBitmap(),
  758. wx.ITEM_NORMAL, Icons["printmap"].GetLabel(), Icons["printmap"].GetDesc(),
  759. self.parent.PrintMenu),
  760. (self.quit, 'quit', Icons["quit"].GetBitmap(),
  761. wx.ITEM_NORMAL, Icons["quit"].GetLabel(), Icons["quit"].GetDesc(),
  762. self.parent.OnQuit),
  763. )