source: orange/Orange/OrangeCanvas/gui/framelesswindow.py @ 11170:dcc12da18030

Revision 11170:dcc12da18030, 2.2 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added fallback using 'setMask' to FramelessWindow.

Line 
1"""
2A Frameless window widget
3
4"""
5
6from PyQt4.QtGui import QWidget, QPalette, QPainter, QStyleOption, QBitmap
7
8from PyQt4.QtCore import Qt, pyqtProperty as Property
9
10from .utils import is_transparency_supported, StyledWidget_paintEvent
11
12
13class FramelessWindow(QWidget):
14    """A Basic frameless window widget with rounded corners (if supported by
15    the windowing system).
16
17    """
18    def __init__(self, parent=None, **kwargs):
19        QWidget.__init__(self, parent, **kwargs)
20        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
21
22        self.__radius = 6
23        self.__isTransparencySupported = is_transparency_supported()
24        self.setAttribute(Qt.WA_TranslucentBackground,
25                          self.__isTransparencySupported)
26
27    def setRadius(self, radius):
28        """Set the window rounded border radius.
29        """
30        if self.__radius != radius:
31            self.__radius = radius
32            if not self.__isTransparencySupported:
33                self.__updateMask()
34            self.update()
35
36    def radius(self):
37        """Return the border radius.
38        """
39        return self.__radius
40
41    radius_ = Property(int, fget=radius, fset=setRadius,
42                       designable=True)
43
44    def resizeEvent(self, event):
45        QWidget.resizeEvent(self, event)
46        if not self.__isTransparencySupported:
47            self.__updateMask()
48
49    def __updateMask(self):
50        opt = QStyleOption()
51        opt.init(self)
52        rect = opt.rect
53
54        size = rect.size()
55        mask = QBitmap(size)
56
57        p = QPainter(mask)
58        p.setRenderHint(QPainter.Antialiasing)
59        p.setBrush(Qt.black)
60        p.setPen(Qt.NoPen)
61        p.drawRoundedRect(rect, self.__radius, self.__radius)
62        p.end()
63
64        self.setMask(mask)
65
66    def paintEvent(self, event):
67        if self.__isTransparencySupported:
68            opt = QStyleOption()
69            opt.init(self)
70            rect = opt.rect
71
72            p = QPainter(self)
73            p.setRenderHint(QPainter.Antialiasing, True)
74            p.setBrush(opt.palette.brush(QPalette.Window))
75            p.setPen(Qt.NoPen)
76            p.drawRoundedRect(rect, self.__radius, self.__radius)
77            p.end()
78        else:
79            StyledWidget_paintEvent(self, event)
Note: See TracBrowser for help on using the repository browser.