2 * Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include <QStringList>
21 #include "previewpluginsmodel.h"
23 PreviewPluginsModel::PreviewPluginsModel(QObject
*parent
)
24 : QAbstractListModel(parent
)
26 plugins
= KServiceTypeTrader::self()->query("ThumbCreator");
28 // Sort the list alphabetially
29 QMap
<QString
, KSharedPtr
<KService
> > map
;
30 for (int i
= 0; i
< plugins
.size(); i
++) {
31 map
.insert(plugins
[i
]->name().toLower(), plugins
[i
]);
33 plugins
= map
.values();
36 PreviewPluginsModel::~PreviewPluginsModel()
40 Qt::ItemFlags
PreviewPluginsModel::flags(const QModelIndex
&index
) const
44 return Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled
;
47 QVariant
PreviewPluginsModel::data(const QModelIndex
&index
, int role
) const
49 if (index
.row() < 0 || index
.row() >= plugins
.size()) {
55 return plugins
.at(index
.row())->name();
57 case Qt::CheckStateRole
:
58 return checkedRows
.contains(index
.row()) ? Qt::Checked
: Qt::Unchecked
;
64 bool PreviewPluginsModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
66 if (role
!= Qt::CheckStateRole
) {
70 const Qt::CheckState state
= static_cast<Qt::CheckState
>(value
.toInt());
71 if (state
== Qt::Checked
) {
72 checkedRows
.append(index
.row());
74 checkedRows
.removeAll(index
.row());
77 emit
dataChanged(index
, index
);
81 int PreviewPluginsModel::indexOfPlugin(const QString
&name
) const
83 for (int i
= 0; i
< plugins
.size(); i
++) {
84 if (plugins
.at(i
)->desktopEntryName() == name
) {
91 void PreviewPluginsModel::setCheckedPlugins(const QStringList
&list
)
93 foreach (const QString
&name
, list
) {
94 const int row
= indexOfPlugin(name
);
96 checkedRows
.append(row
);
97 emit
dataChanged(index(row
, 0), index(row
, 0));
102 QStringList
PreviewPluginsModel::checkedPlugins() const
105 foreach (int row
, checkedRows
) {
106 list
.append(plugins
.at(row
)->desktopEntryName());