class ArtellaImage(QtWidgets.QLabel, object): def __init__(self, parent=None): super(ArtellaImage, self).__init__(parent) self._default_pixmap = resource.pixmap('artella') self._pixmap = self._default_pixmap self._artella_size = 0 self.set_artella_size(theme.theme().default_size) def set_artella_size(self, value): self._artella_size = value self._set_artella_size() def _set_artella_size(self): self.setFixedSize(QtCore.QSize(self._artella_size, self._artella_size)) self._set_artella_image() def _set_artella_image(self): self.setPixmap(self._pixmap.scaledToWidth(self.height(), QtCore.Qt.SmoothTransformation)) def set_artella_image(self, value): """ Set avatar image. :param value: QPixmap or None. :return: None """ if value is None: self._pixmap = self._default_pixmap elif isinstance(value, QtGui.QPixmap): self._pixmap = value else: raise TypeError("Input argument 'value' should be QPixmap or None, but get {}".format(type(value))) self._set_artella_image() def get_artella_image(self): return self._pixmap def get_artella_size(self): return self._artella_size artella_image = QtCore.Property(QtGui.QPixmap, get_artella_image, set_artella_image) artella_size = QtCore.Property(int, get_artella_size, set_artella_size) @classmethod def tiny(cls, image=None): inst = cls() inst.set_artella_size(theme.theme().tiny) inst.set_artella_image(image) return inst @classmethod def small(cls, image=None): inst = cls() inst.set_artella_size(theme.theme().small) inst.set_artella_image(image) return inst @classmethod def medium(cls, image=None): inst = cls() inst.set_artella_size(theme.theme().medium) inst.set_artella_image(image) return inst @classmethod def large(cls, image=None): inst = cls() inst.set_artella_size(theme.theme().large) inst.set_artella_image(image) return inst @classmethod def huge(cls, image=None): inst = cls() inst.set_artella_size(theme.theme().huge) inst.set_artella_image(image) return inst
class ArtellaLabel(QtWidgets.QLabel, object): def __init__(self, text='', parent=None): super(ArtellaLabel, self).__init__(text, parent) self._actual_text = text self._artella_type = '' self._artella_underline = False self._artella_mark = False self._artella_delete = False self._artella_strong = False self._artella_code = False self._artella_level = 0 self._elide_mode = QtCore.Qt.ElideNone self.setProperty('artella_text', text) self.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction | QtCore.Qt.LinksAccessibleByMouse) self.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum) def get_artella_level(self): return self._artella_level def set_artella_level(self, value): self._artella_level = value self.style().polish(self) def set_artella_underline(self, value): self._artella_underline = value self.style().polish(self) def get_artella_underline(self): return self._artella_underline def set_artella_delete(self, value): self._artella_delete = value self.style().polish(self) def get_artella_delete(self): return self._artella_delete def set_artella_strong(self, value): self._artella_strong = value self.style().polish(self) def get_artella_strong(self): return self._artella_strong def set_artella_mark(self, value): self._artella_mark = value self.style().polish(self) def get_artella_mark(self): return self._artella_mark def set_artella_code(self, value): self._artella_code = value self.style().polish(self) def get_artella_code(self): return self._artella_code def get_elide_mode(self): return self._elide_mode def set_elide_mode(self, value): self._elide_mode = value self._update_elided_text() def get_artella_type(self): return self._artella_type def set_artella_type(self, value): self._artella_type = value self.style().polish(self) artella_level = QtCore.Property(int, get_artella_level, set_artella_level) artella_type = QtCore.Property(str, get_artella_type, set_artella_type) artella_underline = QtCore.Property(bool, get_artella_underline, set_artella_underline) artella_delete = QtCore.Property(bool, get_artella_delete, set_artella_delete) artella_strong = QtCore.Property(bool, get_artella_strong, set_artella_strong) artella_mark = QtCore.Property(bool, get_artella_mark, set_artella_mark) artella_code = QtCore.Property(bool, get_artella_code, set_artella_code) artella_elide_mod = QtCore.Property(QtCore.Qt.TextElideMode, get_artella_code, set_artella_code) def minimumSizeHint(self): return QtCore.QSize(1, self.fontMetrics().height()) def text(self): return self._actual_text def setText(self, text): self._actual_text = text self._update_elided_text() self.setToolTip(text) def _update_elided_text(self): _font_metrics = self.fontMetrics() _elided_text = _font_metrics.elidedText(self._actual_text, self._elide_mode, self.width() - 2 * 2) super(ArtellaLabel, self).setText(_elided_text) def resizeEvent(self, event): self._update_elided_text() def h1(self): self.set_artella_level(ArtellaLabelTypes.H1) return self def h2(self): self.set_artella_level(ArtellaLabelTypes.H2) return self def h3(self): self.set_artella_level(ArtellaLabelTypes.H3) return self def h4(self): self.set_artella_level(ArtellaLabelTypes.H4) return self def secondary(self): self.set_artella_type(ArtellaLabelTypes.SECONDARY) return self def warning(self): self.set_artella_type(ArtellaLabelTypes.WARNING) return self def danger(self): self.set_artella_type(ArtellaLabel.DangerType) return self def strong(self): """Set QLabel with strong style.""" self.set_artella_strong(True) return self def mark(self): self.set_artella_mark(True) return self def code(self): self.set_artella_code(True) return self def delete(self): self.set_artella_delete(True) return self def underline(self): self.set_artella_underline(True) return self
class ArtellaToolButton(QtWidgets.QToolButton): def __init__(self, parent=None): super(ArtellaToolButton, self).__init__(parent=parent) self._artella_image = None self._artella_size = theme.theme().default_size self.setAutoExclusive(False) self.setAutoRaise(True) self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) self._polish_icon() self.toggled.connect(self._polish_icon) def enterEvent(self, event): if self._artella_image: self.setIcon( resource.icon(self._artella_image, color=theme.theme().main_color)) return super(ArtellaToolButton, self).enterEvent(event) def leaveEvent(self, event): self._polish_icon() return super(ArtellaToolButton, self).leaveEvent(event) def get_artella_size(self): return self._artella_size def set_artella_size(self, value): self._artella_size = value self.style().polish(self) if self.toolButtonStyle() == QtCore.Qt.ToolButtonIconOnly: self.setFixedSize( QtCore.QSize(self._artella_size, self._artella_size)) def get_artella_image(self): return self._artella_image def set_artella_image(self, path): self._artella_image = path self._polish_icon() artella_size = QtCore.Property(int, get_artella_size, set_artella_size) def huge(self): self.set_artella_size(theme.theme().huge) return self def large(self): self.set_artella_size(theme.theme().large) return self def medium(self): self.set_artella_size(theme.theme().medium) return self def small(self): self.set_artella_size(theme.theme().small) return self def tiny(self): self.set_artella_size(theme.theme().tiny) return self def image(self, path): self.set_artella_image(path) return self def icon_only(self): self.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly) self.setFixedSize( QtCore.QSize(self._artella_size, self._artella_size)) return self def text_only(self): self.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly) return self def text_beside_icon(self): self.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon) return self def text_under_icon(self): self.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon) return self def _polish_icon(self, *args, **kwargs): if self._artella_image: if self.isCheckable() and self.isChecked(): self.setIcon( resource.icon(self._artella_image, color=theme.theme().main_color)) else: self.setIcon(resource.icon(self._artella_image))