graphics.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 (string) the 'default' pen is used if is not defined
  129. @param label (string) label, which will be drawn with point. It is
  130. relavant just for 'point' type.
  131. @param hide (bool) If it is True, the item is not drawn
  132. when self.Draw is called. Hidden items are also counted in drawing
  133. order.
  134. @return (GraphicsSetItem) - added item reference
  135. """
  136. item = GraphicsSetItem(coords=coords, penName=penName, label=label, hide=hide)
  137. self.itemsList.append(item)
  138. return item
  139. def DeleteItem(self, item):
  140. """!Deletes item
  141. @param item (GraphicsSetItem) - item to remove
  142. @return True if item was removed
  143. @return False if item was not found
  144. """
  145. try:
  146. self.itemsList.remove(item)
  147. except ValueError:
  148. return False
  149. return True
  150. def GetAllItems(self):
  151. """!Returns list of all containing instances of GraphicsSetItem, in order
  152. as they are drawn. If you want to change order of drawing use: SetItemDrawOrder method.
  153. """
  154. # user can edit objects but not order in list, that is reason,
  155. # why is returned shallow copy of data list it should be used
  156. # SetItemDrawOrder for changing order
  157. return copy(self.itemsList)
  158. def GetItem(self, drawNum):
  159. """!Get given item from the list.
  160. @param drawNum (int) - drawing order (index) number of item
  161. @return instance of GraphicsSetItem which is drawn in drawNum order
  162. @return False if drawNum was out of range
  163. """
  164. if drawNum < len(self.itemsList) and drawNum >= 0:
  165. return self.itemsList[drawNum]
  166. else:
  167. return False
  168. def SetPropertyVal(self, propName, propVal):
  169. """!Set property value
  170. @param propName (string) - property name: "size", "text"
  171. - both properties are relevant for "point" type
  172. @param propVal - property value to be set
  173. @return True - if value was set
  174. @return False - if propName is not "size" or "text" or type is "line"
  175. """
  176. if propName in self.properties:
  177. self.properties[propName] = propVal
  178. return True
  179. return False
  180. def GetPropertyVal(self, propName):
  181. """!Get property value
  182. Raises KeyError if propName is not "size" or "text" or type is
  183. "line"
  184. @param propName (string) property name: "size", "text"
  185. both properties are relevant for "point" type
  186. @return value of property
  187. """
  188. if propName in self.properties:
  189. return self.properties[propName]
  190. raise KeyError(_("Property does not exist: %s") % (propName))
  191. def AddPen(self, penName, pen):
  192. """!Add pen
  193. @param penName (string) - name of added pen
  194. @param pen (wx.Pen) - added pen
  195. @return True - if pen was added
  196. @return False - if pen already exists
  197. """
  198. if penName in self.pens:
  199. return False
  200. self.pens[penName] = pen
  201. return True
  202. def GetPen(self, penName):
  203. """!Get existing pen
  204. @param penName (string) - name of pen
  205. @return wx.Pen reference if is found
  206. @return None if penName was not found
  207. """
  208. if penName in self.pens:
  209. return self.pens[penName]
  210. return None
  211. def SetItemDrawOrder(self, item, drawNum):
  212. """!Set draw order for item
  213. @param item (GraphicsSetItem)
  214. @param drawNum (int) - drawing order of item to be set
  215. @return True - if order was changed
  216. @return False - if drawNum is out of range or item was not found
  217. """
  218. if drawNum < len(self.itemsList) and drawNum >= 0 and \
  219. item in self.itemsList:
  220. self.itemsList.insert(drawNum, self.itemsList.pop(self.itemsList.index(item)))
  221. return True
  222. return False
  223. def GetItemDrawOrder(self, item):
  224. """!Get draw order for given item
  225. @param item (GraphicsSetItem)
  226. @return (int) - drawing order of item
  227. @return None - if item was not found
  228. """
  229. try:
  230. return self.itemsList.index(item)
  231. except ValueError:
  232. return None
  233. def _clearId(self, pdc, drawid):
  234. """!Clears old object before drawing new object."""
  235. try:
  236. pdc.ClearId(drawid)
  237. except:
  238. pass
  239. class GraphicsSetItem:
  240. def __init__(self, coords, penName=None, label=None, hide=False):
  241. """!Could be point or line according to graphicsType in
  242. GraphicsSet class
  243. @param coords - list of coordinates (double) of item
  244. Example: point: [1023, 122]
  245. line: [[10, 12],[20,40],[23, 2334]]
  246. rectangle: [[10, 12], [33, 45]]
  247. @param penName (string) if it is not defined 'default' pen is used
  248. @param label (string) label, which will be drawn with point. It is
  249. relevant just for 'point' type
  250. @param hide (bool) if it is True, item is not drawn
  251. Hidden items are also counted in drawing order in
  252. GraphicsSet class.
  253. """
  254. self.coords = coords
  255. self.properties = {"penName": penName,
  256. "hide": hide,
  257. "label": label}
  258. self.id = wx.NewId()
  259. def SetPropertyVal(self, propName, propVal):
  260. """!Set property value
  261. @param propName (string) - property name: "penName", "hide" or "label"
  262. - property "label" is relevant just for 'point' type
  263. @param propVal - property value to be set
  264. @return True - if value was set
  265. @return False - if propName is not "penName", "hide" or "label"
  266. """
  267. if propName in self.properties:
  268. self.properties[propName] = propVal
  269. return True
  270. return False
  271. def GetPropertyVal(self, propName):
  272. """!Get property value
  273. Raises KeyError if propName is not "penName", "hide" or
  274. "label".
  275. @param propName (string) - property name: "penName", "hide" or "label"
  276. - property "label" is relevant just for 'point' type
  277. @return value of property
  278. """
  279. if propName in self.properties:
  280. return self.properties[propName]
  281. raise KeyError(_("Property does not exist: %s") % (propName))
  282. def SetCoords(self, coords):
  283. """!Set coordinates of item
  284. @param coords - list of east, north coordinates (double) of item
  285. Example: point: [1023, 122]
  286. line: [[10, 12],[20,40],[23, 2334]]
  287. rectangle: [[10, 12], [33, 45]]
  288. """
  289. self.coords = coords
  290. def GetCoords(self):
  291. """!Get item coordinates
  292. @returns coordinates
  293. """
  294. return self.coords
  295. def GetId(self):
  296. """!Get item id (drawing id).
  297. """
  298. return self.id