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 <QPainter>
00027 #include <QStyle>
00028 #include <QStyleOption>
00029 #include <QStyleOptionViewItemV4>
00030 #include <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 = QString();
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
00109 if ( d->drawUnreadAfterFolder && index.column() == 0 ) {
00110
00111 QVariant unreadCount = index.model()->data( index,
00112 CollectionStatisticsModel::UnreadRole );
00113 QVariant unreadRecursiveCount = index.model()->data( index,
00114 CollectionStatisticsModel::RecursiveUnreadRole );
00115 Q_ASSERT( unreadCount.type() == QVariant::LongLong );
00116 Q_ASSERT( unreadRecursiveCount.type() == QVariant::LongLong );
00117
00118
00119
00120 QString unread;
00121 QString unreadCountInChilds = QString::number( unreadRecursiveCount.toLongLong() -
00122 unreadCount.toLongLong() );
00123 if ( expanded && unreadCount.toLongLong() > 0 )
00124 unread = QString( QLatin1String(" (%1)") ).arg( unreadCount.toLongLong() );
00125 else if ( !expanded ) {
00126 if ( unreadCount.toLongLong() != unreadRecursiveCount.toLongLong() )
00127 unread = QString( QLatin1String(" (%1 + %2)") ).arg( unreadCount.toString(),
00128 unreadCountInChilds );
00129 else if ( unreadCount.toLongLong() > 0 )
00130 unread = QString( QLatin1String(" (%1)") ).arg( unreadCount.toString() );
00131 }
00132
00133 painter->save();
00134
00135 if ( !unread.isEmpty() ) {
00136 QFont font = painter->font();
00137 font.setBold( true );
00138 painter->setFont( font );
00139 }
00140
00141
00142
00143 QString folderName = text;
00144 QFontMetrics fm( painter->fontMetrics() );
00145 int unreadWidth = fm.width( unread );
00146 if ( fm.width( folderName ) + unreadWidth > textRect.width() ) {
00147 folderName = fm.elidedText( folderName, Qt::ElideRight,
00148 textRect.width() - unreadWidth );
00149 }
00150 int folderWidth = fm.width( folderName );
00151 QRect folderRect = textRect;
00152 QRect unreadRect = textRect;
00153 folderRect.setRight( textRect.left() + folderWidth );
00154 unreadRect.setLeft( folderRect.right() );
00155
00156
00157 painter->drawText( folderRect, Qt::AlignLeft, folderName );
00158 KColorScheme::ColorSet cs = ( option.state & QStyle::State_Selected ) ?
00159 KColorScheme::Selection : KColorScheme::View;
00160 QColor unreadColor = KColorScheme( QPalette::Active, cs ).
00161 foreground( KColorScheme::LinkText ).color();
00162 painter->setPen( unreadColor );
00163 painter->drawText( unreadRect, Qt::AlignLeft, unread );
00164 painter->restore();
00165 return;
00166 }
00167
00168
00169
00170 if ( ( index.column() == 1 || index.column() == 2 ) ) {
00171
00172 painter->save();
00173
00174 int role = 0;
00175 if ( index.column() == 1 ) {
00176 if ( !expanded )
00177 role = CollectionStatisticsModel::RecursiveUnreadRole;
00178 else
00179 role = CollectionStatisticsModel::UnreadRole;
00180 }
00181 else if ( index.column() == 2 ) {
00182 if ( !expanded )
00183 role = CollectionStatisticsModel::RecursiveTotalRole;
00184 else
00185 role = CollectionStatisticsModel::TotalRole;
00186 }
00187
00188 QVariant sum = index.model()->data( index, role );
00189 Q_ASSERT( sum.type() == QVariant::LongLong );
00190 QStyleOptionViewItem opt = option;
00191 if ( index.column() == 1 && sum.toLongLong() > 0 ) {
00192 QFont font = painter->font();
00193 font.setBold( true );
00194 painter->setFont( font );
00195 }
00196 QString sumText;
00197 if ( sum.toLongLong() <= 0 )
00198 sumText = text;
00199 else
00200 sumText = sum.toString();
00201
00202 painter->drawText( textRect, Qt::AlignRight, sumText );
00203 painter->restore();
00204 return;
00205 }
00206
00207 painter->drawText( textRect, option4.displayAlignment, text );
00208 }
00209
00210 #include "collectionstatisticsdelegate.moc"