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 SEARCHRESULTVIEW_H
18#define SEARCHRESULTVIEW_H
19
20#include <QTreeView>
21#include <QMap>
22#include <QStyledItemDelegate>
23#include "qsynedit/SearchBase.h"
24#include "utils.h"
25
26#define MAX_SEARCH_RESULTS 20
27struct SearchResultTreeItem;
28using PSearchResultTreeItem = std::shared_ptr<SearchResultTreeItem>;
29using SearchResultTreeItemList = QList<PSearchResultTreeItem>;
30using PSearchResultTreeItemList = std::shared_ptr<SearchResultTreeItemList>;
31
32enum class SearchType {
33 Search,
34 FindOccurences
35};
36
37struct SearchResultTreeItem {
38 QString filename;
39 int line;
40 int start;
41 int len;
42 QString text;
43 SearchResultTreeItem* parent;
44 SearchResultTreeItemList results;
45 bool selected;
46};
47
48struct SearchResults{
49 QSynedit::SearchOptions options;
50 QString keyword;
51 QString statementFullname;
52 SearchFileScope scope;
53 SearchType searchType;
54 QString filename;
55 QList<PSearchResultTreeItem> results;
56};
57
58using PSearchResults = std::shared_ptr<SearchResults>;
59
60class SearchResultModel : public QObject {
61 Q_OBJECT
62public:
63 explicit SearchResultModel(QObject* parent=nullptr);
64 PSearchResults addSearchResults(const QString& keyword,QSynedit::SearchOptions options,
65 SearchFileScope scope);
66 PSearchResults addSearchResults(
67 const QString& keyword,
68 const QString& symbolFullname,
69 SearchFileScope scope);
70 PSearchResults results(int index);
71 void notifySearchResultsUpdated();
72 int currentIndex() const;
73 int resultsCount() const;
74 PSearchResults currentResults();
75 void setCurrentIndex(int index);
76 void clear();
77 void removeSearchResults(int index);
78signals:
79 void modelChanged();
80 void currentChanged(int index);
81private:
82 QList<PSearchResults> mSearchResults;
83 int mCurrentIndex;
84
85};
86
87class SearchResultListModel: public QAbstractListModel {
88Q_OBJECT
89 // QAbstractItemModel interface
90public:
91 explicit SearchResultListModel(SearchResultModel* model,QObject* parent=nullptr);
92 int rowCount(const QModelIndex &parent) const override;
93 QVariant data(const QModelIndex &index, int role) const override;
94public slots:
95 void onResultModelChanged();
96private:
97 SearchResultModel *mSearchResultModel;
98};
99
100using PSearchResultListModel = std::shared_ptr<SearchResultListModel>;
101
102class SearchResultTreeModel : public QAbstractItemModel {
103Q_OBJECT
104 // QAbstractItemModel interface
105public:
106 explicit SearchResultTreeModel(SearchResultModel* model,QObject* parent=nullptr);
107 QModelIndex index(int row, int column, const QModelIndex &parent) const override;
108 QModelIndex parent(const QModelIndex &child) const override;
109 int rowCount(const QModelIndex &parent) const override;
110 int columnCount(const QModelIndex &parent) const override;
111 QVariant data(const QModelIndex &index, int role) const override;
112 SearchResultModel *searchResultModel() const;
113 bool getItemFileAndLineChar(
114 const QModelIndex&index,
115 QString& filename,
116 int& line,
117 int& startChar);
118 bool selectable() const;
119 void setSelectable(bool newSelectable);
120
121public slots:
122 void onResultModelChanged();
123private:
124 SearchResultModel *mSearchResultModel;
125 bool mSelectable;
126
127 // QAbstractItemModel interface
128public:
129 Qt::ItemFlags flags(const QModelIndex &index) const override;
130
131 // QAbstractItemModel interface
132public:
133 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
134};
135
136using PSearchResultTreeModel = std::shared_ptr<SearchResultTreeModel>;
137
138class SearchResultTreeViewDelegate: public QStyledItemDelegate{
139Q_OBJECT
140 // QAbstractItemDelegate interface
141public:
142 explicit SearchResultTreeViewDelegate(PSearchResultTreeModel model,
143 QObject* parent=nullptr);
144 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
145private:
146 PSearchResultTreeModel mModel;
147};
148
149using PSearchResultTreeViewDelegate = std::shared_ptr<SearchResultTreeViewDelegate>;
150
151#endif // SEARCHRESULTVIEW_H
152