-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
509 lines (436 loc) · 15.3 KB
/
mainwindow.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileSystemModel>
#include "metronome.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
metronome(new Metronome(this))
{
ui->setupUi(this);
// Connect signals
connect(dynamic_cast<QObject*>(metronome), SIGNAL(notifyTick(int,int)), this, SLOT(on_metronomeTick(int,int)));
connect(dynamic_cast<QObject*>(metronome), SIGNAL(notifyAddMeasure(IMeasure*)), this, SLOT(on_metronomeAddMeasure(IMeasure*)));
connect(dynamic_cast<QObject*>(metronome), SIGNAL(notifyChangeMeasure(IMeasure*)), this, SLOT(on_metronomeChangeMeasure(IMeasure*)));
connect(dynamic_cast<QObject*>(metronome), SIGNAL(notifyStop()), this, SLOT(on_metronomeStop()));
connect(dynamic_cast<QObject*>(metronome), SIGNAL(notifyMeasureMoveUp(IMeasure*)), this, SLOT(on_measureMoveUp(IMeasure*)));
connect(dynamic_cast<QObject*>(metronome), SIGNAL(notifyMeasureMoveDown(IMeasure*)), this, SLOT(on_measureMoveDown(IMeasure*)));
// Setup vertical / horizontal layouts
ui->hLayout_currentFile->setAlignment(Qt::AlignLeft);
ui->hLayout_currentPlayingMeasure->setAlignment(Qt::AlignLeft);
ui->hLayout_repeitionsUntilNextMeasure->setAlignment(Qt::AlignLeft);
ui->vLayout_information->setAlignment(Qt::AlignTop);
ui->vLayout_measures->setAlignment(Qt::AlignTop);
// Start with empty state
clearWindow();
}
MainWindow::~MainWindow()
{
delete ui;
}
// =============================================================================
// File
/**
* @brief MainWindow::openFile
* @param filePath the location of the file to open
* @return wheter or not the file could be opened
*
* Read the file and turn it's contents into a json document and
* delegate the loading of it to another method; if the json document
* could not be loaded by the other method return false
*
*/
bool MainWindow::openFile(QString filePath)
{
QFile file(filePath);
if (!file.exists() || !file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("Opening File"), tr("The selected file does not exist"));
return false;
}
QString fileContents = QString(file.readAll());
QJsonDocument jsonDocument = QJsonDocument::fromJson(fileContents.toUtf8());
file.flush();
file.close();
bool fileOpened = loadFromJson(jsonDocument);
if (!fileOpened) {
QMessageBox::warning(this, tr("Opening File"), tr("The selected file is not a valid Advanced Metronome file"));
return false;
}
lastSavedFile = filePath;
ui->lbl_currentFile->setText(lastSavedFile);
return true;
}
/**
* @brief MainWindow::saveFile
* @param filePath the path to save the file to
* @return wheter or not the file was saved
*
* Write a json document to the given file path
*
*/
bool MainWindow::saveFile(QString filePath)
{
QFile file(filePath);
if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
QJsonDocument document = getJsonDocument();
file.write(document.toJson());
file.flush();
file.close();
return true;
}
return false;
}
// =============================================================================
// JSON
/**
* @brief MainWindow::loadFromJson
* @param jsonDocument the json document that needs to be loaded
* @return wheter or not the json document could be loaded
*
* Load all the values from the json document in the correct
* user fillable fields; if the json document isn't valid then
* this method will return false and not load the fields
*
*/
bool MainWindow::loadFromJson(QJsonDocument jsonDocument)
{
// Invalid JSON
if (jsonDocument.isNull() || !jsonDocument.isObject()) {
return false;
}
clearWindow();
QJsonObject jsonObject = jsonDocument.object();
QString songTitle = jsonObject.value(JSON_KEY_TITLE).toString();
bool preMetronomeEnabled = jsonObject.value(JSON_KEY_PRE_METRONOME_ENABLED).toBool();
int preMetronomeBeatsPerMinute = jsonObject.value(JSON_KEY_PRE_METRONOME_BPM).toInt();
int preMetronomeNumberOfTicks = jsonObject.value(JSON_KEY_PRE_METRONOME_TICKS).toInt();
QJsonArray jsonArrayMeasures = jsonObject.value(JSON_KEY_MEASURES).toArray();
metronome->loadMeasuresFromJson(jsonArrayMeasures);
ui->le_songTitle->setText(songTitle);
ui->cb_usePreMetronomeTicks->setChecked(preMetronomeEnabled);
ui->spn_preMetronomeBeatsPerMinute->setValue(preMetronomeBeatsPerMinute);
ui->spn_preMetronomeNumberOfTicks->setValue(preMetronomeNumberOfTicks);
return true;
}
/**
* @brief MainWindow::getJsonDocument
* @return JSON representation of the user fillable fields
*
* Put all user fillable fields into a JSON document; the jsonification
* is handled by the metronome object
*
*/
QJsonDocument MainWindow::getJsonDocument()
{
QJsonDocument jsonDocument;
QJsonObject jsonObject;
QString songTitle = ui->le_songTitle->text();
bool preMetronomeEnabled = ui->cb_usePreMetronomeTicks->isChecked();
int preMetronomeBeatsPerMinute = ui->spn_preMetronomeBeatsPerMinute->value();
int preMetronomeNumberOfTicks= ui->spn_preMetronomeNumberOfTicks->value();
QJsonArray jsonArrayMeasures = metronome->getMeasuresAsJsonArray();
jsonObject.insert(JSON_KEY_TITLE, songTitle);
jsonObject.insert(JSON_KEY_PRE_METRONOME_ENABLED, preMetronomeEnabled);
jsonObject.insert(JSON_KEY_PRE_METRONOME_BPM, preMetronomeBeatsPerMinute);
jsonObject.insert(JSON_KEY_PRE_METRONOME_TICKS, preMetronomeNumberOfTicks);
jsonObject.insert(JSON_KEY_MEASURES, jsonArrayMeasures);
jsonDocument.setObject(jsonObject);
return jsonDocument;
}
// =============================================================================
// Window
/**
* @brief MainWindow::clearWindow
*
* Empty and set all the user fillable fields
* to their defaults
*
*/
void MainWindow::clearWindow()
{
// Clear song
ui->le_songTitle->clear();
ui->cb_usePreMetronomeTicks->setChecked(DEFAULT_PRE_METRONOME_ENABLED);
ui->spn_preMetronomeBeatsPerMinute->setValue(DEFAULT_PRE_METRONOME_BPM);
ui->spn_preMetronomeNumberOfTicks->setValue(DEFAULT_PRE_METRONOME_TICKS);
// Delete metronome
metronome->deleteMeasures();
// Clear file
lastSavedFile.clear();
ui->lbl_currentFile->setText(DEFAULT_NONE_VALUE);
ui->lbl_currentPlayingMeasure->setText(DEFAULT_NONE_VALUE);
}
/**
* @brief MainWindow::swapMeasures
* @param indexOne first measure to swap with second measure
* @param indexTwo second measure to swap with first measure
*
* Swap two measures at given indices
*/
void MainWindow::swapMeasures(int indexOne, int indexTwo)
{
int totalMeasures = ui->vLayout_measures->count();
bool indexOneExists = (indexOne >= 0 && indexOne <= totalMeasures);
bool indexTwoExists = (indexTwo >= 0 && indexTwo <= totalMeasures);
bool indicesAreSame = (indexOne == indexTwo);
if (!indicesAreSame && indexOneExists && indexTwoExists) {
QWidget *widgetOne = ui->vLayout_measures->itemAt(indexOne)->widget();
QWidget *widgetTwo = ui->vLayout_measures->itemAt(indexTwo)->widget();
ui->vLayout_measures->insertWidget(indexOne, widgetTwo);
ui->vLayout_measures->insertWidget(indexTwo, widgetOne);
}
}
// =============================================================================
// Slots
/**
* @brief MainWindow::on_metronomeTick
* @param totalRepetitions total repetitions for a measure
* @param currentRepetition current repetition the measure is in
*
* Update the UI to show the user how many repetitions of a
* measure are left
*
*/
void MainWindow::on_metronomeTick(int totalRepetitions, int currentRepetition)
{
if (currentRepetition > totalRepetitions) {
return;
}
int repetitionsLeft = totalRepetitions - currentRepetition;
ui->lbl_repetitionsUntilNextMeasure->setText(
QString::number(repetitionsLeft));
}
/**
* @brief MainWindow::on_metronomeAddMeasure
* @param measure the measure to add to the measures layout
*
* Add the given measure to the measures layout
*
*/
void MainWindow::on_metronomeAddMeasure(IMeasure *measure)
{
if (measure != NULL) {
ui->vLayout_measures->addWidget(dynamic_cast<QWidget*>(measure));
}
}
/**
* @brief MainWindow::on_metronomeChangeMeasure
* @param newMeasure the measure that is being played right now
*
* Display the title of the currently playing measure on the
* screen
*
*/
void MainWindow::on_metronomeChangeMeasure(IMeasure *measure)
{
if (measure == NULL) {
ui->lbl_currentPlayingMeasure->setText(DEFAULT_NONE_VALUE);
return;
}
ui->lbl_currentPlayingMeasure->setText(measure->getTitle());
}
/**
* @brief MainWindow::on_metronomeStop
*
* Update UI elements to show that the metronome has stopped
*
*/
void MainWindow::on_metronomeStop()
{
ui->lbl_currentPlayingMeasure->setText(DEFAULT_NONE_VALUE);
ui->lbl_repetitionsUntilNextMeasure->setText(
QString::number(DEFAULT_REPETITIONS_UNTIL_NEXT_MEASURE));
}
/**
* @brief MainWindow::on_measureMoveUp
* @param measure the measure to move up in the scroll area
*
* Move the given measure on position up in the scroll area
*
*/
void MainWindow::on_measureMoveUp(IMeasure *measure)
{
QWidget *currentWidget = dynamic_cast<QWidget*>(measure);
if (currentWidget == NULL) {
return;
}
int currentIndex = ui->vLayout_measures->indexOf(currentWidget);
int nextIndex = currentIndex + 1;
swapMeasures(currentIndex, nextIndex);
}
/**
* @brief MainWindow::on_measureMoveDown
* @param measure the measure to move down in the scroll area
*
* Move the given measure on position down in the scroll area
*
*/
void MainWindow::on_measureMoveDown(IMeasure *measure)
{
QWidget *currentWidget = dynamic_cast<QWidget*>(measure);
if (currentWidget == NULL) {
return;
}
int currentIndex = ui->vLayout_measures->indexOf(currentWidget);
int previousIndex = currentIndex - 1;
swapMeasures(currentIndex, previousIndex);
}
// =============================================================================
// File action menu
/**
* @brief MainWindow::on_actionNew_triggered
*
* This method displays a dialog to the user if a file is being
* edited, if the user still wants to continue creating a new file
* then the actual clearing of the window is being delegated to another
* method
*
*/
void MainWindow::on_actionNew_triggered()
{
// Check if a file is opened
if (lastSavedFile != NULL && lastSavedFile.length() > 0) {
int reply = QMessageBox::question(this, "Create new song", tr("It looks like you have a file opened, are you sure you want to create a new song?"), QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
clearWindow();
}
}
}
/**
* @brief MainWindow::on_actionSave_triggered
*
* This method checks if a file is already being
* edited, if that's the case then it delegates
* the writing of the file to another method,
* if there isn't a file being edited is calls
* the method that always shows a save file dialog
*
*/
void MainWindow::on_actionSave_triggered()
{
if (lastSavedFile.length() <= 0) {
on_actionSave_As_triggered();
return;
}
saveFile(lastSavedFile);
}
/**
* @brief MainWindow::on_actionSave_As_triggered
*
* This method always displays a save file dialog,
* the actual writing of the file is delegated
* to another method
*
*/
void MainWindow::on_actionSave_As_triggered()
{
QFileDialog saveFileDialog(this, tr("Save Advanced Metronome Song"));
saveFileDialog.setAcceptMode(QFileDialog::AcceptSave);
saveFileDialog.setNameFilter(tr("Advanced Metronome Song") + FILE_EXT_PATTERN);
saveFileDialog.setDefaultSuffix(FILE_EXT);
int result = saveFileDialog.exec();
if (result == QMessageBox::Accepted &&
saveFileDialog.selectedFiles().length() > 0) {
saveFile(saveFileDialog.selectedFiles().first());
}
}
/**
* @brief MainWindow::on_actionOpen_triggered
*
* This method displays the open file dialog
* and delegates the opening/reading of the
* file to another method
*
*/
void MainWindow::on_actionOpen_triggered()
{
// Open
QFileDialog openFileDialog(this, tr("Open Advanced Metronome Song"));
openFileDialog.setAcceptMode(QFileDialog::AcceptOpen);
openFileDialog.setNameFilter(tr("Advanced Metronome Song") + FILE_EXT_PATTERN);
int result = openFileDialog.exec();
if (result == QMessageBox::Accepted &&
openFileDialog.selectedFiles().length() > 0) {
openFile(openFileDialog.selectedFiles().first());
}
}
// =============================================================================
// Metronome action menu
/**
* @brief MainWindow::on_actionPlay_triggered
*
* Delegate the starting of the metronome to another
* method while passing in wheter or not the pre-metronome
* should be used
*
*/
void MainWindow::on_actionPlay_triggered()
{
bool usePreMetronome = ui->cb_usePreMetronomeTicks->isChecked();
int preMetronomeBpm = ui->spn_preMetronomeBeatsPerMinute->value();
int preMetronomeTicks = ui->spn_preMetronomeNumberOfTicks->value();
metronome->setupPreMetronome(preMetronomeBpm, preMetronomeTicks);
metronome->start(usePreMetronome);
}
/**
* @brief MainWindow::on_actionStop_triggered
*
* Stop the metronome
*
*/
void MainWindow::on_actionStop_triggered()
{
metronome->stop();
}
// =============================================================================
// Measure action menu
/**
* @brief MainWindow::on_actionAdd_Measure_triggered
*
* Delegate the creation and adding of a new measure object
* to another method
*
*/
void MainWindow::on_actionAdd_Measure_triggered()
{
metronome->addMeasure();
}
// =============================================================================
// Help action menu
/**
* @brief MainWindow::on_actionAbout_Advanced_Metronome_triggered
*
* Show about dialog for Advanced Metronome
*
*/
void MainWindow::on_actionAbout_Advanced_Metronome_triggered()
{
QMessageBox::about(this, tr("About Advanced Metronome"), tr("Advanced Metronome is created for musicians who want to practice songs which "
"have multiple speeds and/or time-durations. 'Cause regular metronomes only "
"allow one constant tempo Advanced Metronome steps in to solve this problem.\n\n"
"Advanced Metronome also gives you the ability to save and share your songs "
"with others so this application will suit many bands in their song-writing "
"process.\n\n"
"This application is licensed under the GPLv3 or higher.\n\n"
"Copyright (c) Bart Kessels"));
}
/**
* @brief MainWindow::on_actionAbout_Qt_triggered
*
* Show about dialog for Qt
*
*/
void MainWindow::on_actionAbout_Qt_triggered()
{
QMessageBox::aboutQt(this);
}
/**
* @brief MainWindow::on_actionView_on_GitHub_triggered
*
* Open the github page
*
*/
void MainWindow::on_actionView_on_GitHub_triggered()
{
QDesktopServices::openUrl(QUrl(URI_GITHUB));
}