graphics.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. """
  2. @package mapwin.graphics
  3. @brief Map display canvas - buffered window.
  4. Classes:
  5. - graphics::GraphicsSet
  6. - graphics::GraphicsSetItem
  7. (C) 2006-2013 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Stepan Turek <stepan.turek seznam.cz> (handlers support, GraphicsSet)
  11. """
  12. from copy import copy
  13. import wx
  14. from core.utils import _
  15. class GraphicsSet:
  16. def __init__(self, parentMapWin, graphicsType,
  17. setStatusFunc=None, drawFunc=None, mapCoords=True):
  18. """Class, which contains instances of GraphicsSetItem and
  19. draws them For description of parameters look at method
  20. RegisterGraphicsToDraw in BufferedWindow class.
  21. """
  22. self.pens = {
  23. "default": wx.Pen(colour=wx.BLACK, width=2, style=wx.SOLID),
  24. "selected": wx.Pen(colour=wx.GREEN, width=2, style=wx.SOLID),
  25. "unused": wx.Pen(colour=wx.LIGHT_GREY, width=2, style=wx.SOLID),
  26. "highest": wx.Pen(colour=wx.RED, width=2, style=wx.SOLID)
  27. }
  28. self.brushes = {
  29. 'default': wx.TRANSPARENT_BRUSH
  30. }
  31. # list contains instances of GraphicsSetItem
  32. self.itemsList = []
  33. self.properties = {}
  34. self.graphicsType = graphicsType
  35. self.parentMapWin = parentMapWin
  36. self.setStatusFunc = setStatusFunc
  37. self.mapCoords = mapCoords
  38. if drawFunc:
  39. self.drawFunc = drawFunc
  40. elif self.graphicsType == "point":
  41. self.properties["size"] = 5
  42. self.properties["text"] = {}
  43. self.properties["text"]['font'] = wx.Font(pointSize=self.properties["size"],
  44. family=wx.FONTFAMILY_DEFAULT,
  45. style=wx.FONTSTYLE_NORMAL,
  46. weight=wx.FONTWEIGHT_NORMAL)
  47. self.properties["text"]['active'] = True
  48. self.drawFunc = self.parentMapWin.DrawCross
  49. elif self.graphicsType == "line":
  50. self.drawFunc = self.parentMapWin.DrawPolylines
  51. elif self.graphicsType == "rectangle":
  52. self.drawFunc = self.parentMapWin.DrawRectangle
  53. elif self.graphicsType == "polygon":
  54. self.drawFunc = self.parentMapWin.DrawPolygon
  55. def Draw(self, pdc):
  56. """Draws all containing items.
  57. :param pdc: device context, where items are drawn
  58. """
  59. itemOrderNum = 0
  60. for item in self.itemsList:
  61. self._clearId(pdc, item.GetId())
  62. if self.setStatusFunc is not None:
  63. self.setStatusFunc(item, itemOrderNum)
  64. if item.GetPropertyVal("hide") is True:
  65. itemOrderNum += 1
  66. continue
  67. if self.graphicsType == "point":
  68. if item.GetPropertyVal("penName"):
  69. self.parentMapWin.pen = self.pens[item.GetPropertyVal("penName")]
  70. else:
  71. self.parentMapWin.pen = self.pens["default"]
  72. if self.mapCoords:
  73. coords = self.parentMapWin.Cell2Pixel(item.GetCoords())
  74. else:
  75. coords = item.GetCoords()
  76. size = self.properties["size"]
  77. label = item.GetPropertyVal("label")
  78. if label is None:
  79. self.properties["text"] = None
  80. else:
  81. self.properties["text"]['coords'] = [coords[0] + size,
  82. coords[1] + size,
  83. size, size]
  84. self.properties["text"]['color'] = self.parentMapWin.pen.GetColour()
  85. self.properties["text"]['text'] = label
  86. self.drawFunc(pdc=pdc, drawid=item.GetId(),
  87. coords=coords,
  88. text=self.properties["text"],
  89. size=self.properties["size"])
  90. elif self.graphicsType == "line":
  91. if item.GetPropertyVal("penName"):
  92. pen = self.pens[item.GetPropertyVal("penName")]
  93. else:
  94. pen = self.pens["default"]
  95. if self.mapCoords:
  96. coords = [self.parentMapWin.Cell2Pixel(coords) for coords in item.GetCoords()]
  97. else:
  98. coords = item.GetCoords()
  99. self.drawFunc(pdc=pdc, pen=pen,
  100. coords=coords, drawid=item.GetId())
  101. elif self.graphicsType == "rectangle":
  102. if item.GetPropertyVal("penName"):
  103. pen = self.pens[item.GetPropertyVal("penName")]
  104. else:
  105. pen = self.pens["default"]
  106. if item.GetPropertyVal("brushName"):
  107. brush = self.brushes[item.GetPropertyVal("brushName")]
  108. else:
  109. brush = self.brushes["default"]
  110. if self.mapCoords:
  111. coords = [self.parentMapWin.Cell2Pixel(coords) for coords in item.GetCoords()]
  112. else:
  113. coords = item.GetCoords()
  114. self.drawFunc(pdc=pdc, pen=pen, brush=brush, drawid=item.GetId(),
  115. point1=coords[0],
  116. point2=coords[1])
  117. elif self.graphicsType == "polygon":
  118. if item.GetPropertyVal("penName"):
  119. pen = self.pens[item.GetPropertyVal("penName")]
  120. else:
  121. pen = self.pens["default"]
  122. if item.GetPropertyVal("brushName"):
  123. brush = self.brushes[item.GetPropertyVal("brushName")]
  124. else:
  125. brush = self.brushes["default"]
  126. if self.mapCoords:
  127. coords = [self.parentMapWin.Cell2Pixel(coords) for coords in item.GetCoords()]
  128. else:
  129. coords = item.GetCoords()
  130. self.drawFunc(pdc=pdc, pen=pen, brush=brush,
  131. coords=coords, drawid=item.GetId())
  132. itemOrderNum += 1
  133. def AddItem(self, coords, penName=None, label=None, hide=False):
  134. """Append item to the list.
  135. Added item is put to the last place in drawing order.
  136. Could be 'point' or 'line' according to graphicsType.
  137. :param coords: list of east, north coordinates (double) of item.
  138. Example:
  139. * point: [1023, 122]
  140. * line: [[10, 12],[20,40],[23, 2334]]
  141. * rectangle: [[10, 12], [33, 45]]
  142. :param penName: the 'default' pen is used if is not defined
  143. :type penName: str
  144. :param label: label, which will be drawn with point. It is
  145. relavant just for 'point' type.
  146. :type label: str
  147. :param hide: if it is True, the item is not drawn when self.Draw
  148. is called. Hidden items are also counted in drawing
  149. order.
  150. :type hide: bool
  151. :return: (GraphicsSetItem) - added item reference
  152. """
  153. item = GraphicsSetItem(coords=coords, penName=penName, label=label,
  154. hide=hide)
  155. self.itemsList.append(item)
  156. return item
  157. def DeleteItem(self, item):
  158. """Deletes item
  159. :param item: (GraphicsSetItem) - item to remove
  160. :return: True if item was removed
  161. :return: False if item was not found
  162. """
  163. try:
  164. self.itemsList.remove(item)
  165. except ValueError:
  166. return False
  167. return True
  168. def GetAllItems(self):
  169. """Returns list of all containing instances of GraphicsSetItem,
  170. in order as they are drawn. If you want to change order of
  171. drawing use: SetItemDrawOrder method.
  172. """
  173. # user can edit objects but not order in list, that is reason,
  174. # why is returned shallow copy of data list it should be used
  175. # SetItemDrawOrder for changing order
  176. return copy(self.itemsList)
  177. def GetItem(self, drawNum):
  178. """Get given item from the list.
  179. :param drawNum: drawing order (index) number of item
  180. :type drawNum: int
  181. :return: instance of GraphicsSetItem which is drawn in drawNum order
  182. :return: False if drawNum was out of range
  183. """
  184. return self.itemsList[drawNum]
  185. def SetPropertyVal(self, propName, propVal):
  186. """Set property value
  187. :param propName: - property name: "size", "text"
  188. - both properties are relevant for "point" type
  189. :type propName: str
  190. :param propVal: property value to be set
  191. :return: True if value was set
  192. :return: False if propName is not "size" or "text" or type is "line"
  193. """
  194. if propName in self.properties:
  195. self.properties[propName] = propVal
  196. return True
  197. return False
  198. def GetPropertyVal(self, propName):
  199. """Get property value
  200. Raises KeyError if propName is not "size" or "text" or type is
  201. "line"
  202. :param propName: property name: "size", "text" both properties
  203. are relevant for "point" type
  204. :type propName: str
  205. :return: value of property
  206. """
  207. if propName in self.properties:
  208. return self.properties[propName]
  209. raise KeyError(_("Property does not exist: %s") % (propName))
  210. def AddPen(self, penName, pen):
  211. """Add pen
  212. :param penName: name of added pen
  213. :type penName: str
  214. :param pen: added pen
  215. :type pen: Wx.Pen
  216. :return: True - if pen was added
  217. :return: False - if pen already exists
  218. """
  219. if penName in self.pens:
  220. return False
  221. self.pens[penName] = pen
  222. return True
  223. def GetPen(self, penName):
  224. """Get existing pen
  225. :param penName: name of pen
  226. :type penName: str
  227. :return: wx.Pen reference if is found
  228. :return: None if penName was not found
  229. """
  230. if penName in self.pens:
  231. return self.pens[penName]
  232. return None
  233. def AddBrush(self, brushName, brush):
  234. """Add brush
  235. :param brushName: name of added brush
  236. :type brushName: str
  237. :param brush: added brush
  238. :type brush: wx.Brush
  239. :return: True - if brush was added
  240. :return: False - if brush already exists
  241. """
  242. if brushName in self.brushes:
  243. return False
  244. self.brushes[brushName] = brush
  245. return True
  246. def GetBrush(self, brushName):
  247. """Get existing brush
  248. :param brushName: name of brush
  249. :type brushName: str
  250. :return: wx.Brush reference if is found
  251. :return: None if brushName was not found
  252. """
  253. if brushName in self.brushes:
  254. return self.brushes[brushName]
  255. return None
  256. def SetItemDrawOrder(self, item, drawNum):
  257. """Set draw order for item
  258. :param item: (GraphicsSetItem)
  259. :param drawNum: drawing order of item to be set
  260. :type drawNum: int
  261. :return: True if order was changed
  262. :return: False if drawNum is out of range or item was not found
  263. """
  264. if drawNum < len(self.itemsList) and drawNum >= 0 and \
  265. item in self.itemsList:
  266. self.itemsList.insert(drawNum, self.itemsList.pop(self.itemsList.index(item)))
  267. return True
  268. return False
  269. def GetItemDrawOrder(self, item):
  270. """Get draw order for given item
  271. :param item: (GraphicsSetItem)
  272. :return: (int) - drawing order of item
  273. :return: None - if item was not found
  274. """
  275. try:
  276. return self.itemsList.index(item)
  277. except ValueError:
  278. return None
  279. def _clearId(self, pdc, drawid):
  280. """Clears old object before drawing new object."""
  281. try:
  282. pdc.ClearId(drawid)
  283. except:
  284. pass
  285. class GraphicsSetItem:
  286. def __init__(self, coords, penName=None, brushName=None, label=None, hide=False):
  287. """Could be point or line according to graphicsType in
  288. GraphicsSet class
  289. :param coords: list of coordinates (double) of item
  290. Example: point: [1023, 122]
  291. line: [[10, 12],[20,40],[23, 2334]]
  292. rectangle: [[10, 12], [33, 45]]
  293. :param penName: if it is not defined 'default' pen is used
  294. :type penName: str
  295. :param brushName: if it is not defined 'default' brush is used
  296. :type brushName: str
  297. :param label: label, which will be drawn with point. It is
  298. relevant just for 'point' type
  299. :type label: str
  300. :param hide: if it is True, item is not drawn Hidden items are
  301. also counted in drawing order in GraphicsSet class.
  302. :type hide: bool
  303. """
  304. self.coords = coords
  305. self.properties = {"penName": penName,
  306. "brushName": brushName,
  307. "hide": hide,
  308. "label": label}
  309. self.id = wx.NewId()
  310. def AddProperty(self, propName):
  311. """Adds new property, to set it, call SetPropertyVal afterwards.
  312. :param propName - name of the newly defined property
  313. :type propName: str
  314. """
  315. if not propName in self.properties:
  316. self.properties[propName] = None
  317. def SetPropertyVal(self, propName, propVal):
  318. """Set property value
  319. :param propName: - property name: "penName", "hide" or "label"
  320. - property "label" is relevant just for 'point' type
  321. - or newly defined property name
  322. :type propName: str
  323. :param propVal: property value to be set
  324. :return: True if value was set
  325. :return: False if propName is not "penName", "hide" or "label"
  326. """
  327. if propName in self.properties:
  328. self.properties[propName] = propVal
  329. return True
  330. return False
  331. def GetPropertyVal(self, propName):
  332. """Get property value
  333. Raises KeyError if propName is not "penName", "hide" or
  334. "label".
  335. :param propName: - property name: "penName", "hide" or "label"
  336. - property "label" is relevant just for 'point' type
  337. :type propName: str
  338. :return: value of property
  339. """
  340. if propName in self.properties:
  341. return self.properties[propName]
  342. raise KeyError(_("Property does not exist: %s") % (propName))
  343. def SetCoords(self, coords):
  344. """Set coordinates of item
  345. :param coords: list of east, north coordinates (double) of item
  346. Example:
  347. * point: [1023, 122]
  348. * line: [[10, 12],[20,40],[23, 2334]]
  349. * rectangle: [[10, 12], [33, 45]]
  350. """
  351. self.coords = coords
  352. def GetCoords(self):
  353. """Get item coordinates
  354. :return: coordinates
  355. """
  356. return self.coords
  357. def GetId(self):
  358. """Get item id (drawing id).
  359. """
  360. return self.id