Plasma
backgrounddelegate.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "backgrounddelegate.h"
00011
00012 #include <KGlobalSettings>
00013 #include <QPen>
00014 #include <QPainter>
00015 #include "backgroundpackage.h"
00016
00017 BackgroundDelegate::BackgroundDelegate(QObject *listener,
00018 float ratio, QObject *parent)
00019 : QAbstractItemDelegate(parent)
00020 , m_listener(listener)
00021 , m_ratio(ratio)
00022 {
00023 }
00024
00025 void BackgroundDelegate::paint(QPainter *painter,
00026 const QStyleOptionViewItem &option,
00027 const QModelIndex &index) const
00028 {
00029 QString title = index.model()->data(index, Qt::DisplayRole).toString();
00030 QString author = index.model()->data(index, AuthorRole).toString();
00031 QPixmap pix = index.model()->data(index, ScreenshotRole).value<QPixmap>();
00032
00033
00034 if (option.state & QStyle::State_Selected) {
00035 QPen oldPen = painter->pen();
00036 painter->setPen(option.palette.color(QPalette::Highlight));
00037 painter->drawRect(option.rect.adjusted(2, 2, -2, -2));
00038 painter->setPen(oldPen);
00039 }
00040
00041
00042 int maxheight = Background::SCREENSHOT_HEIGHT;
00043 int maxwidth = int(maxheight * m_ratio);
00044 if (!pix.isNull()) {
00045 QSize sz = pix.size();
00046 int x = MARGIN + (maxwidth - pix.width()) / 2;
00047 int y = MARGIN + (maxheight - pix.height()) / 2;
00048 QRect imgRect = QRect(option.rect.topLeft(), pix.size()).translated(x, y);
00049 painter->drawPixmap(imgRect, pix);
00050 }
00051
00052
00053 painter->save();
00054 QFont font = painter->font();
00055 font.setWeight(QFont::Bold);
00056 painter->setFont(font);
00057 int x = option.rect.left() + MARGIN * 5 + maxwidth;
00058
00059 QRect textRect(x,
00060 option.rect.top() + MARGIN,
00061 option.rect.width() - x - MARGIN * 2,
00062 maxheight);
00063 QString text = title;
00064 QString authorCaption;
00065 if (!author.isEmpty()) {
00066 authorCaption = i18nc("Caption to wallpaper preview, %1 author name",
00067 "by %1", author);
00068 text += '\n' + authorCaption;
00069 }
00070 QRect boundingRect = painter->boundingRect(
00071 textRect, Qt::AlignVCenter | Qt::TextWordWrap, text);
00072 painter->drawText(boundingRect, Qt::TextWordWrap, title);
00073 if (!author.isEmpty()) {
00074 QRect titleRect = painter->boundingRect(boundingRect, Qt::TextWordWrap, title);
00075 QRect authorRect(titleRect.bottomLeft(), textRect.size());
00076 painter->setFont(KGlobalSettings::smallestReadableFont());
00077 painter->drawText(authorRect, Qt::TextWordWrap, authorCaption);
00078 }
00079
00080 painter->restore();
00081 }
00082
00083 QSize BackgroundDelegate::sizeHint(const QStyleOptionViewItem &,
00084 const QModelIndex &) const
00085 {
00086 return QSize(100, Background::SCREENSHOT_HEIGHT + MARGIN * 2);
00087 }
00088