-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmainwindow.cpp
263 lines (216 loc) · 7.66 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "timelinesettingsdialog.h"
#include "projectsettingsdialog.h"
#include "timeline.h"
#include "project.h"
#include "placeholderevent.h"
#include "util.h"
#include <QFileDialog>
#include <QShortcut>
#include <QDebug>
#include <QMessageBox>
#include <QSettings>
#include <memory>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// wire placeholder actions to create a PlaceholderEvent
const auto placeholders = ui->menuAdd_Placeholder->actions();
for (int i = 0; i < placeholders.size(); i++) {
QAction* action = placeholders.at(i);
QObject::connect(action, &QAction::triggered,
[this, i] {
auto event = std::make_shared<PlaceholderEvent>(ui->timeline->cursorSnapped(), i);
ui->timeline->addEventToCurrentLayer(event);
});
QObject::connect(ui->timeline, &TimelineContainer::currentLayerChanged,
[action](int idx) {
action->setEnabled(idx != -1);
});
placeholders.at(i)->setShortcut(QKeySequence(Qt::Key_1 + i));
}
QObject::connect(ui->timeline, &TimelineContainer::currentLayerChanged,
[this](int idx) {
statusBar()->showMessage(QString("Current layer: %1.").arg(idx));
});
// selection-related stuff
// print a message when selection changes and
// disable selection-related actions when we have no selection
QObject::connect(ui->timeline, &TimelineContainer::currentSelectionChanged,
[this](const Selection& selection) {
if (selection.state() == Selection::DONE) {
statusBar()->showMessage(QString("Selected %1 to %2.")
.arg(selection.left())
.arg(selection.right()));
}
ui->actionDelete_Selection->setEnabled(selection.state() == Selection::DONE);
});
updateTitle();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newProject()
{
QFileInfo inputPath = QFileDialog::getSaveFileName(this, "New Project (will make subfolder)", "", "");
if (inputPath.filePath().isEmpty() || !closeProject())
return;
const QString name = inputPath.fileName();
const QString dir = inputPath.path() + "/" + name;
Project::init(dir, name);
statusBar()->showMessage(QString("Created project in %1.")
.arg(Project::get()->root()));
newTimeline();
}
void MainWindow::openProject()
{
QSettings settings;
const char* key = "mainwindow/lastOpenedProject";
QString openPath = QFileDialog::getOpenFileName(this, "Open Project",
settings.value(key, "").toString(),
"Bugel Project (*.bgp)");
if (!QFileInfo(openPath).exists() || !closeProject())
return;
settings.setValue(key, openPath);
Project::open(openPath);
statusBar()->showMessage(QString("Opened project %1.")
.arg(openPath));
updateTitle();
const QString lastPath = Project::get()->lastOpenedTimeline();
if (QFileInfo(lastPath).isFile()) {
openTimeline(lastPath);
}
}
void MainWindow::openProjectSettingsDialog()
{
if (!Project::get())
return;
ProjectSettingsDialog dlg(Project::get());
if (dlg.exec() == QDialog::Accepted) {
dlg.apply(Project::get());
}
}
bool MainWindow::closeProject()
{
if (!closeTimeline())
return false;
Project::close();
updateTitle();
return true;
}
void MainWindow::newTimeline()
{
if (Project::get() == nullptr || !closeTimeline())
return;
ui->timeline->setTimeline(std::make_shared<Timeline>());
mTimelinePath = "";
updateTitle();
}
void MainWindow::openTimeline()
{
if (Project::get() == nullptr)
return;
QString openPath = QFileDialog::getOpenFileName(this, "Open Timeline",
Project::get()->root() + "/timelines",
"Bugel Timeline (*.bgt)");
openTimeline(openPath);
}
void MainWindow::openTimeline(const QString& openPath)
{
if (Project::get() == nullptr)
return;
QFileInfo file(openPath);
if (!file.exists() || !closeTimeline())
return;
auto timeline = std::make_shared<Timeline>();
timeline->fromJSON(Util::readJSON(openPath));
ui->timeline->setTimeline(timeline);
Project::get()->setLastOpenedTimeline(file.filePath());
mTimelinePath = openPath;
statusBar()->showMessage(QString("Opened timeline %1.")
.arg(openPath));
updateTitle();
}
void MainWindow::openTimelineSettingsDialog()
{
if (!Project::get() || ui->timeline->timeline() == nullptr)
return;
TimelineSettingsDialog dlg;
dlg.init(*ui->timeline->timeline());
if (dlg.exec() == QDialog::Accepted) {
dlg.apply(*ui->timeline->timeline());
}
}
bool MainWindow::saveTimeline()
{
if (!Project::get() || ui->timeline->timeline() == nullptr)
return false;
if (mTimelinePath.isEmpty())
return saveTimelineAs();
Util::writeJSON(ui->timeline->timeline()->toJSON(), mTimelinePath);
Project::get()->setLastOpenedTimeline(mTimelinePath);
statusBar()->showMessage(QString("Saved timeline to %1.")
.arg(mTimelinePath));
updateTitle();
return true;
}
bool MainWindow::saveTimelineAs()
{
if (!Project::get() || ui->timeline->timeline() == nullptr)
return false;
QString path = QFileDialog::getSaveFileName(this, "Save Timeline As",
Project::get()->root() + "/timelines",
"Bugel Timeline (*.bgt)");
if (path.isEmpty())
return false;
mTimelinePath = path;
return saveTimeline();
}
void MainWindow::exportTimelineAs()
{
if (!Project::get() || ui->timeline->timeline() == nullptr)
return;
QString exportPath = QFileDialog::getSaveFileName(this, "Export Timeline As", "", "JSON (*.json)");
if (!exportPath.isEmpty()) {
std::shared_ptr<Timeline> exportTimeline = ui->timeline->timeline()->process();
Util::writeJSON(exportTimeline->toJSON(), exportPath);
statusBar()->showMessage(QString("Exported timeline to %1.").arg(exportPath));
}
}
bool MainWindow::closeTimeline()
{
// already closed?
if (ui->timeline->timeline() == nullptr)
return true;
// if timeline is dirty, prompt to save/discard/cancel
if (ui->timeline->dirty()) {
QMessageBox:: StandardButton res =
QMessageBox::question(this, "Save Timeline?",
"The open timeline has not been saved.",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
QMessageBox::Cancel);
// user clicked cancel on the question or cancelled during the save (ambiguous)
if (res == QMessageBox::Cancel || (res == QMessageBox::Save && !saveTimeline()))
return false;
}
// ok, we can close
ui->timeline->setTimeline(nullptr);
updateTitle();
return true;
}
void MainWindow::updateTitle()
{
QString proj = Project::get() ? Project::get()->name() : "";
QString tl = ui->timeline->timeline() ? QFileInfo(mTimelinePath).fileName() : "";
if (!proj.isEmpty() && !tl.isEmpty()) {
setWindowTitle(QString("%1 [%2]").arg(tl).arg(proj));
} else if (!proj.isEmpty()) {
setWindowTitle(QString("[%1]").arg(proj));
} else {
setWindowTitle("Bugel");
}
}