TopicsControl.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. from ..ui.ControlScroller import ControlScroller, PropertyNames, traceback, \
  20. HelpIds
  21. from .AgendaWizardDialogConst import HID
  22. from ..common.Properties import Properties
  23. from ..ui.event.CommonListener import FocusListenerProcAdapter, \
  24. KeyListenerProcAdapter
  25. from com.sun.star.awt.Key import DOWN, UP, TAB
  26. from com.sun.star.awt.KeyModifier import SHIFT, MOD1
  27. '''
  28. This class implements the UI functionality of the topics scroller control.
  29. <br/>
  30. During development, there has been a few changes which were not *fully* done
  31. mainly in converting the topics and time boxes
  32. from combobox and time box to normal textboxes,
  33. so in the code they might be referenced as combobox or timebox. This should be
  34. rather understood as topicstextbox and timetextbox.<br/><br/>
  35. Important behaviour of this control is that there is always a
  36. blank row at the end, in which the user can enter data.<br/>
  37. Once the row is not blank (thus, the user entered data...),
  38. a new blank row is added.<br/>
  39. Once the user removes the last *unempty* row, binsertRowy deleting its data, it becomes
  40. the *last empty row* and the one after is being automatically removed.<br/><br/>
  41. The control shows 5 rows at a time.<br/>
  42. If, for example, only 2 rows exist (from which the 2ed one is empty...)
  43. then the other three rows, which do not exist in the data model, are disabled.
  44. <br/>
  45. The following other functionality is implemented:
  46. <br/>
  47. 0. synchronizing data between controls, data model and live preview.
  48. 1. Tab scrolling.<br/>
  49. 2. Keyboard scrolling.<br/>
  50. 3. Removing rows and adding new rows.<br/>
  51. 4. Moving rows up and down. <br/>
  52. <br/>
  53. This control relays on the ControlScroller control which uses the following
  54. Data model:<br/>
  55. 1. It uses a vector, whose members are arrays of PropertyValue.<br/>
  56. 2. Each array represents a row.<br/>
  57. (Note: the Name and Value members of the PropertyValue object are being used)
  58. 3. Each property Value represents a value
  59. for a single control with the following rules:<br/>
  60. 3. a. the Value of the property is used for as value
  61. of the controls (usually text).<br/>
  62. 3. b. the Name of the property is used to map values
  63. to UI controls in the following manner:<br/>
  64. 3. b. 1. only the Name of the first X Rows is regarded,
  65. where X is the number of visible rows (in the ainsertRowgenda wizard this would be 5,
  66. since 5 topic rows are visible on the dialog).<br/>
  67. 3. b. 2. The Names of the first X (or 5...) rows are the names
  68. of the UI Controls to hold values. When the control scroller scrolls,
  69. it looks at the first 5 rows and uses the names specified there to map the
  70. current values to the specified controls. <br/>
  71. This data model makes the following limitations on the implementation:
  72. When moving rows, only the values should be moved. The Rows objects,
  73. which contain also the Names of the controls should not be switched. <br/>
  74. also when deleting or inserting rows, attention should be paid that no rows
  75. should be removed or inserted. Instead, only the Values should rotate. <br/><br/>
  76. To save the topics in the registry a ConfigSet of objects of type CGTopic is
  77. being used.
  78. This one is not synchronized "live", since it is unnecessary... instead, it is
  79. synchronized on call, before the settings should be saved.
  80. '''
  81. class TopicsControl(ControlScroller):
  82. LABEL = "lblTopicCnt_"
  83. TOPIC = "txtTopicTopic_"
  84. RESP = "cbTopicResp_"
  85. TIME = "txtTopicTime_"
  86. LABEL_PROPS = (PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL,
  87. PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y,
  88. PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX,
  89. PropertyNames.PROPERTY_WIDTH)
  90. TEXT_PROPS = (PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL,
  91. PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y,
  92. PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX,
  93. PropertyNames.PROPERTY_WIDTH)
  94. def __init__(self, dialog, xmsf, agenda):
  95. try:
  96. super(TopicsControl, self).__init__(
  97. dialog, xmsf, 5, 92, 38, 212, 5, 18, HID + 32)
  98. self.dialog = dialog
  99. #fill preview's table
  100. self.initializeScrollFields(agenda)
  101. #fill gui's table
  102. self.fillupControls(True)
  103. self.nscrollvalue = 0
  104. self.lastFocusRow = 0
  105. self.lastFocusControl = None
  106. # set some focus listeners for TAB scroll down and up...
  107. # prepare scroll down on tab press...
  108. self.lastTime = \
  109. self.ControlGroupVector[self.nblockincrement - 1].timebox
  110. self.lastTime.addKeyListener(KeyListenerProcAdapter(
  111. self.lastControlKeyPressed))
  112. #prepare scroll up on tab press...
  113. self.firstTopic = self.ControlGroupVector[0].textbox
  114. self.firstTopic.addKeyListener(KeyListenerProcAdapter(
  115. self.firstControlKeyPressed))
  116. self.enableButtons()
  117. except Exception:
  118. traceback.print_exc()
  119. '''
  120. initializes the data of the control.
  121. '''
  122. def initializeScrollFields(self, agenda):
  123. # create a row for each topic with the given values...
  124. for index,item in enumerate(agenda.cp_Topics.childrenList):
  125. row = self.newRow(index)
  126. item.setDataToRow(row)
  127. # a parent class method
  128. self.registerControlGroup(row, index)
  129. self.updateDocumentRow(index)
  130. # inserts a blank row at the end...
  131. self.insertRowAtEnd()
  132. '''
  133. Insert a blank (empty) row
  134. as last row of the control.
  135. The control has always a blank row at the
  136. end, which enables the user to enter data...
  137. '''
  138. def insertRowAtEnd(self):
  139. l = len(self.scrollfields)
  140. self.registerControlGroup(self.newRow(l), l)
  141. self.setTotalFieldCount(l + 1)
  142. # if the new row is visible, it must have been disabled
  143. # so it should be now enabled...
  144. if l - self.nscrollvalue < self.nblockincrement:
  145. self.ControlGroupVector[l - self.nscrollvalue].\
  146. setEnabled(True)
  147. def saveTopics(self, agenda):
  148. # last row is always empty
  149. agenda.cp_Topics.childrenList = self.scrollfields[:-1]
  150. '''
  151. remove the last row
  152. '''
  153. def removeLastRow(self):
  154. l = len(self.scrollfields)
  155. # if we should scroll up...
  156. if (l - self.nscrollvalue) >= 1 \
  157. and (l - self.nscrollvalue) <= self.nblockincrement \
  158. and self.nscrollvalue > 0:
  159. while (l - self.nscrollvalue >= 1) \
  160. and l - self.nscrollvalue <= self.nblockincrement \
  161. and self.nscrollvalue > 0:
  162. self.setScrollValue(self.nscrollvalue - 1)
  163. # if we should disable a row...
  164. elif self.nscrollvalue == 0 and l - 1 < self.nblockincrement:
  165. self.ControlGroupVector[l - 1].setEnabled(False)
  166. self.unregisterControlGroup(l - 1)
  167. self.setTotalFieldCount(l - 1)
  168. '''
  169. in order to use the "move up", "down" "insert" and "remove" buttons,
  170. we track the last control the gained focus, in order to know which
  171. row should be handled.
  172. @param fe
  173. '''
  174. def focusGained(self, fe):
  175. xc = fe.Source
  176. self.focusGained2(xc)
  177. '''
  178. Sometimes I set the focus programmatically to a control
  179. (for example when moving a row up or down, the focus should move
  180. with it).
  181. In such cases, no VCL event is being triggered so it must
  182. be called programmatically.
  183. This is done by this method.
  184. @param control
  185. '''
  186. def focusGained2(self, control):
  187. try:
  188. #calculate in which row we are...
  189. name = control.Model.Name
  190. num = name[name.index("_") + 1:]
  191. self.lastFocusRow = int(num) + self.nscrollvalue
  192. self.lastFocusControl = control
  193. # enable/disable the buttons...
  194. self.enableButtons()
  195. except Exception:
  196. traceback.print_exc()
  197. '''
  198. enable or disable the buttons according to the
  199. current row we are in.
  200. '''
  201. def enableButtons(self):
  202. self.CurUnoDialog.btnInsert.Model.Enabled = \
  203. self.lastFocusRow < len(self.scrollfields)
  204. self.CurUnoDialog.btnRemove.Model.Enabled = \
  205. self.lastFocusRow < len(self.scrollfields) - 1
  206. if self.lastFocusControl is not None:
  207. self.CurUnoDialog.btnUp.Model.Enabled = self.lastFocusRow > 0
  208. self.CurUnoDialog.btnDown.Model.Enabled = \
  209. self.lastFocusRow < len(self.scrollfields) - 1
  210. else:
  211. self.CurUnoDialog.btnUp.Model.Enabled = False
  212. self.CurUnoDialog.btnDown.Model.Enabled = False
  213. '''
  214. Removes the current row.
  215. See general class documentation explanation about the
  216. data model used and the limitations which explain the implementation here.
  217. '''
  218. def removeRow(self):
  219. try:
  220. for i in range(self.lastFocusRow,
  221. len(self.scrollfields) - 1):
  222. pv1 = self.scrollfields[i]
  223. pv2 = self.scrollfields[i + 1]
  224. pv1[1].Value = pv2[1].Value
  225. pv1[2].Value = pv2[2].Value
  226. pv1[3].Value = pv2[3].Value
  227. self.updateDocumentRow(i)
  228. if i - self.nscrollvalue < self.nblockincrement:
  229. self.fillupControl(i - self.nscrollvalue)
  230. self.removeLastRow()
  231. # update the live preview background document
  232. self.reduceDocumentToTopics()
  233. self.enableButtons()
  234. if self.lastFocusControl is not None:
  235. # the focus should return to the edit control
  236. self.focus(self.lastFocusControl)
  237. except Exception:
  238. traceback.print_exc()
  239. '''
  240. Inserts a row before the current row.
  241. See general class documentation explanation about the
  242. data model used and the limitations which explain the implementation here.
  243. '''
  244. def insertRow(self):
  245. try:
  246. self.insertRowAtEnd()
  247. for i in range(len(self.scrollfields) - 2,
  248. self.lastFocusRow, -1):
  249. pv1 = self.scrollfields[i]
  250. pv2 = self.scrollfields[i - 1]
  251. pv1[1].Value = pv2[1].Value
  252. pv1[2].Value = pv2[2].Value
  253. pv1[3].Value = pv2[3].Value
  254. self.updateDocumentRow(i)
  255. if i - self.nscrollvalue < self.nblockincrement:
  256. self.fillupControl(i - self.nscrollvalue)
  257. # after rotating all the properties from this row on,
  258. # we clear the row, so it is practically a new one...
  259. pv1 = self.scrollfields[self.lastFocusRow]
  260. pv1[1].Value = ""
  261. pv1[2].Value = ""
  262. pv1[3].Value = ""
  263. # update the preview document.
  264. self.updateDocumentRow(self.lastFocusRow)
  265. self.fillupControl(
  266. self.lastFocusRow - self.nscrollvalue)
  267. self.enableButtons()
  268. if self.lastFocusControl is not None:
  269. self.focus(self.lastFocusControl)
  270. except Exception:
  271. traceback.print_exc()
  272. '''
  273. create a new row with the given index.
  274. The index is important because it is used in the
  275. Name member of the PropertyValue objects.
  276. To know why see general class documentation above (data model explanation)
  277. @param i the index of the new row
  278. @return
  279. '''
  280. def newRow(self, i):
  281. pv = list(range(4))
  282. pv[0] = Properties.createProperty(
  283. TopicsControl.LABEL + str(i), "" + str(i + 1) + ".")
  284. pv[1] = Properties.createProperty(TopicsControl.TOPIC + str(i), "")
  285. pv[2] = Properties.createProperty(TopicsControl.RESP + str(i), "")
  286. pv[3] = Properties.createProperty(TopicsControl.TIME + str(i), "")
  287. return pv
  288. '''
  289. Implementation of ControlScroller
  290. This is a UI method which inserts a new row to the control.
  291. It uses the child-class ControlRow. (see below).
  292. '''
  293. def insertControlGroup(self, _index, npos):
  294. oControlRow = ControlRow(
  295. self.CurUnoDialog, self.iCompPosX, npos, _index,
  296. ControlRow.tabIndex, self)
  297. self.ControlGroupVector.append(oControlRow)
  298. ControlRow.tabIndex += 4
  299. '''
  300. Checks if a row is empty.
  301. This is used when the last row is changed.
  302. If it is empty, the next row (which is always blank) is removed.
  303. If it is not empty, a next row must exist.
  304. @param row the index number of the row to check.
  305. @return true if empty. false if not.
  306. '''
  307. def isRowEmpty(self, row):
  308. data = self.getTopicData(row)
  309. # now - is this row empty?
  310. return not data[1].Value and not data[2].Value and not data[3].Value
  311. '''
  312. update the preview document and
  313. remove/insert rows if needed.
  314. @param guiRow
  315. @param column
  316. '''
  317. def fieldChanged(self, guiRow, column):
  318. try:
  319. # First, I update the document
  320. data = self.getTopicData(guiRow + self.nscrollvalue)
  321. if data is None:
  322. return
  323. self.updateDocumentCell(
  324. guiRow + self.nscrollvalue, column, data)
  325. if self.isRowEmpty(guiRow + self.nscrollvalue):
  326. '''
  327. if this is the row before the last one
  328. (the last row is always empty)
  329. delete the last row...
  330. '''
  331. if (guiRow + self.nscrollvalue) \
  332. == len(self.scrollfields) - 2:
  333. self.removeLastRow()
  334. '''now consequently check the last two rows,
  335. and remove the last one if they are both empty.
  336. (actually I check always the "before last" row,
  337. because the last one is always empty...
  338. '''
  339. while len(self.scrollfields) > 1 \
  340. and self.isRowEmpty(
  341. len(self.scrollfields) - 2):
  342. self.removeLastRow()
  343. cr = self.ControlGroupVector[
  344. len(self.scrollfields) - \
  345. self.nscrollvalue - 1]
  346. # if a remove was performed, set focus
  347. #to the last row with some data in it...
  348. self.focus(self.getControlByIndex(cr, column))
  349. # update the preview document.
  350. self.reduceDocumentToTopics()
  351. else:
  352. # row contains data
  353. # is this the last row?
  354. if (guiRow + self.nscrollvalue + 1) \
  355. == len(self.scrollfields):
  356. self.insertRowAtEnd()
  357. except Exception:
  358. traceback.print_exc()
  359. '''
  360. return the corresponding row data for the given index.
  361. @param topic index of the topic to get.
  362. @return a PropertyValue array with the data for the given topic.
  363. '''
  364. def getTopicData(self, topic):
  365. if topic < len(self.scrollfields):
  366. return self.scrollfields[topic]
  367. else:
  368. return None
  369. '''
  370. If the user presses tab on the last control, and
  371. there *are* more rows in the model, scroll down.
  372. @param event
  373. '''
  374. def lastControlKeyPressed(self, event):
  375. # if tab without shift was pressed...
  376. try:
  377. if event.KeyCode == TAB and event.Modifiers == 0:
  378. # if there is another row...
  379. if (self.nblockincrement + self.nscrollvalue) \
  380. < len(self.scrollfields):
  381. self.setScrollValue(self.nscrollvalue + 1)
  382. self.focus(self.getControlByIndex(self.ControlGroupVector[4], 1))
  383. except Exception:
  384. traceback.print_exc()
  385. '''
  386. If the user presses shift-tab on the first control, and
  387. there *are* more rows in the model, scroll up.
  388. @param event
  389. '''
  390. def firstControlKeyPressed(self, event):
  391. # if tab with shift was pressed...
  392. if (event.KeyCode == TAB) and \
  393. (event.Modifiers == SHIFT):
  394. if self.nscrollvalue > 0:
  395. self.setScrollValue(self.nscrollvalue - 1)
  396. self.focus(self.lastTime)
  397. '''
  398. sets focus to the given control.
  399. @param textControl
  400. '''
  401. def focus(self, textControl):
  402. textControl.setFocus()
  403. text = textControl.Text
  404. textControl.Selection = uno.createUnoStruct( \
  405. 'com.sun.star.awt.Selection', 0, len(text))
  406. self.focusGained2(textControl)
  407. '''
  408. moves the given row one row down.
  409. @param guiRow the gui index of the row to move.
  410. @param control the control to gain focus after moving.
  411. '''
  412. def rowDown(self, guiRow=None, control=None):
  413. try:
  414. if guiRow is None:
  415. guiRow = self.lastFocusRow - self.nscrollvalue
  416. if control is None:
  417. control = self.lastFocusControl
  418. # only perform if this is not the last row.
  419. actuallRow = guiRow + self.nscrollvalue
  420. if actuallRow + 1 < len(self.scrollfields):
  421. # get the current selection
  422. selection = control.Selection
  423. # the last row should scroll...
  424. scroll = (guiRow == self.nblockincrement - 1)
  425. if scroll:
  426. self.setScrollValue(self.nscrollvalue + 1)
  427. scroll1 = self.nscrollvalue
  428. if scroll:
  429. aux = -1
  430. else:
  431. aux = 1
  432. self.switchRows(guiRow, guiRow + aux)
  433. if self.nscrollvalue != scroll1:
  434. guiRow += (self.nscrollvalue - scroll1)
  435. self.setSelection(guiRow + (not scroll), control, selection)
  436. except Exception:
  437. traceback.print_exc()
  438. '''
  439. move the current row up
  440. '''
  441. def rowUp(self, guiRow=None, control=None):
  442. try:
  443. if guiRow is None:
  444. guiRow = self.lastFocusRow - self.nscrollvalue
  445. if control is None:
  446. control = self.lastFocusControl
  447. # only perform if this is not the first row
  448. actuallRow = guiRow + self.nscrollvalue
  449. if actuallRow > 0:
  450. # get the current selection
  451. selection = control.Selection
  452. # the last row should scroll...
  453. scroll = (guiRow == 0)
  454. if scroll:
  455. self.setScrollValue(self.nscrollvalue - 1)
  456. if scroll:
  457. aux = 1
  458. else:
  459. aux = -1
  460. self.switchRows(guiRow, guiRow + aux)
  461. self.setSelection(guiRow - (not scroll), control, selection)
  462. except Exception:
  463. traceback.print_exc()
  464. '''
  465. moves the cursor up.
  466. @param guiRow
  467. @param control
  468. '''
  469. def cursorUp(self, guiRow, control):
  470. # is this the last full row ?
  471. actuallRow = guiRow + self.nscrollvalue
  472. #if this is the first row
  473. if actuallRow == 0:
  474. return
  475. # the first row should scroll...
  476. scroll = (guiRow == 0)
  477. if scroll:
  478. self.setScrollValue(self.nscrollvalue - 1)
  479. upperRow = self.ControlGroupVector[guiRow]
  480. else:
  481. upperRow = self.ControlGroupVector[guiRow - 1]
  482. self.focus(self.getControl(upperRow, control))
  483. '''
  484. moves the cursor down
  485. @param guiRow
  486. @param control
  487. '''
  488. def cursorDown(self, guiRow, control):
  489. # is this the last full row ?
  490. actuallRow = guiRow + self.nscrollvalue
  491. #if this is the last row, exit
  492. if actuallRow == len(self.scrollfields) - 1:
  493. return
  494. # the first row should scroll...
  495. scroll = (guiRow == self.nblockincrement - 1)
  496. if scroll:
  497. self.setScrollValue(self.nscrollvalue + 1)
  498. lowerRow = self.ControlGroupVector[guiRow]
  499. else:
  500. # if we scrolled we are done...
  501. #otherwise...
  502. lowerRow = self.ControlGroupVector[guiRow + 1]
  503. self.focus(self.getControl(lowerRow, control))
  504. '''
  505. changes the values of the given rows with each other
  506. @param row1 one can figure out what this parameter is...
  507. @param row2 one can figure out what this parameter is...
  508. '''
  509. def switchRows(self, row1, row2):
  510. o1 = self.scrollfields[row1 + self.nscrollvalue]
  511. o2 = self.scrollfields[row2 + self.nscrollvalue]
  512. temp = None
  513. for i in range(1, len(o1)):
  514. temp = o1[i].Value
  515. o1[i].Value = o2[i].Value
  516. o2[i].Value = temp
  517. self.fillupControl(row1)
  518. self.fillupControl(row2)
  519. self.updateDocumentRow(row1 + self.nscrollvalue, o1)
  520. self.updateDocumentRow(row2 + self.nscrollvalue, o2)
  521. '''
  522. if we changed the last row, add another one...
  523. '''
  524. if (row1 + self.nscrollvalue + 1 == \
  525. len(self.scrollfields)) \
  526. or (row2 + self.nscrollvalue + 1 == \
  527. len(self.scrollfields)):
  528. self.insertRowAtEnd()
  529. '''
  530. if we did not change the last row but
  531. we did change the one before - check if we
  532. have two empty rows at the end.
  533. If so, delete the last one...
  534. '''
  535. elif (row1 + self.nscrollvalue) + \
  536. (row2 + self.nscrollvalue) \
  537. == (len(self.scrollfields) * 2 - 5):
  538. if self.isRowEmpty(len(self.scrollfields) - 2) \
  539. and self.isRowEmpty(
  540. len(self.scrollfields) - 1):
  541. self.removeLastRow()
  542. self.reduceDocumentToTopics()
  543. '''
  544. sets a text selection to a given control.
  545. This is used when one moves a row up or down.
  546. After moving row X to X+/-1, the selection (or cursor position) of the
  547. last focused control should be restored.
  548. The control's row is the given guiRow.
  549. The control's column is detected according to the given event.
  550. This method is called as subsequent to different events,
  551. thus it is comfortable to use the event here to detect the column,
  552. rather than in the different event methods.
  553. @param guiRow the row of the control to set the selection to.
  554. @param eventSource helps to detect
  555. the control's column to set the selection to.
  556. @param s the selection object to set.
  557. '''
  558. def setSelection(self, guiRow, eventSource, s):
  559. cr = self.ControlGroupVector[guiRow]
  560. control = self.getControl(cr, eventSource)
  561. control.setFocus()
  562. control.setSelection(s)
  563. '''
  564. returns a control out of the given row, according to a column number.
  565. @param cr control row object.
  566. @param column the column number.
  567. @return the control...
  568. '''
  569. def getControlByIndex(self, cr, column):
  570. tmp_switch_var1 = column
  571. if tmp_switch_var1 == 0:
  572. return cr.label
  573. elif tmp_switch_var1 == 1:
  574. return cr.textbox
  575. elif tmp_switch_var1 == 2:
  576. return cr.combobox
  577. elif tmp_switch_var1 == 3:
  578. return cr.timebox
  579. else:
  580. raise Exception("No such column");
  581. '''getControl
  582. returns a control out of the given row, which is
  583. in the same column as the given control.
  584. @param cr control row object
  585. @param control a control indicating a column.
  586. @return
  587. '''
  588. def getControl(self, cr, control):
  589. column = self.getColumn(control)
  590. return self.getControlByIndex(cr, column)
  591. '''
  592. returns the column number of the given control.
  593. @param control
  594. @return
  595. '''
  596. def getColumn(self, control):
  597. name = control.Model.Name
  598. if name.startswith(TopicsControl.TOPIC):
  599. return 1
  600. if name.startswith(TopicsControl.RESP):
  601. return 2
  602. if name.startswith(TopicsControl.TIME):
  603. return 3
  604. if name.startswith(TopicsControl.LABEL):
  605. return 0
  606. return -1
  607. '''
  608. update the given row in the preview document with the given data.
  609. @param row
  610. @param data
  611. '''
  612. def updateDocumentRow(self, row, data=None):
  613. if data is None:
  614. data = self.scrollfields[row]
  615. try:
  616. for i in range(len(data)):
  617. self.CurUnoDialog.myAgendaDoc.topics.writeCell(
  618. row, i, data)
  619. except Exception:
  620. traceback.print_exc()
  621. '''
  622. updates a single cell in the preview document.
  623. Is called when a single value is changed, since we really
  624. don't have to update the whole row for one small change...
  625. @param row the data row to update (topic number).
  626. @param column the column to update (a gui column, not a document column).
  627. @param data the data of the entire row.
  628. '''
  629. def updateDocumentCell(self, row, column, data):
  630. try:
  631. self.CurUnoDialog.myAgendaDoc.topics.writeCell(
  632. row, column, data)
  633. except Exception:
  634. traceback.print_exc()
  635. '''
  636. when removing rows, this method updates
  637. the preview document to show the number of rows
  638. according to the data model.
  639. '''
  640. def reduceDocumentToTopics(self):
  641. try:
  642. self.CurUnoDialog.myAgendaDoc.topics.reduceDocumentTo(
  643. len(self.scrollfields) - 1)
  644. except Exception:
  645. traceback.print_exc()
  646. '''
  647. A class represting a single GUI row.
  648. Note that the instance methods of this class
  649. are being called and handle controls of
  650. a single row.
  651. '''
  652. class ControlRow(object):
  653. tabIndex = 520
  654. '''
  655. constructor. Create the row in the given dialog given coordinates,
  656. with the given offset (row number) and tabindex.
  657. Note that since I use this specifically for the agenda wizard,
  658. the step and all control coordinates inside the
  659. row are constant (5).
  660. '''
  661. def __init__(self, dialog, x, y, i, tabindex, topicsControl):
  662. self.offset = i
  663. self.dialog = dialog
  664. self.topicsControl = topicsControl
  665. self.label = self.dialog.insertLabel(
  666. self.topicsControl.LABEL + str(i),
  667. self.topicsControl.LABEL_PROPS,
  668. (8, "" + str(i + 1) + ".",
  669. x + 4, y + 2, self.topicsControl.iStep, tabindex, 10))
  670. self.textbox = self.dialog.insertTextField(
  671. self.topicsControl.TOPIC + str(i), "topicTextChanged",
  672. self.topicsControl.TEXT_PROPS,
  673. (12, HelpIds.getHelpIdString(self.topicsControl.curHelpIndex + i * 3 + 1),
  674. x + 15, y, self.topicsControl.iStep, tabindex + 1, 84), self)
  675. self.combobox = self.dialog.insertTextField(
  676. self.topicsControl.RESP + str(i), "responsibleTextChanged",
  677. self.topicsControl.TEXT_PROPS,
  678. (12, HelpIds.getHelpIdString(self.topicsControl.curHelpIndex + i * 3 + 2),
  679. x + 103, y, self.topicsControl.iStep, tabindex + 2, 68), self)
  680. self.timebox = self.dialog.insertTextField(
  681. self.topicsControl.TIME + str(i), "timeTextChanged",
  682. self.topicsControl.TEXT_PROPS,
  683. (12, HelpIds.getHelpIdString(self.topicsControl.curHelpIndex + i * 3 + 3),
  684. x + 175, y, self.topicsControl.iStep, tabindex + 3, 20), self)
  685. self.setEnabled(False)
  686. self.textbox.addKeyListener(KeyListenerProcAdapter(self.keyPressed))
  687. self.combobox.addKeyListener(KeyListenerProcAdapter(self.keyPressed))
  688. self.timebox.addKeyListener(KeyListenerProcAdapter(self.keyPressed))
  689. self.textbox.addFocusListener(FocusListenerProcAdapter(
  690. self.topicsControl.focusGained))
  691. self.combobox.addFocusListener(FocusListenerProcAdapter(
  692. self.topicsControl.focusGained))
  693. self.timebox.addFocusListener(FocusListenerProcAdapter(
  694. self.topicsControl.focusGained))
  695. def topicTextChanged(self):
  696. try:
  697. # update the data model
  698. self.topicsControl.fieldInfo(self.offset, 1)
  699. # update the preview document
  700. self.topicsControl.fieldChanged(self.offset, 1)
  701. except Exception:
  702. traceback.print_exc()
  703. '''
  704. called through an event listener when the
  705. responsible text is changed by the user.
  706. updates the data model and the preview document.
  707. '''
  708. def responsibleTextChanged(self):
  709. try:
  710. # update the data model
  711. self.topicsControl.fieldInfo(self.offset, 2)
  712. # update the preview document
  713. self.topicsControl.fieldChanged(self.offset, 2)
  714. except Exception:
  715. traceback.print_exc()
  716. '''
  717. called through an event listener when the
  718. time text is changed by the user.
  719. updates the data model and the preview document.
  720. '''
  721. def timeTextChanged(self):
  722. try:
  723. # update the data model
  724. self.topicsControl.fieldInfo(self.offset, 3)
  725. # update the preview document
  726. self.topicsControl.fieldChanged(self.offset, 3)
  727. except Exception:
  728. traceback.print_exc()
  729. '''
  730. enables/disables the row.
  731. @param enabled true for enable, false for disable.
  732. '''
  733. def setEnabled(self, enabled):
  734. self.label.Model.Enabled = enabled
  735. self.textbox.Model.Enabled = enabled
  736. self.combobox.Model.Enabled = enabled
  737. self.timebox.Model.Enabled = enabled
  738. '''
  739. Implementation of XKeyListener.
  740. Optionally performs the one of the following:
  741. cursor up, or down, row up or down
  742. '''
  743. def keyPressed(self, event):
  744. try:
  745. if self.isMoveDown(event):
  746. self.topicsControl.rowDown(self.offset, event.Source)
  747. elif self.isMoveUp(event):
  748. self.topicsControl.rowUp(self.offset, event.Source)
  749. elif self.isDown(event):
  750. self.topicsControl.cursorDown(self.offset, event.Source)
  751. elif self.isUp(event):
  752. self.topicsControl.cursorUp(self.offset, event.Source)
  753. self.topicsControl.enableButtons()
  754. except Exception:
  755. traceback.print_exc()
  756. def isMoveDown(self, e):
  757. return (e.KeyCode == DOWN) and (e.Modifiers == MOD1)
  758. def isMoveUp(self, e):
  759. return (e.KeyCode == UP) and (e.Modifiers == MOD1)
  760. def isDown(self, e):
  761. return (e.KeyCode == DOWN) and (e.Modifiers == 0)
  762. def isUp(self, e):
  763. return (e.KeyCode == UP) and (e.Modifiers == 0)