File: import.cpp

package info (click to toggle)
olive-editor 20200620-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,228 kB
  • sloc: cpp: 51,932; sh: 56; makefile: 7; xml: 7
file content (485 lines) | stat: -rw-r--r-- 16,099 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
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
/***

  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 "widget/timelinewidget/timelinewidget.h"

#include <QCheckBox>
#include <QMessageBox>
#include <QMimeData>
#include <QToolTip>

#include "config/config.h"
#include "common/qtutils.h"
#include "core.h"
#include "dialog/sequence/sequence.h"
#include "node/audio/volume/volume.h"
#include "node/generator/matrix/matrix.h"
#include "node/input/media/audio/audio.h"
#include "node/input/media/video/video.h"
#include "node/math/math/math.h"
#include "project/item/sequence/sequence.h"
#include "widget/nodeview/nodeviewundo.h"
#include "window/mainwindow/mainwindow.h"

OLIVE_NAMESPACE_ENTER

Timeline::TrackType TrackTypeFromStreamType(Stream::Type stream_type)
{
  switch (stream_type) {
  case Stream::kVideo:
  case Stream::kImage:
    return Timeline::kTrackTypeVideo;
  case Stream::kAudio:
    return Timeline::kTrackTypeAudio;
  case Stream::kSubtitle:
    // Temporarily disabled until we figure out a better thing to do with this
    //return Timeline::kTrackTypeSubtitle;
  case Stream::kUnknown:
  case Stream::kData:
  case Stream::kAttachment:
    break;
  }

  return Timeline::kTrackTypeNone;
}

TimelineWidget::ImportTool::ImportTool(TimelineWidget *parent) :
  Tool(parent)
{
  // Calculate width used for importing to give ghosts a slight lead-in so the ghosts aren't right on the cursor
  import_pre_buffer_ = QFontMetricsWidth(parent->fontMetrics(), "HHHHHHHH");
}

void TimelineWidget::ImportTool::DragEnter(TimelineViewMouseEvent *event)
{
  QStringList mime_formats = event->GetMimeData()->formats();

  // Listen for MIME data from a ProjectViewModel
  if (mime_formats.contains("application/x-oliveprojectitemdata")) {

    // Data is drag/drop data from a ProjectViewModel
    QByteArray model_data = event->GetMimeData()->data("application/x-oliveprojectitemdata");

    // Use QDataStream to deserialize the data
    QDataStream stream(&model_data, QIODevice::ReadOnly);

    // Variables to deserialize into
    quintptr item_ptr;
    int r;
    quint64 enabled_streams;

    // Set drag start position
    drag_start_ = event->GetCoordinates();

    snap_points_.clear();

    while (!stream.atEnd()) {
      stream >> enabled_streams >> r >> item_ptr;

      // Get Item object
      Item* item = reinterpret_cast<Item*>(item_ptr);

      // Check if Item is Footage
      if (item->type() == Item::kFootage) {

        // If the Item is Footage, we can create a Ghost from it
        dragged_footage_.append(DraggedFootage(static_cast<Footage*>(item), enabled_streams));

      }
    }

    PrepGhosts(drag_start_.GetFrame() - parent()->SceneToTime(import_pre_buffer_),
               drag_start_.GetTrack().index());

    event->accept();
  } else {
    // FIXME: Implement dropping from file
    event->ignore();
  }
}

void TimelineWidget::ImportTool::DragMove(TimelineViewMouseEvent *event)
{
  if (!dragged_footage_.isEmpty()) {

    if (parent()->HasGhosts()) {
      rational time_movement = event->GetFrame() - drag_start_.GetFrame();
      int track_movement = event->GetTrack().index() - drag_start_.GetTrack().index();

      time_movement = ValidateTimeMovement(time_movement, parent()->ghost_items_);
      track_movement = ValidateTrackMovement(track_movement, parent()->ghost_items_);

      // If snapping is enabled, check for snap points
      if (Core::instance()->snapping()) {
        parent()->SnapPoint(snap_points_, &time_movement);

        time_movement = ValidateTimeMovement(time_movement, parent()->ghost_items_);
        track_movement = ValidateTrackMovement(track_movement, parent()->ghost_items_);
      }

      rational earliest_ghost = RATIONAL_MAX;

      // Move ghosts to the mouse cursor
      foreach (TimelineViewGhostItem* ghost, parent()->ghost_items_) {
        ghost->SetInAdjustment(time_movement);
        ghost->SetOutAdjustment(time_movement);
        ghost->SetTrackAdjustment(track_movement);

        TrackReference adjusted_track = ghost->GetAdjustedTrack();
        ghost->SetYCoords(parent()->GetTrackY(adjusted_track), parent()->GetTrackHeight(adjusted_track));

        earliest_ghost = qMin(earliest_ghost, ghost->GetAdjustedIn());
      }

      // Generate tooltip (showing earliest in point of imported clip)
      int64_t earliest_timestamp = Timecode::time_to_timestamp(earliest_ghost, parent()->GetToolTipTimebase());
      QString tooltip_text = Timecode::timestamp_to_timecode(earliest_timestamp,
                                                             parent()->GetToolTipTimebase(),
                                                             Core::instance()->GetTimecodeDisplay());

      // Force tooltip to update (otherwise the tooltip won't move as written in the documentation, and could get in the way
      // of the cursor)
      QToolTip::hideText();
      QToolTip::showText(QCursor::pos(),
                         tooltip_text,
                         parent());
    }

    event->accept();
  } else {
    event->ignore();
  }
}

void TimelineWidget::ImportTool::DragLeave(QDragLeaveEvent* event)
{
  if (!dragged_footage_.isEmpty()) {
    parent()->ClearGhosts();
    dragged_footage_.clear();

    event->accept();
  } else {
    event->ignore();
  }
}

void TimelineWidget::ImportTool::DragDrop(TimelineViewMouseEvent *event)
{
  if (!dragged_footage_.isEmpty()) {
    DropGhosts(event->GetModifiers() & Qt::ControlModifier);

    event->accept();
  } else {
    event->ignore();
  }
}

void TimelineWidget::ImportTool::PlaceAt(const QList<Footage *> &footage, const rational &start, bool insert)
{
  PlaceAt(FootageToDraggedFootage(footage), start, insert);
}

void TimelineWidget::ImportTool::PlaceAt(const QList<DraggedFootage> &footage, const rational &start, bool insert)
{
  dragged_footage_ = footage;

  if (dragged_footage_.isEmpty()) {
    return;
  }

  PrepGhosts(start, 0);
  DropGhosts(insert);
}

void TimelineWidget::ImportTool::FootageToGhosts(rational ghost_start, const QList<DraggedFootage> &footage_list, const rational& dest_tb, const int& track_start)
{
  foreach (const DraggedFootage& footage, footage_list) {

    // Each stream is offset by one track per track "type", we keep track of them in this vector
    QVector<int> track_offsets(Timeline::kTrackTypeCount);
    track_offsets.fill(track_start);

    QVector<TimelineViewGhostItem*> footage_ghosts;
    rational footage_duration;
    bool contains_image_stream = false;

    quint64 enabled_streams = footage.streams();

    // Loop through all streams in footage
    foreach (StreamPtr stream, footage.footage()->streams()) {
      Timeline::TrackType track_type = TrackTypeFromStreamType(stream->type());

      quint64 cached_enabled_streams = enabled_streams;
      enabled_streams >>= 1;

      // Check if this stream has a compatible TrackList
      if (track_type == Timeline::kTrackTypeNone
          || !(cached_enabled_streams & 0x1)) {
        continue;
      }

      TimelineViewGhostItem* ghost = new TimelineViewGhostItem();

      if (stream->type() == Stream::kImage) {
        // Stream is essentially length-less - we may use the default still image length in config,
        // or we may use another stream's length depending on the circumstance
        contains_image_stream = true;
      } else {
        // Rescale stream duration to timeline timebase
        // Convert to rational time
        if (footage.footage()->workarea()->enabled()) {
          footage_duration = qMax(footage_duration, footage.footage()->workarea()->range().length());
          ghost->SetMediaIn(footage.footage()->workarea()->in());
        } else {
          int64_t stream_duration = Timecode::rescale_timestamp_ceil(stream->duration(), stream->timebase(), dest_tb);
          footage_duration = qMax(footage_duration, Timecode::timestamp_to_time(stream_duration, dest_tb));
        }
      }

      ghost->SetTrack(TrackReference(track_type, track_offsets.at(track_type)));

      // Increment track count for this track type
      track_offsets[track_type]++;

      snap_points_.append(ghost->In());
      snap_points_.append(ghost->Out());

      ghost->setData(TimelineViewGhostItem::kAttachedFootage, QVariant::fromValue(stream));
      ghost->SetMode(Timeline::kMove);

      footage_ghosts.append(ghost);

    }

    if (contains_image_stream && footage_duration.isNull()) {
      // Footage must ONLY be image streams so no duration value was found, use default in config
      footage_duration = Config::Current()["DefaultStillLength"].value<rational>();
    }

    foreach (TimelineViewGhostItem* ghost, footage_ghosts) {
      ghost->SetIn(ghost_start);
      ghost->SetOut(ghost_start + footage_duration);

      parent()->AddGhost(ghost);
    }

    // Stack each ghost one after the other
    ghost_start += footage_duration;

  }
}

void TimelineWidget::ImportTool::PrepGhosts(const rational& frame, const int& track_index)
{
  if (parent()->GetConnectedNode()) {
    FootageToGhosts(frame,
                    dragged_footage_,
                    parent()->timebase(),
                    track_index);
  }
}

void TimelineWidget::ImportTool::DropGhosts(bool insert)
{
  QUndoCommand* command = new QUndoCommand();

  NodeGraph* dst_graph = nullptr;
  ViewerOutput* viewer_node = nullptr;
  Sequence* open_sequence = nullptr;

  if (parent()->GetConnectedNode()) {
    viewer_node = parent()->GetConnectedNode();
    dst_graph = static_cast<NodeGraph*>(parent()->GetConnectedNode()->parent());
  } else {
    // There's no active timeline here, ask the user what to do

    DropWithoutSequenceBehavior behavior = static_cast<DropWithoutSequenceBehavior>(Config::Current()["DropWithoutSequenceBehavior"].toInt());

    if (behavior == kDWSAsk) {
      QCheckBox* dont_ask_again_box = new QCheckBox(tr("Don't ask me again"));

      QMessageBox mbox(parent());

      mbox.setIcon(QMessageBox::Question);
      mbox.setWindowTitle(tr("No Active Sequence"));
      mbox.setText(tr("No sequence is currently open. Would you like to create one?"));
      mbox.setCheckBox(dont_ask_again_box);

      QPushButton* auto_params_btn = mbox.addButton(tr("Automatically Detect Parameters From Footage"), QMessageBox::YesRole);
      QPushButton* manual_params_btn = mbox.addButton(tr("Set Parameters Manually"), QMessageBox::NoRole);
      mbox.addButton(QMessageBox::Cancel);

      mbox.exec();

      if (mbox.clickedButton() == auto_params_btn) {
        behavior = kDWSAuto;
      } else if (mbox.clickedButton() == manual_params_btn) {
        behavior = kDWSManual;
      } else {
        behavior = kDWSDisable;
      }

      if (behavior != kDWSDisable && dont_ask_again_box->isChecked()) {
        Config::Current()["DropWithoutSequenceBehavior"] = behavior;
      }
    }

    if (behavior != kDWSDisable) {
      ProjectPtr active_project = Core::instance()->GetActiveProject();

      if (active_project) {
        SequencePtr new_sequence = Core::instance()->CreateNewSequenceForProject(active_project.get());

        new_sequence->set_default_parameters();

        bool sequence_is_valid = true;

        if (behavior == kDWSAuto) {

          QList<Footage*> footage_only;

          foreach (const DraggedFootage& df, dragged_footage_) {
            footage_only.append(df.footage());
          }

          new_sequence->set_parameters_from_footage(footage_only);

        } else {

          SequenceDialog sd(new_sequence.get(), SequenceDialog::kNew, parent());
          sd.SetUndoable(false);

          if (sd.exec() != QDialog::Accepted) {
            sequence_is_valid = false;
          }

        }

        if (sequence_is_valid) {
          new_sequence->add_default_nodes();

          new ProjectViewModel::AddItemCommand(Core::instance()->GetActiveProjectModel(),
                                               Core::instance()->GetSelectedFolderInActiveProject(),
                                               new_sequence,
                                               command);

          FootageToGhosts(0, dragged_footage_, new_sequence->video_params().time_base(), 0);

          dst_graph = new_sequence.get();
          viewer_node = new_sequence->viewer_output();

          // Set this as the sequence to open
          open_sequence = new_sequence.get();
        }
      }
    }
  }

  if (dst_graph) {

    QVector<Block*> block_items(parent()->ghost_items_.size());

    // Check if we're inserting
    if (insert) {
      InsertGapsAtGhostDestination(parent()->ghost_items_, command);
    }

    for (int i=0;i<parent()->ghost_items_.size();i++) {
      TimelineViewGhostItem* ghost = parent()->ghost_items_.at(i);

      StreamPtr footage_stream = ghost->data(TimelineViewGhostItem::kAttachedFootage).value<StreamPtr>();

      ClipBlock* clip = new ClipBlock();
      clip->set_media_in(ghost->MediaIn());
      clip->set_length_and_media_out(ghost->Length());
      clip->SetLabel(footage_stream->footage()->name());
      new NodeAddCommand(dst_graph, clip, command);

      switch (footage_stream->type()) {
      case Stream::kVideo:
      case Stream::kImage:
      {
        VideoInput* video_input = new VideoInput();
        video_input->SetFootage(footage_stream);
        new NodeAddCommand(dst_graph, video_input, command);

        new NodeEdgeAddCommand(video_input->output(), clip->texture_input(), command);

        /*
        MatrixGenerator* matrix = new MatrixGenerator();
        new NodeAddCommand(dst_graph, matrix, command);

        MathNode* multiply = new MathNode();
        multiply->SetOperation(MathNode::kOpMultiply);
        new NodeAddCommand(dst_graph, multiply, command);

        new NodeEdgeAddCommand(video_input->output(), multiply->param_a_in(), command);
        new NodeEdgeAddCommand(matrix->output(), multiply->param_b_in(), command);
        new NodeEdgeAddCommand(multiply->output(), clip->texture_input(), command);
        */
        break;
      }
      case Stream::kAudio:
      {
        AudioInput* audio_input = new AudioInput();
        audio_input->SetFootage(footage_stream);
        new NodeAddCommand(dst_graph, audio_input, command);

        VolumeNode* volume_node = new VolumeNode();
        new NodeAddCommand(dst_graph, volume_node, command);

        new NodeEdgeAddCommand(audio_input->output(), volume_node->samples_input(), command);
        new NodeEdgeAddCommand(volume_node->output(), clip->texture_input(), command);
        break;
      }
      default:
        break;
      }

      new TrackPlaceBlockCommand(viewer_node->track_list(ghost->GetAdjustedTrack().type()),
                                 ghost->GetAdjustedTrack().index(),
                                 clip,
                                 ghost->GetAdjustedIn(),
                                 command);

      block_items.replace(i, clip);

      // Link any clips so far that share the same Footage with this one
      for (int j=0;j<i;j++) {
        StreamPtr footage_compare = parent()->ghost_items_.at(j)->data(TimelineViewGhostItem::kAttachedFootage).value<StreamPtr>();

        if (footage_compare->footage() == footage_stream->footage()) {
          Block::Link(block_items.at(j), clip);
        }
      }
    }
  }

  Core::instance()->undo_stack()->pushIfHasChildren(command);

  parent()->ClearGhosts();
  dragged_footage_.clear();

  if (open_sequence) {
    Core::instance()->main_window()->OpenSequence(open_sequence);
  }
}

OLIVE_NAMESPACE_EXIT