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 BOOKMARKMODEL_H
18#define BOOKMARKMODEL_H
19
20#include <QAbstractTableModel>
21#include <memory>
22#include <QDebug>
23
24struct Bookmark {
25 QString filename;
26 int line;
27 QString description;
28};
29
30using PBookmark=std::shared_ptr<Bookmark>;
31
32class BookmarkModel : public QAbstractTableModel
33{
34 Q_OBJECT
35public:
36 BookmarkModel(QObject* parent=nullptr);
37 QSet<int> bookmarksInFile(const QString& filename);
38 void addBookmark(const QString&filename, int line, const QString& description);
39 PBookmark bookmark(int i);
40 PBookmark bookmark(const QString&filename, int line);
41 bool removeBookmark(const QString&filename, int line);
42 void removeBookmarks(const QString& filename);
43 void clear();
44 bool updateDescription(const QString&filename, int line, const QString& description);
45 void save(const QString& filename);
46 void load(const QString& filename);
47 void removeBookmarkAt(int i);
48public slots:
49 void onFileDeleteLines(const QString& filename, int startLine, int count);
50 void onFileInsertLines(const QString& filename, int startLine, int count);
51private:
52 bool isBookmarkExists(const QString&filename, int line);
53
54private:
55 QList<PBookmark> mBookmarks;
56
57 // QAbstractItemModel interface
58public:
59 int rowCount(const QModelIndex &parent) const override;
60 QVariant data(const QModelIndex &index, int role) const override;
61 int columnCount(const QModelIndex &parent) const override;
62
63 // QAbstractItemModel interface
64public:
65 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
66};
67
68using PBookmarkModel = std::shared_ptr<BookmarkModel>;
69
70#endif // BOOKMARKMODEL_H
71