| 1 | //File: terminalpane.cpp |
| 2 | /* |
| 3 | Pep8-1 is a virtual machine for writing machine language and assembly |
| 4 | language programs. |
| 5 | |
| 6 | Copyright (C) 2009 J. Stanley Warford, Pepperdine University |
| 7 | |
| 8 | This program is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | #include <QFontDialog> |
| 22 | #include <QScrollBar> |
| 23 | #include "terminalpane.h" |
| 24 | #include "ui_terminalpane.h" |
| 25 | #include "sim.h" |
| 26 | #include "pep.h" |
| 27 | |
| 28 | TerminalPane::TerminalPane(QWidget *parent) : |
| 29 | QWidget(parent), |
| 30 | ui(new Ui::TerminalPane) |
| 31 | { |
| 32 | ui->setupUi(this); |
| 33 | |
| 34 | waiting = false; |
| 35 | |
| 36 | connect(ui->plainTextEdit, SIGNAL(undoAvailable(bool)), this, SIGNAL(undoAvailable(bool))); |
| 37 | connect(ui->plainTextEdit, SIGNAL(redoAvailable(bool)), this, SIGNAL(redoAvailable(bool))); |
| 38 | |
| 39 | ui->label->setFont(QFont(Pep::labelFont, Pep::labelFontSize)); |
| 40 | ui->plainTextEdit->setFont(QFont(Pep::codeFont, Pep::ioFontSize)); |
| 41 | |
| 42 | qApp->installEventFilter(this); |
| 43 | } |
| 44 | |
| 45 | TerminalPane::~TerminalPane() |
| 46 | { |
| 47 | delete ui; |
| 48 | } |
| 49 | |
| 50 | void TerminalPane::appendOutput(QString str) |
| 51 | { |
| 52 | ui->plainTextEdit->setPlainText(ui->plainTextEdit->toPlainText().append(str)); |
| 53 | strokeString.append(str); |
| 54 | ui->plainTextEdit->verticalScrollBar()->setValue(ui->plainTextEdit->verticalScrollBar()->maximum()); // Scroll to bottom |
| 55 | } |
| 56 | |
| 57 | void TerminalPane::waitingForInput() |
| 58 | { |
| 59 | waiting = true; |
| 60 | displayTerminal(); |
| 61 | ui->plainTextEdit->setFocus(); |
| 62 | } |
| 63 | |
| 64 | void TerminalPane::clearTerminal() |
| 65 | { |
| 66 | ui->plainTextEdit->clear(); |
| 67 | retString = ""; |
| 68 | strokeString = ""; |
| 69 | } |
| 70 | |
| 71 | void TerminalPane::highlightOnFocus() |
| 72 | { |
| 73 | if (ui->plainTextEdit->hasFocus()) { |
| 74 | ui->label->setAutoFillBackground(true); |
| 75 | } |
| 76 | else { |
| 77 | ui->label->setAutoFillBackground(false); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool TerminalPane::hasFocus() |
| 82 | { |
| 83 | return ui->plainTextEdit->hasFocus(); |
| 84 | } |
| 85 | |
| 86 | void TerminalPane::copy() |
| 87 | { |
| 88 | ui->plainTextEdit->copy(); |
| 89 | } |
| 90 | |
| 91 | void TerminalPane::onFontChanged(QFont font) |
| 92 | { |
| 93 | ui->plainTextEdit->setFont(font); |
| 94 | } |
| 95 | |
| 96 | void TerminalPane::displayTerminal() |
| 97 | { |
| 98 | if (waiting) { |
| 99 | ui->plainTextEdit->setPlainText(strokeString + retString + QString("_")); |
| 100 | } |
| 101 | else { |
| 102 | ui->plainTextEdit->setPlainText(strokeString + retString); |
| 103 | } |
| 104 | ui->plainTextEdit->verticalScrollBar()->setValue(ui->plainTextEdit->verticalScrollBar()->maximum()); // Scroll to bottom |
| 105 | } |
| 106 | |
| 107 | bool TerminalPane::eventFilter(QObject *, QEvent *event) |
| 108 | { |
| 109 | if (event->type() == QEvent::KeyPress && ui->plainTextEdit->hasFocus() && waiting) { |
| 110 | QKeyEvent *e = static_cast<QKeyEvent *>(event); |
| 111 | if (e->key() == Qt::Key_Shift || e->key() == Qt::Key_Control || |
| 112 | e->key() == Qt::Key_Meta || e->key() == Qt::Key_Alt || |
| 113 | e->key() == Qt::Key_CapsLock || e->key() == Qt::Key_NumLock || |
| 114 | e->key() == Qt::Key_ScrollLock || e->key() == Qt::Key_Tab) { |
| 115 | // skip |
| 116 | } |
| 117 | else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { |
| 118 | retString.append('\n'); |
| 119 | strokeString.append(retString); |
| 120 | waiting = false; |
| 121 | Sim::inputBuffer = retString; |
| 122 | retString = ""; |
| 123 | displayTerminal(); |
| 124 | emit inputReceived(); |
| 125 | return true; |
| 126 | } |
| 127 | else if (e->key() == Qt::Key_Backspace && !retString.isEmpty()) { |
| 128 | retString.truncate(retString.length() - 1); |
| 129 | } |
| 130 | else { |
| 131 | retString.append(e->text()); |
| 132 | } |
| 133 | displayTerminal(); |
| 134 | return true; |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | void TerminalPane::mouseReleaseEvent(QMouseEvent *) |
| 140 | { |
| 141 | ui->plainTextEdit->setFocus(); |
| 142 | } |
| 143 |