void RatingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const { QStyleOptionViewItemV4 opt(option); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); const int left = option.rect.left(); const int top = option.rect.top(); const int width = option.rect.width(); const int height = option.rect.height(); //Create base pixmap QPixmap pixmap(width, height); pixmap.fill(Qt::transparent); QPainter p(&pixmap); p.translate(-option.rect.topLeft()); //Paint rating int rating = index.data(Qt::DisplayRole).toInt(); StarRating starRating = StarRating(rating, StarRating::Medium); starRating.setRating(rating); QSize ratingSize = starRating.sizeHint(); int ratingLeft = left + 2; int ratingTop = top + (height - ratingSize.height())/2; QRect ratingRect = QRect(QPoint(ratingLeft, ratingTop), ratingSize); starRating.setPoint(ratingRect.topLeft()); starRating.paint(&p); p.end(); //Draw finished pixmap painter->drawPixmap(option.rect.topLeft(), pixmap); }
//! [1] QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarRating>()) { StarRating starRating = qvariant_cast<StarRating>(index.data()); return starRating.sizeHint(); } else { return QStyledItemDelegate::sizeHint(option, index); } }
//! [0] void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarRating>()) { StarRating starRating = qvariant_cast<StarRating>(index.data()); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); starRating.paint(painter, option.rect, option.palette, StarRating::ReadOnly); } else { QStyledItemDelegate::paint(painter, option, index); } //! [0] }
bool PlaylistItemDelegate::eventFilter(QObject *object, QEvent *event) { // Cancel input and close editor if (event->type() == QEvent::KeyPress) { QKeyEvent *ke = static_cast<QKeyEvent*>(event); if (ke->key() == Qt::Key_Escape) { auto starEditor = qobject_cast<StarEditor*>(object); StarRating sr = _playlist->model()->data(starEditor->index()).value<StarRating>(); starEditor->starRating.setStarCount(sr.starCount()); closeEditor(starEditor); return true; } } else if (event->type() == QEvent::FocusOut) { // Save value only if different from the moment where the editor was opened auto starEditor = qobject_cast<StarEditor*>(object); StarRating sr = _playlist->model()->data(starEditor->index()).value<StarRating>(); if (starEditor->starRating.starCount() != sr.starCount()) { commitAndClose(); return true; } } return MiamStyledItemDelegate::eventFilter(object, event); }
QSize StarDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(option); StarRating starRating = qVariantValue<StarRating>(index.data()); return starRating.sizeHint(); }
/** 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(); }