Hex offsets

Hex offsets Git Source Tree

Root/mainwindow.cpp

1/*
2 Copyright (C) 2017 Grégory Soutadé
3
4 This file is part of Hex offsets.
5
6 Hex offsets is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Hex offsets is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Hex offsets. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "mainwindow.hpp"
21#include "ui_mainwindow.h"
22#include "uientry.hpp"
23
24#include <QClipboard>
25
26MainWindow::MainWindow(QWidget *parent, int entriesNumber) :
27 QMainWindow(parent),
28 ui(new Ui::MainWindow),
29 editConversion(false),
30 entriesNumber(entriesNumber)
31{
32 int i;
33 UIEntry* entry;
34
35 errorFont.setBold(true);
36 redPalette.setColor(QPalette::Text, Qt::red);
37
38 ui->setupUi(this);
39
40 QWidget* curWidget = this->ui->lineBase;
41 for(i=0; i<UIEntry::MIN_ENTRIES_NUMBER; i++)
42 {
43 entry = new UIEntry(0, this);
44 ui->verticalLayout->insertWidget(1+i, entry);
45 setTabOrder(curWidget, entry->getLine());
46 entries.push_back(entry);
47 curWidget = entry->getLine();
48 }
49
50 this->ui->spinUIEntries->setValue(entriesNumber);
51}
52
53class InvalidNumberException
54{
55};
56
57MainWindow::~MainWindow()
58{
59 delete ui;
60}
61
62void MainWindow::addValue(QLineEdit* line, UIEntry* entry, long* result, int base)
63{
64 long temp;
65 bool ok;
66
67 if (line->text().length() == 0) return;
68
69 temp = line->text().toLong(&ok, base);
70 if (!ok)
71 {
72 line->setPalette(redPalette);
73 line->setFont(errorFont);
74 throw(InvalidNumberException());
75 }
76 else
77 {
78 line->setPalette(blackPalette);
79 line->setFont(normalFont);
80
81 if (entry && !entry->enabled())
82 return;
83
84 if (!entry || entry->getOperation() == UIEntry::OPERATION_PLUS)
85 *result += temp;
86 else
87 *result -= temp;
88 }
89}
90
91void MainWindow::setTextValue(QLineEdit* line, long value, int base)
92{
93 QString qres;
94
95 if (base == 10 || value >= 0)
96 qres.setNum(value, base);
97 else
98 {
99 qres = "-" + qres.setNum(-value, 16);
100 }
101 line->setText(qres);
102
103}
104
105void MainWindow::computeResult(void)
106{
107 long result = 0;
108 QString qres;
109
110 try
111 {
112 this->addValue(this->ui->lineBase, 0, &result);
113 for (auto entry : entries)
114 {
115 addValue(entry->getLine(), entry, &result);
116 }
117
118 setTextValue(ui->lineResult, result);
119 }
120 catch(InvalidNumberException e)
121 {
122 this->ui->lineResult->setText(QString("NaN"));
123 }
124}
125
126void MainWindow::on_lineBase_textChanged(const QString &arg1)
127{
128 (void) arg1;
129 computeResult();
130}
131
132void MainWindow::on_buttonCopyClipboard_clicked()
133{
134 QApplication::clipboard()->setText(this->ui->lineResult->text());
135}
136
137void MainWindow::on_lineDec_textChanged(const QString &arg1)
138{
139 long result = 0;
140 (void) arg1;
141
142 if (editConversion) return;
143
144 editConversion = true;
145 try
146 {
147 addValue(ui->lineDec, 0, &result, 10);
148 setTextValue(ui->lineHex, result);
149 }
150 catch (InvalidNumberException e)
151 {
152 }
153
154 editConversion = false;
155}
156
157void MainWindow::on_lineHex_textChanged(const QString &arg1)
158{
159 long result = 0;
160
161 (void) arg1;
162
163 if (editConversion) return;
164
165 editConversion = true;
166 try
167 {
168 addValue(ui->lineHex, 0, &result);
169 setTextValue(ui->lineDec, result, 10);
170 }
171 catch (InvalidNumberException e)
172 {
173 }
174
175 editConversion = false;
176}
177
178void MainWindow::on_buttonClearBase_clicked()
179{
180 this->ui->lineBase->clear();
181}
182
183void MainWindow::on_buttonClearAll_clicked()
184{
185 this->ui->lineBase->clear();
186 for (auto entry : entries)
187 {
188 entry->clear();
189 }
190}
191
192void MainWindow::on_spinUIEntries_valueChanged(int newValue)
193{
194 int i, start;
195 UIEntry* entry = entries[entries.size()-1];
196 QWidget* curWidget = entry->getLine();
197
198 start = entries.size();
199 if (newValue == start)
200 return;
201 else if (newValue > start)
202 {
203 for(i=start; i<newValue; i++)
204 {
205 entry = new UIEntry((QWidget*)ui->verticalLayout, this);
206 ui->verticalLayout->insertWidget(1+i, entry);
207 entries.push_back(entry);
208 setTabOrder(curWidget, entry->getLine());
209 curWidget = entry->getLine();
210 }
211 }
212 else
213 {
214 while (start-- > newValue)
215 {
216 entry = entries.back();
217 ui->verticalLayout->removeWidget(entry);
218
219 delete entry;
220 entries.pop_back();
221 }
222 }
223
224 resize(width(), minimumHeight());
225 computeResult();
226}

Archive Download this file

Branches