1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
# Copyright (c) 2015 Yann Lanthony |
4
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
5
|
|
|
# |
6
|
|
|
# Licensed under the terms of the MIT License |
7
|
|
|
# (See LICENSE.txt for details) |
8
|
|
|
# ----------------------------------------------------------------------------- |
9
|
|
|
"""Contains the Qt implementation of the Watcher api.""" |
10
|
|
|
|
11
|
|
|
# yapf: disable |
12
|
|
|
|
13
|
|
|
from __future__ import absolute_import |
14
|
|
|
|
15
|
|
|
# Local imports |
16
|
|
|
from qtsass.watchers.polling import PollingWatcher |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
# We cascade through Qt bindings here rather than relying on a comprehensive |
20
|
|
|
# Qt compatability library like qtpy or Qt.py. This prevents us from forcing a |
21
|
|
|
# specific compatability library on users. |
22
|
|
|
QT_BINDING = None |
23
|
|
|
if not QT_BINDING: |
24
|
|
|
try: |
25
|
|
|
from PySide2.QtWidgets import QApplication |
26
|
|
|
from PySide2.QtCore import QObject, Signal |
27
|
|
|
QT_BINDING = 'pyside2' |
28
|
|
|
except ImportError: |
29
|
|
|
pass |
30
|
|
|
if not QT_BINDING: |
31
|
|
|
try: |
32
|
|
|
from PyQt5.QtWidgets import QApplication |
33
|
|
|
from PyQt5.QtCore import QObject |
34
|
|
|
from PyQt5.QtCore import pyqtSignal as Signal |
35
|
|
|
QT_BINDING = 'pyqt5' |
36
|
|
|
except ImportError: |
37
|
|
|
pass |
38
|
|
|
if not QT_BINDING: |
39
|
|
|
try: |
40
|
|
|
from PySide.QtGui import QApplication |
41
|
|
|
from PySide2.QtCore import QObject, Signal |
42
|
|
|
QT_BINDING == 'pyside' |
43
|
|
|
except ImportError: |
44
|
|
|
pass |
45
|
|
|
if not QT_BINDING: |
46
|
|
|
from PyQt4.QtGui import QApplication |
47
|
|
|
from PyQt4.QtCore import QObject |
48
|
|
|
from PyQt4.QtCore import pyqtSignal as Signal |
49
|
|
|
QT_BINDING == 'pyqt4' |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
# yapf: enable |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
class QtDispatcher(QObject): |
|
|
|
|
56
|
|
|
"""Used by QtWatcher to dispatch callbacks in the main ui thread.""" |
57
|
|
|
|
58
|
|
|
signal = Signal() |
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
class QtWatcher(PollingWatcher): |
62
|
|
|
"""The Qt implementation of the Watcher api. |
63
|
|
|
|
64
|
|
|
Subclasses PollingWatcher but dispatches :meth:`compile_and_dispatch` |
65
|
|
|
using a Qt Signal to ensure that these calls are executed in the main ui |
66
|
|
|
thread. We aren't using a QFileSystemWatcher because it fails to report |
67
|
|
|
changes in certain circumstances. |
68
|
|
|
""" |
69
|
|
|
|
70
|
|
|
_qt_binding = QT_BINDING |
71
|
|
|
|
72
|
|
|
def setup(self): |
73
|
|
|
"""Set up QtWatcher.""" |
74
|
|
|
super(QtWatcher, self).setup() |
75
|
|
|
self._qtdispatcher = None |
76
|
|
|
|
77
|
|
|
@property |
78
|
|
|
def qtdispatcher(self): |
79
|
|
|
"""Get the QtDispatcher.""" |
80
|
|
|
if self._qtdispatcher is None: |
81
|
|
|
self._qtdispatcher = QtDispatcher() |
82
|
|
|
self._qtdispatcher.signal.connect(self.compile_and_dispatch) |
83
|
|
|
return self._qtdispatcher |
84
|
|
|
|
85
|
|
|
def on_change(self): |
86
|
|
|
"""Call when a change is detected.""" |
87
|
|
|
self._log.debug('Change detected...') |
88
|
|
|
|
89
|
|
|
# If a QApplication event loop has not been started |
90
|
|
|
# call compile_and_dispatch in the current thread. |
91
|
|
|
if not QApplication.instance(): |
|
|
|
|
92
|
|
|
return super(PollingWatcher, self).compile_and_dispatch() |
93
|
|
|
|
94
|
|
|
# Create and use a QtDispatcher to ensure compile and any |
95
|
|
|
# connected callbacks get executed in the main gui thread. |
96
|
|
|
self.qtdispatcher.signal.emit() |
97
|
|
|
|