-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqcommandedit.h
98 lines (84 loc) · 2.76 KB
/
qcommandedit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* QCommandEdit - a widget for entering commands, with completion and history
* Copyright (C) 2018 Federico Ferri
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef QCOMMANDEDIT_H
#define QCOMMANDEDIT_H
#include <QLineEdit>
#include <QStringList>
class QCommandEdit : public QLineEdit
{
Q_OBJECT
public:
explicit QCommandEdit(QWidget *parent = nullptr);
void setShowMatchingHistory(bool show);
void setAutoAcceptLongestCommonCompletionPrefix(bool accept);
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
bool eventFilter(QObject *obj, QEvent *event);
public Q_SLOTS:
void clear();
void setHistory(const QStringList &history);
void navigateHistory(int delta);
void setHistoryIndex(int index);
void insertTextAtCursor(const QString &txt, bool selected);
void setCompletion(const QStringList &completion);
void resetCompletion();
void setCurrentCompletion(const QString &s);
void navigateCompletion(int delta);
void acceptCompletion();
void cancelCompletion();
void setToolTipAtCursor(const QString &tip);
void moveCursorToEnd();
Q_SIGNALS:
void execute(const QString &cmd);
void askCompletion(const QString &cmd, int cursorPos);
void escape();
void escapePressed();
void upPressed();
void downPressed();
void tabPressed();
void shiftTabPressed();
private Q_SLOTS:
void onReturnPressed();
void onEscapePressed();
void onUpPressed();
void onDownPressed();
void onTabPressed();
void onShiftTabPressed();
void onSelectionChanged();
void onCursorPositionChanged(int old, int now);
void onTextEdited();
private:
struct HistoryState
{
QStringList history_;
int index_;
QString prefixFilter_;
void reset();
} historyState_;
struct CompletionState
{
QStringList completion_;
bool requested_;
int index_;
void reset();
} completionState_;
void searchMatchingHistoryAndShowGhost();
bool showMatchingHistory_;
bool autoAcceptLongestCommonCompletionPrefix_;
QString ghostSuffix_; // for showing matching history
};
#endif // QCOMMANDEDIT_H