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

akonadi

statisticsproxymodel.cpp

00001 /*
00002     Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
00003 
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to the
00017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018     02110-1301, USA.
00019 */
00020 
00021 #include "statisticsproxymodel.h"
00022 
00023 #include "entitytreemodel.h"
00024 #include "collectionutils_p.h"
00025 
00026 #include <akonadi/collectionquotaattribute.h>
00027 #include <akonadi/collectionstatistics.h>
00028 #include <akonadi/entitydisplayattribute.h>
00029 
00030 #include <kdebug.h>
00031 #include <kiconloader.h>
00032 #include <klocale.h>
00033 #include <kio/global.h>
00034 
00035 #include <QtGui/QApplication>
00036 #include <QtGui/QPalette>
00037 #include <KIcon>
00038 using namespace Akonadi;
00039 
00043 class StatisticsProxyModel::Private
00044 {
00045   public:
00046     Private( StatisticsProxyModel *parent )
00047       : mParent( parent ), mToolTipEnabled( false ), mExtraColumnsEnabled( true )
00048     {
00049     }
00050 
00051     int sourceColumnCount( const QModelIndex &parent )
00052     {
00053       return mParent->sourceModel()->columnCount( mParent->mapToSource( parent ) );
00054     }
00055 
00056     QString toolTipForCollection( const QModelIndex &index, const Collection &collection )
00057     {
00058       QString bckColor = QApplication::palette().color( QPalette::ToolTipBase ).name();
00059       QString txtColor = QApplication::palette().color( QPalette::ToolTipText ).name();
00060 
00061       QString tip = QString::fromLatin1(
00062         "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n"
00063         );
00064 
00065       tip += QString::fromLatin1(
00066         "  <tr>\n"
00067         "    <td bgcolor=\"%1\" colspan=\"2\" align=\"left\" valign=\"middle\">\n"
00068         "      <div style=\"color: %2; font-weight: bold;\">\n"
00069         "      %3\n"
00070         "      </div>\n"
00071         "    </td>\n"
00072         "  </tr>\n"
00073         ).arg( txtColor ).arg( bckColor ).arg( index.data( Qt::DisplayRole ).toString() );
00074 
00075 
00076       tip += QString::fromLatin1(
00077         "  <tr>\n"
00078         "    <td align=\"left\" valign=\"top\">\n"
00079         );
00080 
00081       tip += QString::fromLatin1(
00082         "      <strong>%1</strong>: %2<br>\n"
00083         "      <strong>%3</strong>: %4<br><br>\n"
00084         ).arg( i18n("Total Messages") ).arg( collection.statistics().count() )
00085          .arg( i18n("Unread Messages") ).arg( collection.statistics().unreadCount() );
00086 
00087       if ( collection.hasAttribute<CollectionQuotaAttribute>() ) {
00088         CollectionQuotaAttribute *quota = collection.attribute<CollectionQuotaAttribute>();
00089         if ( quota->currentValue() > -1 && quota->maximumValue() > 0 ) {
00090           qreal percentage = ( 100.0 * quota->currentValue() ) / quota->maximumValue();
00091 
00092           if ( qAbs( percentage ) >= 0.01 ) {
00093             QString percentStr = QString::number( percentage, 'f', 2 );
00094             tip += QString::fromLatin1(
00095               "      <strong>%1</strong>: %2%<br>\n"
00096               ).arg( i18n( "Quota" ) ).arg( percentStr );
00097           }
00098         }
00099       }
00100 
00101       tip += QString::fromLatin1(
00102         "      <strong>%1</strong>: %2<br>\n"
00103         ).arg( i18n("Storage Size") ).arg( KIO::convertSize( (KIO::filesize_t)( collection.statistics().size() ) ) );
00104 
00105 
00106       QString iconName = CollectionUtils::defaultIconName( collection );
00107       if ( collection.hasAttribute<EntityDisplayAttribute>() &&
00108            !collection.attribute<EntityDisplayAttribute>()->iconName().isEmpty() ) {
00109         iconName = collection.attribute<EntityDisplayAttribute>()->iconName();
00110       }
00111 
00112       int iconSizes[] = { 32, 22 };
00113       QString iconPath;
00114 
00115       for ( int i = 0; i < 2; i++ ) {
00116         iconPath = KIconLoader::global()->iconPath( iconName, -iconSizes[ i ], true );
00117         if ( !iconPath.isEmpty() )
00118           break;
00119       }
00120 
00121       if ( iconPath.isEmpty() ) {
00122         iconPath = KIconLoader::global()->iconPath( QLatin1String("folder"), -32, false );
00123       }
00124 
00125       tip += QString::fromLatin1(
00126         "    </td>\n"
00127         "    <td align=\"right\" valign=\"top\">\n"
00128         "      <table border=\"0\"><tr><td width=\"32\" height=\"32\" align=\"center\" valign=\"middle\">\n"
00129         "      <img src=\"%1\">\n"
00130         "      </td></tr></table>\n"
00131         "    </td>\n"
00132         "  </tr>\n"
00133         ).arg( iconPath );
00134 
00135       tip += QString::fromLatin1(
00136         "</table>"
00137         );
00138 
00139       return tip;
00140     }
00141 
00142     StatisticsProxyModel *mParent;
00143 
00144     bool mToolTipEnabled;
00145     bool mExtraColumnsEnabled;
00146 };
00147 
00148 StatisticsProxyModel::StatisticsProxyModel( QObject *parent )
00149   : QSortFilterProxyModel( parent ),
00150     d( new Private( this ) )
00151 {
00152 }
00153 
00154 StatisticsProxyModel::~StatisticsProxyModel()
00155 {
00156   delete d;
00157 }
00158 
00159 void StatisticsProxyModel::setToolTipEnabled( bool enable )
00160 {
00161   d->mToolTipEnabled = enable;
00162 }
00163 
00164 bool StatisticsProxyModel::isToolTipEnabled() const
00165 {
00166   return d->mToolTipEnabled;
00167 }
00168 
00169 void StatisticsProxyModel::setExtraColumnsEnabled( bool enable )
00170 {
00171   d->mExtraColumnsEnabled = enable;
00172 }
00173 
00174 bool StatisticsProxyModel::isExtraColumnsEnabled() const
00175 {
00176   return d->mExtraColumnsEnabled;
00177 }
00178 
00179 QModelIndex Akonadi::StatisticsProxyModel::index( int row, int column, const QModelIndex & parent ) const
00180 {
00181     if (!hasIndex(row, column, parent))
00182       return QModelIndex();
00183 
00184 
00185     int sourceColumn = column;
00186 
00187     if ( column>=d->sourceColumnCount( parent ) ) {
00188       sourceColumn = 0;
00189     }
00190 
00191     QModelIndex i = QSortFilterProxyModel::index( row, sourceColumn, parent );
00192     return createIndex( i.row(), column, i.internalPointer() );
00193 }
00194 
00195 QVariant StatisticsProxyModel::data( const QModelIndex & index, int role) const
00196 {
00197   if ( role == Qt::DisplayRole && index.column()>=d->sourceColumnCount( index.parent() ) ) {
00198     const QModelIndex sourceIndex = mapToSource( index.sibling( index.row(), 0 ) );
00199     Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
00200 
00201     if ( collection.isValid() && collection.statistics().count()>=0 ) {
00202       if ( index.column() == d->sourceColumnCount( QModelIndex() )+2 ) {
00203         return KIO::convertSize( (KIO::filesize_t)( collection.statistics().size() ) );
00204       } else if ( index.column() == d->sourceColumnCount( QModelIndex() )+1 ) {
00205         return collection.statistics().count();
00206       } else if ( index.column() == d->sourceColumnCount( QModelIndex() ) ) {
00207         if ( collection.statistics().unreadCount() > 0 ) {
00208           return collection.statistics().unreadCount();
00209         } else {
00210           return QString();
00211         }
00212       } else {
00213         kWarning() << "We shouldn't get there for a column which is not total, unread or size.";
00214         return QVariant();
00215       }
00216     }
00217 
00218   } else if ( role == Qt::TextAlignmentRole && index.column()>=d->sourceColumnCount( index.parent() ) ) {
00219     return Qt::AlignRight;
00220 
00221   } else if ( role == Qt::ToolTipRole && d->mToolTipEnabled ) {
00222     const QModelIndex sourceIndex = mapToSource( index.sibling( index.row(), 0 ) );
00223     Collection collection
00224       = sourceModel()->data( sourceIndex,
00225                              EntityTreeModel::CollectionRole ).value<Collection>();
00226 
00227     if ( collection.isValid() && collection.statistics().count()>0 ) {
00228       return d->toolTipForCollection( index, collection );
00229     }
00230 
00231   } else if ( role == Qt::DecorationRole && index.column() == 0 ) {
00232     const QModelIndex sourceIndex = mapToSource( index.sibling( index.row(), 0 ) );
00233     Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
00234 
00235     if ( collection.isValid() )
00236       return KIcon(  CollectionUtils::displayIconName( collection ) );
00237     else
00238       return QVariant();
00239   }
00240 
00241   return QAbstractProxyModel::data( index, role );
00242 }
00243 
00244 QVariant StatisticsProxyModel::headerData( int section, Qt::Orientation orientation, int role) const
00245 {
00246   if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
00247     if ( section == d->sourceColumnCount( QModelIndex() ) + 2 ) {
00248       return i18nc( "collection size", "Size" );
00249     } else if ( section == d->sourceColumnCount( QModelIndex() ) + 1 ) {
00250       return i18nc( "number of entities in the collection", "Total" );
00251     } else if ( section == d->sourceColumnCount( QModelIndex() ) ) {
00252       return i18nc( "number of unread entities in the collection", "Unread" );
00253     }
00254   }
00255 
00256   return QSortFilterProxyModel::headerData( section, orientation, role );
00257 }
00258 
00259 Qt::ItemFlags StatisticsProxyModel::flags( const QModelIndex & index ) const
00260 {
00261   if ( index.column()>=d->sourceColumnCount( index.parent() ) ) {
00262     return QSortFilterProxyModel::flags( index.sibling( index.row(), 0 ) )
00263          & ( Qt::ItemIsSelectable | Qt::ItemIsDragEnabled // Allowed flags
00264            | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled );
00265   }
00266 
00267   return QSortFilterProxyModel::flags( index );
00268 }
00269 
00270 int StatisticsProxyModel::columnCount( const QModelIndex & parent ) const
00271 {
00272   if ( sourceModel()==0 ) {
00273     return 0;
00274   } else {
00275     return d->sourceColumnCount( parent )
00276          + ( d->mExtraColumnsEnabled ? 3 : 0 );
00277   }
00278 }
00279 
00280 QModelIndexList StatisticsProxyModel::match(const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags) const
00281 {
00282   if (role < Qt::UserRole)
00283     return QSortFilterProxyModel::match(start, role, value, hits, flags);
00284 
00285   QModelIndexList list;
00286   QModelIndex proxyIndex;
00287   foreach(const QModelIndex idx, sourceModel()->match(mapToSource(start), role, value, hits, flags))
00288   {
00289     proxyIndex = mapFromSource(idx);
00290     if (proxyIndex.isValid())
00291       list << proxyIndex;
00292   }
00293   return list;
00294 }
00295 
00296 
00297 #include "statisticsproxymodel.moc"
00298 

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