eventcal.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #see license.txt for license details
  2. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/graphics/widgets/eventcal.py
  3. # Event Calendar widget
  4. # author: Andy Robinson
  5. __version__='3.3.0'
  6. __doc__="""This file is a
  7. """
  8. from reportlab.lib import colors
  9. from reportlab.lib.validators import *
  10. from reportlab.lib.attrmap import *
  11. from reportlab.graphics.shapes import Line, Rect, Polygon, Drawing, Group, String, Circle, Wedge
  12. from reportlab.graphics.charts.textlabels import Label
  13. from reportlab.graphics.widgetbase import Widget
  14. from reportlab.graphics import renderPDF
  15. class EventCalendar(Widget):
  16. def __init__(self):
  17. self.x = 0
  18. self.y = 0
  19. self.width = 300
  20. self.height = 150
  21. self.timeColWidth = None # if declared, use it; otherwise auto-size.
  22. self.trackRowHeight = 20
  23. self.data = [] # list of Event objects
  24. self.trackNames = None
  25. self.startTime = None #displays ALL data on day if not set
  26. self.endTime = None # displays ALL data on day if not set
  27. self.day = 0
  28. # we will keep any internal geometry variables
  29. # here. These are computed by computeSize(),
  30. # which is the first thing done when drawing.
  31. self._talksVisible = [] # subset of data which will get plotted, cache
  32. self._startTime = None
  33. self._endTime = None
  34. self._trackCount = 0
  35. self._colWidths = []
  36. self._colLeftEdges = [] # left edge of each column
  37. def computeSize(self):
  38. "Called at start of draw. Sets various column widths"
  39. self._talksVisible = self.getRelevantTalks(self.data)
  40. self._trackCount = len(self.getAllTracks())
  41. self.computeStartAndEndTimes()
  42. self._colLeftEdges = [self.x]
  43. if self.timeColWidth is None:
  44. w = self.width / (1 + self._trackCount)
  45. self._colWidths = [w] * (1+ self._trackCount)
  46. for i in range(self._trackCount):
  47. self._colLeftEdges.append(self._colLeftEdges[-1] + w)
  48. else:
  49. self._colWidths = [self.timeColWidth]
  50. w = (self.width - self.timeColWidth) / self._trackCount
  51. for i in range(self._trackCount):
  52. self._colWidths.append(w)
  53. self._colLeftEdges.append(self._colLeftEdges[-1] + w)
  54. def computeStartAndEndTimes(self):
  55. "Work out first and last times to display"
  56. if self.startTime:
  57. self._startTime = self.startTime
  58. else:
  59. for (title, speaker, trackId, day, start, duration) in self._talksVisible:
  60. if self._startTime is None: #first one
  61. self._startTime = start
  62. else:
  63. if start < self._startTime:
  64. self._startTime = start
  65. if self.endTime:
  66. self._endTime = self.endTime
  67. else:
  68. for (title, speaker, trackId, day, start, duration) in self._talksVisible:
  69. if self._endTime is None: #first one
  70. self._endTime = start + duration
  71. else:
  72. if start + duration > self._endTime:
  73. self._endTime = start + duration
  74. def getAllTracks(self):
  75. tracks = []
  76. for (title, speaker, trackId, day, hours, duration) in self.data:
  77. if trackId is not None:
  78. if trackId not in tracks:
  79. tracks.append(trackId)
  80. tracks.sort()
  81. return tracks
  82. def getRelevantTalks(self, talkList):
  83. "Scans for tracks actually used"
  84. used = []
  85. for talk in talkList:
  86. (title, speaker, trackId, day, hours, duration) = talk
  87. assert trackId != 0, "trackId must be None or 1,2,3... zero not allowed!"
  88. if day == self.day:
  89. if (((self.startTime is None) or ((hours + duration) >= self.startTime))
  90. and ((self.endTime is None) or (hours <= self.endTime))):
  91. used.append(talk)
  92. return used
  93. def scaleTime(self, theTime):
  94. "Return y-value corresponding to times given"
  95. axisHeight = self.height - self.trackRowHeight
  96. # compute fraction between 0 and 1, 0 is at start of period
  97. proportionUp = ((theTime - self._startTime) / (self._endTime - self._startTime))
  98. y = self.y + axisHeight - (axisHeight * proportionUp)
  99. return y
  100. def getTalkRect(self, startTime, duration, trackId, text):
  101. "Return shapes for a specific talk"
  102. g = Group()
  103. y_bottom = self.scaleTime(startTime + duration)
  104. y_top = self.scaleTime(startTime)
  105. y_height = y_top - y_bottom
  106. if trackId is None:
  107. #spans all columns
  108. x = self._colLeftEdges[1]
  109. width = self.width - self._colWidths[0]
  110. else:
  111. #trackId is 1-based and these arrays have the margin info in column
  112. #zero, so no need to add 1
  113. x = self._colLeftEdges[trackId]
  114. width = self._colWidths[trackId]
  115. lab = Label()
  116. lab.setText(text)
  117. lab.setOrigin(x + 0.5*width, y_bottom+0.5*y_height)
  118. lab.boxAnchor = 'c'
  119. lab.width = width
  120. lab.height = y_height
  121. lab.fontSize = 6
  122. r = Rect(x, y_bottom, width, y_height, fillColor=colors.cyan)
  123. g.add(r)
  124. g.add(lab)
  125. #now for a label
  126. # would expect to color-code and add text
  127. return g
  128. def draw(self):
  129. self.computeSize()
  130. g = Group()
  131. # time column
  132. g.add(Rect(self.x, self.y, self._colWidths[0], self.height - self.trackRowHeight, fillColor=colors.cornsilk))
  133. # track headers
  134. x = self.x + self._colWidths[0]
  135. y = self.y + self.height - self.trackRowHeight
  136. for trk in range(self._trackCount):
  137. wid = self._colWidths[trk+1]
  138. r = Rect(x, y, wid, self.trackRowHeight, fillColor=colors.yellow)
  139. s = String(x + 0.5*wid, y, 'Track %d' % trk, align='middle')
  140. g.add(r)
  141. g.add(s)
  142. x = x + wid
  143. for talk in self._talksVisible:
  144. (title, speaker, trackId, day, start, duration) = talk
  145. r = self.getTalkRect(start, duration, trackId, title + '\n' + speaker)
  146. g.add(r)
  147. return g
  148. def test():
  149. "Make a conference event for day 1 of UP Python 2003"
  150. d = Drawing(400,200)
  151. cal = EventCalendar()
  152. cal.x = 50
  153. cal.y = 25
  154. cal.data = [
  155. # these might be better as objects instead of tuples, since I
  156. # predict a large number of "optionsl" variables to affect
  157. # formatting in future.
  158. #title, speaker, track id, day, start time (hrs), duration (hrs)
  159. # track ID is 1-based not zero-based!
  160. ('Keynote: Why design another programming language?', 'Guido van Rossum', None, 1, 9.0, 1.0),
  161. ('Siena Web Service Architecture', 'Marc-Andre Lemburg', 1, 1, 10.5, 1.5),
  162. ('Extreme Programming in Python', 'Chris Withers', 2, 1, 10.5, 1.5),
  163. ('Pattern Experiences in C++', 'Mark Radford', 3, 1, 10.5, 1.5),
  164. ('What is the Type of std::toupper()', 'Gabriel Dos Reis', 4, 1, 10.5, 1.5),
  165. ('Linguistic Variables: Clear Thinking with Fuzzy Logic ', 'Walter Banks', 5, 1, 10.5, 1.5),
  166. ('lunch, short presentations, vendor presentations', '', None, 1, 12.0, 2.0),
  167. ("CORBA? Isn't that obsolete", 'Duncan Grisby', 1, 1, 14.0, 1.5),
  168. ("Python Design Patterns", 'Duncan Booth', 2, 1, 14.0, 1.5),
  169. ("Inside Security Checks and Safe Exceptions", 'Brandon Bray', 3, 1, 14.0, 1.5),
  170. ("Studying at a Distance", 'Panel Discussion, Panel to include Alan Lenton & Francis Glassborow', 4, 1, 14.0, 1.5),
  171. ("Coding Standards - Given the ANSI C Standard why do I still need a coding Standard", 'Randy Marques', 5, 1, 14.0, 1.5),
  172. ("RESTful Python", 'Hamish Lawson', 1, 1, 16.0, 1.5),
  173. ("Parsing made easier - a radical old idea", 'Andrew Koenig', 2, 1, 16.0, 1.5),
  174. ("C++ & Multimethods", 'Julian Smith', 3, 1, 16.0, 1.5),
  175. ("C++ Threading", 'Kevlin Henney', 4, 1, 16.0, 1.5),
  176. ("The Organisation Strikes Back", 'Alan Griffiths & Sarah Lees', 5, 1, 16.0, 1.5),
  177. ('Birds of a Feather meeting', '', None, 1, 17.5, 2.0),
  178. ('Keynote: In the Spirit of C', 'Greg Colvin', None, 2, 9.0, 1.0),
  179. ('The Infinite Filing Cabinet - object storage in Python', 'Jacob Hallen', 1, 2, 10.5, 1.5),
  180. ('Introduction to Python and Jython for C++ and Java Programmers', 'Alex Martelli', 2, 2, 10.5, 1.5),
  181. ('Template metaprogramming in Haskell', 'Simon Peyton Jones', 3, 2, 10.5, 1.5),
  182. ('Plenty People Programming: C++ Programming in a Group, Workshop with a difference', 'Nico Josuttis', 4, 2, 10.5, 1.5),
  183. ('Design and Implementation of the Boost Graph Library', 'Jeremy Siek', 5, 2, 10.5, 1.5),
  184. ('lunch, short presentations, vendor presentations', '', None, 2, 12.0, 2.0),
  185. ("Building GUI Applications with PythonCard and PyCrust", 'Andy Todd', 1, 2, 14.0, 1.5),
  186. ("Integrating Python, C and C++", 'Duncan Booth', 2, 2, 14.0, 1.5),
  187. ("Secrets and Pitfalls of Templates", 'Nicolai Josuttis & David Vandevoorde', 3, 2, 14.0, 1.5),
  188. ("Being a Mentor", 'Panel Discussion, Panel to include Alan Lenton & Francis Glassborow', 4, 2, 14.0, 1.5),
  189. ("The Embedded C Extensions to C", 'Willem Wakker', 5, 2, 14.0, 1.5),
  190. ("Lightning Talks", 'Paul Brian', 1, 2, 16.0, 1.5),
  191. ("Scripting Java Applications with Jython", 'Anthony Eden', 2, 2, 16.0, 1.5),
  192. ("Metaprogramming and the Boost Metaprogramming Library", 'David Abrahams', 3, 2, 16.0, 1.5),
  193. ("A Common Vendor ABI for C++ -- GCC's why, what and not", 'Nathan Sidwell & Gabriel Dos Reis', 4, 2, 16.0, 1.5),
  194. ("The Timing and Cost of Choices", 'Hubert Matthews', 5, 2, 16.0, 1.5),
  195. ('Birds of a Feather meeting', '', None, 2, 17.5, 2.0),
  196. ('Keynote: The Cost of C &amp; C++ Compatibility', 'Andy Koenig', None, 3, 9.0, 1.0),
  197. ('Prying Eyes: Generic Observer Implementations in C++', 'Andrei Alexandrescu', 1, 2, 10.5, 1.5),
  198. ('The Roadmap to Generative Programming With C++', 'Ulrich Eisenecker', 2, 2, 10.5, 1.5),
  199. ('Design Patterns in C++ and C# for the Common Language Runtime', 'Brandon Bray', 3, 2, 10.5, 1.5),
  200. ('Extreme Hour (XH): (workshop) - Jutta Eckstein and Nico Josuttis', 'Jutta Ecstein', 4, 2, 10.5, 1.5),
  201. ('The Lambda Library : Unnamed Functions for C++', 'Jaako Jarvi', 5, 2, 10.5, 1.5),
  202. ('lunch, short presentations, vendor presentations', '', None, 3, 12.0, 2.0),
  203. ('Reflective Metaprogramming', 'Daveed Vandevoorde', 1, 3, 14.0, 1.5),
  204. ('Advanced Template Issues and Solutions (double session)', 'Herb Sutter',2, 3, 14.0, 3),
  205. ('Concurrent Programming in Java (double session)', 'Angelika Langer', 3, 3, 14.0, 3),
  206. ('What can MISRA-C (2nd Edition) do for us?', 'Chris Hills', 4, 3, 14.0, 1.5),
  207. ('C++ Metaprogramming Concepts and Results', 'Walter E Brown', 5, 3, 14.0, 1.5),
  208. ('Binding C++ to Python with the Boost Python Library', 'David Abrahams', 1, 3, 16.0, 1.5),
  209. ('Using Aspect Oriented Programming for Enterprise Application Integration', 'Arno Schmidmeier', 4, 3, 16.0, 1.5),
  210. ('Defective C++', 'Marc Paterno', 5, 3, 16.0, 1.5),
  211. ("Speakers' Banquet & Birds of a Feather meeting", '', None, 3, 17.5, 2.0),
  212. ('Keynote: The Internet, Software and Computers - A Report Card', 'Alan Lenton', None, 4, 9.0, 1.0),
  213. ('Multi-Platform Software Development; Lessons from the Boost libraries', 'Beman Dawes', 1, 5, 10.5, 1.5),
  214. ('The Stability of the C++ ABI', 'Steve Clamage', 2, 5, 10.5, 1.5),
  215. ('Generic Build Support - A Pragmatic Approach to the Software Build Process', 'Randy Marques', 3, 5, 10.5, 1.5),
  216. ('How to Handle Project Managers: a survival guide', 'Barb Byro', 4, 5, 10.5, 1.5),
  217. ('lunch, ACCU AGM', '', None, 5, 12.0, 2.0),
  218. ('Sauce: An OO recursive descent parser; its design and implementation.', 'Jon Jagger', 1, 5, 14.0, 1.5),
  219. ('GNIRTS ESAC REWOL - Bringing the UNIX filters to the C++ iostream library.', 'JC van Winkel', 2, 5, 14.0, 1.5),
  220. ('Pattern Writing: Live and Direct', 'Frank Buschmann & Kevlin Henney', 3, 5, 14.0, 3.0),
  221. ('The Future of Programming Languages - A Goldfish Bowl', 'Francis Glassborow and friends', 3, 5, 14.0, 1.5),
  222. ('Honey, I Shrunk the Threads: Compile-time checked multithreaded transactions in C++', 'Andrei Alexandrescu', 1, 5, 16.0, 1.5),
  223. ('Fun and Functionality with Functors', 'Lois Goldthwaite', 2, 5, 16.0, 1.5),
  224. ('Agile Enough?', 'Alan Griffiths', 4, 5, 16.0, 1.5),
  225. ("Conference Closure: A brief plenary session", '', None, 5, 17.5, 0.5),
  226. ]
  227. #return cal
  228. cal.day = 1
  229. d.add(cal)
  230. for format in ['pdf']:#,'gif','png']:
  231. out = d.asString(format)
  232. open('eventcal.%s' % format, 'wb').write(out)
  233. print('saved eventcal.%s' % format)
  234. if __name__=='__main__':
  235. test()