SVN_SILENT made messages (.desktop file)
[kdepim.git] / akonadi_next / quotacolorproxymodel.cpp
blob487c5622b48cd0506a7602f0a630d7363478f6c2
1 /*
2 Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
21 #include "quotacolorproxymodel.h"
23 #include <akonadi/entitytreemodel.h>
24 #include <akonadi/collectionquotaattribute.h>
26 #include <QColor>
28 using namespace Akonadi;
30 static const int qmlForegroundRole = 1984;
32 /**
33 * @internal
35 class QuotaColorProxyModel::Private
37 public:
38 Private( QuotaColorProxyModel *parent )
39 : mParent( parent ), mThreshold( 100.0 ), mColor( Qt::red )
43 QuotaColorProxyModel *mParent;
45 qreal mThreshold;
46 QColor mColor;
49 QuotaColorProxyModel::QuotaColorProxyModel( QObject *parent )
50 : KIdentityProxyModel( parent ),
51 d( new Private( this ) )
55 QuotaColorProxyModel::~QuotaColorProxyModel()
57 delete d;
60 void QuotaColorProxyModel::setWarningThreshold( qreal threshold )
62 d->mThreshold = threshold;
65 qreal QuotaColorProxyModel::warningThreshold() const
67 return d->mThreshold;
70 void QuotaColorProxyModel::setWarningColor( const QColor &color )
72 d->mColor = color;
75 QColor QuotaColorProxyModel::warningColor() const
77 return d->mColor;
80 QVariant QuotaColorProxyModel::data( const QModelIndex & index, int role) const
82 if ( role == Qt::ForegroundRole || role == qmlForegroundRole ) {
83 const QModelIndex sourceIndex = mapToSource( index );
84 const QModelIndex rowIndex = sourceIndex.sibling( sourceIndex.row(), 0 );
85 const Collection collection = sourceModel()->data( rowIndex, EntityTreeModel::CollectionRole ).value<Collection>();
87 if ( collection.isValid() && collection.hasAttribute<CollectionQuotaAttribute>() ) {
88 const CollectionQuotaAttribute *quota = collection.attribute<CollectionQuotaAttribute>();
90 if ( quota->currentValue() > -1 && quota->maximumValue() > 0 ) {
91 const qreal percentage = ( 100.0 * quota->currentValue() ) / quota->maximumValue();
93 if ( percentage >= d->mThreshold ) {
94 return (role == Qt::ForegroundRole ? d->mColor : d->mColor.name());
100 return KIdentityProxyModel::data( index, role );
103 void QuotaColorProxyModel::setSourceModel( QAbstractItemModel *sourceModel )
105 KIdentityProxyModel::setSourceModel( sourceModel );
107 if ( sourceModel ) {
108 QHash<int, QByteArray> names = sourceModel->roleNames();
109 names.insert( qmlForegroundRole, "foreground" );
110 setRoleNames( names );
114 #include "quotacolorproxymodel.moc"