| 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 TODOPARSER_H |
| 18 | #define TODOPARSER_H |
| 19 | |
| 20 | #include <QObject> |
| 21 | #include <QThread> |
| 22 | #include <QMutex> |
| 23 | #include <QAbstractListModel> |
| 24 | |
| 25 | struct TodoItem { |
| 26 | QString filename; |
| 27 | int lineNo; |
| 28 | int ch; |
| 29 | QString line; |
| 30 | }; |
| 31 | |
| 32 | using PTodoItem = std::shared_ptr<TodoItem>; |
| 33 | |
| 34 | class TodoModel : public QAbstractListModel { |
| 35 | Q_OBJECT |
| 36 | public: |
| 37 | explicit TodoModel(QObject* parent=nullptr); |
| 38 | void addItem(const QString& filename, int lineNo, |
| 39 | int ch, const QString& line); |
| 40 | void clear(); |
| 41 | PTodoItem getItem(const QModelIndex& index); |
| 42 | private: |
| 43 | QList<PTodoItem> mItems; |
| 44 | |
| 45 | // QAbstractItemModel interface |
| 46 | public: |
| 47 | int rowCount(const QModelIndex &parent) const override; |
| 48 | QVariant data(const QModelIndex &index, int role) const override; |
| 49 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
| 50 | |
| 51 | // QAbstractItemModel interface |
| 52 | public: |
| 53 | int columnCount(const QModelIndex &parent) const override; |
| 54 | }; |
| 55 | |
| 56 | class TodoThread: public QThread |
| 57 | { |
| 58 | Q_OBJECT |
| 59 | public: |
| 60 | explicit TodoThread(const QString& filename, QObject* parent = nullptr); |
| 61 | signals: |
| 62 | void parseStarted(const QString& filename); |
| 63 | void todoFound(const QString& filename, int lineNo, int ch, const QString& line); |
| 64 | void parseFinished(); |
| 65 | private: |
| 66 | QString mFilename; |
| 67 | |
| 68 | // QThread interface |
| 69 | protected: |
| 70 | void run() override; |
| 71 | }; |
| 72 | |
| 73 | using PTodoThread = std::shared_ptr<TodoThread>; |
| 74 | |
| 75 | class TodoParser : public QObject |
| 76 | { |
| 77 | Q_OBJECT |
| 78 | public: |
| 79 | explicit TodoParser(QObject *parent = nullptr); |
| 80 | void parseFile(const QString& filename); |
| 81 | bool parsing() const; |
| 82 | |
| 83 | private: |
| 84 | TodoThread* mThread; |
| 85 | QMutex mMutex; |
| 86 | }; |
| 87 | |
| 88 | using PTodoParser = std::shared_ptr<TodoParser>; |
| 89 | |
| 90 | #endif // TODOPARSER_H |
| 91 | |