]> Shamusworld >> Repos - wozmaker/blob - src/mainwin.cpp
4ca0ae1716a2d12e8e8f30638ec3cc42fe6b9633
[wozmaker] / src / mainwin.cpp
1 //
2 // mainwin.cpp: Implementation of the MainWin class
3 //
4 // Part of the WOZ Maker project
5 // by James Hammons
6 // (C) 2018 Underground Software
7 //
8
9 #include "mainwin.h"
10
11 #include <stdlib.h>                     // For rand()
12 #include <time.h>                       // For time()
13 #include "diskwidget.h"
14 #include "fileio.h"
15 #include "global.h"
16 #include "infowidget.h"
17 #include "mainwidget.h"
18 #include "navwidget.h"
19 #include "nibblewidget.h"
20 #include "settingsdialog.h"
21 #include "waveformwidget.h"
22
23
24 MainWin::MainWin()
25 {
26         mainWidget = new MainWidget(this);
27         setCentralWidget(mainWidget);
28
29         setWindowTitle("WOZ Maker");
30         setWindowIcon(QIcon(":/woz-icon.png"));
31
32         loadFile = CreateAction(tr("&Open"), tr("Open A2R"), tr("Open an A2R file"), QIcon(), QKeySequence(tr("ctrl+o")));
33         connect(loadFile, SIGNAL(triggered()), this, SLOT(HandleLoadFile()));
34
35         saveFile = CreateAction(tr("&Save"), tr("Save WOZ"), tr("Save a WOZ file"), QIcon(), QKeySequence(tr("ctrl+s")));
36         connect(saveFile, SIGNAL(triggered()), this, SLOT(HandleSaveFile()));
37
38         settings = CreateAction(tr("&Disk Settings"), tr("Disk Settings"), tr("Settings for the loaded A2R file"), QIcon(), QKeySequence(tr("ctrl+d")));
39         connect(settings, SIGNAL(triggered()), this, SLOT(HandleSettings()));
40
41         undoAction = CreateAction(tr("&Undo"), tr(""), tr(""), QIcon(), QKeySequence(tr("ctrl+z")));
42 //      connect(undoAction, SIGNAL(triggered()), this, SLOT(HandleUndo()));
43
44         resetLevel = CreateAction(tr("&Reset Level"), tr("Reset Level"), tr("Start the current level over"), QIcon(), QKeySequence(tr("ctrl+r")));
45 //      connect(resetLevel, SIGNAL(triggered()), this, SLOT(HandleReset()));
46
47         skipLevel = CreateAction(tr("&Skip Level"), tr("Skip Level"), tr("Skip the current level"), QIcon(), QKeySequence(tr("ctrl+k")));
48 //      connect(skipLevel, SIGNAL(triggered()), this, SLOT(HandleSkipLevel()));
49
50         gameOptions = CreateAction(tr("&Options..."), tr("Options"), tr("Configure Warehouse Man Deluxe's options"), QIcon(), QKeySequence(tr("ctrl+o")));
51 //      connect(gameOptions, SIGNAL(triggered()), this, SLOT(OnGameOptions()));
52
53         gameStats = CreateAction(tr("&Stats..."), tr("Game Stats"), tr("Show game stats"), QIcon(), QKeySequence(tr("ctrl+s")));
54 //      connect(gameStats, SIGNAL(triggered()), this, SLOT(OnGameStats()));
55
56         appExit = CreateAction(tr("E&xit"), tr("Exit"), tr("Quit playing Warehouse Man Deluxe..."), QIcon(), QKeySequence(tr("ctrl+q")));
57         connect(appExit, SIGNAL(triggered()), this, SLOT(close()));
58
59         appAbout = CreateAction(tr("&About"), tr("About"), tr("Display program information..."), QIcon(), QKeySequence(tr("")));
60         connect(appAbout, SIGNAL(triggered()), this, SLOT(AboutWozMaker()));
61
62         // Get signals from the gameboard to update scores...
63 //      connect(gameBoard, SIGNAL(UpdateScore(int)), this, SLOT(OnUpdateScore(int)));
64 //      connect(gameWidget, SIGNAL(GameWasWon()), this, SLOT(WeHaveAWinner()));
65
66         // Connect editor to game widget...
67 //      connect(editorWindow, SIGNAL(SetupLevel(Level *)), gameWidget,
68 //              SLOT(HandlePlayGameFromEditor(Level *)));
69
70
71         QMenu * menu = menuBar()->addMenu(tr("&File"));
72         menu->addAction(loadFile);
73         menu->addAction(saveFile);
74         menu->addAction(settings);
75         menu->addSeparator();
76 //      menu->addAction(undoAction);
77 //      menu->addAction(resetLevel);
78 //      menu->addAction(skipLevel);
79 //      menu->addSeparator();
80 //      menu->addAction(gameOptions);
81 //      menu->addAction(gameStats);
82 //      menu->addSeparator();
83         menu->addAction(appExit);
84
85         menu = menuBar()->addMenu(tr("&Help"));
86         menu->addAction(appAbout);
87
88 //      statusBar()->addPermanentWidget(gameBoard->score);
89         statusBar()->showMessage(tr("Ready"));
90
91         // Create dock widgets
92         QDockWidget * dw1 = new QDockWidget("A2R Info", this);
93         infoWidget = new InfoWidget;
94         dw1->setWidget(infoWidget);
95         addDockWidget(Qt::RightDockWidgetArea, dw1);
96
97         QDockWidget * dw2 = new QDockWidget("Disk Nav", this);
98         navWidget = new NavWidget;
99         dw2->setWidget(navWidget);
100         addDockWidget(Qt::RightDockWidgetArea, dw2);
101
102         connect(navWidget, SIGNAL(UpdateWaveform()), mainWidget->wfWidget, SLOT(HandleUpdate()));
103         connect(navWidget, SIGNAL(InitSync()), mainWidget->wfWidget, SLOT(HandleInitSync()));
104         connect(navWidget, SIGNAL(StepBack()), mainWidget->wfWidget, SLOT(HandleStepBack()));
105         connect(navWidget, SIGNAL(ManualSync()), mainWidget->wfWidget, SLOT(HandleManualSync()));
106         connect(navWidget, SIGNAL(ZeroAll()), mainWidget->wfWidget, SLOT(HandleZeroAll()));
107         connect(navWidget, SIGNAL(Synthesize()), mainWidget->wfWidget, SLOT(HandleSynthesize()));
108         connect(navWidget, SIGNAL(Sync(int, int)), mainWidget->wfWidget, SLOT(HandleSync(int, int)));
109         connect(navWidget, SIGNAL(WaveSync(int)), mainWidget->wfWidget, SLOT(HandleWaveSync(int)));
110         connect(navWidget, SIGNAL(Ratio(int, int)), mainWidget->wfWidget, SLOT(HandleRatio(int, int)));
111         connect(navWidget, SIGNAL(Synthesize2()), mainWidget->wfWidget, SLOT(HandleSynthesize2()));
112
113         connect(mainWidget->diskWidget, SIGNAL(UpdateWaveform()), mainWidget->wfWidget, SLOT(HandleUpdate()));
114         connect(mainWidget->diskWidget, SIGNAL(UpdateInfo()), navWidget, SLOT(ShowInfo()));
115
116         connect(mainWidget->wfWidget, SIGNAL(UpdateInfo()), navWidget, SLOT(ShowInfo()));
117
118         QSettings settings("Underground Software", "WOZ Maker");
119         QPoint mainWinPosition = settings.value("pos", QPoint(200, 100)).toPoint();
120         move(mainWinPosition);
121         QSize mainWinSize = settings.value("size", QSize(500, 500)).toSize();
122         resize(mainWinSize);
123         lastDir = settings.value("lastDir", "").toString();
124 }
125
126
127 MainWin::~MainWin()
128 {
129         if (Global::a2r)
130                 free(Global::a2r);
131 }
132
133
134 //
135 // Consolidates action creation from a multi-step process to a single-step one.
136 //
137 QAction * MainWin::CreateAction(QString name, QString tooltip, QString statustip,
138         QIcon icon, QKeySequence key, bool checkable/*= false*/)
139 {
140         QAction * action = new QAction(icon, name, this);
141         action->setToolTip(tooltip);
142         action->setStatusTip(statustip);
143         action->setShortcut(key);
144         action->setCheckable(checkable);
145
146         return action;
147 }
148
149
150 //
151 // This is essentially the same as the previous function, but this allows more
152 // than one key sequence to be added as key shortcuts.
153 //
154 QAction * MainWin::CreateAction(QString name, QString tooltip, QString statustip,
155         QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
156 {
157         QAction * action = new QAction(icon, name, this);
158         action->setToolTip(tooltip);
159         action->setStatusTip(statustip);
160         QList<QKeySequence> keyList;
161         keyList.append(key1);
162         keyList.append(key2);
163         action->setShortcuts(keyList);
164         action->setCheckable(checkable);
165
166         return action;
167 }
168
169
170 void MainWin::closeEvent(QCloseEvent * event)
171 {
172         QSettings settings("Underground Software", "WOZ Maker");
173         settings.setValue("pos", pos());
174         settings.setValue("size", size());
175         settings.setValue("lastDir", lastDir);
176
177         event->accept();
178 }
179
180
181 void MainWin::HandleLoadFile(void)
182 {
183         QString filename = QFileDialog::getOpenFileName(this, tr("Open A2R"), lastDir, tr("A2R Disk Image Files (*.a2r)"));
184
185         if (filename.isEmpty())
186                 return;
187
188         lastDir = QFileInfo(filename).path();
189
190         if (!LoadA2R(filename.toUtf8().data()))
191         {
192                 QMessageBox::critical(this, "Bad A2R file", "It may have an .a2r extension, but it isn't a valid A2R file.");
193                 return;
194         }
195
196         Global::trackNum = 0;
197         mainWidget->wfWidget->HandleUpdate();
198         mainWidget->nibbleWidget->Update();
199         mainWidget->diskWidget->DrawDisk();
200         infoWidget->ShowInfo();
201         navWidget->ShowInfo();
202         setWindowTitle(QString("WOZ Maker - %1").arg(QFileInfo(filename).baseName()));
203 }
204
205
206 void MainWin::HandleSaveFile(void)
207 {
208 }
209
210
211 void MainWin::HandleSettings(void)
212 {
213         SettingsDialog dlg(this);
214
215         if (dlg.exec() == false)
216                 return;
217
218         // Deal with stuff here, since user hit "OK" button
219 }
220
221
222 void MainWin::AboutWozMaker(void)
223 {
224         QMessageBox::about(this, tr("About WOZ Maker"), tr("WOZ Maker v1.0.0\n\nWritten by James Hammons\n\nCopyright (C) 2018 Underground Software"));
225 }
226
227
228 #if 0
229 bool MainWin::LoadA2R(const QString filename)
230 {
231 /*
232 Really, this crap should go into fileio.cpp.  !!! FIX !!!
233 */
234         static uint8_t a2rHdr[8] = { 0x41, 0x32, 0x52, 0x32, 0xFF, 0x0A, 0x0D, 0x0A };
235         static uint8_t a2rHdr1[8] = { 0x41, 0x32, 0x52, 0x31, 0xFF, 0x0A, 0x0D, 0x0A };
236
237         if (Global::a2r != NULL)
238                 free(Global::a2r);
239
240         Global::a2r = (A2R *)ReadFile(filename.toUtf8().data(), &Global::a2rSize);
241
242         if (Global::a2r == NULL)
243                 return false;
244
245         // Sanity check, to see if what we asked for is what we got
246         if (memcmp(Global::a2r->magic1, a2rHdr, 8) != 0)
247         {
248                 // It's not a v2 A2R file, maybe it's v1?
249                 if (memcmp(Global::a2r->magic1, a2rHdr1, 8) == 0)
250                 {
251                         // Bytes 8-F are usually: 01 00 8D 00 A5 01 00 00
252                         // First, allocate mem to copy the old version to the new:
253                         uint8_t * oldfile = (uint8_t *)Global::a2r;
254                         uint8_t * newfile = (uint8_t *)malloc(Global::a2rSize + 36);
255                         memcpy(newfile + 52, oldfile + 16, Global::a2rSize - 16);
256
257                         // Make sure creator is set correctly (v1 doesn't have it)
258                         memset(Global::a2r->creator, 0x20, 32);
259
260                         // Swap in the new file, free the old one
261                         free(Global::a2r);
262                         Global::a2r = (A2R *)newfile;
263                         Global::a2rSize += 36;  // Add in the size of the INFO chunk
264
265                         // Fix up stuff that's different between v1 and v2
266 //printf("A2R Size: %08X\n", Global::a2rSize);
267 //printf("A2R Stream Size: %08X (preswap)\n", Global::a2r->strmSize);
268                         SwapBytes32((uint8_t *)&Global::a2r->strmSize);
269 //printf("A2R Stream Size: %08X (postswap)\n", Global::a2r->strmSize);
270                         uint32_t dataSize = Uint32LE(Global::a2r->strmSize);
271                         uint32_t pos = 0;
272
273                         while (pos < dataSize)
274                         {
275                                 A2RStream * stream = (A2RStream *)(&Global::a2r->data[pos]);
276
277                                 if (stream->location == 0xFF)
278                                         break;
279
280                                 SwapBytes32(stream->dataLength);
281                                 SwapBytes32(stream->estLoopPoint);
282                                 pos += 10 + Uint32LE(stream->dataLength);
283                         }
284
285                         // Change INFO to META & fix size (if any, dunno if it's optional)
286                         if (Global::a2rSize > (60 + dataSize))
287                         {
288                                 memcpy(&Global::a2r->data[dataSize], "META", 4);
289                                 SwapBytes32(&Global::a2r->data[dataSize + 4]);
290                         }
291                 }
292                 else
293                 {
294                         free(Global::a2r);
295                         Global::a2r = NULL;
296
297                         QMessageBox::critical(this, "Bad A2R file", "It may have an .a2r extension, but it isn't a valid A2R file.");
298
299                         return false;
300                 }
301         }
302
303         // Setup individual stream pointers
304         Global::numStreams = 0;
305         uint8_t * streamPtr = Global::a2r->data;
306
307         while ((*streamPtr != 0xFF) && (Global::numStreams < 800))
308         {
309                 Global::stream[Global::numStreams] = (A2RStream *)streamPtr;
310                 streamPtr += 10 + Uint32LE(Global::stream[Global::numStreams]->dataLength);
311                 Global::numStreams++;
312         }
313
314         // Metadata check
315         Global::metadata = NULL;
316
317         if (Global::a2rSize > (60 + Uint32LE(Global::a2r->strmSize)))
318         {
319                 Global::metadata = (A2RMetadata *)((uint8_t *)Global::a2r + (60 + Uint32LE(Global::a2r->strmSize)));
320
321                 // Make sure it's plausible metadata
322                 if (memcmp(Global::metadata->metaTag, "META", 4) != 0)
323                         Global::metadata = NULL;
324         }
325
326         // Unpack TMNG & XTMG streams to simplify analysis
327         for(uint32_t i=0; i<Global::numStreams; i++)
328         {
329                 Global::waveLen[i] = 0;
330
331                 // Don't bother with BITS captures
332                 if (Global::stream[i]->captureType == 2)
333                         continue;
334
335                 // We skip the first timing byte because it can't tell you what the actual time was when the pulse was seen--it's the time between when the *capture* started and the pulse was seen, not the time between the previous pulse and this one!  So that first one is discarded since it's worse than useless; it's misleading.
336                 for(uint32_t j=1; j<Uint32LE(Global::stream[i]->dataLength); j++)
337                 {
338                         uint32_t a = Global::stream[i]->data[j];
339
340                         while ((Global::stream[i]->data[j] == 0xFF) || (a < 24))
341                         {
342                                 j++;
343                                 a += Global::stream[i]->data[j];
344                         }
345
346                         Global::wave[i][Global::waveLen[i]] = a;
347                         Global::waveLen[i]++;
348                 }
349         }
350
351 //ISO-8061 date format
352 #if 0
353         char buf[200];
354         time_t t = time(NULL);
355         tm * tmp = gmtime(&t);
356         strftime(buf, sizeof(buf), "%FT%TZ", tmp);
357         printf("Time is: %s\n", buf);
358 #endif
359
360         return true;
361 }
362 #endif
363