| 1 | // File: byteconverterhex.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 "byteconverterhex.h" |
| 22 | #include "ui_byteconverterhex.h" |
| 23 | #include <QTextCursor> |
| 24 | |
| 25 | ByteConverterHex::ByteConverterHex(QWidget *parent) : |
| 26 | QWidget(parent), |
| 27 | ui(new Ui::ByteConverterHex) |
| 28 | { |
| 29 | ui->setupUi(this); |
| 30 | // Regular expression to validate 0x00 - 0xff |
| 31 | QRegExp hexRx("0x([0-9]|[a-f]|[A-F])([0-9]|[a-f]|[A-F])"); |
| 32 | hexValidator = new QRegExpValidator(hexRx, this); |
| 33 | ui->lineEdit->setValidator(hexValidator); |
| 34 | // Forward the textEdited() signal from ui->lineEdit up to the main window |
| 35 | connect(ui->lineEdit, SIGNAL(textEdited(const QString &)), this, SIGNAL(textEdited(const QString &))); |
| 36 | connect(ui->lineEdit, SIGNAL(cursorPositionChanged(int,int)), this, SLOT(moveCursorAwayFromPrefix(int, int))); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | ByteConverterHex::~ByteConverterHex() |
| 41 | { |
| 42 | delete ui; |
| 43 | } |
| 44 | |
| 45 | void ByteConverterHex::setValue(int value) |
| 46 | { |
| 47 | if (value == -1) { |
| 48 | ui->lineEdit->setText("0x"); |
| 49 | } |
| 50 | else { |
| 51 | ui->lineEdit->setText(QString("0x%1").arg(value, 2, 16, QLatin1Char('0'))); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void ByteConverterHex::moveCursorAwayFromPrefix(int old, int current) |
| 56 | { |
| 57 | if (current < 2) { |
| 58 | ui->lineEdit->setCursorPosition(old); |
| 59 | } |
| 60 | } |