def drawPrimitive(self, element, style_option: QStyleOption, painter: QPainter, widget=None): if element == QStyle.PE_FrameGroupBox: top_margin = max( self.pixelMetric(QStyle.PM_ExclusiveIndicatorHeight), style_option.fontMetrics.height()) + 3 frame_rect = style_option.rect.adjusted(0, top_margin, -1, -1) tab_frame_color = get_tab_frame_color(style_option.palette) painter.save() painter.setRenderHint(QPainter.Antialiasing) painter.translate(0.5, 0.5) painter.setPen( merged_colors(get_outline_color(style_option.palette), tab_frame_color)) painter.setBrush( merged_colors(style_option.palette.window().color(), tab_frame_color)) painter.drawRoundedRect(frame_rect, rounded_rect_radius(), rounded_rect_radius()) painter.restore() else: QProxyStyle.drawPrimitive(self, element, style_option, painter, widget)
def drawControl(self, element, style_option: QStyleOption, painter: QPainter, widget=None): """TODO""" QProxyStyle.drawControl(self, element, style_option, painter, widget)
def drawControl(self, element, opt, painter, widget): if element == QStyle.CE_TabBarTabLabel: ic = self.pixelMetric(QStyle.PM_TabBarIconSize) r = QRect(opt.rect) w = 0 if opt.icon.isNull() else opt.rect.width() + self.pixelMetric(QStyle.PM_TabBarIconSize) r.setHeight(opt.fontMetrics.width(opt.text) + w) r.moveBottom(opt.rect.bottom()) opt.rect = r QProxyStyle.drawControl(self, element, opt, painter, widget)
def drawControl(self, element: QStyle.ControlElement, opt: 'QStyleOption', painter: QtGui.QPainter, widget: typing.Optional[QWidget] = ...) -> None: if element == QStyle.CE_TabBarTabLabel: ic = self.pixelMetric(QStyle.PM_TabBarIconSize) r = QtCore.QRect(opt.rect) w = 0 if opt.icon.isNull() else opt.rect.width( ) + self.pixelMetric(QStyle.PM_TabBarIconSize) r.setHeight(opt.fontMetrics.width(opt.text) + w) r.moveBottom(opt.rect.bottom()) opt.rect = r QProxyStyle.drawControl(self, element, opt, painter, widget)
def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None): if QStyle_PixelMetric == QStyle.PM_SmallIconSize: return 24 else: return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
def subControlRect(self, cc, opt, sc, widget): rect = QProxyStyle.subControlRect(self, cc, opt, sc, widget) if cc == QStyle.CC_Slider: if sc == QStyle.SC_SliderHandle: rect.moveLeft(rect.x() - self._width / 2) rect.setWidth(self._width) return rect
def standardIcon(self, standard_icon, style_option: QStyleOption = None, widget=None): """TODO""" return QProxyStyle.standardIcon(self, standard_icon, style_option, widget)
def styleHint(self, style_hint, style_option: QStyleOption = None, widget=None, return_data=None) -> int: """TODO""" return QProxyStyle.styleHint(self, style_hint, style_option, widget, return_data)
def polish(self, args): if isinstance(args, QLabel): pal = args.palette() args.setPalette(self.panel_palette(pal, self.light_colored(args))) elif isinstance(args, QComboBox) or \ isinstance(args, QToolButton): args.setMaximumHeight(24) elif isinstance(args, QLineEdit): args.setMaximumHeight(24) return QProxyStyle.polish(self, args)
def styleHint(self, hint, option: QStyleOption = 0, widget: QWidget = 0, returnData: QStyleHintReturn = 0) -> int: if hint in { QStyle.SH_UnderlineShortcut, QStyle.SH_DialogButtons_DefaultButton, QStyle.SH_DialogButtonBox_ButtonsHaveIcons }: return 0 return QProxyStyle.styleHint(self, hint, option, widget, returnData)
def pixelMetric(self, metric, opt=None, widget=None): if metric in (QStyle.PM_ButtonShiftVertical, QStyle.PM_ButtonShiftHorizontal, QStyle.PM_MenuBarPanelWidth, QStyle.PM_ToolBarItemMargin, QStyle.PM_ToolBarItemSpacing): return 0 elif metric in (QStyle.PM_MenuPanelWidth, QStyle.PM_MenuBarHMargin, QStyle.PM_MenuBarVMargin, QStyle.PM_ToolBarFrameWidth, QStyle.PM_SplitterWidth): return 1 return QProxyStyle.pixelMetric(self, metric, opt, widget)
def pixelMetric(self, style_pixel_metric, option=None, widget=None): """Applies custom metric to button icons. :param self: self :param style_pixel_metric: type of element :param option: metric options :param widget: widget containing element :returns: metric for element """ if style_pixel_metric == QStyle.PM_ButtonIconSize: return 200 else: return QProxyStyle.pixelMetric(self, style_pixel_metric, option, widget)
def subElementRect(self, element, option, widget): rect = QProxyStyle.subElementRect(element, option, widget) tabWidget = QTabWidget(widget) if not tabWidget: if element == QStyle.SE_TabWidgetLeftCorner: if not tabWidget.count(): rect.setHeight(tabWidget.cornerWidget(Qt.TopLeftCorner).height()) elif element == QStyle.SE_TabWidgetRightCorner: if not tabWidget.count(): rect.setHeight(tabWidget.cornerWidget(Qt.TopRightCorner).height()) if element == QStyle.SE_TabWidgetTabPane: if not tabWidget.count(): rect.setTop(tabWidget.cornerWidget(Qt.TopLeftCorner).height()) return rect
def polish(self, args): if isinstance(args, QPalette): palette = args for role, color in self._palette.items(): qcolor = QColor(color) color_group = QPalette.All if role.endswith("Disabled"): role = role.split("Disabled")[0] color_group = QPalette.Disabled elif role.endswith("Inactive"): role = role.split("Inactive")[0] qcolor.setAlpha(90) color_group = QPalette.Inactive color_role = getattr(palette, role) palette.setBrush(color_group, color_role, qcolor) elif isinstance(args, QApplication): # Set style sheet filename = os.path.join(resources.NINJA_QSS, self._qss) with open(filename + ".qss") as fileaccess: qss = fileaccess.read() args.setStyleSheet(qss) return QProxyStyle.polish(self, args)
def polish(self, args): if isinstance(args, QPalette): palette = args for role, color in self._palette.items(): qcolor = QColor(color) color_group = QPalette.All if role.endswith("Disabled"): role = role.split("Disabled")[0] color_group = QPalette.Disabled elif role.endswith("Inactive"): role = role.split("Inactive")[0] qcolor.setAlpha(90) color_group = QPalette.Inactive color_role = getattr(palette, role) palette.setBrush(color_group, color_role, qcolor) elif isinstance(args, QApplication): # Set style sheet filename = os.path.join(resources.PERCENTAGE_QSS, self._qss) with open(filename + ".qss") as fileaccess: qss = fileaccess.read() args.setStyleSheet(qss) return QProxyStyle.polish(self, args)
def pixelMetric(self, metric, option, widget): if metric == QProxyStyle.PM_TextCursorWidth: if self.cursor_width > 0: return self.cursor_width return QProxyStyle.pixelMetric(self, metric, option, widget)
def pixelMetric(self, qt_pixel_metric_style, option=None, widget=None): if qt_pixel_metric_style == QStyle.PM_ToolBarIconSize: return 40 else: return QProxyStyle.pixelMetric(self, qt_pixel_metric_style, option, widget)
def drawControl(self, element, opt, painter, widget): """elif element == QStyle.CE_PushButtonBevel: # States is_down = (opt.state & STATE_SUNKEN) | (opt.state & STATE_ON) hovered = opt.state & STATE_ENABLED and opt.state & STATE_MOUSEOVER has_focus = opt.state & STATE_HASFOCUS rect = opt.rect btn_color = opt.palette.button().color() painter.setPen(btn_color) painter.setRenderHint(QPainter.Antialiasing) path = QPainterPath() path.addRoundedRect(QRectF(rect), 3, 3) if is_down: painter.setBrush(btn_color.darker(115)) elif has_focus: painter.setBrush(btn_color.lighter(130)) elif hovered: grad = QLinearGradient(rect.topLeft(), rect.bottomLeft()) grad.setColorAt(0.6, btn_color) grad.setColorAt(1, btn_color.lighter(120)) painter.setBrush(grad) else: painter.setBrush(btn_color) painter.drawPath(path)""" if element == QStyle.CE_ComboBoxLabel: cb = opt painter.save() edit_rect = self.subControlRect(QStyle.CC_ComboBox, cb, QStyle.SC_ComboBoxEditField, widget) # Draw icon if not cb.currentIcon.isNull(): if cb.state & STATE_ENABLED: mode = QIcon.Normal else: mode = QIcon.Disabled pixmap = cb.currentIcon.pixmap(cb.iconSize, mode) icon_rect = QRect(cb.rect) icon_rect.setWidth(cb.iconSize.width() + 4) # icon_rect = self.alignedRect(opt.direction, # Qt.AlignLeft | Qt.AlignVCenter, # icon_rect.size(), edit_rect) self.drawItemPixmap(painter, icon_rect, Qt.AlignCenter, pixmap) # Space between text if cb.direction == Qt.RightToLeft: edit_rect.translate(-4, -cb.iconSize.width(), 0) else: edit_rect.translate(cb.iconSize.width() + 4, 0) edit_rect.adjusted(0, 0, -13, 0) # Draw text elide_width = edit_rect.width() - opt.fontMetrics.width('**') text = opt.fontMetrics.elidedText(cb.currentText, Qt.ElideRight, elide_width) # FIXME: states painter.setPen(_COLORS['ComboBoxTextColor']) painter.drawText(edit_rect.adjusted(1, 0, -1, 0), Qt.AlignLeft | Qt.AlignVCenter, text) painter.restore() # TODO: tab with flat style # elif element == QStyle.CE_TabBarTabShape: # pass elif element == QStyle.CE_ToolBar: rect = opt.rect # print(widget.property("border"), widget) painter.fillRect(rect, _COLORS['ToolButtonColor']) if widget.property("border"): # painter.setPen(opt.palette.light().color().lighter(150)) painter.setPen(_COLORS["Border"]) painter.drawLine(rect.topLeft(), rect.topRight()) # painter.setPen(_COLORS['Border']) # painter.drawLine(opt.rect.topRight(), opt.rect.bottomRight()) elif element == QStyle.CE_MenuItem: painter.save() enabled = opt.state & STATE_ENABLED item = opt item.rect = opt.rect pal = opt.palette if enabled: color = _COLORS['MenuItemEnabled'] else: color = _COLORS['MenuItemDisabled'] item.palette = pal pal.setBrush(QPalette.Text, color) QProxyStyle.drawControl(self, element, opt, painter, widget) painter.restore() elif element == QStyle.CE_MenuBarEmptyArea: painter.fillRect(opt.rect, _COLORS['MenuBar']) # Draw border painter.save() # FIXME: color from theme painter.setPen(_COLORS['MenuBarBorderColor']) painter.drawLine(opt.rect.bottomLeft() + QPointF(.5, .5), opt.rect.bottomRight() + QPointF(.5, .5)) painter.restore() # elif element == QStyle.CE_PushButtonBevel: # painter.setPen(Qt.red) # painter.fillRect(opt.rect, QColor("red")) elif element == QStyle.CE_MenuBarItem: painter.save() act = opt.state & (STATE_SUNKEN | QStyle.State_Selected) dis = not (opt.state & STATE_ENABLED) painter.fillRect(opt.rect, _COLORS['MenuBar']) pal = opt.palette item = opt item.rect = opt.rect if dis: color = _COLORS['MenuBarItemDisabled'] else: color = _COLORS['MenuBarItemEnabled'] pal.setBrush(QPalette.ButtonText, color) item.palette = pal QCommonStyle.drawControl(self, element, item, painter, widget) if act: pal = opt.palette color = _COLORS['MenuBarHover'] painter.fillRect(opt.rect, color) align = (Qt.AlignCenter | Qt.TextShowMnemonic | Qt.TextDontClip | Qt.TextSingleLine) if not self.styleHint(QStyle.SH_UnderlineShortcut, opt, widget): align |= Qt.TextHideMnemonic # FIXME: if dis: co = _COLORS['IconDisabledColor'] else: co = _COLORS['MenuBarTextHover'] painter.setPen(Qt.NoPen) pal.setBrush(QPalette.Text, co) self.drawItemText(painter, item.rect, align, pal, not dis, opt.text, QPalette.Text) painter.restore() else: QProxyStyle.drawControl(self, element, opt, painter, widget)
def __init__(self, width, parent=None): QProxyStyle.__init__(self, parent) self._width = width
def drawPrimitive(self, element, opt, painter, widget): if not self.__panel_widget(widget): return QProxyStyle.drawPrimitive(self, element, opt, painter, widget) if element == QStyle.PE_PanelButtonTool: flat = True pressed = (opt.state & STATE_SUNKEN or opt.state & STATE_ON) hovered = opt.state & STATE_ENABLED and opt.state & STATE_MOUSEOVER button_color = _COLORS['ToolButtonColor'] if not flat and widget.property("gradient"): button_color = QLinearGradient(opt.rect.topRight(), opt.rect.bottomRight()) button_color.setColorAt(0, QColor("#454545")) button_color.setColorAt(1, QColor("#191919")) if pressed: button_color = _COLORS['ToolButtonSelected'] elif hovered: if not flat and widget.property("gradient"): button_color.setColorAt(0, QColor("#555")) button_color.setColorAt(1, QColor("#191919")) else: button_color = _COLORS['ToolButtonHover'] if widget.property("border_bottom"): painter.setPen(_COLORS['Border']) painter.drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight()) painter.fillRect(opt.rect.adjusted(2, 2, -2, -2), button_color) # elif not opt.state & STATE_ENABLED: # color = _PALETTE['ButtonDisabled'] # painter.fillRect(opt.rect, color) # TODO: keyboard focus change state # elif element == QStyle.PE_PanelButtonCommand: # Draw a flat push button # is_down = opt.state & STATE_SUNKEN or opt.state & STATE_ON # is_enabled = opt.state & STATE_ENABLED # is_hover = is_enabled and opt.state & STATE_MOUSEOVER # FIXME: has_focus state # FIXME: from theme # color = QColor("#444a58") # if is_down: # color = color.darker(130) # elif is_hover: # color = color.lighter(110) # painter.fillRect(opt.rect, color) elif element == QStyle.PE_PanelLineEdit: painter.save() # Fill background rect = opt.rect enabled = False if opt.state & STATE_ENABLED: enabled = True if not enabled: painter.setOpacity(0.55) painter.fillRect(rect, _COLORS['LineEditBackground']) has_focus = False if opt.state & QStyle.State_HasFocus: has_focus = True if enabled and (has_focus or opt.state & STATE_MOUSEOVER): # FIXME: color from theme # hover = QColor("#6a6ea9") # if has_focus: # alpha = 200 # else: # alpha = 55 # hover.setAlpha(alpha) # painter.setPen(QPen(hover, 2, Qt.SolidLine, # Qt.SquareCap, Qt.RoundJoin)) # painter.drawRect(rect.adjusted(0, 0, 0, 0)) pass painter.restore() elif element == QStyle.PE_IndicatorToolBarSeparator: rect = opt.rect painter.setPen(_COLORS['SeparatorColor']) border_rect = QRectF(rect).adjusted(0.5, 0.5, -0.5, -0.5) if opt.state & QStyle.State_Horizontal: border_rect.setWidth(1) painter.drawLine(border_rect.topRight() + QPointF(0, 3), border_rect.bottomRight() - QPointF(0, 3)) else: border_rect.setHeight(1) painter.drawLine(border_rect.topLeft() + QPointF(3, 0), border_rect.topRight() - QPointF(3, 0)) elif element == QStyle.PE_IndicatorToolBarHandle: # FIXME: draw a fancy handler QProxyStyle.drawPrimitive(self, element, opt, painter, widget) else: QProxyStyle.drawPrimitive(self, element, opt, painter, widget)
def drawComplexControl(self, element, opt, painter, widget): if not self.__panel_widget(widget): QProxyStyle.drawComplexControl(self, element, opt, painter, widget) return if element == QStyle.CC_ComboBox: empty = False if not opt.currentText and opt.currentIcon.isNull(): empty = True tool_btn = opt if empty: tool_btn.state &= ~(STATE_ENABLED | STATE_SUNKEN) self.drawPrimitive(QStyle.PE_PanelButtonTool, tool_btn, painter, widget) # Draw border if widget.property("border"): # painter.setPen(opt.palette.light().color().lighter(150)) painter.setPen(_COLORS["MenuBarBorderColor"]) painter.drawLine(opt.rect.topRight() + QPoint(0, 6), opt.rect.bottomRight() - QPoint(0, 6)) if widget.property("border_bottom"): painter.setPen(_COLORS['Border']) painter.drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight()) arrow_rect = QRect((opt.rect.left() + opt.rect.right()) / 2 + 6, opt.rect.center().y(), 9, 9) arrow_rect.moveRight(opt.rect.width() - 10) # FIXME: arrow_opt = QStyleOptionComboBox() arrow_opt.state = opt.state arrow_opt.rect = arrow_rect if empty: arrow_opt.state &= ~(STATE_ENABLED | STATE_SUNKEN) if self.styleHint(QStyle.SH_ComboBox_Popup, opt, widget): arrow_opt.rect.translate(0, -3) arrow_opt.palette.setColor(QPalette.ButtonText, _COLORS['IconBaseColor']) QCommonStyle.drawPrimitive(self, QStyle.PE_IndicatorArrowDown, arrow_opt, painter, widget) # elif element == QStyle.CC_ScrollBar: # pass elif element == QStyle.CC_ToolButton: # reverse = opt.direction == Qt.RightToLeft button = self.subControlRect(element, opt, QStyle.SC_ToolButton, widget) flags = opt.state if flags & QStyle.State_AutoRaise: if not flags & STATE_MOUSEOVER: flags &= ~QStyle.State_Raised tool = opt tool.palette = self.panel_palette(opt.palette) if opt.subControls & QStyle.SC_ToolButton: tool.rect = button tool.state = flags self.drawPrimitive(QStyle.PE_PanelButtonTool, tool, painter, widget) # Draw border if widget.property("border"): # painter.setPen(_COLORS["MenuBarBorderColor"]) # painter.drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight()) pass label = opt label.palette.setColor(QPalette.ButtonText, label.palette.buttonText().color()) fw = self.pixelMetric(QStyle.PM_DefaultFrameWidth, opt, widget) label.rect = opt.rect.adjusted(fw, fw, -fw, -fw) self.drawControl(QStyle.CE_ToolButtonLabel, label, painter, widget) else: QProxyStyle.drawComplexControl(self, element, opt, painter, widget)
def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None): if QStyle_PixelMetric == QStyle.PM_ToolBarIconSize: return 40 else: return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
def __init__(self, hilite_color): QProxyStyle.__init__(self, QStyleFactory.create("fusion")) self.hilite_color = hilite_color
def subElementRect(self, sub_element, style_option: QStyleOption, widget: QWidget): """TODO""" return QProxyStyle.subElementRect(self, sub_element, style_option, widget)
def __init__(self, theme): QProxyStyle.__init__(self) self.setBaseStyle(QStyleFactory.create("fusion")) self._palette = theme["palette"] self._qss = theme["stylesheet"]
def __init__(self, key): QProxyStyle.__init__(key)
def __init__(self): QProxyStyle.__init__(self)
self.widget = widget def clickHandler(self): self.hide() self.widget.show() def setCSS(self): self.setStyleSheet(""" .QPushButton{ background: rgb(66, 184, 221); /* #4CAF50; Green */ color: white; padding: 20px 40px; font-size: 24px; max-width: 100px; max-height: 30px; border-radius: 4px; } .QPushButton:pressed { border: 4px solid black; background: grey } """) if __name__ == '__main__': app = QApplication(sys.argv) myStyle = QProxyStyle('Fusion') app.setStyle(myStyle) ex = wifiUI() sys.exit(app.exec_())
def styleHint(self, hint, option=None, widget=None, returnData=None): if isinstance(widget, InstantTooltipButton ) and hint == QStyle.SH_ToolTip_WakeUpDelay: return 0 return QProxyStyle.styleHint(self, hint, option, widget, returnData)
def pixelMetric(self, metric, style_option: QStyleOption = None, widget=None) -> int: """TODO""" return QProxyStyle.pixelMetric(self, metric, style_option, widget)
def sizeFromContents(self, contents_type, style_option: QStyleOption, size: QSize, widget: QWidget) -> QSize: """TODO""" return QProxyStyle.sizeFromContents(self, contents_type, style_option, size, widget)
def styleHint(self, hint, option=None, widget=None, returnData=None): if hint == QProxyStyle.SH_TextControl_FocusIndicatorTextCharFormat: return False return QProxyStyle.styleHint(self, hint, option, widget, returnData)
if param_type is None or param_count == current_count: param = param.split() param_type = param[0] param_count = int(param[1]) current_count = 0 continue elif param_type == "change": param = param.split('|') columns = [col.split('=') for col in param[2].split(',')] columns = {col[0]: col[1] for col in columns} change_columns(db, param[1], param[0], **columns) elif param_type == "sql": sql(param) db.commit() current_count += 1 db.close() if __name__ == "__main__" and (not dir_exists("save") or len(sys_args) > 1): app = QApplication(sys_args) app.setStyle(QProxyStyle('Fusion')) args = None if not dir_exists("save"): make_dir("save") args = "4.0" if len(sys_args) > 1: args = sys_args[2] form = Updater(args) app.exec_()