graphics.py 11 KB

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