Beispiel #1
0
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if (standard) {
            return QStyledItemDelegate::sizeHint(option, index);
        }
        if (!index.isValid()) {
            return QSize(0, 0);
        }

        const QString text = index.model()->data(index, Qt::DisplayRole).toString();
        const QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
        const QPixmap pixmap = icon.pixmap(iconSize, iconSize);

        QFontMetrics fm = option.fontMetrics;
        int gap = fm.height();
        #if QT_VERSION >= 0x050100
        QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio();
        #else
        QSize layoutSize = pixmap.size();
        #endif

        if (layoutSize.height() == 0) {
            /**
            * No pixmap loaded yet, we'll use the default icon size in this case.
            */
            layoutSize=QSize(iconSize, iconSize);
        }

        QTextLayout iconTextLayout(text, option.font);
        int wt = layoutText(&iconTextLayout, qMax(3 * layoutSize.width(), 8 * fm.height()));
        int ht = iconTextLayout.boundingRect().height();

        int width, height;
        if (text.isEmpty()) {
            height = layoutSize.height();
        } else {
            height = layoutSize.height() + ht + 10;

        }

        width = qMax(wt, layoutSize.width()) + gap;
        return QSize(width, height);
    }
Beispiel #2
0
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
        if (!index.isValid()) {
            return QSize(0, 0);
        }

        const QString text = index.model()->data(index, Qt::DisplayRole).toString();
        const QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
        const QPixmap pixmap = icon.pixmap(iconSize, iconSize);

        QFontMetrics fm = option.fontMetrics;
        int gap = fm.height();
        int wp = pixmap.width();
        int hp = pixmap.height();

        if (hp == 0) {
            /**
            * No pixmap loaded yet, we'll use the default icon size in this case.
            */
            hp = iconSize;
            wp = iconSize;
        }

        QTextLayout iconTextLayout(text, option.font);
        int wt = layoutText(&iconTextLayout, qMax(3 * wp, 8 * fm.height()));
        int ht = iconTextLayout.boundingRect().height();

        int width, height;
        if (text.isEmpty()) {
            height = hp;
        } else {
            height = hp + ht + 10;

        }

        width = qMax(wt, wp) + gap;
        return QSize(width, height);
    }
Beispiel #3
0
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if (!index.isValid()) {
            return;
        }
        bool mouseOver=option.state&QStyle::State_MouseOver;
        bool selected=option.state&QStyle::State_Selected;

        if (standard) {
            if (GtkStyle::isActive()) {
                bool mouseOver=option.state&QStyle::State_MouseOver;
                QStyleOptionViewItemV4 opt = option;
                initStyleOption(&opt, index);

                if (!underMouse) {
                    mouseOver=false;
                }

                if (mouseOver) {
                    opt.showDecorationSelected=true;

                    GtkStyle::drawSelection(option, painter, selected ? 0.75 : 0.25);
                    opt.showDecorationSelected=false;
                    opt.state&=~(QStyle::State_MouseOver|QStyle::State_Selected);
                    opt.backgroundBrush=QBrush(Qt::transparent);
                    if (selected) {
                        opt.palette.setBrush(QPalette::Text, opt.palette.highlightedText());
                    }
                }
                QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
            } else {
                QStyledItemDelegate::paint(painter, option, index);
            }
            return;
        }

        bool gtk=mouseOver && GtkStyle::isActive();
        bool drawBgnd=true;

        if (!underMouse) {
            if (mouseOver && !selected) {
                drawBgnd=false;
            }
            mouseOver=false;
        }

        const QString text = index.model()->data(index, Qt::DisplayRole).toString();
        const QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
        const QPixmap pixmap = icon.pixmap(iconSize, iconSize);

        QFontMetrics fm = painter->fontMetrics();
        #if QT_VERSION >= 0x050100
        QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio();
        #else
        QSize layoutSize = pixmap.size();
        #endif

        QTextLayout iconTextLayout(text, option.font);
        QTextOption textOption(Qt::AlignHCenter);
        iconTextLayout.setTextOption(textOption);
        int maxWidth = qMax(3 * layoutSize.width(), 8 * fm.height());
        layoutText(&iconTextLayout, maxWidth);

        QPen pen = painter->pen();
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                ? QPalette::Normal : QPalette::Disabled;
        if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) {
            cg = QPalette::Inactive;
        }

        QStyleOptionViewItemV4 opt(option);
        opt.showDecorationSelected = true;
        QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();

        if (drawBgnd) {
            if (mouseOver && gtk) {
                GtkStyle::drawSelection(opt, painter, selected ? 0.75 : 0.25);
            } else {
                style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
            }
        }

        #ifndef Q_OS_WIN
        if (selected) {
            painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
        } else
        #endif
        {
            painter->setPen(option.palette.color(cg, QPalette::Text));
        }

        painter->drawPixmap(option.rect.x() + (option.rect.width()/2)-(layoutSize.width()/2), option.rect.y() + 5, pixmap);
        if (!text.isEmpty()) {
            iconTextLayout.draw(painter, QPoint(option.rect.x() + (option.rect.width()/2)-(maxWidth/2), option.rect.y() + layoutSize.height()+7));
        }
        painter->setPen(pen);
        drawFocus(painter, option, option.rect);
    }
Beispiel #4
0
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        if (!index.isValid()) {
            return;
        }
        bool mouseOver = option.state & QStyle::State_MouseOver;
        bool gtk = mouseOver && qApp->style()->inherits("QGtkStyle");
        bool selected = option.state & QStyle::State_Selected;
        bool drawBgnd = true;

        if (!underMouse) {
            if (mouseOver && !selected) {
                drawBgnd = false;
            }
            mouseOver = false;
        }

        const QString text = index.model()->data(index, Qt::DisplayRole).toString();
        const QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
        const QPixmap pixmap = icon.pixmap(iconSize, iconSize);

        QFontMetrics fm = painter->fontMetrics();
        int wp = pixmap.width();
        int hp = pixmap.height();

        QTextLayout iconTextLayout(text, option.font);
        QTextOption textOption(Qt::AlignHCenter);
        iconTextLayout.setTextOption(textOption);
        int maxWidth = qMax(3 * wp, 8 * fm.height());
        layoutText(&iconTextLayout, maxWidth);

        QPen pen = painter->pen();
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                                  ? QPalette::Normal : QPalette::Disabled;
        if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) {
            cg = QPalette::Inactive;
        }

        QStyleOptionViewItem opt(option);
        opt.showDecorationSelected = true;
        QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();

        if (drawBgnd) {
            if (mouseOver && gtk) {
                painter->save();
                opt.state |= QStyle::State_Selected;
                painter->setOpacity(selected ? 0.75 : 0.25);
            }
            style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
            if (mouseOver && gtk) {
                painter->restore();
            }
        }

        if (selected) {
            painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
        } else {
            painter->setPen(option.palette.color(cg, QPalette::Text));
        }

        painter->drawPixmap(option.rect.x() + (option.rect.width() / 2) - (wp / 2), option.rect.y() + 5, pixmap);
        if (!text.isEmpty()) {
            iconTextLayout.draw(painter, QPoint(option.rect.x() + (option.rect.width() / 2) - (maxWidth / 2), option.rect.y() + hp + 7));
        }
        painter->setPen(pen);
        drawFocus(painter, option, option.rect);
    }