| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #ifndef OJPROBLEMSETMODEL_H |
| 18 | #define OJPROBLEMSETMODEL_H |
| 19 | |
| 20 | #include <QAbstractTableModel> |
| 21 | #include <memory> |
| 22 | #include "../problems/ojproblemset.h" |
| 23 | |
| 24 | class OJProblemModel: public QAbstractTableModel { |
| 25 | Q_OBJECT |
| 26 | public: |
| 27 | explicit OJProblemModel(QObject *parent = nullptr); |
| 28 | const POJProblem &problem() const; |
| 29 | void setProblem(const POJProblem &newProblem); |
| 30 | void addCase(POJProblemCase problemCase); |
| 31 | void removeCase(int index); |
| 32 | void removeCases(); |
| 33 | POJProblemCase getCase(int index); |
| 34 | POJProblemCase getCaseById(const QString& id); |
| 35 | int getCaseIndexById(const QString& id); |
| 36 | void clear(); |
| 37 | int count(); |
| 38 | void update(int row); |
| 39 | QString getTitle(); |
| 40 | QString getTooltip(); |
| 41 | |
| 42 | private: |
| 43 | POJProblem mProblem; |
| 44 | int mMoveTargetRow; |
| 45 | |
| 46 | // QAbstractItemModel interface |
| 47 | public: |
| 48 | int rowCount(const QModelIndex &parent) const override; |
| 49 | QVariant data(const QModelIndex &index, int role) const override; |
| 50 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 51 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 52 | |
| 53 | // QAbstractItemModel interface |
| 54 | public: |
| 55 | int columnCount(const QModelIndex &parent) const override; |
| 56 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
| 57 | |
| 58 | // QAbstractItemModel interface |
| 59 | public: |
| 60 | Qt::DropActions supportedDropActions() const override; |
| 61 | |
| 62 | // QAbstractItemModel interface |
| 63 | public: |
| 64 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; |
| 65 | |
| 66 | // QAbstractItemModel interface |
| 67 | public: |
| 68 | bool insertRows(int row, int count, const QModelIndex &parent) override; |
| 69 | bool removeRows(int row, int count, const QModelIndex &parent) override; |
| 70 | }; |
| 71 | |
| 72 | class OJProblemSetModel : public QAbstractListModel |
| 73 | { |
| 74 | Q_OBJECT |
| 75 | public: |
| 76 | explicit OJProblemSetModel(QObject *parent = nullptr); |
| 77 | void clear(); |
| 78 | int count(); |
| 79 | void create(const QString& name); |
| 80 | void rename(const QString& newName); |
| 81 | QString name() const; |
| 82 | QString exportFilename() const; |
| 83 | void addProblem(POJProblem problem); |
| 84 | POJProblem problem(int index); |
| 85 | void removeProblem(int index); |
| 86 | bool problemNameUsed(const QString& name); |
| 87 | void removeAllProblems(); |
| 88 | void saveToFile(const QString& fileName); |
| 89 | void loadFromFile(const QString& fileName); |
| 90 | void updateProblemAnswerFilename(const QString& oldFilename, const QString& newFilename); |
| 91 | |
| 92 | signals: |
| 93 | void problemNameChanged(int index); |
| 94 | |
| 95 | private: |
| 96 | OJProblemSet mProblemSet; |
| 97 | |
| 98 | // QAbstractItemModel interface |
| 99 | public: |
| 100 | int rowCount(const QModelIndex &parent) const override; |
| 101 | QVariant data(const QModelIndex &index, int role) const override; |
| 102 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 103 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 104 | |
| 105 | // QAbstractItemModel interface |
| 106 | public: |
| 107 | Qt::DropActions supportedDropActions() const override; |
| 108 | bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override; |
| 109 | }; |
| 110 | |
| 111 | #endif // OJPROBLEMSETMODEL_H |
| 112 | |