• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

collectionstatisticsdelegate.cpp

00001 /*
00002     Copyright (c) 2008 Thomas McGuire <thomas.mcguire@gmx.net>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
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   // First, paint the basic, but without the text. We remove the text
00089   // in initStyleOption(), which gets called by QStyledItemDelegate::paint().
00090   QStyledItemDelegate::paint( painter, option, index );
00091 
00092   // No, we retrieve the correct style option by calling intiStyleOption from
00093   // the superclass.
00094   QStyleOptionViewItemV4 option4 = option;
00095   QStyledItemDelegate::initStyleOption( &option4, index );
00096   QString text = option4.text;
00097 
00098   // Now calculate the rectangle for the text
00099   QStyle *s = d->parent->style();
00100   const QWidget *widget = option4.widget;
00101   QRect textRect = s->subElementRect( QStyle::SE_ItemViewItemText, &option4, widget );
00102 
00103    // When checking if the item is expanded, we need to check that for the first
00104   // column, as Qt only recogises the index as expanded for the first column
00105   QModelIndex firstColumn = index.model()->index( index.row(), 0, index.parent() );
00106   bool expanded = d->parent->isExpanded( firstColumn );
00107 
00108   // Draw the unread count after the folder name (in parenthesis)
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     // Construct the string which will appear after the foldername (with the
00119     // unread count)
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     // Squeeze the folder text if it is to big and calculate the rectangles
00142     // where the folder text and the unread count will be drawn to
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     // Draw folder name and unread count
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   // For the unread/total column, paint the summed up count if the item
00169   // is collapsed
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"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7.1
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