akonadi
collectionstatisticsdelegate.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collectionstatisticsdelegate.h"
00021 #include "collectionstatisticsmodel.h"
00022
00023 #include <kcolorscheme.h>
00024 #include <kdebug.h>
00025
00026 #include <QtGui/QPainter>
00027 #include <QtGui/QStyle>
00028 #include <QtGui/QStyleOption>
00029 #include <QtGui/QStyleOptionViewItemV4>
00030 #include <QtGui/QTreeView>
00031
00032 using namespace Akonadi;
00033
00034 namespace Akonadi {
00035
00036 class CollectionStatisticsDelegatePrivate
00037 {
00038 public:
00039 QTreeView *parent;
00040 bool drawUnreadAfterFolder;
00041
00042 CollectionStatisticsDelegatePrivate( QTreeView *treeView )
00043 : parent( treeView ), drawUnreadAfterFolder( false )
00044 {
00045 }
00046 };
00047
00048 }
00049
00050 CollectionStatisticsDelegate::CollectionStatisticsDelegate( QTreeView *parent )
00051 : QStyledItemDelegate( parent ),
00052 d_ptr( new CollectionStatisticsDelegatePrivate( parent ) )
00053 {
00054 }
00055
00056 CollectionStatisticsDelegate::~CollectionStatisticsDelegate()
00057 {
00058 delete d_ptr;
00059 }
00060
00061 void CollectionStatisticsDelegate::setUnreadCountShown( bool enable )
00062 {
00063 Q_D( CollectionStatisticsDelegate );
00064 d->drawUnreadAfterFolder = enable;
00065 }
00066
00067 bool CollectionStatisticsDelegate::unreadCountShown() const
00068 {
00069 Q_D( const CollectionStatisticsDelegate );
00070 return d->drawUnreadAfterFolder;
00071 }
00072
00073 void CollectionStatisticsDelegate::initStyleOption( QStyleOptionViewItem *option,
00074 const QModelIndex &index ) const
00075 {
00076 QStyleOptionViewItemV4 *noTextOption =
00077 qstyleoption_cast<QStyleOptionViewItemV4 *>( option );
00078 QStyledItemDelegate::initStyleOption( noTextOption, index );
00079 noTextOption->text.clear();
00080 }
00081
00082 void CollectionStatisticsDelegate::paint( QPainter *painter,
00083 const QStyleOptionViewItem &option,
00084 const QModelIndex &index ) const
00085 {
00086 Q_D( const CollectionStatisticsDelegate );
00087
00088
00089
00090 QStyledItemDelegate::paint( painter, option, index );
00091
00092
00093
00094 QStyleOptionViewItemV4 option4 = option;
00095 QStyledItemDelegate::initStyleOption( &option4, index );
00096 QString text = option4.text;
00097
00098
00099 QStyle *s = d->parent->style();
00100 const QWidget *widget = option4.widget;
00101 QRect textRect = s->subElementRect( QStyle::SE_ItemViewItemText, &option4, widget );
00102
00103
00104
00105 QModelIndex firstColumn = index.model()->index( index.row(), 0, index.parent() );
00106 bool expanded = d->parent->isExpanded( firstColumn );
00107
00108 if ( option.state & QStyle::State_Selected ) {
00109 painter->save();
00110 painter->setPen( option.palette.highlightedText().color() );
00111 }
00112
00113
00114 if ( d->drawUnreadAfterFolder && index.column() == 0 ) {
00115
00116 QVariant unreadCount = index.model()->data( index,
00117 CollectionStatisticsModel::UnreadRole );
00118 QVariant unreadRecursiveCount = index.model()->data( index,
00119 CollectionStatisticsModel::RecursiveUnreadRole );
00120 Q_ASSERT( unreadCount.type() == QVariant::LongLong );
00121 Q_ASSERT( unreadRecursiveCount.type() == QVariant::LongLong );
00122
00123
00124
00125 QString unread;
00126 QString unreadCountInChilds = QString::number( unreadRecursiveCount.toLongLong() -
00127 unreadCount.toLongLong() );
00128 if ( expanded && unreadCount.toLongLong() > 0 )
00129 unread = QString( QLatin1String(" (%1)") ).arg( unreadCount.toLongLong() );
00130 else if ( !expanded ) {
00131 if ( unreadCount.toLongLong() != unreadRecursiveCount.toLongLong() )
00132 unread = QString( QLatin1String(" (%1 + %2)") ).arg( unreadCount.toString(),
00133 unreadCountInChilds );
00134 else if ( unreadCount.toLongLong() > 0 )
00135 unread = QString( QLatin1String(" (%1)") ).arg( unreadCount.toString() );
00136 }
00137
00138 painter->save();
00139
00140 if ( !unread.isEmpty() ) {
00141 QFont font = painter->font();
00142 font.setBold( true );
00143 painter->setFont( font );
00144 }
00145
00146
00147
00148 QString folderName = text;
00149 QFontMetrics fm( painter->fontMetrics() );
00150 int unreadWidth = fm.width( unread );
00151 if ( fm.width( folderName ) + unreadWidth > textRect.width() ) {
00152 folderName = fm.elidedText( folderName, Qt::ElideRight,
00153 textRect.width() - unreadWidth );
00154 }
00155 int folderWidth = fm.width( folderName );
00156 QRect folderRect = textRect;
00157 QRect unreadRect = textRect;
00158 folderRect.setRight( textRect.left() + folderWidth );
00159 unreadRect.setLeft( folderRect.right() );
00160
00161
00162 painter->drawText( folderRect, Qt::AlignLeft, folderName );
00163 KColorScheme::ColorSet cs = ( option.state & QStyle::State_Selected ) ?
00164 KColorScheme::Selection : KColorScheme::View;
00165 QColor unreadColor = KColorScheme( QPalette::Active, cs ).
00166 foreground( KColorScheme::LinkText ).color();
00167 painter->setPen( unreadColor );
00168 painter->drawText( unreadRect, Qt::AlignLeft, unread );
00169 painter->restore();
00170
00171 if ( option.state & QStyle::State_Selected ) {
00172 painter->restore();
00173 }
00174 return;
00175 }
00176
00177
00178
00179 if ( ( index.column() == 1 || index.column() == 2 ) ) {
00180
00181 painter->save();
00182
00183 int role = 0;
00184 if ( index.column() == 1 ) {
00185 if ( !expanded )
00186 role = CollectionStatisticsModel::RecursiveUnreadRole;
00187 else
00188 role = CollectionStatisticsModel::UnreadRole;
00189 }
00190 else if ( index.column() == 2 ) {
00191 if ( !expanded )
00192 role = CollectionStatisticsModel::RecursiveTotalRole;
00193 else
00194 role = CollectionStatisticsModel::TotalRole;
00195 }
00196
00197 QVariant sum = index.model()->data( index, role );
00198 Q_ASSERT( sum.type() == QVariant::LongLong );
00199 QStyleOptionViewItem opt = option;
00200 if ( index.column() == 1 && sum.toLongLong() > 0 ) {
00201 QFont font = painter->font();
00202 font.setBold( true );
00203 painter->setFont( font );
00204 }
00205 QString sumText;
00206 if ( sum.toLongLong() <= 0 )
00207 sumText = text;
00208 else
00209 sumText = sum.toString();
00210
00211 painter->drawText( textRect, Qt::AlignRight, sumText );
00212 painter->restore();
00213
00214 if ( option.state & QStyle::State_Selected ) {
00215 painter->restore();
00216 }
00217 return;
00218 }
00219
00220 painter->drawText( textRect, option4.displayAlignment, text );
00221
00222 if ( option.state & QStyle::State_Selected ) {
00223 painter->restore();
00224 }
00225 }
00226
00227 #include "collectionstatisticsdelegate.moc"