#include "mediaitemdelegate.h"
mediaItemDelegate
::mediaItemDelegate(QObject *parent
):{
}
{
if(index.isValid()){
const int left = option.rect.left();
const int top = option.rect.top();
const int width = option.rect.width();
QFont titlefont
(option.
font);
titlefont.setBold(true);
titlefont.setPointSize(titlefont.pointSize());
int height = (2 * titlefont.pixelSize()) + 10;
int padding = 3;
QColor foregroundColor
= (option.
state.
testFlag(QStyle::State_Selected))?
option.
palette.
color(QPalette::HighlightedText):option.
palette.
color(QPalette::Text);
//base pixmap
pixmap.fill(Qt::transparent);
p.translate(-option.rect.topLeft());
//icon time
int iconwidth = height - 6;
icon.
addFile(QString::fromUtf8(":/icons/icons/hi48-action-filename-filetype-amarok.png"),
QSize(iconwidth,iconwidth
),
QIcon::Normal,
QIcon::On);
if(!icon.isNull()){
icon.
paint(&p, left
+ padding, top
+ padding, iconwidth, iconwidth, Qt
::AlignCenter,
QIcon::Normal);
}
//now for text
QString artist, album, title,duration;
QMap<int, QVariant> map(index.model()->itemData(index));
title.append(map.value(mediaItem::TitleRole).toString());
artist.append(map.value(mediaItem::ArtistRole).toString());
album.append(map.value(mediaItem::AlbumRole).toString());
duration.append(map.value(mediaItem::DurationRole).toString());
font.setBold(true);
int lefttext = iconwidth + 2 * padding;
int topoff = (height - iconwidth) / 2;
QRect titlerect
(left
+ lefttext, top
+ padding,
(width
/ 2) - padding
- lefttext, topoff
);
QRect artistrect
(left
+ lefttext, top
+ (2*padding
) + topoff
+ 1,
(width
/ 2) - padding
- lefttext, topoff
);
QRect albumrect
(left
+ lefttext
+ titlerect.
width() + padding, top
+ (2*padding
) + topoff
+ 1,
(width
/ 2) - padding
- lefttext, topoff
);
QRect durrect
(left
+ lefttext
+ titlerect.
width() + padding, top
+ padding,
(width
/ 2) - padding
- lefttext, topoff
);
font.setPixelSize(titlerect.height()/7);
p.setFont(font);
p.setPen(foregroundColor);
p.
drawText(QRectF(titlerect
), title, txtopt
);
p.
drawText(QRectF(artistrect
), artist, txtopt
);
txtopt.setAlignment(Qt::AlignRight | Qt::AlignBottom);
p.
drawText(QRectF(albumrect
), album, txtopt
);
p.
drawText(QRectF(durrect
), duration, txtopt
);
p.end();
//hey presto
painter->drawPixmap(option.rect.topLeft(), pixmap);
/*
QPixmap reflect = Utilities::reflection(pixmap);
painter->drawPixmap(option.rect.topleft() + QPoint(0, size.width() + 1), reflect);
*/
}
}
{
if(!index.isValid()){
}
int width = 0;
QFont titlefont
(option.
font);
titlefont.setBold(true);
titlefont.setPointSize(titlefont.pointSize());
int height = (2 * titlefont.pixelSize()) + 10;
return QSize(width, height
);
}
#include "mediaitemdelegate.h"
mediaItemDelegate::mediaItemDelegate(QObject *parent):
QItemDelegate(parent)
{
}
void mediaItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.isValid()){
const int left = option.rect.left();
const int top = option.rect.top();
const int width = option.rect.width();
QFont titlefont(option.font);
titlefont.setBold(true);
titlefont.setPointSize(titlefont.pointSize());
int height = (2 * titlefont.pixelSize()) + 10;
int padding = 3;
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
//base pixmap
QPixmap pixmap(width, height);
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.translate(-option.rect.topLeft());
//icon time
QIcon icon;
int iconwidth = height - 6;
icon.addFile(QString::fromUtf8(":/icons/icons/hi48-action-filename-filetype-amarok.png"), QSize(iconwidth,iconwidth),QIcon::Normal, QIcon::On);
if(!icon.isNull()){
icon.paint(&p, left + padding, top + padding, iconwidth, iconwidth, Qt::AlignCenter, QIcon::Normal);
}
//now for text
QString artist, album, title,duration;
QMap<int, QVariant> map(index.model()->itemData(index));
title.append(map.value(mediaItem::TitleRole).toString());
artist.append(map.value(mediaItem::ArtistRole).toString());
album.append(map.value(mediaItem::AlbumRole).toString());
duration.append(map.value(mediaItem::DurationRole).toString());
QFont font(option.font);
font.setBold(true);
int lefttext = iconwidth + 2 * padding;
int topoff = (height - iconwidth) / 2;
QTextOption txtopt(Qt::AlignLeft | Qt::AlignBottom);
txtopt.setWrapMode(QTextOption::WordWrap);
QRect titlerect(left + lefttext, top + padding, (width / 2) - padding - lefttext, topoff);
QRect artistrect(left + lefttext, top + (2*padding) + topoff + 1, (width / 2) - padding - lefttext, topoff);
QRect albumrect(left + lefttext + titlerect.width() + padding, top + (2*padding) + topoff + 1, (width / 2) - padding - lefttext, topoff);
QRect durrect(left + lefttext + titlerect.width() + padding, top + padding, (width / 2) - padding - lefttext, topoff);
font.setPixelSize(titlerect.height()/7);
p.setFont(font);
p.setPen(foregroundColor);
p.drawText(QRectF(titlerect), title, txtopt);
p.drawText(QRectF(artistrect), artist, txtopt);
txtopt.setAlignment(Qt::AlignRight | Qt::AlignBottom);
p.drawText(QRectF(albumrect), album, txtopt);
p.drawText(QRectF(durrect), duration, txtopt);
p.end();
//hey presto
painter->drawPixmap(option.rect.topLeft(), pixmap);
/*
QPixmap reflect = Utilities::reflection(pixmap);
painter->drawPixmap(option.rect.topleft() + QPoint(0, size.width() + 1), reflect);
*/
}
}
QSize mediaItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid()){
return QSize(0, 0);
}
int width = 0;
QFont titlefont(option.font);
titlefont.setBold(true);
titlefont.setPointSize(titlefont.pointSize());
int height = (2 * titlefont.pixelSize()) + 10;
return QSize(width, height);
}
To copy to clipboard, switch view to plain text mode
Bookmarks