aboutsummaryrefslogtreecommitdiff
path: root/desktop-utilities/lumina-terminal/TerminalWidget.cpp
blob: 589d3fa33a7dd94f733fd9b9c9526884fc9bd0a4 (plain)
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
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "TerminalWidget.h"

#include <QProcessEnvironment>
#include <QDebug>
#include <QApplication>

TerminalWidget::TerminalWidget(QWidget *parent, QString dir) : QTextEdit(parent){
  //Setup the text widget
  this->setLineWrapMode(QTextEdit::WidgetWidth);
  //this->setReadOnly(true); //the key event catch will do the process/widget forwarding
  //this->setPlainText("WARNING: This utility is still incomplete and does not function properly yet");
  
  //Create/open the serial port
  /*PROC = new QSerialPort(this);
    QList<QSerialPortInfo>  openports = QSerialPortInfo::availablePorts();
    //Now print out all the information
    if(openports.isEmpty()){ this->setEnabled(false);}
    else{
      //Go through the open ports until we find one that can be used
      for(int i=0; i<openports.length(); i++){
	qDebug() << "Port:" << openports[i].description();
	qDebug() << "Name:" << openports[i].portName();
	qDebug() << "Serial Number:" << openports[i].serialNumber();
	qDebug() << "System Location:" << openports[i].systemLocation();
	PROC->setPort(openports[i]);
	if(PROC->open(QIODevice::ReadWrite) ){ break; }
      }
    }
  this->setEnabled(PROC->isOpen());
  //Connect the signals/slots
  connect(PROC, SIGNAL(readyRead()), this, SLOT(UpdateText()) );
  connect(PROC, SIGNAL(aboutToClose()), this, SLOT(ShellClosed()) );
  */
    
   //Create/launch the process 
  PROC = new QProcess(this);
    PROC->setProcessChannelMode(QProcess::MergedChannels);
    PROC->setProcessEnvironment(QProcessEnvironment::systemEnvironment());
    PROC->setProgram( PROC->processEnvironment().value("SHELL","/bin/sh") );
    PROC->setWorkingDirectory(dir);
  //Connect the signals/slots
  connect(PROC, SIGNAL(readyReadStandardOutput()), this, SLOT(UpdateText()) );
  connect(PROC, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ShellClosed()) );
  //Now start the shell
  //PROC->start("login" , QStringList() << "-f" << getlogin(), QIODevice::ReadWrite);
  PROC->start(QIODevice::ReadWrite);
  
}

TerminalWidget::~TerminalWidget(){
	
}

void TerminalWidget::aboutToClose(){
  //if(PROC->isOpen()){ PROC->close(); }
  if(PROC->state()!=QProcess::NotRunning){ PROC->kill(); }
}

// ==================
//    PRIVATE SLOTS
// ==================
void TerminalWidget::UpdateText(){
  //read the data from the process
  //QByteArray data = PROC->readAll();
  qDebug() << "Process Data Available";
  QByteArray data = PROC->readAllStandardOutput();
  this->insertPlainText(QString(data));
  //adjust the scrollbar as needed
	
}

void TerminalWidget::ShellClosed(){
  emit ProcessClosed(this->whatsThis());
}

// ==================
//       PROTECTED
// ==================
void TerminalWidget::keyPressEvent(QKeyEvent *ev){
    //Check for special key combinations first
    QString txt = ev->text();
    switch(ev->key()){
	case Qt::Key_Backspace:
	case Qt::Key_Left:
	case Qt::Key_Right:
	case Qt::Key_Up:
	case Qt::Key_Down:
	  break;
	case Qt::Key_Return:
	case Qt::Key_Enter:
	  txt = "\r";
	default:
	  //All other events can get echoed onto the widget (non-movement)
	  QTextEdit::keyPressEvent(ev); //echo the input on the widget
    }
  qDebug() << "Forward Input:" << txt << ev->key();
  PROC->write(txt.toLocal8Bit());
}

void TerminalWidget::mousePressEvent(QMouseEvent *ev){
  this->setFocus();
  Q_UNUSED(ev);
}

void TerminalWidget::mouseDoubleClickEvent(QMouseEvent *ev){
  Q_UNUSED(ev);	
}

void TerminalWidget::contextMenuEvent(QContextMenuEvent *ev){
  Q_UNUSED(ev);	
}
bgstack15