AgendaDocument.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. #
  18. import uno
  19. import traceback
  20. from ..text.TextElement import TextElement
  21. from ..text.TextDocument import TextDocument
  22. from ..text.TextSectionHandler import TextSectionHandler
  23. from ..common.FileAccess import FileAccess
  24. from datetime import datetime
  25. from com.sun.star.text.PlaceholderType import TEXT
  26. from com.sun.star.i18n.NumberFormatIndex import TIME_HHMM, DATE_SYSTEM_LONG
  27. '''
  28. The classes here implement the whole document-functionality of the agenda wizard:
  29. the live-preview and the final "creation" of the document,
  30. when the user clicks "finish". <br/>
  31. <br/>
  32. <h2>Some terminology:<h2/>
  33. items are names or headings. we don't make any distinction.
  34. <br/>
  35. The Agenda Template is used as general "controller"
  36. of the whole document, whereas the two child-classes ItemsTable
  37. and TopicsTable control the item tables (note plural!) and the
  38. topics table (note singular).<br/>
  39. <br/>
  40. Other small classes are used to abstract the handling of cells and text and we
  41. try to use them as components.
  42. <br/><br/>
  43. We tried to keep the Agenda Template as flexible as possible, though there
  44. must be many limitations, because it is generated dynamically.<br/><br/>
  45. To keep the template flexible the following decisions were made:<br/>
  46. 1. Item tables.<br/>
  47. 1.a. there might be arbitrary number of Item tables.<br/>
  48. 1.b. Item tables design (bordewr, background) is arbitrary.<br/>
  49. 1.c. Items text styles are individual,
  50. and use stylelist styles with predefined names.<br/>
  51. As result the following limitations:<br/>
  52. Pairs of Name->value for each item.<br/>
  53. Tables contain *only* those pairs.<br/>
  54. 2. Topics table.<br/>
  55. 2.a. arbitrary structure.<br/>
  56. 2.b. design is arbitrary.<br/>
  57. As result the following limitations:<br/>
  58. No column merge is allowed.<br/>
  59. One compulsory Heading row.<br/>
  60. <br/><br/>
  61. To let the template be flexible, we use a kind of "detection": we look where
  62. the items are read the design of each table, re-applying it after writing the
  63. table.self.xTextDocument
  64. <br/><br/>
  65. A note about threads:<br/>
  66. Many methods here are synchronized, in order to avoid collision made by
  67. events fired too often.
  68. '''
  69. class AgendaDocument(TextDocument):
  70. '''
  71. constructor. The document is *not* loaded here.
  72. only some formal members are set.
  73. '''
  74. def __init__(self, xmsf, agenda, resources, templateConsts, listener):
  75. super(AgendaDocument,self).__init__(xmsf,listener, None,
  76. "WIZARD_LIVE_PREVIEW")
  77. self.agenda = agenda
  78. self.templateConsts = templateConsts
  79. self.resources = resources
  80. self.itemsMap = {}
  81. self.allItems = []
  82. def load(self, templateURL):
  83. # Each template is duplicated. aw-XXX.ott is the template itself
  84. # and XXX.ott is a section link.
  85. self.template = self.calcTemplateName(templateURL)
  86. self.loadAsPreview(templateURL, False)
  87. self.xFrame.ComponentWindow.Enable = False
  88. self.xTextDocument.lockControllers()
  89. self.initialize()
  90. self.initializeData()
  91. self.xTextDocument.unlockControllers()
  92. '''
  93. The agenda templates are in format of aw-XXX.ott
  94. the templates name is then XXX.ott.
  95. This method calculates it.
  96. '''
  97. def calcTemplateName(self, url):
  98. return FileAccess.connectURLs(
  99. FileAccess.getParentDir(url), FileAccess.getFilename(url)[3:])
  100. '''synchronize the document to the model.<br/>
  101. this method rewrites all titles, item tables , and the topics table-
  102. thus synchronizing the document to the data model (CGAgenda).
  103. information (it is only actualized on save) the given list
  104. supplies this information.
  105. '''
  106. def initializeData(self):
  107. for i in self.itemsTables:
  108. try:
  109. i.write()
  110. except Exception:
  111. traceback.print_exc()
  112. self.redrawTitle("txtTitle")
  113. self.redrawTitle("txtDate")
  114. self.redrawTitle("txtTime")
  115. self.redrawTitle("cbLocation")
  116. '''
  117. redraws/rewrites the table which contains the given item
  118. This method is called when the user checks/unchecks an item.
  119. The table is being found, in which the item is, and redrawn.
  120. '''
  121. def redraw(self, itemName):
  122. self.xTextDocument.lockControllers()
  123. try:
  124. # get the table in which the item is...
  125. itemsTable = self.itemsMap[itemName]
  126. # rewrite the table.
  127. itemsTable.write()
  128. except Exception:
  129. traceback.print_exc()
  130. self.xTextDocument.unlockControllers()
  131. '''
  132. checks the data model if the
  133. item corresponding to the given string should be shown
  134. '''
  135. def isShowItem(self, itemName):
  136. if itemName == self.templateConsts.FILLIN_MEETING_TYPE:
  137. return self.agenda.cp_ShowMeetingType
  138. elif itemName == self.templateConsts.FILLIN_READ:
  139. return self.agenda.cp_ShowRead
  140. elif itemName == self.templateConsts.FILLIN_BRING:
  141. return self.agenda.cp_ShowBring
  142. elif itemName == self.templateConsts.FILLIN_NOTES:
  143. return self.agenda.cp_ShowNotes
  144. elif itemName == self.templateConsts.FILLIN_FACILITATOR:
  145. return self.agenda.cp_ShowFacilitator
  146. elif itemName == self.templateConsts.FILLIN_TIMEKEEPER:
  147. return self.agenda.cp_ShowTimekeeper
  148. elif itemName == self.templateConsts.FILLIN_NOTETAKER:
  149. return self.agenda.cp_ShowNotetaker
  150. elif itemName == self.templateConsts.FILLIN_PARTICIPANTS:
  151. return self.agenda.cp_ShowAttendees
  152. elif itemName == self.templateConsts.FILLIN_CALLED_BY:
  153. return self.agenda.cp_ShowCalledBy
  154. elif itemName == self.templateConsts.FILLIN_OBSERVERS:
  155. return self.agenda.cp_ShowObservers
  156. elif itemName == self.templateConsts.FILLIN_RESOURCE_PERSONS:
  157. return self.agenda.cp_ShowResourcePersons
  158. else:
  159. raise ValueError("No such item")
  160. '''itemsCache is a Map containing all agenda item. These are object which
  161. "write themselves" to the table, given a table cursor.
  162. A cache is used in order to reuse the objects, instead of recreate them.
  163. This method fills the cache will all items objects (names and headings).
  164. '''
  165. def initItemsCache(self):
  166. self.itemsCache = {}
  167. # Headings
  168. self.itemsCache[
  169. self.templateConsts.FILLIN_MEETING_TYPE] = \
  170. AgendaItem(self.templateConsts.FILLIN_MEETING_TYPE,
  171. self.resources.itemMeetingType,
  172. PlaceholderElement(
  173. self.resources.reschkMeetingTitle_value,
  174. self.resources.resPlaceHolderHint, self.xTextDocument))
  175. self.itemsCache[
  176. self.templateConsts.FILLIN_BRING] = \
  177. AgendaItem(self.templateConsts.FILLIN_BRING,
  178. self.resources.itemBring,
  179. PlaceholderElement (
  180. self.resources.reschkBring_value,
  181. self.resources.resPlaceHolderHint, self.xTextDocument))
  182. self.itemsCache[
  183. self.templateConsts.FILLIN_READ] = \
  184. AgendaItem (self.templateConsts.FILLIN_READ,
  185. self.resources.itemRead,
  186. PlaceholderElement (
  187. self.resources.reschkRead_value,
  188. self.resources.resPlaceHolderHint, self.xTextDocument))
  189. self.itemsCache[
  190. self.templateConsts.FILLIN_NOTES] = \
  191. AgendaItem (self.templateConsts.FILLIN_NOTES,
  192. self.resources.itemNote,
  193. PlaceholderElement (
  194. self.resources.reschkNotes_value,
  195. self.resources.resPlaceHolderHint, self.xTextDocument))
  196. # Names
  197. self.itemsCache[
  198. self.templateConsts.FILLIN_CALLED_BY] = \
  199. AgendaItem(self.templateConsts.FILLIN_CALLED_BY,
  200. self.resources.itemCalledBy,
  201. PlaceholderElement (
  202. self.resources.reschkConvenedBy_value,
  203. self.resources.resPlaceHolderHint, self.xTextDocument))
  204. self.itemsCache[
  205. self.templateConsts.FILLIN_FACILITATOR] = \
  206. AgendaItem(self.templateConsts.FILLIN_FACILITATOR,
  207. self.resources.itemFacilitator,
  208. PlaceholderElement (
  209. self.resources.reschkPresiding_value,
  210. self.resources.resPlaceHolderHint, self.xTextDocument))
  211. self.itemsCache[
  212. self.templateConsts.FILLIN_PARTICIPANTS] = \
  213. AgendaItem(self.templateConsts.FILLIN_PARTICIPANTS,
  214. self.resources.itemAttendees,
  215. PlaceholderElement(
  216. self.resources.reschkAttendees_value,
  217. self.resources.resPlaceHolderHint, self.xTextDocument))
  218. self.itemsCache[
  219. self.templateConsts.FILLIN_NOTETAKER] = \
  220. AgendaItem(self.templateConsts.FILLIN_NOTETAKER,
  221. self.resources.itemNotetaker,
  222. PlaceholderElement(
  223. self.resources.reschkNoteTaker_value,
  224. self.resources.resPlaceHolderHint, self.xTextDocument))
  225. self.itemsCache[
  226. self.templateConsts.FILLIN_TIMEKEEPER] = \
  227. AgendaItem(self.templateConsts.FILLIN_TIMEKEEPER,
  228. self.resources.itemTimekeeper,
  229. PlaceholderElement(
  230. self.resources.reschkTimekeeper_value,
  231. self.resources.resPlaceHolderHint, self.xTextDocument))
  232. self.itemsCache[
  233. self.templateConsts.FILLIN_OBSERVERS] = \
  234. AgendaItem(self.templateConsts.FILLIN_OBSERVERS,
  235. self.resources.itemObservers,
  236. PlaceholderElement(
  237. self.resources.reschkObservers_value,
  238. self.resources.resPlaceHolderHint, self.xTextDocument))
  239. self.itemsCache[
  240. self.templateConsts.FILLIN_RESOURCE_PERSONS] = \
  241. AgendaItem(self.templateConsts.FILLIN_RESOURCE_PERSONS,
  242. self.resources.itemResource,
  243. PlaceholderElement(
  244. self.resources.reschkResourcePersons_value,
  245. self.resources.resPlaceHolderHint, self.xTextDocument))
  246. '''Initializes a template.<br/>
  247. This method does the following tasks:<br/>
  248. get a Time and Date format for the document, and retrieve the null
  249. date of the document (which is document-specific).<br/>
  250. Initializes the Items Cache map.
  251. Analyses the document:<br/>
  252. -find all "filled-ins" (appear as &gt;xxx&lt; in the document).
  253. -analyze all items sections (and the tables in them).
  254. -locate the titles and actualize them
  255. -analyze the topics table
  256. '''
  257. def initialize(self):
  258. '''
  259. Get the default locale of the document,
  260. and create the date and time formatters.
  261. '''
  262. self.dateUtils = self.DateUtils(self.xMSF, self.xTextDocument)
  263. self.formatter = self.dateUtils.formatter
  264. self.dateFormat = self.dateUtils.getFormat(DATE_SYSTEM_LONG)
  265. self.timeFormat = self.dateUtils.getFormat(TIME_HHMM)
  266. self.initItemsCache()
  267. self.allItems = self.searchFillInItems(0)
  268. self.initializeTitles()
  269. self.initializeItemsSections()
  270. self.textSectionHandler = TextSectionHandler(
  271. self.xTextDocument, self.xTextDocument)
  272. self.topics = Topics(self)
  273. '''
  274. locates the titles (name, location, date, time)
  275. and saves a reference to their Text ranges.
  276. '''
  277. def initializeTitles(self):
  278. auxList = []
  279. for i in self.allItems:
  280. text = i.String.lstrip().lower()
  281. if text == self.templateConsts.FILLIN_TITLE:
  282. self.teTitle = PlaceholderTextElement(
  283. i, self.resources.resPlaceHolderTitle,
  284. self.resources.resPlaceHolderHint, self.xTextDocument)
  285. self.trTitle = i
  286. elif text == self.templateConsts.FILLIN_DATE:
  287. self.teDate = PlaceholderTextElement(
  288. i, self.resources.resPlaceHolderDate,
  289. self.resources.resPlaceHolderHint, self.xTextDocument)
  290. self.trDate = i
  291. elif text == self.templateConsts.FILLIN_TIME:
  292. self.teTime = PlaceholderTextElement(
  293. i, self.resources.resPlaceHolderTime,
  294. self.resources.resPlaceHolderHint, self.xTextDocument)
  295. self.trTime = i
  296. elif text == self.templateConsts.FILLIN_LOCATION:
  297. self.teLocation = PlaceholderTextElement(
  298. i, self.resources.resPlaceHolderLocation,
  299. self.resources.resPlaceHolderHint, self.xTextDocument)
  300. self.trLocation = i
  301. else:
  302. auxList.append(i)
  303. self.allItems = auxList
  304. '''
  305. analyze the item sections in the template.
  306. delegates the analyze of each table to the ItemsTable class.
  307. '''
  308. def initializeItemsSections(self):
  309. sections = self.getSections(
  310. self.xTextDocument, self.templateConsts.SECTION_ITEMS)
  311. # for each section - there is a table...
  312. self.itemsTables = []
  313. for i in sections:
  314. try:
  315. self.itemsTables.append(
  316. ItemsTable(self.getSection(i), self.getTable(i), self))
  317. except Exception:
  318. traceback.print_exc()
  319. raise AttributeError (
  320. "Fatal Error while initializing \
  321. Template: items table in section " + i)
  322. def getSections(self, document, s):
  323. allSections = document.TextSections.ElementNames
  324. return self.getNamesWhichStartWith(allSections, s)
  325. def getSection(self, name):
  326. return self.xTextDocument.TextSections.getByName(name)
  327. def getTable(self, name):
  328. return self.xTextDocument.TextTables.getByName(name)
  329. def redrawTitle(self, controlName):
  330. try:
  331. if controlName == "txtTitle":
  332. self.teTitle.placeHolderText = self.agenda.cp_Title
  333. self.teTitle.write(self.trTitle)
  334. elif controlName == "txtDate":
  335. self.teDate.placeHolderText = \
  336. self.getDateString(self.agenda.cp_Date)
  337. self.teDate.write(self.trDate)
  338. elif controlName == "txtTime":
  339. self.teTime.placeHolderText = self.agenda.cp_Time
  340. self.teTime.write(self.trTime)
  341. elif controlName == "cbLocation":
  342. self.teLocation.placeHolderText = self.agenda.cp_Location
  343. self.teLocation.write(self.trLocation)
  344. else:
  345. raise Exception("No such title control...")
  346. except Exception:
  347. traceback.print_exc()
  348. def getDateString(self, date):
  349. if not date:
  350. return ""
  351. dateObject = datetime.strptime(date, '%d/%m/%y').date()
  352. return self.dateUtils.format(self.dateFormat, dateObject)
  353. def finish(self, topics):
  354. self.createMinutes(topics)
  355. self.deleteHiddenSections()
  356. self.textSectionHandler.removeAllTextSections()
  357. '''
  358. hidden sections exist when an item's section is hidden because the
  359. user specified not to display any items which it contains.
  360. When finishing the wizard removes this sections
  361. entirely from the document.
  362. '''
  363. def deleteHiddenSections(self):
  364. allSections = self.xTextDocument.TextSections.ElementNames
  365. try:
  366. for i in allSections:
  367. self.section = self.getSection(i)
  368. visible = bool(self.section.IsVisible)
  369. if not visible:
  370. self.section.Anchor.String = ""
  371. except Exception:
  372. traceback.print_exc()
  373. '''
  374. create the minutes for the given topics or remove the minutes
  375. section from the document.
  376. If no topics are supplied, or the user specified not to create minutes,
  377. the minutes section will be removed,
  378. @param topicsData supplies PropertyValue arrays containing
  379. the values for the topics.
  380. '''
  381. def createMinutes(self, topicsData):
  382. # if the minutes section should be removed (the
  383. # user did not check "create minutes")
  384. if not self.agenda.cp_IncludeMinutes \
  385. or len(topicsData) <= 1:
  386. try:
  387. minutesAllSection = self.getSection(
  388. self.templateConsts.SECTION_MINUTES_ALL)
  389. minutesAllSection.Anchor.String = ""
  390. except Exception:
  391. traceback.print_exc()
  392. # the user checked "create minutes"
  393. else:
  394. try:
  395. topicStartTime = int(self.agenda.cp_Time)
  396. # first I replace the minutes titles...
  397. self.items = self.searchFillInItems()
  398. itemIndex = 0
  399. for item in self.items:
  400. itemText = item.String.lstrip().lower()
  401. if itemText == \
  402. self.templateConsts.FILLIN_MINUTES_TITLE:
  403. self.fillMinutesItem(
  404. item, self.agenda.cp_Title,
  405. self.resources.resPlaceHolderTitle)
  406. elif itemText == \
  407. self.templateConsts.FILLIN_MINUTES_LOCATION:
  408. self.fillMinutesItem(
  409. item, self.agenda.cp_Location,
  410. self.resources.resPlaceHolderLocation)
  411. elif itemText == \
  412. self.templateConsts.FILLIN_MINUTES_DATE:
  413. self.fillMinutesItem(
  414. item, getDateString(self.agenda.cp_Date),
  415. self.resources.resPlaceHolderDate)
  416. elif itemText == \
  417. self.templateConsts.FILLIN_MINUTES_TIME:
  418. self.fillMinutesItem( item, self.agenda.cp_Time,
  419. self.resources.resPlaceHolderTime)
  420. self.items.clear()
  421. '''
  422. now add minutes for each topic.
  423. The template contains *one* minutes section, so
  424. we first use the one available, and then add a one...
  425. topics data has *always* an empty topic at the end...
  426. '''
  427. for i in xrange(len(topicsData) - 1):
  428. topic = topicsData[i]
  429. items = self.searchFillInItems()
  430. itemIndex = 0
  431. for item in items:
  432. itemText = item.String.lstrip().lower()
  433. if itemText == \
  434. self.templateConsts.FILLIN_MINUTE_NUM:
  435. self.fillMinutesItem(item, topic[0].Value, "")
  436. elif itemText == \
  437. self.templateConsts.FILLIN_MINUTE_TOPIC:
  438. self.fillMinutesItem(item, topic[1].Value, "")
  439. elif itemText == \
  440. self.templateConsts.FILLIN_MINUTE_RESPONSIBLE:
  441. self.fillMinutesItem(item, topic[2].Value, "")
  442. elif itemText == \
  443. self.templateConsts.FILLIN_MINUTE_TIME:
  444. topicTime = 0
  445. try:
  446. topicTime = topic[3].Value
  447. except Exception:
  448. pass
  449. '''
  450. if the topic has no time, we do not
  451. display any time here.
  452. '''
  453. if topicTime == 0 or topicStartTime == 0:
  454. time = topic[3].Value
  455. else:
  456. time = str(topicStartTime) + " - "
  457. topicStartTime += topicTime * 1000
  458. time += str(topicStartTime)
  459. self.fillMinutesItem(item, time, "")
  460. self.textSectionHandler.removeTextSectionbyName(
  461. self.templateConsts.SECTION_MINUTES)
  462. # after the last section we do not insert a one.
  463. if i < len(topicsData) - 2:
  464. self.textSectionHandler.insertTextSection(
  465. self.templateConsts.SECTION_MINUTES,
  466. self.template, False)
  467. except Exception:
  468. traceback.print_exc()
  469. '''given a text range and a text, fills the given
  470. text range with the given text.
  471. If the given text is empty, uses a placeholder with the given
  472. placeholder text.
  473. @param range text range to fill
  474. @param text the text to fill to the text range object.
  475. @param placeholder the placeholder text to use, if the
  476. text argument is empty (null or "")
  477. '''
  478. def fillMinutesItem(self, Range, text, placeholder):
  479. paraStyle = Range.ParaStyleName
  480. Range.setString(text)
  481. Range.ParaStyleName = paraStyle
  482. if text is None or text == "":
  483. if placeholder is not None and not placeholder == "":
  484. placeHolder = self.createPlaceHolder(
  485. self.xTextDocument, placeholder,
  486. self.resources.resPlaceHolderHint)
  487. try:
  488. Range.Start.Text.insertTextContent(
  489. Range.Start, placeHolder, True)
  490. except Exception:
  491. traceback.print_exc()
  492. '''
  493. creates a placeholder field with the given text and given hint.
  494. '''
  495. @classmethod
  496. def createPlaceHolder(self, xmsf, ph, hint):
  497. try:
  498. placeHolder = xmsf.createInstance(
  499. "com.sun.star.text.TextField.JumpEdit")
  500. except Exception:
  501. traceback.print_exc()
  502. return None
  503. placeHolder.PlaceHolder = ph
  504. placeHolder.Hint = hint
  505. placeHolder.PlaceHolderType = uno.Any("short",TEXT)
  506. return placeHolder
  507. def getNamesWhichStartWith(self, allNames, prefix):
  508. v = []
  509. for i in allNames:
  510. if i.startswith(prefix):
  511. v.append(i)
  512. return v
  513. '''
  514. Convenience method for inserting some cells into a table.
  515. '''
  516. @classmethod
  517. def insertTableRows(self, table, start, count):
  518. rows = table.Rows
  519. rows.insertByIndex(start, count)
  520. '''
  521. returns the rows count of this table, assuming
  522. there is no vertical merged cells.
  523. '''
  524. @classmethod
  525. def getRowCount(self, table):
  526. cells = table.getCellNames()
  527. return int(cells[len(cells) - 1][1:])
  528. class ItemsTable(object):
  529. '''
  530. the items in the table.
  531. '''
  532. items = []
  533. table = None
  534. def __init__(self, section, table, agenda):
  535. self.agenda = agenda
  536. ItemsTable.table = table
  537. self.section = section
  538. self.items = []
  539. '''
  540. go through all <*> items in the document
  541. and each one if it is in this table.
  542. If they are, register them to belong here, notice their order
  543. and remove them from the list of all <*> items, so the next
  544. search will be faster.
  545. '''
  546. aux = []
  547. for item in self.agenda.allItems:
  548. t = item.TextTable
  549. if t == ItemsTable.table:
  550. iText = item.String.lower().lstrip()
  551. ai = self.agenda.itemsCache[iText]
  552. if ai is not None:
  553. self.items.append(ai)
  554. self.agenda.itemsMap[iText] = self
  555. else:
  556. aux.append(item)
  557. self.agenda.allItems = aux
  558. '''
  559. link the section to the template. this will restore the original table
  560. with all the items.<br/>
  561. then break the link, to make the section editable.<br/>
  562. then, starting at cell one, write all items that should be visible.
  563. then clear the rest and remove obsolete rows.
  564. If no items are visible, hide the section.
  565. '''
  566. def write(self):
  567. name = self.section.Name
  568. # link and unlink the section to the template.
  569. self.agenda.textSectionHandler.linkSectiontoTemplate(
  570. self.agenda.template, name, self.section)
  571. self.agenda.textSectionHandler.breakLinkOfTextSection(
  572. self.section)
  573. # we need to get an instance after linking
  574. ItemsTable.table = self.agenda.getTable(name)
  575. self.section = self.agenda.getSection(name)
  576. cursor = ItemsTable.table.createCursorByCellName("A1")
  577. # should this section be visible?
  578. visible = False
  579. # write items
  580. cellName = ""
  581. '''
  582. now go through all items that belong to this
  583. table. Check each one against the model. If it should
  584. be displayed, call its write method.
  585. All items are of type AgendaItem which means they write
  586. two cells to the table: a title (text) and a placeholder.
  587. see AgendaItem class below.
  588. '''
  589. for i in self.items:
  590. if self.agenda.isShowItem(i.name):
  591. visible = True
  592. i.table = ItemsTable.table
  593. i.write(cursor)
  594. # I store the cell name which was last written...
  595. cellName = cursor.RangeName
  596. cursor.goRight(1, False)
  597. if visible:
  598. boolean = True
  599. else:
  600. boolean = False
  601. self.section.IsVisible = boolean
  602. if not visible:
  603. return
  604. '''
  605. if the cell that was last written is the current cell,
  606. it means this is the end of the table, so we end here.
  607. (because after getting the cellName above,
  608. I call the goRight method.
  609. If it did not go right, it means it's the last cell.
  610. '''
  611. if cellName == cursor.RangeName:
  612. return
  613. '''
  614. if not, we continue and clear all cells until
  615. we are at the end of the row.
  616. '''
  617. while not cellName == cursor.RangeName and \
  618. not cursor.RangeName.startswith("A"):
  619. cell = ItemsTable.table.getCellByName(cursor.RangeName)
  620. cell.String = ""
  621. cellName = cursor.RangeName
  622. cursor.goRight(1, False)
  623. '''
  624. again: if we are at the end of the table, end here.
  625. '''
  626. if cellName == cursor.RangeName:
  627. return
  628. '''
  629. now before deleting i move the cursor up so it
  630. does not disappear, because it will crash office.
  631. '''
  632. cursor.gotoStart(False)
  633. '''
  634. This class handles the preview of the topics table.
  635. You can call it the controller of the topics table.
  636. It differs from ItemsTable in that it has no data model -
  637. the update is done programmatically.<br/>
  638. <br/>
  639. The decision to make this class a class by its own
  640. was done out of logic reasons and not design/functionality reasons,
  641. since there is anyway only one instance of this class at runtime
  642. it could have also be implemented in the AgendaDocument class
  643. but for clarity and separation I decided to make a sub class for it.
  644. '''
  645. class Topics(object):
  646. '''Analyze the structure of the Topics table.
  647. The structure Must be as follows:<br>
  648. -One Header Row. <br>
  649. -arbitrary number of rows per topic <br>
  650. -arbitrary content in the topics row <br>
  651. -only soft formatting will be restored. <br>
  652. -the topic rows must repeat three times. <br>
  653. -in the topics rows, placeholders for number, topic, responsible,
  654. and duration must be placed.<br><br>
  655. A word about table format: to reconstruct the format of the table we hold
  656. to the following formats: first row (header), topic, and last row.
  657. We hold the format of the last row, because one might wish to give it
  658. a special format, other than the one on the bottom of each topic.
  659. The left and right borders of the whole table are, on the other side,
  660. part of the topics rows format, and need not be preserved separately.
  661. '''
  662. table = None
  663. lastRowFormat = []
  664. rowsPerTopic = None
  665. def __init__(self, agenda):
  666. self.firstRowFormat = []
  667. self.agenda = agenda
  668. self.writtenTopics = -1
  669. try:
  670. Topics.table = self.agenda.getTable(
  671. self.agenda.templateConsts.SECTION_TOPICS)
  672. except Exception:
  673. traceback.print_exc()
  674. raise AttributeError (
  675. "Fatal error while loading template: table " + \
  676. self.agenda.templateConsts.SECTION_TOPICS + " could not load.")
  677. '''
  678. first I store all <*> ranges
  679. which are in the topics table.
  680. I store each <*> range in this - the key
  681. is the cell it is in. Later when analyzing the topic,
  682. cell by cell, I check in this map to know
  683. if a cell contains a <*> or not.
  684. '''
  685. try:
  686. items = {}
  687. for i in self.agenda.allItems:
  688. t = i.TextTable
  689. if t == Topics.table:
  690. cell = i.Cell
  691. iText = cell.CellName
  692. items[iText] = i
  693. '''
  694. in the topics table, there are always one
  695. title row and three topics defined.
  696. So no mutter how many rows a topic takes - we
  697. can restore its structure and format.
  698. '''
  699. rows = self.agenda.getRowCount(Topics.table)
  700. Topics.rowsPerTopic = int((rows - 1) / 3)
  701. firstCell = "A" + str(1 + Topics.rowsPerTopic + 1)
  702. afterLastCell = "A" + str(1 + (Topics.rowsPerTopic * 2) + 1)
  703. # go to the first row of the 2. topic
  704. cursor = Topics.table.createCursorByCellName(firstCell)
  705. # analyze the structure of the topic rows.
  706. while not cursor.RangeName == afterLastCell:
  707. cell = Topics.table.getCellByName(cursor.RangeName)
  708. # first I store the content and para style of the cell
  709. ae = TextElement(cell, cell.String)
  710. ae.write()
  711. # goto next cell.
  712. cursor.goRight(1, False)
  713. except Exception:
  714. traceback.print_exc()
  715. '''rewrites a single cell containing.
  716. This is used in order to refresh the topic/responsible/duration data
  717. in the preview document, in response to a change in the gui (by the user)
  718. Since the structure of the topics table is flexible,
  719. The Topics object, which analyzed the structure of the topics table upon
  720. initialization, refreshes the appropriate cell.
  721. '''
  722. def writeCell(self, row, column, data):
  723. # if the whole row should be written...
  724. if self.writtenTopics < row:
  725. self.writtenTopics += 1
  726. rows = self.agenda.getRowCount(Topics.table)
  727. reqRows = 1 + (row + 1) * Topics.rowsPerTopic
  728. firstRow = reqRows - Topics.rowsPerTopic + 1
  729. diff = reqRows - rows
  730. if diff > 0:
  731. # set the item's text...
  732. self.agenda.insertTableRows(Topics.table, rows, diff)
  733. column = 0
  734. cursor = Topics.table.createCursorByCellName("A" + str(firstRow))
  735. else:
  736. # calculate the table row.
  737. firstRow = 1 + (row * Topics.rowsPerTopic) + 1
  738. cursor = Topics.table.createCursorByCellName("A" + str(firstRow))
  739. # move the cursor to the needed cell...
  740. cursor.goRight(column, False)
  741. xc = Topics.table.getCellByName(cursor.RangeName)
  742. # and write it !
  743. te = TextElement(xc, data[column].Value)
  744. te.write()
  745. '''removes obsolete rows, reducing the
  746. topics table to the given number of topics.
  747. Note this method does only reducing - if
  748. the number of topics given is greater than the
  749. number of actual topics it does *not* add
  750. rows!
  751. Note also that the first topic will never be removed.
  752. If the table contains no topics, the whole section will
  753. be removed upon finishing.
  754. The reason for that is a "table-design" one: the first topic is
  755. maintained in order to be able to add rows with a design of this topic,
  756. and not of the header row.
  757. @param topics the number of topics the table should contain.
  758. @throws Exception
  759. '''
  760. def reduceDocumentTo(self, topics):
  761. # we never remove the first topic...
  762. if topics <= 0:
  763. topics = 1
  764. tableRows = Topics.table.Rows
  765. targetNumOfRows = topics * Topics.rowsPerTopic + 1
  766. if tableRows.Count > targetNumOfRows:
  767. tableRows.removeByIndex(
  768. targetNumOfRows, tableRows.Count - targetNumOfRows)
  769. '''
  770. A Text element which, if the text to write is empty (null or "")
  771. inserts a placeholder instead.
  772. '''
  773. class PlaceholderTextElement(TextElement):
  774. def __init__(self, textRange, placeHolderText_, hint_, xmsf_):
  775. super(PlaceholderTextElement,self).__init__(textRange, "")
  776. self.text = placeHolderText_
  777. self.hint = hint_
  778. self.xmsf = xmsf_
  779. self.xTextContentList = []
  780. def write(self, textRange):
  781. textRange.String = self.placeHolderText
  782. if self.placeHolderText is None or self.placeHolderText == "":
  783. try:
  784. xTextContent = AgendaDocument.createPlaceHolder(
  785. self.xmsf, self.text, self.hint)
  786. self.xTextContentList.append(xTextContent)
  787. textRange.Text.insertTextContent(
  788. textRange.Start, xTextContent, True)
  789. except Exception:
  790. traceback.print_exc()
  791. else:
  792. if self.xTextContentList:
  793. for i in self.xTextContentList:
  794. textRange.Text.removeTextContent(i)
  795. self.xTextContentList = []
  796. '''
  797. An Agenda element which writes no text, but inserts a placeholder, and formats
  798. it using a ParaStyleName.
  799. '''
  800. class PlaceholderElement(object):
  801. def __init__(self, placeHolderText_, hint_, textDocument):
  802. self.placeHolderText = placeHolderText_
  803. self.hint = hint_
  804. self.textDocument = textDocument
  805. def write(self, textRange):
  806. try:
  807. xTextContent = AgendaDocument.createPlaceHolder(
  808. self.textDocument, self.placeHolderText, self.hint)
  809. textRange.Text.insertTextContent(
  810. textRange.Start, xTextContent, True)
  811. except Exception:
  812. traceback.print_exc()
  813. '''
  814. An implementation of AgendaElement which
  815. gets as a parameter a table cursor, and writes
  816. a text to the cell marked by this table cursor, and
  817. a place holder to the next cell.
  818. '''
  819. class AgendaItem(object):
  820. def __init__(self, name_, te, f):
  821. self.name = name_
  822. self.field = f
  823. self.textElement = te
  824. def write(self, tableCursor):
  825. cellname = tableCursor.RangeName
  826. cell = ItemsTable.table.getCellByName(cellname)
  827. cell.String = self.textElement
  828. tableCursor.goRight(1, False)
  829. # second field is actually always null...
  830. # this is a preparation for adding placeholders.
  831. if self.field is not None:
  832. self.field.write(ItemsTable.table.getCellByName(
  833. tableCursor.RangeName))