Esempio n. 1
0
void AnnotatedItemDelegate::paint(QPainter *painter,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    auto opt = option;
    initStyleOption(&opt, index);

    QStyle *style = QApplication::style();
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);

    QString annotation = index.data(m_annotationRole).toString();
    if (!annotation.isEmpty()) {

        int newlinePos = annotation.indexOf(QLatin1Char('\n'));
        if (newlinePos != -1) {
            // print first line with '...' at end
            const QChar ellipsisChar(0x2026);
            annotation = annotation.left(newlinePos) + ellipsisChar;
        }

        QPalette disabled(opt.palette);
        disabled.setCurrentColorGroup(QPalette::Disabled);

        painter->save();
        painter->setPen(disabled.color(QPalette::WindowText));

        static int extra = opt.fontMetrics.width(m_delimiter) + 10;
        const QPixmap &pixmap = opt.icon.pixmap(opt.decorationSize);
        const QRect &iconRect = style->itemPixmapRect(opt.rect, opt.decorationAlignment, pixmap);
        const QRect &displayRect = style->itemTextRect(opt.fontMetrics, opt.rect,
            opt.displayAlignment, true, index.data(Qt::DisplayRole).toString());
        QRect annotationRect = style->itemTextRect(opt.fontMetrics, opt.rect,
            opt.displayAlignment, true, annotation);
        annotationRect.adjust(iconRect.width() + displayRect.width() + extra, 0,
                              iconRect.width() + displayRect.width() + extra, 0);

        QApplication::style()->drawItemText(painter, annotationRect,
            Qt::AlignLeft | Qt::AlignBottom, disabled, true, annotation);

        painter->restore();
    }
}
Esempio n. 2
0
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
    int row = index.row();

    ItemWidget *w = m_cache[row];
    if (w == NULL)
        return;

    const QRect &rect = option.rect;

    /* render background (selected, alternate, ...) */
    QStyle *style = m_parent->style();
    style->drawControl(QStyle::CE_ItemViewItem, &option, painter);
    QPalette::ColorRole role = option.state & QStyle::State_Selected ?
                QPalette::HighlightedText : QPalette::Text;

    /* render number */
    QRect numRect(0, 0, 0, 0);
    if (m_showNumber) {
        QString num = QString::number(row) + "  ";
        painter->save();
        painter->setFont(m_numberFont);
        style->drawItemText(painter, rect.translated(4, 4), 0,
                            m_numberPalette, true, num,
                            role);
        painter->restore();
        numRect = style->itemTextRect( QFontMetrics(m_numberFont), rect, 0,
                                       true, num );
    }

    /* highlight search string */
    w->setHighlight(m_re, m_foundFont, m_foundPalette);

    /* text color for selected/unselected item */
    QPalette palette = w->widget()->palette();
    palette.setColor( QPalette::Text, option.palette.color(role) );
    w->widget()->setPalette(palette);

    /* render widget */
    w->widget()->show();
}
void WidgetDiskItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyleOptionViewItemV4 opt = *qstyleoption_cast<const QStyleOptionViewItemV4*>(&option);

    bool selected = opt.state & QStyle::State_Selected;

    QStyle* style  = (opt.widget != NULL ? opt.widget->style() : QApplication::style());
    QString text   = index.data(Qt::DisplayRole).toString();
    QIcon   icon   = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    QPixmap pixmap = icon.pixmap(opt.decorationSize, (selected == true ? QIcon::Selected : QIcon::Normal));

    int alignment = 0;
    switch (opt.decorationPosition) {
        case QStyleOptionViewItem::Left:
            alignment |= Qt::AlignLeft | Qt::AlignVCenter;
            break;
        case QStyleOptionViewItem::Right:
            alignment |= Qt::AlignRight | Qt::AlignVCenter;
            break;
        case QStyleOptionViewItem::Top:
            alignment |= Qt::AlignTop | Qt::AlignHCenter;
            break;
        case QStyleOptionViewItem::Bottom:
            alignment |= Qt::AlignBottom | Qt::AlignHCenter;
            break;
    }

    QRect icon_rect = style->itemPixmapRect(opt.rect.adjusted(adjust_dx, adjust_dy, -adjust_dx, -adjust_dy), alignment, pixmap);
    style->drawItemPixmap(painter, icon_rect, opt.decorationAlignment, pixmap);

    QRect free_rect;
    switch (opt.decorationPosition) {
        case QStyleOptionViewItem::Left:
            free_rect = opt.rect.adjusted(icon_rect.width() + adjust_dx * 3, adjust_dy, -adjust_dx, -adjust_dy);
            break;
        case QStyleOptionViewItem::Right:
            free_rect = opt.rect.adjusted(adjust_dx, adjust_dy, -icon_rect.width() - adjust_dx * 3, -adjust_dy);
            break;
        case QStyleOptionViewItem::Top:
            free_rect = opt.rect.adjusted(adjust_dx, icon_rect.height() + adjust_dy * 3, -adjust_dx, -adjust_dy);
            break;
        case QStyleOptionViewItem::Bottom:
            free_rect = opt.rect.adjusted(adjust_dx, adjust_dy, -adjust_dx, -icon_rect.height() - adjust_dy * 3);
            break;
    }

    if (selected == true) {
        int dr = qMax(adjust_dx, adjust_dy);

        QRect text_rect = style->itemTextRect(opt.fontMetrics, free_rect, alignment, true, text);
        QRect high_rect = text_rect.adjusted(-dr, -dr, dr, dr);

        painter->save();

        painter->setRenderHint(QPainter::Antialiasing);

        painter->setPen(opt.palette.highlight().color());
        painter->setBrush(opt.palette.highlight());
        painter->drawRoundedRect(high_rect, dr, dr);

        painter->restore();
    }

    style->drawItemText(painter, free_rect, alignment, option.palette, true, text, (selected == true ? QPalette::HighlightedText : QPalette::WindowText));
}
Esempio n. 4
0
void BlogListDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    BlogListWidget * listWidget = qobject_cast<BlogListWidget *>(this->parent());
    if (listWidget == NULL) return;

    QStyle * style = listWidget->style();
    if (style == NULL) return;

    painter->save();

    QFont itemFont(painter->font());

    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, listWidget);

    QPixmap pixmap = qvariant_cast<QPixmap>(index.data(IconRole));
	QString title = index.data(TitleRole).toString();
	QString date = index.data(DateRole).toString();
	QString author = index.data(AuthorRole).toString();
	QString intro = index.data(IntroRole).toString();

  //  QRect rect;
	int imageSpace = ImageSpace + 10;

    // TITLE
    painter->setPen(listWidget->titleTextColor());
    QFont titleFont(listWidget->titleTextFontFamily());
    titleFont.setPixelSize(pixelSize(listWidget->titleTextFontSize()));
    painter->setFont(titleFont);
    QRect rect = option.rect.adjusted(imageSpace, TopSpace, 0, 0);
    style->drawItemText(painter, rect, Qt::AlignLeft, option.palette, true, title);
    QFontMetrics titleFontMetrics(titleFont);

    // INTRO
    painter->setPen(listWidget->introTextColor());
    QFont introFont(listWidget->introTextFontFamily());
    introFont.setPixelSize(pixelSize(listWidget->introTextFontSize()));
    painter->setFont(introFont);
    rect = option.rect.adjusted(imageSpace, TopSpace + titleFontMetrics.lineSpacing() + pixelSize(listWidget->titleTextExtraLeading()) , 0, 0);
    style->drawItemText(painter, rect, Qt::AlignLeft, option.palette, true, intro);
    QFontMetrics introFontMetrics(introFont);

    // DATE
    painter->setPen(listWidget->dateTextColor());
    QFont font(listWidget->dateTextFontFamily());
    font.setPixelSize(pixelSize(listWidget->dateTextFontSize()));
    painter->setFont(font);
    rect = option.rect.adjusted(imageSpace, TopSpace + titleFontMetrics.lineSpacing() + introFontMetrics.lineSpacing() + pixelSize(listWidget->introTextExtraLeading()), 0, 0);
    style->drawItemText(painter, rect, Qt::AlignLeft, option.palette, true, date);
    QFontMetrics dateTextFontMetrics(font);

    // AUTHOR
    QRect textRect = style->itemTextRect(dateTextFontMetrics, option.rect, Qt::AlignLeft, true, date);
    rect = option.rect.adjusted(imageSpace + textRect.width() + 7, TopSpace + titleFontMetrics.lineSpacing() + introFontMetrics.lineSpacing() + pixelSize(listWidget->introTextExtraLeading()), 0, 0);
    style->drawItemText(painter, rect, Qt::AlignLeft, option.palette, true, author);

    if (!pixmap.isNull()) {
		//ic.paint(painter, option.rect, Qt::AlignVCenter|Qt::AlignLeft);
        style->drawItemPixmap(painter, option.rect.adjusted(0, TopSpace, 0, -TopSpace), Qt::AlignLeft, pixmap);
	}

    painter->restore();
}