def paintEvent(self, event): """Override QProgressBar's paintEvent.""" if self.text() != self._default_label.text(): self._default_label.setText(self.text()) if self.isTextVisible() != self._default_label.isVisible(): self._default_label.setVisible(self.isTextVisible()) percent = utils.get_percent(self.value(), self.minimum(), self.maximum()) total_width = self.get_dayu_width() pen_width = int(3 * total_width / 50.0) radius = total_width - pen_width - 1 painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) # draw background circle pen_background = QPen() pen_background.setWidth(pen_width) pen_background.setColor(QColor(dayu_theme.background_selected_color)) pen_background.setCapStyle(Qt.RoundCap) painter.setPen(pen_background) painter.drawArc(pen_width / 2.0 + 1, pen_width / 2.0 + 1, radius, radius, self._start_angle, -self._max_delta_angle) # draw foreground circle pen_foreground = QPen() pen_foreground.setWidth(pen_width) pen_foreground.setColor(QColor(self._color)) pen_foreground.setCapStyle(Qt.RoundCap) painter.setPen(pen_foreground) painter.drawArc(pen_width / 2.0 + 1, pen_width / 2.0 + 1, radius, radius, self._start_angle, -percent * 0.01 * self._max_delta_angle) painter.end()
def generate_color(primary_color, index): """ Reference to ant-design color system algorithm. :param primary_color: base color. #RRGGBB :param index: color step. 1-10 from light to dark :return: result color """ # 这里生成颜色的算法,来自 Ant Design, 只做了语言的转换,和颜色的类型的转换,没对算法做任何修改 # https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less # https://zhuanlan.zhihu.com/p/32422584 hue_step = 2 saturation_step = 16 saturation_step2 = 5 brightness_step1 = 5 brightness_step2 = 15 light_color_count = 5 dark_color_count = 4 def _get_hue(color, i, is_light): h_comp = color.hue() if 60 <= h_comp <= 240: hue = h_comp - hue_step * i if is_light else h_comp + hue_step * i else: hue = h_comp + hue_step * i if is_light else h_comp - hue_step * i if hue < 0: hue += 359 elif hue >= 359: hue -= 359 return hue / 359.0 def _get_saturation(color, i, is_light): s_comp = color.saturationF() * 100 if is_light: saturation = s_comp - saturation_step * i elif i == dark_color_count: saturation = s_comp + saturation_step else: saturation = s_comp + saturation_step2 * i saturation = min(100.0, saturation) if is_light and i == light_color_count and saturation > 10: saturation = 10 saturation = max(6.0, saturation) return round(saturation * 10) / 1000.0 def _get_value(color, i, is_light): v_comp = color.valueF() if is_light: return min((v_comp * 100 + brightness_step1 * i) / 100, 1.0) return max((v_comp * 100 - brightness_step2 * i) / 100, 0.0) light = index <= 6 hsv_color = QColor(primary_color) if isinstance(primary_color, basestring) else primary_color index = light_color_count + 1 - index if light else index - light_color_count - 1 return QColor.fromHsvF( _get_hue(hsv_color, index, light), _get_saturation(hsv_color, index, light), _get_value(hsv_color, index, light) ).name()
def fade_color(color, alpha): """ Fade color with given alpha. eg. fade_color('#ff0000', '10%) => 'rgba(255, 0, 0, 10%)' :param color: string, hex digit format '#RRGGBB' :param alpha: string, percent 'number%' :return: qss/css color format rgba(r, g, b, a) """ q_color = QColor(color) return 'rgba({}, {}, {}, {})'.format(q_color.red(), q_color.green(), q_color.blue(), alpha)
def compile_color(color1, color2): """ There is some bias when calculate with float. Set margin of error to 0.01 """ delta = 0.01 q_color_1 = QColor(color1) q_color_2 = QColor(color2) return (abs(q_color_1.redF() - q_color_2.redF()) < delta) and \ (abs(q_color_1.greenF() - q_color_2.greenF()) < delta) and \ (abs(q_color_1.blueF() - q_color_2.blueF()) < delta)
def paint(self, painter, option, index): painter.save() icon_color = dayu_theme.icon_color if option.state & QStyle.State_MouseOver: painter.fillRect(option.rect, QColor(dayu_theme.primary_5)) icon_color = '#fff' if option.state & QStyle.State_Selected: painter.fillRect(option.rect, QColor(dayu_theme.primary_6)) icon_color = '#fff' painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(Qt.white)) pix = MPixmap('down_fill.svg', icon_color) h = option.rect.height() pix = pix.scaledToWidth(h * 0.5, Qt.SmoothTransformation) painter.drawPixmap(option.rect.x() + option.rect.width() - h, option.rect.y() + h / 4, pix) painter.restore() super(MOptionDelegate, self).paint(painter, option, index)
def draw_empty_content(view, text=None, pix_map=None): from dayu_widgets import dayu_theme pix_map = pix_map or MPixmap('empty.svg') text = text or view.tr('No Data') painter = QPainter(view) font_metrics = painter.fontMetrics() painter.setPen(QPen(QColor(dayu_theme.secondary_text_color))) content_height = pix_map.height() + font_metrics.height() padding = 10 proper_min_size = min(view.height() - padding * 2, view.width() - padding * 2, content_height) if proper_min_size < content_height: pix_map = pix_map.scaledToHeight(proper_min_size - font_metrics.height(), Qt.SmoothTransformation) content_height = proper_min_size painter.drawText(view.width() / 2 - font_metrics.width(text) / 2, view.height() / 2 + content_height / 2 - font_metrics.height() / 2, text) painter.drawPixmap(view.width() / 2 - pix_map.width() / 2, view.height() / 2 - content_height / 2, pix_map) painter.end()