-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
269 lines (223 loc) · 7.8 KB
/
mainwindow.cpp
File metadata and controls
269 lines (223 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <QFileDialog>
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include <QKeyEvent>
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mypixmapitem.h"
#include "conversiontools.h"
const QStringList MainWindow::listImgFormats = { "Mono", "256 col", "24bpp", "32bpp" };
// Bits per pixel in the same order as listImgFormats
static const int8_t listImgFmtsBpp[] = {1, 8, 24, 32};
// Image formats in the same order as listImgFormats
static const QImage::Format listQImageFmts[] = {
QImage::Format::Format_Mono, QImage::Format::Format_Indexed8,
QImage::Format::Format_RGB888, QImage::Format::Format_ARGB32};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Populate formats combo box
ui->comboBoxFmt->addItems(listImgFormats);
// Create graphics view for sprites
std::unique_ptr<MyPixmapItem> temp = std::unique_ptr<MyPixmapItem>(new MyPixmapItem());
pmapitem = std::move(temp);
// Initial format
ui->comboBoxFmt->setCurrentIndex(0);
pmapitem->setBitsPerPixel(listImgFmtsBpp[ui->comboBoxFmt->currentIndex()]);
ui->graphicsView->setScene(new QGraphicsScene());
ui->graphicsView->scene()->addItem(pmapitem.get());
// Set font to monospace for hexdump zone
ui->textEditHexDmp->setFontFamily("Monospace");
// Check command line params (a file to load)
if(QCoreApplication::arguments().size() > 1) {
const QString filePath = QCoreApplication::arguments().at(1);
doOpenFile(filePath);
}
sceneUpdate();
}
MainWindow::~MainWindow()
{
delete ui;
}
// Open a file requested
void MainWindow::on_actionOpen_triggered()
{
const QString filePath = QFileDialog::getOpenFileName(this, ("Open binary file"),
"/home/pablo",
("Binary file (*.*)"));
doOpenFile(filePath);
}
void MainWindow::doOpenFile(const QString& filePath) {
bool res = false;
std::unique_ptr<QFile> fPtr(new QFile(filePath));
if(fPtr->exists())
res = fPtr->open(QFile::ReadOnly);
if(res) {
file = std::move(fPtr);
fb = std::unique_ptr<filebuffer>(new filebuffer(file));
char fbSize[32];
snprintf(fbSize, sizeof(fbSize), "%lu", fb->getBufferSize());
ui->labelFileName->setText(file.get()->fileName().append(" [").append(fbSize).append(" bytes]"));
fileName=filePath;
// Pass filebuffer to pixmap item, width, offset
pmapitem->setBuffer(fb->getBufferPtr(), fb->getBufferSize());
// Default values
ui->spinBoxWidth->setValue(16);
// Byte boundary if more than 1 pixels per byte
ui->spinBoxWidth->setSingleStep(8);
ui->spinBoxWidth->setMinimum(8);
ui->lineEditOffset->setText("0x0000");
pmapitem->setBufferOffset(0LU);
// Initial color format: MONO, 1 bpp
ui->comboBoxFmt->setCurrentIndex(0);
int w = ui->spinBoxWidth->value();
pmapitem->setWidth(w);
// WARN: after setW so that height is calculated based on width
pmapitem->setBitsPerPixel(listImgFmtsBpp[ui->comboBoxFmt->currentIndex()]);
pmapitem->setImageFormat(listQImageFmts[ui->comboBoxFmt->currentIndex()]);
pmapitem->setGeo(ui->graphicsView->geometry());
hexdump();
sceneUpdate();
}
else {
QMessageBox mb;
mb.critical(0, "Error opening file", QString("Error opening file ").append(filePath));
}
}
// Offset in buffer changed
void MainWindow::onOffsetChange()
{
if (!ui->lineEditOffset->isModified())
return; // Ignore second signal.
ui->lineEditOffset->setModified(false); // Ignore second signal when user presses ENTER
// (one for ENTER and the second for lost focus)
const QString offset = ui->lineEditOffset->text();
uint64_t iOffset = ConversionTools::numberStringToInteger(offset);
if(iOffset != -1LU && fb->getBufferPtr() == nullptr) {
iOffset = -1LU;
}
if(iOffset != -1LU) {
pmapitem->setBufferOffset(iOffset);
qDebug() << "NEW OFFSET:" << offset;
// Hex dump
hexdump();
}
else {
// Wrong number: restore current offset
ui->lineEditOffset->setText(
ConversionTools::int64ToHexString(pmapitem->getBufferOffset()));
qDebug() << "INVALID OFFSET " << offset;
}
sceneUpdate();
}
// Number of pixels per line shown changed
void MainWindow::onWidthChange(int pixelsPerLine)
{
if(pmapitem->getBitsPerPixel() < 8 && pixelsPerLine % 8 != 0)
qDebug() << "IGNORED " << pixelsPerLine << " PX PER LINE: NOT BYTE BOUNDARY";
else {
qDebug() << "NEW PIXELS PER LINE:" << pixelsPerLine;
pmapitem->setWidth(pixelsPerLine);
hexdump();
sceneUpdate();
}
}
void MainWindow::onImgFormatChange(int formatIndex)
{
int bpp = listImgFmtsBpp[formatIndex];
qDebug() << "NEW IMG FMT " << listImgFormats[formatIndex];
pmapitem->setBitsPerPixel(bpp);
pmapitem->setImageFormat(listQImageFmts[formatIndex]);
ui->spinBoxWidth->setValue(ui->spinBoxWidth->value()%bpp);
if (bpp<8) {
ui->spinBoxWidth->setSingleStep(8); // Byte boundary if more than 1 pixels per byte
ui->spinBoxWidth->setMinimum(8);
}
else
ui->spinBoxWidth->setSingleStep(1);
hexdump();
sceneUpdate();
}
void MainWindow::onScaleChange(int scale)
{
// Pixmap scale modified so that we can see more data reducing width
pmapitem->setPixmapScale(scale);
hexdump();
sceneUpdate();
}
void MainWindow::keyReleaseEvent(QKeyEvent* ev)
{
//QWidget* focusWidget = QApplication::focusWidget();
bool updated = false;
if(ev->key() == Qt::Key_PageDown) {
// Increase offset one page
uint64_t bytes = pmapitem->getVisibleBufferSize();
updated = pmapitem->incBufferOffset(bytes);
}
else if(ev->key() == Qt::Key_PageUp) {
// Decrease offset one page
uint64_t bytes = pmapitem->getVisibleBufferSize();
updated = pmapitem->decBufferOffset(bytes);
}
else if(ev->key() == Qt::Key_Down) {
// Increase offset by one line
uint64_t bytes = pmapitem->getBytesPerLine();
updated = pmapitem->incBufferOffset(bytes);
}
else if(ev->key() == Qt::Key_Up) {
// Decrease offset one line
uint64_t bytes = pmapitem->getBytesPerLine();
updated = pmapitem->decBufferOffset(bytes);
}
if(updated) {
// Update edit
ui->lineEditOffset->setText(
ConversionTools::int64ToHexString(pmapitem->getBufferOffset()));
hexdump();
sceneUpdate();
}
else
QMainWindow::keyReleaseEvent(ev);
}
void MainWindow::on_actionExit_triggered()
{
QApplication::exit();
}
void MainWindow::hexdump()
{
// Do a dump of current buffer
if(fb->getBufferSize() > 0) {
uint64_t offset = pmapitem->getBufferOffset();
uint8_t* ptr = fb->getBufferPtr() + offset;
ui->textEditHexDmp->clear();
QString line;
char byteStr[3];
uint64_t cnt=0;
uint64_t endOffset = offset + pmapitem->getVisibleBufferSize();
while(offset < endOffset) {
sprintf(byteStr, "%02X", *ptr);
if(cnt%16 == 0) {
char addrStr[32] = {0};
sprintf(addrStr, "%08" PRIx64 ": ", offset); //"%p: ", ptr);
line.append(addrStr);
}
line.append(byteStr);
cnt++;
offset++;
ptr++;
if(cnt%16==0 || offset == endOffset) {
line.append("\n");
ui->textEditHexDmp->insertPlainText(line);
line.clear();
}
}
}
}
// Refresh Scene
void MainWindow::sceneUpdate()
{
ui->graphicsView->scene()->update();
}