def draw_snake_spinner(painter, rect, angle, light, dark): painter.setRenderHint(QPainter.Antialiasing) if rect.width() > rect.height(): delta = (rect.width() - rect.height()) // 2 rect = rect.adjusted(delta, 0, -delta, 0) elif rect.height() > rect.width(): delta = (rect.height() - rect.width()) // 2 rect = rect.adjusted(0, delta, 0, -delta) disc_width = max(4, rect.width() // 10) drawing_rect = QRect(rect.x() + disc_width, rect.y() + disc_width, rect.width() - 2 * disc_width, rect.height() - 2 *disc_width) try: angle_for_width = math.degrees(math.atan2(1.3 * disc_width, drawing_rect.width())) except ZeroDivisionError: angle_for_width = 5 gradient = QConicalGradient(drawing_rect.center(), angle - angle_for_width) gradient.setColorAt(1, light) gradient.setColorAt(0, dark) painter.setPen(QPen(light, disc_width)) painter.drawArc(drawing_rect, 0, 360 * 16) pen = QPen(QBrush(gradient), disc_width) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) painter.drawArc(drawing_rect, angle * 16, (360 - 2 * angle_for_width) * 16)
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) hovering = index.data(HOVER_ROLE) is True painter.save() rect = option.rect is_current = index.data(Qt.FontRole) is not None if not hovering and is_current: qpp = QPainterPath() qpp.addRoundedRect(QRectF(rect), 6, 6) painter.fillPath(qpp, self.current_background) icon_rect = QRect(rect.left() + self.MARGIN, rect.top() + self.MARGIN, ICON_SIZE, ICON_SIZE) left = icon_rect.right() + 2 * self.MARGIN text_rect = QRect(left, icon_rect.top(), rect.width() - left + rect.left(), icon_rect.height()) mark = index.data(MARK_ROLE) if hovering or mark: text_rect.adjust(0, 0, -text_rect.height(), 0) text = index.data(DISPLAY_ROLE) or '' font = index.data(Qt.FontRole) if font: painter.setFont(font) text_flags = Qt.AlignVCenter | Qt.AlignLeft | Qt.TextSingleLine text = elided_text(text, font, text_rect.width(), 'right') if option.state & QStyle.State_Selected: painter.setPen(QPen(self.highlighted_text)) painter.drawText(text_rect, text_flags, text) if mark: hrect = QRect(text_rect.right(), text_rect.top(), text_rect.height(), text_rect.height()) painter.fillRect(hrect, QColor('#ffffaa')) painter.drawText(hrect, Qt.AlignCenter, mark) elif hovering: hrect = QRect(text_rect.right(), text_rect.top(), text_rect.height(), text_rect.height()) close_hover = index.data(CLOSE_HOVER_ROLE) is True if close_hover: pen = painter.pen() pen.setColor(QColor('red')) painter.setPen(pen) painter.drawText(hrect, Qt.AlignCenter, '✖ ') if index.data(LOADING_ROLE): if not self.errored_out: angle = index.data(ANGLE_ROLE) try: draw_snake_spinner(painter, icon_rect, angle, self.light, self.dark) except Exception: import traceback traceback.print_exc() self.errored_out = True else: icurl = index.data(URL_ROLE) if icurl == WELCOME_URL: icon = welcome_icon() elif icurl == DOWNLOADS_URL: icon = downloads_icon() else: icon = index.data(DECORATION_ROLE) icon.paint(painter, icon_rect) painter.restore()
def __call__(self, painter, rect, color_theme, title_block, subtitle_block, footer_block): painter.fillRect(rect, self.color1) r = QRect(0, int(title_block.position.y), rect.width(), title_block.height + subtitle_block.height + subtitle_block.line_spacing // 2 + title_block.leading) painter.save() p = QPainterPath() p.addRoundedRect(QRectF(r), 10, 10 * r.width()/r.height(), Qt.RelativeSize) painter.setClipPath(p) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(r, self.color2) painter.restore() r = QRect(0, 0, int(title_block.position.x), rect.height()) painter.fillRect(r, self.color2) return self.ccolor2, self.ccolor2, self.ccolor1
def paintEvent(self, event): ''' @param event QPaintEvent ''' p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) size = 16 pixmapSize = round(size * self.data().animationPixmap.devicePixelRatioF()) # Center the pixmap in rect r = QRect(self.rect()) r.setX((r.width() - size) / 2) r.setY((r.height() - size) / 2) r.setWidth(size) r.setHeight(size) if self._animationRunning: p.drawPixmap( r, self.data().animationPixmap, QRect(self._currentFrame * pixmapSize, 0, pixmapSize, pixmapSize)) elif self._audioIconDisplayed and not self._tab.isPinned(): self._audioIconRect = QRect(r) p.drawPixmap( r, self._tab.isMuted() and self.data().audioMutedPixmap or self.data().audioPlayingPixmap) elif not self._sitePixmap.isNull(): p.drawPixmap(r, self._sitePixmap) elif self._tab and self._tab.isPinned(): p.drawPixmap(r, IconProvider.emptyWebIcon().pixmap(size)) # Draw audio icon on top of site icon for pinned tabs if not self._animationRunning and self._audioIconDisplayed and self._tab.isPinned( ): s = size - 4 r0 = QRect(self.width() - 4, 0, s, s) self._audioIconRect = r0 c = self.palette().color(QPalette.Window) c.setAlpha(180) p.setPen(c) p.setBrush(c) p.drawEllipse(r) p.drawPixmap( r, self._tab.isMuted() and self.data().audioMutedPixmap or self.data().audioPlayingPixmap) # Draw background activity indicator if self._tab and self._tab.isPinned() and self._tab.webView( ).backgroundActivity(): s = 5 # Background r1 = QRect(self.width() - s - 2, self.height() - s - 2, s + 2, s + 2) c1 = self.palette().color(QPalette.Window) c1.setAlpha(180) p.setPen(Qt.transparent) p.setBrush(c1) p.drawEllipse(r1) # Forground r2 = QRect(self.width() - s - 1, self.height() - s - 1, s, s) c2 = self.palette().color(QPalette.Text) p.setPen(Qt.transparent) p.setBrush(c2) p.drawEllipse(r2)
def drawControl(self, element, option, painter, widget): ''' @param: element ControlElement @param: option QStyleOption @param: painter QPainter @param: widget QWidget ''' v_opt = option if element != self.CE_TabBarTab or not isinstance( v_opt, QStyleOptionTab): QProxyStyle.drawControl(element, option, painter, widget) return rect = v_opt.rect selected = v_opt.state & self.State_Selected vertical_tabs = v_opt.shape == QTabBar.RoundedWest text = v_opt.text if selected: # background painter.save() grad = QLinearGradient(rect.topLeft(), rect.topRight()) grad.setColorAt(0, QColor(255, 255, 255, 140)) grad.setColorAt(0, QColor(255, 255, 255, 210)) painter.fillRect(rect.adjusted(0, 0, 0, -1), grad) painter.restore() # shadows painter.setPen(QColor(0, 0, 0, 110)) painter.drawLine(rect.topLeft() + QPoint(1, -1), rect.topRight() - QPoint(0, 1)) painter.drawLine(rect.bottomLeft(), rect.bottomRight()) painter.setPen(QColor(0, 0, 0, 40)) painter.drawLine(rect.topLeft(), rect.bottomLeft()) # highlights painter.setPen(QColor(255, 255, 255, 50)) painter.drawLine(rect.topLeft() + QPoint(0, -2), rect.topRight() - QPoint(0, 2)) painter.drawLine(rect.bottomLeft() + QPoint(0, 1), rect.bottomRight() + QPoint(0, 1)) painter.setPen(QColor(255, 255, 255, 40)) painter.drawLine(rect.topLeft() + QPoint(0, 0), rect.topRight()) painter.drawLine(rect.topRight() + QPoint(0, 1), rect.bottomRight() - QPoint(0, 1)) painter.drawLine(rect.bottomLeft() + QPoint(0, -1), rect.bottomRight() - QPoint(0, 1)) m = QTransform() if vertical_tabs: m = QTransform.fromTranslate(rect.left(), rect.bottom()) m.rotate(-90) else: m = QTransform.fromTranslate(rect.left(), rect.top()) draw_rect = QRect(QPoint(0, 0), m.mapRect(rect).size()) painter.save() painter.setTransform(m) icon_rect = QRect(QPoint(8, 0), v_opt.iconSize) text_rect = QRect(icon_rect.topRight() + QPoint(4, 0), draw_rect.size()) text_rect.setRight(draw_rect.width()) icon_rect.translate(0, (draw_rect.height() - icon_rect.height()) / 2) boldFont = QFont(painter.font()) boldFont.setPointSizeF(styleHelper.sidebarFontSize()) boldFont.setBold(True) painter.setFont(boldFont) painter.setPen(selected and QColor(255, 255, 255, 160) or QColor(0, 0, 0, 110)) textFlags = Qt.AlignHCenter | Qt.AlignVCenter painter.drawText(text_rect, textFlags, text) painter.setPen(selected and QColor(60, 60, 60) or styleHelper.panelTextColor()) if widget: fader_key = 'tab_' + text + '_fader' animation_key = 'tab_' + text + '_animation' tab_hover = widget.property('tab_hover') # int fader = widget.property(fader_key) # QPropertyAnimation animation = widget.property(animation_key) if not animation: mut_widget = widget fader = 0 mut_widget.setProperty(fader_key, fader) animation = QPropertyAnimation(mut_widget, fader_key, mut_widget) animation.valueChanged.connect(mut_widget.update) mut_widget.setProperty(animation_key, animation) if text == tab_hover: if animation.state( ) != QAbstractAnimation.Running and fader != 40: animation.stop() animation.setDuration(80) animation.setEndValue(40) animation.start() else: if animation.state( ) != QAbstractAnimation.Running and fader != 0: animation.stop() animation.setDuration(160) animation.setEndValue(0) animation.start() if not selected: painter.save() painter.fillRect(draw_rect, QColor(255, 255, 255, fader)) painter.setPen(QPen(QColor(255, 255, 255, fader), 1.0)) painter.drawLine( draw_rect.topLeft(), vertical_tabs and draw_rect.bottomLeft() or draw_rect.topRight()) painter.drawLine( draw_rect.bottomRight(), vertical_tabs and draw_rect.topRight() or draw_rect.bottomLeft()) painter.restore() if selected: iconMode = QIcon.Selected else: iconMode = QIcon.Normal styleHelper.drawIconWithShadow(v_opt.icon, icon_rect, painter, iconMode) painter.drawText(text_rect.translated(0, -1), textFlags, text) painter.restore()
class CameraImage(QWidget): _aspect_ratio = (setting.camera_resolution[1] / setting.camera_resolution[0]) def __init__(self, resize_leader): super().__init__() self._pixmap = None self._paint_rect = QRect(0, 0, self.width(), self.height()) self._hover = False self._inspect = False self._resize_leader = resize_leader self._setup_ui() def set_map(self, pixmap): if isinstance(pixmap, QPixmap): self._pixmap = pixmap self.update() def _setup_ui(self): self.setAttribute(Qt.WA_Hover, True) self.setContentsMargins(0, 0, 0, 0) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._inspect_map = icons.get('inspect') self._pixmap = icons.get('connect_none') def set_inspect(self, inspect): if self._inspect != inspect: self._inspect = inspect self.update() def resizeEvent(self, event): if self.width() * setting.camera_resolution[1] >\ self.height() * setting.camera_resolution[0]: width = self.height() / self._aspect_ratio width_margin = (self.width() - width) / 2 self._paint_rect = QRect(width_margin, 0, width, self.height()) else: height = self.width() * self._aspect_ratio height_margin = (self.height() - height) / 2 self._paint_rect = QRect(0, height_margin, self.width(), height) if (self._resize_leader and self.isVisible()): value = self._paint_rect.width() if value > 0: state.set('live_view_size', value) def enterEvent(self, event): if not self._inspect: self._hover = True self.setCursor(Qt.PointingHandCursor) def leaveEvent(self, event): self._hover = False self.unsetCursor() def paintEvent(self, event): painter = QPainter(self) if self._inspect: painter.drawPixmap( (self.width() - self._inspect_map.width()) / 2, (self.height() - self._inspect_map.height()) / 2, self._inspect_map) return if self._pixmap.width() > 50: painter.setRenderHint(QPainter.SmoothPixmapTransform) painter.drawPixmap(self._paint_rect, self._pixmap) else: painter.drawPixmap((self.width() - self._pixmap.width()) / 2, (self.height() - self._pixmap.height()) / 2, self._pixmap) if self._hover: painter.setPen(QPen(self.palette().highlight().color(), 2)) painter.drawRect(self._paint_rect)
def paint(self, painter, option, index): # noqa C901 ''' @param painter QPainter @param option index QStyleOptionViewItem @param option index QModelIndex ''' from ..LocationBar import LocationBar opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) w = opt.widget if w: style = w.style() else: style = QApplication.style() height = opt.rect.height() center = height / 2 + opt.rect.top() # Prepare link font # QFont linkFont = opt.font linkFont.setPointSize(linkFont.pointSize() - 1) linkMetrics = QFontMetrics(linkFont) leftPosition = self._padding * 2 rightPosition = opt.rect.right() - self._padding opt.state |= QStyle.State_Active if opt.state & QStyle.State_Selected: iconMode = QIcon.Selected colorRole = QPalette.HighlightedText colorLinkRole = QPalette.HighlightedText else: iconMode = QIcon.Normal colorRole = QPalette.Text colorLinkRole = QPalette.Link if const.OS_WIN: opt.palette.setColor(QPalette.All, QPalette.HighlightedText, opt.palette.color(QPalette.Active, QPalette.Text)) opt.palette.setColor(QPalette.All, QPalette.Highlight, opt.palette.base().color().darker(108)) textPalette = QPalette(opt.palette) if opt.state & QStyle.State_Enabled: textPalette.setCurrentColorGroup(QPalette.Normal) else: textPalette.setCurrentColorGroup(QPalette.Disabled) # Draw background style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, w) isVisitSearchItem = index.data(LocationCompleterModel.VisitSearchItemRole) isSearchSuggestion = index.data(LocationCompleterModel.SearchSuggestionRole) loadAction = LocationBar.LoadAction() isWebSearch = isSearchSuggestion # BookmarkItem bookmark = index.data(LocationCompleterModel.BookmarkItemRole) if isVisitSearchItem: text = index.data(LocationCompleterModel.SearchStringRole) loadAction = LocationBar.loadAction(text) isWebSearch = loadAction.type == LocationBar.LoadAction.Search if not self._forceVisitItem: bookmark = loadAction.bookmark # Draw icon iconSize = 16 iconYPos = center - iconSize / 2 iconRect = QRect(leftPosition, iconYPos, iconSize, iconSize) icon = index.data(Qt.DecorationRole) if not icon: icon = QIcon() pixmap = icon.pixmap(iconSize) if isSearchSuggestion or (isVisitSearchItem and isWebSearch): pixmap = QIcon.fromTheme('edit-find', QIcon(':/icons/menu/search-icon.svg')).pixmap(iconSize, iconMode) if isVisitSearchItem and bookmark: pixmap = bookmark.icon().pixmap(iconSize) elif loadAction.type == LocationBar.LoadAction.Search: if loadAction.searchEngine.name != LocationBar.searchEngine().name: pixmap = loadAction.searchEngine.icon.pixmap(iconSize) painter.drawPixmap(iconRect, pixmap) leftPosition = iconRect.right() + self._padding * 2 # Draw star to bookmark items starPixmapWidth = 0 if bookmark: icon = IconProvider.instance().bookmarkIcon starSize = QSize(16, 16) starPixmapWidth = starSize.width() pos = QPoint(rightPosition - starPixmapWidth, center - starSize.height() / 2) starRect = QRect(pos, starSize) painter.drawPixmap(starRect, icon.pixmap(starSize, iconMode)) searchText = index.data(LocationCompleterModel.SearchStringRole) # Draw title leftPosition += 2 titleRect = QRect(leftPosition, center - opt.fontMetrics.height() / 2, opt.rect.width() * 0.6, opt.fontMetrics.height()) title = index.data(LocationCompleterModel.TitleRole) painter.setFont(opt.font) if isVisitSearchItem: if bookmark: title = bookmark.title() else: title = index.data(LocationCompleterModel.SearchStringRole) searchText = '' leftPosition += self.viewItemDrawText(painter, opt, titleRect, title, textPalette.color(colorRole), searchText) leftPosition += self._padding * 2 # Trim link to maximum number characters that can be visible, # otherwise there may be perf issue with huge URLs maxChars = int((opt.rect.width() - leftPosition) / opt.fontMetrics.width('i')) link = index.data(Qt.DisplayRole) if not link.startswith('data') and not link.startswith('javascript'): link = unquote(link)[:maxChars] else: link = link[:maxChars] if isVisitSearchItem or isSearchSuggestion: if not (opt.state & QStyle.State_Selected) and not (opt.state & QStyle.State_MouseOver): link = '' elif isVisitSearchItem and (not isWebSearch or self._forceVisitItem): link = _('Visit') else: searchEngineName = loadAction.searchEngine.name if not searchEngineName: searchEngineName = LocationBar.searchEngine().name link = _('Search with %s') % searchEngineName if bookmark: link = bookmark.url().toString() # Draw separator if link: separator = '-' separatorRect = QRect(leftPosition, center - linkMetrics.height() / 2, linkMetrics.width(separator), linkMetrics.height()) style.drawItemText(painter, separatorRect, Qt.AlignCenter, textPalette, True, separator, colorRole) leftPosition += separatorRect.width() + self._padding * 2 # Draw link leftLinkEdge = leftPosition rightLinkEdge = rightPosition - self._padding - starPixmapWidth linkRect = QRect(leftLinkEdge, center - linkMetrics.height() / 2, rightLinkEdge - leftLinkEdge, linkMetrics.height()) painter.setFont(linkFont) # Darw url (or switch to tab) tabPos = index.data(LocationCompleterModel.TabPositionTabRole) if gVar.appSettings.showSwitchTab and not self._forceVisitItem and tabPos != -1: tabIcon = QIcon(':/icons/menu/tab.svg') iconRect = QRect(linkRect) iconRect.setX(iconRect.x()) iconRect.setWidth(16) painter.drawPixmap(iconRect, tabIcon.pixmap(iconRect.size(), iconMode)) textRect = QRect(linkRect) textRect.setX(textRect.x() + self._padding + 16 + self._padding) self.viewItemDrawText(painter, opt, textRect, _('Switch to tab'), textPalette.color(colorLinkRole)) elif isVisitSearchItem or isSearchSuggestion: self.viewItemDrawText(painter, opt, linkRect, link, textPalette.color(colorLinkRole)) else: self.viewItemDrawText(painter, opt, linkRect, link, textPalette.color(colorLinkRole), searchText)