//=========================================== // 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 #include #include #include 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 TTY port PROC = new TTYProcess(this); qDebug() << "Open new TTY"; //int fd; bool ok = PROC->startTTY( QProcessEnvironment::systemEnvironment().value("SHELL","/bin/sh") ); qDebug() << " - opened:" << ok; this->setEnabled(PROC->isOpen()); //Connect the signals/slots connect(PROC, SIGNAL(readyRead()), this, SLOT(UpdateText()) ); connect(PROC, SIGNAL(processClosed()), this, SLOT(ShellClosed()) ); } TerminalWidget::~TerminalWidget(){ aboutToClose(); } void TerminalWidget::aboutToClose(){ if(PROC->isOpen()){ PROC->closeTTY(); } //TTY PORT } // ================== // PRIVATE SLOTS // ================== void TerminalWidget::UpdateText(){ //read the data from the process //qDebug() << "UpdateText"; if(!PROC->isOpen()){ return; } QByteArray buffer = PROC->readTTY(); QString text = QString(buffer); text.replace("\r\n","\n"); this->insertPlainText(text); //adjust the scrollbar as needed this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum()); } void TerminalWidget::ShellClosed(){ emit ProcessClosed(this->whatsThis()); } // ================== // PROTECTED // ================== void TerminalWidget::keyPressEvent(QKeyEvent *ev){ if(ev->text().isEmpty() || ev->text()=="\b" ){ PROC->writeQtKey(ev->key()); }else{ QByteArray ba; ba.append(ev->text()); //avoid any byte conversions PROC->writeTTY(ba); } ev->ignore(); } 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); }