proxy model with first 4 items used as favorites

This commit is contained in:
Marco Martin 2015-04-09 14:58:49 +02:00
parent 5730bae979
commit 5d4061efcf
6 changed files with 103 additions and 0 deletions

View file

@ -3,6 +3,7 @@ project(satellitecomponents)
set(satellitecomponents_SRCS
satellitecomponentsplugin.cpp
applicationlistmodel.cpp
favoritesmodel.cpp
)
add_library(satellitecomponentsplugin SHARED ${satellitecomponents_SRCS})

View file

@ -19,6 +19,7 @@
// Self
#include "applicationlistmodel.h"
#include "favoritesmodel.h"
// Qt
#include <QByteArray>
@ -37,6 +38,9 @@
ApplicationListModel::ApplicationListModel(QObject *parent)
: QAbstractListModel(parent)
{
m_favoritesModel = new FavoritesModel(this);
m_favoritesModel->setSourceModel(this);
//can't use the new syntax as this signal is overloaded
connect(KSycoca::self(), SIGNAL(databaseChanged(const QStringList &)),
this, SLOT(sycocaDbChanged(const QStringList &)));
@ -46,6 +50,11 @@ ApplicationListModel::~ApplicationListModel()
{
}
FavoritesModel *ApplicationListModel::favoritesModel()
{
return m_favoritesModel;
}
QHash<int, QByteArray> ApplicationListModel::roleNames() const
{
QHash<int, QByteArray> roleNames;

View file

@ -27,6 +27,8 @@
class QString;
class FavoritesModel;
struct ApplicationData {
QString name;
QString icon;
@ -39,11 +41,13 @@ class ApplicationListModel : public QAbstractListModel {
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_PROPERTY(QStringList appOrder READ appOrder WRITE setAppOrder NOTIFY appOrderChanged)
Q_PROPERTY(FavoritesModel *favoritesModel READ favoritesModel CONSTANT)
public:
ApplicationListModel(QObject *parent = 0);
virtual ~ApplicationListModel();
FavoritesModel *favoritesModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int count() { return m_applicationList.count(); }
@ -81,6 +85,7 @@ private:
QStringList m_appOrder;
QHash<QString, int> m_appPositions;
FavoritesModel *m_favoritesModel;
};
#endif // APPLICATIONLISTMODEL_H

View file

@ -0,0 +1,38 @@
/*
* Copyright 2015 by Marco Martin <mart@kde.org>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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 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 "favoritesmodel.h"
#include <QDebug>
FavoritesModel::FavoritesModel(QObject *parent)
: QIdentityProxyModel(parent)
{
}
FavoritesModel::~FavoritesModel()
{
}
QHash<int, QByteArray> FavoritesModel::roleNames() const
{
return sourceModel()->roleNames();
}
#include "moc_favoritesmodel.cpp"

View file

@ -0,0 +1,48 @@
/*
* Copyright 2015 by Marco MArtin <mart@kde.org>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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 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 FAVORITESMODEL_H
#define FAVORITESMODEL_H
#include <QIdentityProxyModel>
class QTimer;
class FavoritesModel : public QIdentityProxyModel
{
Q_OBJECT
public:
FavoritesModel(QObject *parent = 0);
~FavoritesModel();
QHash<int, QByteArray> roleNames() const;
int count() const
{
return qMin(rowCount(), 4);
}
private:
};
#endif

View file

@ -28,6 +28,7 @@
#include <kdeclarative/kdeclarative.h>
#include "applicationlistmodel.h"
#include "favoritesmodel.h"
void SatelliteComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
@ -45,6 +46,7 @@ void SatelliteComponentsPlugin::registerTypes(const char *uri)
Q_ASSERT(uri == QLatin1String("org.kde.satellite.components"));
qmlRegisterType<ApplicationListModel>(uri, 0, 1, "ApplicationListModel");
qmlRegisterType<FavoritesModel>();
}