source: orange/Orange/OrangeCanvas/gui/quickhelp.py @ 11243:e788addef69c

Revision 11243:e788addef69c, 3.1 KB checked in by Ales Erjavec <ales.erjavec@…>, 16 months ago (diff)

Implemented a different way to handle toolbox/canvas hover/selection help text.

Now using a QStatusTip subclass to notify the top level window.

Line 
1from collections import Callable
2
3from PyQt4.QtGui import QTextBrowser, QStatusTipEvent
4from PyQt4.QtCore import QObject, QCoreApplication, QEvent, QTimer
5from PyQt4.QtCore import pyqtSignal as Signal
6
7
8class QuickHelp(QTextBrowser):
9    textChanged = Signal()
10
11    def __init__(self, *args, **kwargs):
12        QTextBrowser.__init__(self, *args, **kwargs)
13
14        self.setOpenExternalLinks(False)
15        self.setOpenLinks(False)
16
17        self.__text = ""
18        self.__permanentText = ""
19
20        self.__timer = QTimer(self, timeout=self.__on_timeout,
21                              singleShot=True)
22
23    def showHelp(self, text, timeout=0):
24        """Show help for `timeout` milliseconds. if timeout is 0 then
25        show the text until it is cleared with clearHelp or showHelp is
26        called with an empty string.
27
28        """
29        if self.__text != text:
30            self.__text = unicode(text)
31            self.__update()
32            self.textChanged.emit()
33
34        if timeout > 0:
35            self.__timer.start(timeout)
36
37    def clearHelp(self):
38        """Clear help text previously set with `showHelp`.
39        """
40        self.__timer.stop()
41        self.showHelp("")
42
43    def showPermanentHelp(self, text):
44        """Set permanent help text. The text may be temporarily
45        overridden by showHelp but will be shown again when that is cleared
46
47        """
48        if self.__permanentText != text:
49            self.__permanentText = text
50            self.__update()
51            self.textChanged.emit()
52
53    def currentText(self):
54        """Return the current shown text.
55        """
56        return self.__text or self.__permanentText
57
58    def __update(self):
59        if self.__text:
60            self.setHtml(self.__text)
61        else:
62            self.setHtml(self.__permanentText)
63
64    def __on_timeout(self):
65        if self.__text:
66            self.__text = ""
67            self.__update()
68            self.textChanged.emit()
69
70
71class QuickHelpTipEvent(QStatusTipEvent):
72    Temporary, Normal, Permanent = range(1, 4)
73
74    def __init__(self, tip, html=None, priority=Normal, timeout=None):
75        QStatusTipEvent.__init__(self, tip)
76        self.__html = html or ""
77        self.__priority = priority
78        self.__timeout = timeout
79
80    def html(self):
81        return self.__html
82
83    def priority(self):
84        return self.__priority
85
86    def timeout(self):
87        return self.__timeout
88
89
90class StatusTipPromoter(QObject):
91    """Promotes `QStatusTipEvent` to `QuickHelpTipEvent` using whatsThis
92    property of the object.
93
94    """
95    def eventFilter(self, obj, event):
96        if event.type() == QEvent.StatusTip and \
97                not isinstance(event, QuickHelpTipEvent) and \
98                hasattr(obj, "whatsThis") and \
99                isinstance(obj.whatsThis, Callable):
100            tip = event.tip()
101
102            try:
103                text = obj.whatsThis()
104            except Exception:
105                text = None
106
107            if text:
108                ev = QuickHelpTipEvent(tip, text if tip else "")
109                return QCoreApplication.sendEvent(obj, ev)
110
111        return QObject.eventFilter(self, obj, event)
Note: See TracBrowser for help on using the repository browser.