not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / ksysguard / processui / KTextEditVT.cpp
blob4b505b65318f405f1ef4d646e78068c21471003e
1 /*
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.
25 #include <klocale.h>
26 #include "KTextEditVT.h"
28 #include "KTextEditVT.moc"
29 #include <kglobalsettings.h>
31 KTextEditVT::KTextEditVT(QWidget* parent)
32 : QTextEdit( parent )
34 mParseAnsi = true;
35 escape_sequence = false;
36 escape_CSI = false;
37 escape_OSC = false;
38 escape_number1 = -1;
39 escape_number_seperator = false;
40 escape_number2 = -1;
41 escape_code = 0;
42 setFont( KGlobalSettings::fixedFont() );
46 void KTextEditVT::insertVTChar(const QChar & c) {
47 if(escape_sequence) {
48 if(escape_CSI || escape_OSC) {
49 if(c.isDigit()) {
50 if(!escape_number_seperator) {
51 if(escape_number1 == -1)
52 escape_number1 = c.digitValue();
53 else
54 escape_number1 = escape_number1*10 + c.digitValue();
55 } else {
56 if(escape_number2 == -1)
57 escape_number2 = c.digitValue();
58 else
59 escape_number2 = escape_number2*10 + c.digitValue();
62 } else if(c == ';') {
63 escape_number_seperator = true;
64 } else if(escape_OSC && c==7) { //Throw away any letters that are not OSC
65 escape_code = c;
66 } else if(escape_CSI)
67 escape_code = c;
68 } else if(c=='[') {
69 escape_CSI = true;
70 } else if(c==']') {
71 escape_OSC = true;
73 else if(c=='(' || c==')') {}
74 else
75 escape_code = 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){
80 case 0: //all off
81 setFontWeight(QFont::Normal);
82 setTextColor(Qt::black);
83 break;
84 case 1: //bold
85 setFontWeight(QFont::Bold);
86 break;
87 case 31: //red
88 setTextColor(Qt::red);
89 break;
90 case 32: //green
91 setTextColor(Qt::green);
92 break;
93 case 33: //yellow
94 setTextColor(Qt::yellow);
95 break;
96 case 34: //blue
97 setTextColor(Qt::blue);
98 break;
99 case 35: //magenta
100 setTextColor(Qt::magenta);
101 break;
102 case 36: //cyan
103 setTextColor(Qt::cyan);
104 break;
105 case -1:
106 case 30: //black
107 case 39: //reset
108 case 37: //white
109 setTextColor(Qt::black);
110 break;
113 escape_code = 0;
114 escape_number1 = -1;
115 escape_number2 = -1;
116 escape_CSI = false;
117 escape_OSC = false;
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;
132 escape_CSI = true;
133 } else if(c==0x9d) { // OSC - equivalent to esc ]
134 escape_sequence = true;
135 escape_OSC = true;
138 } else if(!c.isNull()) {
139 insertPlainText("[");
140 QByteArray num;
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
168 return mParseAnsi;