2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-08-04 13:16:15 +08:00
|
|
|
#ifndef SEARCHRESULTVIEW_H
|
|
|
|
#define SEARCHRESULTVIEW_H
|
|
|
|
|
|
|
|
#include <QTreeView>
|
|
|
|
#include <QMap>
|
2021-08-05 12:31:53 +08:00
|
|
|
#include <QStyledItemDelegate>
|
2023-01-11 16:22:26 +08:00
|
|
|
#include "qsynedit/searcher/baseseacher.h"
|
2021-08-04 13:16:15 +08:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define MAX_SEARCH_RESULTS 20
|
2021-08-05 12:31:53 +08:00
|
|
|
struct SearchResultTreeItem;
|
|
|
|
using PSearchResultTreeItem = std::shared_ptr<SearchResultTreeItem>;
|
|
|
|
using SearchResultTreeItemList = QList<PSearchResultTreeItem>;
|
|
|
|
using PSearchResultTreeItemList = std::shared_ptr<SearchResultTreeItemList>;
|
2021-08-04 13:16:15 +08:00
|
|
|
|
2021-09-03 11:50:04 +08:00
|
|
|
enum class SearchType {
|
|
|
|
Search,
|
|
|
|
FindOccurences
|
|
|
|
};
|
|
|
|
|
2021-08-05 12:31:53 +08:00
|
|
|
struct SearchResultTreeItem {
|
2021-08-04 13:16:15 +08:00
|
|
|
QString filename;
|
|
|
|
int line;
|
|
|
|
int start;
|
|
|
|
int len;
|
2021-08-05 12:31:53 +08:00
|
|
|
QString text;
|
|
|
|
SearchResultTreeItem* parent;
|
|
|
|
SearchResultTreeItemList results;
|
2021-10-04 12:49:55 +08:00
|
|
|
bool selected;
|
2021-08-04 13:16:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SearchResults{
|
2022-09-27 14:01:38 +08:00
|
|
|
QSynedit::SearchOptions options;
|
2021-08-04 13:16:15 +08:00
|
|
|
QString keyword;
|
2021-11-05 10:44:23 +08:00
|
|
|
QString statementFullname;
|
2021-08-04 13:16:15 +08:00
|
|
|
SearchFileScope scope;
|
2021-09-03 11:50:04 +08:00
|
|
|
SearchType searchType;
|
|
|
|
QString filename;
|
2023-07-05 15:02:39 +08:00
|
|
|
QString folder;
|
|
|
|
QString filters;
|
|
|
|
bool searchSubfolders;
|
2021-08-05 12:31:53 +08:00
|
|
|
QList<PSearchResultTreeItem> results;
|
2021-08-04 13:16:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using PSearchResults = std::shared_ptr<SearchResults>;
|
|
|
|
|
2021-08-05 12:31:53 +08:00
|
|
|
class SearchResultModel : public QObject {
|
2021-08-04 13:16:15 +08:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit SearchResultModel(QObject* parent=nullptr);
|
2022-09-27 14:01:38 +08:00
|
|
|
PSearchResults addSearchResults(const QString& keyword,QSynedit::SearchOptions options,
|
2023-07-05 15:02:39 +08:00
|
|
|
SearchFileScope scope, const QString& folder=QString(), const QString& filters=QString(), bool searchSubfolders=true);
|
2021-09-03 11:50:04 +08:00
|
|
|
PSearchResults addSearchResults(
|
2021-11-05 10:44:23 +08:00
|
|
|
const QString& keyword,
|
2021-11-04 22:56:11 +08:00
|
|
|
const QString& symbolFullname,
|
|
|
|
SearchFileScope scope);
|
2021-08-04 13:16:15 +08:00
|
|
|
PSearchResults results(int index);
|
2021-08-05 12:31:53 +08:00
|
|
|
void notifySearchResultsUpdated();
|
2021-08-04 13:16:15 +08:00
|
|
|
int currentIndex() const;
|
|
|
|
int resultsCount() const;
|
|
|
|
PSearchResults currentResults();
|
|
|
|
void setCurrentIndex(int index);
|
|
|
|
void clear();
|
2021-09-05 21:05:38 +08:00
|
|
|
void removeSearchResults(int index);
|
2021-08-04 13:16:15 +08:00
|
|
|
signals:
|
|
|
|
void modelChanged();
|
|
|
|
void currentChanged(int index);
|
|
|
|
private:
|
|
|
|
QList<PSearchResults> mSearchResults;
|
|
|
|
int mCurrentIndex;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class SearchResultListModel: public QAbstractListModel {
|
|
|
|
Q_OBJECT
|
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
explicit SearchResultListModel(SearchResultModel* model,QObject* parent=nullptr);
|
|
|
|
int rowCount(const QModelIndex &parent) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
|
|
|
public slots:
|
|
|
|
void onResultModelChanged();
|
|
|
|
private:
|
|
|
|
SearchResultModel *mSearchResultModel;
|
|
|
|
};
|
|
|
|
|
2021-08-05 12:31:53 +08:00
|
|
|
using PSearchResultListModel = std::shared_ptr<SearchResultListModel>;
|
|
|
|
|
2021-08-04 13:16:15 +08:00
|
|
|
class SearchResultTreeModel : public QAbstractItemModel {
|
|
|
|
Q_OBJECT
|
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
explicit SearchResultTreeModel(SearchResultModel* model,QObject* parent=nullptr);
|
|
|
|
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
|
|
|
QModelIndex parent(const QModelIndex &child) const override;
|
|
|
|
int rowCount(const QModelIndex &parent) const override;
|
|
|
|
int columnCount(const QModelIndex &parent) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
2021-08-05 12:31:53 +08:00
|
|
|
SearchResultModel *searchResultModel() const;
|
2021-09-05 21:05:38 +08:00
|
|
|
bool getItemFileAndLineChar(
|
|
|
|
const QModelIndex&index,
|
|
|
|
QString& filename,
|
|
|
|
int& line,
|
|
|
|
int& startChar);
|
2021-10-04 12:49:55 +08:00
|
|
|
bool selectable() const;
|
|
|
|
void setSelectable(bool newSelectable);
|
|
|
|
|
2021-08-05 12:31:53 +08:00
|
|
|
public slots:
|
|
|
|
void onResultModelChanged();
|
2021-08-04 13:16:15 +08:00
|
|
|
private:
|
|
|
|
SearchResultModel *mSearchResultModel;
|
2021-10-04 12:49:55 +08:00
|
|
|
bool mSelectable;
|
|
|
|
|
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
|
|
|
|
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
2021-08-04 13:16:15 +08:00
|
|
|
};
|
|
|
|
|
2021-08-05 12:31:53 +08:00
|
|
|
using PSearchResultTreeModel = std::shared_ptr<SearchResultTreeModel>;
|
|
|
|
|
|
|
|
class SearchResultTreeViewDelegate: public QStyledItemDelegate{
|
|
|
|
Q_OBJECT
|
|
|
|
// QAbstractItemDelegate interface
|
2021-08-04 13:16:15 +08:00
|
|
|
public:
|
2021-08-05 12:31:53 +08:00
|
|
|
explicit SearchResultTreeViewDelegate(PSearchResultTreeModel model,
|
|
|
|
QObject* parent=nullptr);
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
|
|
|
private:
|
|
|
|
PSearchResultTreeModel mModel;
|
2021-08-04 13:16:15 +08:00
|
|
|
};
|
|
|
|
|
2021-08-05 12:31:53 +08:00
|
|
|
using PSearchResultTreeViewDelegate = std::shared_ptr<SearchResultTreeViewDelegate>;
|
|
|
|
|
2021-08-04 13:16:15 +08:00
|
|
|
#endif // SEARCHRESULTVIEW_H
|