| 1 | // File: inputpane.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 | |
| 22 | #include <QFontDialog> |
| 23 | #include <QKeyEvent> |
| 24 | #include "inputpane.h" |
| 25 | #include "ui_inputpane.h" |
| 26 | #include "pep.h" |
| 27 | |
| 28 | InputPane::InputPane(QWidget *parent) : |
| 29 | QWidget(parent), |
| 30 | ui(new Ui::InputPane) |
| 31 | { |
| 32 | ui->setupUi(this); |
| 33 | |
| 34 | connect(ui->plainTextEdit, SIGNAL(undoAvailable(bool)), this, SIGNAL(undoAvailable(bool))); |
| 35 | connect(ui->plainTextEdit, SIGNAL(redoAvailable(bool)), this, SIGNAL(redoAvailable(bool))); |
| 36 | |
| 37 | ui->label->setFont(QFont(Pep::labelFont, Pep::labelFontSize)); |
| 38 | ui->plainTextEdit->setFont(QFont(Pep::codeFont, Pep::ioFontSize)); |
| 39 | } |
| 40 | |
| 41 | InputPane::~InputPane() |
| 42 | { |
| 43 | delete ui; |
| 44 | } |
| 45 | |
| 46 | void InputPane::highlightOnFocus() |
| 47 | { |
| 48 | if (ui->plainTextEdit->hasFocus()) { |
| 49 | ui->label->setAutoFillBackground(true); |
| 50 | } |
| 51 | else { |
| 52 | ui->label->setAutoFillBackground(false); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | QString InputPane::toPlainText() |
| 57 | { |
| 58 | return ui->plainTextEdit->toPlainText(); |
| 59 | } |
| 60 | |
| 61 | void InputPane::setText(QString input) |
| 62 | { |
| 63 | ui->plainTextEdit->setPlainText(input); |
| 64 | } |
| 65 | |
| 66 | bool InputPane::hasFocus() |
| 67 | { |
| 68 | return ui->plainTextEdit->hasFocus(); |
| 69 | } |
| 70 | |
| 71 | void InputPane::undo() |
| 72 | { |
| 73 | ui->plainTextEdit->undo(); |
| 74 | } |
| 75 | |
| 76 | void InputPane::redo() |
| 77 | { |
| 78 | ui->plainTextEdit->redo(); |
| 79 | } |
| 80 | |
| 81 | bool InputPane::isUndoable() |
| 82 | { |
| 83 | return ui->plainTextEdit->document()->isUndoAvailable(); |
| 84 | } |
| 85 | |
| 86 | bool InputPane::isRedoable() |
| 87 | { |
| 88 | return ui->plainTextEdit->document()->isRedoAvailable(); |
| 89 | } |
| 90 | |
| 91 | void InputPane::cut() |
| 92 | { |
| 93 | ui->plainTextEdit->cut(); |
| 94 | } |
| 95 | |
| 96 | void InputPane::copy() |
| 97 | { |
| 98 | ui->plainTextEdit->copy(); |
| 99 | } |
| 100 | |
| 101 | void InputPane::paste() |
| 102 | { |
| 103 | ui->plainTextEdit->paste(); |
| 104 | } |
| 105 | |
| 106 | void InputPane::setReadOnly(bool b) |
| 107 | { |
| 108 | ui->plainTextEdit->setReadOnly(b); |
| 109 | } |
| 110 | |
| 111 | void InputPane::tab() |
| 112 | { |
| 113 | if (!ui->plainTextEdit->isReadOnly()) { |
| 114 | QTextCursor cursor = ui->plainTextEdit->textCursor(); |
| 115 | cursor.movePosition(QTextCursor::StartOfLine); |
| 116 | |
| 117 | int curLinePos = ui->plainTextEdit->textCursor().position() - cursor.position(); |
| 118 | |
| 119 | QString string; |
| 120 | int spaces; |
| 121 | spaces = 4 - (curLinePos % 4); |
| 122 | |
| 123 | for (int i = 0; i < spaces; i++) { |
| 124 | string.append(" "); |
| 125 | } |
| 126 | |
| 127 | ui->plainTextEdit->insertPlainText(string); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void InputPane::onFontChanged(QFont font) |
| 132 | { |
| 133 | ui->plainTextEdit->setFont(font); |
| 134 | } |
| 135 | |
| 136 | void InputPane::mouseReleaseEvent(QMouseEvent *) |
| 137 | { |
| 138 | ui->plainTextEdit->setFocus(); |
| 139 | } |