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 |
27 | struct SearchResultTreeItem; |
28 | using PSearchResultTreeItem = std::shared_ptr<SearchResultTreeItem>; |
29 | using SearchResultTreeItemList = QList<PSearchResultTreeItem>; |
30 | using PSearchResultTreeItemList = std::shared_ptr<SearchResultTreeItemList>; |
31 | |
32 | enum class SearchType { |
33 | Search, |
34 | FindOccurences |
35 | }; |
36 | |
37 | struct 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 | |
48 | struct 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 | |
58 | using PSearchResults = std::shared_ptr<SearchResults>; |
59 | |
60 | class SearchResultModel : public QObject { |
61 | Q_OBJECT |
62 | public: |
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); |
78 | signals: |
79 | void modelChanged(); |
80 | void currentChanged(int index); |
81 | private: |
82 | QList<PSearchResults> mSearchResults; |
83 | int mCurrentIndex; |
84 | |
85 | }; |
86 | |
87 | class SearchResultListModel: public QAbstractListModel { |
88 | Q_OBJECT |
89 | // QAbstractItemModel interface |
90 | public: |
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; |
94 | public slots: |
95 | void onResultModelChanged(); |
96 | private: |
97 | SearchResultModel *mSearchResultModel; |
98 | }; |
99 | |
100 | using PSearchResultListModel = std::shared_ptr<SearchResultListModel>; |
101 | |
102 | class SearchResultTreeModel : public QAbstractItemModel { |
103 | Q_OBJECT |
104 | // QAbstractItemModel interface |
105 | public: |
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 | |
121 | public slots: |
122 | void onResultModelChanged(); |
123 | private: |
124 | SearchResultModel *mSearchResultModel; |
125 | bool mSelectable; |
126 | |
127 | // QAbstractItemModel interface |
128 | public: |
129 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
130 | |
131 | // QAbstractItemModel interface |
132 | public: |
133 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
134 | }; |
135 | |
136 | using PSearchResultTreeModel = std::shared_ptr<SearchResultTreeModel>; |
137 | |
138 | class SearchResultTreeViewDelegate: public QStyledItemDelegate{ |
139 | Q_OBJECT |
140 | // QAbstractItemDelegate interface |
141 | public: |
142 | explicit SearchResultTreeViewDelegate(PSearchResultTreeModel model, |
143 | QObject* parent=nullptr); |
144 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; |
145 | private: |
146 | PSearchResultTreeModel mModel; |
147 | }; |
148 | |
149 | using PSearchResultTreeViewDelegate = std::shared_ptr<SearchResultTreeViewDelegate>; |
150 | |
151 | #endif // SEARCHRESULTVIEW_H |
152 | |