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"
27 #include <KServiceTypeTrader>
29 #include "kcategorizedsortfilterproxymodel.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
);
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();
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" ) ) {
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;
80 class KCModuleModelPrivate
{
82 KCModuleModelPrivate(){
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()
102 int KCModuleModel::rowCount( const QModelIndex
& index
) const
106 if ( index
.isValid() ) {
107 mi
= static_cast<MenuItem
*>( index
.internalPointer() );
112 foreach ( MenuItem
* i
, mi
->children
) {
113 count
+= i
->children
.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
;
133 parentItem
= static_cast<MenuItem
*>( parent
.internalPointer() );
135 MenuItem
* foundItem
= parentItem
->grandChildAt( row
);
137 QModelIndex index
= createIndex( row
, column
, (void*)foundItem
);
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();
154 return createIndex( parentItem
->parent
->children
.indexOf( parentItem
), 0, parentItem
);
158 QVariant
KCModuleModel::data(const QModelIndex
&index
, int role
) const
162 if ( !index
.isValid() ) {
165 mi
= static_cast<MenuItem
*>( index
.internalPointer() );
166 QStringList searchKeyWords
;
168 case Qt::DisplayRole
:
169 switch ( index
.column() ) {
171 theData
.setValue( mi
->service
->name());
174 theData
.setValue( mi
->service
->comment());
180 case Qt::DecorationRole
:
181 if ( index
.column() == 0 )
182 theData
= QVariant( KIcon( mi
->service
->icon() ) );
184 case KCategorizedSortFilterProxyModel::CategorySortRole
:
186 theData
.setValue( QString("%1%2").arg( QString::number( weightOfService( mi
->parent
->service
) ), 5, '0' ).arg( mi
->parent
->service
->name() ) );
188 case KCategorizedSortFilterProxyModel::CategoryDisplayRole
:
190 theData
.setValue( mi
->parent
->service
->name());
193 theData
.setValue( mi
);
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() ) );
203 theData
.setValue( weightOfService( mi
->service
) );
211 Qt::ItemFlags
KCModuleModel::flags(const QModelIndex
&index
) const
213 if (!index
.isValid())
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
);