• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

itemdelegate.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003     Copyright 2007 Kevin Ottens <ervin@kde.org>
00004     Copyright 2007 Alexis Menard <darktears31@gmail.com>
00005 
00006   
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "itemdelegate.h"
00024 
00025 // Qt
00026 #include <QApplication>
00027 #include <QFontMetrics>
00028 #include <QIcon>
00029 #include <QModelIndex>
00030 #include <QPainter>
00031 #include <QStyleOptionViewItem>
00032 
00033 // KDE
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         // use a slightly translucent version of the palette's highlight color
00086         // for the background
00087         backgroundColor.setAlphaF(0.5);
00088         painter->setBrush(QBrush(backgroundColor));
00089         painter->drawPath(roundedRectangle(textAreaRect, 5));
00090         painter->restore();
00091     }
00092 
00093     // draw icon
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     // draw title
00110     painter->setFont(titleFont);
00111 
00112     textAreaRect.setHeight(textAreaRect.height()/2);
00113 
00114     painter->drawText(textAreaRect, Qt::AlignLeft|Qt::AlignVCenter, titleText);
00115 
00116     // draw sub-title
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 // Taken from kdelibs/kio/kio/kfileitemdelegate.cpp
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());         // Top left corner
00164     path.lineTo(rect.right() - radius, rect.top());                                 // Top side
00165     path.quadTo(rect.right(), rect.top(), rect.right(), rect.top() + radius);       // Top right corner
00166     path.lineTo(rect.right(), rect.bottom() - radius);                              // Right side
00167     path.quadTo(rect.right(), rect.bottom(), rect.right() - radius, rect.bottom()); // Bottom right corner
00168     path.lineTo(rect.left() + radius, rect.bottom());                               // Bottom side
00169     path.quadTo(rect.left(), rect.bottom(), rect.left(), rect.bottom() - radius);   // Bottom left corner
00170     path.closeSubpath();
00171 
00172     return path;
00173 }

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal