graphics.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. # list contains instances of GraphicsSetItem
  29. self.itemsList = []
  30. self.properties = {}
  31. self.graphicsType = graphicsType
  32. self.parentMapWin = parentMapWin
  33. self.setStatusFunc = setStatusFunc
  34. self.mapCoords = mapCoords
  35. if drawFunc:
  36. self.drawFunc = drawFunc
  37. elif self.graphicsType == "point":
  38. self.properties["size"] = 5
  39. self.properties["text"] = {}
  40. self.properties["text"]['font'] = wx.Font(pointSize=self.properties["size"],
  41. family=wx.FONTFAMILY_DEFAULT,
  42. style=wx.FONTSTYLE_NORMAL,
  43. weight=wx.FONTWEIGHT_NORMAL)
  44. self.properties["text"]['active'] = True
  45. self.drawFunc = self.parentMapWin.DrawCross
  46. elif self.graphicsType == "line":
  47. self.drawFunc = self.parentMapWin.DrawPolylines
  48. elif self.graphicsType == "rectangle":
  49. self.drawFunc = self.parentMapWin.DrawRectangle
  50. elif self.graphicsType == "polygon":
  51. self.drawFunc = self.parentMapWin.DrawPolygon
  52. def Draw(self, pdc):
  53. """Draws all containing items.
  54. :param pdc: device context, where items are drawn
  55. """
  56. itemOrderNum = 0
  57. for item in self.itemsList:
  58. self._clearId(pdc, item.GetId())
  59. if self.setStatusFunc is not None:
  60. self.setStatusFunc(item, itemOrderNum)
  61. if item.GetPropertyVal("hide") is True:
  62. itemOrderNum += 1
  63. continue
  64. if self.graphicsType == "point":
  65. if item.GetPropertyVal("penName"):
  66. self.parentMapWin.pen = self.pens[item.GetPropertyVal("penName")]
  67. else:
  68. self.parentMapWin.pen = self.pens["default"]
  69. if self.mapCoords:
  70. coords = self.parentMapWin.Cell2Pixel(item.GetCoords())
  71. else:
  72. coords = item.GetCoords()
  73. size = self.properties["size"]
  74. label = item.GetPropertyVal("label")
  75. if label is None:
  76. self.properties["text"] = None
  77. else:
  78. self.properties["text"]['coords'] = [coords[0] + size, coords[1] + size, size, size]
  79. self.properties["text"]['color'] = self.parentMapWin.pen.GetColour()
  80. self.properties["text"]['text'] = label
  81. self.drawFunc(pdc=pdc, drawid=item.GetId(),
  82. coords=coords,
  83. text=self.properties["text"],
  84. size=self.properties["size"])
  85. elif self.graphicsType == "line":
  86. if item.GetPropertyVal("penName"):
  87. pen = self.pens[item.GetPropertyVal("penName")]
  88. else:
  89. pen = self.pens["default"]
  90. if self.mapCoords:
  91. coords = [self.parentMapWin.Cell2Pixel(coords) for coords in item.GetCoords()]
  92. else:
  93. coords = item.GetCoords()
  94. self.drawFunc(pdc=pdc, pen=pen,
  95. coords=coords, drawid=item.GetId())
  96. elif self.graphicsType == "rectangle":
  97. if item.GetPropertyVal("penName"):
  98. pen = self.pens[item.GetPropertyVal("penName")]
  99. else:
  100. pen = self.pens["default"]
  101. if self.mapCoords:
  102. coords = [self.parentMapWin.Cell2Pixel(coords) for coords in item.GetCoords()]
  103. else:
  104. coords = item.GetCoords()
  105. self.drawFunc(pdc=pdc, pen=pen, drawid=item.GetId(),
  106. point1=coords[0],
  107. point2=coords[1])
  108. elif self.graphicsType == "polygon":
  109. if item.GetPropertyVal("penName"):
  110. pen = self.pens[item.GetPropertyVal("penName")]
  111. else:
  112. pen = self.pens["default"]
  113. if self.mapCoords:
  114. coords = [self.parentMapWin.Cell2Pixel(coords) for coords in item.GetCoords()]
  115. else:
  116. coords = item.GetCoords()
  117. self.drawFunc(pdc=pdc, pen=pen,
  118. coords=coords, drawid=item.GetId())
  119. itemOrderNum += 1
  120. def AddItem(self, coords, penName=None, label=None, hide=False):
  121. """Append item to the list.
  122. Added item is put to the last place in drawing order.
  123. Could be 'point' or 'line' according to graphicsType.
  124. :param coords: - list of east, north coordinates (double) of item
  125. Example: point: [1023, 122]
  126. line: [[10, 12],[20,40],[23, 2334]]
  127. rectangle: [[10, 12], [33, 45]]
  128. :param penName: the 'default' pen is used if is not defined
  129. :type penName: str
  130. :param label: label, which will be drawn with point. It is
  131. relavant just for 'point' type.
  132. :type label: str
  133. :param hide: if it is True, the item is not drawn when self.Draw
  134. is called. Hidden items are also counted in drawing
  135. order.
  136. :type hide: bool
  137. :return: (GraphicsSetItem) - added item reference
  138. """
  139. item = GraphicsSetItem(coords=coords, penName=penName, label=label, hide=hide)
  140. self.itemsList.append(item)
  141. return item
  142. def DeleteItem(self, item):
  143. """Deletes item
  144. :param item: (GraphicsSetItem) - item to remove
  145. :return: True if item was removed
  146. :return: False if item was not found
  147. """
  148. try:
  149. self.itemsList.remove(item)
  150. except ValueError:
  151. return False
  152. return True
  153. def GetAllItems(self):
  154. """Returns list of all containing instances of GraphicsSetItem,
  155. in order as they are drawn. If you want to change order of
  156. drawing use: SetItemDrawOrder method.
  157. """
  158. # user can edit objects but not order in list, that is reason,
  159. # why is returned shallow copy of data list it should be used
  160. # SetItemDrawOrder for changing order
  161. return copy(self.itemsList)
  162. def GetItem(self, drawNum):
  163. """Get given item from the list.
  164. :param drawNum: drawing order (index) number of item
  165. :type drawNum: int
  166. :return: instance of GraphicsSetItem which is drawn in drawNum order
  167. :return: False if drawNum was out of range
  168. """
  169. if drawNum < len(self.itemsList) and drawNum >= 0:
  170. return self.itemsList[drawNum]
  171. else:
  172. return False
  173. def SetPropertyVal(self, propName, propVal):
  174. """Set property value
  175. :param propName: - property name: "size", "text"
  176. - both properties are relevant for "point" type
  177. :type propName: str
  178. :param propVal: property value to be set
  179. :return: True if value was set
  180. :return: False if propName is not "size" or "text" or type is "line"
  181. """
  182. if propName in self.properties:
  183. self.properties[propName] = propVal
  184. return True
  185. return False
  186. def GetPropertyVal(self, propName):
  187. """Get property value
  188. Raises KeyError if propName is not "size" or "text" or type is
  189. "line"
  190. :param propName: property name: "size", "text" both properties
  191. are relevant for "point" type
  192. :type propName: str
  193. :return: value of property
  194. """
  195. if propName in self.properties:
  196. return self.properties[propName]
  197. raise KeyError(_("Property does not exist: %s") % (propName))
  198. def AddPen(self, penName, pen):
  199. """Add pen
  200. :param penName: name of added pen
  201. :type penName: str
  202. :param pen: added pen
  203. :type pen: Wx.Pen
  204. :return: True - if pen was added
  205. :return: False - if pen already exists
  206. """
  207. if penName in self.pens:
  208. return False
  209. self.pens[penName] = pen
  210. return True
  211. def GetPen(self, penName):
  212. """Get existing pen
  213. :param penName: name of pen
  214. :type penName: str
  215. :return: wx.Pen reference if is found
  216. :return: None if penName was not found
  217. """
  218. if penName in self.pens:
  219. return self.pens[penName]
  220. return None
  221. def SetItemDrawOrder(self, item, drawNum):
  222. """Set draw order for item
  223. :param item: (GraphicsSetItem)
  224. :param drawNum: drawing order of item to be set
  225. :type drawNum: int
  226. :return: True if order was changed
  227. :return: False if drawNum is out of range or item was not found
  228. """
  229. if drawNum < len(self.itemsList) and drawNum >= 0 and \
  230. item in self.itemsList:
  231. self.itemsList.insert(drawNum, self.itemsList.pop(self.itemsList.index(item)))
  232. return True
  233. return False
  234. def GetItemDrawOrder(self, item):
  235. """Get draw order for given item
  236. :param item: (GraphicsSetItem)
  237. :return: (int) - drawing order of item
  238. :return: None - if item was not found
  239. """
  240. try:
  241. return self.itemsList.index(item)
  242. except ValueError:
  243. return None
  244. def _clearId(self, pdc, drawid):
  245. """Clears old object before drawing new object."""
  246. try:
  247. pdc.ClearId(drawid)
  248. except:
  249. pass
  250. class GraphicsSetItem:
  251. def __init__(self, coords, penName=None, label=None, hide=False):
  252. """Could be point or line according to graphicsType in
  253. GraphicsSet class
  254. :param coords: list of coordinates (double) of item
  255. Example: point: [1023, 122]
  256. line: [[10, 12],[20,40],[23, 2334]]
  257. rectangle: [[10, 12], [33, 45]]
  258. :param penName: if it is not defined 'default' pen is used
  259. :type penName: str
  260. :param label: label, which will be drawn with point. It is
  261. relevant just for 'point' type
  262. :type label: str
  263. :param hide: if it is True, item is not drawn Hidden items are
  264. also counted in drawing order in GraphicsSet class.
  265. :type hide: bool
  266. """
  267. self.coords = coords
  268. self.properties = {"penName": penName,
  269. "hide": hide,
  270. "label": label}
  271. self.id = wx.NewId()
  272. def SetPropertyVal(self, propName, propVal):
  273. """Set property value
  274. :param propName: - property name: "penName", "hide" or "label"
  275. - property "label" is relevant just for 'point' type
  276. :type propName: str
  277. :param propVal: property value to be set
  278. :return: True if value was set
  279. :return: False if propName is not "penName", "hide" or "label"
  280. """
  281. if propName in self.properties:
  282. self.properties[propName] = propVal
  283. return True
  284. return False
  285. def GetPropertyVal(self, propName):
  286. """Get property value
  287. Raises KeyError if propName is not "penName", "hide" or
  288. "label".
  289. :param propName: - property name: "penName", "hide" or "label"
  290. - property "label" is relevant just for 'point' type
  291. :type propName: str
  292. :return: value of property
  293. """
  294. if propName in self.properties:
  295. return self.properties[propName]
  296. raise KeyError(_("Property does not exist: %s") % (propName))
  297. def SetCoords(self, coords):
  298. """Set coordinates of item
  299. :param coords: list of east, north coordinates (double) of item
  300. Example: point: [1023, 122]
  301. line: [[10, 12],[20,40],[23, 2334]]
  302. rectangle: [[10, 12], [33, 45]]
  303. """
  304. self.coords = coords
  305. def GetCoords(self):
  306. """Get item coordinates
  307. :return: coordinates
  308. """
  309. return self.coords
  310. def GetId(self):
  311. """Get item id (drawing id).
  312. """
  313. return self.id