Exemplo n.º 1
0
 def paintEvent(self, event):
     canvas_size = self.rect()
     width = self.current_pixmap_size.width()
     extrax = canvas_size.width() - width
     if extrax < 0:
         extrax = 0
     x = int(extrax/2.)
     height = self.current_pixmap_size.height()
     extray = canvas_size.height() - height
     if extray < 0:
         extray = 0
     y = int(extray/2.)
     target = QRect(x, y, width, height)
     p = QPainter(self)
     p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
     try:
         dpr = self.devicePixelRatioF()
     except AttributeError:
         dpr = self.devicePixelRatio()
     spmap = self.pixmap.scaled(target.size() * dpr, Qt.KeepAspectRatio, Qt.SmoothTransformation)
     spmap.setDevicePixelRatio(dpr)
     p.drawPixmap(target, spmap)
     if gprefs['bd_overlay_cover_size']:
         sztgt = target.adjusted(0, 0, 0, -4)
         f = p.font()
         f.setBold(True)
         p.setFont(f)
         sz = u'\u00a0%d x %d\u00a0'%(self.pixmap.width(), self.pixmap.height())
         flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine
         szrect = p.boundingRect(sztgt, flags, sz)
         p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200))
         p.setPen(QPen(QColor(255,255,255)))
         p.drawText(sztgt, flags, sz)
     p.end()
Exemplo n.º 2
0
 def paintEvent(self, event):
     if self.pixmap.isNull():
         return
     canvas_size = self.rect()
     width = self.current_pixmap_size.width()
     extrax = canvas_size.width() - width
     if extrax < 0:
         extrax = 0
     x = int(extrax / 2.)
     height = self.current_pixmap_size.height()
     extray = canvas_size.height() - height
     if extray < 0:
         extray = 0
     y = int(extray / 2.)
     target = QRect(x, y, min(canvas_size.width(), width),
                    min(canvas_size.height(), height))
     p = QPainter(self)
     p.setRenderHints(QPainter.RenderHint.Antialiasing
                      | QPainter.RenderHint.SmoothPixmapTransform)
     p.drawPixmap(
         target,
         self.pixmap.scaled(target.size(),
                            Qt.AspectRatioMode.KeepAspectRatio,
                            Qt.TransformationMode.SmoothTransformation))
     p.end()
Exemplo n.º 3
0
 def paintEvent(self, event):
     if self.pixmap.isNull():
         return
     canvas_size = self.rect()
     width = self.current_pixmap_size.width()
     extrax = canvas_size.width() - width
     if extrax < 0:
         extrax = 0
     x = int(extrax / 2.0)
     height = self.current_pixmap_size.height()
     extray = canvas_size.height() - height
     if extray < 0:
         extray = 0
     y = int(extray / 2.0)
     target = QRect(x, y, min(canvas_size.width(), width), min(canvas_size.height(), height))
     p = QPainter(self)
     p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
     p.drawPixmap(target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
     p.end()
Exemplo n.º 4
0
    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()
    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)