-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreMenu.cpp
263 lines (215 loc) · 8.58 KB
/
ScoreMenu.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
// This source code is part of QAbc, a minimal ABC music notation editor.
// QAbc is Copyright © 2021 Benoît Rouits <[email protected]>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "ScoreMenu.h"
#include "AbcApplication.h"
#include "AbcPlainTextEdit.h"
#include "EditTabWidget.h"
#include "EditWidget.h"
#include "settings.h"
#include <QMessageBox>
#include <QApplication>
#include <QFileDialog>
#include <QStandardPaths>
#include <QDebug>
ScoreMenu::ScoreMenu(QWidget* parent)
: QMenu(parent)
{
setTitle(tr("Score"));
#if (QT_VERSION < QT_VERSION_CHECK(6, 3, 0))
addAction(tr("New"), this, SLOT(onNewActionTriggered()), QKeySequence::New);
addAction(tr("Open"), this, SLOT(onOpenActionTriggered()), QKeySequence::Open);
#else
addAction(tr("New"), QKeySequence::New, this, SLOT(onNewActionTriggered()));
addAction(tr("Open"), QKeySequence::Open, this, SLOT(onOpenActionTriggered()));
#endif
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActs[i] = new QAction(this);
recentFileActs[i]->setVisible(false);
connect(recentFileActs[i], &QAction::triggered, this, &ScoreMenu::onOpenRecentActionTriggered);
}
QMenu* sub = addMenu(tr("Recently opened"));
for (int i = 0; i < MaxRecentFiles; ++i)
sub->addAction(recentFileActs[i]);
updateRecentFileActions();
#if (QT_VERSION < QT_VERSION_CHECK(6, 3, 0))
addAction(tr("Save"), this, SLOT(onSaveActionTriggered()), QKeySequence::Save);
addAction(tr("Save as"), this, SLOT(onSaveAsActionTriggered()), QKeySequence::SaveAs);
addAction(tr("Close"), this, SLOT(onCloseActionTriggered()), QKeySequence::Close);
addAction(tr("Quit"), this, SLOT(onQuitActionTriggered()), QKeySequence::Quit);
#else
addAction(tr("Save"), QKeySequence::Save, this, SLOT(onSaveActionTriggered()));
addAction(tr("Save as"), QKeySequence::SaveAs, this, SLOT(onSaveAsActionTriggered()));
addAction(tr("Close"), QKeySequence::Close, this, SLOT(onCloseActionTriggered()));
addAction(tr("Quit"), QKeySequence::Quit, this, SLOT(onQuitActionTriggered()));
#endif
}
ScoreMenu::~ScoreMenu()
{
}
QMessageBox::StandardButton ScoreMenu::gracefulQuit()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
EditTabWidget* tabs = a->mainWindow()->mainHBoxLayout()->editTabWidget();
int unsaved= 0;
for (int i = 0; i < tabs->editWidgetList()->length(); i++) {
if (!tabs->editWidgetList()->at(i)->editVBoxLayout()->abcPlainTextEdit()->isSaved())
unsaved++;
}
if (unsaved && QMessageBox::StandardButton::No == QMessageBox::question(a->mainWindow(), tr("Really quit?"),
QString::number(unsaved) +
tr(" score(s) not saved.\nDo you want to quit anyway?")))
return QMessageBox::StandardButton::No;
return QMessageBox::StandardButton::Yes;
}
void ScoreMenu::onQuitActionTriggered()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
EditTabWidget* tabs = a->mainWindow()->mainHBoxLayout()->editTabWidget();
if (QMessageBox::StandardButton::Yes == gracefulQuit()) {
tabs->removeTabs();
a->quit();
}
}
void ScoreMenu::onOpenActionTriggered()
{
QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QString fileName = QFileDialog::getOpenFileName(this, tr("Open ABC Score"), home, tr("ABC score (*.abc)"));
/* user cancelled */
if (fileName.isEmpty())
return;
loadFile(fileName);
}
QString ScoreMenu::strippedName(const QString& fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void ScoreMenu::updateRecentFileActions()
{
Settings settings;
QStringList files = settings.value("recentFileList").toStringList();
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
for (int i = 0; i < numRecentFiles; ++i) {
QString text = QString("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
recentFileActs[i]->setText(text);
recentFileActs[i]->setData(files[i]);
recentFileActs[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
recentFileActs[j]->setVisible(false);
}
void ScoreMenu::setRecentFile(const QString& fileName, bool ok)
{
Settings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(fileName);
if (ok)
files.prepend(fileName);
while (files.size() > MaxRecentFiles)
files.removeLast();
settings.setValue("recentFileList", files);
updateRecentFileActions();
}
bool ScoreMenu::loadFile(const QString& fileName)
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* w = a->mainWindow();
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text)) {
EditTabWidget *edittabs = w->mainHBoxLayout()->editTabWidget();
EditWidget* widget = new EditWidget(fileName, edittabs);
AbcPlainTextEdit *edit = widget->editVBoxLayout()->abcPlainTextEdit();
edit->setPlainText(file.readAll());
file.close();
edit->setSaved();
edittabs->addTab(widget);
setRecentFile(fileName, true);
return true;
}
setRecentFile(fileName, false);
return false;
}
void ScoreMenu::onOpenRecentActionTriggered()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* w = a->mainWindow();
QAction *action = qobject_cast<QAction *>(sender());
if (action) {
if (!loadFile(action->data().toString())) {
QMessageBox::warning(w, tr("Warning"), tr("Could not open score!"));
}
}
}
void ScoreMenu::onSaveActionTriggered()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* w = a->mainWindow();
EditTabWidget *edittabs = w->mainHBoxLayout()->editTabWidget();
int cur = edittabs->currentIndex();
if (cur < 0)
return;
QString fileName = (*edittabs->currentEditWidget()->fileName());
if (fileName.isEmpty()) {
QMessageBox::warning(w, tr("Warning"), tr("Could not save an untitled ABC score!"));
return;
}
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Text)) {
AbcApplication* a = static_cast<AbcApplication*>(qApp);
EditTabWidget *edittabs = a->mainWindow()->mainHBoxLayout()->editTabWidget();
EditWidget* widget = static_cast<EditWidget*>(edittabs->currentWidget());
AbcPlainTextEdit *edit = widget->editVBoxLayout()->abcPlainTextEdit();
QString tosave = edit->toPlainText();
file.write(tosave.toUtf8());
file.close();
edit->setSaved();
w->statusBar()->showMessage(tr("Score saved."));
setRecentFile(fileName, true);
} else {
QMessageBox::warning(w, tr("Warning"), tr("Could not save ABC score!"));
}
}
void ScoreMenu::onSaveAsActionTriggered()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* w = a->mainWindow();
EditTabWidget *edittabs = w->mainHBoxLayout()->editTabWidget();
int cur = edittabs->currentIndex();
if (cur < 0)
return;
QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QString fileName = QFileDialog::getSaveFileName(this, tr("Save ABC score"), home, tr("ABC score (*.abc)"));
if (fileName.isEmpty())
return; /* cancelled */
if (!fileName.endsWith(".abc"))
fileName.append(".abc");
QFileInfo info(fileName);
edittabs->setTabText(edittabs->currentIndex(), info.baseName());
edittabs->currentEditWidget()->setFileName(fileName);
return onSaveActionTriggered();
}
void ScoreMenu::onCloseActionTriggered()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* w = a->mainWindow();
EditTabWidget *edittabs = w->mainHBoxLayout()->editTabWidget();
int cur = edittabs->currentIndex();
if (cur >= 0)
edittabs->askRemoveTab(cur);
}
void ScoreMenu::onNewActionTriggered()
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* w = a->mainWindow();
EditTabWidget *edittabs = w->mainHBoxLayout()->editTabWidget();
QString empty;
EditWidget* swidget = new EditWidget(empty, nullptr);
swidget->editVBoxLayout()->abcPlainTextEdit()->setPlainText(NEW_TEMPLATE);
edittabs->addTab(swidget);
}