From 62de2cb1bed45cb054619aa33f0cc26ef16b38de Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Fri, 23 Aug 2019 16:37:21 +0200 Subject: [PATCH] working load and save of order and favs --- .../homescreen2/applicationlistmodel.cpp | 54 +++++++------------ .../homescreen2/applicationlistmodel.h | 14 +++-- containments/homescreen2/homescreen.cpp | 5 +- containments/homescreen2/homescreen.h | 2 +- .../package/contents/config/main.xml | 8 ++- 5 files changed, 34 insertions(+), 49 deletions(-) diff --git a/containments/homescreen2/applicationlistmodel.cpp b/containments/homescreen2/applicationlistmodel.cpp index 4df94f88..b3ef771b 100644 --- a/containments/homescreen2/applicationlistmodel.cpp +++ b/containments/homescreen2/applicationlistmodel.cpp @@ -37,12 +37,20 @@ #include #include -ApplicationListModel::ApplicationListModel(QObject *parent) - : QAbstractListModel(parent) +ApplicationListModel::ApplicationListModel(HomeScreen *parent) + : QAbstractListModel(parent), + m_homeScreen(parent) { //can't use the new syntax as this signal is overloaded connect(KSycoca::self(), SIGNAL(databaseChanged(const QStringList &)), this, SLOT(sycocaDbChanged(const QStringList &))); + m_favorites = m_homeScreen->config().readEntry("Favorites", QStringList()); + m_appOrder = m_homeScreen->config().readEntry("AppOrder", QStringList()); + int i = 0; + for (auto app : m_appOrder) { + m_appPositions[app] = i; + ++i; + } //here or delayed? loadApplications(); } @@ -104,8 +112,6 @@ void ApplicationListModel::loadApplications() int i = 0; // for default bookmarks - m_favoriteCount = 0; - // Iterate over all entries in the group while (!subGroupList.isEmpty()) { KSycocaEntry::Ptr groupEntry = subGroupList.first(); @@ -142,20 +148,16 @@ void ApplicationListModel::loadApplications() data.storageId = service->storageId(); data.entryPath = service->exec(); + if (m_favorites.contains(data.storageId)) { + data.location = Favorites; + } + auto it = m_appPositions.constFind(service->storageId()); if (it != m_appPositions.constEnd()) { - //TODO: proper bookmarks - data.location = (*it) < 6 ? Favorites : Grid; orderedList[*it] = data; } else { - //TODO: proper bookmarks - data.location = (++i + m_appPositions.size() < 6) ? Favorites : Grid; unorderedList << data; } - if (data.location == Favorites) { - ++m_favoriteCount; - } - emit favoriteCountChanged(); } } } @@ -234,10 +236,12 @@ void ApplicationListModel::setLocation(int row, LauncherLocation location) } if (location == Favorites) { - ++m_favoriteCount; + m_favorites.insert(row, data.storageId); + m_homeScreen->config().writeEntry("Favorites", m_favorites); emit favoriteCountChanged(); } else if (data.location == Favorites) { - m_favoriteCount = qMax(0, m_favoriteCount - 1); + m_favorites.removeAll(data.storageId); + m_homeScreen->config().writeEntry("Favorites", m_favorites); emit favoriteCountChanged(); } @@ -276,8 +280,8 @@ void ApplicationListModel::moveItem(int row, int destination) ++i; } + m_homeScreen->config().writeEntry("AppOrder", m_appOrder); - emit appOrderChanged(); endMoveRows(); } @@ -292,23 +296,5 @@ void ApplicationListModel::runApplication(const QString &storageId) KRun::runService(*service, QList(), nullptr); } -QStringList ApplicationListModel::appOrder() const -{ - return m_appOrder; -} +#include "moc_applicationlistmodel.cpp" -void ApplicationListModel::setAppOrder(const QStringList &order) -{ - if (m_appOrder == order) { - return; - } - - m_appOrder = order; - m_appPositions.clear(); - int i = 0; - for (auto app : m_appOrder) { - m_appPositions[app] = i; - ++i; - } - emit appOrderChanged(); -} diff --git a/containments/homescreen2/applicationlistmodel.h b/containments/homescreen2/applicationlistmodel.h index e1419f79..05bb3986 100644 --- a/containments/homescreen2/applicationlistmodel.h +++ b/containments/homescreen2/applicationlistmodel.h @@ -25,6 +25,8 @@ #include #include +#include "homescreen.h" + class QString; class ApplicationListModel; @@ -42,7 +44,6 @@ class ApplicationListModel : public QAbstractListModel { Q_PROPERTY(int count READ count NOTIFY countChanged) Q_PROPERTY(int favoriteCount READ favoriteCount NOTIFY favoriteCountChanged) - Q_PROPERTY(QStringList appOrder READ appOrder WRITE setAppOrder NOTIFY appOrderChanged) public: enum LauncherLocation { @@ -61,7 +62,7 @@ public: ApplicationLocationRole }; - ApplicationListModel(QObject *parent = nullptr); + ApplicationListModel(HomeScreen *parent = nullptr); ~ApplicationListModel() override; int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; @@ -69,7 +70,7 @@ public: void moveRow(const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild); int count() const { return m_applicationList.count(); } - int favoriteCount() const { return m_favoriteCount;} + int favoriteCount() const { return m_favorites.count();} QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; @@ -77,9 +78,6 @@ public: QHash roleNames() const Q_DECL_OVERRIDE; - QStringList appOrder() const; - void setAppOrder(const QStringList &order); - Q_INVOKABLE void setLocation(int row, LauncherLocation location); Q_INVOKABLE void moveItem(int row, int order); @@ -94,14 +92,14 @@ public Q_SLOTS: Q_SIGNALS: void countChanged(); void favoriteCountChanged(); - void appOrderChanged(); private: QList m_applicationList; + HomeScreen *m_homeScreen = nullptr; QStringList m_appOrder; + QStringList m_favorites; QHash m_appPositions; - int m_favoriteCount = 0; }; #endif // APPLICATIONLISTMODEL_H diff --git a/containments/homescreen2/homescreen.cpp b/containments/homescreen2/homescreen.cpp index 567ee8c0..8932d62f 100644 --- a/containments/homescreen2/homescreen.cpp +++ b/containments/homescreen2/homescreen.cpp @@ -28,7 +28,7 @@ HomeScreen::HomeScreen(QObject *parent, const QVariantList &args) : Plasma::Containment(parent, args) { qmlRegisterUncreatableType("org.kde.phone.homescreen", 1, 0, "ApplicationListModel", QStringLiteral("Cannot create item of type ApplicationListModel")); - m_applicationListModel = new ApplicationListModel(this); + setHasConfigurationInterface(true); } @@ -37,6 +37,9 @@ HomeScreen::~HomeScreen() ApplicationListModel *HomeScreen::applicationListModel() { + if (!m_applicationListModel) { + m_applicationListModel = new ApplicationListModel(this); + } return m_applicationListModel; } diff --git a/containments/homescreen2/homescreen.h b/containments/homescreen2/homescreen.h index 0bff3256..1387b81e 100644 --- a/containments/homescreen2/homescreen.h +++ b/containments/homescreen2/homescreen.h @@ -42,7 +42,7 @@ public: Q_INVOKABLE void stackAfter(QQuickItem *item1, QQuickItem *item2); private: - ApplicationListModel *m_applicationListModel; + ApplicationListModel *m_applicationListModel = nullptr; }; diff --git a/containments/homescreen2/package/contents/config/main.xml b/containments/homescreen2/package/contents/config/main.xml index d35a3081..d191ada3 100644 --- a/containments/homescreen2/package/contents/config/main.xml +++ b/containments/homescreen2/package/contents/config/main.xml @@ -6,11 +6,9 @@ - -