From c0d4d075afa8c9a112bac9672bf0cc4a15f4759b Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 5 Mar 2015 13:37:39 +0100 Subject: [PATCH] support model reordering the order is still not saved/restored --- .../homescreen/contents/ui/HomeLauncher.qml | 2 +- containments/homescreen/contents/ui/main.qml | 15 ++++++----- qmlcomponents/applicationlistmodel.cpp | 26 ++++++++++++++++++- qmlcomponents/applicationlistmodel.h | 7 ++++- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/containments/homescreen/contents/ui/HomeLauncher.qml b/containments/homescreen/contents/ui/HomeLauncher.qml index 045bfbfd..7a0142ce 100644 --- a/containments/homescreen/contents/ui/HomeLauncher.qml +++ b/containments/homescreen/contents/ui/HomeLauncher.qml @@ -19,7 +19,7 @@ MouseArea { } onPositionChanged: { if (root.drag.target) { - print("New position: " +(Math.round(GridView.view.width / GridView.view.cellWidth) * Math.round(root.y / GridView.view.cellHeight) + Math.round(root.x / GridView.view.cellWidth))) + appListModel.setOrder(model.ApplicationOriginalRowRole, (Math.round(GridView.view.width / GridView.view.cellWidth) * Math.round(root.y / GridView.view.cellHeight) + Math.round(root.x / GridView.view.cellWidth))); } } diff --git a/containments/homescreen/contents/ui/main.qml b/containments/homescreen/contents/ui/main.qml index 359a2250..4d625c20 100644 --- a/containments/homescreen/contents/ui/main.qml +++ b/containments/homescreen/contents/ui/main.qml @@ -34,6 +34,10 @@ Item { property alias appletsSpace: applicationsView.headerItem property int buttonHeight: width/4 + SatelliteComponents.ApplicationListModel { + id: appListModel + } + Containment.onAppletAdded: { var container = appletContainerComponent.createObject(appletsSpace.layout) container.visible = true @@ -88,12 +92,11 @@ Item { cellWidth: root.buttonHeight cellHeight: cellWidth - model: PlasmaCore.SortFilterModel { - sourceModel: SatelliteComponents.ApplicationListModel { - id: appListModel - } - sortRole: "ApplicationNameRole" - } + model: appListModel + /* PlasmaCore.SortFilterModel { + sourceModel: appListModel + sortRole: "ApplicationOrderRole" + }*/ snapMode: GridView.SnapToRow //clip: true delegate: HomeLauncher {} diff --git a/qmlcomponents/applicationlistmodel.cpp b/qmlcomponents/applicationlistmodel.cpp index 10fc4161..0d63b9e4 100644 --- a/qmlcomponents/applicationlistmodel.cpp +++ b/qmlcomponents/applicationlistmodel.cpp @@ -50,6 +50,8 @@ QHash ApplicationListModel::roleNames() const roleNames[ApplicationIconRole] = "ApplicationIconRole"; roleNames[ApplicationStorageIdRole] = "ApplicationStorageIdRole"; roleNames[ApplicationEntryPathRole] = "ApplicationEntryPathRole"; + roleNames[ApplicationOrderRole] = "ApplicationOrderRole"; + roleNames[ApplicationOriginalRowRole] = "ApplicationOriginalRowRole"; return roleNames; } @@ -87,6 +89,7 @@ void ApplicationListModel::loadApplications() data.icon = plugin.icon(); data.storageId = service->storageId(); data.entryPath = plugin.entryPath(); + data.order = 99; m_applicationList << data; } } @@ -115,6 +118,10 @@ QVariant ApplicationListModel::data(const QModelIndex &index, int role) const return m_applicationList.at(index.row()).storageId; case ApplicationEntryPathRole: return m_applicationList.at(index.row()).entryPath; + case ApplicationOrderRole: + return m_applicationList.at(index.row()).order; + case ApplicationOriginalRowRole: + return index.row(); default: return QVariant(); @@ -130,7 +137,24 @@ int ApplicationListModel::rowCount(const QModelIndex &parent) const return m_applicationList.count(); } -void ApplicationListModel::runApplication(const QString &storageId) { +Q_INVOKABLE void ApplicationListModel::setOrder(int row, int destination) +{ + if (row < 0 || destination < 0 || row >= m_applicationList.length() || + destination >= m_applicationList.length() || row == destination) { + return; + } + if (destination > row) { + ++destination; + } + + beginMoveRows(QModelIndex(), row, row, QModelIndex(), destination); + ApplicationData data = m_applicationList.takeAt(row); + m_applicationList.insert(destination, data); + endMoveRows(); +} + +void ApplicationListModel::runApplication(const QString &storageId) +{ if (storageId.isEmpty()) { return; } diff --git a/qmlcomponents/applicationlistmodel.h b/qmlcomponents/applicationlistmodel.h index e963e9ce..c3a4f5dd 100644 --- a/qmlcomponents/applicationlistmodel.h +++ b/qmlcomponents/applicationlistmodel.h @@ -32,6 +32,7 @@ struct ApplicationData { QString icon; QString storageId; QString entryPath; + int order; }; class ApplicationListModel : public QAbstractListModel { @@ -55,9 +56,13 @@ public: ApplicationNameRole = Qt::UserRole + 1, ApplicationIconRole = Qt::UserRole + 2, ApplicationStorageIdRole = Qt::UserRole + 3, - ApplicationEntryPathRole = Qt::UserRole + 4 + ApplicationEntryPathRole = Qt::UserRole + 4, + ApplicationOrderRole = Qt::UserRole + 5, + ApplicationOriginalRowRole = Qt::UserRole + 6 }; + Q_INVOKABLE void setOrder(int row, int order); + Q_INVOKABLE void runApplication(const QString &storageId); Q_SIGNALS: