-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeebview.cpp
331 lines (280 loc) · 8.17 KB
/
Beebview.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright © 1999-2000 David Robinson
* Copyright © 2007-2016 Matt Robinson
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>
#include <QTimer>
#include <QUrl>
#include <BbcImageLoader.h>
#include "About.h"
#include "Beebview.h"
#include "ui_Beebview.h"
Beebview::Beebview(QStringList args, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Beebview),
modesGroup(this),
coloursGroup(this)
{
ui->setupUi(this);
QApplication::setWindowIcon(QIcon(":Graphics/BeebView.png"));
for(int i = 0; i < 6 ; i++)
{
if(i == 3)
{
continue;
}
QAction *action = new QAction(tr("Mode &%1").arg(i), this);
action->setCheckable(true);
action->setData(i);
modesGroup.addAction(action);
ui->menuMode->addAction(action);
}
for(int i = 0; i < 8; i++)
{
QAction *action = new QAction(tr("Colour &%1").arg(i), this);
action->setData(i);
coloursGroup.addAction(action);
ui->menuCycleColour->addAction(action);
}
QObject::connect(&modesGroup, &QActionGroup::triggered, this, &Beebview::modesGroup_triggered);
QObject::connect(&coloursGroup, &QActionGroup::triggered, this, &Beebview::coloursGroup_triggered);
QObject::connect(ui->actionExit, &QAction::triggered, this, &Beebview::close);
image = new BbcScreenWidget();
this->setCentralWidget(image);
this->setFixedSize(this->sizeHint());
this->UpdateInfo();
QString fileName;
char screenMode = -1;
bool autoSave = false;
// Process command line arguments
args.removeFirst();
foreach (const QString &arg, args)
{
if(arg == "--save")
{
autoSave = true;
}
else if(arg == "--mode0")
{
screenMode = 0;
}
else if(arg == "--mode1")
{
screenMode = 1;
}
else if(arg == "--mode2")
{
screenMode = 2;
}
else if(arg == "--mode4")
{
screenMode = 4;
}
else if(arg == "--mode5")
{
screenMode = 5;
}
else
{
// As this doesn't match any switches, assume it is a filename
fileName = arg;
}
}
if(!fileName.isEmpty())
{
LoadFile(fileName);
// Set the mode if this was requested, and the image loaded okay
if(screenMode != -1 && screen != NULL)
{
screen->setMode(screenMode);
image->setScreen(screen);
this->setFixedSize(this->sizeHint());
}
this->UpdateInfo();
}
// Automatically save the file and exit if autoSave is true
if(autoSave)
{
// Only attempt to save a file if one was loaded
if(screen != NULL)
{
QFileInfo info(fileName);
SaveAs(info.path() + QDir::separator() + info.completeBaseName() + ".bmp");
}
// Close the program
QTimer::singleShot(0, this, &Beebview::close);
}
}
void Beebview::on_menuFile_aboutToShow()
{
ui->actionSaveAs->setEnabled(screen != NULL);
}
void Beebview::on_menuMode_aboutToShow()
{
modesGroup.setEnabled(screen != NULL);
if(screen != NULL)
{
int checkItem = -1;
switch(screen->getMode())
{
case 0:
checkItem = 0;
break;
case 1:
checkItem = 1;
break;
case 2:
checkItem = 2;
break;
case 4:
checkItem = 3;
break;
case 5:
checkItem = 4;
break;
}
modesGroup.actions()[checkItem]->setChecked(true);
}
}
void Beebview::on_menuCycleColour_aboutToShow()
{
coloursGroup.setEnabled(screen != NULL);
if(screen != NULL)
{
switch(screen->getMode())
{
case 0:
case 4:
coloursGroup.actions()[2]->setEnabled(false);
coloursGroup.actions()[3]->setEnabled(false);
coloursGroup.actions()[4]->setEnabled(false);
coloursGroup.actions()[5]->setEnabled(false);
coloursGroup.actions()[6]->setEnabled(false);
coloursGroup.actions()[7]->setEnabled(false);
break;
case 1:
case 5:
coloursGroup.actions()[2]->setEnabled(true);
coloursGroup.actions()[3]->setEnabled(true);
coloursGroup.actions()[4]->setEnabled(false);
coloursGroup.actions()[5]->setEnabled(false);
coloursGroup.actions()[6]->setEnabled(false);
coloursGroup.actions()[7]->setEnabled(false);
break;
case 2:
coloursGroup.actions()[2]->setEnabled(true);
coloursGroup.actions()[3]->setEnabled(true);
coloursGroup.actions()[4]->setEnabled(true);
coloursGroup.actions()[5]->setEnabled(true);
coloursGroup.actions()[6]->setEnabled(true);
coloursGroup.actions()[7]->setEnabled(true);
break;
}
}
}
void Beebview::modesGroup_triggered(QAction *action)
{
screen->setMode(action->data().toInt());
image->setScreen(screen);
this->setFixedSize(this->sizeHint());
this->UpdateInfo();
}
void Beebview::on_actionReportBug_triggered(bool checked)
{
QDesktopServices::openUrl(QUrl("https://nerdoftheherd.com/tools/beebview/report-a-bug/"));
}
void Beebview::on_actionHelp_triggered(bool checked)
{
QDesktopServices::openUrl(QUrl("https://nerdoftheherd.com/tools/beebview/help/"));
}
void Beebview::on_actionAbout_triggered(bool checked)
{
About *about = new About(this);
about->show();
}
Beebview::~Beebview()
{
delete ui;
// Clean up the screen object if it exists
if(screen != NULL)
{
delete screen;
screen = NULL;
}
}
void Beebview::coloursGroup_triggered(QAction *action)
{
uint8_t colour = action->data().toInt();
screen->setColour(colour, (screen->getColour(colour) + 1) % 8);
image->setScreen(screen);
}
void Beebview::on_actionOpen_triggered(bool checked)
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"),
QString(), tr("BBC Graphics Files (*.bbg);;All Files (*)"));
if(!fileName.isNull())
{
LoadFile(fileName);
this->UpdateInfo();
}
}
void Beebview::LoadFile(QString fileName)
{
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this, tr("File Error"),
tr("There was a problem opening the file \"%1\".").arg(fileName));
return;
}
if(file.size() > BbcScreen::MAX_MEMSIZE)
{
QMessageBox::critical(this, tr("File Error"),
tr("\"%1\" is too large to be a BBC graphics file.").arg(fileName));
return;
}
// Clean up the old screen object if there is one
if(screen != NULL)
{
delete screen;
screen = NULL;
}
uint8_t *data = file.map(0, file.size());
BbcImageLoader loader(data, file.size());
screen = loader.LoadAuto();
image->setScreen(screen);
this->setFixedSize(this->sizeHint());
QFileInfo info(fileName);
currentFileTitle = info.completeBaseName(); // File name minus path and extension
}
void Beebview::UpdateInfo()
{
QString title = tr("BBC Graphics Viewer");
if(screen != NULL)
{
title.append(tr(" - %1 [MODE %2]")
.arg(currentFileTitle, QString::number(screen->getMode())));
}
this->setWindowTitle(title);
}
void Beebview::on_actionSaveAs_triggered(bool checked)
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
QString(), tr("Portable Network Graphics (*.png);;Windows Bitmap (*.bmp)"));
if(!fileName.isNull())
{
SaveAs(fileName);
}
}
void Beebview::SaveAs(QString fileName)
{
if(!image->saveAs(fileName))
{
QMessageBox::critical(this, tr("File Error"),
tr("Unable to save to \"%1\", please check the name and try again.").arg(fileName));
}
}