Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / systemsettings / kcmodulemodel.cpp
blob0ee1669cf5f2524c4923bd660570813f12df764d
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 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()
101 int KCModuleModel::rowCount( const QModelIndex & index ) const
103 int count = 0;
104 MenuItem * mi;
105 if ( index.isValid() ) {
106 mi = static_cast<MenuItem *>( index.internalPointer() );
107 } else {
108 mi = d->rootItem;
110 if ( mi ) {
111 foreach ( MenuItem * i, mi->children ) {
112 count += i->children.count();
115 return count;
118 int KCModuleModel::columnCount( const QModelIndex & /*index*/ ) const
120 return 2; // name and comment
123 QModelIndex KCModuleModel::index( int row, int column, const QModelIndex & parent ) const
125 if ( !hasIndex( row, column, parent ) ) {
126 return QModelIndex();
128 MenuItem * parentItem;
129 if ( !parent.isValid() ) {
130 parentItem = d->rootItem;
131 } else {
132 parentItem = static_cast<MenuItem*>( parent.internalPointer() );
134 MenuItem * foundItem = parentItem->grandChildAt( row );
135 if ( foundItem ) {
136 QModelIndex index = createIndex( row, column, (void*)foundItem );
137 return index;
139 return QModelIndex();
143 QModelIndex KCModuleModel::parent( const QModelIndex & index ) const
145 if ( !index.isValid() ) {
146 return QModelIndex();
149 MenuItem * parentItem = static_cast<MenuItem*>( index.internalPointer() )->parent;
150 if ( parentItem == d->rootItem ) {
151 return QModelIndex();
152 } else {
153 return createIndex( parentItem->parent->children.indexOf( parentItem ), 0, parentItem );
157 QVariant KCModuleModel::data(const QModelIndex &index, int role) const
159 MenuItem * mi = 0;
160 QVariant theData;
161 if ( !index.isValid() ) {
162 return QVariant();
164 mi = static_cast<MenuItem *>( index.internalPointer() );
165 QStringList searchKeyWords;
166 switch ( role ) {
167 case Qt::DisplayRole:
168 switch ( index.column() ) {
169 case 0:
170 theData.setValue( mi->service->name());
171 break;
172 case 1:
173 theData.setValue( mi->service->comment());
174 break;
175 default:
176 break;
178 break;
179 case Qt::DecorationRole:
180 if ( index.column() == 0 )
181 theData = QVariant( KIcon( mi->service->icon() ) );
182 break;
183 case KCategorizedSortFilterProxyModel::CategorySortRole:
184 if ( mi->parent )
185 theData.setValue( QString("%1%2").arg( QString::number( weightOfService( mi->parent->service) ), 5, '0' ).arg( mi->parent->service->name() ) );
186 break;
187 case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
188 if ( mi->parent )
189 theData.setValue( mi->parent->service->name());
190 break;
191 case Qt::UserRole:
192 theData.setValue( mi );
193 break;
194 case UserFilterRole:
195 foreach ( MenuItem * child, mi->children ) {
196 searchKeyWords << child->item.keywords() << child->service->name();
198 searchKeyWords << mi->item.keywords() << mi->service->name();
199 theData.setValue( searchKeyWords.join( QString() ) );
200 break;
201 case WeightRole:
202 theData.setValue( weightOfService( mi->service ) );
203 break;
204 default:
205 break;
207 return theData;
210 Qt::ItemFlags KCModuleModel::flags(const QModelIndex &index) const
212 if (!index.isValid())
213 return 0;
215 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
218 QVariant KCModuleModel::headerData(int section, Qt::Orientation orientation, int role ) const
220 return QAbstractItemModel::headerData( section, orientation, role );