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 CODESNIPPETSMANAGER_H |
18 | #define CODESNIPPETSMANAGER_H |
19 | |
20 | #include <QObject> |
21 | #include "parser/parserutils.h" |
22 | #include <QAbstractListModel> |
23 | |
24 | class CodeSnippetsModel: public QAbstractListModel { |
25 | Q_OBJECT |
26 | public: |
27 | void addSnippet( |
28 | const QString& caption, |
29 | const QString& prefix, |
30 | const QString& code, |
31 | const QString& description, |
32 | int ); |
33 | void remove(int index); |
34 | void clear(); |
35 | QModelIndex lastSnippetCaption(); |
36 | |
37 | // QAbstractItemModel interface |
38 | public: |
39 | int rowCount(const QModelIndex &parent) const override; |
40 | int columnCount(const QModelIndex &parent) const override; |
41 | QVariant data(const QModelIndex &index, int role) const override; |
42 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
43 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
44 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
45 | const QList<PCodeSnippet> &snippets() const; |
46 | void updateSnippets(const QList<PCodeSnippet>& snippets); |
47 | |
48 | private: |
49 | QList<PCodeSnippet> mSnippets; |
50 | }; |
51 | |
52 | class CodeSnippetsManager : public QObject |
53 | { |
54 | Q_OBJECT |
55 | public: |
56 | explicit CodeSnippetsManager(QObject *parent = nullptr); |
57 | |
58 | void load(); |
59 | void save(); |
60 | |
61 | const QList<PCodeSnippet> &snippets() const; |
62 | |
63 | void setSnippets(const QList<PCodeSnippet> &newSnippets); |
64 | |
65 | const QString &newFileTemplate() const; |
66 | void setNewFileTemplate(const QString &newNewFileTemplate); |
67 | |
68 | private: |
69 | void loadSnippets(); |
70 | void saveSnippets(); |
71 | void loadNewFileTemplate(); |
72 | void saveNewFileTemplate(); |
73 | |
74 | private: |
75 | QList<PCodeSnippet> mSnippets; |
76 | QString mNewFileTemplate; |
77 | }; |
78 | |
79 | using PCodeSnippetManager = std::shared_ptr<CodeSnippetsManager>; |
80 | |
81 | #endif // CODESNIPPETSMANAGER_H |
82 | |