Skip to content

Commit fa186dc

Browse files
committed
Snippet Area
1 parent 6e36209 commit fa186dc

17 files changed

Lines changed: 1966 additions & 1890 deletions

CodeEditor/codeeditor.cpp

Lines changed: 146 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -45,189 +45,176 @@
4545
#include <QTextStream>
4646
#include "CodeEditor/codeeditor.h"
4747

48-
CodeEditor::CodeEditor(QWidget* parent)
49-
: QPlainTextEdit(parent)
50-
{
51-
lineNumberArea = new LineNumberArea(this);
48+
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
49+
lineNumberArea = new LineNumberArea(this);
5250

53-
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
54-
connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int)));
51+
connect(this, SIGNAL(blockCountChanged(int)), this,
52+
SLOT(updateLineNumberAreaWidth(int)));
53+
connect(this, SIGNAL(updateRequest(QRect, int)), this,
54+
SLOT(updateLineNumberArea(QRect, int)));
5555

56-
updateLineNumberAreaWidth(0);
56+
updateLineNumberAreaWidth(0);
5757

58-
QPalette p = this->palette();
59-
p.setColor(QPalette::Base, Qt::black);
60-
p.setColor(QPalette::Text, Qt::white);
61-
this->setPalette(p);
62-
}
63-
void CodeEditor::setSingleLine(bool isSingleLine)
64-
{
65-
mSingleLine = isSingleLine;
58+
QPalette p = this->palette();
59+
p.setColor(QPalette::Base, Qt::black);
60+
p.setColor(QPalette::Text, Qt::white);
61+
this->setPalette(p);
6662
}
67-
int CodeEditor::lineNumberAreaWidth()
68-
{
69-
int digits = 1;
70-
int max = qMax(1, blockCount());
71-
while (max >= 10) {
72-
max /= 10;
73-
++digits;
74-
}
63+
int CodeEditor::lineNumberAreaWidth() {
64+
int digits = 1;
65+
int max = qMax(1, blockCount());
66+
while (max >= 10) {
67+
max /= 10;
68+
++digits;
69+
}
7570

76-
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
71+
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
7772

78-
return space;
73+
return space;
7974
}
8075

81-
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
82-
{
83-
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
76+
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) {
77+
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
8478
}
8579

86-
void CodeEditor::updateLineNumberArea(const QRect& rect, int dy)
87-
{
88-
if (dy)
89-
lineNumberArea->scroll(0, dy);
90-
else
91-
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
80+
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) {
81+
if (dy)
82+
lineNumberArea->scroll(0, dy);
83+
else
84+
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
9285

93-
if (rect.contains(viewport()->rect()))
94-
updateLineNumberAreaWidth(0);
86+
if (rect.contains(viewport()->rect()))
87+
updateLineNumberAreaWidth(0);
9588
}
9689

97-
void CodeEditor::resizeEvent(QResizeEvent* e)
98-
{
99-
QPlainTextEdit::resizeEvent(e);
90+
void CodeEditor::resizeEvent(QResizeEvent *e) {
91+
QPlainTextEdit::resizeEvent(e);
10092

101-
QRect cr = contentsRect();
102-
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
93+
QRect cr = contentsRect();
94+
lineNumberArea->setGeometry(
95+
QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
10396
}
10497

105-
void CodeEditor::SelectLineMarginBlock()
106-
{
107-
int start, end;
108-
109-
// get current positions
110-
start = this->textCursor().selectionStart();
111-
end = this->textCursor().selectionEnd();
112-
113-
QTextCursor cursor(this->document());
114-
115-
// move cursor to begin of the line, of the line
116-
// start position is located.
117-
cursor.clearSelection();
118-
cursor.setPosition(start);
119-
cursor.movePosition(QTextCursor::StartOfLine);
120-
this->setTextCursor(cursor);
121-
start = this->textCursor().selectionStart();
122-
123-
// move cursor to end of the line, of the line
124-
// end position is located.
125-
cursor.setPosition(end);
126-
cursor.movePosition(QTextCursor::EndOfLine);
127-
this->setTextCursor(cursor);
128-
end = this->textCursor().selectionEnd();
129-
130-
// select line margin block
131-
cursor.setPosition(start, QTextCursor::KeepAnchor);
132-
this->setTextCursor(cursor);
98+
void CodeEditor::SelectLineMarginBlock() {
99+
int start, end;
100+
101+
// get current positions
102+
start = this->textCursor().selectionStart();
103+
end = this->textCursor().selectionEnd();
104+
105+
QTextCursor cursor(this->document());
106+
107+
// move cursor to begin of the line, of the line
108+
// start position is located.
109+
cursor.clearSelection();
110+
cursor.setPosition(start);
111+
cursor.movePosition(QTextCursor::StartOfLine);
112+
this->setTextCursor(cursor);
113+
start = this->textCursor().selectionStart();
114+
115+
// move cursor to end of the line, of the line
116+
// end position is located.
117+
cursor.setPosition(end);
118+
cursor.movePosition(QTextCursor::EndOfLine);
119+
this->setTextCursor(cursor);
120+
end = this->textCursor().selectionEnd();
121+
122+
// select line margin block
123+
cursor.setPosition(start, QTextCursor::KeepAnchor);
124+
this->setTextCursor(cursor);
133125
}
134-
QString CodeEditor::GetLine()
135-
{
136-
int start = this->textCursor().position();
137-
QTextCursor cursor(this->document());
138-
cursor.setPosition(start);
139-
cursor.movePosition(QTextCursor::StartOfLine);
140-
this->setTextCursor(cursor);
141-
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
142-
this->setTextCursor(cursor);
143-
QString text = this->textCursor().selection().toPlainText();
144-
cursor.movePosition(QTextCursor::EndOfLine);
145-
this->setTextCursor(cursor);
146-
return text;
126+
QString CodeEditor::GetLine() {
127+
int start = this->textCursor().position();
128+
QTextCursor cursor(this->document());
129+
cursor.setPosition(start);
130+
cursor.movePosition(QTextCursor::StartOfLine);
131+
this->setTextCursor(cursor);
132+
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
133+
this->setTextCursor(cursor);
134+
QString text = this->textCursor().selection().toPlainText();
135+
cursor.movePosition(QTextCursor::EndOfLine);
136+
this->setTextCursor(cursor);
137+
return text;
147138
}
148-
bool CodeEditor::KeepIndent()
149-
{
150-
if (this->textCursor().hasSelection() || !this->textCursor().atBlockEnd()) {
151-
return false;
152-
}
153-
QRegExp spaces("(\\s*).*");
139+
bool CodeEditor::KeepIndent() {
140+
if (this->textCursor().hasSelection() || !this->textCursor().atBlockEnd()) {
141+
return false;
142+
}
143+
QRegExp spaces("(\\s*).*");
154144

155-
spaces.indexIn(GetLine());
156-
this->insertPlainText(tr("\n"));
157-
this->insertPlainText(spaces.cap(1));
145+
spaces.indexIn(GetLine());
146+
this->insertPlainText(tr("\n"));
147+
this->insertPlainText(spaces.cap(1));
158148

159-
return true;
149+
return true;
160150
}
161-
void CodeEditor::keyPressEvent(QKeyEvent* e)
162-
{
163-
switch (e->key()) {
164-
case Qt::Key_Backtab:
165-
SelectLineMarginBlock();
166-
{
167-
QString text("");
168-
QStringList lines = this->textCursor().selection().toPlainText().split(QRegExp("\n|\r\n|\r"));
169-
foreach (QString line, lines) {
170-
line.replace(QRegExp("^( | | | )(.*)"), "\\2");
171-
text.append(line);
172-
text.append("\n");
173-
}
174-
text.truncate(text.length() - 1);
175-
this->textCursor().insertText(text);
176-
}
177-
break;
178-
case Qt::Key_Tab:
179-
if (this->textCursor().hasSelection()) {
180-
SelectLineMarginBlock();
181-
{
182-
QString text("");
183-
QStringList lines = this->textCursor().selection().toPlainText().split(QRegExp("\n|\r\n|\r"));
184-
foreach (QString line, lines) {
185-
text.append(" ");
186-
text.append(line);
187-
text.append("\n");
188-
}
189-
text.truncate(text.length() - 1);
190-
this->textCursor().insertText(text);
191-
}
192-
} else {
193-
this->insertPlainText(" ");
194-
}
195-
break;
196-
case Qt::Key_Enter:
197-
case Qt::Key_Return:
198-
if (mSingleLine){
199-
break;
200-
}
201-
if (!KeepIndent()) {
202-
QPlainTextEdit::keyPressEvent(e);
151+
void CodeEditor::keyPressEvent(QKeyEvent *e) {
152+
switch (e->key()) {
153+
case Qt::Key_Backtab:
154+
SelectLineMarginBlock();
155+
{
156+
QString text("");
157+
QStringList lines = this->textCursor().selection().toPlainText().split(
158+
QRegExp("\n|\r\n|\r"));
159+
foreach (QString line, lines) {
160+
line.replace(QRegExp("^( | | | )(.*)"), "\\2");
161+
text.append(line);
162+
text.append("\n");
163+
}
164+
text.truncate(text.length() - 1);
165+
this->textCursor().insertText(text);
166+
}
167+
break;
168+
case Qt::Key_Tab:
169+
if (this->textCursor().hasSelection()) {
170+
SelectLineMarginBlock();
171+
{
172+
QString text("");
173+
QStringList lines = this->textCursor().selection().toPlainText().split(
174+
QRegExp("\n|\r\n|\r"));
175+
foreach (QString line, lines) {
176+
text.append(" ");
177+
text.append(line);
178+
text.append("\n");
203179
}
204-
break;
205-
default:
206-
QPlainTextEdit::keyPressEvent(e);
180+
text.truncate(text.length() - 1);
181+
this->textCursor().insertText(text);
182+
}
183+
} else {
184+
this->insertPlainText(" ");
185+
}
186+
break;
187+
case Qt::Key_Enter:
188+
case Qt::Key_Return:
189+
if (!KeepIndent()) {
190+
QPlainTextEdit::keyPressEvent(e);
207191
}
192+
break;
193+
default:
194+
QPlainTextEdit::keyPressEvent(e);
195+
}
208196
}
209197

210-
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event)
211-
{
212-
QPainter painter(lineNumberArea);
213-
painter.fillRect(event->rect(), Qt::darkGray);
214-
215-
QTextBlock block = firstVisibleBlock();
216-
int blockNumber = block.blockNumber();
217-
int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
218-
int bottom = top + (int)blockBoundingRect(block).height();
219-
220-
while (block.isValid() && top <= event->rect().bottom()) {
221-
if (block.isVisible() && bottom >= event->rect().top()) {
222-
QString number = QString::number(blockNumber + 1);
223-
painter.setPen(Qt::black);
224-
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
225-
Qt::AlignRight, number);
226-
}
227-
228-
block = block.next();
229-
top = bottom;
230-
bottom = top + (int)blockBoundingRect(block).height();
231-
++blockNumber;
198+
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) {
199+
QPainter painter(lineNumberArea);
200+
painter.fillRect(event->rect(), Qt::darkGray);
201+
202+
QTextBlock block = firstVisibleBlock();
203+
int blockNumber = block.blockNumber();
204+
int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
205+
int bottom = top + (int)blockBoundingRect(block).height();
206+
207+
while (block.isValid() && top <= event->rect().bottom()) {
208+
if (block.isVisible() && bottom >= event->rect().top()) {
209+
QString number = QString::number(blockNumber + 1);
210+
painter.setPen(Qt::black);
211+
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
212+
Qt::AlignRight, number);
232213
}
214+
215+
block = block.next();
216+
top = bottom;
217+
bottom = top + (int)blockBoundingRect(block).height();
218+
++blockNumber;
219+
}
233220
}

0 commit comments

Comments
 (0)