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 <QApplication>
00013 #include <QPen>
00014 #include <QPainter>
00015
00016 #include <KGlobalSettings>
00017 #include <KLocalizedString>
00018
00019 BackgroundDelegate::BackgroundDelegate(QObject *listener, float ratio, QObject *parent)
00020 : QAbstractItemDelegate(parent),
00021 m_listener(listener),
00022 m_ratio(ratio)
00023 {
00024 }
00025
00026 void BackgroundDelegate::paint(QPainter *painter,
00027 const QStyleOptionViewItem &option,
00028 const QModelIndex &index) const
00029 {
00030 QString title = index.model()->data(index, Qt::DisplayRole).toString();
00031 QString author = index.model()->data(index, AuthorRole).toString();
00032 QString resolution = index.model()->data(index, ResolutionRole).toString();
00033 QPixmap pix = index.model()->data(index, ScreenshotRole).value<QPixmap>();
00034
00035
00036 QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter);
00037
00038
00039 int maxheight = SCREENSHOT_SIZE;
00040 int maxwidth = int(maxheight * m_ratio);
00041 if (!pix.isNull()) {
00042 QSize sz = pix.size();
00043 int x = MARGIN + (maxwidth - pix.width()) / 2;
00044 int y = MARGIN + (maxheight - pix.height()) / 2;
00045 QRect imgRect = QRect(option.rect.topLeft(), pix.size()).translated(x, y);
00046 painter->drawPixmap(imgRect, pix);
00047 }
00048
00049
00050 painter->save();
00051 QFont font = painter->font();
00052 font.setWeight(QFont::Bold);
00053 painter->setFont(font);
00054 if (option.state & QStyle::State_Selected) {
00055 painter->setPen(QApplication::palette().brush(QPalette::HighlightedText).color());
00056 }else{
00057 painter->setPen(QApplication::palette().brush(QPalette::Text).color());
00058 }
00059 int x = option.rect.left() + MARGIN * 2 + maxwidth;
00060
00061 QRect textRect(x,
00062 option.rect.top() + MARGIN,
00063 option.rect.width() - x - MARGIN,
00064 maxheight);
00065 QString text = title;
00066 QString authorCaption;
00067 if (!author.isEmpty()) {
00068 authorCaption = i18nc("Caption to wallpaper preview, %1 author name",
00069 "by %1", author);
00070 text += '\n' + authorCaption;
00071 }
00072
00073 QRect boundingRect = painter->boundingRect(
00074 textRect, Qt::AlignVCenter | Qt::TextWordWrap, text) & option.rect;
00075 painter->drawText(boundingRect, Qt::TextWordWrap, title);
00076 QRect titleRect = painter->boundingRect(boundingRect, Qt::TextWordWrap, title);
00077 QPoint lastText(titleRect.bottomLeft());
00078
00079 if (!author.isEmpty()) {
00080 QRect authorRect = QRect(lastText, textRect.size()) & option.rect;
00081
00082 if (!authorRect.isEmpty()) {
00083 painter->setFont(KGlobalSettings::smallestReadableFont());
00084 painter->drawText(authorRect, Qt::TextWordWrap, authorCaption);
00085 lastText = painter->boundingRect(authorRect, Qt::TextWordWrap, authorCaption).bottomLeft();
00086 }
00087 }
00088
00089 if (!resolution.isEmpty()) {
00090 QRect resolutionRect = QRect(lastText, textRect.size()) & option.rect;
00091
00092 if (!resolutionRect.isEmpty()) {
00093 painter->setFont(KGlobalSettings::smallestReadableFont());
00094 painter->drawText(resolutionRect, Qt::TextWordWrap, resolution);
00095 }
00096 }
00097
00098 painter->restore();
00099 }
00100
00101 QSize BackgroundDelegate::sizeHint(const QStyleOptionViewItem &option,
00102 const QModelIndex &index) const
00103 {
00104 const QString title = index.model()->data(index, Qt::DisplayRole).toString();
00105 const int maxwidth = int(SCREENSHOT_SIZE * m_ratio);
00106 QFont font = option.font;
00107 font.setWeight(QFont::Bold);
00108 QFontMetrics fm(font);
00109
00110 return QSize(maxwidth + qBound(100, fm.width(title), 500), SCREENSHOT_SIZE + MARGIN * 2);
00111 }
00112