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 HEADERCOMPLETIONPOPUP_H
18#define HEADERCOMPLETIONPOPUP_H
19
20#include <QDir>
21#include <QWidget>
22#include "codecompletionlistview.h"
23#include "../parser/cppparser.h"
24
25enum class HeaderCompletionListItemType {
26 LocalHeader,
27 ProjectHeader,
28 SystemHeader
29};
30
31struct HeaderCompletionListItem {
32 QString filename;
33 QString fullpath;
34 bool isFolder;
35 int usageCount;
36 HeaderCompletionListItemType itemType;
37};
38
39using PHeaderCompletionListItem=std::shared_ptr<HeaderCompletionListItem>;
40
41class HeaderCompletionListModel: public QAbstractListModel {
42 Q_OBJECT
43public:
44 explicit HeaderCompletionListModel(const QList<PHeaderCompletionListItem>* files,QObject *parent = nullptr);
45 int rowCount(const QModelIndex &parent) const override;
46 QVariant data(const QModelIndex &index, int role) const override;
47 void notifyUpdated();
48 void setLocalColor(const QColor &newColor);
49 void setSystemColor(const QColor &newColor);
50 void setProjectColor(const QColor &newColor);
51
52 void setFolderColor(const QColor &newFolderColor);
53
54private:
55 const QList<PHeaderCompletionListItem> *mFiles;
56 QColor mLocalColor;
57 QColor mSystemColor;
58 QColor mProjectColor;
59 QColor mFolderColor;
60};
61
62class HeaderCompletionPopup : public QWidget
63{
64 Q_OBJECT
65public:
66 HeaderCompletionPopup(QWidget* parent=nullptr);
67 ~HeaderCompletionPopup();
68 void prepareSearch(const QString& phrase, const QString& fileName);
69 bool search(const QString& phrase, bool autoHideOnSingleResult);
70 void setKeypressedCallback(const KeyPressedCallback &newKeypressedCallback);
71 void setSuggestionColor(const QColor& localColor,
72 const QColor& projectColor,
73 const QColor& systemColor,
74 const QColor& folderColor);
75 QString selectedFilename(bool updateUsageCount);
76
77private:
78 void filterList(const QString& member);
79 void getCompletionFor(const QString& phrase);
80 void addFilesInPath(const QString& path, HeaderCompletionListItemType type);
81 void addFile(const QDir& dir, const QFileInfo &fileInfo, HeaderCompletionListItemType type);
82 void addFilesInSubDir(const QString& baseDirPath, const QString& subDirName, HeaderCompletionListItemType type);
83private:
84
85 CodeCompletionListView* mListView;
86 HeaderCompletionListModel* mModel;
87 QHash<QString, PHeaderCompletionListItem> mFullCompletionList;
88 QList<PHeaderCompletionListItem> mCompletionList;
89 QHash<QString,int> mHeaderUsageCounts;
90 int mShowCount;
91 QSet<QString> mAddedFileNames;
92
93 PCppParser mParser;
94 QString mPhrase;
95 bool mIgnoreCase;
96 bool mSearchLocal;
97 QString mCurrentFile;
98
99 // QWidget interface
100protected:
101 void showEvent(QShowEvent *event) override;
102 void hideEvent(QHideEvent *event) override;
103
104 // QObject interface
105public:
106 bool event(QEvent *event) override;
107 void setParser(const PCppParser &newParser);
108 const QString &phrase() const;
109 bool ignoreCase() const;
110 void setIgnoreCase(bool newIgnoreCase);
111 bool searchLocal() const;
112 void setSearchLocal(bool newSearchLocal);
113};
114
115#endif // HEADERCOMPLETIONPOPUP_H
116