Pristine copies of applet model from
from: kde-workspace/libs/plasmagenericshell/widgetsexplorer
This commit is contained in:
parent
d4d9b70721
commit
d099b7e2c6
249
src/shell/widgetexplorer/kcategorizeditemsviewmodels.cpp
Normal file
249
src/shell/widgetexplorer/kcategorizeditemsviewmodels.cpp
Normal file
@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library/Lesser General Public License
|
||||
* version 2, or (at your option) any later version, as published by the
|
||||
* Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details
|
||||
*
|
||||
* You should have received a copy of the GNU Library/Lesser General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "kcategorizeditemsviewmodels_p.h"
|
||||
#include <klocale.h>
|
||||
|
||||
#define COLUMN_COUNT 4
|
||||
|
||||
namespace KCategorizedItemsViewModels {
|
||||
|
||||
// AbstractItem
|
||||
|
||||
QString AbstractItem::name() const
|
||||
{
|
||||
return text();
|
||||
}
|
||||
|
||||
QString AbstractItem::id() const
|
||||
{
|
||||
QString plugin = data().toMap()["pluginName"].toString();
|
||||
|
||||
if (plugin.isEmpty()) {
|
||||
return name();
|
||||
}
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
QString AbstractItem::description() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
bool AbstractItem::isFavorite() const
|
||||
{
|
||||
return passesFiltering(Filter("favorite", true));
|
||||
}
|
||||
|
||||
int AbstractItem::running() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool AbstractItem::matches(const QString &pattern) const
|
||||
{
|
||||
return
|
||||
name().contains(pattern, Qt::CaseInsensitive) ||
|
||||
description().contains(pattern, Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
// DefaultFilterModel
|
||||
|
||||
DefaultFilterModel::DefaultFilterModel(QObject *parent) :
|
||||
QStandardItemModel(0, 1, parent)
|
||||
{
|
||||
setHeaderData(1, Qt::Horizontal, i18n("Filters"));
|
||||
//This is to make QML that is understand it
|
||||
QHash<int, QByteArray> newRoleNames = roleNames();
|
||||
newRoleNames[FilterTypeRole] = "filterType";
|
||||
newRoleNames[FilterDataRole] = "filterData";
|
||||
newRoleNames[SeparatorRole] = "separator";
|
||||
|
||||
setRoleNames(newRoleNames);
|
||||
connect(this, SIGNAL(modelReset()),
|
||||
this, SIGNAL(countChanged()));
|
||||
connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)),
|
||||
this, SIGNAL(countChanged()));
|
||||
connect(this, SIGNAL(rowsRemoved(QModelIndex, int, int)),
|
||||
this, SIGNAL(countChanged()));
|
||||
}
|
||||
|
||||
void DefaultFilterModel::addFilter(const QString &caption, const Filter &filter, const KIcon &icon)
|
||||
{
|
||||
QList<QStandardItem *> newRow;
|
||||
QStandardItem *item = new QStandardItem(caption);
|
||||
item->setData(qVariantFromValue<Filter>(filter));
|
||||
if (!icon.isNull()) {
|
||||
item->setIcon(icon);
|
||||
}
|
||||
item->setData(filter.first, FilterTypeRole);
|
||||
item->setData(filter.second, FilterDataRole);
|
||||
|
||||
newRow << item;
|
||||
appendRow(newRow);
|
||||
}
|
||||
|
||||
void DefaultFilterModel::addSeparator(const QString &caption)
|
||||
{
|
||||
QList<QStandardItem *> newRow;
|
||||
QStandardItem *item = new QStandardItem(caption);
|
||||
item->setEnabled(false);
|
||||
item->setData(true, SeparatorRole);
|
||||
|
||||
newRow << item;
|
||||
appendRow(newRow);
|
||||
}
|
||||
|
||||
QVariantHash DefaultFilterModel::get(int row) const
|
||||
{
|
||||
QModelIndex idx = index(row, 0);
|
||||
QVariantHash hash;
|
||||
|
||||
QHash<int, QByteArray>::const_iterator i;
|
||||
for (i = roleNames().constBegin(); i != roleNames().constEnd(); ++i) {
|
||||
hash[i.value()] = data(idx, i.key());
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
// DefaultItemFilterProxyModel
|
||||
|
||||
DefaultItemFilterProxyModel::DefaultItemFilterProxyModel(QObject *parent)
|
||||
: QSortFilterProxyModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void DefaultItemFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
||||
{
|
||||
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(sourceModel);
|
||||
|
||||
if (!model) {
|
||||
kWarning() << "Expecting a QStandardItemModel!";
|
||||
return;
|
||||
}
|
||||
|
||||
setRoleNames(sourceModel->roleNames());
|
||||
|
||||
QSortFilterProxyModel::setSourceModel(model);
|
||||
connect(this, SIGNAL(modelReset()),
|
||||
this, SIGNAL(countChanged()));
|
||||
connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)),
|
||||
this, SIGNAL(countChanged()));
|
||||
connect(this, SIGNAL(rowsRemoved(QModelIndex, int, int)),
|
||||
this, SIGNAL(countChanged()));
|
||||
}
|
||||
|
||||
QAbstractItemModel *DefaultItemFilterProxyModel::sourceModel() const
|
||||
{
|
||||
return QSortFilterProxyModel::sourceModel();
|
||||
}
|
||||
|
||||
int DefaultItemFilterProxyModel::columnCount(const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(index);
|
||||
return COLUMN_COUNT;
|
||||
}
|
||||
|
||||
QVariant DefaultItemFilterProxyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
return QSortFilterProxyModel::data(index, role);
|
||||
}
|
||||
|
||||
bool DefaultItemFilterProxyModel::filterAcceptsRow(int sourceRow,
|
||||
const QModelIndex &sourceParent) const
|
||||
{
|
||||
QStandardItemModel *model = (QStandardItemModel *) sourceModel();
|
||||
|
||||
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
|
||||
|
||||
AbstractItem *item = (AbstractItem *) model->itemFromIndex(index);
|
||||
//kDebug() << "ITEM " << (item ? "IS NOT " : "IS") << " NULL\n";
|
||||
|
||||
return item &&
|
||||
(m_filter.first.isEmpty() || item->passesFiltering(m_filter)) &&
|
||||
(m_searchPattern.isEmpty() || item->matches(m_searchPattern));
|
||||
}
|
||||
|
||||
QVariantHash DefaultItemFilterProxyModel::get(int row) const
|
||||
{
|
||||
QModelIndex idx = index(row, 0);
|
||||
QVariantHash hash;
|
||||
|
||||
QHash<int, QByteArray>::const_iterator i;
|
||||
for (i = roleNames().constBegin(); i != roleNames().constEnd(); ++i) {
|
||||
hash[i.value()] = data(idx, i.key());
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
bool DefaultItemFilterProxyModel::lessThan(const QModelIndex &left,
|
||||
const QModelIndex &right) const
|
||||
{
|
||||
return
|
||||
sourceModel()->data(left).toString().localeAwareCompare(
|
||||
sourceModel()->data(right).toString()) < 0;
|
||||
}
|
||||
|
||||
void DefaultItemFilterProxyModel::setSearchTerm(const QString &pattern)
|
||||
{
|
||||
m_searchPattern = pattern;
|
||||
invalidateFilter();
|
||||
emit searchTermChanged(pattern);
|
||||
}
|
||||
|
||||
QString DefaultItemFilterProxyModel::searchTerm() const
|
||||
{
|
||||
return m_searchPattern;
|
||||
}
|
||||
|
||||
void DefaultItemFilterProxyModel::setFilter(const Filter &filter)
|
||||
{
|
||||
m_filter = filter;
|
||||
invalidateFilter();
|
||||
emit filterChanged();
|
||||
}
|
||||
|
||||
void DefaultItemFilterProxyModel::setFilterType(const QString type)
|
||||
{
|
||||
m_filter.first = type;
|
||||
invalidateFilter();
|
||||
emit filterChanged();
|
||||
}
|
||||
|
||||
QString DefaultItemFilterProxyModel::filterType() const
|
||||
{
|
||||
return m_filter.first;
|
||||
}
|
||||
|
||||
void DefaultItemFilterProxyModel::setFilterQuery(const QVariant query)
|
||||
{
|
||||
m_filter.second = query;
|
||||
invalidateFilter();
|
||||
emit filterChanged();
|
||||
}
|
||||
|
||||
QVariant DefaultItemFilterProxyModel::filterQuery() const
|
||||
{
|
||||
return m_filter.second;
|
||||
}
|
||||
|
||||
}
|
178
src/shell/widgetexplorer/kcategorizeditemsviewmodels_p.h
Normal file
178
src/shell/widgetexplorer/kcategorizeditemsviewmodels_p.h
Normal file
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library/Lesser General Public License
|
||||
* version 2, or (at your option) any later version, as published by the
|
||||
* Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details
|
||||
*
|
||||
* You should have received a copy of the GNU Library/Lesser General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef PLASMA_KCATEGORIZEDITEMSVIEWMODELS_P_H
|
||||
#define PLASMA_KCATEGORIZEDITEMSVIEWMODELS_P_H
|
||||
|
||||
#include <QtGui/QtGui>
|
||||
#include <QtCore/QtCore>
|
||||
#include <KIcon>
|
||||
#include <KDebug>
|
||||
|
||||
namespace KCategorizedItemsViewModels {
|
||||
|
||||
typedef QPair<QString, QVariant> Filter;
|
||||
|
||||
/**
|
||||
* Abstract class that needs to be implemented and used with the ItemModel
|
||||
*/
|
||||
class AbstractItem : public QStandardItem
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Returns a localized string - name of the item
|
||||
*/
|
||||
virtual QString name() const;
|
||||
|
||||
/**
|
||||
* Returns a unique id related to this item
|
||||
*/
|
||||
virtual QString id() const;
|
||||
|
||||
/**
|
||||
* Returns a localized string - description of the item
|
||||
*/
|
||||
virtual QString description() const;
|
||||
|
||||
/**
|
||||
* Returns if the item is flagged as favorite
|
||||
* Default implementation checks if the item passes the Filter("favorite", "1") filter
|
||||
*/
|
||||
virtual bool isFavorite() const;
|
||||
|
||||
/**
|
||||
* Returns the item's number of running applets
|
||||
* Default implementation just returns 0
|
||||
*/
|
||||
virtual int running() const;
|
||||
|
||||
/**
|
||||
* Returns if the item contains string specified by pattern.
|
||||
* Default implementation checks whether name or description contain the
|
||||
* string (not needed to be exactly that string)
|
||||
*/
|
||||
virtual bool matches(const QString &pattern) const;
|
||||
|
||||
/**
|
||||
* sets the favorite flag for the item
|
||||
*/
|
||||
virtual void setFavorite(bool favorite) = 0;
|
||||
/**
|
||||
* sets the number of running applets for the item
|
||||
*/
|
||||
virtual void setRunning(int count) = 0;
|
||||
|
||||
/**
|
||||
* Returns if the item passes the filter specified
|
||||
*/
|
||||
virtual bool passesFiltering(const Filter &filter) const = 0;
|
||||
private:
|
||||
};
|
||||
|
||||
/**
|
||||
* The default implementation of the model containing filters
|
||||
*/
|
||||
class DefaultFilterModel : public QStandardItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
||||
public:
|
||||
enum Roles {
|
||||
FilterTypeRole = Qt::UserRole+1,
|
||||
FilterDataRole = Qt::UserRole+2,
|
||||
SeparatorRole = Qt::UserRole+3
|
||||
};
|
||||
DefaultFilterModel(QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* Adds a filter to the model
|
||||
* @param caption The localized string to be displayed as a name of the filter
|
||||
* @param filter The filter structure
|
||||
*/
|
||||
void addFilter(const QString &caption, const Filter &filter, const KIcon &icon = KIcon());
|
||||
|
||||
/**
|
||||
* Adds a separator to the model
|
||||
* @param caption The localized string to be displayed as a name of the separator
|
||||
*/
|
||||
void addSeparator(const QString &caption);
|
||||
|
||||
int count() {return rowCount(QModelIndex());}
|
||||
|
||||
Q_INVOKABLE QVariantHash get(int i) const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void countChanged();
|
||||
};
|
||||
|
||||
/**
|
||||
* Default filter proxy model.
|
||||
*/
|
||||
class DefaultItemFilterProxyModel : public QSortFilterProxyModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString searchTerm READ searchTerm WRITE setSearchTerm NOTIFY searchTermChanged)
|
||||
Q_PROPERTY(QString filterType READ filterType WRITE setFilterType NOTIFY filterChanged)
|
||||
Q_PROPERTY(QVariant filterQuery READ filterQuery WRITE setFilterQuery NOTIFY filterChanged)
|
||||
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
||||
|
||||
public:
|
||||
DefaultItemFilterProxyModel(QObject *parent = 0);
|
||||
|
||||
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
|
||||
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
|
||||
|
||||
void setSearchTerm(const QString &pattern);
|
||||
QString searchTerm() const;
|
||||
|
||||
void setFilterType(const QString type);
|
||||
QString filterType() const;
|
||||
|
||||
void setFilterQuery(const QVariant query);
|
||||
QVariant filterQuery() const;
|
||||
|
||||
void setFilter(const Filter &filter);
|
||||
|
||||
void setSourceModel(QAbstractItemModel *sourceModel);
|
||||
|
||||
QAbstractItemModel *sourceModel() const;
|
||||
|
||||
int columnCount(const QModelIndex &index) const;
|
||||
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||
|
||||
int count() {return rowCount(QModelIndex());}
|
||||
|
||||
Q_INVOKABLE QVariantHash get(int i) const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void searchTermChanged(const QString &term);
|
||||
void filterChanged();
|
||||
void countChanged();
|
||||
|
||||
private:
|
||||
Filter m_filter;
|
||||
QString m_searchPattern;
|
||||
};
|
||||
|
||||
} //end of namespace
|
||||
|
||||
Q_DECLARE_METATYPE(KCategorizedItemsViewModels::Filter)
|
||||
|
||||
#endif /*KCATEGORIZEDITEMSVIEWMODELS_H_*/
|
351
src/shell/widgetexplorer/plasmaappletitemmodel.cpp
Normal file
351
src/shell/widgetexplorer/plasmaappletitemmodel.cpp
Normal file
@ -0,0 +1,351 @@
|
||||
/*
|
||||
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library/Lesser General Public License
|
||||
* version 2, or (at your option) any later version, as published by the
|
||||
* Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details
|
||||
*
|
||||
* You should have received a copy of the GNU Library/Lesser General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "plasmaappletitemmodel_p.h"
|
||||
|
||||
#include <KStandardDirs>
|
||||
#include <KSycoca>
|
||||
|
||||
PlasmaAppletItem::PlasmaAppletItem(PlasmaAppletItemModel *model,
|
||||
const KPluginInfo& info,
|
||||
FilterFlags flags)
|
||||
: QObject(model),
|
||||
m_model(model),
|
||||
m_info(info),
|
||||
m_runningCount(0),
|
||||
m_favorite(flags & Favorite),
|
||||
m_local(false)
|
||||
{
|
||||
const QString api(m_info.property("X-Plasma-API").toString());
|
||||
if (!api.isEmpty()) {
|
||||
QDir dir(KStandardDirs::locateLocal("data", "plasma/plasmoids/" + info.pluginName() + '/', false));
|
||||
m_local = dir.exists();
|
||||
}
|
||||
|
||||
//attrs.insert("recommended", flags & Recommended ? true : false);
|
||||
setText(m_info.name() + " - "+ m_info.category().toLower());
|
||||
|
||||
const QString iconName = m_info.icon().isEmpty() ? "application-x-plasma" : info.icon();
|
||||
KIcon icon(iconName);
|
||||
setIcon(icon);
|
||||
|
||||
//set plugininfo parts as roles in the model, only way qml can understand it
|
||||
setData(info.name(), PlasmaAppletItemModel::NameRole);
|
||||
setData(info.pluginName(), PlasmaAppletItemModel::PluginNameRole);
|
||||
setData(info.comment(), PlasmaAppletItemModel::DescriptionRole);
|
||||
setData(info.category().toLower(), PlasmaAppletItemModel::CategoryRole);
|
||||
setData(info.fullLicense().name(KAboutData::FullName), PlasmaAppletItemModel::LicenseRole);
|
||||
setData(info.website(), PlasmaAppletItemModel::WebsiteRole);
|
||||
setData(info.version(), PlasmaAppletItemModel::VersionRole);
|
||||
setData(info.author(), PlasmaAppletItemModel::AuthorRole);
|
||||
setData(info.email(), PlasmaAppletItemModel::EmailRole);
|
||||
setData(0, PlasmaAppletItemModel::RunningRole);
|
||||
setData(m_local, PlasmaAppletItemModel::LocalRole);
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::pluginName() const
|
||||
{
|
||||
return m_info.pluginName();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::name() const
|
||||
{
|
||||
return m_info.name();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::description() const
|
||||
{
|
||||
return m_info.comment();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::license() const
|
||||
{
|
||||
return m_info.license();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::category() const
|
||||
{
|
||||
return m_info.category();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::website() const
|
||||
{
|
||||
return m_info.website();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::version() const
|
||||
{
|
||||
return m_info.version();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::author() const
|
||||
{
|
||||
return m_info.author();
|
||||
}
|
||||
|
||||
QString PlasmaAppletItem::email() const
|
||||
{
|
||||
return m_info.email();
|
||||
}
|
||||
|
||||
int PlasmaAppletItem::running() const
|
||||
{
|
||||
return m_runningCount;
|
||||
}
|
||||
|
||||
void PlasmaAppletItem::setRunning(int count)
|
||||
{
|
||||
m_runningCount = count;
|
||||
setData(count, PlasmaAppletItemModel::RunningRole);
|
||||
emitDataChanged();
|
||||
}
|
||||
|
||||
bool PlasmaAppletItem::matches(const QString &pattern) const
|
||||
{
|
||||
if (m_info.service()) {
|
||||
const QStringList keywords = m_info.service()->keywords();
|
||||
foreach (const QString &keyword, keywords) {
|
||||
if (keyword.startsWith(pattern, Qt::CaseInsensitive)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return AbstractItem::matches(pattern);
|
||||
}
|
||||
|
||||
bool PlasmaAppletItem::isFavorite() const
|
||||
{
|
||||
return m_favorite;
|
||||
}
|
||||
|
||||
void PlasmaAppletItem::setFavorite(bool favorite)
|
||||
{
|
||||
if (m_favorite != favorite) {
|
||||
m_favorite = favorite;
|
||||
m_model->setFavorite(m_info.pluginName(), favorite);
|
||||
emitDataChanged();
|
||||
}
|
||||
}
|
||||
|
||||
bool PlasmaAppletItem::isLocal() const
|
||||
{
|
||||
return m_local;
|
||||
}
|
||||
|
||||
bool PlasmaAppletItem::passesFiltering(const KCategorizedItemsViewModels::Filter &filter) const
|
||||
{
|
||||
if (filter.first == "running") {
|
||||
return running();
|
||||
} else if (filter.first == "category") {
|
||||
return m_info.category().toLower() == filter.second;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QMimeData *PlasmaAppletItem::mimeData() const
|
||||
{
|
||||
QMimeData *data = new QMimeData();
|
||||
QByteArray appletName;
|
||||
appletName += pluginName().toUtf8();
|
||||
data->setData(mimeTypes().at(0), appletName);
|
||||
return data;
|
||||
}
|
||||
|
||||
QStringList PlasmaAppletItem::mimeTypes() const
|
||||
{
|
||||
QStringList types;
|
||||
types << QLatin1String("text/x-plasmoidservicename");
|
||||
return types;
|
||||
}
|
||||
|
||||
PlasmaAppletItemModel* PlasmaAppletItem::appletItemModel()
|
||||
{
|
||||
return m_model;
|
||||
}
|
||||
|
||||
//PlasmaAppletItemModel
|
||||
|
||||
PlasmaAppletItemModel::PlasmaAppletItemModel(QObject * parent)
|
||||
: QStandardItemModel(parent)
|
||||
{
|
||||
KConfig config("plasmarc");
|
||||
m_configGroup = KConfigGroup(&config, "Applet Browser");
|
||||
m_favorites = m_configGroup.readEntry("favorites").split(',');
|
||||
connect(KSycoca::self(), SIGNAL(databaseChanged(QStringList)), this, SLOT(populateModel(QStringList)));
|
||||
|
||||
//This is to make QML that is understand it
|
||||
QHash<int, QByteArray> newRoleNames = roleNames();
|
||||
newRoleNames[NameRole] = "name";
|
||||
newRoleNames[PluginNameRole] = "pluginName";
|
||||
newRoleNames[DescriptionRole] = "description";
|
||||
newRoleNames[CategoryRole] = "category";
|
||||
newRoleNames[LicenseRole] = "license";
|
||||
newRoleNames[WebsiteRole] = "website";
|
||||
newRoleNames[VersionRole] = "version";
|
||||
newRoleNames[AuthorRole] = "author";
|
||||
newRoleNames[EmailRole] = "email";
|
||||
newRoleNames[RunningRole] = "running";
|
||||
newRoleNames[LocalRole] = "local";
|
||||
|
||||
setRoleNames(newRoleNames);
|
||||
|
||||
setSortRole(Qt::DisplayRole);
|
||||
}
|
||||
|
||||
void PlasmaAppletItemModel::populateModel(const QStringList &whatChanged)
|
||||
{
|
||||
if (!whatChanged.isEmpty() && !whatChanged.contains("services")) {
|
||||
return;
|
||||
}
|
||||
|
||||
clear();
|
||||
//kDebug() << "populating model, our application is" << m_application;
|
||||
|
||||
//kDebug() << "number of applets is"
|
||||
// << Plasma::Applet::listAppletInfo(QString(), m_application).count();
|
||||
foreach (const KPluginInfo &info, Plasma::Applet::listAppletInfo(QString(), m_application)) {
|
||||
//kDebug() << info.pluginName() << "NoDisplay" << info.property("NoDisplay").toBool();
|
||||
if (info.property("NoDisplay").toBool() || info.category() == i18n("Containments")) {
|
||||
// we don't want to show the hidden category
|
||||
continue;
|
||||
}
|
||||
|
||||
//kDebug() << info.pluginName() << " is the name of the plugin at" << info.entryPath();
|
||||
//kDebug() << info.name() << info.property("X-Plasma-Thumbnail");
|
||||
|
||||
PlasmaAppletItem::FilterFlags flags(PlasmaAppletItem::NoFilter);
|
||||
if (m_favorites.contains(info.pluginName())) {
|
||||
flags |= PlasmaAppletItem::Favorite;
|
||||
}
|
||||
|
||||
appendRow(new PlasmaAppletItem(this, info, flags));
|
||||
}
|
||||
|
||||
emit modelPopulated();
|
||||
}
|
||||
|
||||
void PlasmaAppletItemModel::setRunningApplets(const QHash<QString, int> &apps)
|
||||
{
|
||||
//foreach item, find that string and set the count
|
||||
for (int r = 0; r < rowCount(); ++r) {
|
||||
QStandardItem *i = item(r);
|
||||
PlasmaAppletItem *p = dynamic_cast<PlasmaAppletItem *>(i);
|
||||
|
||||
if (p) {
|
||||
const bool running = apps.value(p->pluginName());
|
||||
p->setRunning(running);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlasmaAppletItemModel::setRunningApplets(const QString &name, int count)
|
||||
{
|
||||
for (int r=0; r<rowCount(); ++r) {
|
||||
QStandardItem *i = item(r);
|
||||
PlasmaAppletItem *p = dynamic_cast<PlasmaAppletItem *>(i);
|
||||
if (p && p->pluginName() == name) {
|
||||
p->setRunning(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QStringList PlasmaAppletItemModel::mimeTypes() const
|
||||
{
|
||||
QStringList types;
|
||||
types << QLatin1String("text/x-plasmoidservicename");
|
||||
return types;
|
||||
}
|
||||
|
||||
QSet<QString> PlasmaAppletItemModel::categories() const
|
||||
{
|
||||
QSet<QString> cats;
|
||||
for (int r = 0; r < rowCount(); ++r) {
|
||||
QStandardItem *i = item(r);
|
||||
PlasmaAppletItem *p = dynamic_cast<PlasmaAppletItem *>(i);
|
||||
if (p) {
|
||||
cats.insert(p->category().toLower());
|
||||
}
|
||||
}
|
||||
|
||||
return cats;
|
||||
}
|
||||
|
||||
QMimeData *PlasmaAppletItemModel::mimeData(const QModelIndexList &indexes) const
|
||||
{
|
||||
//kDebug() << "GETTING MIME DATA\n";
|
||||
if (indexes.count() <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
QStringList types = mimeTypes();
|
||||
|
||||
if (types.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
QMimeData *data = new QMimeData();
|
||||
|
||||
QString format = types.at(0);
|
||||
|
||||
QByteArray appletNames;
|
||||
int lastRow = -1;
|
||||
foreach (const QModelIndex &index, indexes) {
|
||||
if (index.row() == lastRow) {
|
||||
continue;
|
||||
}
|
||||
|
||||
lastRow = index.row();
|
||||
PlasmaAppletItem *selectedItem = (PlasmaAppletItem *) itemFromIndex(index);
|
||||
appletNames += '\n' + selectedItem->pluginName().toUtf8();
|
||||
//kDebug() << selectedItem->pluginName() << index.column() << index.row();
|
||||
}
|
||||
|
||||
data->setData(format, appletNames);
|
||||
return data;
|
||||
}
|
||||
|
||||
void PlasmaAppletItemModel::setFavorite(const QString &plugin, bool favorite)
|
||||
{
|
||||
if (favorite) {
|
||||
if (!m_favorites.contains(plugin)) {
|
||||
m_favorites.append(plugin);
|
||||
}
|
||||
} else {
|
||||
m_favorites.removeAll(plugin);
|
||||
}
|
||||
|
||||
m_configGroup.writeEntry("favorites", m_favorites.join(","));
|
||||
m_configGroup.sync();
|
||||
}
|
||||
|
||||
void PlasmaAppletItemModel::setApplication(const QString &app)
|
||||
{
|
||||
m_application = app;
|
||||
populateModel();
|
||||
}
|
||||
|
||||
QString &PlasmaAppletItemModel::Application()
|
||||
{
|
||||
return m_application;
|
||||
}
|
||||
|
||||
//#include <plasmaappletitemmodel_p.moc>
|
||||
|
124
src/shell/widgetexplorer/plasmaappletitemmodel_p.h
Normal file
124
src/shell/widgetexplorer/plasmaappletitemmodel_p.h
Normal file
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library/Lesser General Public License
|
||||
* version 2, or (at your option) any later version, as published by the
|
||||
* Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details
|
||||
*
|
||||
* You should have received a copy of the GNU Library/Lesser General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef PLASMA_PLASMAAPPLETITEMMODEL_P_H
|
||||
#define PLASMA_PLASMAAPPLETITEMMODEL_P_H
|
||||
|
||||
#include <kplugininfo.h>
|
||||
#include <Plasma/Applet>
|
||||
#include "kcategorizeditemsviewmodels_p.h"
|
||||
|
||||
class PlasmaAppletItemModel;
|
||||
|
||||
/**
|
||||
* Implementation of the KCategorizedItemsViewModels::AbstractItem
|
||||
*/
|
||||
class PlasmaAppletItem : public QObject, public KCategorizedItemsViewModels::AbstractItem
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum FilterFlag {
|
||||
NoFilter = 0,
|
||||
Favorite = 1
|
||||
};
|
||||
|
||||
Q_DECLARE_FLAGS(FilterFlags, FilterFlag)
|
||||
|
||||
PlasmaAppletItem(PlasmaAppletItemModel *model, const KPluginInfo& info, FilterFlags flags = NoFilter);
|
||||
|
||||
QString pluginName() const;
|
||||
QString name() const;
|
||||
QString category() const;
|
||||
QString description() const;
|
||||
QString license() const;
|
||||
QString website() const;
|
||||
QString version() const;
|
||||
QString author() const;
|
||||
QString email() const;
|
||||
|
||||
int running() const;
|
||||
bool isLocal() const;
|
||||
bool isFavorite() const;
|
||||
void setFavorite(bool favorite);
|
||||
PlasmaAppletItemModel* appletItemModel();
|
||||
bool matches(const QString &pattern) const;
|
||||
|
||||
//set how many instances of this applet are running
|
||||
void setRunning(int count);
|
||||
bool passesFiltering(const KCategorizedItemsViewModels::Filter & filter) const;
|
||||
QMimeData *mimeData() const;
|
||||
QStringList mimeTypes() const;
|
||||
|
||||
private:
|
||||
PlasmaAppletItemModel * m_model;
|
||||
KPluginInfo m_info;
|
||||
int m_runningCount;
|
||||
bool m_favorite;
|
||||
bool m_local;
|
||||
};
|
||||
|
||||
class PlasmaAppletItemModel : public QStandardItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
NameRole = Qt::UserRole+1,
|
||||
PluginNameRole = Qt::UserRole+2,
|
||||
DescriptionRole = Qt::UserRole+3,
|
||||
CategoryRole = Qt::UserRole+4,
|
||||
LicenseRole = Qt::UserRole+5,
|
||||
WebsiteRole = Qt::UserRole+6,
|
||||
VersionRole = Qt::UserRole+7,
|
||||
AuthorRole = Qt::UserRole+8,
|
||||
EmailRole = Qt::UserRole+9,
|
||||
RunningRole = Qt::UserRole+10,
|
||||
LocalRole = Qt::UserRole+11
|
||||
};
|
||||
|
||||
explicit PlasmaAppletItemModel(QObject * parent = 0);
|
||||
|
||||
QStringList mimeTypes() const;
|
||||
QSet<QString> categories() const;
|
||||
|
||||
QMimeData *mimeData(const QModelIndexList &indexes) const;
|
||||
|
||||
void setFavorite(const QString &plugin, bool favorite);
|
||||
void setApplication(const QString &app);
|
||||
void setRunningApplets(const QHash<QString, int> &apps);
|
||||
void setRunningApplets(const QString &name, int count);
|
||||
|
||||
QString &Application();
|
||||
|
||||
Q_SIGNALS:
|
||||
void modelPopulated();
|
||||
|
||||
private:
|
||||
QString m_application;
|
||||
QStringList m_favorites;
|
||||
KConfigGroup m_configGroup;
|
||||
|
||||
private slots:
|
||||
void populateModel(const QStringList &whatChanged = QStringList());
|
||||
};
|
||||
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS(PlasmaAppletItem::FilterFlags)
|
||||
|
||||
#endif /*PLASMAAPPLETSMODEL_H_*/
|
Loading…
x
Reference in New Issue
Block a user