def __init__(self, parent, message, severity, actions=None): QFrame.__init__(self, parent) self.ui = Ui_NotificationFrame() self.ui.setupUi(self) self.ui.closeButton.setIcon(QApplication.style().standardIcon(QStyle.SP_DialogCloseButton)) if severity == self.INFO: bgcolor = "#4398c8" fgcolor = self.palette().base().color().name() icon = self._standardIconAsPixmap(QStyle.SP_MessageBoxInformation) elif severity == self.WARNING: bgcolor = "#d0b05f" fgcolor = self.palette().text().color().name() icon = self._standardIconAsPixmap(QStyle.SP_MessageBoxWarning) elif severity == self.ERROR: bgcolor = "#cda8a8" fgcolor = self.palette().text().color().name() icon = self._standardIconAsPixmap(QStyle.SP_MessageBoxCritical) self.ui.iconLabel.setPixmap(icon) self.ui.messageLabel.setText(message) self._setColor(bgcolor, fgcolor) if actions: for action in reversed(actions): action.triggered.connect(self.deleteLater) b = QToolButton() b.setDefaultAction(action) self.layout().insertWidget(2, b)
class NotificationFrame(QFrame): INFO, WARNING, ERROR = range(3) ICONSIZE = 24 def __init__(self, parent, message, severity, actions=None): QFrame.__init__(self, parent) self.ui = Ui_NotificationFrame() self.ui.setupUi(self) self.ui.closeButton.setIcon(QApplication.style().standardIcon(QStyle.SP_DialogCloseButton)) if severity == self.INFO: bgcolor = "#4398c8" fgcolor = self.palette().base().color().name() icon = self._standardIconAsPixmap(QStyle.SP_MessageBoxInformation) elif severity == self.WARNING: bgcolor = "#d0b05f" fgcolor = self.palette().text().color().name() icon = self._standardIconAsPixmap(QStyle.SP_MessageBoxWarning) elif severity == self.ERROR: bgcolor = "#cda8a8" fgcolor = self.palette().text().color().name() icon = self._standardIconAsPixmap(QStyle.SP_MessageBoxCritical) self.ui.iconLabel.setPixmap(icon) self.ui.messageLabel.setText(message) self._setColor(bgcolor, fgcolor) if actions: for action in reversed(actions): action.triggered.connect(self.deleteLater) b = QToolButton() b.setDefaultAction(action) self.layout().insertWidget(2, b) def _standardIconAsPixmap(self, icon): return QApplication.style().standardIcon(icon).pixmap(self.ICONSIZE, self.ICONSIZE) def _setColor(self, bgcolor, fgcolor): stylesheet = \ """ #NotificationFrame { border-radius: 5px; padding: 2px; background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 %s, stop: 0.1 %s, stop: 1 %s); border: 1px solid %s; } #messageLabel { color: %s; }""" lighter = lambda c: QColor(c).lighter(110).name() darker = lambda c: QColor(c).darker(110).name() self.setStyleSheet(stylesheet % (lighter(bgcolor), bgcolor, darker(bgcolor), "black", fgcolor))