Esempio n. 1
0
void PhotoDelegate::paint(
        QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QPen oldPen = painter->pen();

    QStyle *style = QApplication::style();

    int margin = style->pixelMetric(QStyle::PM_ButtonMargin);

    QRect rect = option.rect;

    if (option.state & QStyle::State_HasFocus) {
        painter->fillRect(rect, option.palette.highlight());

        painter->setPen(option.palette.color(QPalette::HighlightedText));
    }

    rect.adjust(margin, margin, -margin, -margin);

    QRect decorationRect = rect;
    decorationRect.setRight(decorationRect.left() + option.decorationSize.width());

    QPixmap decoration = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole));
    if (!decoration.isNull())
        style->drawItemPixmap(painter, decorationRect, Qt::AlignCenter, decoration);
    else
        painter->drawRect(decorationRect);

    painter->setPen(oldPen);
}
void KTp::ContactViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyle *style = QApplication::style();
    int textHeight = option.fontMetrics.height() * 2;

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

    QRect avatarRect = option.rect.adjusted(0, 0, 0, -textHeight);
    QRect textRect = option.rect.adjusted(0, option.rect.height() - textHeight, 0, -3);

    QPixmap avatar;
    avatar.load(index.data(KTp::ContactAvatarPathRole).toString());
    if (avatar.isNull()) {
        avatar = QIcon::fromTheme(QStringLiteral("im-user-online")).pixmap(option.decorationSize);
    } else if (avatar.width() > option.decorationSize.width() || avatar.height() > option.decorationSize.height()) {
        //resize larger avatars if required
        avatar = avatar.scaled(option.decorationSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        //draw leaving paddings on smaller (or non square) avatars
    }
    style->drawItemPixmap(painter, avatarRect, Qt::AlignCenter, avatar);


    QTextOption textOption;
    textOption.setAlignment(Qt::AlignCenter);
    textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    painter->drawText(textRect, index.data().toString(), textOption);

}
Esempio n. 3
0
void AlbumDelegate::paint(
        QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QPen oldPen = painter->pen();

    QStyle *style = QApplication::style();

    int margin = style->pixelMetric(QStyle::PM_ButtonMargin);

    QRect rect = option.rect;

    if (option.state & QStyle::State_HasFocus) {
        painter->fillRect(rect, option.palette.highlight());

        painter->setPen(option.palette.color(QPalette::HighlightedText));
    }

    rect.adjust(margin, margin, -margin, -margin);

    QRect decorationRect = rect;
    decorationRect.setRight(decorationRect.left() + option.decorationSize.width());

    QPixmap decoration = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole));
    if (!decoration.isNull())
        style->drawItemPixmap(painter, decorationRect, Qt::AlignCenter, decoration);
    else
        painter->drawRect(decorationRect);

    rect.setLeft(decorationRect.right() + margin);

    QString text = index.data(Qt::DisplayRole).toString();

    QRect textRect;
    painter->drawText(rect, Qt::TextWordWrap, text, &textRect);
    rect.setTop(textRect.bottom() + margin);

    text = index.data(Artist).toString();

    painter->drawText(rect, Qt::TextWordWrap, text, &textRect);
    rect.setTop(textRect.bottom() + margin);

    QVariant trackCount = index.data(TrackCount);

    if (trackCount.isValid()) {
        text = trackCount.toInt() != 1
                ? tr("%1 Songs").arg(trackCount.toInt())
                : tr("1 Song");

        painter->drawText(rect, Qt::TextWordWrap, text, &textRect);
    }

    painter->setPen(oldPen);
}
Esempio n. 4
0
void QMirrorLabel::paintEvent(QPaintEvent *event)
{

    QStyle *style = QWidget::style();
    QPainter painter(this);
    drawFrame(&painter);
    QRect cr = contentsRect();
    cr.adjust(margin(), margin(), -margin(), -margin());
    int align = QStyle::visualAlignment(layoutDirection(), QFlag(alignment()));

    if(!text().isNull())
    {
        painter.save();

        painter.scale(-1.0, 1.0);
        QPointF	translationOffset;
        translationOffset.setX(qreal(-cr.width()));
        translationOffset.setY(0.0);
        painter.translate(translationOffset);
        style->drawItemText(&painter, cr, align, palette(), isEnabled(), text(), foregroundRole());

        painter.restore();
    }

    if(pixmap())
    {
        QPixmap pix = *pixmap();

        painter.save();

        painter.scale(-1.0, 1.0);
        QPointF	translationOffset;
        translationOffset.setX(qreal(-cr.width()));
        translationOffset.setY(0.0);
        painter.translate(translationOffset);
        style->drawItemPixmap(&painter, cr, align, pix);

        painter.restore();
    }
}
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. 6
0
/*!\reimp
*/
void QLabel::paintEvent(QPaintEvent *)
{
    Q_D(QLabel);
    QStyle *style = QWidget::style();
    QPainter painter(this);
    drawFrame(&painter);
    QRect cr = contentsRect();
    cr.adjust(d->margin, d->margin, -d->margin, -d->margin);
    int align = QStyle::visualAlignment(d->isTextLabel ? d->textDirection()
                                                       : layoutDirection(), QFlag(d->align));

#if QT_CONFIG(movie)
    if (d->movie) {
        if (d->scaledcontents)
            style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size()));
        else
            style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap());
    }
    else
#endif
    if (d->isTextLabel) {
        QRectF lr = d->layoutRect().toAlignedRect();
        QStyleOption opt;
        opt.initFrom(this);
#ifndef QT_NO_STYLE_STYLESHEET
        if (QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(style)) {
            cssStyle->styleSheetPalette(this, &opt, &opt.palette);
        }
#endif
        if (d->control) {
#ifndef QT_NO_SHORTCUT
            const bool underline = (bool)style->styleHint(QStyle::SH_UnderlineShortcut, 0, this, 0);
            if (d->shortcutId != 0
                && underline != d->shortcutCursor.charFormat().fontUnderline()) {
                QTextCharFormat fmt;
                fmt.setFontUnderline(underline);
                d->shortcutCursor.mergeCharFormat(fmt);
            }
#endif
            d->ensureTextLayouted();

            QAbstractTextDocumentLayout::PaintContext context;
            // Adjust the palette
            context.palette = opt.palette;

            if (foregroundRole() != QPalette::Text && isEnabled())
                context.palette.setColor(QPalette::Text, context.palette.color(foregroundRole()));

            painter.save();
            painter.translate(lr.topLeft());
            painter.setClipRect(lr.translated(-lr.x(), -lr.y()));
            d->control->setPalette(context.palette);
            d->control->drawContents(&painter, QRectF(), this);
            painter.restore();
        } else {
            int flags = align | (d->textDirection() == Qt::LeftToRight ? Qt::TextForceLeftToRight
                                                                       : Qt::TextForceRightToLeft);
            if (d->hasShortcut) {
                flags |= Qt::TextShowMnemonic;
                if (!style->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
                    flags |= Qt::TextHideMnemonic;
            }
            style->drawItemText(&painter, lr.toRect(), flags, opt.palette, isEnabled(), d->text, foregroundRole());
        }
    } else
#ifndef QT_NO_PICTURE
    if (d->picture) {
        QRect br = d->picture->boundingRect();
        int rw = br.width();
        int rh = br.height();
        if (d->scaledcontents) {
            painter.save();
            painter.translate(cr.x(), cr.y());
            painter.scale((double)cr.width()/rw, (double)cr.height()/rh);
            painter.drawPicture(-br.x(), -br.y(), *d->picture);
            painter.restore();
        } else {
            int xo = 0;
            int yo = 0;
            if (align & Qt::AlignVCenter)
                yo = (cr.height()-rh)/2;
            else if (align & Qt::AlignBottom)
                yo = cr.height()-rh;
            if (align & Qt::AlignRight)
                xo = cr.width()-rw;
            else if (align & Qt::AlignHCenter)
                xo = (cr.width()-rw)/2;
            painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture);
        }
    } else
#endif
    if (d->pixmap && !d->pixmap->isNull()) {
        QPixmap pix;
        if (d->scaledcontents) {
            QSize scaledSize = cr.size() * devicePixelRatioF();
            if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) {
                if (!d->cachedimage)
                    d->cachedimage = new QImage(d->pixmap->toImage());
                delete d->scaledpixmap;
                QImage scaledImage =
                    d->cachedimage->scaled(scaledSize,
                                           Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
                d->scaledpixmap = new QPixmap(QPixmap::fromImage(scaledImage));
                d->scaledpixmap->setDevicePixelRatio(devicePixelRatioF());
            }
            pix = *d->scaledpixmap;
        } else
            pix = *d->pixmap;
        QStyleOption opt;
        opt.initFrom(this);
        if (!isEnabled())
            pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt);
        style->drawItemPixmap(&painter, cr, align, pix);
    }
}
Esempio n. 7
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();
}
Esempio n. 8
0
void AMDetailedItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const  {

	QStyleOptionViewItemV4 opt(option);
	initStyleOption(&opt, index);


	QStyle* sty = QApplication::style();

	// Draw the background: (this will handle selection for us. You can also probe selection directly with opt.state & QStyle::State_Selected)
	sty->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter);


	int textStartingPoint = horizontalMargin();
	// Do we have a pixmap available?
	QRect pmap = opt.rect;
	pmap.setLeft( pmap.left() + horizontalMargin() );
	pmap.setTop( pmap.top() + verticalMargin() );
	pmap.setBottom( pmap.bottom() - verticalMargin() );

	if(opt.features & QStyleOptionViewItemV2::HasDecoration) {
		QPixmap p = opt.icon.pixmap(pmap.height());
		sty->drawItemPixmap(painter, pmap, Qt::AlignLeft | Qt::AlignVCenter, p);
		textStartingPoint += horizontalMargin() + p.width() + horizontalSpacing();
	}

	QRect textRect = opt.rect;
	textRect.setLeft( textRect.left() + textStartingPoint);


	if (index.data(AM::ModifiedRole).toBool()) {
		fontItalic_.setPointSize(size1_);
		painter->setFont(fontItalic_);
	}
	else {
		font_.setPointSize(size1_);
		painter->setFont(font_);
	}
	painter->setPen(color1_);

	painter->drawText(textRect, index.data(Qt::DisplayRole).toString());

	QVariant description = index.data(AM::DescriptionRole);
	if(!description.isNull()){
		font_.setPointSize(size2_);
		painter->setFont(font_);
		painter->setPen(color2_);
		QFontMetrics fontMetrics(font_);

		painter->drawText(textRect.translated(QPoint(0,20)), fontMetrics.elidedText(description.toString(), Qt::ElideRight, textRect.width() ));

	}

	/// call base class to draw close button:
	drawCloseButton(painter, opt, index);


	/* What info is available:
enum OptionType
enum Position
enum StyleOptionType
enum StyleOptionVersion
enum ViewItemFeature
flags ViewItemFeatures
enum ViewItemPosition
QStyleOptionViewItemV4 ()
QStyleOptionViewItemV4 ( const QStyleOptionViewItemV4 & )
QStyleOptionViewItemV4 ( const QStyleOptionViewItem & )
backgroundBrush : QBrush
checkState : Qt::CheckState
decorationAlignment : Qt::Alignment
decorationPosition : Position
decorationSize : QSize
direction : Qt::LayoutDirection
displayAlignment : Qt::Alignment
features : ViewItemFeatures
font : QFont
fontMetrics : QFontMetrics
icon : QIcon
index : QModelIndex
initFrom ( const QWidget * )
locale : QLocale
palette : QPalette
rect : QRect
showDecorationSelected : bool
state : QStyle::State
text : QString
textElideMode : Qt::TextElideMode
type : int
version : int
viewItemPosition : ViewItemPosition
widget : const QWidget *
operator= ( const QStyleOptionViewItem & ) : QStyleOption
*/
}
/** Redefined. */
void PlaylistItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &index) const
{
	QStyleOptionViewItem o(opt);
	initStyleOption(&o, index);

	QStyle *style = QApplication::style();
	QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &o, o.widget);

	if (_playlist->horizontalHeader()->visualIndex(index.column()) == 0) {
		textRect = textRect.adjusted(1, 0, 0, 0);
		o.rect = textRect;
	}

	MiamStyledItemDelegate::paint(p, o, index);

	// Highlight the current playing item
	QFont font = SettingsPrivate::instance()->font(SettingsPrivate::FF_Playlist);
	if (_playlist->mediaPlaylist()->currentIndex() == index.row() && _playlist->mediaPlayer()->state() != QMediaPlayer::StoppedState) {
		font.setBold(true);
		font.setItalic(true);
	}
	p->setFont(font);

	p->save();
	if (o.state.testFlag(QStyle::State_Selected)) {
		if ((opt.palette.highlight().color().lighter(160).saturation() - opt.palette.highlightedText().color().saturation()) < 128) {
			p->setPen(opt.palette.text().color());
		} else {
			p->setPen(opt.palette.highlightedText().color());
		}
	}

	QString text;
	switch (index.column()) {

	case Playlist::COL_LENGTH:
		if (index.data().toInt() >= 0) {
			text = QDateTime::fromTime_t(index.data().toInt()).toString("m:ss");
			text = p->fontMetrics().elidedText(text, o.textElideMode, textRect.width());
			style->drawItemText(p, textRect, Qt::AlignCenter, o.palette, true, text);
		}
		break;
	case Playlist::COL_TRACK_NUMBER:
	case Playlist::COL_YEAR:
		text = p->fontMetrics().elidedText(index.data().toString(), o.textElideMode, textRect.width());
		style->drawItemText(p, textRect, Qt::AlignCenter, o.palette, true, text);
		break;
	case Playlist::COL_RATINGS:
		if (index.data().canConvert<StarRating>() || opt.state.testFlag(QStyle::State_Selected)) {
			StarRating r = index.data().value<StarRating>();
			r.paintStars(p, opt);
		}
		break;
	case Playlist::COL_ICON:{
		//QRect iconRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &o, o.widget);
		text = p->fontMetrics().elidedText(index.data().toString(), o.textElideMode, textRect.width());
		QSize iconSize(textRect.height() * 0.8, textRect.height() * 0.8);
		/// FIXME
		//style->drawItemText(p, textRect, Qt::AlignCenter, o.palette, true, text);
		style->drawItemPixmap(p, o.rect, Qt::AlignCenter, o.icon.pixmap(iconSize));
		break;
	}
	case Playlist::COL_TITLE:
	case Playlist::COL_ALBUM:
	case Playlist::COL_ARTIST:
	default:
		text = p->fontMetrics().elidedText(index.data().toString(), o.textElideMode, textRect.width());
		if (QApplication::isLeftToRight()) {
			textRect.adjust(2, 0, 0, 0);
		} else {
			textRect.adjust(0, 0, -2, 0);
		}
		style->drawItemText(p, textRect, Qt::AlignLeft | Qt::AlignVCenter, o.palette, true, text);
		break;
	}
	p->restore();
}
void ContactDelegateCompact::paintContact(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    QStyleOptionViewItemV4 optV4 = option;
    initStyleOption(&optV4, index);

    painter->save();

    painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
    painter->setClipRect(optV4.rect);

    QStyle *style = QApplication::style();
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);

    QRect iconRect = optV4.rect;
    iconRect.setSize(QSize(m_avatarSize, m_avatarSize));
    iconRect.moveTo(QPoint(iconRect.x() + m_spacing, iconRect.y() + m_spacing));

    QPixmap avatar;
    avatar.load(index.data(KTp::ContactAvatarPathRole).toString());

    bool noContactAvatar = avatar.isNull();

    if (noContactAvatar) {
        avatar = SmallIcon("im-user", KIconLoader::SizeMedium);
    }

    style->drawItemPixmap(painter, iconRect, Qt::AlignCenter, avatar.scaled(iconRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

    // This value is used to set the correct width for the username and the presence message.
    int rightIconsWidth = m_presenceIconSize + m_spacing;

    QPixmap icon = KIcon(index.data(KTp::ContactPresenceIconRole).toString()).pixmap(KIconLoader::SizeSmallMedium);

    QRect statusIconRect = optV4.rect;

    statusIconRect.setSize(QSize(m_presenceIconSize, m_presenceIconSize));
    statusIconRect.moveTo(QPoint(optV4.rect.right() - rightIconsWidth,
                                 optV4.rect.top() + (optV4.rect.height() - m_presenceIconSize) / 2));

    painter->drawPixmap(statusIconRect, icon);

    // Right now we only check for 'phone', as that's the most interesting type.
    if (index.data(KTp::ContactClientTypesRole).toStringList().contains(QLatin1String("phone"))) {
        // Additional space is needed for the icons, don't add too much spacing between the two icons
        rightIconsWidth += m_clientTypeIconSize + (m_spacing / 2);

        QPixmap phone = QIcon::fromTheme("phone").pixmap(m_clientTypeIconSize);
        QRect phoneIconRect = optV4.rect;
        phoneIconRect.setSize(QSize(m_clientTypeIconSize, m_clientTypeIconSize));
        phoneIconRect.moveTo(QPoint(optV4.rect.right() - rightIconsWidth,
                                    optV4.rect.top() + (optV4.rect.height() - m_clientTypeIconSize) / 2));
        painter->drawPixmap(phoneIconRect, phone);
    }

    QFont nameFont;

    if (m_listSize == ContactDelegateCompact::Mini) {
        nameFont = KGlobalSettings::smallestReadableFont();
    } else {
        nameFont = KGlobalSettings::generalFont();
    }

    const QFontMetrics nameFontMetrics(nameFont);

    if (option.state & QStyle::State_Selected) {
        painter->setPen(option.palette.color(QPalette::Active, QPalette::HighlightedText));
    } else {
        painter->setPen(option.palette.color(QPalette::Active, QPalette::Text));
    }

    painter->setFont(nameFont);

    QRect userNameRect = optV4.rect;
    userNameRect.setX(iconRect.x() + iconRect.width() + m_spacing * 2);
    userNameRect.setY(userNameRect.y() + (userNameRect.height()/2 - nameFontMetrics.height()/2));
    userNameRect.setWidth(userNameRect.width() - rightIconsWidth);

    painter->drawText(userNameRect,
                      nameFontMetrics.elidedText(optV4.text, Qt::ElideRight, userNameRect.width()));

    QRect presenceMessageRect = optV4.rect;
    presenceMessageRect.setX(userNameRect.x() + nameFontMetrics.boundingRect(optV4.text).width() + m_spacing * 2);
    presenceMessageRect.setWidth(optV4.rect.width() - presenceMessageRect.x() - rightIconsWidth);
    presenceMessageRect.setY(presenceMessageRect.y() + (presenceMessageRect.height()/2 - nameFontMetrics.height()/2));

    if (option.state & QStyle::State_Selected) {
        painter->setPen(option.palette.color(QPalette::Disabled, QPalette::HighlightedText));
    } else {
        painter->setPen(option.palette.color(QPalette::Disabled, QPalette::Text));
    }

    painter->drawText(presenceMessageRect,
                      nameFontMetrics.elidedText(index.data(KTp::ContactPresenceMessageRole).toString().trimmed(),
                                                 Qt::ElideRight, presenceMessageRect.width()));

    painter->restore();
}
Esempio n. 11
0
	void PlaylistDelegate::PaintTrack (QPainter *painter,
			const QStyleOptionViewItem& optionOld, const QModelIndex& index) const
	{
		QStyleOptionViewItemV4 option = optionOld;
		const auto& info = index.data (Player::Role::Info).value<MediaInfo> ();

		QStyle *style = option.widget ?
				option.widget->style () :
				QApplication::style ();

		const bool isSubAlbum = index.parent ().isValid ();

		QStyleOptionViewItemV4 bgOpt = option;

		painter->save ();

		if (index.data (Player::Role::IsCurrent).toBool ())
		{
			const QColor& highlight = bgOpt.palette.color (QPalette::Highlight);
			QLinearGradient grad (0, 0, 0, bgOpt.rect.height ());
			grad.setColorAt (0, highlight.lighter (100));
			grad.setColorAt (1, highlight.darker (200));
			bgOpt.backgroundBrush = QBrush (grad);

			QFont font = option.font;
			font.setItalic (true);
			painter->setFont (font);
			painter->setPen (bgOpt.palette.color (QPalette::HighlightedText));
		}

		style->drawPrimitive (QStyle::PE_PanelItemViewItem, &bgOpt, painter, option.widget);
		QString lengthText = Util::MakeTimeFromLong (info.Length_);
		if (lengthText.startsWith ("00:"))
			lengthText = lengthText.mid (3);

		if (index.data (Player::Role::IsStop).toBool ())
		{
			const auto& icon = Core::Instance ().GetProxy ()->GetIcon ("media-playback-stop");
			const auto& px = icon.pixmap (option.rect.size ());
			style->drawItemPixmap (painter, option.rect, Qt::AlignLeft | Qt::AlignVCenter, px);

			option.rect.adjust (px.width () + Padding, 0, 0, 0);
		}

		const int width = option.fontMetrics.width (lengthText);
		style->drawItemText (painter, option.rect,
				Qt::AlignRight, option.palette, true, lengthText);

		const auto& itemTextRect = option.rect.adjusted (0, 0, -width, 0);
		QString itemStr;
		if (!isSubAlbum)
			itemStr = index.data ().toString ();
		else
		{
			if (info.TrackNumber_ > 0 && !info.Title_.isEmpty ())
				itemStr = QString::fromUtf8 ("%1 — %2").arg (info.TrackNumber_).arg (info.Title_);
			else if (!info.Title_.isEmpty ())
				itemStr = info.Title_;
			else
				itemStr = QFileInfo (info.LocalPath_).fileName ();
		}
		itemStr = option.fontMetrics.elidedText (itemStr, Qt::ElideRight, itemTextRect.width ());

		style->drawItemText (painter, itemTextRect, 0, option.palette, true, itemStr);
		painter->restore ();
	}
Esempio n. 12
0
void PluginItemDelegate::paint( QPainter *painter,
                                const QStyleOptionViewItem& option,
                                const QModelIndex& index ) const
{
    Q_ASSERT( index.isValid() );
    QRect rect = option.rect;
    QStyle *style = QApplication::style();

    painter->save();

    // Drawing the background
    QStyleOption background = option;
    style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );

    painter->translate( rect.topLeft() );

    // rect is now represented in item coordinates
    rect.moveTopLeft( QPoint( 0, 0 ) );
    // The point at the top left of the available drawing area.
    QPoint topLeft( 0, 0 );
    // The point at the top right of the available drawing area.
    QPoint topRight( rect.topRight() );

    QRect nameRect = rect;
    
    // Painting the checkbox
    QStyleOptionButton checkBox = checkboxOption( option, index, topLeft.x(), Qt::AlignLeft );
    painter->save();
    style->drawControl( QStyle::CE_CheckBox, &checkBox, painter );
    painter->restore();

    nameRect.setLeft( checkBox.rect.right() + 1 );
    
    // Painting the About Button
    QStyleOptionButton button = buttonOption( option, index, PluginItemDelegate::About,
                                              topRight.x(), Qt::AlignRight );
    style->drawControl( QStyle::CE_PushButton, &button, painter );
    topRight -= QPoint( button.rect.width(), 0 );

    // Painting the Configure Button
    if ( index.data( RenderPluginModel::ConfigurationDialogAvailable ).toBool() ) {
        QStyleOptionButton button = buttonOption( option, index, PluginItemDelegate::Configure,
                                                  topRight.x(), Qt::AlignRight );
        style->drawControl( QStyle::CE_PushButton, &button, painter );
        topRight -= QPoint( button.rect.width(), 0 );
        
        nameRect.setRight( button.rect.left() -1 );
    }

    // Painting the Icon
    const QIcon icon = index.data( Qt::DecorationRole ).value<QIcon>();
    const QPixmap iconPixmap = icon.pixmap(16, 16);

    nameRect.moveBottom( nameRect.bottom()+5 );
    style->drawItemPixmap( painter,
                           nameRect,
                           Qt::AlignLeft,
                           iconPixmap );

    nameRect.setLeft( nameRect.left() + 16 + 5 );
    nameRect.moveBottom( nameRect.bottom()-5 );

    // Painting the Name string
    QString name = index.data( Qt::DisplayRole ).toString();
    
    style->drawItemText( painter,
                         nameRect,
                         Qt::AlignLeft | Qt::AlignVCenter,
                         option.palette,
                         true,
                         name );

    painter->restore();
}
void AMSamplePlatePre2013ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {

	QString sampleName(index.data(Qt::DisplayRole).toString());
	QString elementString(index.data(AM::UserRole).toString());
	if(!elementString.isEmpty()) {
		elementString.prepend("(");
		elementString.append(")");
	}
	QString createdString(index.data(AM::DescriptionRole).toString());
	QString positionsString(index.data(AM::UserRole+1).toString());
	if(positionsString.isEmpty()) {
		positionsString = "[Unknown sample position]";
	}

	QStyleOptionViewItemV4 opt(option);
	initStyleOption(&opt, index);


	QStyle* sty = QApplication::style();

	// Draw the background: (this will handle selection for us. You can also probe selection directly with opt.state & QStyle::State_Selected)
	sty->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter);


	int textStartingPoint = 0;
	// Do we have a pixmap available?
	QRect iconRect = opt.rect;
	iconRect.adjust(6, 0, 0, 0);

	if(opt.features & QStyleOptionViewItemV4::HasDecoration) {
		QPixmap p = opt.icon.pixmap(opt.decorationSize);
		sty->drawItemPixmap(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter, p);
		textStartingPoint += 6 + p.width() + 6;
	}

	// textRect starts 6px from the edge of the decoration, and is inset 6px on top and bottom and right. It's the full room we have available for text.
	QRect textRect = opt.rect;
	textRect.adjust( textStartingPoint, 6, -6, -6);



	QFont font = opt.font;
	font.setPointSize(font.pointSize()+4);
	painter->setFont(font);
	painter->setPen(opt.palette.text().color());
	QRect actualNameTextRect;
	painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, sampleName, &actualNameTextRect);

	// Move over by the amount that we filled with text
	QRect elementTextRect(actualNameTextRect.right() + 6,
						  textRect.top(),
						  textRect.width()-actualNameTextRect.width() - 6,
						  actualNameTextRect.height());
	font.setPointSize(font.pointSize()-4);
	font.setItalic(true);
	font.setBold(true);
	painter->setFont(font);
	painter->setPen(Qt::darkGray);
	QFontMetrics fm(font);
	QString elidedElements = fm.elidedText(elementString, Qt::ElideRight, elementTextRect.width());
	painter->drawText(elementTextRect, Qt::AlignLeft | Qt::AlignBottom, elidedElements);

	QRect descriptionTextRect(textRect.left(), actualNameTextRect.bottom(), textRect.width(), textRect.height()-actualNameTextRect.height());
	font.setPointSize(font.pointSize()-2);
	font.setItalic(false);
	font.setBold(false);
	painter->setFont(font);
	painter->setPen(Qt::darkGray);
	painter->drawText(descriptionTextRect, Qt::AlignLeft | Qt::AlignTop, createdString);


	QFontMetrics fm2(font);
	QRect actualPositionsTextRect;
	painter->drawText(textRect, Qt::AlignLeft | Qt::AlignBottom, fm2.elidedText(positionsString, Qt::ElideMiddle, textRect.width()), &actualPositionsTextRect);

	int dividerLineY = actualPositionsTextRect.top() - 4;
	painter->drawLine(QPoint(textRect.left(), dividerLineY), QPoint(textRect.right(), dividerLineY));
}