File indexing completed on 2024-12-29 05:06:05
0001 // SPDX-FileCopyrightText: 2022-2023 Devin Lin <devin@kde.org> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #pragma once 0005 0006 #include "folioapplication.h" 0007 #include "folioapplicationfolder.h" 0008 #include "foliodelegate.h" 0009 0010 #include <QAbstractListModel> 0011 #include <QJsonArray> 0012 #include <QList> 0013 0014 #include <Plasma/Applet> 0015 0016 class FolioPageDelegate : public FolioDelegate 0017 { 0018 Q_OBJECT 0019 Q_PROPERTY(int row READ row NOTIFY rowChanged) 0020 Q_PROPERTY(int column READ column NOTIFY columnChanged) 0021 0022 public: 0023 FolioPageDelegate(int row = 0, int column = 0, QObject *parent = nullptr); 0024 FolioPageDelegate(int row, int column, FolioApplication *application, QObject *parent); 0025 FolioPageDelegate(int row, int column, FolioApplicationFolder *folder, QObject *parent); 0026 FolioPageDelegate(int row, int column, FolioWidget *widget, QObject *parent); 0027 FolioPageDelegate(int row, int column, FolioDelegate *delegate, QObject *parent); 0028 0029 static FolioPageDelegate *fromJson(QJsonObject &obj, QObject *parent); 0030 static int getTranslatedTopLeftRow(int realRow, int realColumn, FolioDelegate *fd); 0031 static int getTranslatedTopLeftColumn(int realRow, int realColumn, FolioDelegate *fd); 0032 static int getTranslatedRow(int realRow, int realColumn); 0033 static int getTranslatedColumn(int realRow, int realColumn); 0034 0035 virtual QJsonObject toJson() const override; 0036 0037 int row(); 0038 void setRow(int row); 0039 0040 int column(); 0041 void setColumn(int column); 0042 0043 Q_SIGNALS: 0044 void rowChanged(); 0045 void columnChanged(); 0046 0047 private: 0048 void setRowOnly(int row); 0049 void setColumnOnly(int column); 0050 void init(); 0051 0052 int m_realRow; 0053 int m_realColumn; 0054 int m_row; 0055 int m_column; 0056 }; 0057 0058 class PageModel : public QAbstractListModel 0059 { 0060 Q_OBJECT 0061 public: 0062 enum Roles { 0063 DelegateRole = Qt::UserRole + 1, 0064 XPositionRole, 0065 YPositionRole, 0066 ShownRole, 0067 }; 0068 0069 PageModel(QList<FolioPageDelegate *> delegates = QList<FolioPageDelegate *>{}, QObject *parent = nullptr); 0070 ~PageModel(); 0071 0072 static PageModel *fromJson(QJsonArray &arr, QObject *parent); 0073 0074 QJsonArray toJson() const; 0075 0076 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0077 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0078 QHash<int, QByteArray> roleNames() const override; 0079 0080 Q_INVOKABLE void removeDelegate(int row, int col); 0081 Q_INVOKABLE void removeDelegate(int index); 0082 Q_INVOKABLE bool canAddDelegate(int row, int column, FolioDelegate *delegate); 0083 bool addDelegate(FolioPageDelegate *delegate); 0084 FolioPageDelegate *getDelegate(int row, int col); 0085 0086 Q_INVOKABLE void moveAndResizeWidgetDelegate(FolioPageDelegate *delegate, int newRow, int newColumn, int newGridWidth, int newGridHeight); 0087 0088 bool isPageEmpty(); 0089 0090 public Q_SLOTS: 0091 void save(); 0092 0093 Q_SIGNALS: 0094 void saveRequested(); 0095 0096 private: 0097 void connectSaveRequests(FolioDelegate *delegate); 0098 QList<FolioPageDelegate *> m_delegates; 0099 };