-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqconsole.h
More file actions
207 lines (169 loc) · 5.61 KB
/
qconsole.h
File metadata and controls
207 lines (169 loc) · 5.61 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
Change log:
(C) 2005 by Houssem BDIOUI <houssem.bdioui@gmail.com>
(C) 2010 by YoungTaek Oh. (migrated to Qt4)
(C) 2014-2015 Igor Malinovskiy
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 2 of the License, or
(at your option) any later version.
*/
#pragma once
#include <QStringList>
#include <QTextEdit>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QMenu>
#include <QDialog>
#include <QListWidget>
#include <QDebug>
/**
* Subclasssing QListWidget
*
* @author YoungTaek Oh
*/
class PopupListWidget : public QListWidget
{
Q_OBJECT
public:
PopupListWidget(QWidget *parent = 0): QListWidget(parent) {
setUniformItemSizes(true);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
virtual ~PopupListWidget() { }
protected:
virtual QSize sizeHint() const;
virtual void keyPressEvent(QKeyEvent *e);
};
/**
* Popup Completer class
*
* @author YoungTaek Oh
* @todo 1. beautifying
* 2. icons for classifying words (eg. functions, properties...)
* 3. bugs?
* @note still experimental
*/
class PopupCompleter : public QDialog
{
Q_OBJECT
public:
PopupCompleter(const QStringList&, QWidget *parent = 0);
virtual ~PopupCompleter();
public:
QString selected(void) { return selected_; }
int exec(QTextEdit*);
protected:
virtual void showEvent(QShowEvent*);
private Q_SLOTS:
void onItemActivated(QListWidgetItem*);
public:
QListWidget *listWidget_;
QString selected_;
};
/**
* An abstract Qt console
* @author Houssem BDIOUI
*/
class QConsole : public QTextEdit
{
Q_OBJECT
public:
//constructor
QConsole(QWidget *parent = NULL, const QString &welcomeText = "");
//set the prompt of the console
void setPrompt(const QString &prompt, bool display = true);
//clear & reset the console (useful sometimes)
void clear();
void reset(const QString &welcomeText = "");
//cosmetic methods !
// @{
/// get/set command color
QColor cmdColor() const { return cmdColor_; }
void setCmdColor(QColor c) {cmdColor_ = c;}
// @}
// @{
/// get/set error color
QColor errColor() const { return errColor_; }
void setErrColor(QColor c) {errColor_ = c;}
// @}
// @{
/// get/set output color
QColor outColor() const { return outColor_; }
void setOutColor(QColor c) {outColor_ = c;}
// @}
void setCompletionColor(QColor c) {completionColor = c;}
// @{
/// get set font
void setFont(const QFont& f);
QFont font() const { return currentFont(); }
// @}
void correctPathName(QString& pathName);
enum ResultType {Error, Partial, Complete};
private:
void dropEvent( QDropEvent * event);
void dragMoveEvent( QDragMoveEvent * event);
void keyPressEvent(QKeyEvent * e);
void contextMenuEvent( QContextMenuEvent * event);
//Return false if the command is incomplete (e.g. unmatched braces)
virtual bool isCommandComplete(const QString &command);
//Get the command to validate
QString getCurrentCommand();
//Replace current command with a new one
void replaceCurrentCommand(const QString &newCommand);
//Test whether the cursor is in the edition zone
bool isInEditionZone();
bool isInEditionZone(const int& pos);
//Test whether the selection is in the edition zone
bool isSelectionInEditionZone();
//Change paste behaviour
void insertFromMimeData(const QMimeData *);
protected:
//colors
QColor cmdColor_, errColor_, outColor_, completionColor;
int oldPosition;
// cached prompt length
int promptLength;
// The prompt string
QString prompt;
// The commands history
QStringList history;
//Contains the commands that has succeeded
QStringList recordedScript;
// Current history index (needed because afaik QStringList does not have such an index)
int historyIndex;
//Holds the paragraph number of the prompt (useful for multi-line command handling)
int promptParagraph;
protected:
//Implement paste with middle mouse button
void mousePressEvent(QMouseEvent*);
//execute a validated command (should be reimplemented and called at the end)
//the return value of the function is the string result
//res must hold back the return value of the command (0: passed; else: error)
virtual QString addCommandToHistory(const QString &command);
//give suggestions to autocomplete a command (should be reimplemented)
//the return value of the function is the string list of all suggestions
//the returned prefix is useful to complete "sub-commands"
virtual QStringList suggestCommand(const QString &cmd, QString &prefix);
public slots:
//Contextual menu slots
void cut();
//void paste();
void del();
//displays the prompt
void displayPrompt();
void printCommandExecutionResults(const QString &, ResultType t = ResultType::Complete);
signals:
//Signal emitted after that a command is executed
void commandAddedToHistory(const QString &command);
void execCommand(const QString &command);
private:
bool isLocked;
void handleTabKeyPress();
void handleReturnKeyPress();
bool handleBackspaceKeyPress();
void handleUpKeyPress();
void handleDownKeyPress();
void setHome(bool);
void pExecCommand(const QString &command);
};