add more spacing
[personal-kdebase.git] / workspace / systemsettings / kcmodulemodel.cpp
blob92852c0374bbf42cfec5fff0d1d3c40f84a19159
1 /* This file is part of the KDE project
2 Copyright 2007 Will Stephenson <wstephenson@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License or (at your option) version 3 or any later version
8 accepted by the membership of KDE e.V. (or its successor approved
9 by the membership of KDE e.V.), which shall act as a proxy
10 defined in Section 14 of version 3 of the license.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "kcmodulemodel.h"
23 #include <QHash>
24 #include <QList>
25 #include <KDebug>
26 #include <KIcon>
27 #include <KServiceTypeTrader>
29 #include "kcategorizedsortfilterproxymodel.h"
30 #include "menuitem.h"
32 Q_DECLARE_METATYPE(MenuItem *)
34 SystemSettingsProxyModel::SystemSettingsProxyModel( QObject * parent )
35 : KCategorizedSortFilterProxyModel( parent )
40 SystemSettingsProxyModel::~SystemSettingsProxyModel()
43 bool SystemSettingsProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
45 QVariant leftWeight = left.data( KCModuleModel::WeightRole );
46 QVariant rightWeight = right.data( KCModuleModel::WeightRole );
48 if ( !( leftWeight.isValid() && rightWeight.isValid() ) ) {
49 return KCategorizedSortFilterProxyModel::subSortLessThan( left, right );
50 } else {
51 // kDebug() << "comparing " << left.data().toString() << " (" << leftWeight.toInt() << ") and " << right.data().toString() << " (" << rightWeight.toInt() << ")";
52 if ( leftWeight.toInt() == rightWeight.toInt() ) {
53 return left.data().toString() < right.data().toString();
54 } else {
55 return leftWeight.toInt() < rightWeight.toInt();
61 bool SystemSettingsProxyModel::filterAcceptsRow( int source_row, const QModelIndex & source_parent ) const
63 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
64 MenuItem * mItem = index.data( Qt::UserRole ).value<MenuItem*>();
65 // accept only systemsettings categories that have children
66 if ( mItem->children.isEmpty() && mItem->service->serviceTypes().contains("SystemSettingsCategory" ) ) {
67 return false;
68 } else {
69 return KCategorizedSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
73 inline int weightOfService( const KService::Ptr service )
75 QVariant tmp = service->property( "X-KDE-Weight", QVariant::Int );
76 int weight = tmp.isValid() ? tmp.toInt() : 100;
77 return weight;
80 class KCModuleModelPrivate {
81 public:
82 KCModuleModelPrivate(){
85 MenuItem * rootItem;
88 const int KCModuleModel::UserFilterRole = 0x015D1AE6;
89 const int KCModuleModel::WeightRole = 0x03A8CC00;
91 KCModuleModel::KCModuleModel( MenuItem * menuRoot, QObject * parent )
92 : QAbstractItemModel( parent ), d( new KCModuleModelPrivate )
94 d->rootItem = menuRoot;
97 KCModuleModel::~KCModuleModel()
99 delete d;
102 int KCModuleModel::rowCount( const QModelIndex & index ) const
104 int count = 0;
105 MenuItem * mi;
106 if ( index.isValid() ) {
107 mi = static_cast<MenuItem *>( index.internalPointer() );
108 } else {
109 mi = d->rootItem;
111 if ( mi ) {
112 foreach ( MenuItem * i, mi->children ) {
113 count += i->children.count();
116 return count;
119 int KCModuleModel::columnCount( const QModelIndex & /*index*/ ) const
121 return 2; // name and comment
124 QModelIndex KCModuleModel::index( int row, int column, const QModelIndex & parent ) const
126 if ( !hasIndex( row, column, parent ) ) {
127 return QModelIndex();
129 MenuItem * parentItem;
130 if ( !parent.isValid() ) {
131 parentItem = d->rootItem;
132 } else {
133 parentItem = static_cast<MenuItem*>( parent.internalPointer() );
135 MenuItem * foundItem = parentItem->grandChildAt( row );
136 if ( foundItem ) {
137 QModelIndex index = createIndex( row, column, (void*)foundItem );
138 return index;
140 return QModelIndex();
144 QModelIndex KCModuleModel::parent( const QModelIndex & index ) const
146 if ( !index.isValid() ) {
147 return QModelIndex();
150 MenuItem * parentItem = static_cast<MenuItem*>( index.internalPointer() )->parent;
151 if ( parentItem == d->rootItem ) {
152 return QModelIndex();
153 } else {
154 return createIndex( parentItem->parent->children.indexOf( parentItem ), 0, parentItem );
158 QVariant KCModuleModel::data(const QModelIndex &index, int role) const
160 MenuItem * mi = 0;
161 QVariant theData;
162 if ( !index.isValid() ) {
163 return QVariant();
165 mi = static_cast<MenuItem *>( index.internalPointer() );
166 QStringList searchKeyWords;
167 switch ( role ) {
168 case Qt::DisplayRole:
169 switch ( index.column() ) {
170 case 0:
171 theData.setValue( mi->service->name());
172 break;
173 case 1:
174 theData.setValue( mi->service->comment());
175 break;
176 default:
177 break;
179 break;
180 case Qt::DecorationRole:
181 if ( index.column() == 0 )
182 theData = QVariant( KIcon( mi->service->icon() ) );
183 break;
184 case KCategorizedSortFilterProxyModel::CategorySortRole:
185 if ( mi->parent )
186 theData.setValue( QString("%1%2").arg( QString::number( weightOfService( mi->parent->service) ), 5, '0' ).arg( mi->parent->service->name() ) );
187 break;
188 case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
189 if ( mi->parent )
190 theData.setValue( mi->parent->service->name());
191 break;
192 case Qt::UserRole:
193 theData.setValue( mi );
194 break;
195 case UserFilterRole:
196 foreach ( MenuItem * child, mi->children ) {
197 searchKeyWords << child->item.keywords() << child->service->name();
199 searchKeyWords << mi->item.keywords() << mi->service->name();
200 theData.setValue( searchKeyWords.join( QString() ) );
201 break;
202 case WeightRole:
203 theData.setValue( weightOfService( mi->service ) );
204 break;
205 default:
206 break;
208 return theData;
211 Qt::ItemFlags KCModuleModel::flags(const QModelIndex &index) const
213 if (!index.isValid())
214 return 0;
216 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
219 QVariant KCModuleModel::headerData(int section, Qt::Orientation orientation, int role ) const
221 return QAbstractItemModel::headerData( section, orientation, role );