Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #
  2. # Copyright (C) 2009 - 2019 Isotropix SAS. All rights reserved.
  3. #
  4. # The information in this file is provided for the exclusive use of
  5. # the software licensees of Isotropix. Contents of this file may not
  6. # be distributed, copied or duplicated in any form, in whole or in
  7. # part, without the prior written permission of Isotropix SAS.
  8. #
  9. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  10. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  11. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  12. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  13. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  14. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21.  
  22. ## @package pyqt_clarisse
  23. # This module defines helper methods to integrate PyQt/PySide functionalities
  24. # into Clarisse.
  25. #
  26. # @note
  27. # This module must be imported **AFTER** having imported PyQt or PySide,
  28. # and only one of those must be imported. This is due to the code
  29. # detecting which version of Qt is currently loaded.
  30. #
  31. # @code{python}
  32. # from PySide import QtGui
  33. # import pyqt_clarisse
  34. #
  35. # # ... the rest of the code ...
  36. # @endcode
  37. #
  38. # or:
  39. #
  40. # @code{python}
  41. # from PyQt4 import Qt
  42. # import pyqt_clarisse
  43. #
  44. # # ... the rest of the code ...
  45. # @endcode
  46. #
  47.  
  48. import ix
  49. import sys
  50.  
  51. ## Runs the specified QApplication without blocking Clarisse main-loop
  52. #
  53. # @param app
  54. # The QApplication to run.
  55. #
  56. def exec_(application):
  57. is_history_enabled = ix.application.get_command_manager().is_history_enabled()
  58. PyQtAppClarisseHelper(application).exec_()
  59. while are_windows_visible():
  60. if is_history_enabled == False: ix.enable_command_history()
  61. ix.application.check_for_events()
  62. if is_history_enabled == False: ix.disable_command_history()
  63.  
  64. def are_windows_visible():
  65. # return if any top window is visible
  66. return any(w.isVisible() for w in _qt_gui.QApplication.topLevelWidgets())
  67.  
  68. class PyQtAppClarisseHelper:
  69. def __init__(self, app):
  70. self.app = app
  71. self.event_loop = _qt_core.QEventLoop()
  72.  
  73. def exec_(self):
  74. # add the callback to Clarisse main loop
  75. ix.application.add_to_event_loop_single(self.process_events)
  76.  
  77. def process_events(self):
  78. if are_windows_visible():
  79. print("Window visible")
  80. # call Qt main loop
  81. self.event_loop.processEvents()
  82. # flush stacked events
  83. self.app.sendPostedEvents(None, 0)
  84. # add the callback to Clarisse main loop
  85. ix.application.add_to_event_loop_single(self.process_events)
  86.  
  87. ## This function will check loaded modules to try and guess which version was
  88. # loaded. In case no version or multiple ones were loaded, it will log an error
  89. # and return nothing.
  90. #
  91. # @note
  92. # This function is "private" and shouldn't be used directly by anthing
  93. # other than the pyqt_clarisse module.
  94. #
  95. # @returns
  96. # A pair containing the QtCore and QtGui modules in that order.
  97. # Both modules can be None in case of errors.
  98. #
  99. def _get_qt():
  100. # check which versions of Qt are loaded
  101. pyqt4 = 1 if sys.modules.has_key("PyQt4") else 0
  102. pyside = 1 if sys.modules.has_key("PySide") else 0
  103. pyqt5 = 1 if sys.modules.has_key("PySide2") else 0
  104. pyside2 = 1 if sys.modules.has_key("PySide2") else 0
  105.  
  106. # get the number of loaded versions
  107. loaded_qt_versions = pyqt4 + pyside + pyqt5 + pyside2
  108.  
  109. # if no version of Qt is loaded, or if multiple ones are,
  110. # throw an exception
  111. if loaded_qt_versions == 0:
  112. raise Exception("pyqt_clarisse - no known Qt module found. Try importing PyQt4 or PySide **before** importing pyqt_clarisse")
  113. elif loaded_qt_versions > 1:
  114. raise Exception("pyqt_clarisse - more than one Qt module loaded ! Load only one of PyQt4 or PySide before importing pyqt_clarisse")
  115.  
  116. # here we can actually load the correct version of Qt and
  117. # return its QtCore and QtGui parts.
  118. if pyqt4 == 1:
  119. from PyQt4 import QtCore, QtGui
  120. return QtCore, QtGui
  121. elif pyside == 1:
  122. from PySide import QtCore, QtGui
  123. return QtCore, QtGui
  124. elif pyqt5 == 1:
  125. from PyQt4 import QtCore, QtWidgets
  126. return QtCore, QtGui
  127. elif pyside2 == 1:
  128. from PySide2 import QtCore, QtWidgets
  129. return QtCore, QtWidgets
  130.  
  131.  
  132. # load QtCore and QtGui (or QtWidgets in case Qt5 is used)
  133. _qt_core, _qt_gui = _get_qt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement