1 //===========================================
2 // Lumina-DE source code
3 // Copyright (c) 2015, Ken Moore
4 // Available under the 3-clause BSD license
5 // See the LICENSE file for full details
6 //===========================================
7 #include "TerminalWidget.h"
8
9 #include <QProcessEnvironment>
10 #include <QDebug>
11 #include <QApplication>
12 #include <QScrollBar>
13 #include <QTextBlock>
14
15 #include <LuminaXDG.h>
16
17 #define DEBUG 0
18
19 //Special control code ending symbols (aside from letters)
20
TerminalWidget(QWidget * parent,QString dir)21 TerminalWidget::TerminalWidget(QWidget *parent, QString dir) : QTextEdit(parent){
22 //Setup the text widget
23 closing = false;
24 QPalette P = this->palette();
25 P.setColor(QPalette::Base, Qt::black);
26 P.setColor(QPalette::Text, Qt::white);
27 this->setPalette(P);
28 this->setLineWrapMode(QTextEdit::WidgetWidth);
29 this->setAcceptRichText(false);
30 this->setOverwriteMode(true);
31 this->setFocusPolicy(Qt::StrongFocus);
32 this->setTabStopWidth( 8 * this->fontMetrics().horizontalAdvance(" ") ); //8 character spaces per tab (UNIX standard)
33 this->setTabChangesFocus(false);
34 //this->setWordWrapMode(QTextOption::NoWrap);
35 this->setContextMenuPolicy(Qt::CustomContextMenu);
36 resizeTimer = new QTimer(this);
37 resizeTimer->setInterval(20);
38 resizeTimer->setSingleShot(true);
39 connect(resizeTimer, SIGNAL(timeout()), this, SLOT(updateTermSize()) );
40 DEFFMT = this->textCursor().charFormat(); //save the default structure for later
41 DEFFMT.setForeground(Qt::white);
42 CFMT = DEFFMT; //current format
43 selCursor = this->textCursor(); //used for keeping track of selections
44 lastCursor = this->textCursor();
45 startrow = endrow = -1;
46 altkeypad = false;
47 QFontDatabase FDB;
48 QStringList fonts = FDB.families(QFontDatabase::Latin);
49 for(int i=0; i<fonts.length(); i++){
50 if(FDB.isFixedPitch(fonts[i]) && FDB.isSmoothlyScalable(fonts[i]) ){ this->setFont(QFont(fonts[i])); qDebug() << "Using Font:" << fonts[i]; break; }
51 //if(FDB.isSmoothlyScalable(fonts[i]) ){ this->setFont(QFont(fonts[i])); qDebug() << "Using Font:" << fonts[i]; break; }
52 }
53 //Create/open the TTY port
54 PROC = new TTYProcess(this);
55 //qDebug() << "Open new TTY";
56 //int fd;
57 bool ok = PROC->startTTY( QProcessEnvironment::systemEnvironment().value("SHELL","/bin/sh"), QStringList(), dir);
58 //qDebug() << " - opened:" << ok;
59 this->setEnabled(false);
60 contextMenu = new QMenu(this);
61 copyA = contextMenu->addAction(LXDG::findIcon("edit-copy"), tr("Copy Selection"), this, SLOT(copySelection()) );
62 pasteA = contextMenu->addAction(LXDG::findIcon("edit-paste"), tr("Paste"), this, SLOT(pasteSelection()) );
63 //Connect the signals/slots
64 connect(PROC, SIGNAL(readyRead()), this, SLOT(UpdateText()) );
65 connect(PROC, SIGNAL(processClosed()), this, SLOT(ShellClosed()) );
66
67 }
68
~TerminalWidget()69 TerminalWidget::~TerminalWidget(){
70 aboutToClose();
71 }
72
setTerminalFont(QFont font)73 void TerminalWidget::setTerminalFont(QFont font){
74
75 this->setFont(font);
76 }
77
aboutToClose()78 void TerminalWidget::aboutToClose(){
79 closing = true;
80 if(PROC->isOpen()){ PROC->closeTTY(); } //TTY PORT
81
82 }
83
84 // ==================
85 // PRIVATE
86 // ==================
InsertText(QString txt)87 void TerminalWidget::InsertText(QString txt){
88 if(txt.isEmpty()){ return; }
89 //qDebug() << "Insert Text:" << txt << "Cursor Pos:" << this->textCursor().position() << "Column:" << this->textCursor().columnNumber();
90 QTextCursor cur = this->textCursor();
91 cur.setCharFormat(CFMT);
92 this->setTextCursor(cur); //ensure the current cursor has the proper format
93 this->insertPlainText(txt);
94 /*cur.setPosition( this->textCursor().position(), QTextCursor::KeepAnchor);
95 //cur.setCharFormat(CFMT);
96 //Now make sure the new characters are the right color
97 QList<QTextEdit::ExtraSelection> sels = this->extraSelections();
98 QTextEdit::ExtraSelection sel;
99 sel.format = CFMT;
100 sel.cursor = cur;
101 sels << sel;
102 this->setExtraSelections(sels);*/
103 if(DEBUG){
104 qDebug() << "Insert Text:"<< txt << "Font Color:" << CFMT.foreground() << "Background Color:" << CFMT.background() << "Font Weight:" << CFMT.fontWeight();
105 }
106 }
107
applyData(QByteArray data)108 void TerminalWidget::applyData(QByteArray data){
109 this->setEnabled(true);
110 if(DEBUG){ qDebug() << "Got Data: " << data; }
111 //Make sure the current cursor is the right cursor
112 if(this->textCursor()==selCursor){ this->setTextCursor(lastCursor); }
113 //Iterate through the data and apply it when possible
114 QByteArray chars;
115 //qDebug() << "Data:" << data;
116 for(int i=0; i<data.size(); i++){
117 if( data.at(i)=='\b' ){
118 //Backspace
119 if(!chars.isEmpty()){ chars.chop(1); }
120 else{
121 QTextCursor cur = this->textCursor();
122 cur.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1);
123 cur.removeSelectedText();
124 this->setTextCursor(cur);
125 }
126
127 //}else if( data.at(i)=='\t' ){
128 //chars.append(" ");
129 }else if( data.at(i)=='\x1B' ){
130 //Flush current text buffer to widget
131 if(!chars.isEmpty()){ InsertText(chars); chars.clear(); }
132 //ANSI Control Code start
133 //Look for the end of the code
134 int end = -1;
135 for(int j=1; j<(data.size()-i) && end<0; j++){
136 if(QChar(data.at(i+j)).isLetter() || (QChar(data.at(i+j)).isSymbol() && data.at(i+j)!=';') ){ end = j; }
137 else if(data.at(i+j)=='\x1B'){ end = j-1; } //start of the next control code
138 }
139 if(end<0){ return; } //skip everything else - no end to code found
140 applyANSI(data.mid(i+1, end));
141 //qDebug() << "Code:" << data.mid(i+1, end) << "Next Char:" << data[i+end+2];
142 i+=end; //move the final loop along - already handled these bytes
143
144 }else if( data.at(i) == '\r' ){
145 //Move cursor to end of line
146 //qDebug() << "Got a return char";
147 QTextCursor cur = this->textCursor();
148 cur.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor, 1);
149 cur.removeSelectedText();
150 this->setTextCursor(cur);
151 }else{
152 chars.append(data.at(i)); //Add the character to the buffer
153 }
154 } //end loop over data
155 if(!chars.isEmpty()){ InsertText(chars); }
156 }
157
applyANSI(QByteArray code)158 void TerminalWidget::applyANSI(QByteArray code){
159 //Note: the first byte is often the "[" character
160 //qDebug() << "Handle ANSI:" << code;
161 if(code.length()==1){
162 //KEYPAD MODES
163 if(code.at(0)=='='){ altkeypad = true; }
164 else if(code.at(0)=='>'){ altkeypad = false; }
165 else if(code.at(0)=='H'){ InsertText("\t"); }
166 else{
167 qDebug() << "Unhandled ANSI Code:" << code;
168 }
169
170 }else if(code.startsWith("[") && code.contains("@")){
171 code = code.remove(0, code.indexOf("@")+1);
172 InsertText(code); //insert character (cursor position already accounted for with other systems)
173 }else if(code.startsWith("[")){
174 // VT100 ESCAPE CODES
175 //CURSOR MOVEMENT
176 if( code.endsWith("A") ){ //Move Up
177 int num = 1;
178 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
179 QTextCursor cur = this->textCursor();
180 qDebug() << "Move Cursor Up:" << num;
181 cur.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, num);
182 this->setTextCursor(cur);
183 }else if(code.endsWith("B")){ //Move Down
184 int num = 1;
185 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
186 QTextCursor cur = this->textCursor();
187 qDebug() << "Move Cursor Down:" << num;
188 for(int i=1; i<num; i++){
189 if(!cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 1)){
190 //Need to add a new line(row) to the editor
191 this->document()->setPlainText(this->document()->toPlainText()+"\n");
192 cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line
193 }
194 }
195 //cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, num);
196 this->setTextCursor(cur);
197 }else if(code.endsWith("C")){ //Move Forward
198 int num = 1;
199 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
200 QTextCursor cur = this->textCursor();
201 qDebug() << "Move Cursor Forward:" << num;
202 for(int i=1; i<num; i++){
203 if(!cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1)){
204 //Need to add a new line(row) to the editor
205 this->document()->setPlainText(this->document()->toPlainText()+" ");
206 cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line
207 }
208 }
209 //cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, num);
210 this->setTextCursor(cur);
211 }else if(code.endsWith("D")){ //Move Back
212 int num = 1;
213 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
214 QTextCursor cur = this->textCursor();
215 qDebug() << "Move Cursor Back:" << num;
216 cur.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, num);
217 this->setTextCursor(cur);
218 }else if(code.endsWith("E")){ //Move Next/down Lines (go toward end)
219 int num = 1;
220 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
221 QTextCursor cur = this->textCursor();
222 qDebug() << "Move Cursor NextRow:" << num;
223 cur.movePosition(QTextCursor::NextRow, QTextCursor::MoveAnchor, num);
224 this->setTextCursor(cur);
225 }else if(code.endsWith("F")){ //Move Previous/up Lines (go to beginning)
226 int num = 1;
227 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
228 QTextCursor cur = this->textCursor();
229 qDebug() << "Move Cursor PrevRow:" << num;
230 cur.movePosition(QTextCursor::PreviousRow, QTextCursor::MoveAnchor, num);
231 this->setTextCursor(cur);
232 }else if(code.endsWith("G")){ //Move to specific column
233 int num = 1;
234 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
235 QTextCursor cur = this->textCursor();
236 qDebug() << "Move Cursor To Column:" << num;
237 cur.setPosition(num);
238 this->setTextCursor(cur);
239 }else if(code.endsWith("H") || code.endsWith("f") ){ //Move to specific position (row/column)
240 int mid = code.indexOf(";");
241 if(mid>1){
242 int numR, numC; numR = numC = 1;
243 if(mid >=2){ numR = code.mid(1,mid-1).toInt(); }
244 if(mid < code.size()-1){ numC = code.mid(mid+1,code.size()-mid-2).toInt(); }
245 //qDebug() << "NumR:" << numR << startrow << endrow << this->document()->lineCount() -1;
246 //qDebug() << "NumC:" << numC;
247 if(startrow>=0 && endrow>=0){
248 if(numR == startrow){ numR = 0;}
249 else if(numR==endrow){ numR = this->document()->lineCount()-1; }
250 }
251 //qDebug() << "Set Text Position (absolute):" << "Code:" << code << "Row:" << numR << "Col:" << numC;
252 //qDebug() << " - Current Pos:" << this->textCursor().position() << "Line Count:" << this->document()->lineCount();
253 //if(!this->textCursor().movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1) ){ qDebug() << "Could not go to start"; }
254 QTextCursor cur(this->textCursor());
255 cur.setPosition(QTextCursor::Start, QTextCursor::MoveAnchor); //go to start of document
256 //qDebug() << " - Pos After Start Move:" << cur.position();
257 for(int i=1; i<numR; i++){
258 if(!cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 1)){
259 //Need to add a new line(row) to the editor
260 this->document()->setPlainText(this->document()->toPlainText()+"\n");
261 cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line
262 }
263 }
264 //if( !cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numR) ){ qDebug() << "Could not go to row:" << numR; }
265 //qDebug() << " - Pos After Down Move:" << cur.position();
266 for(int i=1; i<numC; i++){
267 if(!cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1)){
268 //Need to add a new line(row) to the editor
269 this->document()->setPlainText(this->document()->toPlainText()+" ");
270 cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line
271 }
272 }
273 //if( !cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numC) ){ qDebug() << "Could not go to col:" << numC; }
274 /*this->textCursor().setPosition( this->document()->findBlockByLineNumber(numR).position() );
275 qDebug() << " - Pos After Row Move:" << this->textCursor().position();
276 if( !this->textCursor().movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numC) ){ qDebug() << "Could not go to col:" << numC; }*/
277 //qDebug() << " - Ending Pos:" << cur.position();
278 this->setTextCursor(cur);
279 }else{
280 //Go to home position
281 this->moveCursor(QTextCursor::Start);
282 }
283
284 // CURSOR MANAGEMENT
285 }else if(code.endsWith("r")){ //Tag top/bottom lines as perticular numbers
286 int mid = code.indexOf(";");
287 qDebug() << "New Row Codes:" << code << "midpoint:" << mid;
288 if(mid>1){
289 if(mid >=2){ startrow = code.mid(1,mid-1).toInt(); }
290 if(mid < code.size()-1){ endrow = code.mid(mid+1,code.size()-mid-2).toInt(); }
291 }
292 qDebug() << "New Row Codes:" << startrow << endrow;
293 // DISPLAY CLEAR CODES
294 }else if(code.endsWith("J")){ //ED - Erase Display
295 int num = 0;
296 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
297 //qDebug() << "Erase Display:" << num;
298 if(num==1){
299 //Clear from cursor to beginning of screen
300 QTextCursor cur = this->textCursor();
301 cur.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor, 1);
302 cur.removeSelectedText();
303 this->setTextCursor(cur);
304 }else if(num==2){
305 //Clear the whole screen
306 qDebug() << "Clear Screen:" << this->document()->lineCount();
307 this->clear();
308 }else{
309 //Clear from cursor to end of screen
310 QTextCursor cur = this->textCursor();
311 cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor, 1);
312 cur.removeSelectedText();
313 this->setTextCursor(cur);
314 }
315 }else if(code.endsWith("K")){ //EL - Erase in Line
316 int num = 0;
317 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
318 //qDebug() << "Erase Number" << num;
319 //Now determine what should be cleared based on code
320 if(num==1){
321 //Clear from current cursor to beginning of line
322 QTextCursor cur = this->textCursor();
323 cur.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor, 1);
324 cur.removeSelectedText();
325 this->setTextCursor(cur);
326 }else if(num==2){
327 //Clear the entire line
328 QTextCursor cur = this->textCursor();
329 cur.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor, 1);
330 cur.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1);
331 cur.removeSelectedText();
332 this->setTextCursor(cur);
333 }else{
334 //Clear from current cursor to end of line
335 QTextCursor cur = this->textCursor();
336 cur.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1);
337 cur.removeSelectedText();
338 this->setTextCursor(cur);
339 }
340 }else if(code.endsWith("g")){
341 //Tab Clear codes (0 or 3 only)
342 int num = 0;
343 if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
344 if(num==0){ //clear current column (delete key analogue)
345 QTextCursor cur = this->textCursor();
346 cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1);
347 cur.removeSelectedText();
348 this->setTextCursor(cur);
349 }else if(num==3){ //clear all
350 this->clear();
351 }
352 //SCROLL MOVEMENT CODES
353 //}else if(code.endsWith("S")){ // SU - Scroll Up
354 //qDebug() << "Scroll Up:" << code;
355 //}else if(code.endsWith("T")){ // SD - Scroll Down
356 //qDebug() << "Scroll Down:" << code;
357
358 // GRAPHICS RENDERING
359 }else if(code.endsWith("m")){
360 //Format: "[<number>;<number>m" (no limit to sections separated by ";")
361 //qDebug() << "Got Graphics Code:" << code;
362 code.chop(1); //chop the "m" off the end
363 int start = 1;
364 int end = code.indexOf(";");
365 while(end>start){
366 //qDebug() << "Color Code:" << code << start << end << code.mid(start, end-start);
367 applyANSIColor(code.mid(start, end-start).toInt());
368 //Now update the iterators and try again
369 start = end;
370 end = code.indexOf(";",start); //go to the next one
371 //qDebug() << "Next end:" << end;
372 }
373 //Need the last section as well
374 end = code.size();
375 if(start>1){ start ++; }
376 //qDebug() << "Color Code:" << code << start << end << code.mid(start, end-start);
377 if(end>start){ applyANSIColor(code.mid(start, end-start).toInt());}
378 else{ applyANSIColor(0); }
379
380
381 // GRAPHICS MODES
382 //}else if(code.endsWith("h")){
383
384 //}else if(code.endsWith("l")){
385
386 }else{
387 qDebug() << "Unhandled Control Code:" << code;
388 }
389
390 } //End VT100 control codes
391 else{
392 qDebug() << "Unhandled Control Code:" << code;
393 }
394 }
395
applyANSIColor(int code)396 void TerminalWidget::applyANSIColor(int code){
397 //qDebug() << "Apply Color code:" << code;
398 if(code <=0){ CFMT = DEFFMT; } //Reset back to default
399 else if(code==1){ CFMT.setFontWeight(75); } //BOLD font
400 else if(code==2){ CFMT.setFontWeight(25); } //Faint font (smaller than normal by a bit)
401 else if(code==3){ CFMT.setFontWeight(75); } //Italic font
402 else if(code==4){ CFMT.setFontUnderline(true); } //Underline
403 //5-6: Blink text (unsupported)
404 //7: Reverse foreground/background (unsupported)
405 //8: Conceal (unsupported)
406 else if(code==9){ CFMT.setFontStrikeOut(true); } //Crossed out
407 //10-19: Change font family (unsupported)
408 //20: Fraktur Font (unsupported)
409 //21: Bold:off or Underline:Double (unsupported)
410 else if(code==22){ CFMT.setFontWeight(50); } //Normal weight
411 //23: Reset font (unsupported)
412 else if(code==24){ CFMT.setFontUnderline(false); } //disable underline
413 //25: Disable blinking (unsupported)
414 //26: Reserved
415 //27: Reset reversal (7) (unsupported)
416 //28: Reveal (cancel 8) (unsupported)
417 else if(code==29){ CFMT.setFontStrikeOut(false); } //Not Crossed out
418 else if(code>=30 && code<=39){
419 //Set the font color
420 QColor color;
421 if(code==30){color=QColor(Qt::black); }
422 else if(code==31){ color=QColor(Qt::red); }
423 else if(code==32){ color=QColor(Qt::green); }
424 else if(code==33){ color=QColor(Qt::yellow); }
425 else if(code==34){ color=QColor(Qt::blue); }
426 else if(code==35){ color=QColor(Qt::magenta); }
427 else if(code==36){ color=QColor(Qt::cyan); }
428 else if(code==37){ color=QColor(Qt::white); }
429 //48: Special extended color setting (unsupported)
430 else if(code==39){ color= DEFFMT.foreground().color(); } //reset to default color
431 QBrush brush = CFMT.foreground();
432 color.setAlpha(255); //fully opaque
433 brush.setColor(color);
434 CFMT.setForeground( brush );
435 //this->setTextColor(color); //just in case the format is not used
436 }
437 else if(code>=40 && code<=49){
438 //Set the font color
439 QColor color;
440 if(code==40){color=QColor(Qt::black); }
441 else if(code==41){ color=QColor(Qt::red); }
442 else if(code==42){ color=QColor(Qt::green); }
443 else if(code==43){ color=QColor(Qt::yellow); }
444 else if(code==44){ color=QColor(Qt::blue); }
445 else if(code==45){ color=QColor(Qt::magenta); }
446 else if(code==46){ color=QColor(Qt::cyan); }
447 else if(code==47){ color=QColor(Qt::white); }
448 //48: Special extended color setting (unsupported)
449 else if(code==49){ color= DEFFMT.background().color(); } //reset to default color
450 QBrush brush = CFMT.background();
451 color.setAlpha(255); //fully opaque
452 brush.setColor(color);
453 CFMT.setBackground( brush );
454 }
455 //50: Reserved
456 //51: Framed
457 //52: Encircled
458 else if(code==53){ CFMT.setFontOverline(true); } //enable overline
459 //54: Not framed/circled (51/52)
460 else if(code==55){ CFMT.setFontOverline(false); } //disable overline
461 //56-59: Reserved
462 //60+: Not generally supported (special code for particular terminals such as aixterm)
463 else{ qDebug() << "Unknown Color Code:" << code; }
464 }
465
466 //Outgoing Data parsing
sendKeyPress(int key)467 void TerminalWidget::sendKeyPress(int key){
468 QByteArray ba;
469 //if(this->textCursor()==selCursor){ this->setTextCursor(lastCursor); }
470 //int fromEnd = this->document()->characterCount() - this->textCursor().position();
471 //Check for special keys
472 switch(key){
473 case Qt::Key_Delete:
474 ba.append("\e[3~");
475 break;
476 case Qt::Key_Backspace:
477 ba.append("\x08");
478 break;
479 case Qt::Key_Left:
480 if(altkeypad){ ba.append("^[D"); }
481 else{ ba.append("\x1b[D"); }
482 break;
483 case Qt::Key_Right:
484 if(altkeypad){ ba.append("^[C"); }
485 else{ ba.append("\x1b[C"); }
486 break;
487 case Qt::Key_Up:
488 if(altkeypad){ ba.append("^[A"); }
489 else{ ba.append("\x1b[A"); }
490 break;
491 case Qt::Key_Down:
492 if(altkeypad){ ba.append("^[B"); }
493 else{ ba.append("\x1b[B"); }
494 break;
495 case Qt::Key_Home:
496 ba.append("\x1b[H");
497 break;
498 case Qt::Key_End:
499 ba.append("\x1b[F");
500 break;
501 }
502 //qDebug() << "Forward Input:" << ba;
503 if(!ba.isEmpty()){ PROC->writeTTY(ba); }
504 }
505
506 // ==================
507 // PRIVATE SLOTS
508 // ==================
UpdateText()509 void TerminalWidget::UpdateText(){
510 //read the data from the process
511 //qDebug() << "UpdateText";
512 if(!PROC->isOpen()){ return; }
513 applyData(PROC->readTTY());
514 //adjust the scrollbar as needed
515 this->ensureCursorVisible();
516 }
517
ShellClosed()518 void TerminalWidget::ShellClosed(){
519 if(!closing){
520 emit ProcessClosed(this->whatsThis());
521 }
522 }
523
copySelection()524 void TerminalWidget::copySelection(){
525 QApplication::clipboard()->setText( selCursor.selectedText() );
526 }
527
pasteSelection()528 void TerminalWidget::pasteSelection(){
529 QString text = QApplication::clipboard()->text();
530 if(!text.isEmpty()){
531 QByteArray ba; ba.append(text); //avoid any byte conversions
532 PROC->writeTTY(ba);
533 }
534 }
535
updateTermSize()536 void TerminalWidget::updateTermSize(){
537 if(!PROC->isOpen()){ return; }
538 QSize pix = this->size(); //pixels
539 QSize chars;
540 chars.setWidth( pix.width()/this->fontMetrics().horizontalAdvance("W") );
541 chars.setHeight( pix.height()/this->fontMetrics().lineSpacing() );
542 //qDebug() << "Set Terminal Size:" << chars << pix;
543 if(chars.width() <2 || chars.height() <2){ return; } //skip this - cannot go less than 2 characters wide/high
544 PROC->setTerminalSize(chars,pix);
545 }
546
547 // ==================
548 // PROTECTED
549 // ==================
keyPressEvent(QKeyEvent * ev)550 void TerminalWidget::keyPressEvent(QKeyEvent *ev){
551
552 if(ev->text().isEmpty() || ev->text()=="\b" ){
553 sendKeyPress(ev->key());
554 }else{
555 QByteArray ba; ba.append(ev->text()); //avoid any byte conversions
556 //qDebug() << "Forward Input:" << ba;
557 PROC->writeTTY(ba);
558 }
559
560 ev->ignore();
561 }
562
mousePressEvent(QMouseEvent * ev)563 void TerminalWidget::mousePressEvent(QMouseEvent *ev){
564 this->setFocus();
565 this->activateWindow();
566 if(ev->button()==Qt::RightButton){
567 QTextEdit::mousePressEvent(ev);
568 }else if(ev->button()==Qt::MiddleButton){
569 pasteSelection();
570 }else if(ev->button()==Qt::LeftButton){
571 if(this->textCursor()!=selCursor){ lastCursor = this->textCursor(); }
572 selCursor = this->cursorForPosition(ev->pos());
573 }
574 Q_UNUSED(ev);
575 }
576
mouseMoveEvent(QMouseEvent * ev)577 void TerminalWidget::mouseMoveEvent(QMouseEvent *ev){
578 //qDebug() << "MouseMove Event" << ev->button() << ev->buttons() << Qt::LeftButton;
579 if(ev->buttons().testFlag(Qt::LeftButton) ){
580 selCursor.setPosition(this->cursorForPosition(ev->pos()).position(), QTextCursor::KeepAnchor);
581 if(selCursor.hasSelection()){ this->setTextCursor(selCursor); }
582 //qDebug() << "Mouse Movement:" << selCursor.hasSelection();
583 }else{
584 QTextEdit::mouseMoveEvent(ev);
585 }
586 }
587
mouseReleaseEvent(QMouseEvent * ev)588 void TerminalWidget::mouseReleaseEvent(QMouseEvent *ev){
589 if(ev->button()==Qt::LeftButton){
590 selCursor.setPosition(this->cursorForPosition(ev->pos()).position(), QTextCursor::KeepAnchor);
591 if(selCursor.hasSelection()){ this->setTextCursor(selCursor); }
592 else{ this->setTextCursor(lastCursor); }
593 }else if(ev->button()==Qt::RightButton){
594 copyA->setEnabled( selCursor.hasSelection() );
595 pasteA->setEnabled( !QApplication::clipboard()->text().isEmpty() );
596 contextMenu->popup( this->mapToGlobal(ev->pos()) );
597 }
598 Q_UNUSED(ev);
599 }
600
mouseDoubleClickEvent(QMouseEvent * ev)601 void TerminalWidget::mouseDoubleClickEvent(QMouseEvent *ev){
602 Q_UNUSED(ev);
603 }
604
resizeEvent(QResizeEvent * ev)605 void TerminalWidget::resizeEvent(QResizeEvent *ev){
606 if(resizeTimer->isActive()){ resizeTimer->stop(); }
607 resizeTimer->start();
608 QTextEdit::resizeEvent(ev);
609 }
610