graphics.py 13 KB

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