File: sourcetable.cpp

package info (click to toggle)
olive-editor 20181223-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,844 kB
  • sloc: cpp: 20,147; xml: 315; ansic: 16; makefile: 11
file content (309 lines) | stat: -rw-r--r-- 10,915 bytes parent folder | download
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
#include "sourcetable.h"
#include "panels/project.h"

#include "project/footage.h"
#include "panels/timeline.h"
#include "panels/viewer.h"
#include "panels/panels.h"
#include "playback/playback.h"
#include "project/undo.h"
#include "project/sequence.h"
#include "mainwindow.h"
#include "io/config.h"
#include "project/media.h"
#include "debug.h"

#include <QDragEnterEvent>
#include <QMimeData>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QFileInfo>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QProcess>

SourceTable::SourceTable(QWidget* parent) : QTreeView(parent) {
    editing_item = NULL;
	setSortingEnabled(true);
    sortByColumn(0, Qt::AscendingOrder);
    rename_timer.setInterval(1000);
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(&rename_timer, SIGNAL(timeout()), this, SLOT(rename_interval()));
    connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(item_click(const QModelIndex&)));
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(show_context_menu()));
}

void SourceTable::show_context_menu() {
    QMenu menu(this);

    QAction* import_action = menu.addAction("Import...");
    connect(import_action, SIGNAL(triggered(bool)), project_parent, SLOT(import_dialog()));

    QAction* new_folder_action = menu.addAction("New Folder...");
    connect(new_folder_action, SIGNAL(triggered(bool)), mainWindow, SLOT(on_actionFolder_triggered()));

    QModelIndexList selected_items = selectionModel()->selectedRows();

    if (selected_items.size() > 0) {
        Media* m = project_parent->item_to_media(selected_items.at(0));

        if (selected_items.size() == 1) {
            // replace footage
            int type = m->get_type();
			if (type == MEDIA_TYPE_FOOTAGE) {
				QAction* replace_action = menu.addAction("Replace/Relink Media");
                connect(replace_action, SIGNAL(triggered(bool)), project_parent, SLOT(replace_selected_file()));

#if defined(Q_OS_WIN)
				QAction* reveal_in_explorer = menu.addAction("Reveal in Explorer");
#elif defined(Q_OS_MAC)
				QAction* reveal_in_explorer = menu.addAction("Reveal in Finder");
#else
				QAction* reveal_in_explorer = menu.addAction("Reveal in File Manager");
#endif
				connect(reveal_in_explorer, SIGNAL(triggered(bool)), this, SLOT(reveal_in_browser()));
            }
			if (type != MEDIA_TYPE_FOLDER) {
				QAction* replace_clip_media = menu.addAction("Replace Clips Using This Media");
                connect(replace_clip_media, SIGNAL(triggered(bool)), project_parent, SLOT(replace_clip_media()));
			}
        }

        // duplicate item
        bool all_sequences = true;
		bool all_footage = true;
        for (int i=0;i<selected_items.size();i++) {
            if (m->get_type() != MEDIA_TYPE_SEQUENCE) {
				all_sequences = false;
            }
            if (m->get_type() != MEDIA_TYPE_FOOTAGE) {
				all_footage = false;
			}
        }

		// create sequence from
		QAction* create_seq_from = menu.addAction("Create Sequence With This Media");
		connect(create_seq_from, SIGNAL(triggered(bool)), this, SLOT(create_seq_from_selected()));

		// ONLY sequences are selected
        if (all_sequences) {
			// ONLY sequences are selected
            QAction* duplicate_action = menu.addAction("Duplicate");
            connect(duplicate_action, SIGNAL(triggered(bool)), project_parent, SLOT(duplicate_selected()));
		}

		// ONLY footage is selected
		if (all_footage) {
			QAction* delete_footage_from_sequences = menu.addAction("Delete All Clips Using This Media");
            connect(delete_footage_from_sequences, SIGNAL(triggered(bool)), project_parent, SLOT(delete_clips_using_selected_media()));
		}

        // delete media
        QAction* delete_action = menu.addAction("Delete");
        connect(delete_action, SIGNAL(triggered(bool)), project_parent, SLOT(delete_selected_media()));

        if (selected_items.size() == 1) {
			QAction* properties_action = menu.addAction("Properties...");
            connect(properties_action, SIGNAL(triggered(bool)), project_parent, SLOT(open_properties()));
		}
    }

	menu.exec(QCursor::pos());
}

void SourceTable::create_seq_from_selected() {
    QModelIndexList selected_items = selectionModel()->selectedRows();

    if (!selected_items.isEmpty()) {
        QVector<Media*> media_list;
        for (int i=0;i<selected_items.size();i++) {
            media_list.append(project_parent->item_to_media(selected_items.at(i)));
		}

		ComboAction* ca = new ComboAction();
        Sequence* s = create_sequence_from_media(media_list);

		// add clips to it
        panel_timeline->create_ghosts_from_media(s, 0, media_list);
		panel_timeline->add_clips_from_ghosts(ca, s);

        project_parent->new_sequence(ca, s, true, NULL);
		undo_stack.push(ca);
	}
}

void SourceTable::reveal_in_browser() {
    QModelIndexList selected_items = selectionModel()->selectedRows();
    Media* media = project_parent->item_to_media(selected_items.at(0));
    Footage* m = media->to_footage();

#if defined(Q_OS_WIN)
	QStringList args;
	args << "/select," << QDir::toNativeSeparators(m->url);
	QProcess::startDetached("explorer", args);
#elif defined(Q_OS_MAC)
	QStringList args;
	args << "-e";
	args << "tell application \"Finder\"";
	args << "-e";
	args << "activate";
	args << "-e";
	args << "select POSIX file \""+m->url+"\"";
	args << "-e";
	args << "end tell";
	QProcess::startDetached("osascript", args);
#else
	QDesktopServices::openUrl(QUrl::fromLocalFile(m->url.left(m->url.lastIndexOf('/'))));
#endif
}

void SourceTable::item_renamed(Media* item) {
    if (editing_item == item) {
        MediaRename* mr = new MediaRename(item, "idk");
        undo_stack.push(mr);
        editing_item = NULL;
    }
}

void SourceTable::stop_rename_timer() {
    rename_timer.stop();
}

void SourceTable::rename_interval() {
    stop_rename_timer();
    if (hasFocus() && editing_item != NULL) {
        edit(editing_index);
        //editItem(editing_item, 0);
    }
}
void SourceTable::item_click(const QModelIndex& index) {
    if (selectionModel()->selectedRows().size() == 1 && index.column() == 0) {
        Media* m = project_parent->item_to_media(index);
        if (editing_item == m) {
            rename_timer.start();
        } else {
            editing_item = m;
            editing_index = index;
        }
    }
}

void SourceTable::mousePressEvent(QMouseEvent* event) {
    stop_rename_timer();
    QTreeView::mousePressEvent(event);
}

void SourceTable::mouseDoubleClickEvent(QMouseEvent* e) {
    stop_rename_timer();
    QModelIndexList selected_items = selectionModel()->selectedRows();
    if (selected_items.size() == 0) {
        project_parent->import_dialog();
    } else if (selected_items.size() == 1) {
        Media* item = project_parent->item_to_media(selected_items.at(0));
        switch (item->get_type()) {
		case MEDIA_TYPE_FOOTAGE:
            panel_footage_viewer->set_media(item);
            panel_footage_viewer->setFocus();
			break;
		case MEDIA_TYPE_SEQUENCE:
            undo_stack.push(new ChangeSequenceAction(item->to_sequence()));
			break;
		}
    }
}

void SourceTable::dragEnterEvent(QDragEnterEvent *event) {
	if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    } else {
        QTreeView::dragEnterEvent(event);
    }
}

void SourceTable::dragMoveEvent(QDragMoveEvent *event) {
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    } else {
        QTreeView::dragMoveEvent(event);
    }
}

void SourceTable::dropEvent(QDropEvent* event) {
    const QMimeData* mimeData = event->mimeData();
    const QModelIndex& drop_item = indexAt(event->pos());
    Media* m = project_parent->item_to_media(drop_item);
    if (mimeData->hasUrls()) {
        // drag files in from outside
        QList<QUrl> urls = mimeData->urls();
        if (!urls.isEmpty()) {			
            QStringList paths;
			for (int i=0;i<urls.size();i++) {
                paths.append(urls.at(i).toLocalFile());
            }
			bool replace = false;
			if (urls.size() == 1
                    && drop_item.isValid()
                    && m->get_type() == MEDIA_TYPE_FOOTAGE
					&& !QFileInfo(paths.at(0)).isDir()
                    && config.drop_on_media_to_replace
                    && QMessageBox::question(this, "Replace Media", "You dropped a file onto '" + m->get_name() + "'. Would you like to replace it with the dropped file?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
				replace = true;
                project_parent->replace_media(m, paths.at(0));
			}
			if (!replace) {
                QModelIndex parent;
                if (drop_item.isValid()) {
                    if (m->get_type() == MEDIA_TYPE_FOLDER) {
                        parent = drop_item;
					} else {
                        parent = drop_item.parent();
					}
				}
                if (parent.isValid()) setExpanded(parent, true);
                project_parent->process_file_list(paths, false, NULL, panel_project->item_to_media(parent));
			}
        }
        event->acceptProposedAction();
    } else {
		event->ignore();

        // dragging files within project
        // if we dragged to the root OR dragged to a folder
        if (!drop_item.isValid() || (drop_item.isValid() && m->get_type() == MEDIA_TYPE_FOLDER)) {
            QVector<Media*> move_items;
            QModelIndexList selected_items = selectionModel()->selectedRows();
            for (int i=0;i<selected_items.size();i++) {
                const QModelIndex& item = selected_items.at(i);
                const QModelIndex& parent = item.parent();
                Media* s = project_parent->item_to_media(item);
                if (parent != drop_item && item != drop_item) {
					bool ignore = false;
                    if (parent.isValid()) {
						// if child belongs to a selected parent, assume the user is just moving the parent and ignore the child
                        QModelIndex par = parent;
                        while (par.isValid() && !ignore) {
							for (int j=0;j<selected_items.size();j++) {
								if (par == selected_items.at(j)) {
									ignore = true;
									break;
								}
							}
                            par = par.parent();
						}
					}
					if (!ignore) {
						move_items.append(s);
					}
				}
            }
            if (move_items.size() > 0) {
                MediaMove* mm = new MediaMove(this);
                mm->to = m;
                mm->items = move_items;
                undo_stack.push(mm);
            }
		}
    }
}