def focusInEvent(self, event): # reimplemented event.accept() if self.__focusframe is None: self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) palette = self.palette() palette.setColor(QPalette.Foreground, palette.color(QPalette.Highlight)) self.__focusframe.setPalette(palette)
class SimpleButton(QAbstractButton): """ A simple icon button widget. """ def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.__focusframe = None def focusInEvent(self, event): # reimplemented event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) def focusOutEvent(self, event): # reimplemented event.accept() self.__focusframe.deleteLater() self.__focusframe = None def sizeHint(self): # reimplemented self.ensurePolished() iconsize = self.iconSize() icon = self.icon() if not icon.isNull(): iconsize = icon.actualSize(iconsize) return iconsize def minimumSizeHint(self): # reimplemented return self.sizeHint() def paintEvent(self, event): # reimplemented painter = QStylePainter(self) option = QStyleOptionButton() option.initFrom(self) option.icon = self.icon() option.iconSize = self.iconSize() icon = self.icon() if not icon.isNull(): if option.state & QStyle.State_Active: mode = (QIcon.Normal if option.state & QStyle.State_MouseOver else QIcon.Active) else: mode = QIcon.Disabled pixmap = icon.pixmap( option.iconSize, mode, ) painter.drawItemPixmap(option.rect, Qt.AlignCenter, pixmap)
class SimpleButton(QAbstractButton): """ A simple icon button widget. """ def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.__focusframe = None def focusInEvent(self, event): # reimplemented event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) def focusOutEvent(self, event): # reimplemented event.accept() self.__focusframe.deleteLater() self.__focusframe = None def sizeHint(self): # reimplemented self.ensurePolished() iconsize = self.iconSize() icon = self.icon() if not icon.isNull(): iconsize = icon.actualSize(iconsize) return iconsize def minimumSizeHint(self): # reimplemented return self.sizeHint() def paintEvent(self, event): # reimplemented painter = QStylePainter(self) option = QStyleOptionButton() option.initFrom(self) option.icon = self.icon() option.iconSize = self.iconSize() icon = self.icon() if not icon.isNull(): if option.state & QStyle.State_Active: mode = (QIcon.Normal if option.state & QStyle.State_MouseOver else QIcon.Active) else: mode = QIcon.Disabled pixmap = icon.pixmap(option.iconSize, mode, ) painter.drawItemPixmap(option.rect, Qt.AlignCenter, pixmap)
def focusInEvent(self, event): event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) self.__deleteaction.setEnabled(True)
class Frame(QDockWidget): """ Widget frame with a handle. """ closeRequested = Signal() def __init__(self, parent=None, widget=None, title=None, **kwargs): super().__init__(parent, **kwargs) self.setFeatures(QDockWidget.DockWidgetClosable) self.setAllowedAreas(Qt.NoDockWidgetArea) self.__title = "" self.__icon = "" self.__focusframe = None self.__deleteaction = QAction("Remove", self, shortcut=QKeySequence.Delete, enabled=False, triggered=self.closeRequested) self.addAction(self.__deleteaction) if widget is not None: self.setWidget(widget) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) if title: self.setTitle(title) self.setFocusPolicy(Qt.StrongFocus) def setTitle(self, title): if self.__title != title: self.__title = title self.setWindowTitle(title) self.update() def setIcon(self, icon): icon = QIcon(icon) if self.__icon != icon: self.__icon = icon self.setWindowIcon(icon) self.update() def paintEvent(self, event): super().paintEvent(event) painter = QStylePainter(self) opt = QStyleOptionFrame() opt.initFrom(self) painter.drawPrimitive(QStyle.PE_FrameDockWidget, opt) painter.end() def focusInEvent(self, event): event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) self.__deleteaction.setEnabled(True) def focusOutEvent(self, event): event.accept() if self.__focusframe is not None: self.__focusframe.deleteLater() self.__focusframe = None self.__deleteaction.setEnabled(False) def closeEvent(self, event): super().closeEvent(event) event.ignore() self.closeRequested.emit()
def focusInEvent(self, event): # reimplemented event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self)
class Frame(QDockWidget): """ Widget frame with a handle. """ closeRequested = Signal() def __init__(self, parent=None, widget=None, title=None, **kwargs): super().__init__(parent, **kwargs) self.setFeatures(QDockWidget.DockWidgetClosable) self.setAllowedAreas(Qt.NoDockWidgetArea) self.__title = "" self.__icon = "" self.__focusframe = None self.__deleteaction = QAction( "Remove", self, shortcut=QKeySequence.Delete, enabled=False, triggered=self.closeRequested ) self.addAction(self.__deleteaction) if widget is not None: self.setWidget(widget) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) if title: self.setTitle(title) self.setFocusPolicy(Qt.ClickFocus | Qt.TabFocus) def setTitle(self, title): if self.__title != title: self.__title = title self.setWindowTitle(title) self.update() def setIcon(self, icon): icon = QIcon(icon) if self.__icon != icon: self.__icon = icon self.setWindowIcon(icon) self.update() def paintEvent(self, event): super().paintEvent(event) painter = QStylePainter(self) opt = QStyleOptionFrame() opt.initFrom(self) painter.drawPrimitive(QStyle.PE_FrameDockWidget, opt) painter.end() def focusInEvent(self, event): event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) self.__deleteaction.setEnabled(True) def focusOutEvent(self, event): event.accept() if self.__focusframe is not None: self.__focusframe.deleteLater() self.__focusframe = None self.__deleteaction.setEnabled(False) def closeEvent(self, event): super().closeEvent(event) event.ignore() self.closeRequested.emit()
class SimpleButton(QAbstractButton): """ A simple icon button widget. """ def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.__focusframe = None def focusInEvent(self, event): # reimplemented event.accept() if self.__focusframe is None: self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) palette = self.palette() palette.setColor(QPalette.Foreground, palette.color(QPalette.Highlight)) self.__focusframe.setPalette(palette) def focusOutEvent(self, event): # reimplemented event.accept() if self.__focusframe is not None: self.__focusframe.hide() self.__focusframe.deleteLater() self.__focusframe = None def event(self, event): if event.type() == QEvent.Enter or event.type() == QEvent.Leave: self.update() return super().event(event) def sizeHint(self): # reimplemented self.ensurePolished() iconsize = self.iconSize() icon = self.icon() if not icon.isNull(): iconsize = icon.actualSize(iconsize) return iconsize def minimumSizeHint(self): # reimplemented return self.sizeHint() def paintEvent(self, event): painter = QStylePainter(self) option = QStyleOptionButton() option.initFrom(self) option.text = "" option.icon = self.icon() option.iconSize = self.iconSize() option.features = QStyleOptionButton.Flat if self.isDown(): option.state |= QStyle.State_Sunken painter.drawPrimitive(QStyle.PE_PanelButtonBevel, option) if not option.icon.isNull(): if option.state & QStyle.State_Active: mode = (QIcon.Active if option.state & QStyle.State_MouseOver else QIcon.Normal) else: mode = QIcon.Disabled if self.isChecked(): state = QIcon.On else: state = QIcon.Off option.icon.paint(painter, option.rect, Qt.AlignCenter, mode, state)