Ejemplo n.º 1
0
    def updateEditorGeometry(self, editor, option, index):
        if editor is None:
            return
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.showDecorationSelected = True
        opt.decorationSize = QSize(0, 0)  # We want the editor to cover the decoration
        style = QApplication.style()
        geom = style.subElementRect(style.SE_ItemViewItemText, opt, None)

        if editor.layoutDirection() == Qt.RightToLeft:
            delta = editor.sizeHint().width() - geom.width()
            if delta > 0:
                geom.adjust(-delta, 0, 0, 0)
        editor.setGeometry(geom)
Ejemplo n.º 2
0
    def updateEditorGeometry(self, editor, option, index):
        if editor is None:
            return
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.showDecorationSelected = True
        opt.decorationSize = QSize(
            0, 0)  # We want the editor to cover the decoration
        style = QApplication.style()
        geom = style.subElementRect(style.SE_ItemViewItemText, opt, None)

        if editor.layoutDirection() == Qt.RightToLeft:
            delta = editor.sizeHint().width() - geom.width()
            if delta > 0:
                geom.adjust(-delta, 0, 0, 0)
        editor.setGeometry(geom)
    def sizeHint(self, option, index):
        '''
        @param option QStyleOptionViewItem
        @param index QModelIndex
        @return: QSize
        '''
        if not self._rowHeight:
            opt = QStyleOptionViewItem(option)
            self.initStyleOption(opt, index)

            w = opt.widget
            if w:
                style = w.style()
            else:
                style = QApplication.style()
            padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin, None) + 1

            self._padding = max(padding, 3)
            self._rowHeight = 4 * self._padding + max(16, opt.fontMetrics.height())

        return QSize(200, self._rowHeight)
Ejemplo n.º 4
0
 def paint(self, p, opt, idx):
     copy = QStyleOptionViewItem(opt)
     copy.showDecorationSelected = True
     if self.view.currentIndex() == idx:
         copy.state |= QStyle.State_HasFocus
     QStyledItemDelegate.paint(self, p, copy, idx)
Ejemplo n.º 5
0
    def updateEditorGeometry(self, editor, option, index):
        if editor is None:
            return
        fm = editor.fontMetrics()

        # get the original size of the edit widget
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.showDecorationSelected = True
        opt.decorationSize = QSize(0, 0)  # We want the editor to cover the decoration
        style = QApplication.style()
        initial_geometry = style.subElementRect(style.SE_ItemViewItemText, opt, None)
        orig_width = initial_geometry.width()

        # Compute the required width: the width that can show all of the current value
        if hasattr(self, 'get_required_width'):
            new_width = self.get_required_width(editor, style, fm)
        else:
            # The line edit box seems to extend by the space consumed by an 'M'.
            # So add that to the text
            text = self.displayText(index.data(Qt.DisplayRole), QLocale()) + u'M'
            srect = style.itemTextRect(fm, editor.geometry(), Qt.AlignLeft, False, text)
            new_width = srect.width()

        # Now get the size of the combo/spinner arrows and add them to the needed width
        if isinstance(editor, (QComboBox, QDateTimeEdit)):
            r = style.subControlRect(QStyle.CC_ComboBox, QStyleOptionComboBox(),
                                      QStyle.SC_ComboBoxArrow, editor)
            new_width += r.width()
        elif isinstance(editor, (QSpinBox, QDoubleSpinBox)):
            r = style.subControlRect(QStyle.CC_SpinBox, QStyleOptionSpinBox(),
                                  QStyle.SC_SpinBoxUp, editor)
            new_width += r.width()

        # Compute the maximum we can show if we consume the entire viewport
        max_width = (self.table_widget.horizontalScrollBar().geometry().width() -
                     self.table_widget.verticalHeader().width())
        # What we have to display might not fit. If so, adjust down
        new_width = new_width if new_width < max_width else max_width

        # See if we need to change the editor's geometry
        if new_width <= orig_width:
            delta_x = 0
            delta_width = 0
        else:
            # Compute the space available from the left edge of the widget to
            # the right edge of the displayed table (the viewport) and the left
            # edge of the widget to the left edge of the viewport. These are
            # used to position the edit box
            space_left = initial_geometry.x()
            space_right = max_width - space_left

            if editor.layoutDirection() == Qt.RightToLeft:
                # If language is RtL, align to the cell's right edge if possible
                cw = initial_geometry.width()
                consume_on_left = min(space_left, new_width - cw)
                consume_on_right = max(0, new_width - (consume_on_left + cw))
                delta_x = -consume_on_left
                delta_width = consume_on_right
            else:
                # If language is LtR, align to the left if possible
                consume_on_right = min(space_right, new_width)
                consume_on_left = max(0, new_width - consume_on_right)
                delta_x = -consume_on_left
                delta_width = consume_on_right - initial_geometry.width()

        initial_geometry.adjust(delta_x, 0, delta_width, 0)
        editor.setGeometry(initial_geometry)
Ejemplo n.º 6
0
 def paint(self, p, opt, idx):
     copy = QStyleOptionViewItem(opt)
     copy.showDecorationSelected = True
     if self.view.currentIndex() == idx:
         copy.state |= QStyle.State_HasFocus
     QStyledItemDelegate.paint(self, p, copy, idx)
Ejemplo n.º 7
0
    def updateEditorGeometry(self, editor, option, index):
        if editor is None:
            return
        fm = editor.fontMetrics()

        # get the original size of the edit widget
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.showDecorationSelected = True
        opt.decorationSize = QSize(
            0, 0)  # We want the editor to cover the decoration
        style = QApplication.style()
        initial_geometry = style.subElementRect(style.SE_ItemViewItemText, opt,
                                                None)
        orig_width = initial_geometry.width()

        # Compute the required width: the width that can show all of the current value
        if hasattr(self, 'get_required_width'):
            new_width = self.get_required_width(editor, style, fm)
        else:
            # The line edit box seems to extend by the space consumed by an 'M'.
            # So add that to the text
            text = self.displayText(index.data(Qt.DisplayRole),
                                    QLocale()) + u'M'
            srect = style.itemTextRect(fm, editor.geometry(), Qt.AlignLeft,
                                       False, text)
            new_width = srect.width()

        # Now get the size of the combo/spinner arrows and add them to the needed width
        if isinstance(editor, (QComboBox, QDateTimeEdit)):
            r = style.subControlRect(QStyle.CC_ComboBox,
                                     QStyleOptionComboBox(),
                                     QStyle.SC_ComboBoxArrow, editor)
            new_width += r.width()
        elif isinstance(editor, (QSpinBox, QDoubleSpinBox)):
            r = style.subControlRect(QStyle.CC_SpinBox, QStyleOptionSpinBox(),
                                     QStyle.SC_SpinBoxUp, editor)
            new_width += r.width()

        # Compute the maximum we can show if we consume the entire viewport
        max_width = (
            self.table_widget.horizontalScrollBar().geometry().width() -
            self.table_widget.verticalHeader().width())
        # What we have to display might not fit. If so, adjust down
        new_width = new_width if new_width < max_width else max_width

        # See if we need to change the editor's geometry
        if new_width <= orig_width:
            delta_x = 0
            delta_width = 0
        else:
            # Compute the space available from the left edge of the widget to
            # the right edge of the displayed table (the viewport) and the left
            # edge of the widget to the left edge of the viewport. These are
            # used to position the edit box
            space_left = initial_geometry.x()
            space_right = max_width - space_left

            if editor.layoutDirection() == Qt.RightToLeft:
                # If language is RtL, align to the cell's right edge if possible
                cw = initial_geometry.width()
                consume_on_left = min(space_left, new_width - cw)
                consume_on_right = max(0, new_width - (consume_on_left + cw))
                delta_x = -consume_on_left
                delta_width = consume_on_right
            else:
                # If language is LtR, align to the left if possible
                consume_on_right = min(space_right, new_width)
                consume_on_left = max(0, new_width - consume_on_right)
                delta_x = -consume_on_left
                delta_width = consume_on_right - initial_geometry.width()

        initial_geometry.adjust(delta_x, 0, delta_width, 0)
        editor.setGeometry(initial_geometry)
    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)