21 #include "collectionstatisticsdelegate.h"
22 #include "collectionstatisticsmodel.h"
24 #include <kcolorscheme.h>
26 #include <kio/global.h>
28 #include <QtGui/QPainter>
29 #include <QtGui/QStyle>
30 #include <QtGui/QStyleOption>
31 #include <QtGui/QStyleOptionViewItemV4>
32 #include <QtGui/QAbstractItemView>
33 #include <QtGui/QTreeView>
35 #include "entitytreemodel.h"
36 #include "collectionstatistics.h"
37 #include "collection.h"
38 #include "progressspinnerdelegate_p.h"
40 using namespace Akonadi;
50 class CollectionStatisticsDelegatePrivate
53 QAbstractItemView *parent;
54 bool drawUnreadAfterFolder;
55 DelegateAnimator *animator;
56 QColor mSelectedUnreadColor;
57 QColor mDeselectedUnreadColor;
59 CollectionStatisticsDelegatePrivate( QAbstractItemView *treeView )
61 drawUnreadAfterFolder( false ),
67 void getCountRecursive(
const QModelIndex &index, qint64 &totalCount, qint64 &unreadCount, qint64 &totalSize )
const
69 Collection collection = qvariant_cast<Collection>( index.data( EntityTreeModel::CollectionRole ) );
72 if ( collection.isValid() ) {
73 CollectionStatistics statistics = collection.statistics();
74 totalCount += qMax( 0LL, statistics.count() );
75 unreadCount += qMax( 0LL, statistics.unreadCount() );
76 totalSize += qMax( 0LL, statistics.size() );
77 if ( index.model()->hasChildren( index ) ) {
78 const int rowCount = index.model()->rowCount( index );
79 for (
int row = 0; row < rowCount; row++ ) {
80 static const int column = 0;
81 getCountRecursive( index.model()->index( row, column, index ), totalCount, unreadCount, totalSize );
89 mSelectedUnreadColor = KColorScheme( QPalette::Active, KColorScheme::Selection )
90 .foreground( KColorScheme::LinkText ).color();
91 mDeselectedUnreadColor = KColorScheme( QPalette::Active, KColorScheme::View )
92 .foreground( KColorScheme::LinkText ).color();
98 CollectionStatisticsDelegate::CollectionStatisticsDelegate( QAbstractItemView *parent )
99 : QStyledItemDelegate( parent ),
100 d_ptr( new CollectionStatisticsDelegatePrivate( parent ) )
105 CollectionStatisticsDelegate::CollectionStatisticsDelegate( QTreeView *parent )
106 : QStyledItemDelegate( parent ),
107 d_ptr( new CollectionStatisticsDelegatePrivate( parent ) )
112 CollectionStatisticsDelegate::~CollectionStatisticsDelegate()
117 void CollectionStatisticsDelegate::setUnreadCountShown(
bool enable )
120 d->drawUnreadAfterFolder = enable;
123 bool CollectionStatisticsDelegate::unreadCountShown()
const
126 return d->drawUnreadAfterFolder;
129 void CollectionStatisticsDelegate::setProgressAnimationEnabled(
bool enable )
132 if ( enable == ( d->animator != 0 ) )
135 Q_ASSERT( !d->animator );
136 Akonadi::DelegateAnimator *animator =
new Akonadi::DelegateAnimator( d->parent );
137 d->animator = animator;
144 bool CollectionStatisticsDelegate::progressAnimationEnabled()
const
146 Q_D(
const CollectionStatisticsDelegate );
147 return d->animator != 0;
150 void CollectionStatisticsDelegate::initStyleOption( QStyleOptionViewItem *option,
151 const QModelIndex &index )
const
153 Q_D(
const CollectionStatisticsDelegate );
155 QStyleOptionViewItemV4 *noTextOption =
156 qstyleoption_cast<QStyleOptionViewItemV4 *>( option );
157 QStyledItemDelegate::initStyleOption( noTextOption, index );
158 if ( option->decorationPosition != QStyleOptionViewItem::Top ) {
159 noTextOption->text.clear();
164 const Akonadi::Collection collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
166 if (!collection.isValid())
168 d->animator->pop(index);
174 d->animator->pop(index);
178 d->animator->push(index);
180 if (QStyleOptionViewItemV4 *v4 = qstyleoption_cast<QStyleOptionViewItemV4 *>(option)) {
181 v4->icon = d->animator->sequenceFrame(index);
186 class PainterStateSaver
189 PainterStateSaver( QPainter *painter )
204 void CollectionStatisticsDelegate::paint( QPainter *painter,
205 const QStyleOptionViewItem &option,
206 const QModelIndex &index )
const
208 Q_D(
const CollectionStatisticsDelegate );
209 PainterStateSaver stateSaver( painter );
211 const QColor textColor = index.data( Qt::ForegroundRole ).value<QColor>();
214 QStyledItemDelegate::paint( painter, option, index );
218 QStyleOptionViewItemV4 option4 = option;
219 QStyledItemDelegate::initStyleOption( &option4, index );
220 QString text = option4.text;
223 QStyle *s = d->parent->style();
224 const QWidget *widget = option4.widget;
225 const QRect textRect = s->subElementRect( QStyle::SE_ItemViewItemText, &option4, widget );
229 QModelIndex firstColumn = index.model()->index( index.row(), 0, index.parent() );
230 QTreeView* treeView = qobject_cast<QTreeView*>( d->parent );
231 bool expanded = treeView && treeView->isExpanded( firstColumn );
233 if ( option.state & QStyle::State_Selected ) {
234 painter->setPen( textColor.isValid() ? textColor : option.palette.highlightedText().color() );
237 Collection collection = index.sibling( index.row(), 0 ).data( EntityTreeModel::CollectionRole ).value<Collection>();
239 Q_ASSERT(collection.isValid());
241 CollectionStatistics statistics = collection.statistics();
243 qint64 unreadCount = qMax( 0LL, statistics.unreadCount() );
244 qint64 totalRecursiveCount = 0;
245 qint64 unreadRecursiveCount = 0;
246 qint64 totalSize = 0;
247 d->getCountRecursive( index.sibling( index.row(), 0 ), totalRecursiveCount, unreadRecursiveCount, totalSize );
250 if ( d->drawUnreadAfterFolder && index.column() == 0 ) {
255 if ( expanded && unreadCount > 0 )
256 unread = QString::fromLatin1(
" (%1)" ).arg( unreadCount );
257 else if ( !expanded ) {
258 if ( unreadCount != unreadRecursiveCount )
259 unread = QString::fromLatin1(
" (%1 + %2)" ).arg( unreadCount ).arg( unreadRecursiveCount - unreadCount );
260 else if ( unreadCount > 0 )
261 unread = QString::fromLatin1(
" (%1)" ).arg( unreadCount );
264 PainterStateSaver stateSaver( painter );
266 if ( !unread.isEmpty() ) {
267 QFont font = painter->font();
268 font.setBold(
true );
269 painter->setFont( font );
272 const QColor unreadColor = (option.state & QStyle::State_Selected) ? d->mSelectedUnreadColor : d->mDeselectedUnreadColor;
273 const QRect iconRect = s->subElementRect( QStyle::SE_ItemViewItemDecoration, &option4, widget );
275 if ( option.decorationPosition == QStyleOptionViewItem::Left
276 || option.decorationPosition == QStyleOptionViewItem::Right ) {
279 QString folderName = text;
280 QFontMetrics fm( painter->fontMetrics() );
281 const int unreadWidth = fm.width( unread );
282 int folderWidth( fm.width( folderName ) );
283 const bool enoughPlaceForText = ( option.rect.width() > ( folderWidth + unreadWidth + iconRect.width() ) );
285 if ( !enoughPlaceForText && ( folderWidth + unreadWidth > textRect.width() )) {
286 folderName = fm.elidedText( folderName, Qt::ElideRight,
287 option.rect.width() - unreadWidth - iconRect.width() );
288 folderWidth = fm.width( folderName );
290 QRect folderRect = textRect;
291 QRect unreadRect = textRect;
292 folderRect.setRight( textRect.left() + folderWidth );
293 unreadRect = QRect( folderRect.right(), folderRect.top(), unreadRect.width(), unreadRect.height() );
294 if ( textColor.isValid() )
295 painter->setPen( textColor );
298 painter->drawText( folderRect, Qt::AlignLeft | Qt::AlignVCenter, folderName );
299 painter->setPen( unreadColor );
300 painter->drawText( unreadRect, Qt::AlignLeft | Qt::AlignVCenter, unread );
301 }
else if ( option.decorationPosition == QStyleOptionViewItem::Top ) {
302 if ( unreadCount > 0 ) {
304 painter->setPen( unreadColor );
305 painter->drawText( iconRect, Qt::AlignCenter, QString::number( unreadCount ) );
313 if ( ( index.column() == 1 || index.column() == 2 ) ) {
315 QFont savedFont = painter->font();
317 if ( index.column() == 1 && ( ( !expanded && unreadRecursiveCount > 0 ) || ( expanded && unreadCount > 0 ) ) ) {
318 QFont font = painter->font();
319 font.setBold(
true );
320 painter->setFont( font );
321 sumText = QString::number( expanded ? unreadCount : unreadRecursiveCount );
324 qint64 totalCount = statistics.count();
325 if (index.column() == 2 && ( ( !expanded && totalRecursiveCount > 0 ) || ( expanded && totalCount > 0 ) ) ) {
326 sumText = QString::number( expanded ? totalCount : totalRecursiveCount );
330 painter->drawText( textRect, Qt::AlignRight | Qt::AlignVCenter, sumText );
331 painter->setFont( savedFont );
336 if ( index.column() == 3 && !expanded ) {
337 if ( textColor.isValid() )
338 painter->setPen( textColor );
339 painter->drawText( textRect, option4.displayAlignment | Qt::AlignVCenter, KIO::convertSize( (KIO::filesize_t)totalSize ) );
343 if ( textColor.isValid() )
344 painter->setPen( textColor );
345 painter->drawText( textRect, option4.displayAlignment | Qt::AlignVCenter, text );
348 void CollectionStatisticsDelegate::updatePalette()
354 #include "collectionstatisticsdelegate.moc"