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
|
#include "editor.h"
#include "Preferences.h"
void Editor::indentSelection()
{
QTextCursor cursor = textCursor();
int p1 = cursor.selectionStart();
QString txt = cursor.selectedText();
txt.replace(QString(QChar(8233)), QString(QChar(8233)) + QString("\t"));
if (txt.endsWith(QString(QChar(8233)) + QString("\t")))
txt.chop(1);
txt = QString("\t") + txt;
cursor.insertText(txt);
int p2 = cursor.position();
cursor.setPosition(p1, QTextCursor::MoveAnchor);
cursor.setPosition(p2, QTextCursor::KeepAnchor);
setTextCursor(cursor);
}
void Editor::unindentSelection()
{
QTextCursor cursor = textCursor();
int p1 = cursor.selectionStart();
QString txt = cursor.selectedText();
txt.replace(QString(QChar(8233)) + QString("\t"), QString(QChar(8233)));
if (txt.startsWith(QString("\t")))
txt.remove(0, 1);
cursor.insertText(txt);
int p2 = cursor.position();
cursor.setPosition(p1, QTextCursor::MoveAnchor);
cursor.setPosition(p2, QTextCursor::KeepAnchor);
setTextCursor(cursor);
}
void Editor::commentSelection()
{
QTextCursor cursor = textCursor();
int p1 = cursor.selectionStart();
QString txt = cursor.selectedText();
txt.replace(QString(QChar(8233)), QString(QChar(8233)) + QString("//"));
if (txt.endsWith(QString(QChar(8233)) + QString("//")))
txt.chop(2);
txt = QString("//") + txt;
cursor.insertText(txt);
int p2 = cursor.position();
cursor.setPosition(p1, QTextCursor::MoveAnchor);
cursor.setPosition(p2, QTextCursor::KeepAnchor);
setTextCursor(cursor);
}
void Editor::uncommentSelection()
{
QTextCursor cursor = textCursor();
int p1 = cursor.selectionStart();
QString txt = cursor.selectedText();
txt.replace(QString(QChar(8233)) + QString("//"), QString(QChar(8233)));
if (txt.startsWith(QString("//")))
txt.remove(0, 2);
cursor.insertText(txt);
int p2 = cursor.position();
cursor.setPosition(p1, QTextCursor::MoveAnchor);
cursor.setPosition(p2, QTextCursor::KeepAnchor);
setTextCursor(cursor);
}
void Editor::zoomIn()
{
// See also QT's implementation in QEditor.cpp
QSettings settings;
QFont tmp_font = this->font() ;
if ( font().pointSize() >= 1 )
tmp_font.setPointSize( 1 + font().pointSize() );
else
tmp_font.setPointSize( 1 );
settings.setValue("editor/fontsize", tmp_font.pointSize());
this->setFont( tmp_font );
}
void Editor::zoomOut()
{
QSettings settings;
QFont tmp_font = this->font();
if ( font().pointSize() >= 2 )
tmp_font.setPointSize( -1 + font().pointSize() );
else
tmp_font.setPointSize( 1 );
settings.setValue("editor/fontsize", tmp_font.pointSize());
this->setFont( tmp_font );
}
void Editor::wheelEvent ( QWheelEvent * event )
{
if (event->modifiers() == Qt::ControlModifier) {
if (event->delta() > 0 )
zoomIn();
else if (event->delta() < 0 )
zoomOut();
} else {
QTextEdit::wheelEvent( event );
}
}
void Editor::setPlainText(const QString &text)
{
int y = verticalScrollBar()->sliderPosition();
// Save current cursor position
QTextCursor cursor = textCursor();
int n = cursor.position();
QTextEdit::setPlainText(text);
// Restore cursor position
if (n < text.length()) {
cursor.setPosition(n);
setTextCursor(cursor);
verticalScrollBar()->setSliderPosition(y);
}
}
|