-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodeeditor.h
120 lines (107 loc) · 3.44 KB
/
codeeditor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include "executor.h"
#include "Utils/systemapi.h"
#include <QLabel>
#include <QLineEdit>
#include <QWidget>
#include "cmdlistwidget.h"
#include "Utils/cacheiconprovider.h"
#include <QCompleter>
#include "Utils/request.h"
struct PastCodeList {
PastCodeList()
: lineLimit(10), index(0) {}
explicit PastCodeList(int LineLimit)
: lineLimit(LineLimit), index(0) {}
void setLineLimit(int LineLimit) { lineLimit = LineLimit; }
PastCodeList& operator<<(const QString& str)
{
if (list.empty() || list.back() != str) { //短路求值保证list有效访问
if (list.size() >= lineLimit) list.removeFirst();
list << str;
}
index = list.size();
return *this;
}
QString past(void)
{
index--;
fixIndex();
if (!isValid(index)) return "";
return list.at(index);
}
QString next(void)
{
index++;
fixIndex();
if (!isValid(index)) return "";
return list.at(index);
}
bool isValid(int Index)
{
if (Index < 0 || list.empty()) return false;
return Index < list.size();
}
void fixIndex(void)
{
if (index < 0) index = 0;
if (index >= list.size()) index = list.size() > 0 ? list.size() - 1 : 0;
}
private:
QStringList list;
int lineLimit;
int index;
};
class Widget;
class CodeEditor : public QLineEdit
{
Q_OBJECT
using IconStrList = CMDListWidget::IconStrList;
public:
explicit CodeEditor(int width, int height, QWidget* parent = nullptr);
friend class Widget;
signals:
//只报告自身内部Size,不理睬主窗体Margin(所谓,两耳不闻窗外事)
void reportResize(int width, int height); //-1 means Automatic
void returnWithoutEcho(bool noHide = false); //按下回车,且无回显的Text
private:
Executor executor;
QLabel* label = nullptr;
CMDListWidget* lw = nullptr;
//QFileIconProvider iconPro;
CacheIconProvider& iconPro = CacheIconProvider::instance();
QCompleter* comp = nullptr;
Request request;
//QClipboard* clipboard;
const int normalWidth; //LineEditor宽度
const int normalHeight;
const int Margin = DPI(5); //内部Margin,与主窗体无关
const int TextLimit = 128;
const QString Holder_note = "#note?";
PastCodeList pastCodeList { 10 }; //code历史记录//不能用()存在歧义(函数声明or变量声明)
QAction* act_admin = nullptr;
const QString BasicQss{ "QLineEdit{background-color:rgb(15,15,15);color:rgb(225,225,225);font-family:Consolas;font-size:14pt}" };
// QWidget interface
protected:
void hideEvent(QHideEvent* event) override;
void showEvent(QShowEvent* event) override;
void focusInEvent(QFocusEvent* event) override;
void resizeEvent(QResizeEvent* event) override;
void keyPressEvent(QKeyEvent* event) override;
void keyReleaseEvent(QKeyEvent* event) override;
void wheelEvent(QWheelEvent* event) override;
private:
void showLabel(const QString& text);
void hideLabel(bool isAdjustSize = true);
void showList(const IconStrList& list);
void hideList(bool isAdjustSize = true);
void hideDisplay(void); //list && label
void textEdit(const QString& text);
void adjustWholeSize(const QString& str = QString());
void returnPress(void);
public:
void silent(void);
void wake(const QString& placeHolder = "");
};
#endif // CODEEDITOR_H