Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 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. if _qt_widgets is not None:
  67. return any(w.isVisible() for w in _qt_widgets.QApplication.topLevelWidgets())
  68. return any(w.isVisible() for w in _qt_gui.QApplication.topLevelWidgets())
  69.  
  70. class PyQtAppClarisseHelper:
  71. def __init__(self, app):
  72. self.app = app
  73. self.event_loop = _qt_core.QEventLoop()
  74.  
  75. def exec_(self):
  76. # add the callback to Clarisse main loop
  77. ix.application.add_to_event_loop_single(self.process_events)
  78.  
  79. def process_events(self):
  80. if are_windows_visible():
  81. # call Qt main loop
  82. self.event_loop.processEvents()
  83. # flush stacked events
  84. self.app.sendPostedEvents(None, 0)
  85. # add the callback to Clarisse main loop
  86. ix.application.add_to_event_loop_single(self.process_events)
  87.  
  88. ## This function will check loaded modules to try and guess which version was
  89. # loaded. In case no version or multiple ones were loaded, it will log an error
  90. # and return nothing.
  91. #
  92. # @note
  93. # This function is "private" and shouldn't be used directly by anthing
  94. # other than the pyqt_clarisse module.
  95. #
  96. # @returns
  97. # A pair containing the QtCore and QtGui modules in that order.
  98. # Both modules can be None in case of errors.
  99. #
  100. def _get_qt():
  101. # check which versions of Qt are loaded
  102. pyqt4 = 1 if sys.modules.has_key("PyQt4") else 0
  103. pyside = 1 if sys.modules.has_key("PySide") 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 +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, None
  121. elif pyside == 1:
  122. from PySide import QtCore, QtGui
  123. return QtCore, QtGui, None
  124. elif pyside2 == 1:
  125. from PySide2 import QtCore, QtGui, QtWidgets
  126. return QtCore, QtGui, QtWidgets
  127.  
  128.  
  129. # load QtCore and QtGui (or QtWidgets in case Qt5 is used)
  130. _qt_core, _qt_gui, _qt_widgets = _get_qt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement