• Skip to content
  • Skip to link menu
KDE 4.4 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 <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   // 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   if ( option.state & QStyle::State_Selected ) {
00109     painter->save();
00110     painter->setPen( option.palette.highlightedText().color() );
00111   }
00112 
00113   // Draw the unread count after the folder name (in parenthesis)
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     // Construct the string which will appear after the foldername (with the
00124     // unread count)
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     // Squeeze the folder text if it is to big and calculate the rectangles
00147     // where the folder text and the unread count will be drawn to
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     // Draw folder name and unread count
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   // For the unread/total column, paint the summed up count if the item
00178   // is collapsed
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"

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
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.2-20100208
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