-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcodeeditor.cpp
331 lines (292 loc) · 11.4 KB
/
codeeditor.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <QtWidgets>
#include <QDebug>
#include "codeeditor.h"
//![constructor]
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
connect(this,SIGNAL(cursorPositionChanged()),this,SLOT(showCompleteWidget()));
updateLineNumberAreaWidth(0);
highlightCurrentLine();
//set color value
lineColor.setRgb(56,60,69);
editorColor.setRgb(34,39,49);
//set background color
QPalette p = this->palette();
p.setColor(QPalette::Active, QPalette::Base, editorColor);
p.setColor(QPalette::Inactive, QPalette::Base, editorColor);
p.setColor(QPalette::Text,Qt::white);
this->setPalette(p);
//初始化补全列表
setUpCompleteList();
completeWidget= new CompleteListWidget(this);
completeWidget->hide();
completeWidget->setMaximumHeight(fontMetrics().height()*5);
completeState=CompleteState::Hide;
}
//![constructor]
//![extraAreaWidth]
int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;//行数数字的位数
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
if(digits<3)digits=3;
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
//![extraAreaWidth]
//![slotUpdateExtraAreaWidth]
//设定左边留白的宽度,参数无效,没有用到
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
//![slotUpdateExtraAreaWidth]
//![slotUpdateRequest]
//文本框滚动时同时滚动行数
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
//![slotUpdateRequest]
//![resizeEvent]
//尺寸调整函数
void CodeEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
//![resizeEvent]
//![cursorPositionChanged]
void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::red).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
//![cursorPositionChanged]
//![extraAreaPaintEvent_0]
//打印行号,此函数被paintEvent 调用,paintEvent在头文件里被重写
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), lineColor);
//![extraAreaPaintEvent_0]
//![extraAreaPaintEvent_1]
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
//![extraAreaPaintEvent_1]
//![extraAreaPaintEvent_2]
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::lightGray);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignCenter, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
//![extraAreaPaintEvent_2]
//自动补全
void CodeEditor::keyPressEvent(QKeyEvent *event){
//qDebug()<<event->key();
if(event->modifiers()==Qt::ShiftModifier&&event->key()==40){
this->insertPlainText(tr("()"));
this->moveCursor(QTextCursor::PreviousCharacter);
}
else if(event->modifiers()==Qt::ShiftModifier&&event->key()==34){
this->insertPlainText(tr("\"\""));
this->moveCursor(QTextCursor::PreviousCharacter);
}
else if(event->key()==16777235&&completeState==CompleteState::Showing){
if(completeWidget->currentRow()>0)
completeWidget->setCurrentRow(completeWidget->currentRow()-1);
}
else if(event->key()==16777237&&(completeState==CompleteState::Showing)){
if(completeWidget->currentRow()<completeWidget->count()-1)
completeWidget->setCurrentRow(completeWidget->currentRow()+1);
}
else if(event->key()==Qt::Key_Return&&(completeState==CompleteState::Showing)){
QString insertText=completeWidget->currentItem()->text();
QString word=this->getWordOfCursor();
completeState=CompleteState::Ignore;
for(int i=0;i<word.count();++i)
this->textCursor().deletePreviousChar();
this->insertPlainText(insertText);
if(insertText.contains(tr("#include")))
this->moveCursor(QTextCursor::PreviousCharacter);
completeState=CompleteState::Hide;
completeWidget->hide();
}//*
else if(event->key()==Qt::Key_Return){//回车下行层级自动缩进功能
//获得本行的文本
QString temp=this->document()->findBlockByLineNumber(this->textCursor().blockNumber()).text();
QPlainTextEdit::keyPressEvent(event);
if(temp.count()<=0)return;
//输出回车那一行的前距
foreach(const QChar &c,temp){
if(c.isSpace())this->insertPlainText(c);
else break;
}
//如果是for() while() switch() if()则缩进一个tab,一种粗略地做法可能会出错
if(temp.at(temp.count()-1)==')'&&(temp.contains(tr("for("))||temp.contains(tr("while("))
||temp.contains(tr("switch("))||temp.contains(tr("if("))))
this->insertPlainText(tr("\t"));
//如果是{ 则缩进并补}
if(temp.at(temp.count()-1)=='{'){
this->insertPlainText(tr("\t"));
QTextCursor cursor=this->textCursor();
int pos=this->textCursor().position();
this->insertPlainText(tr("\n"));
foreach(const QChar &c,temp){
if(c.isSpace())this->insertPlainText(c);
else break;
}
this->insertPlainText(tr("}"));
cursor.setPosition(pos);
this->setTextCursor(cursor);//返回中间一行
}
}//*/
else if(event->key()==Qt::Key_Backspace){
switch(this->document()->characterAt(this->textCursor().position()-1).toLatin1()){
case '(':
QPlainTextEdit::keyPressEvent(event);
if(this->document()->characterAt(this->textCursor().position())==')'){
this->textCursor().deleteChar();
}break;
case '\"':
QPlainTextEdit::keyPressEvent(event);
if(this->document()->characterAt(this->textCursor().position())=='\"'){
this->textCursor().deleteChar();
}break;
case '<':
QPlainTextEdit::keyPressEvent(event);
if(this->document()->characterAt(this->textCursor().position())=='>'){
this->textCursor().deleteChar();
}break;
default:
QPlainTextEdit::keyPressEvent(event);
}
}
else{
QPlainTextEdit::keyPressEvent(event);
//qDebug()<<event->key();
}
}
void CodeEditor::setUpCompleteList(){
completeList<< "char" << "class" << "const"
<< "double" << "enum" << "explicit"
<< "friend" << "inline" << "int"
<< "long" << "namespace" << "operator"
<< "private" << "protected" << "public"
<< "short" << "signals" << "signed"
<< "slots" << "static" << "struct"
<< "template" << "typedef" << "typename"
<< "union" << "unsigned" << "virtual"
<< "void" << "volatile" << "bool"<<"using"<<"constexpr"
<<"sizeof"<<"if"<<"for"<<"foreach"<<"while"<<"do"<<"case"
<<"break"<<"continue"<<"template"<<"delete"<<"new"
<<"default"<<"try"<<"return"<<"throw"<<"catch"<<"goto"<<"else"
<<"extren"<<"this"<<"switch"<<"#include <>"<<"#include \"\""<<"#define"<<"iostream";
}
//得到当前光标位置的字符串
QString CodeEditor::getWordOfCursor(){
int pos=this->textCursor().position()-1;
QVector<QChar> words;
QString result;
QChar ch=this->document()->characterAt(pos+1);
if(ch.isDigit()||ch.isLetter()||ch==' ')return result;
ch=this->document()->characterAt(pos);
if(ch==' ')return result;
while(ch.isDigit()||ch.isLetter()||ch=='_'||ch=='#'){
words.append(ch);
pos--;
ch=this->document()->characterAt(pos);
}
for(int i=words.size()-1;i>=0;i--)
result+=words[i];
return result;
}
void CodeEditor::showCompleteWidget(){
if(completeState==CompleteState::Ignore)return;//忽略光标和文本变化的响应,避免陷入事件死循环和互相钳制
completeWidget->hide();
completeState=CompleteState::Hide;
QString word=this->getWordOfCursor();
completeWidget->clear();
if(!word.isEmpty()){//光标所在单词是不是合法(能不能联想)
int maxSize=0;
QMap<QString,int> distance;
vector<QString> itemList;
foreach(const QString &temp,completeList){
if(temp.contains(word)){
//completeWidget->addItem(new QListWidgetItem(temp));
itemList.push_back(temp);
distance[temp]=CompleteListWidget::ldistance(temp.toStdString(),word.toStdString());
if(temp.length()>maxSize)maxSize=temp.length();
}
}
//有没有匹配的字符
if(itemList.size()>0){//如果有的话
sort(itemList.begin(),itemList.end(),[&](const QString &s1,const QString &s2)->bool{return distance[s1]<distance[s2]; });
foreach(const QString& item,itemList){
completeWidget->addItem(new QListWidgetItem(item));
}
int x=this->getCompleteWidgetX();
int y=this->cursorRect().y()+fontMetrics().height();
completeWidget->move(x,y);
if(completeWidget->count()>5)completeWidget->setFixedHeight(fontMetrics().height()*6);
else completeWidget->setFixedHeight(fontMetrics().height()*(completeWidget->count()+1));
completeWidget->setFixedWidth((fontMetrics().width(QLatin1Char('9'))+6)*maxSize);
completeWidget->show();
completeState=CompleteState::Showing;
completeWidget->setCurrentRow(0,QItemSelectionModel::Select);
}
}
}
int CodeEditor::getCompleteWidgetX(){
QTextCursor cursor=this->textCursor();
int pos=cursor.position()-1;
int origianlPos=pos+1;
QChar ch;
ch=this->document()->characterAt(pos);
while((ch.isDigit()||ch.isLetter()||ch=='_'||ch=='#')&&pos>0){
pos--;
ch=this->document()->characterAt(pos);
}
pos++;
completeState=CompleteState::Ignore;
cursor.setPosition(pos);
this->setTextCursor(cursor);
int x=this->cursorRect().x()+2*fontMetrics().width(QLatin1Char('9'));
cursor.setPosition(origianlPos);
this->setTextCursor(cursor);
completeState=CompleteState::Hide;
return x;
}