toolbars.py 39 KB

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