forked from olive-editor/olive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oliveglobal.cpp
377 lines (307 loc) · 10.9 KB
/
oliveglobal.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
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "oliveglobal.h"
#include "mainwindow.h"
#include "panels/panels.h"
#include "io/path.h"
#include "io/config.h"
#include "ui/mediaiconservice.h"
#include "rendering/audio.h"
#include "dialogs/demonotice.h"
#include "dialogs/preferencesdialog.h"
#include "dialogs/exportdialog.h"
#include "dialogs/debugdialog.h"
#include "dialogs/aboutdialog.h"
#include "dialogs/speeddialog.h"
#include "dialogs/actionsearch.h"
#include "project/sequence.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QAction>
#include <QApplication>
#include <QDebug>
std::unique_ptr<OliveGlobal> olive::Global;
QString olive::ActiveProjectFilename;
QString olive::AppName;
OliveGlobal::OliveGlobal() {
// sets current app name
QString version_id;
#ifdef GITHASH
version_id = QString(" | %1").arg(GITHASH);
#endif
olive::AppName = QString("Olive (February 2019 | Alpha%1)").arg(version_id);
// set the file filter used in all file dialogs pertaining to Olive project files.
project_file_filter = tr("Olive Project %1").arg("(*.ove)");
// set default value
enable_load_project_on_init = false;
// alloc QTranslator
translator = std::unique_ptr<QTranslator>(new QTranslator());
}
const QString &OliveGlobal::get_project_file_filter() {
return project_file_filter;
}
void OliveGlobal::update_project_filename(const QString &s) {
// set filename to s
olive::ActiveProjectFilename = s;
// update main window title to reflect new project filename
olive::MainWindow->updateTitle();
}
void OliveGlobal::check_for_autorecovery_file() {
QString data_dir = get_data_path();
if (!data_dir.isEmpty()) {
// detect auto-recovery file
autorecovery_filename = data_dir + "/autorecovery.ove";
if (QFile::exists(autorecovery_filename)) {
if (QMessageBox::question(nullptr, tr("Auto-recovery"), tr("Olive didn't close properly and an autorecovery file was detected. Would you like to open it?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
enable_load_project_on_init = false;
open_project_worker(autorecovery_filename, true);
}
}
autorecovery_timer.setInterval(60000);
QObject::connect(&autorecovery_timer, SIGNAL(timeout()), this, SLOT(save_autorecovery_file()));
autorecovery_timer.start();
}
}
void OliveGlobal::set_rendering_state(bool rendering) {
audio_rendering = rendering;
if (rendering) {
autorecovery_timer.stop();
} else {
autorecovery_timer.start();
}
}
void OliveGlobal::load_project_on_launch(const QString& s) {
olive::ActiveProjectFilename = s;
enable_load_project_on_init = true;
}
QString OliveGlobal::get_recent_project_list_file() {
return get_data_dir().filePath("recents");
}
void OliveGlobal::load_translation_from_config() {
QString language_file = olive::CurrentRuntimeConfig.external_translation_file.isEmpty() ?
olive::CurrentConfig.language_file :
olive::CurrentRuntimeConfig.external_translation_file;
// clear runtime language file so if the user sets a different language, we won't load it next time
olive::CurrentRuntimeConfig.external_translation_file.clear();
// remove current translation if there is one
QApplication::removeTranslator(translator.get());
if (!language_file.isEmpty()) {
// translation files are stored relative to app path (see GitHub issue #454)
QString full_language_path = QDir(get_app_path()).filePath(language_file);
// load translation file
if (QFileInfo::exists(full_language_path)
&& translator->load(full_language_path)) {
QApplication::installTranslator(translator.get());
} else {
qWarning() << "Failed to load translation file" << full_language_path << ". No language will be loaded.";
}
}
}
void OliveGlobal::new_project() {
if (can_close_project()) {
// clear effects panel
panel_effect_controls->clear_effects(true);
// clear project contents (footage, sequences, etc.)
panel_project->new_project();
// clear undo stack
olive::UndoStack.clear();
// empty current project filename
update_project_filename("");
// full update of all panels
update_ui(false);
}
}
void OliveGlobal::open_project() {
QString fn = QFileDialog::getOpenFileName(olive::MainWindow, tr("Open Project..."), "", project_file_filter);
if (!fn.isEmpty() && can_close_project()) {
open_project_worker(fn, false);
}
}
void OliveGlobal::open_recent(int index) {
QString recent_url = recent_projects.at(index);
if (!QFile::exists(recent_url)) {
if (QMessageBox::question(
olive::MainWindow,
tr("Missing recent project"),
tr("The project '%1' no longer exists. Would you like to remove it from the recent projects list?").arg(recent_url),
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
recent_projects.removeAt(index);
panel_project->save_recent_projects();
}
} else if (can_close_project()) {
open_project_worker(recent_url, false);
}
}
bool OliveGlobal::save_project_as() {
QString fn = QFileDialog::getSaveFileName(olive::MainWindow, tr("Save Project As..."), "", project_file_filter);
if (!fn.isEmpty()) {
if (!fn.endsWith(".ove", Qt::CaseInsensitive)) {
fn += ".ove";
}
update_project_filename(fn);
panel_project->save_project(false);
return true;
}
return false;
}
bool OliveGlobal::save_project() {
if (olive::ActiveProjectFilename.isEmpty()) {
return save_project_as();
} else {
panel_project->save_project(false);
return true;
}
}
bool OliveGlobal::can_close_project() {
if (olive::MainWindow->isWindowModified()) {
QMessageBox* m = new QMessageBox(
QMessageBox::Question,
tr("Unsaved Project"),
tr("This project has changed since it was last saved. Would you like to save it before closing?"),
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,
olive::MainWindow
);
m->setWindowModality(Qt::WindowModal);
int r = m->exec();
delete m;
if (r == QMessageBox::Yes) {
return save_project();
} else if (r == QMessageBox::Cancel) {
return false;
}
}
return true;
}
void OliveGlobal::open_export_dialog() {
if (olive::ActiveSequence == nullptr) {
QMessageBox::information(olive::MainWindow,
tr("No active sequence"),
tr("Please open the sequence you wish to export."),
QMessageBox::Ok);
} else {
ExportDialog e(olive::MainWindow);
e.exec();
}
}
void OliveGlobal::finished_initialize() {
if (enable_load_project_on_init) {
// if a project was set as a command line argument, we load it here
if (QFileInfo::exists(olive::ActiveProjectFilename)) {
open_project_worker(olive::ActiveProjectFilename, false);
} else {
QMessageBox::critical(olive::MainWindow,
tr("Missing Project File"),
tr("Specified project '%1' does not exist.").arg(olive::ActiveProjectFilename),
QMessageBox::Ok);
update_project_filename(nullptr);
}
enable_load_project_on_init = false;
} else {
// if we are not loading a project on launch and are running a release build, open the demo notice dialog
#ifndef QT_DEBUG
DemoNotice* d = new DemoNotice(olive::MainWindow);
connect(d, SIGNAL(finished(int)), d, SLOT(deleteLater()));
d->open();
#endif
}
}
void OliveGlobal::save_autorecovery_file() {
if (olive::MainWindow->isWindowModified()) {
panel_project->save_project(true);
qInfo() << "Auto-recovery project saved";
}
}
void OliveGlobal::open_preferences() {
panel_sequence_viewer->pause();
panel_footage_viewer->pause();
PreferencesDialog pd(olive::MainWindow);
pd.setup_kbd_shortcuts(olive::MainWindow->menuBar());
pd.exec();
}
void OliveGlobal::set_sequence(SequencePtr s)
{
panel_effect_controls->clear_effects(true);
olive::ActiveSequence = s;
panel_sequence_viewer->set_main_sequence();
panel_timeline->update_sequence();
panel_timeline->setFocus();
}
void OliveGlobal::open_project_worker(const QString& fn, bool autorecovery) {
update_project_filename(fn);
panel_project->load_project(autorecovery);
olive::UndoStack.clear();
}
QIcon OliveGlobal::CreateIconFromSVG(const QString &path)
{
QIcon icon;
QPixmap normal(path);
icon.addPixmap(normal, QIcon::Normal, QIcon::On);
QPixmap disabled(normal.size());
disabled.fill(Qt::transparent);
// draw semi-transparent version of icon for the disabled variant
QPainter p(&disabled);
p.setOpacity(0.5);
p.drawPixmap(0, 0, normal);
p.end();
icon.addPixmap(disabled, QIcon::Disabled, QIcon::On);
return icon;
}
void OliveGlobal::undo() {
// workaround to prevent crash (and also users should never need to do this)
if (!panel_timeline->importing) {
olive::UndoStack.undo();
update_ui(true);
}
}
void OliveGlobal::redo() {
// workaround to prevent crash (and also users should never need to do this)
if (!panel_timeline->importing) {
olive::UndoStack.redo();
update_ui(true);
}
}
void OliveGlobal::paste() {
if (olive::ActiveSequence != nullptr) {
panel_timeline->paste(false);
}
}
void OliveGlobal::paste_insert() {
if (olive::ActiveSequence != nullptr) {
panel_timeline->paste(true);
}
}
void OliveGlobal::open_about_dialog() {
AboutDialog a(olive::MainWindow);
a.exec();
}
void OliveGlobal::open_debug_log() {
olive::DebugDialog->show();
}
void OliveGlobal::open_speed_dialog() {
if (olive::ActiveSequence != nullptr) {
QVector<Clip*> selected_clips = olive::ActiveSequence->SelectedClips();
if (!selected_clips.isEmpty()) {
SpeedDialog s(olive::MainWindow, selected_clips);
s.exec();
}
}
}
void OliveGlobal::clear_undo_stack() {
olive::UndoStack.clear();
}
void OliveGlobal::open_action_search() {
ActionSearch as(olive::MainWindow);
as.exec();
}