]> Shamusworld >> Repos - wozmaker/blob - src/mainwin.cpp
Flesh out the disk settings dialog.
[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         // Sanity check
214         if (Global::a2r == NULL)
215                 return;
216
217         SettingsDialog dlg(this);
218
219         dlg.line[0].setText((char *)GetMetadata("title"));
220         dlg.line[1].setText((char *)GetMetadata("subtitle"));
221         dlg.line[2].setText((char *)GetMetadata("publisher"));
222         dlg.line[3].setText((char *)GetMetadata("developer"));
223         dlg.line[4].setText((char *)GetMetadata("copyright"));
224         dlg.line[5].setText((char *)GetMetadata("version"));
225         dlg.line[6].setText((char *)GetMetadata("notes"));
226         dlg.line[7].setText((char *)GetMetadata("side_name"));
227         dlg.line[8].setText((char *)GetMetadata("contributor"));
228
229         uint16_t machBits = GetRequiredMachineBits();
230
231         dlg.check[0].setChecked(machBits & 0x001 ? true : false);
232         dlg.check[1].setChecked(machBits & 0x002 ? true : false);
233         dlg.check[2].setChecked(machBits & 0x004 ? true : false);
234         dlg.check[3].setChecked(machBits & 0x008 ? true : false);
235         dlg.check[4].setChecked(machBits & 0x010 ? true : false);
236         dlg.check[5].setChecked(machBits & 0x020 ? true : false);
237         dlg.check[6].setChecked(machBits & 0x040 ? true : false);
238         dlg.check[7].setChecked(machBits & 0x080 ? true : false);
239         dlg.check[8].setChecked(machBits & 0x100 ? true : false);
240         dlg.check[9].setChecked(Global::a2r->writeProtected ? true : false);
241
242         dlg.combo[1].setCurrentIndex(GetRequiredRAMIndex());
243
244         if (dlg.exec() == false)
245                 return;
246
247         // Deal with stuff here, since user hit "OK" button
248 }
249
250
251 void MainWin::AboutWozMaker(void)
252 {
253         QMessageBox::about(this, tr("About WOZ Maker"), tr("WOZ Maker v1.0.0\n\nWritten by James Hammons\n\nCopyright (C) 2019 Underground Software"));
254 }
255