diff --git a/abstractmodel.cpp b/abstractmodel.cpp index 8bfdc097c..1d9cd342a 100644 --- a/abstractmodel.cpp +++ b/abstractmodel.cpp @@ -1,144 +1,164 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, 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 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 "abstractmodel.h" #include "actionlist.h" AbstractModel::AbstractModel(QObject *parent) : QAbstractListModel(parent) +, m_favoritesModel(nullptr) , m_iconSize(32) { } AbstractModel::~AbstractModel() { } QHash AbstractModel::roleNames() const { QHash roles; roles.insert(Qt::DisplayRole, "display"); roles.insert(Qt::DecorationRole, "decoration"); roles.insert(Kicker::GroupRole, "group"); roles.insert(Kicker::DescriptionRole, "description"); roles.insert(Kicker::FavoriteIdRole, "favoriteId"); roles.insert(Kicker::IsParentRole, "isParent"); roles.insert(Kicker::IsSeparatorRole, "isSeparator"); roles.insert(Kicker::HasChildrenRole, "hasChildren"); roles.insert(Kicker::HasActionListRole, "hasActionList"); roles.insert(Kicker::ActionListRole, "actionList"); roles.insert(Kicker::UrlRole, "url"); return roles; } int AbstractModel::count() const { return rowCount(); } int AbstractModel::separatorCount() const { return 0; } int AbstractModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) { return 0; } return 1; } int AbstractModel::iconSize() const { return m_iconSize; } void AbstractModel::setIconSize(int iconSize) { if (m_iconSize != iconSize) { m_iconSize = iconSize; refresh(); } } void AbstractModel::refresh() { } QString AbstractModel::labelForRow(int row) { return data(index(row, 0), Qt::DisplayRole).toString(); } AbstractModel *AbstractModel::modelForRow(int row) { Q_UNUSED(row) return 0; } int AbstractModel::rowForModel(AbstractModel *model) { Q_UNUSED(model) return -1; } bool AbstractModel::hasActions() const { return false; } QVariantList AbstractModel::actions() const { return QVariantList(); } AbstractModel* AbstractModel::favoritesModel() { - return rootModel()->favoritesModel(); + if (m_favoritesModel) { + return m_favoritesModel; + } else { + AbstractModel *model = rootModel(); + + if (model) { + return model->favoritesModel(); + } + } + + return nullptr; +} + +void AbstractModel::setFavoritesModel(AbstractModel *model) +{ + if (m_favoritesModel != model) { + m_favoritesModel = model; + + emit favoritesModelChanged(); + } } AbstractModel* AbstractModel::rootModel() { if (!parent()) { return nullptr; } QObject *p = this; AbstractModel *rootModel = nullptr; while (p) { if (qobject_cast(p)) { rootModel = qobject_cast(p); } else { return rootModel; } p = p->parent(); } return rootModel; } void AbstractModel::entryChanged(AbstractEntry *entry) { Q_UNUSED(entry) } diff --git a/abstractmodel.h b/abstractmodel.h index 029e8fc2e..c34b6033b 100644 --- a/abstractmodel.h +++ b/abstractmodel.h @@ -1,81 +1,86 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, 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 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 ABSTRACTMODEL_H #define ABSTRACTMODEL_H #include class AbstractEntry; class AbstractModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(QString description READ description NOTIFY descriptionChanged) Q_PROPERTY(int count READ count NOTIFY countChanged) Q_PROPERTY(int separatorCount READ separatorCount NOTIFY separatorCountChanged) Q_PROPERTY(int iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged) - Q_PROPERTY(QObject* favoritesModel READ favoritesModel CONSTANT) + Q_PROPERTY(AbstractModel* favoritesModel READ favoritesModel WRITE setFavoritesModel NOTIFY favoritesModelChanged) public: explicit AbstractModel(QObject *parent = 0); ~AbstractModel(); virtual QHash roleNames() const; virtual QString description() const = 0; int count() const; virtual int separatorCount() const; int columnCount(const QModelIndex &parent = QModelIndex()) const; int iconSize() const; void setIconSize(int size); Q_INVOKABLE virtual bool trigger(int row, const QString &actionId, const QVariant &argument) = 0; Q_INVOKABLE virtual void refresh(); Q_INVOKABLE virtual QString labelForRow(int row); Q_INVOKABLE virtual AbstractModel *modelForRow(int row); Q_INVOKABLE virtual int rowForModel(AbstractModel *model); virtual bool hasActions() const; virtual QVariantList actions() const; virtual AbstractModel* favoritesModel(); + virtual void setFavoritesModel(AbstractModel *model); AbstractModel* rootModel(); virtual void entryChanged(AbstractEntry *entry); Q_SIGNALS: void descriptionChanged() const; void countChanged() const; void separatorCountChanged() const; void iconSizeChanged() const; + void favoritesModelChanged() const; + + protected: + AbstractModel *m_favoritesModel; private: int m_iconSize; }; #endif diff --git a/systemmodel.cpp b/systemmodel.cpp index 209d030f7..0e7b3be1b 100644 --- a/systemmodel.cpp +++ b/systemmodel.cpp @@ -1,136 +1,132 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, 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 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 "systemmodel.h" #include "actionlist.h" #include "favoritesmodel.h" #include "systementry.h" #include #include #include SystemModel::SystemModel(QObject *parent) : AbstractModel(parent) -, m_favoritesModel(new FavoritesModel(this)) { init(); + m_favoritesModel = new FavoritesModel(this); + const QString configFile = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/ksmserverrc"; KDirWatch *watch = new KDirWatch(this); watch->addFile(configFile); connect(watch, &KDirWatch::dirty, this, &SystemModel::refresh); connect(watch, &KDirWatch::created, this, &SystemModel::refresh); } SystemModel::~SystemModel() { qDeleteAll(m_entryList); } void SystemModel::init() { QList actions; actions << new SystemEntry(this, SystemEntry::LockSession); actions << new SystemEntry(this, SystemEntry::LogoutSession); actions << new SystemEntry(this, SystemEntry::SaveSession); actions << new SystemEntry(this, SystemEntry::NewSession); actions << new SystemEntry(this, SystemEntry::SuspendToRam); actions << new SystemEntry(this, SystemEntry::SuspendToDisk); actions << new SystemEntry(this, SystemEntry::Reboot); actions << new SystemEntry(this, SystemEntry::Shutdown); foreach(SystemEntry *entry, actions) { if (entry->isValid()) { m_entryList << entry; } else { delete entry; } } } QString SystemModel::description() const { return i18n("System actions"); } QVariant SystemModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= m_entryList.count()) { return QVariant(); } const SystemEntry *entry = m_entryList.at(index.row()); if (role == Qt::DisplayRole) { return entry->name(); } else if (role == Qt::DecorationRole) { return entry->iconName(); } else if (role == Kicker::GroupRole) { return entry->group(); } else if (role == Kicker::FavoriteIdRole) { return entry->id(); } return QVariant(); } int SystemModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : m_entryList.count(); } bool SystemModel::trigger(int row, const QString &actionId, const QVariant &argument) { Q_UNUSED(actionId) Q_UNUSED(argument) if (row >= 0 && row < m_entryList.count()) { m_entryList.at(row)->run(); return true; } return false; } -AbstractModel* SystemModel::favoritesModel() -{ - return m_favoritesModel; -} - void SystemModel::refresh() { beginResetModel(); qDeleteAll(m_entryList); m_entryList.clear(); init(); endResetModel(); emit countChanged(); m_favoritesModel->refresh(); } diff --git a/systemmodel.h b/systemmodel.h index 1b3eab79a..2319ee498 100644 --- a/systemmodel.h +++ b/systemmodel.h @@ -1,55 +1,52 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, 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 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 SYSTEMMODEL_H #define SYSTEMMODEL_H #include "abstractmodel.h" class SystemEntry; class SystemModel : public AbstractModel { Q_OBJECT public: explicit SystemModel(QObject *parent = 0); ~SystemModel(); QString description() const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE bool trigger(int row, const QString &actionId, const QVariant &argument); - AbstractModel* favoritesModel(); - protected Q_SLOTS: virtual void refresh(); private: void init(); QList m_entryList; - AbstractModel *m_favoritesModel; }; #endif