/GUI/Generic/GCanvases.py

https://bitbucket.org/alsh/pygui-mirror · Python · 277 lines · 198 code · 62 blank · 17 comment · 4 complexity · 3f06368373c62ba34ba5ddcaf79f6674 MD5 · raw file

  1. #
  2. # Python GUI - Drawing - Generic
  3. #
  4. from StdColors import black, white
  5. from StdFonts import application_font
  6. from Properties import Properties, overridable_property
  7. class Canvas(Properties):
  8. _default_forecolor = black
  9. _default_backcolor = white
  10. pencolor = overridable_property('pencolor', "Current color for stroking paths.")
  11. fillcolor = overridable_property('fillcolor', "Current color for filling paths.")
  12. textcolor = overridable_property('textcolor', "Current color for drawint text.")
  13. forecolor = overridable_property('forecolor', "Sets pen, fill and text colors to the same color.")
  14. backcolor = overridable_property('backcolor', "Current color for erasing regions.")
  15. pensize = overridable_property('pensize', "Width of pen for framing and stroking.")
  16. font = overridable_property('font', "Font for drawing text.")
  17. current_point = overridable_property('current_point', "The current point, or None.")
  18. #forecolor = overridable_property('forecolor', "Sets both pencolor and fillcolor.")
  19. def __init__(self):
  20. self.newpath()
  21. def initgraphics(self):
  22. self.set_forecolor(self._default_forecolor)
  23. self.set_backcolor(self._default_backcolor)
  24. self.set_pensize(1)
  25. self.set_font(application_font)
  26. def set_forecolor(self, c):
  27. self.pencolor = c
  28. self.fillcolor = c
  29. self.textcolor = c
  30. def rmoveto(self, dx, dy):
  31. x0, y0 = self._current_point()
  32. self.moveto(x0 + dx, y0 + dy)
  33. def rlineto(self, dx, dy):
  34. x0, y0 = self.current_point
  35. self.lineto(x0 + dx, y0 + dy)
  36. def curve(self, sp, cp1, cp2, ep):
  37. self.moveto(sp)
  38. self.curveto(cp1, cp2, ep)
  39. def rcurveto(self, cp1, cp2, ep):
  40. x0, y0 = self.current_point
  41. x1, y1 = cp1
  42. x2, y2 = cp2
  43. x3, y3 = ep
  44. self.curveto(
  45. (x0 + x1, y0 + y1),
  46. (x0 + x2, y0 + y2),
  47. (x0 + x3, y0 + y3))
  48. def fill_stroke(self):
  49. self.fill()
  50. self.stroke()
  51. # Rectangles
  52. def _pen_inset_rect(self, rect):
  53. l, t, r, b = rect
  54. p = 0.5 * self.pensize
  55. return (l + p, t + p, r - p, b - p)
  56. def rect(self, rect):
  57. l, t, r, b = rect
  58. self.moveto(l, t)
  59. self.lineto(r, t)
  60. self.lineto(r, b)
  61. self.lineto(l, b)
  62. self.closepath()
  63. def rect_frame(self, rect):
  64. self.rect(self._pen_inset_rect(rect))
  65. def fill_rect(self, rect):
  66. self.newpath()
  67. self.rect(rect)
  68. self.fill()
  69. def stroke_rect(self, rect):
  70. self.newpath()
  71. self.rect(rect)
  72. self.stroke()
  73. def frame_rect(self, rect):
  74. self.newpath()
  75. self.rect_frame(rect)
  76. self.stroke()
  77. def fill_stroke_rect(self, rect):
  78. self.rect_path(rect)
  79. self.fill_stroke()
  80. def fill_frame_rect(self, rect):
  81. self.fill_rect(rect)
  82. self.frame_rect(rect)
  83. def erase_rect(self, rect):
  84. self.newpath()
  85. self.rect(rect)
  86. self.erase()
  87. # Ovals
  88. def oval_frame(self, rect):
  89. self.oval(self._pen_inset_rect(rect))
  90. def fill_oval(self, rect):
  91. self.newpath()
  92. self.oval_frame(rect)
  93. self.fill()
  94. def stroke_oval(self, rect):
  95. self.newpath()
  96. self.oval(rect)
  97. self.stroke()
  98. def frame_oval(self, rect):
  99. self.newpath()
  100. self.oval_frame(rect)
  101. self.stroke()
  102. def fill_stroke_oval(self, rect):
  103. self.newpath()
  104. self.oval(rect)
  105. self.fill_stroke()
  106. def fill_frame_oval(self, rect):
  107. self.fill_oval(rect)
  108. self.frame_oval()
  109. def erase_oval(self, rect):
  110. self.newpath()
  111. self.oval(rect)
  112. self.erase()
  113. # Arcs
  114. def _arc_path(self, c, r, a0, a1):
  115. # x, y = c
  116. # a0r = a0 * deg
  117. # x0 = x + r * cos(a0r)
  118. # y0 = y + r * sin(a0r)
  119. self.newpath()
  120. # self.moveto(x0, y0)
  121. self.arc(c, r, a0, a1)
  122. def _arc_frame_path(self, c, r, a0, a1):
  123. self._arc_path(c, r - 0.5 * self.pensize, a0, a1)
  124. def stroke_arc(self, c, r, a0, a1):
  125. self._arc_path(c, r, a0, a1)
  126. self.stroke()
  127. def frame_arc(self, c, r, a0, a1):
  128. self._arc_frame_path(c, r, a0, a1)
  129. self.stroke()
  130. # Wedges
  131. def wedge(self, c, r, a0, a1):
  132. self.moveto(*c)
  133. self.arc(c, r, a0, a1)
  134. self.closepath()
  135. def fill_wedge(self, c, r, a0, a1):
  136. self.newpath()
  137. self.wedge(c, r, a0, a1)
  138. self.fill()
  139. def stroke_wedge(self, c, r, a0, a1):
  140. self.newpath()
  141. self.wedge(c, r, a0, a1)
  142. self.stroke()
  143. def fill_stroke_wedge(self, c, r, a0, a1):
  144. self.newpath()
  145. self.wedge(c, r, a0, a1)
  146. self.fill_stroke()
  147. def erase_wedge(self, c, r, a0, a1):
  148. self.newpath()
  149. self.wedge(c, r, a0, a1)
  150. self.erase()
  151. # Polylines
  152. def lines(self, points):
  153. point_iter = iter(points)
  154. self.moveto(*point_iter.next())
  155. for p in point_iter:
  156. self.lineto(*p)
  157. def linesto(self, points):
  158. for p in points:
  159. self.lineto(*p)
  160. def stroke_lines(self, points):
  161. self.newpath()
  162. self.lines(points)
  163. self.stroke()
  164. # Polycurves
  165. def curves(self, points):
  166. self.moveto(*points[0])
  167. for i in xrange(1, len(points), 3):
  168. self.curveto(*points[i:i+3])
  169. def curvesto(self, points):
  170. for i in xrange(0, len(points), 3):
  171. self.curveto(*points[i:i+3])
  172. def stroke_curves(self, points):
  173. self.newpath()
  174. self.curves(points)
  175. self.stroke()
  176. # Polygons
  177. def poly(self, points):
  178. self.lines(points)
  179. self.closepath()
  180. def fill_poly(self, points):
  181. self.newpath()
  182. self.poly(points)
  183. self.fill()
  184. def stroke_poly(self, points):
  185. self.newpath()
  186. self.poly(points)
  187. self.stroke()
  188. def fill_stroke_poly(self, points):
  189. self.newpath()
  190. self.poly(points)
  191. self.fill_stroke()
  192. def erase_poly(self, points):
  193. self.newpath()
  194. self.poly(points)
  195. self.erase()
  196. # Loops
  197. def loop(self, points):
  198. self.curves(points)
  199. self.closepath()
  200. def fill_loop(self, points):
  201. self.newpath()
  202. self.loop(points)
  203. self.fill()
  204. def stroke_loop(self, points):
  205. self.newpath()
  206. self.loop(points)
  207. self.stroke()
  208. def fill_stroke_loop(self, points):
  209. self.newpath()
  210. self.loop(points)
  211. self.fill_stroke()
  212. def erase_loop(self, points):
  213. self.newpath()
  214. self.loop(points)
  215. self.erase()