-
-
Notifications
You must be signed in to change notification settings - Fork 572
/
Copy pathproject.cpp
1413 lines (1169 loc) · 48.8 KB
/
project.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
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 "project.h"
#include "oliveglobal.h"
#include "panels.h"
#include "rendering/renderfunctions.h"
#include "io/previewgenerator.h"
#include "project/undo.h"
#include "mainwindow.h"
#include "io/config.h"
#include "rendering/cacher.h"
#include "dialogs/replaceclipmediadialog.h"
#include "panels/effectcontrols.h"
#include "dialogs/newsequencedialog.h"
#include "dialogs/mediapropertiesdialog.h"
#include "dialogs/loaddialog.h"
#include "io/clipboard.h"
#include "ui/sourcetable.h"
#include "ui/sourceiconview.h"
#include "ui/icons.h"
#include "ui/menuhelper.h"
#include "ui/mediaiconservice.h"
#include "project/sourcescommon.h"
#include "project/projectfilter.h"
#include "debug.h"
#include <QApplication>
#include <QFileDialog>
#include <QString>
#include <QVariant>
#include <QCharRef>
#include <QMessageBox>
#include <QDropEvent>
#include <QMimeData>
#include <QPushButton>
#include <QInputDialog>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QSizePolicy>
#include <QVBoxLayout>
#include <QMenu>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}
// TODO make these configurable
const int kDefaultSequenceWidth = 1920;
const int kDefaultSequenceHeight = 1080;
const double kDefaultSequenceFrameRate = 29.97;
const int kDefaultSequenceFrequency = 48000;
const int kDefaultSequenceChannelLayout = 3;
#define MAXIMUM_RECENT_PROJECTS 10 // FIXME: should be configurable
QString autorecovery_filename;
QStringList recent_projects;
Project::Project(QWidget *parent) :
Panel(parent)
{
QWidget* dockWidgetContents = new QWidget(this);
QVBoxLayout* verticalLayout = new QVBoxLayout(dockWidgetContents);
verticalLayout->setMargin(0);
verticalLayout->setSpacing(0);
setWidget(dockWidgetContents);
sources_common = new SourcesCommon(this);
sorter = new ProjectFilter(this);
sorter->setSourceModel(&olive::project_model);
// optional toolbar
toolbar_widget = new QWidget();
toolbar_widget->setVisible(olive::CurrentConfig.show_project_toolbar);
toolbar_widget->setObjectName("project_toolbar");
QHBoxLayout* toolbar = new QHBoxLayout(toolbar_widget);
toolbar->setMargin(0);
toolbar->setSpacing(0);
QPushButton* toolbar_new = new QPushButton();
toolbar_new->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/add-button.svg")));
toolbar_new->setToolTip("New");
connect(toolbar_new, SIGNAL(clicked(bool)), this, SLOT(make_new_menu()));
toolbar->addWidget(toolbar_new);
QPushButton* toolbar_open = new QPushButton();
toolbar_open->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/open.svg")));
toolbar_open->setToolTip("Open Project");
connect(toolbar_open, SIGNAL(clicked(bool)), olive::Global.get(), SLOT(open_project()));
toolbar->addWidget(toolbar_open);
QPushButton* toolbar_save = new QPushButton();
toolbar_save->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/save.svg")));
toolbar_save->setToolTip("Save Project");
connect(toolbar_save, SIGNAL(clicked(bool)), olive::Global.get(), SLOT(save_project()));
toolbar->addWidget(toolbar_save);
QPushButton* toolbar_undo = new QPushButton();
toolbar_undo->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/undo.svg")));
toolbar_undo->setToolTip("Undo");
connect(toolbar_undo, SIGNAL(clicked(bool)), olive::Global.get(), SLOT(undo()));
toolbar->addWidget(toolbar_undo);
QPushButton* toolbar_redo = new QPushButton();
toolbar_redo->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/redo.svg")));
toolbar_redo->setToolTip("Redo");
connect(toolbar_redo, SIGNAL(clicked(bool)), olive::Global.get(), SLOT(redo()));
toolbar->addWidget(toolbar_redo);
toolbar_search = new QLineEdit();
toolbar_search->setClearButtonEnabled(true);
connect(toolbar_search, SIGNAL(textChanged(QString)), sorter, SLOT(update_search_filter(const QString&)));
toolbar->addWidget(toolbar_search);
QPushButton* toolbar_tree_view = new QPushButton();
toolbar_tree_view->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/treeview.svg")));
toolbar_tree_view->setToolTip("Tree View");
connect(toolbar_tree_view, SIGNAL(clicked(bool)), this, SLOT(set_tree_view()));
toolbar->addWidget(toolbar_tree_view);
QPushButton* toolbar_icon_view = new QPushButton();
toolbar_icon_view->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/iconview.svg")));
toolbar_icon_view->setToolTip("Icon View");
connect(toolbar_icon_view, SIGNAL(clicked(bool)), this, SLOT(set_icon_view()));
toolbar->addWidget(toolbar_icon_view);
verticalLayout->addWidget(toolbar_widget);
// tree view
tree_view = new SourceTable();
tree_view->project_parent = this;
tree_view->setModel(sorter);
verticalLayout->addWidget(tree_view);
// Set the first column width
// I'm not sure if there's a better way to do this, default behavior seems to have all columns fixed width
// and let the last column fill up the remainder when really the opposite would be preferable (having the
// first column fill up the majority of the space). Anyway, this will probably do for now.
tree_view->setColumnWidth(0, tree_view->width()/2);
// icon view
icon_view_container = new QWidget();
QVBoxLayout* icon_view_container_layout = new QVBoxLayout(icon_view_container);
icon_view_container_layout->setMargin(0);
icon_view_container_layout->setSpacing(0);
QHBoxLayout* icon_view_controls = new QHBoxLayout();
icon_view_controls->setMargin(0);
icon_view_controls->setSpacing(0);
directory_up = new QPushButton();
directory_up->setIcon(olive::icon::CreateIconFromSVG(QStringLiteral(":/icons/dirup.svg")));
directory_up->setEnabled(false);
icon_view_controls->addWidget(directory_up);
icon_view_controls->addStretch();
QSlider* icon_size_slider = new QSlider(Qt::Horizontal);
icon_size_slider->setMinimum(16);
icon_size_slider->setMaximum(120);
icon_view_controls->addWidget(icon_size_slider);
connect(icon_size_slider, SIGNAL(valueChanged(int)), this, SLOT(set_icon_view_size(int)));
icon_view_container_layout->addLayout(icon_view_controls);
icon_view = new SourceIconView();
icon_view->project_parent = this;
icon_view->setModel(sorter);
icon_view->setIconSize(QSize(100, 100));
icon_view->setViewMode(QListView::IconMode);
icon_view->setUniformItemSizes(true);
icon_view_container_layout->addWidget(icon_view);
icon_size_slider->setValue(icon_view->iconSize().height());
verticalLayout->addWidget(icon_view_container);
connect(directory_up, SIGNAL(clicked(bool)), this, SLOT(go_up_dir()));
connect(icon_view, SIGNAL(changed_root()), this, SLOT(set_up_dir_enabled()));
connect(olive::media_icon_service.get(), SIGNAL(IconChanged()), icon_view->viewport(), SLOT(update()));
connect(olive::media_icon_service.get(), SIGNAL(IconChanged()), tree_view->viewport(), SLOT(update()));
update_view_type();
Retranslate();
}
Project::~Project() {
delete sorter;
}
void Project::Retranslate() {
toolbar_search->setPlaceholderText(tr("Search media, markers, etc."));
setWindowTitle(tr("Project"));
}
QString Project::get_next_sequence_name(QString start) {
if (start.isEmpty()) start = tr("Sequence");
int n = 1;
bool found = true;
QString name;
while (found) {
found = false;
name = start + " ";
if (n < 10) {
name += "0";
}
name += QString::number(n);
for (int i=0;i<olive::project_model.childCount();i++) {
if (QString::compare(olive::project_model.child(i)->get_name(), name, Qt::CaseInsensitive) == 0) {
found = true;
n++;
break;
}
}
}
return name;
}
SequencePtr create_sequence_from_media(QVector<Media*>& media_list) {
SequencePtr s(new Sequence());
s->name = panel_project->get_next_sequence_name();
// shitty hardcoded default values
s->width = kDefaultSequenceWidth;
s->height = kDefaultSequenceHeight;
s->frame_rate = kDefaultSequenceFrameRate;
s->audio_frequency = kDefaultSequenceFrequency;
s->audio_layout = kDefaultSequenceChannelLayout;
bool got_video_values = false;
bool got_audio_values = false;
for (int i=0;i<media_list.size();i++) {
Media* media = media_list.at(i);
switch (media->get_type()) {
case MEDIA_TYPE_FOOTAGE:
{
FootagePtr m = media->to_footage();
if (m->ready) {
if (!got_video_values) {
for (int j=0;j<m->video_tracks.size();j++) {
const FootageStream& ms = m->video_tracks.at(j);
s->width = ms.video_width;
s->height = ms.video_height;
if (!qFuzzyCompare(ms.video_frame_rate, 0.0)) {
s->frame_rate = ms.video_frame_rate * m->speed;
if (ms.video_interlacing != VIDEO_PROGRESSIVE) s->frame_rate *= 2;
// only break with a decent frame rate, otherwise there may be a better candidate
got_video_values = true;
break;
}
}
}
if (!got_audio_values && m->audio_tracks.size() > 0) {
const FootageStream& ms = m->audio_tracks.at(0);
s->audio_frequency = ms.audio_frequency;
got_audio_values = true;
}
}
}
break;
case MEDIA_TYPE_SEQUENCE:
{
SequencePtr seq = media->to_sequence();
s->width = seq->width;
s->height = seq->height;
s->frame_rate = seq->frame_rate;
s->audio_frequency = seq->audio_frequency;
s->audio_layout = seq->audio_layout;
got_video_values = true;
got_audio_values = true;
}
break;
}
if (got_video_values && got_audio_values) break;
}
return s;
}
void Project::duplicate_selected() {
QModelIndexList items = get_current_selected();
bool duped = false;
ComboAction* ca = new ComboAction();
for (int j=0;j<items.size();j++) {
Media* i = item_to_media(items.at(j));
if (i->get_type() == MEDIA_TYPE_SEQUENCE) {
create_sequence_internal(ca, i->to_sequence()->copy(), false, item_to_media(items.at(j).parent()));
duped = true;
}
}
if (duped) {
olive::UndoStack.push(ca);
} else {
delete ca;
}
}
void Project::replace_selected_file() {
QModelIndexList selected_items = get_current_selected();
if (selected_items.size() == 1) {
Media* item = item_to_media(selected_items.at(0));
if (item->get_type() == MEDIA_TYPE_FOOTAGE) {
replace_media(item, nullptr);
}
}
}
void Project::replace_media(Media* item, QString filename) {
if (filename.isEmpty()) {
filename = QFileDialog::getOpenFileName(
this,
tr("Replace '%1'").arg(item->get_name()),
"",
tr("All Files") + " (*)");
}
if (!filename.isEmpty()) {
ReplaceMediaCommand* rmc = new ReplaceMediaCommand(item, filename);
olive::UndoStack.push(rmc);
}
}
void Project::replace_clip_media() {
if (olive::ActiveSequence == nullptr) {
QMessageBox::critical(this,
tr("No active sequence"),
tr("No sequence is active, please open the sequence you want to replace clips from."),
QMessageBox::Ok);
} else {
QModelIndexList selected_items = get_current_selected();
if (selected_items.size() == 1) {
Media* item = item_to_media(selected_items.at(0));
if (item->get_type() == MEDIA_TYPE_SEQUENCE && olive::ActiveSequence == item->to_sequence()) {
QMessageBox::critical(this,
tr("Active sequence selected"),
tr("You cannot insert a sequence into itself, so no clips of this media would be in this sequence."),
QMessageBox::Ok);
} else {
ReplaceClipMediaDialog dialog(this, item);
dialog.exec();
}
}
}
}
void Project::open_properties() {
QModelIndexList selected_items = get_current_selected();
if (selected_items.size() == 1) {
Media* item = item_to_media(selected_items.at(0));
switch (item->get_type()) {
case MEDIA_TYPE_FOOTAGE:
{
MediaPropertiesDialog mpd(this, item);
mpd.exec();
}
break;
case MEDIA_TYPE_SEQUENCE:
{
NewSequenceDialog nsd(this, item);
nsd.exec();
}
break;
default:
{
// fall back to renaming
QString new_name = QInputDialog::getText(this,
tr("Rename '%1'").arg(item->get_name()),
tr("Enter new name:"),
QLineEdit::Normal,
item->get_name());
if (!new_name.isEmpty()) {
MediaRename* mr = new MediaRename(item, new_name);
olive::UndoStack.push(mr);
}
}
}
}
}
void Project::new_folder() {
Media* m = create_folder_internal(nullptr);
olive::UndoStack.push(new AddMediaCommand(m, get_selected_folder()));
QModelIndex index = olive::project_model.create_index(m->row(), 0, m);
switch (olive::CurrentConfig.project_view_type) {
case olive::PROJECT_VIEW_TREE:
tree_view->edit(sorter->mapFromSource(index));
break;
case olive::PROJECT_VIEW_ICON:
icon_view->edit(sorter->mapFromSource(index));
break;
}
}
void Project::new_sequence() {
NewSequenceDialog nsd(this);
nsd.set_sequence_name(get_next_sequence_name());
nsd.exec();
}
Media* Project::create_sequence_internal(ComboAction *ca, SequencePtr s, bool open, Media* parent) {
if (parent == nullptr) {
parent = olive::project_model.get_root();
}
Media* item = new Media(parent);
item->set_sequence(s);
if (ca != nullptr) {
ca->append(new NewSequenceCommand(item, parent));
if (open) ca->append(new ChangeSequenceAction(s));
} else {
olive::project_model.appendChild(parent, item);
if (open) {
olive::Global->set_sequence(s);
}
}
return item;
}
QString Project::get_file_name_from_path(const QString& path) {
return path.mid(path.lastIndexOf('/')+1);
}
/*Media* Project::new_item() {
Media* item = new Media(0);
//item->setFlags(item->flags() | Qt::ItemIsEditable);
return item;
}*/
bool Project::is_focused() {
return tree_view->hasFocus() || icon_view->hasFocus();
}
Media* Project::create_folder_internal(QString name) {
Media* item = new Media(nullptr);
item->set_folder();
item->set_name(name);
return item;
}
Media *Project::item_to_media(const QModelIndex &index) {
return static_cast<Media*>(sorter->mapToSource(index).internalPointer());
// return static_cast<Media*>(index.internalPointer());
}
void Project::get_all_media_from_table(QList<Media*>& items, QList<Media*>& list, int search_type) {
for (int i=0;i<items.size();i++) {
Media* item = items.at(i);
if (item->get_type() == MEDIA_TYPE_FOLDER) {
QList<Media*> children;
for (int j=0;j<item->childCount();j++) {
children.append(item->child(j));
}
get_all_media_from_table(children, list, search_type);
} else if (search_type == item->get_type() || search_type == -1) {
list.append(item);
}
}
}
bool delete_clips_in_clipboard_with_media(ComboAction* ca, Media* m) {
int delete_count = 0;
if (clipboard_type == CLIPBOARD_TYPE_CLIP) {
for (int i=0;i<clipboard.size();i++) {
ClipPtr c = std::static_pointer_cast<Clip>(clipboard.at(i));
if (c->media() == m) {
ca->append(new RemoveClipsFromClipboard(i-delete_count));
delete_count++;
}
}
}
return (delete_count > 0);
}
void Project::delete_selected_media() {
ComboAction* ca = new ComboAction();
QModelIndexList selected_items = get_current_selected();
QList<Media*> items;
for (int i=0;i<selected_items.size();i++) {
items.append(item_to_media(selected_items.at(i)));
}
bool remove = true;
bool redraw = false;
// check if media is in use
QVector<Media*> parents;
QList<Media*> sequence_items;
QList<Media*> all_top_level_items;
for (int i=0;i<olive::project_model.childCount();i++) {
all_top_level_items.append(olive::project_model.child(i));
}
get_all_media_from_table(all_top_level_items, sequence_items, MEDIA_TYPE_SEQUENCE); // find all sequences in project
if (sequence_items.size() > 0) {
QList<Media*> media_items;
get_all_media_from_table(items, media_items, MEDIA_TYPE_FOOTAGE);
for (int i=0;i<media_items.size();i++) {
Media* item = media_items.at(i);
FootagePtr media = item->to_footage();
bool confirm_delete = false;
for (int j=0;j<sequence_items.size();j++) {
SequencePtr s = sequence_items.at(j)->to_sequence();
for (int k=0;k<s->clips.size();k++) {
ClipPtr c = s->clips.at(k);
if (c != nullptr && c->media() == item) {
if (!confirm_delete) {
// we found a reference, so we know we'll need to ask if the user wants to delete it
QMessageBox confirm(this);
confirm.setWindowTitle(tr("Delete media in use?"));
confirm.setText(tr("The media '%1' is currently used in '%2'. Deleting it will remove all instances in the sequence. Are you sure you want to do this?").arg(media->name, s->name));
QAbstractButton* yes_button = confirm.addButton(QMessageBox::Yes);
QAbstractButton* skip_button = nullptr;
if (items.size() > 1) skip_button = confirm.addButton(tr("Skip"), QMessageBox::NoRole);
QAbstractButton* abort_button = confirm.addButton(QMessageBox::Cancel);
confirm.exec();
if (confirm.clickedButton() == yes_button) {
// remove all clips referencing this media
confirm_delete = true;
redraw = true;
} else if (confirm.clickedButton() == skip_button) {
// remove media item and any folders containing it from the remove list
Media* parent = item;
while (parent != nullptr) {
parents.append(parent);
// re-add item's siblings
for (int m=0;m<parent->childCount();m++) {
Media* child = parent->child(m);
bool found = false;
for (int n=0;n<items.size();n++) {
if (items.at(n) == child) {
found = true;
break;
}
}
if (!found) {
items.append(child);
}
}
parent = parent->parentItem();
}
j = sequence_items.size();
k = s->clips.size();
} else if (confirm.clickedButton() == abort_button) {
// break out of loop
i = media_items.size();
j = sequence_items.size();
k = s->clips.size();
remove = false;
}
}
if (confirm_delete) {
ca->append(new DeleteClipAction(s, k));
}
}
}
}
if (confirm_delete) {
delete_clips_in_clipboard_with_media(ca, item);
}
}
}
// remove
if (remove) {
panel_effect_controls->clear_effects(true);
if (olive::ActiveSequence != nullptr) olive::ActiveSequence->selections.clear();
// remove media and parents
for (int m=0;m<parents.size();m++) {
for (int l=0;l<items.size();l++) {
if (items.at(l) == parents.at(m)) {
items.removeAt(l);
l--;
}
}
}
for (int i=0;i<items.size();i++) {
ca->append(new DeleteMediaCommand(items.at(i)));
if (items.at(i)->get_type() == MEDIA_TYPE_SEQUENCE) {
redraw = true;
SequencePtr s = items.at(i)->to_sequence();
if (s == olive::ActiveSequence) {
ca->append(new ChangeSequenceAction(nullptr));
}
if (s == panel_footage_viewer->seq) {
panel_footage_viewer->set_media(nullptr);
}
} else if (items.at(i)->get_type() == MEDIA_TYPE_FOOTAGE) {
if (panel_footage_viewer->seq != nullptr) {
for (int j=0;j<panel_footage_viewer->seq->clips.size();j++) {
ClipPtr c = panel_footage_viewer->seq->clips.at(j);
if (c != nullptr && c->media() == items.at(i)) {
panel_footage_viewer->set_media(nullptr);
break;
}
}
}
}
}
olive::UndoStack.push(ca);
// redraw clips
if (redraw) {
update_ui(true);
}
} else {
delete ca;
}
}
void Project::process_file_list(QStringList& files, bool recursive, Media* replace, Media* parent) {
bool imported = false;
// retrieve the array of image formats from the user's configuration
QStringList image_sequence_formats = olive::CurrentConfig.img_seq_formats.split("|");
// a cache of image sequence formatted URLS to assist the user in importing image sequences
QVector<QString> image_sequence_urls;
QVector<bool> image_sequence_importassequence;
if (!recursive) last_imported_media.clear();
bool create_undo_action = (!recursive && replace == nullptr);
ComboAction* ca = nullptr;
if (create_undo_action) ca = new ComboAction();
// Loop through received files
for (int i=0;i<files.size();i++) {
// If this file is a directory, we'll recursively call this function again to process the directory's contents
if (QFileInfo(files.at(i)).isDir()) {
QString folder_name = get_file_name_from_path(files.at(i));
Media* folder = create_folder_internal(folder_name);
QDir directory(files.at(i));
directory.setFilter(QDir::NoDotAndDotDot | QDir::AllEntries);
QFileInfoList subdir_files = directory.entryInfoList();
QStringList subdir_filenames;
for (int j=0;j<subdir_files.size();j++) {
subdir_filenames.append(subdir_files.at(j).filePath());
}
process_file_list(subdir_filenames, true, nullptr, folder);
if (create_undo_action) {
ca->append(new AddMediaCommand(folder, parent));
} else {
olive::project_model.appendChild(parent, folder);
}
imported = true;
} else if (!files.at(i).isEmpty()) {
QString file = files.at(i);
// Check if the user is importing an Olive project file
if (file.endsWith(".ove", Qt::CaseInsensitive)) {
// This file is an Olive project file. Ask the user if they really want to import it.
if (QMessageBox::question(this,
tr("Import a Project"),
tr("\"%1\" is an Olive project file. It will merge with this project. "
"Do you wish to continue?").arg(file),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
// load the project without clearing the current one
load_project(file, false, false);
}
} else {
// This file is NOT an Olive project file
// Used later if this file is part of an already processed image sequence
bool skip = false;
/* Heuristic to determine whether file is part of an image sequence */
// Firstly, we run a heuristic on whether this file is an image by checking its file extension
bool file_is_an_image = false;
// Get the string position of the extension in the filename
int lastcharindex = file.lastIndexOf(".");
if (lastcharindex != -1 && lastcharindex > file.lastIndexOf('/')) {
QString ext = file.mid(lastcharindex+1);
// If the file extension is part of a predetermined list (from Config::img_seq_formats), we'll treat it
// as an image
if (image_sequence_formats.contains(ext, Qt::CaseInsensitive)) {
file_is_an_image = true;
}
} else {
// If we're here, the file has no extension, but we'll still check if its an image sequence just in case
lastcharindex = file.length();
file_is_an_image = true;
}
// Some image sequence's don't start at "0", if it is indeed an image sequence, we'll use this variable
// later to determine where it does start
int start_number = 0;
// Check if we passed the earlier heuristic to check whether this is a file, and whether the last number in
// the filename (before the extension) is a number
if (file_is_an_image && file[lastcharindex-1].isDigit()) {
// Check how many digits are at the end of this filename
int digit_count = 0;
int digit_test = lastcharindex-1;
while (file[digit_test].isDigit()) {
digit_count++;
digit_test--;
}
// Retrieve the integer represented at the end of this filename
digit_test++;
int file_number = file.mid(digit_test, digit_count).toInt();
// Check whether a file exists with the same format but one number higher or one number lower
if (QFileInfo::exists(QString(file.left(digit_test) + QString("%1").arg(file_number-1, digit_count, 10, QChar('0')) + file.mid(lastcharindex)))
|| QFileInfo::exists(QString(file.left(digit_test) + QString("%1").arg(file_number+1, digit_count, 10, QChar('0')) + file.mid(lastcharindex)))) {
//
// If so, it certainly looks like it *could* be an image sequence, but we'll ask the user just in case
//
// Firstly we should check if this file is part of a sequence the user has already confirmed as either a
// sequence or not a sequence (e.g. if the user happened to select a bunch of images that happen to increase
// consecutively). We format the filename with FFmpeg's '%Nd' (N = digits) formatting for reading image
// sequences
QString new_filename = file.left(digit_test) + "%" + QString::number(digit_count) + "d" + file.mid(lastcharindex);
int does_url_cache_already_contain_this = image_sequence_urls.indexOf(new_filename);
if (does_url_cache_already_contain_this > -1) {
// We've already processed an image with the same formatting
// Check if the last time we saw this formatting, the user chose to import as a sequence
if (image_sequence_importassequence.at(does_url_cache_already_contain_this)) {
// If so, no need to import this file too, so we signal to the rest of the function to skip this file
skip = true;
}
// If not, we can fall-through to the next step which is importing normally
} else {
// If we're here, we've never seen a file with this formatting before, so we'll ask whether to import
// as a sequence or not
// Add this file formatting file to the URL cache
image_sequence_urls.append(new_filename);
// This does look like an image sequence, let's ask the user if it'll indeed be an image sequence
if (QMessageBox::question(this,
tr("Image sequence detected"),
tr("The file '%1' appears to be part of an image sequence. "
"Would you like to import it as such?").arg(file),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes) == QMessageBox::Yes) {
// Proceed to the next step of this with the formatted filename
file = new_filename;
// Cache the user's answer alongside the image_sequence_urls value - in this case, YES, this will be an
// image sequence
image_sequence_importassequence.append(true);
// FFmpeg needs to know what file number to start at in the sequence. In case the image sequence doesn't
// start at a zero, we'll loop decreasing the number until it doesn't exist anymore
QString test_filename_format = QString("%1%2%3").arg(file.left(digit_test), "%1", file.mid(lastcharindex));
int test_file_number = file_number;
do {
test_file_number--;
} while (QFileInfo::exists(test_filename_format.arg(QString("%1").arg(test_file_number, digit_count, 10, QChar('0')))));
// set the image sequence's start number to the last that existed
start_number = test_file_number + 1;
} else {
// Cache the user's response to the image sequence question - i.e. none of the files imported with this
// formatting should be imported as an image sequence
image_sequence_importassequence.append(false);
}
}
}
}
// If we're not skipping this file, let's import it
if (!skip) {
Media* item;
FootagePtr m;
if (replace != nullptr) {
item = replace;
} else {
item = new Media(parent);
}
m = FootagePtr(new Footage());
m->using_inout = false;
m->url = file;
m->name = get_file_name_from_path(files.at(i));
m->start_number = start_number;
item->set_footage(m);
last_imported_media.append(item);
if (replace == nullptr) {
if (create_undo_action) {
ca->append(new AddMediaCommand(item, parent));
} else {
parent->appendChild(item);
}
}
imported = true;
}
}
}
}
if (create_undo_action) {
if (imported) {
olive::UndoStack.push(ca);
for (int i=0;i<last_imported_media.size();i++) {
// generate waveform/thumbnail in another thread
PreviewGenerator::AnalyzeMedia(last_imported_media.at(i));
}
} else {
delete ca;
}
}
}
Media* Project::get_selected_folder() {
// if one item is selected and it's a folder, return it
QModelIndexList selected_items = get_current_selected();
if (selected_items.size() == 1) {
Media* m = item_to_media(selected_items.at(0));
if (m->get_type() == MEDIA_TYPE_FOLDER) return m;
}
return nullptr;
}
bool Project::reveal_media(Media *media, QModelIndex parent) {
for (int i=0;i<olive::project_model.rowCount(parent);i++) {
const QModelIndex& item = olive::project_model.index(i, 0, parent);
Media* m = olive::project_model.getItem(item);
if (m->get_type() == MEDIA_TYPE_FOLDER) {
// if this item is a folder, recursively run this function to search it too
if (reveal_media(media, item)) return true;
} else if (m == media) {
// if m == media, then we found the media object we were looking for
// get sorter proxy item (the item that's "visible")
QModelIndex sorted_index = sorter->mapFromSource(item);
// retrieve its parent item
QModelIndex hierarchy = sorted_index.parent();
if (olive::CurrentConfig.project_view_type == olive::PROJECT_VIEW_TREE) {
// if we're in tree view, expand every folder in the hierarchy containing the media
while (hierarchy.isValid()) {
tree_view->setExpanded(hierarchy, true);
hierarchy = hierarchy.parent();
}
// select item (requires a QItemSelection object to select the whole row)
QItemSelection row_select(
sorter->index(sorted_index.row(), 0, sorted_index.parent()),
sorter->index(sorted_index.row(), sorter->columnCount()-1, sorted_index.parent())
);
tree_view->selectionModel()->select(row_select, QItemSelectionModel::Select);
} else if (olive::CurrentConfig.project_view_type == olive::PROJECT_VIEW_ICON) {
// if we're in icon view, we just "browse" to the parent folder
icon_view->setRootIndex(hierarchy);
// select item in this folder
icon_view->selectionModel()->select(sorted_index, QItemSelectionModel::Select);
// update the "up" button state
set_up_dir_enabled();
}
return true;
}
}
return false;
}
void Project::import_dialog() {
QFileDialog fd(this, tr("Import media..."), "", tr("All Files") + " (*)");
fd.setFileMode(QFileDialog::ExistingFiles);
if (fd.exec()) {
QStringList files = fd.selectedFiles();
process_file_list(files, false, nullptr, get_selected_folder());
}
}
void Project::delete_clips_using_selected_media() {
if (olive::ActiveSequence == nullptr) {
QMessageBox::critical(this,
tr("No active sequence"),
tr("No sequence is active, please open the sequence you want to delete clips from."),
QMessageBox::Ok);
} else {
ComboAction* ca = new ComboAction();
bool deleted = false;
QModelIndexList items = get_current_selected();
for (int i=0;i<olive::ActiveSequence->clips.size();i++) {
const ClipPtr& c = olive::ActiveSequence->clips.at(i);
if (c != nullptr) {
for (int j=0;j<items.size();j++) {
Media* m = item_to_media(items.at(j));
if (c->media() == m) {
ca->append(new DeleteClipAction(olive::ActiveSequence, i));
deleted = true;
}
}
}
}
for (int j=0;j<items.size();j++) {
Media* m = item_to_media(items.at(j));
if (delete_clips_in_clipboard_with_media(ca, m)) deleted = true;
}