source: orange/Orange/OrangeCanvas/gui/quickhelp.py @ 11264:a5c2117750d3

Revision 11264:a5c2117750d3, 3.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 15 months ago (diff)

Providing help for widgets.

Line 
1from collections import Callable
2
3from PyQt4.QtGui import QTextBrowser, QStatusTipEvent, QWhatsThisClickedEvent
4from PyQt4.QtCore import QObject, QCoreApplication, QEvent, QTimer, QUrl
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        self.anchorClicked.connect(self.__on_anchorClicked)
23
24    def showHelp(self, text, timeout=0):
25        """Show help for `timeout` milliseconds. if timeout is 0 then
26        show the text until it is cleared with clearHelp or showHelp is
27        called with an empty string.
28
29        """
30        if self.__text != text:
31            self.__text = unicode(text)
32            self.__update()
33            self.textChanged.emit()
34
35        if timeout > 0:
36            self.__timer.start(timeout)
37
38    def clearHelp(self):
39        """Clear help text previously set with `showHelp`.
40        """
41        self.__timer.stop()
42        self.showHelp("")
43
44    def showPermanentHelp(self, text):
45        """Set permanent help text. The text may be temporarily
46        overridden by showHelp but will be shown again when that is cleared
47
48        """
49        if self.__permanentText != text:
50            self.__permanentText = text
51            self.__update()
52            self.textChanged.emit()
53
54    def currentText(self):
55        """Return the current shown text.
56        """
57        return self.__text or self.__permanentText
58
59    def __update(self):
60        if self.__text:
61            self.setHtml(self.__text)
62        else:
63            self.setHtml(self.__permanentText)
64
65    def __on_timeout(self):
66        if self.__text:
67            self.__text = ""
68            self.__update()
69            self.textChanged.emit()
70
71    def __on_anchorClicked(self, anchor):
72        ev = QuickHelpDetailRequestEvent(anchor.toString(), anchor)
73        QCoreApplication.postEvent(self, ev)
74
75
76class QuickHelpTipEvent(QStatusTipEvent):
77    Temporary, Normal, Permanent = range(1, 4)
78
79    def __init__(self, tip, html=None, priority=Normal, timeout=None):
80        QStatusTipEvent.__init__(self, tip)
81        self.__html = html or ""
82        self.__priority = priority
83        self.__timeout = timeout
84
85    def html(self):
86        return self.__html
87
88    def priority(self):
89        return self.__priority
90
91    def timeout(self):
92        return self.__timeout
93
94
95class QuickHelpDetailRequestEvent(QWhatsThisClickedEvent):
96    def __init__(self, href, url):
97        QWhatsThisClickedEvent.__init__(self, href)
98        self.__url = QUrl(url)
99
100    def url(self):
101        return QUrl(self.__url)
102
103
104class StatusTipPromoter(QObject):
105    """Promotes `QStatusTipEvent` to `QuickHelpTipEvent` using whatsThis
106    property of the object.
107
108    """
109    def eventFilter(self, obj, event):
110        if event.type() == QEvent.StatusTip and \
111                not isinstance(event, QuickHelpTipEvent) and \
112                hasattr(obj, "whatsThis") and \
113                isinstance(obj.whatsThis, Callable):
114            tip = event.tip()
115
116            try:
117                text = obj.whatsThis()
118            except Exception:
119                text = None
120
121            if text:
122                ev = QuickHelpTipEvent(tip, text if tip else "")
123                return QCoreApplication.sendEvent(obj, ev)
124
125        return QObject.eventFilter(self, obj, event)
Note: See TracBrowser for help on using the repository browser.