def __init__(self, parent=None): super(XOverlayWidget, self).__init__(parent) # define custom properties self._centralWidget = None self._result = None self._closable = True self._closeAlignment = QtCore.Qt.AlignTop | QtCore.Qt.AlignRight self._closeButton = XToolButton(self) self._closeButton.setShadowed(True) self._closeButton.setIcon( QtGui.QIcon(resources.find('img/overlay/close.png'))) self._closeButton.setIconSize(QtCore.QSize(24, 24)) self._closeButton.setToolTip('Close') # create the coloring for the overlay palette = self.palette() clr = QtGui.QColor('#222222') clr.setAlpha(210) palette.setColor(palette.Window, clr) self.setPalette(palette) self.setAutoFillBackground(True) # listen to the parents event filter parent.installEventFilter(self) # initialize the widget self.hide() self.move(0, 0) self.resize(parent.size()) self._closeButton.clicked.connect(self.reject)
def __init__(self, parent=None): super(XOverlayWidget, self).__init__(parent) # define custom properties self._centralWidget = None self._result = None self._closable = True self._closeAlignment = QtCore.Qt.AlignTop | QtCore.Qt.AlignRight self._closeButton = XToolButton(self) self._closeButton.setShadowed(True) self._closeButton.setIcon(QtGui.QIcon(resources.find('img/overlay/close.png'))) self._closeButton.setIconSize(QtCore.QSize(24, 24)) self._closeButton.setToolTip('Close') # create the coloring for the overlay palette = self.palette() clr = QtGui.QColor('#222222') clr.setAlpha(210) palette.setColor(palette.Window, clr) self.setPalette(palette) self.setAutoFillBackground(True) # listen to the parents event filter parent.installEventFilter(self) # initialize the widget self.hide() self.move(0, 0) self.resize(parent.size()) self._closeButton.clicked.connect(self.reject)
class XOverlayWidget(QtGui.QWidget): finished = QtCore.Signal(int) def __init__(self, parent=None): super(XOverlayWidget, self).__init__(parent) # define custom properties self._centralWidget = None self._result = None self._closable = True self._closeAlignment = QtCore.Qt.AlignTop | QtCore.Qt.AlignRight self._closeButton = XToolButton(self) self._closeButton.setShadowed(True) self._closeButton.setIcon( QtGui.QIcon(resources.find('img/overlay/close.png'))) self._closeButton.setIconSize(QtCore.QSize(24, 24)) self._closeButton.setToolTip('Close') # create the coloring for the overlay palette = self.palette() clr = QtGui.QColor('#222222') clr.setAlpha(210) palette.setColor(palette.Window, clr) self.setPalette(palette) self.setAutoFillBackground(True) # listen to the parents event filter parent.installEventFilter(self) # initialize the widget self.hide() self.move(0, 0) self.resize(parent.size()) self._closeButton.clicked.connect(self.reject) def accept(self): """ Accepts this overlay and exits the modal window. """ self.close() self.setResult(1) self.finished.emit(1) def adjustSize(self): """ Adjusts the size of this widget as the parent resizes. """ # adjust the close button align = self.closeAlignment() if align & QtCore.Qt.AlignTop: y = 6 else: y = self.height() - 38 if align & QtCore.Qt.AlignLeft: x = 6 else: x = self.width() - 38 self._closeButton.move(x, y) # adjust the central widget widget = self.centralWidget() if widget is not None: center = self.rect().center() widget.move(center.x() - widget.width() / 2, center.y() - widget.height() / 2) def closeAlignment(self): """ Returns the alignment for the close button for this overlay widget. :return <QtCore.Qt.Alignment> """ return self._closeAlignment def centralWidget(self): """ Returns the central widget for this overlay. If there is one, then it will be automatically moved with this object. :return <QtGui.QWidget> """ return self._centralWidget def isClosable(self): """ Returns whether or not the user should be able to close this overlay widget. :return <bool> """ return self._closable def keyPressEvent(self, event): """ Exits the modal window on an escape press. :param event | <QtCore.QKeyPressEvent> """ if event.key() == QtCore.Qt.Key_Escape: self.reject() super(XOverlayWidget, self).keyPressEvent(event) def eventFilter(self, object, event): """ Resizes this overlay as the widget resizes. :param object | <QtCore.QObject> event | <QtCore.QEvent> :return <bool> """ if object == self.parent() and event.type() == QtCore.QEvent.Resize: self.resize(event.size()) elif event.type() == QtCore.QEvent.Close: self.setResult(0) return False def exec_(self, autodelete=True): self.setAttribute(QtCore.Qt.WA_DeleteOnClose, False) if self.centralWidget(): QtCore.QTimer.singleShot(0, self.centralWidget().setFocus) loop = QtCore.QEventLoop() while self.isVisible() and not QtCore.QCoreApplication.closingDown(): loop.processEvents() if autodelete: self.deleteLater() return self.result() def reject(self): """ Rejects this overlay and exits the modal window. """ self.close() self.setResult(1) self.finished.emit(1) def result(self): """ Returns the result from this overlay widget. :return <int> """ return int(self._result) def resizeEvent(self, event): """ Handles a resize event for this overlay, centering the central widget if one is found. :param event | <QtCore.QEvent> """ super(XOverlayWidget, self).resizeEvent(event) self.adjustSize() def setCentralWidget(self, widget): """ Sets the central widget for this overlay to the inputed widget. :param widget | <QtGui.QWidget> """ self._centralWidget = widget if widget is not None: widget.setParent(self) widget.installEventFilter(self) # create the drop shadow effect effect = QtGui.QGraphicsDropShadowEffect(self) effect.setColor(QtGui.QColor('black')) effect.setBlurRadius(80) effect.setOffset(0, 0) widget.setGraphicsEffect(effect) def setClosable(self, state): """ Sets whether or not the user should be able to close this overlay widget. :param state | <bool> """ self._closable = state if state: self._closeButton.show() else: self._closeButton.hide() def setCloseAlignment(self, align): """ Sets the alignment for the close button for this overlay widget. :param align | <QtCore.Qt.Alignment> """ self._closeAlignment = align def setResult(self, result): """ Sets the result for this overlay to the inputed value. :param result | <int> """ self._result = result def setVisible(self, state): """ Closes this widget and kills the result. """ super(XOverlayWidget, self).setVisible(state) if not state: self.setResult(0) def showEvent(self, event): """ Ensures this widget is the top-most widget for its parent. :param event | <QtCore.QEvent> """ super(XOverlayWidget, self).showEvent(event) # raise to the top self.raise_() self._closeButton.setVisible(self.isClosable()) widget = self.centralWidget() if widget: center = self.rect().center() start_x = end_x = center.x() - widget.width() / 2 start_y = -widget.height() end_y = center.y() - widget.height() / 2 start = QtCore.QPoint(start_x, start_y) end = QtCore.QPoint(end_x, end_y) # create the movement animation anim = QtCore.QPropertyAnimation(self) anim.setPropertyName('pos') anim.setTargetObject(widget) anim.setStartValue(start) anim.setEndValue(end) anim.setDuration(500) anim.setEasingCurve(QtCore.QEasingCurve.InOutQuad) anim.finished.connect(anim.deleteLater) anim.start() @staticmethod def modal(widget, parent=None, align=QtCore.Qt.AlignTop | QtCore.Qt.AlignRight, blurred=True): """ Creates a modal dialog for this overlay with the inputed widget. If the user accepts the widget, then 1 will be returned, otherwise, 0 will be returned. :param widget | <QtCore.QWidget> """ if parent is None: parent = QtGui.QApplication.instance().activeWindow() overlay = XOverlayWidget(parent) overlay.setAttribute(QtCore.Qt.WA_DeleteOnClose) overlay.setCentralWidget(widget) overlay.setCloseAlignment(align) overlay.show() return overlay
def __init__(self, parent, windowFlags=0): windowFlags = QtCore.Qt.WindowFlags(windowFlags) super(XMdiSubWindow, self).__init__(parent, windowFlags) # define custom properties palette = self.palette() font = self.font() font.setBold(True) font.setPointSize(font.pointSize() + 2) self._titleBarFont = font self._titleBarBackground = palette.color(palette.Button) self._titleBarForeground = palette.color(palette.ButtonText) self._titleBarBorder = QtGui.QColor('black') # create the drop shadow effect eff = QtGui.QGraphicsDropShadowEffect(self) eff.setOffset(0, 0) eff.setBlurRadius(40) eff.setColor(palette.color(palette.Shadow)) self.setGraphicsEffect(eff) # create the control buttons self._sysmenuBtn = XToolButton(self) self._sysmenuBtn.setIcon(self.windowIcon()) self._sysmenuBtn.setPalette(palette) self._sysmenuBtn.setAutoRaise(True) self._sysmenuBtn.setFixedSize(QtCore.QSize(22, 22)) self._sysmenuBtn.move(4, 4) self._sysmenuBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('yellow')) self._minimizeBtn = XToolButton(self) self._minimizeBtn.setIcon( QtGui.QIcon(resources.find('img/mdiarea/minimize.png'))) self._minimizeBtn.setPalette(palette) self._minimizeBtn.setShadowed(True) self._minimizeBtn.setShadowRadius(10) self._minimizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._minimizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('orange')) self._maximizeBtn = XToolButton(self) self._maximizeBtn.setIcon( QtGui.QIcon(resources.find('img/mdiarea/maximize.png'))) self._maximizeBtn.setPalette(palette) self._maximizeBtn.setShadowed(True) self._maximizeBtn.setShadowRadius(10) self._maximizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._maximizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('red')) self._closeBtn = XToolButton(self) self._closeBtn.setIcon( QtGui.QIcon(resources.find('img/mdiarea/close.png'))) self._closeBtn.setPalette(palette) self._closeBtn.setShadowed(True) self._closeBtn.setShadowRadius(10) self._closeBtn.setFixedSize(QtCore.QSize(22, 22)) self._closeBtn.show() # create connections self._sysmenuBtn.clicked.connect(self.showSystemMenu) self._minimizeBtn.clicked.connect(self.toggleMinimized) self._maximizeBtn.clicked.connect(self.toggleMaximized) self._closeBtn.clicked.connect(self.close)
class XOverlayWidget(QtGui.QWidget): finished = QtCore.Signal(int) def __init__(self, parent=None): super(XOverlayWidget, self).__init__(parent) # define custom properties self._centralWidget = None self._result = None self._closable = True self._closeAlignment = QtCore.Qt.AlignTop | QtCore.Qt.AlignRight self._closeButton = XToolButton(self) self._closeButton.setShadowed(True) self._closeButton.setIcon(QtGui.QIcon(resources.find('img/overlay/close.png'))) self._closeButton.setIconSize(QtCore.QSize(24, 24)) self._closeButton.setToolTip('Close') # create the coloring for the overlay palette = self.palette() clr = QtGui.QColor('#222222') clr.setAlpha(210) palette.setColor(palette.Window, clr) self.setPalette(palette) self.setAutoFillBackground(True) # listen to the parents event filter parent.installEventFilter(self) # initialize the widget self.hide() self.move(0, 0) self.resize(parent.size()) self._closeButton.clicked.connect(self.reject) def accept(self): """ Accepts this overlay and exits the modal window. """ self.close() self.setResult(1) self.finished.emit(1) def adjustSize(self): """ Adjusts the size of this widget as the parent resizes. """ # adjust the close button align = self.closeAlignment() if align & QtCore.Qt.AlignTop: y = 6 else: y = self.height() - 38 if align & QtCore.Qt.AlignLeft: x = 6 else: x = self.width() - 38 self._closeButton.move(x, y) # adjust the central widget widget = self.centralWidget() if widget is not None: center = self.rect().center() widget.move(center.x() - widget.width() / 2, center.y() - widget.height() / 2) def closeAlignment(self): """ Returns the alignment for the close button for this overlay widget. :return <QtCore.Qt.Alignment> """ return self._closeAlignment def centralWidget(self): """ Returns the central widget for this overlay. If there is one, then it will be automatically moved with this object. :return <QtGui.QWidget> """ return self._centralWidget def isClosable(self): """ Returns whether or not the user should be able to close this overlay widget. :return <bool> """ return self._closable def keyPressEvent(self, event): """ Exits the modal window on an escape press. :param event | <QtCore.QKeyPressEvent> """ if event.key() == QtCore.Qt.Key_Escape: self.reject() super(XOverlayWidget, self).keyPressEvent(event) def eventFilter(self, object, event): """ Resizes this overlay as the widget resizes. :param object | <QtCore.QObject> event | <QtCore.QEvent> :return <bool> """ if object == self.parent() and event.type() == QtCore.QEvent.Resize: self.resize(event.size()) elif event.type() == QtCore.QEvent.Close: self.setResult(0) return False def exec_(self, autodelete=True): self.setAttribute(QtCore.Qt.WA_DeleteOnClose, False) if self.centralWidget(): QtCore.QTimer.singleShot(0, self.centralWidget().setFocus) loop = QtCore.QEventLoop() while self.isVisible() and not QtCore.QCoreApplication.closingDown(): loop.processEvents() if autodelete: self.deleteLater() return self.result() def reject(self): """ Rejects this overlay and exits the modal window. """ self.close() self.setResult(0) self.finished.emit(0) def result(self): """ Returns the result from this overlay widget. :return <int> """ return int(self._result) def resizeEvent(self, event): """ Handles a resize event for this overlay, centering the central widget if one is found. :param event | <QtCore.QEvent> """ super(XOverlayWidget, self).resizeEvent(event) self.adjustSize() def setCentralWidget(self, widget): """ Sets the central widget for this overlay to the inputed widget. :param widget | <QtGui.QWidget> """ self._centralWidget = widget if widget is not None: widget.setParent(self) widget.installEventFilter(self) # create the drop shadow effect effect = QtGui.QGraphicsDropShadowEffect(self) effect.setColor(QtGui.QColor('black')) effect.setBlurRadius(80) effect.setOffset(0, 0) widget.setGraphicsEffect(effect) def setClosable(self, state): """ Sets whether or not the user should be able to close this overlay widget. :param state | <bool> """ self._closable = state if state: self._closeButton.show() else: self._closeButton.hide() def setCloseAlignment(self, align): """ Sets the alignment for the close button for this overlay widget. :param align | <QtCore.Qt.Alignment> """ self._closeAlignment = align def setResult(self, result): """ Sets the result for this overlay to the inputed value. :param result | <int> """ self._result = result def setVisible(self, state): """ Closes this widget and kills the result. """ super(XOverlayWidget, self).setVisible(state) if not state: self.setResult(0) def showEvent(self, event): """ Ensures this widget is the top-most widget for its parent. :param event | <QtCore.QEvent> """ super(XOverlayWidget, self).showEvent(event) # raise to the top self.raise_() self._closeButton.setVisible(self.isClosable()) widget = self.centralWidget() if widget: center = self.rect().center() start_x = end_x = center.x() - widget.width() / 2 start_y = -widget.height() end_y = center.y() - widget.height() / 2 start = QtCore.QPoint(start_x, start_y) end = QtCore.QPoint(end_x, end_y) # create the movement animation anim = QtCore.QPropertyAnimation(self) anim.setPropertyName('pos') anim.setTargetObject(widget) anim.setStartValue(start) anim.setEndValue(end) anim.setDuration(500) anim.setEasingCurve(QtCore.QEasingCurve.InOutQuad) anim.finished.connect(anim.deleteLater) anim.start() @staticmethod def modal(widget, parent=None, align=QtCore.Qt.AlignTop | QtCore.Qt.AlignRight, blurred=True): """ Creates a modal dialog for this overlay with the inputed widget. If the user accepts the widget, then 1 will be returned, otherwise, 0 will be returned. :param widget | <QtCore.QWidget> """ if parent is None: parent = QtGui.QApplication.instance().activeWindow() overlay = XOverlayWidget(parent) overlay.setAttribute(QtCore.Qt.WA_DeleteOnClose) overlay.setCentralWidget(widget) overlay.setCloseAlignment(align) overlay.show() return overlay
class XMdiSubWindow(QtGui.QMdiSubWindow): def __init__(self, parent, windowFlags=0): windowFlags = QtCore.Qt.WindowFlags(windowFlags) super(XMdiSubWindow, self).__init__(parent, windowFlags) # define custom properties palette = self.palette() font = self.font() font.setBold(True) font.setPointSize(font.pointSize() + 2) self._titleBarFont = font self._titleBarBackground = palette.color(palette.Button) self._titleBarForeground = palette.color(palette.ButtonText) self._titleBarBorder = QtGui.QColor('black') # create the drop shadow effect eff = QtGui.QGraphicsDropShadowEffect(self) eff.setOffset(0, 0) eff.setBlurRadius(40) eff.setColor(palette.color(palette.Shadow)) self.setGraphicsEffect(eff) # create the control buttons self._sysmenuBtn = XToolButton(self) self._sysmenuBtn.setIcon(self.windowIcon()) self._sysmenuBtn.setPalette(palette) self._sysmenuBtn.setAutoRaise(True) self._sysmenuBtn.setFixedSize(QtCore.QSize(22, 22)) self._sysmenuBtn.move(4, 4) self._sysmenuBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('yellow')) self._minimizeBtn = XToolButton(self) self._minimizeBtn.setIcon( QtGui.QIcon(resources.find('img/mdiarea/minimize.png'))) self._minimizeBtn.setPalette(palette) self._minimizeBtn.setShadowed(True) self._minimizeBtn.setShadowRadius(10) self._minimizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._minimizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('orange')) self._maximizeBtn = XToolButton(self) self._maximizeBtn.setIcon( QtGui.QIcon(resources.find('img/mdiarea/maximize.png'))) self._maximizeBtn.setPalette(palette) self._maximizeBtn.setShadowed(True) self._maximizeBtn.setShadowRadius(10) self._maximizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._maximizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('red')) self._closeBtn = XToolButton(self) self._closeBtn.setIcon( QtGui.QIcon(resources.find('img/mdiarea/close.png'))) self._closeBtn.setPalette(palette) self._closeBtn.setShadowed(True) self._closeBtn.setShadowRadius(10) self._closeBtn.setFixedSize(QtCore.QSize(22, 22)) self._closeBtn.show() # create connections self._sysmenuBtn.clicked.connect(self.showSystemMenu) self._minimizeBtn.clicked.connect(self.toggleMinimized) self._maximizeBtn.clicked.connect(self.toggleMaximized) self._closeBtn.clicked.connect(self.close) def paintEvent(self, event): super(XMdiSubWindow, self).paintEvent(event) palette = self.palette() # draw the title with XPainter(self) as painter: painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setPen(self.titleBarBorder()) painter.setBrush(self.titleBarBackground()) painter.drawRect(0, 0, self.width(), 29) grad = QtGui.QLinearGradient() grad.setColorAt(0, QtGui.QColor(255, 255, 255, 30)) grad.setColorAt(1, QtGui.QColor(0, 0, 0, 28)) grad.setStart(0, 0) grad.setFinalStop(0, 30) painter.setPen(QtCore.Qt.NoPen) painter.setBrush(grad) painter.drawRect(0, 0, self.width(), 30) # draw the text painter.setFont(self.titleBarFont()) bg = self.titleBarBackground() painter.setPen(bg.lighter(110)) painter.drawText(45, 1, self.width(), 30, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, self.windowTitle()) painter.setPen(self.titleBarForeground()) painter.drawText(45, 0, self.width(), 30, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, self.windowTitle()) def resizeEvent(self, event): super(XMdiSubWindow, self).resizeEvent(event) self._minimizeBtn.move(event.size().width() - 70, 4) self._maximizeBtn.move(event.size().width() - 50, 4) self._closeBtn.move(event.size().width() - 30, 4) def setTitleBarBackground(self, background): """ Sets the background color for this titlebar. :param background | <QtGui.QColor> || <QtGui.QBrush> """ self._titleBarBackground = background def setTitleBarBorder(self, border): """ Sets the border color for this titlebar. :param border | <QtGui.QColor> """ self._titleBarBorder = border def setTitleBarForeground(self, foreground): """ Sets the foreground color for this titlebar. :param foreground | <QtGui.QColor> || <QtGui.QPen> """ self._titleBarForeground = foreground def setTitleBarFont(self, font): """ Sets the font color for this titlebar. :param font | <QtGui.QFont> """ self._titleBarFont = font def setWindowIcon(self, icon): super(XMdiSubWindow, self).setWindowIcon(icon) self._sysmenuBtn.setIcon(icon) def toggleMinimized(self): if self.isMinimized() or self.isMaximized(): self.showNormal() else: self.showMinimized() def toggleMaximized(self): if self.isMinimized() or self.isMaximized(): self.showNormal() else: self.showMaximized() def titleBarBackground(self): """ Returns the background color for this titlebar. :return <QtGui.QColor> || <QtGui.QBrush> """ return self._titleBarBackground def titleBarBorder(self): """ Returns the border color for this titlebar. :return <QtGui.QColor> """ return self._titleBarBorder def titleBarForeground(self): """ Returns the foreground color for this titlebar. :return <QtGui.QColor> || <QtGui.QPen> """ return self._titleBarForeground def titleBarFont(self): """ Returns the font color for this titlebar. :return <QtGui.QFont> """ return self._titleBarFont
def __init__(self, parent, windowFlags=0): windowFlags = QtCore.Qt.WindowFlags(windowFlags) super(XMdiSubWindow, self).__init__(parent, windowFlags) # define custom properties palette = self.palette() font = self.font() font.setBold(True) font.setPointSize(font.pointSize() + 2) self._titleBarFont = font self._titleBarBackground = palette.color(palette.Button) self._titleBarForeground = palette.color(palette.ButtonText) self._titleBarBorder = QtGui.QColor('black') # create the drop shadow effect eff = QtGui.QGraphicsDropShadowEffect(self) eff.setOffset(0, 0) eff.setBlurRadius(40) eff.setColor(palette.color(palette.Shadow)) self.setGraphicsEffect(eff) # create the control buttons self._sysmenuBtn = XToolButton(self) self._sysmenuBtn.setIcon(self.windowIcon()) self._sysmenuBtn.setPalette(palette) self._sysmenuBtn.setAutoRaise(True) self._sysmenuBtn.setFixedSize(QtCore.QSize(22, 22)) self._sysmenuBtn.move(4, 4) self._sysmenuBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('yellow')) self._minimizeBtn = XToolButton(self) self._minimizeBtn.setIcon(QtGui.QIcon(resources.find('img/mdiarea/minimize.png'))) self._minimizeBtn.setPalette(palette) self._minimizeBtn.setShadowed(True) self._minimizeBtn.setShadowRadius(10) self._minimizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._minimizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('orange')) self._maximizeBtn = XToolButton(self) self._maximizeBtn.setIcon(QtGui.QIcon(resources.find('img/mdiarea/maximize.png'))) self._maximizeBtn.setPalette(palette) self._maximizeBtn.setShadowed(True) self._maximizeBtn.setShadowRadius(10) self._maximizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._maximizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('red')) self._closeBtn = XToolButton(self) self._closeBtn.setIcon(QtGui.QIcon(resources.find('img/mdiarea/close.png'))) self._closeBtn.setPalette(palette) self._closeBtn.setShadowed(True) self._closeBtn.setShadowRadius(10) self._closeBtn.setFixedSize(QtCore.QSize(22, 22)) self._closeBtn.show() # create connections self._sysmenuBtn.clicked.connect(self.showSystemMenu) self._minimizeBtn.clicked.connect(self.toggleMinimized) self._maximizeBtn.clicked.connect(self.toggleMaximized) self._closeBtn.clicked.connect(self.close)
class XMdiSubWindow(QtGui.QMdiSubWindow): def __init__(self, parent, windowFlags=0): windowFlags = QtCore.Qt.WindowFlags(windowFlags) super(XMdiSubWindow, self).__init__(parent, windowFlags) # define custom properties palette = self.palette() font = self.font() font.setBold(True) font.setPointSize(font.pointSize() + 2) self._titleBarFont = font self._titleBarBackground = palette.color(palette.Button) self._titleBarForeground = palette.color(palette.ButtonText) self._titleBarBorder = QtGui.QColor('black') # create the drop shadow effect eff = QtGui.QGraphicsDropShadowEffect(self) eff.setOffset(0, 0) eff.setBlurRadius(40) eff.setColor(palette.color(palette.Shadow)) self.setGraphicsEffect(eff) # create the control buttons self._sysmenuBtn = XToolButton(self) self._sysmenuBtn.setIcon(self.windowIcon()) self._sysmenuBtn.setPalette(palette) self._sysmenuBtn.setAutoRaise(True) self._sysmenuBtn.setFixedSize(QtCore.QSize(22, 22)) self._sysmenuBtn.move(4, 4) self._sysmenuBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('yellow')) self._minimizeBtn = XToolButton(self) self._minimizeBtn.setIcon(QtGui.QIcon(resources.find('img/mdiarea/minimize.png'))) self._minimizeBtn.setPalette(palette) self._minimizeBtn.setShadowed(True) self._minimizeBtn.setShadowRadius(10) self._minimizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._minimizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('orange')) self._maximizeBtn = XToolButton(self) self._maximizeBtn.setIcon(QtGui.QIcon(resources.find('img/mdiarea/maximize.png'))) self._maximizeBtn.setPalette(palette) self._maximizeBtn.setShadowed(True) self._maximizeBtn.setShadowRadius(10) self._maximizeBtn.setFixedSize(QtCore.QSize(22, 22)) self._maximizeBtn.show() palette.setColor(palette.Shadow, QtGui.QColor('red')) self._closeBtn = XToolButton(self) self._closeBtn.setIcon(QtGui.QIcon(resources.find('img/mdiarea/close.png'))) self._closeBtn.setPalette(palette) self._closeBtn.setShadowed(True) self._closeBtn.setShadowRadius(10) self._closeBtn.setFixedSize(QtCore.QSize(22, 22)) self._closeBtn.show() # create connections self._sysmenuBtn.clicked.connect(self.showSystemMenu) self._minimizeBtn.clicked.connect(self.toggleMinimized) self._maximizeBtn.clicked.connect(self.toggleMaximized) self._closeBtn.clicked.connect(self.close) def paintEvent(self, event): super(XMdiSubWindow, self).paintEvent(event) palette = self.palette() # draw the title with XPainter(self) as painter: painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setPen(self.titleBarBorder()) painter.setBrush(self.titleBarBackground()) painter.drawRect(0, 0, self.width(), 29) grad = QtGui.QLinearGradient() grad.setColorAt(0, QtGui.QColor(255, 255, 255, 30)) grad.setColorAt(1, QtGui.QColor(0, 0, 0, 28)) grad.setStart(0, 0) grad.setFinalStop(0, 30) painter.setPen(QtCore.Qt.NoPen) painter.setBrush(grad) painter.drawRect(0, 0, self.width(), 30) # draw the text painter.setFont(self.titleBarFont()) bg = self.titleBarBackground() painter.setPen(bg.lighter(110)) painter.drawText(45, 1, self.width(), 30, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, self.windowTitle()) painter.setPen(self.titleBarForeground()) painter.drawText(45, 0, self.width(), 30, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, self.windowTitle()) def resizeEvent(self, event): super(XMdiSubWindow, self).resizeEvent(event) self._minimizeBtn.move(event.size().width() - 70, 4) self._maximizeBtn.move(event.size().width() - 50, 4) self._closeBtn.move(event.size().width() - 30, 4) def setTitleBarBackground(self, background): """ Sets the background color for this titlebar. :param background | <QtGui.QColor> || <QtGui.QBrush> """ self._titleBarBackground = background def setTitleBarBorder(self, border): """ Sets the border color for this titlebar. :param border | <QtGui.QColor> """ self._titleBarBorder = border def setTitleBarForeground(self, foreground): """ Sets the foreground color for this titlebar. :param foreground | <QtGui.QColor> || <QtGui.QPen> """ self._titleBarForeground = foreground def setTitleBarFont(self, font): """ Sets the font color for this titlebar. :param font | <QtGui.QFont> """ self._titleBarFont = font def setWindowIcon(self, icon): super(XMdiSubWindow, self).setWindowIcon(icon) self._sysmenuBtn.setIcon(icon) def toggleMinimized(self): if self.isMinimized() or self.isMaximized(): self.showNormal() else: self.showMinimized() def toggleMaximized(self): if self.isMinimized() or self.isMaximized(): self.showNormal() else: self.showMaximized() def titleBarBackground(self): """ Returns the background color for this titlebar. :return <QtGui.QColor> || <QtGui.QBrush> """ return self._titleBarBackground def titleBarBorder(self): """ Returns the border color for this titlebar. :return <QtGui.QColor> """ return self._titleBarBorder def titleBarForeground(self): """ Returns the foreground color for this titlebar. :return <QtGui.QColor> || <QtGui.QPen> """ return self._titleBarForeground def titleBarFont(self): """ Returns the font color for this titlebar. :return <QtGui.QFont> """ return self._titleBarFont
def createWidget(self, parent): btn = XToolButton(parent) btn.setDefaultAction(self) btn.setCheckable(self.isCheckable()) btn.setChecked(self.isChecked()) return btn