graphics.py 11 KB

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