This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
230 lines (210 loc) · 7.83 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QFontDatabase>
#include <QFont>
#include <QPrinter>
#include <QTextDocument>
#include <QTextDocumentWriter>
#include <QMidiFile.h>
#include <QMidiOut.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Load Byzantine symbol fonts
const int FONT_COUNT = 6;
QString fonts[FONT_COUNT] = { "Fthora", "Omega", "Oxeia", "Psaltica", "Special1", "Special2" };
int id = 0;
for (int i = 0; i < FONT_COUNT; i++) {
id = m_fonts.addApplicationFont(":/ByzMusicFonts/" + fonts[i]);
if (id < 0) {
QMessageBox::critical(NULL, "Error", "Failed to load all Byzantine Music fonts. Please contact developer.");
return;
}
}
ui->textBrowser->setFontFamily(m_fonts.applicationFontFamilies(3).at(0));
ui->textBrowser->setFontPointSize(20);
ui->textEdit->setFontPointSize(12);
// Slots / Signals
// Menu Actions
// File
connect(ui->actionNew, SIGNAL(triggered(bool)), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(open()));
connect(ui->actionSave, SIGNAL(triggered(bool)), this, SLOT(save()));
connect(ui->actionSave_As, SIGNAL(triggered(bool)), this, SLOT(saveAs()));
connect(ui->actionExit, SIGNAL(triggered(bool)), this, SLOT(close()));
// Export
connect(ui->actionMIDI, SIGNAL(triggered(bool)), this, SLOT(exportMidi()));
connect(ui->actionODF, SIGNAL(triggered(bool)), this, SLOT(exportODF()));
connect(ui->actionPDF, SIGNAL(triggered(bool)), this, SLOT(exportPDF()));
// Edit
connect(ui->actionUndo, SIGNAL(triggered(bool)), this, SLOT(undoEdit()));
connect(ui->actionRedo, SIGNAL(triggered(bool)), this, SLOT(redoEdit()));
// Other Actions
connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(changed()));
}
MainWindow::~MainWindow()
{
delete ui;
}
// Override close event
void MainWindow::closeEvent(QCloseEvent *event) {
// Ask user if they want to close with unsaved changes
if (!saved) {
QMessageBox::StandardButton res = QMessageBox::question(this, "Ison Notation",
tr("You have unsaved changes. Are you sure you want to exit?"),
QMessageBox::Cancel | QMessageBox::Yes,
QMessageBox::Cancel);
if (res == QMessageBox::Cancel) event->ignore();
else event->accept();
} else event->accept();
}
void MainWindow::resetTitle() {
// Change window title to reflect file name and save state
QString name = QString(m_fileName).remove(0, m_fileName.lastIndexOf('/') + 1);
if (!this->saved) name.append('*');
this->topLevelWidget()->setWindowTitle("Ison Notation v1.0 - " + name);
}
void MainWindow::newFile() {
// Reset to a new file
m_dataSet = nullptr;
m_fileName = QString();
ui->textEdit->clear();
resetTitle();
}
const QString MainWindow::openFile(const QString& fileName) const {
// Load a string of characters from file
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly) || !file.exists()) {
QMessageBox::information(NULL, tr("Unable to open file"), file.errorString());
return "";
}
// Read data as string
QTextStream in(&file);
QString output = in.readAll();
return output;
}
void MainWindow::open() {
// Open a file
const QString fileName = QFileDialog::getOpenFileName(this, tr("Open .ison file"), "", tr("Ison Notation files (*.*)"));
if (fileName.isEmpty()) return;
const QString input = openFile(fileName);
this->m_dataSet = parse(input);
this->m_fileName = fileName;
ui->statusBar->showMessage("File opened", 5000);
ui->textEdit->setText(input);
saved = true;
resetTitle();
}
void MainWindow::save() {
if (m_fileName.isEmpty()) {
saveAs();
} else {
saveFile(m_fileName);
}
}
void MainWindow::saveAs() {
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Ison Notation file (*.ison)"));
if (fileName.isEmpty()) return;
saveFile(fileName);
}
void MainWindow::saveFile(const QString &fileName) {
QFile file(fileName);
if (!file.open(QIODevice::ReadWrite)) {
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}
QTextStream out(&file);
out << ui->textEdit->toPlainText();
saved = true;
ui->statusBar->showMessage("File saved", 5000);
m_fileName = QString(fileName);
resetTitle();
file.close();
}
void MainWindow::changed() {
QString input = ui->textEdit->toPlainText();
m_dataSet = parse(input);
saved = false;
display();
resetTitle();
}
void MainWindow::undoEdit() {
ui->textEdit->undo();
ui->statusBar->showMessage("Edit undone", 5000);
}
void MainWindow::redoEdit() {
ui->textEdit->redo();
ui->statusBar->showMessage("Edit redone", 5000);
}
void MainWindow::display() {
ui->textBrowser->clear();
auto iter = m_dataSet->iterator();
for (iter; iter.hasNext(); iter++) {
// Add item to browser
QString code = QString::fromStdString(iter.symbol()->getFontCode());
ui->textBrowser->setText(ui->textBrowser->toPlainText() + code);
}
}
void MainWindow::exportMidi() {
// Get file name
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("MIDI (*.mid)"));
if (fileName.isEmpty()) return;
// Load base file since I can't manage to create base from scratch
QMidiFile f;
f.load(":/midi/Base");
// Loop through dataset
auto iter = m_dataSet->iterator();
int note = m_dataSet->getStart()->getNote();
for (float i = 0; iter.hasNext(); iter++) {
int step = iter.symbol()->getStep();
// Check if it's a standard symbol (i.e. not a modifier)
if (step > -1) {
float duration = iter.symbol()->getDuration();
note += (iter.symbol()->isUp()) ? step : -step;
int mod = note >= 0 ? note % 7 : (7 - abs(note % 7)) % 7;
IsonNotation::Parallagi current = static_cast<IsonNotation::Parallagi>(mod);
int octave = (note > -1 ? note : note - 7) / 7 * 12;
// 60 = Middle C
int final = 60 + octave + IsonNotation::scaleSteps[current];
// 490 = Quarter Note
f.createNoteOnEvent(0, i * 490, 0, final, 60);
i += duration;
f.createNoteOffEvent(0, i * 490, 0, final, 60);
}
}
// Save to filename
f.save(fileName);
ui->statusBar->showMessage("Exported to MIDI", 5000);
}
void MainWindow::exportODF() {
// Get file name
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Open Document Format (*.odt)"));
if (fileName.isEmpty()) return;
// Get output as text document and write it
QTextDocumentWriter writer(fileName);
QTextDocument *doc = ui->textBrowser->document();
writer.write(doc);
ui->statusBar->showMessage("Exported to ODF", 5000);
}
void MainWindow::exportPDF() {
// Get file name
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Adobe Portable Document Format (*.pdf)"));
if (fileName.isEmpty()) return;
// Set up printer for writing pdf file
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
// Get document from textBrowser
QTextDocument *doc = ui->textBrowser->document();
doc->print(&printer);
ui->statusBar->showMessage("Exported to PDF", 5000);
}
std::shared_ptr<IsonNotation::DataSet> MainWindow::parse(const QString& input) const {
return m_parser.parse(input.toStdString());
}