00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "itemdelegate.h"
00024
00025
00026 #include <QApplication>
00027 #include <QFontMetrics>
00028 #include <QIcon>
00029 #include <QModelIndex>
00030 #include <QPainter>
00031 #include <QStyleOptionViewItem>
00032
00033
00034 #include <KColorUtils>
00035 #include <KDebug>
00036 #include <KGlobal>
00037 #include <KGlobalSettings>
00038
00039
00040 using namespace Notifier;
00041
00042 ItemDelegate::ItemDelegate()
00043 {
00044 }
00045
00046 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
00047 {
00048 const bool hover = option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus);
00049 QRect contentRect = option.rect;
00050
00051 QRect decorationRect = QStyle::alignedRect(option.direction,
00052 option.decorationPosition == QStyleOptionViewItem::Left ? Qt::AlignLeft : Qt::AlignRight,
00053 option.decorationSize,
00054 contentRect);
00055 QSize textSize(option.rect.width() - decorationRect.width() - ICON_TEXT_MARGIN,
00056 option.rect.height() - 2);
00057
00058 Qt::Alignment textAlignment = option.decorationAlignment & Qt::AlignRight ? Qt::AlignLeft : Qt::AlignRight;
00059
00060 QRect textRect = QStyle::alignedRect(option.direction,textAlignment,textSize,contentRect.adjusted(0, 2, 0, 0));
00061 QString titleText = index.data(Qt::DisplayRole).value<QString>();
00062 QString subTitleText = index.data(ActionRole).value<QString>();
00063
00064 if (subTitleText.isEmpty()) {
00065 subTitleText = " ";
00066 }
00067
00068 QFont subTitleFont = fontForSubTitle(option.font);
00069
00070 QFont titleFont(option.font);
00071
00072 QFontMetrics titleMetrics(titleFont);
00073 QFontMetrics subTitleMetrics(subTitleFont);
00074 QRect textAreaRect = contentRect;
00075 qreal actualTextWidth = qMax(titleMetrics.width(titleText), subTitleMetrics.width(subTitleText));
00076 textAreaRect.adjust(decorationRect.width() + ICON_TEXT_MARGIN - 3,
00077 0,
00078 (int)(-(textRect.width() - actualTextWidth) + 3),
00079 1);
00080
00081 if (hover) {
00082 painter->save();
00083 painter->setPen(Qt::NoPen);
00084 QColor backgroundColor = option.palette.color(QPalette::Highlight);
00085
00086
00087 backgroundColor.setAlphaF(0.5);
00088 painter->setBrush(QBrush(backgroundColor));
00089 painter->drawPath(roundedRectangle(textAreaRect, 5));
00090 painter->restore();
00091 }
00092
00093
00094 QIcon decorationIcon = index.data(Qt::DecorationRole).value<QIcon>();
00095
00096 if (!hover) {
00097 painter->save();
00098 painter->setOpacity(0.7);
00099 }
00100
00101 decorationIcon.paint(painter, decorationRect, option.decorationAlignment);
00102
00103 if (!hover) {
00104 painter->restore();
00105 }
00106
00107 painter->save();
00108
00109
00110 painter->setFont(titleFont);
00111
00112 textAreaRect.setHeight(textAreaRect.height()/2);
00113
00114 painter->drawText(textAreaRect, Qt::AlignLeft|Qt::AlignVCenter, titleText);
00115
00116
00117 painter->setFont(subTitleFont);
00118
00119 if (!hover) {
00120 painter->setPen(QPen(Qt::gray));
00121 }
00122 textAreaRect.translate(0, textAreaRect.height());
00123
00124 painter->drawText(textAreaRect, Qt::AlignLeft|Qt::AlignVCenter, subTitleText);
00125
00126 painter->restore();
00127
00128 }
00129
00130 QFont ItemDelegate::fontForSubTitle(const QFont& titleFont) const
00131 {
00132 QFont subTitleFont = titleFont;
00133 subTitleFont.setPointSize(qMax(subTitleFont.pointSize() - 2,
00134 KGlobalSettings::smallestReadableFont().pointSize()));
00135 return subTitleFont;
00136 }
00137
00138 QSize ItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
00139 {
00140 Q_UNUSED(index)
00141 QFontMetrics metrics(option.font);
00142
00143 QFont subTitleFont = option.font;
00144 subTitleFont.setPointSize(qMax(subTitleFont.pointSize() - 2,
00145 KGlobalSettings::smallestReadableFont().pointSize()));
00146 QFontMetrics subMetrics(subTitleFont);
00147
00148 int height = qMax(option.decorationSize.height(), metrics.height() + subMetrics.ascent() + 3);
00149
00150 QString titleText = index.data(Qt::DisplayRole).value<QString>();
00151 QString subTitleText = index.data(ActionRole).value<QString>();
00152
00153 int width = qMax(metrics.width(titleText), subMetrics.width(subTitleText));
00154 width+=option.decorationSize.width()+ICON_TEXT_MARGIN;
00155
00156 return QSize(width, height);
00157 }
00158
00159
00160 QPainterPath ItemDelegate::roundedRectangle(const QRectF& rect, qreal radius) const
00161 {
00162 QPainterPath path(QPointF(rect.left(), rect.top() + radius));
00163 path.quadTo(rect.left(), rect.top(), rect.left() + radius, rect.top());
00164 path.lineTo(rect.right() - radius, rect.top());
00165 path.quadTo(rect.right(), rect.top(), rect.right(), rect.top() + radius);
00166 path.lineTo(rect.right(), rect.bottom() - radius);
00167 path.quadTo(rect.right(), rect.bottom(), rect.right() - radius, rect.bottom());
00168 path.lineTo(rect.left() + radius, rect.bottom());
00169 path.quadTo(rect.left(), rect.bottom(), rect.left(), rect.bottom() - radius);
00170 path.closeSubpath();
00171
00172 return path;
00173 }