source: orange/Orange/OrangeCanvas/gui/splashscreen.py @ 11123:6d803a180097

Revision 11123:6d803a180097, 3.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added SplashScreen widget

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