mirror of
https://invent.kde.org/marcoa/shift-shell.git
synced 2026-04-29 15:03:09 +00:00
homescreens/halcyon: Port folder app list to QAbstractListModel
This commit is contained in:
parent
92d9295995
commit
d94888b5f3
4 changed files with 136 additions and 42 deletions
|
|
@ -8,6 +8,7 @@
|
|||
ApplicationFolder::ApplicationFolder(QObject *parent, QString name)
|
||||
: QObject{parent}
|
||||
, m_name{name}
|
||||
, m_applicationFolderModel{nullptr}
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -64,64 +65,44 @@ QList<Application *> ApplicationFolder::appPreviews()
|
|||
return previews;
|
||||
}
|
||||
|
||||
QList<Application *> ApplicationFolder::applications()
|
||||
ApplicationFolderModel *ApplicationFolder::applications()
|
||||
{
|
||||
return m_applications;
|
||||
return m_applicationFolderModel;
|
||||
}
|
||||
|
||||
void ApplicationFolder::setApplications(QList<Application *> applications)
|
||||
{
|
||||
if (m_applicationFolderModel) {
|
||||
m_applicationFolderModel->deleteLater();
|
||||
}
|
||||
|
||||
m_applications = applications;
|
||||
Q_EMIT applicationsChanged();
|
||||
Q_EMIT applicationsReset();
|
||||
Q_EMIT saveRequested();
|
||||
|
||||
m_applicationFolderModel = new ApplicationFolderModel{this};
|
||||
}
|
||||
|
||||
void ApplicationFolder::moveEntry(int fromRow, int toRow)
|
||||
{
|
||||
if (fromRow < 0 || toRow < 0 || fromRow >= m_applications.length() || toRow >= m_applications.length() || fromRow == toRow) {
|
||||
return;
|
||||
if (m_applicationFolderModel) {
|
||||
m_applicationFolderModel->moveEntry(fromRow, toRow);
|
||||
}
|
||||
if (toRow > fromRow) {
|
||||
++toRow;
|
||||
}
|
||||
|
||||
if (toRow > fromRow) {
|
||||
Application *app = m_applications.at(fromRow);
|
||||
m_applications.insert(toRow, app);
|
||||
m_applications.takeAt(fromRow);
|
||||
|
||||
} else {
|
||||
Application *app = m_applications.takeAt(fromRow);
|
||||
m_applications.insert(toRow, app);
|
||||
}
|
||||
Q_EMIT applicationsChanged();
|
||||
Q_EMIT saveRequested();
|
||||
}
|
||||
|
||||
void ApplicationFolder::addApp(const QString &storageId, int row)
|
||||
{
|
||||
if (row < 0 || row > m_applications.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (KService::Ptr service = KService::serviceByStorageId(storageId)) {
|
||||
Application *app = new Application(this, service);
|
||||
m_applications.insert(row, app);
|
||||
Q_EMIT applicationsChanged();
|
||||
Q_EMIT saveRequested();
|
||||
if (m_applicationFolderModel) {
|
||||
m_applicationFolderModel->addApp(storageId, row);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplicationFolder::removeApp(int row)
|
||||
{
|
||||
if (row < 0 || row >= m_applications.size()) {
|
||||
return;
|
||||
if (m_applicationFolderModel) {
|
||||
m_applicationFolderModel->removeApp(row);
|
||||
}
|
||||
|
||||
m_applications[row]->deleteLater();
|
||||
m_applications.removeAt(row);
|
||||
Q_EMIT applicationsChanged();
|
||||
Q_EMIT saveRequested();
|
||||
}
|
||||
|
||||
void ApplicationFolder::moveAppOut(int row)
|
||||
|
|
@ -133,3 +114,88 @@ void ApplicationFolder::moveAppOut(int row)
|
|||
Q_EMIT moveAppOutRequested(m_applications[row]->storageId());
|
||||
removeApp(row);
|
||||
}
|
||||
|
||||
ApplicationFolderModel::ApplicationFolderModel(ApplicationFolder *folder)
|
||||
: QAbstractListModel{folder}
|
||||
, m_folder{folder}
|
||||
{
|
||||
}
|
||||
|
||||
int ApplicationFolderModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
return m_folder->m_applications.size();
|
||||
}
|
||||
|
||||
QVariant ApplicationFolderModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
switch (role) {
|
||||
case ApplicationRole:
|
||||
return QVariant::fromValue(m_folder->m_applications.at(index.row()));
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ApplicationFolderModel::roleNames() const
|
||||
{
|
||||
return {{ApplicationRole, "application"}};
|
||||
}
|
||||
|
||||
void ApplicationFolderModel::moveEntry(int fromRow, int toRow)
|
||||
{
|
||||
if (fromRow < 0 || toRow < 0 || fromRow >= m_folder->m_applications.length() || toRow >= m_folder->m_applications.length() || fromRow == toRow) {
|
||||
return;
|
||||
}
|
||||
if (toRow > fromRow) {
|
||||
++toRow;
|
||||
}
|
||||
|
||||
beginMoveRows(QModelIndex(), fromRow, fromRow, QModelIndex(), toRow);
|
||||
if (toRow > fromRow) {
|
||||
Application *app = m_folder->m_applications.at(fromRow);
|
||||
m_folder->m_applications.insert(toRow, app);
|
||||
m_folder->m_applications.takeAt(fromRow);
|
||||
} else {
|
||||
Application *app = m_folder->m_applications.takeAt(fromRow);
|
||||
m_folder->m_applications.insert(toRow, app);
|
||||
}
|
||||
endMoveRows();
|
||||
Q_EMIT m_folder->applicationsChanged();
|
||||
Q_EMIT m_folder->saveRequested();
|
||||
}
|
||||
|
||||
void ApplicationFolderModel::addApp(const QString &storageId, int row)
|
||||
{
|
||||
if (row < 0 || row > m_folder->m_applications.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (KService::Ptr service = KService::serviceByStorageId(storageId)) {
|
||||
beginInsertRows(QModelIndex(), row, row);
|
||||
Application *app = new Application(this, service);
|
||||
m_folder->m_applications.insert(row, app);
|
||||
endInsertRows();
|
||||
|
||||
Q_EMIT m_folder->applicationsChanged();
|
||||
Q_EMIT m_folder->saveRequested();
|
||||
}
|
||||
}
|
||||
|
||||
void ApplicationFolderModel::removeApp(int row)
|
||||
{
|
||||
if (row < 0 || row >= m_folder->m_applications.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginRemoveRows(QModelIndex(), row, row);
|
||||
m_folder->m_applications[row]->deleteLater();
|
||||
m_folder->m_applications.removeAt(row);
|
||||
endRemoveRows();
|
||||
|
||||
Q_EMIT m_folder->applicationsChanged();
|
||||
Q_EMIT m_folder->saveRequested();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "application.h"
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
|
|
@ -18,12 +19,14 @@
|
|||
/**
|
||||
* @short Object that represents an application folder on the main page.
|
||||
*/
|
||||
|
||||
class ApplicationFolderModel;
|
||||
class ApplicationFolder : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
|
||||
Q_PROPERTY(QList<Application *> appPreviews READ appPreviews NOTIFY applicationsChanged)
|
||||
Q_PROPERTY(QList<Application *> applications READ applications NOTIFY applicationsChanged)
|
||||
Q_PROPERTY(ApplicationFolderModel *applications READ applications NOTIFY applicationsReset)
|
||||
|
||||
public:
|
||||
ApplicationFolder(QObject *parent = nullptr, QString name = QString{});
|
||||
|
|
@ -36,7 +39,7 @@ public:
|
|||
|
||||
QList<Application *> appPreviews();
|
||||
|
||||
QList<Application *> applications();
|
||||
ApplicationFolderModel *applications();
|
||||
void setApplications(QList<Application *> applications);
|
||||
|
||||
Q_INVOKABLE void moveEntry(int fromRow, int toRow);
|
||||
|
|
@ -46,11 +49,37 @@ public:
|
|||
|
||||
Q_SIGNALS:
|
||||
void nameChanged();
|
||||
void applicationsChanged();
|
||||
void saveRequested();
|
||||
void moveAppOutRequested(const QString &storageId);
|
||||
void applicationsChanged();
|
||||
void applicationsReset();
|
||||
|
||||
private:
|
||||
QString m_name;
|
||||
QList<Application *> m_applications;
|
||||
ApplicationFolderModel *m_applicationFolderModel;
|
||||
|
||||
friend class ApplicationFolderModel;
|
||||
};
|
||||
|
||||
class ApplicationFolderModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles { ApplicationRole = Qt::UserRole + 1 };
|
||||
ApplicationFolderModel(ApplicationFolder *folder);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
void moveEntry(int fromRow, int toRow);
|
||||
void addApp(const QString &storageId, int row);
|
||||
void removeApp(int row);
|
||||
|
||||
private:
|
||||
ApplicationFolder *m_folder;
|
||||
|
||||
friend class ApplicationFolder;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -174,11 +174,10 @@ MobileShell.GridView {
|
|||
model: root.folderModel
|
||||
|
||||
delegate: Item {
|
||||
id: delegateRoot
|
||||
id: delegateRoot
|
||||
width: root.cellWidth
|
||||
height: root.cellHeight
|
||||
|
||||
property var application: model.application
|
||||
property int visualIndex: DelegateModel.itemsIndex
|
||||
|
||||
DropArea {
|
||||
|
|
@ -196,7 +195,7 @@ MobileShell.GridView {
|
|||
visualIndex: delegateRoot.visualIndex
|
||||
|
||||
isFolder: false
|
||||
application: modelData
|
||||
application: model.application
|
||||
|
||||
menuActions: [
|
||||
Kirigami.Action {
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ void PinnedModel::addAppToFolder(int appRow, int folderRow)
|
|||
|
||||
ApplicationFolder *folder = m_folders[folderRow];
|
||||
Application *app = m_applications[appRow];
|
||||
folder->addApp(app->storageId(), folder->applications().count());
|
||||
folder->addApp(app->storageId(), folder->applications() ? folder->applications()->rowCount() : 0);
|
||||
|
||||
removeEntry(appRow);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue