UnoDialog.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #
  2. #
  3. # This file is part of the LibreOffice project.
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public
  6. # License, v. 2.0. If a copy of the MPL was not distributed with this
  7. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. #
  9. # This file incorporates work covered by the following license notice:
  10. #
  11. # Licensed to the Apache Software Foundation (ASF) under one or more
  12. # contributor license agreements. See the NOTICE file distributed
  13. # with this work for additional information regarding copyright
  14. # ownership. The ASF licenses this file to you under the Apache
  15. # License, Version 2.0 (the "License"); you may not use this file
  16. # except in compliance with the License. You may obtain a copy of
  17. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  18. #
  19. import uno
  20. import traceback
  21. from .PeerConfig import PeerConfig
  22. from .UIConsts import UIConsts
  23. from ..common.PropertyNames import PropertyNames
  24. from com.sun.star.awt import Rectangle
  25. from com.sun.star.awt.PosSize import POS
  26. class UnoDialog(object):
  27. createDict = False
  28. dictProperties = None
  29. BisHighContrastModeActivated = None
  30. xVclWindowPeer = None
  31. def __init__(self, xMSF, PropertyNames, PropertyValues):
  32. try:
  33. self.xMSF = xMSF
  34. self.ControlList = {}
  35. self.xDialogModel = xMSF.createInstance(
  36. "com.sun.star.awt.UnoControlDialogModel")
  37. self.xUnoDialog = xMSF.createInstance(
  38. "com.sun.star.awt.UnoControlDialog")
  39. self.xUnoDialog.setModel(self.xDialogModel)
  40. self.m_oPeerConfig = None
  41. self.xWindowPeer = None
  42. except Exception:
  43. traceback.print_exc()
  44. # repaints the currentDialogStep
  45. def repaintDialogStep(self):
  46. try:
  47. ncurstep = int(self.xDialogModel.Step)
  48. self.xDialogModel.Step = 99
  49. self.xDialogModel.Step = ncurstep
  50. except Exception:
  51. traceback.print_exc()
  52. def insertControlModel(self, serviceName, componentName, sPropNames, oPropValues):
  53. try:
  54. xControlModel = self.xDialogModel.createInstance(serviceName)
  55. uno.invoke(xControlModel, "setPropertyValues",
  56. (sPropNames, oPropValues))
  57. self.xDialogModel.insertByName(componentName, xControlModel)
  58. xControlModel.Name = componentName
  59. except Exception:
  60. traceback.print_exc()
  61. aObj = self.xUnoDialog.getControl(componentName)
  62. return aObj
  63. def setFocus(self, ControlName):
  64. oFocusControl = self.xUnoDialog.getControl(ControlName)
  65. oFocusControl.setFocus()
  66. def calculateDialogPosition(self, FramePosSize):
  67. # Todo:check if it would be useful or possible to create a dialog peer
  68. # that can be used for the messageboxes to
  69. # maintain modality when they pop up.
  70. CurPosSize = self.xUnoDialog.getPosSize()
  71. WindowHeight = FramePosSize.Height
  72. WindowWidth = FramePosSize.Width
  73. DialogWidth = CurPosSize.Width
  74. DialogHeight = CurPosSize.Height
  75. iXPos = ((WindowWidth / 2) - (DialogWidth / 2))
  76. iYPos = ((WindowHeight / 2) - (DialogHeight / 2))
  77. self.xUnoDialog.setPosSize(
  78. iXPos, iYPos, DialogWidth, DialogHeight, POS)
  79. '''
  80. @param FramePosSize
  81. @return 0 for cancel, 1 for ok
  82. @throws com.sun.star.uno.Exception
  83. '''
  84. def executeDialog(self, FramePosSize):
  85. if self.xUnoDialog.getPeer() is None:
  86. raise AttributeError(
  87. "Please create a peer, using your own frame")
  88. self.calculateDialogPosition(FramePosSize)
  89. if self.xWindowPeer is None:
  90. self.createWindowPeer()
  91. self.xVclWindowPeer = self.xWindowPeer
  92. self.BisHighContrastModeActivated = self.isHighContrastModeActivated()
  93. return self.xUnoDialog.execute()
  94. def setVisible(self, parent):
  95. self.calculateDialogPosition(parent.xUnoDialog.getPosSize())
  96. if self.xWindowPeer == None:
  97. self.createWindowPeer()
  98. self.xUnoDialog.setVisible(True)
  99. '''
  100. @param XComponent
  101. @return 0 for cancel, 1 for ok
  102. @throws com.sun.star.uno.Exception
  103. '''
  104. def executeDialogFromComponent(self, xComponent):
  105. if xComponent is not None:
  106. w = xComponent.ComponentWindow
  107. if w is not None:
  108. return self.executeDialog(w.PosSize)
  109. return self.executeDialog( Rectangle (0, 0, 640, 400))
  110. '''
  111. create a peer for this
  112. dialog, using the given
  113. peer as a parent.
  114. @param parentPeer
  115. @return
  116. @throws java.lang.Exception
  117. '''
  118. def createWindowPeer(self, parentPeer=None):
  119. self.xUnoDialog.setVisible(False)
  120. xToolkit = self.xMSF.createInstance("com.sun.star.awt.Toolkit")
  121. if parentPeer is None:
  122. parentPeer = xToolkit.getDesktopWindow()
  123. self.xUnoDialog.createPeer(xToolkit, parentPeer)
  124. self.xWindowPeer = self.xUnoDialog.getPeer()
  125. return self.xUnoDialog.getPeer()
  126. @classmethod
  127. def setEnabled(self, control, enabled):
  128. control.Model.Enabled = enabled
  129. @classmethod
  130. def getModel(self, control):
  131. return control.getModel()
  132. @classmethod
  133. def getDisplayProperty(self, xServiceInfo):
  134. if xServiceInfo.supportsService(
  135. "com.sun.star.awt.UnoControlFixedTextModel"):
  136. return PropertyNames.PROPERTY_LABEL
  137. elif xServiceInfo.supportsService(
  138. "com.sun.star.awt.UnoControlButtonModel"):
  139. return PropertyNames.PROPERTY_LABEL
  140. elif xServiceInfo.supportsService(
  141. "com.sun.star.awt.UnoControlCurrencyFieldModel"):
  142. return "Value"
  143. elif xServiceInfo.supportsService(
  144. "com.sun.star.awt.UnoControlDateFieldModel"):
  145. return "Date"
  146. elif xServiceInfo.supportsService(
  147. "com.sun.star.awt.UnoControlFixedLineModel"):
  148. return PropertyNames.PROPERTY_LABEL
  149. elif xServiceInfo.supportsService(
  150. "com.sun.star.awt.UnoControlFormattedFieldModel"):
  151. return "EffectiveValue"
  152. elif xServiceInfo.supportsService(
  153. "com.sun.star.awt.UnoControlNumericFieldModel"):
  154. return "Value"
  155. elif xServiceInfo.supportsService(
  156. "com.sun.star.awt.UnoControlPatternFieldModel"):
  157. return "Text"
  158. elif xServiceInfo.supportsService(
  159. "com.sun.star.awt.UnoControlProgressBarModel"):
  160. return "ProgressValue"
  161. elif xServiceInfo.supportsService(
  162. "com.sun.star.awt.UnoControlTimeFieldModel"):
  163. return "Time"
  164. elif xServiceInfo.supportsService(
  165. "com.sun.star.awt.UnoControlImageControlModel"):
  166. return PropertyNames.PROPERTY_IMAGEURL
  167. elif xServiceInfo.supportsService(
  168. "com.sun.star.awt.UnoControlRadioButtonModel"):
  169. return PropertyNames.PROPERTY_STATE
  170. elif xServiceInfo.supportsService(
  171. "com.sun.star.awt.UnoControlCheckBoxModel"):
  172. return PropertyNames.PROPERTY_STATE
  173. elif xServiceInfo.supportsService(
  174. "com.sun.star.awt.UnoControlEditModel"):
  175. return "Text"
  176. elif xServiceInfo.supportsService(
  177. "com.sun.star.awt.UnoControlComboBoxModel"):
  178. return "Text"
  179. elif xServiceInfo.supportsService(
  180. "com.sun.star.awt.UnoControlListBoxModel"):
  181. return "SelectedItems"
  182. else:
  183. return ""
  184. def isHighContrastModeActivated(self):
  185. if (self.xVclWindowPeer is not None):
  186. if (self.BisHighContrastModeActivated is None):
  187. nUIColor = 0
  188. try:
  189. nUIColor = self.xVclWindowPeer.getProperty("DisplayBackgroundColor")
  190. except Exception:
  191. traceback.print_exc()
  192. return False
  193. # TODO: The following methods could be wrapped in an own class implementation
  194. nRed = self.getRedColorShare(nUIColor)
  195. nGreen = self.getGreenColorShare(nUIColor)
  196. nBlue = self.getBlueColorShare(nUIColor)
  197. nLuminance = ((nBlue * 28 + nGreen * 151 + nRed * 77) / 256)
  198. bisactivated = (nLuminance <= 25)
  199. self.BisHighContrastModeActivated = bool(bisactivated)
  200. return bisactivated;
  201. else:
  202. return self.BisHighContrastModeActivated
  203. else:
  204. return False
  205. def getRedColorShare(self, _nColor):
  206. nRed = _nColor / 65536
  207. nRedModulo = _nColor % 65536
  208. nGreen = nRedModulo / 256
  209. nGreenModulo = (nRedModulo % 256)
  210. nBlue = nGreenModulo
  211. return nRed
  212. def getGreenColorShare(self, _nColor):
  213. nRed = _nColor / 65536
  214. nRedModulo = _nColor % 65536
  215. nGreen = nRedModulo / 256
  216. return nGreen
  217. def getBlueColorShare(self, _nColor):
  218. nRed = _nColor / 65536
  219. nRedModulo = _nColor % 65536
  220. nGreen = nRedModulo / 256
  221. nGreenModulo = (nRedModulo % 256)
  222. nBlue = nGreenModulo
  223. return nBlue