ControlScroller.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 traceback
  19. from .UnoDialog import UnoDialog
  20. from ..common.Desktop import Desktop
  21. from ..common.PropertyNames import PropertyNames
  22. from ..common.HelpIds import HelpIds
  23. from com.sun.star.awt.ScrollBarOrientation import VERTICAL
  24. class ControlScroller(object):
  25. SORELFIRSTPOSY = 3
  26. iScrollBarWidth = 10
  27. # TODO add parameters for tabindices and helpindex
  28. def __init__(self, _CurUnoDialog, _xMSF, _iStep, _iCompPosX, _iCompPosY,
  29. _iCompWidth, _nblockincrement, _nlinedistance, _firsthelpindex):
  30. self.xMSF = _xMSF
  31. self.scrollfields = []
  32. self.ControlGroupVector = []
  33. ControlScroller.nblockincrement = _nblockincrement
  34. self.CurUnoDialog = _CurUnoDialog
  35. self.iStep = _iStep
  36. self.curHelpIndex = _firsthelpindex
  37. self.curtabindex = self.iStep * 100
  38. self.linedistance = _nlinedistance
  39. self.iCompPosX = _iCompPosX
  40. self.iCompPosY = _iCompPosY
  41. self.iCompWidth = _iCompWidth
  42. self.iCompHeight = 2 * ControlScroller.SORELFIRSTPOSY + \
  43. ControlScroller.nblockincrement * self.linedistance
  44. self.iStartPosY = self.iCompPosY + ControlScroller.SORELFIRSTPOSY
  45. ScrollHeight = self.iCompHeight - 2
  46. self.nlineincrement = 1
  47. self.sincSuffix = Desktop.getIncrementSuffix(
  48. self.CurUnoDialog.xDialogModel, "imgBackground")
  49. self.xScrollBar = self.CurUnoDialog.insertScrollBar(
  50. "TitleScrollBar" + self.sincSuffix,
  51. ("Border", PropertyNames.PROPERTY_ENABLED,
  52. PropertyNames.PROPERTY_HEIGHT,
  53. PropertyNames.PROPERTY_HELPURL, "Orientation",
  54. PropertyNames.PROPERTY_POSITION_X,
  55. PropertyNames.PROPERTY_POSITION_Y,
  56. PropertyNames.PROPERTY_STEP,
  57. PropertyNames.PROPERTY_WIDTH),
  58. (0, True, ScrollHeight,
  59. HelpIds.getHelpIdString(self.curHelpIndex),
  60. VERTICAL, self.iCompPosX + self.iCompWidth - \
  61. ControlScroller.iScrollBarWidth - 1,
  62. self.iCompPosY + 1, self.iStep,
  63. ControlScroller.iScrollBarWidth), 0, self)
  64. self.nscrollvalue = 0
  65. ypos = self.iStartPosY + ControlScroller.SORELFIRSTPOSY
  66. for i in range(ControlScroller.nblockincrement):
  67. self.insertControlGroup(i, ypos)
  68. ypos += self.linedistance
  69. def fillupControls(self, binitialize):
  70. for i in range(ControlScroller.nblockincrement):
  71. if i < self.ncurfieldcount:
  72. self.fillupControl(i)
  73. if binitialize:
  74. self.CurUnoDialog.repaintDialogStep()
  75. def fillupControl(self, guiRow):
  76. nameProps = self.scrollfields[guiRow]
  77. valueProps = self.scrollfields[guiRow + self.nscrollvalue]
  78. for index, item in enumerate(nameProps):
  79. if self.CurUnoDialog.xDialogModel.hasByName(item.Name):
  80. self.setControlData(item.Name, valueProps[index].Value)
  81. else:
  82. raise AttributeError("No such control !")
  83. self.ControlGroupVector[guiRow].setEnabled(True)
  84. def setScrollValue(self, _nscrollvalue, _ntotfieldcount=None):
  85. if _ntotfieldcount is not None:
  86. self.setTotalFieldCount(_ntotfieldcount)
  87. if _nscrollvalue >= 0:
  88. self.xScrollBar.Model.ScrollValue = _nscrollvalue
  89. self.scrollControls()
  90. def setCurFieldCount(self):
  91. if self.ntotfieldcount > ControlScroller.nblockincrement:
  92. self.ncurfieldcount = ControlScroller.nblockincrement
  93. else:
  94. self.ncurfieldcount = self.ntotfieldcount
  95. def setTotalFieldCount(self, _ntotfieldcount):
  96. self.ntotfieldcount = _ntotfieldcount
  97. self.setCurFieldCount()
  98. if self.ntotfieldcount > ControlScroller.nblockincrement:
  99. self.xScrollBar.Model.Enabled = True
  100. self.xScrollBar.Model.ScrollValueMax = \
  101. self.ntotfieldcount - ControlScroller.nblockincrement
  102. else:
  103. self.xScrollBar.Model.Enabled = False
  104. def scrollControls(self):
  105. try:
  106. self.nscrollvalue = \
  107. int(self.xScrollBar.Model.ScrollValue)
  108. if self.nscrollvalue + ControlScroller.nblockincrement \
  109. >= self.ntotfieldcount:
  110. self.nscrollvalue = \
  111. self.ntotfieldcount - ControlScroller.nblockincrement
  112. self.fillupControls(False)
  113. except Exception:
  114. traceback.print_exc()
  115. '''
  116. updates the corresponding data to
  117. the control in guiRow and column
  118. @param guiRow 0 based row index
  119. @param column 0 based column index
  120. @return the propertyValue object corresponding to
  121. this control.
  122. '''
  123. def fieldInfo(self, guiRow, column):
  124. if guiRow + self.nscrollvalue < len(self.scrollfields):
  125. valueProp = (self.scrollfields[guiRow + self.nscrollvalue])[column]
  126. nameProp = (self.scrollfields[guiRow])[column]
  127. if self.CurUnoDialog.xDialogModel.hasByName(nameProp.Name):
  128. valueProp.Value = self.getControlData(nameProp.Name)
  129. else:
  130. valueProp.Value = nameProp.Value
  131. return valueProp
  132. else:
  133. return None
  134. def unregisterControlGroup(self, _index):
  135. del self.scrollfields[_index]
  136. def registerControlGroup(self, _currowproperties, _i):
  137. if _i == 0:
  138. del self.scrollfields[:]
  139. if _i >= len(self.scrollfields):
  140. self.scrollfields.append(_currowproperties)
  141. else:
  142. self.scrollfields.insert(_currowproperties, _i)
  143. def setControlData(self, controlname, newvalue):
  144. oControlModel = self.CurUnoDialog.xUnoDialog.getControl(
  145. controlname).Model
  146. propertyname = UnoDialog.getDisplayProperty(oControlModel)
  147. if propertyname:
  148. setattr(oControlModel, propertyname, newvalue)
  149. def getControlData(self, controlname):
  150. oControlModel = self.CurUnoDialog.xUnoDialog.getControl(
  151. controlname).Model
  152. propertyname = UnoDialog.getDisplayProperty(oControlModel)
  153. if propertyname:
  154. return getattr(oControlModel, propertyname)
  155. else:
  156. return None