source: orange/Orange/OrangeCanvas/gui/quickhelp.py @ 11366:7f9332b11252

Revision 11366:7f9332b11252, 3.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 14 months ago (diff)

Added rst documentation for the canvas gui package.

Fixing docstrings in the process.

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