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