227 lines
4.9 KiB
C++
227 lines
4.9 KiB
C++
/*
|
|
Copyright (C) 2017 Grégory Soutadé
|
|
|
|
This file is part of Hex offsets.
|
|
|
|
Hex offsets is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Hex offsets is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with Hex offsets. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "mainwindow.hpp"
|
|
#include "ui_mainwindow.h"
|
|
#include "uientry.hpp"
|
|
|
|
#include <QClipboard>
|
|
|
|
MainWindow::MainWindow(QWidget *parent, int entriesNumber) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MainWindow),
|
|
editConversion(false),
|
|
entriesNumber(entriesNumber)
|
|
{
|
|
int i;
|
|
UIEntry* entry;
|
|
|
|
errorFont.setBold(true);
|
|
redPalette.setColor(QPalette::Text, Qt::red);
|
|
|
|
ui->setupUi(this);
|
|
|
|
QWidget* curWidget = this->ui->lineBase;
|
|
for(i=0; i<UIEntry::MIN_ENTRIES_NUMBER; i++)
|
|
{
|
|
entry = new UIEntry(0, this);
|
|
ui->verticalLayout->insertWidget(1+i, entry);
|
|
setTabOrder(curWidget, entry->getLine());
|
|
entries.push_back(entry);
|
|
curWidget = entry->getLine();
|
|
}
|
|
|
|
this->ui->spinUIEntries->setValue(entriesNumber);
|
|
}
|
|
|
|
class InvalidNumberException
|
|
{
|
|
};
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::addValue(QLineEdit* line, UIEntry* entry, long* result, int base)
|
|
{
|
|
long temp;
|
|
bool ok;
|
|
|
|
if (line->text().length() == 0) return;
|
|
|
|
temp = line->text().toLong(&ok, base);
|
|
if (!ok)
|
|
{
|
|
line->setPalette(redPalette);
|
|
line->setFont(errorFont);
|
|
throw(InvalidNumberException());
|
|
}
|
|
else
|
|
{
|
|
line->setPalette(blackPalette);
|
|
line->setFont(normalFont);
|
|
|
|
if (entry && !entry->enabled())
|
|
return;
|
|
|
|
if (!entry || entry->getOperation() == UIEntry::OPERATION_PLUS)
|
|
*result += temp;
|
|
else
|
|
*result -= temp;
|
|
}
|
|
}
|
|
|
|
void MainWindow::setTextValue(QLineEdit* line, long value, int base)
|
|
{
|
|
QString qres;
|
|
|
|
if (base == 10 || value >= 0)
|
|
qres.setNum(value, base);
|
|
else
|
|
{
|
|
qres = "-" + qres.setNum(-value, 16);
|
|
}
|
|
line->setText(qres);
|
|
|
|
}
|
|
|
|
void MainWindow::computeResult(void)
|
|
{
|
|
long result = 0;
|
|
QString qres;
|
|
|
|
try
|
|
{
|
|
this->addValue(this->ui->lineBase, 0, &result);
|
|
for (auto entry : entries)
|
|
{
|
|
addValue(entry->getLine(), entry, &result);
|
|
}
|
|
|
|
setTextValue(ui->lineResult, result);
|
|
}
|
|
catch(InvalidNumberException e)
|
|
{
|
|
this->ui->lineResult->setText(QString("NaN"));
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_lineBase_textChanged(const QString &arg1)
|
|
{
|
|
(void) arg1;
|
|
computeResult();
|
|
}
|
|
|
|
void MainWindow::on_buttonCopyClipboard_clicked()
|
|
{
|
|
QApplication::clipboard()->setText(this->ui->lineResult->text());
|
|
}
|
|
|
|
void MainWindow::on_lineDec_textChanged(const QString &arg1)
|
|
{
|
|
long result = 0;
|
|
(void) arg1;
|
|
|
|
if (editConversion) return;
|
|
|
|
editConversion = true;
|
|
try
|
|
{
|
|
addValue(ui->lineDec, 0, &result, 10);
|
|
setTextValue(ui->lineHex, result);
|
|
}
|
|
catch (InvalidNumberException e)
|
|
{
|
|
}
|
|
|
|
editConversion = false;
|
|
}
|
|
|
|
void MainWindow::on_lineHex_textChanged(const QString &arg1)
|
|
{
|
|
long result = 0;
|
|
|
|
(void) arg1;
|
|
|
|
if (editConversion) return;
|
|
|
|
editConversion = true;
|
|
try
|
|
{
|
|
addValue(ui->lineHex, 0, &result);
|
|
setTextValue(ui->lineDec, result, 10);
|
|
}
|
|
catch (InvalidNumberException e)
|
|
{
|
|
}
|
|
|
|
editConversion = false;
|
|
}
|
|
|
|
void MainWindow::on_buttonClearBase_clicked()
|
|
{
|
|
this->ui->lineBase->clear();
|
|
}
|
|
|
|
void MainWindow::on_buttonClearAll_clicked()
|
|
{
|
|
this->ui->lineBase->clear();
|
|
for (auto entry : entries)
|
|
{
|
|
entry->clear();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_spinUIEntries_valueChanged(int newValue)
|
|
{
|
|
int i, start;
|
|
UIEntry* entry = entries[entries.size()-1];
|
|
QWidget* curWidget = entry->getLine();
|
|
|
|
start = entries.size();
|
|
if (newValue == start)
|
|
return;
|
|
else if (newValue > start)
|
|
{
|
|
for(i=start; i<newValue; i++)
|
|
{
|
|
entry = new UIEntry((QWidget*)ui->verticalLayout, this);
|
|
ui->verticalLayout->insertWidget(1+i, entry);
|
|
entries.push_back(entry);
|
|
setTabOrder(curWidget, entry->getLine());
|
|
curWidget = entry->getLine();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (start-- > newValue)
|
|
{
|
|
entry = entries.back();
|
|
ui->verticalLayout->removeWidget(entry);
|
|
|
|
delete entry;
|
|
entries.pop_back();
|
|
}
|
|
}
|
|
|
|
resize(width(), minimumHeight());
|
|
computeResult();
|
|
}
|