source: orange/Orange/OrangeCanvas/gui/splashscreen.py @ 11520:9562da789a35

Revision 11520:9562da789a35, 3.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 11 months ago (diff)

Override setPixmap, ... methods on all versions of Qt.

Line 
1"""
2A splash screen widget with support for positioning of the message text.
3
4"""
5
6from PyQt4.QtGui import (
7    QSplashScreen,  QWidget, QPixmap, QPainter, QTextDocument,
8    QTextBlockFormat, QTextCursor, QApplication
9)
10from PyQt4.QtCore import Qt
11
12from .utils import is_transparency_supported
13
14
15class SplashScreen(QSplashScreen):
16    """
17    Splash screen widget.
18
19    Parameters
20    ----------
21    parent : :class:`QWidget`
22        Parent widget
23
24    pixmap : :class:`QPixmap`
25        Splash window pixmap.
26
27    textRect : :class:`QRect`
28        Bounding rectangle of the shown message on the widget.
29
30    """
31    def __init__(self, parent=None, pixmap=None, textRect=None, **kwargs):
32        QSplashScreen.__init__(self, parent, **kwargs)
33        self.__textRect = textRect
34        self.__message = ""
35        self.__color = Qt.black
36        self.__alignment = Qt.AlignLeft
37
38        if pixmap is None:
39            pixmap = QPixmap()
40
41        self.setPixmap(pixmap)
42
43        self.setAutoFillBackground(False)
44        # Also set FramelesWindowHint (if not already set)
45        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
46
47    def setTextRect(self, rect):
48        """
49        Set the rectangle (:class:`QRect`) in which to show the message text.
50        """
51        if self.__textRect != rect:
52            self.__textRect = rect
53            self.update()
54
55    def textRect(self):
56        """
57        Return the text message rectangle.
58        """
59        return self.__textRect
60
61    def showEvent(self, event):
62        QSplashScreen.showEvent(self, event)
63        # Raise to top on show.
64        self.raise_()
65
66    def drawContents(self, painter):
67        """
68        Reimplementation of drawContents to limit the drawing
69        inside `textRext`.
70
71        """
72        painter.setPen(self.__color)
73        painter.setFont(self.font())
74
75        if self.__textRect:
76            rect = self.__textRect
77        else:
78            rect = self.rect().adjusted(5, 5, -5, -5)
79        if Qt.mightBeRichText(self.__message):
80            doc = QTextDocument()
81            doc.setHtml(self.__message)
82            doc.setTextWidth(rect.width())
83            cursor = QTextCursor(doc)
84            cursor.select(QTextCursor.Document)
85            fmt = QTextBlockFormat()
86            fmt.setAlignment(self.__alignment)
87            cursor.mergeBlockFormat(fmt)
88            painter.save()
89            painter.translate(rect.topLeft())
90            doc.drawContents(painter)
91            painter.restore()
92        else:
93            painter.drawText(rect, self.__alignment, self.__message)
94
95    def showMessage(self, message, alignment=Qt.AlignLeft, color=Qt.black):
96        """
97        Show the `message` with `color` and `alignment`.
98        """
99        # Need to store all this arguments for drawContents (no access
100        # methods)
101        self.__alignment = alignment
102        self.__color = color
103        self.__message = message
104        QSplashScreen.showMessage(self, message, alignment, color)
105        QApplication.instance().processEvents()
106
107    # Reimplemented to allow graceful fall back if the windowing system
108    # does not support transparency.
109    def setPixmap(self, pixmap):
110        self.setAttribute(Qt.WA_TranslucentBackground,
111                          pixmap.hasAlpha() and \
112                          is_transparency_supported())
113
114        self.__pixmap = pixmap
115
116        QSplashScreen.setPixmap(self, pixmap)
117        if pixmap.hasAlpha() and not is_transparency_supported():
118            self.setMask(pixmap.createHeuristicMask())
119
120    def repaint(self):
121        QWidget.repaint(self)
122        QApplication.flush()
123
124    def event(self, event):
125        if event.type() == event.Paint:
126            pixmap = self.__pixmap
127            painter = QPainter(self)
128            if not pixmap.isNull():
129                painter.drawPixmap(0, 0, pixmap)
130            self.drawContents(painter)
131            return True
132        return QSplashScreen.event(self, event)
Note: See TracBrowser for help on using the repository browser.