2 KSysGuard, the KDE System Guard
4 Copyright (C) 2007 Trent Waddington <trent.waddington@gmail.com>
5 Copyright (c) 2008 John Tapsell <tapsell@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
26 #include "KTextEditVT.h"
28 #include "KTextEditVT.moc"
29 #include <kglobalsettings.h>
31 KTextEditVT::KTextEditVT(QWidget
* parent
)
35 escape_sequence
= false;
39 escape_number_seperator
= false;
42 setFont( KGlobalSettings::fixedFont() );
46 void KTextEditVT::insertVTChar(const QChar
& c
) {
48 if(escape_CSI
|| escape_OSC
) {
50 if(!escape_number_seperator
) {
51 if(escape_number1
== -1)
52 escape_number1
= c
.digitValue();
54 escape_number1
= escape_number1
*10 + c
.digitValue();
56 if(escape_number2
== -1)
57 escape_number2
= c
.digitValue();
59 escape_number2
= escape_number2
*10 + c
.digitValue();
63 escape_number_seperator
= true;
64 } else if(escape_OSC
&& c
==7) { //Throw away any letters that are not OSC
73 else if(c
=='(' || c
==')') {}
76 if(!escape_code
.isNull()) {
77 //We've read in the whole escape sequence. Now parse it
78 if(escape_code
== 'm') { // change color
79 switch(escape_number2
){
81 setFontWeight(QFont::Normal
);
82 setTextColor(Qt::black
);
85 setFontWeight(QFont::Bold
);
88 setTextColor(Qt::red
);
91 setTextColor(Qt::green
);
94 setTextColor(Qt::yellow
);
97 setTextColor(Qt::blue
);
100 setTextColor(Qt::magenta
);
103 setTextColor(Qt::cyan
);
109 setTextColor(Qt::black
);
118 escape_sequence
= false;
119 escape_number_seperator
= false;
121 } else if(c
== 0x0d) {
122 insertPlainText(QChar('\n'));
123 } else if(c
.isPrint() || c
== '\n') {
124 insertPlainText(QChar(c
));
125 } else if(mParseAnsi
) {
126 if(c
== 127 || c
== 8) { // delete or backspace, respectively
127 textCursor().deletePreviousChar();
128 } else if(c
==27) { // escape key
129 escape_sequence
= true;
130 } else if(c
==0x9b) { // CSI - equivalent to esc [
131 escape_sequence
= true;
133 } else if(c
==0x9d) { // OSC - equivalent to esc ]
134 escape_sequence
= true;
138 } else if(!c
.isNull()) {
139 insertPlainText("[");
141 num
.setNum(c
.toAscii());
142 insertPlainText(num
);
143 insertPlainText("]");
147 void KTextEditVT::insertVTText(const QByteArray
& string
)
149 int size
= string
.size();
150 for(int i
=0; i
< size
; i
++)
151 insertVTChar(QChar(string
.at(i
)));
154 void KTextEditVT::insertVTText(const QString
& string
)
156 int size
= string
.size();
157 for(int i
=0; i
< size
; i
++)
158 insertVTChar(string
.at(i
));
161 void KTextEditVT::setParseAnsiEscapeCodes(bool parseAnsi
)
163 mParseAnsi
= parseAnsi
;
166 bool KTextEditVT::parseAnsiEscapeCodes() const