void PlaylistDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if ((option.state) & (QStyle::State_Selected)) { painter->drawImage(option.rect, QImage("/etc/hildon/theme/images/TouchListBackgroundPressed.png")); } else { painter->drawImage(option.rect, QImage("/etc/hildon/theme/images/TouchListBackgroundNormal.png")); } QImage image = m_cache->image(index.data(m_thumbnailRole).toString(), QSize(64, 64)); if (image.isNull()) { image = QImage("/usr/share/icons/hicolor/64x64/hildon/mediaplayer_default_album.png"); } QRect imageRect = option.rect; imageRect.setLeft(imageRect.left() + 8); imageRect.setWidth(imageRect.height()); drawCenteredImage(painter, imageRect, image); QRect textRect = option.rect; textRect.setLeft(imageRect.right() + 8); textRect.setRight(textRect.right() - 8); textRect.setTop(textRect.top() + 8); textRect.setBottom(textRect.bottom() - 8); QFontMetrics fm = painter->fontMetrics(); QString trackCount = tr("%1 tracks").arg(index.data(m_trackCountRole).toInt()); QString title = fm.elidedText(index.data(m_titleRole).toString(), Qt::ElideRight, textRect.width() - fm.width(trackCount) - 8); painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, title); painter->drawText(textRect, Qt::AlignRight | Qt::AlignTop, trackCount); QFont font; font.setPointSize(13); QString artist = index.data(m_artistRole).toString(); if (artist.isEmpty()) { artist = tr("Unknown artist"); } painter->save(); painter->setFont(font); painter->setPen(QApplication::palette().color(QPalette::Mid)); painter->drawText(textRect, Qt::AlignLeft | Qt::AlignBottom, QFontMetrics(font).elidedText(artist, Qt::ElideRight, textRect.width())); painter->restore(); }
void PlaylistDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyledItemDelegate::paint(painter, option, index); const QImage image = m_cache->image(index.data(m_thumbnailRole).toString(), QSize(80, 60)); QRect imageRect = option.rect; imageRect.setLeft(imageRect.left() + 8); imageRect.setTop(imageRect.top() + 8); imageRect.setWidth(80); imageRect.setHeight(60); painter->fillRect(imageRect, Qt::black); drawCenteredImage(painter, imageRect, image); const int count = index.data(m_videoCountRole).toInt(); if (count != -1) { QFont font; QRect durationRect = imageRect; const int durationHeight = durationRect.height() / 4; font.setPixelSize(durationHeight); durationRect.setTop(durationRect.bottom() - (durationHeight)); painter->save(); painter->setOpacity(0.8); painter->fillRect(durationRect, Qt::black); painter->setFont(font); painter->setOpacity(1); painter->setPen(Qt::white); painter->drawText(durationRect, Qt::AlignCenter, count > 0 ? tr("%1 videos").arg(count) : tr("No videos")); painter->restore(); } painter->save(); painter->setPen(QApplication::palette().color(QPalette::Mid)); painter->drawRect(imageRect); painter->setPen(QApplication::palette().color(QPalette::Text)); QRect textRect = option.rect; textRect.setLeft(imageRect.right() + 8); textRect.setRight(textRect.right() - 8); textRect.setTop(textRect.top() + 8); textRect.setBottom(textRect.bottom() - 8); QString subTitle; const QString username = index.data(m_usernameRole).toString(); const QString date = index.data(m_dateRole).toString(); if (!username.isEmpty()) { subTitle.append(QString("%1 %2").arg(tr("by")).arg(username)); } if (!date.isEmpty()) { if (!username.isEmpty()) { subTitle.append(QString(" %1 ").arg(tr("on"))); } subTitle.append(date); } if (!subTitle.isEmpty()) { painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, painter->fontMetrics().elidedText(index.data(m_titleRole).toString(), Qt::ElideRight, textRect.width())); painter->setPen(QApplication::palette().color(QPalette::Mid)); painter->drawText(textRect, Qt::AlignLeft | Qt::AlignBottom, painter->fontMetrics().elidedText(subTitle, Qt::ElideRight, textRect.width())); } else { painter->drawText(textRect, Qt::AlignVCenter | Qt::TextWordWrap, index.data(m_titleRole).toString()); } painter->restore(); }