File: keyframeviewbase.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 (548 lines) | stat: -rw-r--r-- 17,475 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
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
/***

  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 "keyframeviewbase.h"

#include <QMouseEvent>
#include <QVBoxLayout>

#include "dialog/keyframeproperties/keyframeproperties.h"
#include "keyframeviewundo.h"
#include "node/node.h"
#include "widget/menu/menu.h"
#include "widget/menu/menushared.h"
#include "widget/nodeparamview/nodeparamviewundo.h"

OLIVE_NAMESPACE_ENTER

KeyframeViewBase::KeyframeViewBase(QWidget *parent) :
  TimelineViewBase(parent),
  dragging_bezier_point_(nullptr),
  currently_autoselecting_(false)
{
  SetDefaultDragMode(RubberBandDrag);
  setContextMenuPolicy(Qt::CustomContextMenu);

  connect(this, &KeyframeViewBase::customContextMenuRequested, this, &KeyframeViewBase::ShowContextMenu);
  connect(scene(), &QGraphicsScene::selectionChanged, this, &KeyframeViewBase::AutoSelectKeyTimeNeighbors);
}

void KeyframeViewBase::Clear()
{
  QMap<NodeKeyframe*, KeyframeViewItem*>::iterator iterator;

  for (iterator=item_map_.begin();iterator!=item_map_.end();iterator++) {
    delete iterator.value();
  }

  item_map_.clear();
}

void KeyframeViewBase::DeleteSelected()
{
  QUndoCommand* command = new QUndoCommand();

  QMap<NodeKeyframe*, KeyframeViewItem*>::const_iterator i;

  for (i=item_map_.constBegin(); i!=item_map_.constEnd(); i++) {
    if (i.value()->isSelected()) {
      NodeInput* input_parent = i.key()->parent();

      new NodeParamRemoveKeyframeCommand(input_parent,
                                         input_parent->get_keyframe_shared_ptr_from_raw(i.key()),
                                         command);
    }
  }

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

void KeyframeViewBase::RemoveKeyframe(NodeKeyframePtr key)
{
  KeyframeAboutToBeRemoved(key.get());

  delete item_map_.take(key.get());
}

KeyframeViewItem *KeyframeViewBase::AddKeyframeInternal(NodeKeyframePtr key)
{
  KeyframeViewItem* item = item_map_.value(key.get());

  if (!item) {
    item = new KeyframeViewItem(key);
    item->SetTimeTarget(GetTimeTarget());
    item->SetScale(GetScale());
    item_map_.insert(key.get(), item);
    scene()->addItem(item);

    if (hidden_tracks_.contains(key->track())) {
      item->setVisible(false);
    }
  }

  return item;
}

void KeyframeViewBase::mousePressEvent(QMouseEvent *event)
{
  if (HandPress(event) || PlayheadPress(event)) {
    return;
  }

  active_tool_ = Core::instance()->tool();

  if (event->button() == Qt::LeftButton) {
    QGraphicsView::mousePressEvent(event);

    if (active_tool_ == Tool::kPointer) {
      QGraphicsItem* item_under_cursor = itemAt(event->pos());

      if (item_under_cursor) {

        drag_start_ = event->pos();

        // Determine what type of item is under the cursor
        dragging_bezier_point_ = dynamic_cast<BezierControlPointItem*>(item_under_cursor);

        if (dragging_bezier_point_) {

          dragging_bezier_point_start_ = dragging_bezier_point_->GetCorrespondingKeyframeHandle();
          dragging_bezier_point_opposing_start_ = dragging_bezier_point_->key()->bezier_control(NodeKeyframe::get_opposing_bezier_type(dragging_bezier_point_->mode()));

        } else {

          QList<QGraphicsItem*> selected_items = scene()->selectedItems();

          selected_keys_.resize(selected_items.size());

          for (int i=0;i<selected_items.size();i++) {
            KeyframeViewItem* key = static_cast<KeyframeViewItem*>(selected_items.at(i));

            selected_keys_.replace(i, {key,
                                       key->x(),
                                       GetAdjustedTime(key->key()->parent()->parentNode(), GetTimeTarget(), key->key()->time(), NodeParam::kOutput),
                                       key->key()->value().toDouble()});
          }
        }
      }
    }
  }
}

void KeyframeViewBase::mouseMoveEvent(QMouseEvent *event)
{
  if (HandMove(event) || PlayheadMove(event)) {
    return;
  }

  if (event->buttons() & Qt::LeftButton) {
    QGraphicsView::mouseMoveEvent(event);

    if (active_tool_ == Tool::kPointer) {
      // Calculate cursor difference and scale it
      QPointF mouse_diff_scaled = GetScaledCursorPos(event->pos() - drag_start_);

      if (dragging_bezier_point_) {
        ProcessBezierDrag(mouse_diff_scaled,
                          !(event->modifiers() & Qt::ControlModifier),
                          false);
      } else if (!selected_keys_.isEmpty()) {
        foreach (const KeyframeItemAndTime& keypair, selected_keys_) {
          //NodeInput* input_parent = keypair.key->key()->parent();

          //input_parent->blockSignals(true);

          rational node_time = GetAdjustedTime(GetTimeTarget(),
                                               keypair.key->key()->parent()->parentNode(),
                                               CalculateNewTimeFromScreen(keypair.time, mouse_diff_scaled.x()),
                                               NodeParam::kInput);

          keypair.key->key()->set_time(node_time);

          if (IsYAxisEnabled()) {
            keypair.key->key()->set_value(keypair.value - mouse_diff_scaled.y());
          }

          // We emit a custom value changed signal while the keyframe is being dragged so only the currently viewed
          // frame gets rendered in this time
          //input_parent->blockSignals(false);

          //input_parent->parentNode()->InvalidateVisible(input_parent, input_parent);
        }
      }
    }
  }
}

void KeyframeViewBase::mouseReleaseEvent(QMouseEvent *event)
{
  if (HandRelease(event) || PlayheadRelease(event)) {
    return;
  }

  if (event->button() == Qt::LeftButton) {
    QGraphicsView::mouseReleaseEvent(event);

    if (active_tool_ == Tool::kPointer) {
      QPoint mouse_diff = event->pos() - drag_start_;
      QPointF mouse_diff_scaled = GetScaledCursorPos(mouse_diff);

      if (!mouse_diff.isNull()) {
        if (dragging_bezier_point_) {
          ProcessBezierDrag(mouse_diff_scaled,
                            !(event->modifiers() & Qt::ControlModifier),
                            true);

          dragging_bezier_point_ = nullptr;
        } else if (!selected_keys_.isEmpty()) {
          QUndoCommand* command = new QUndoCommand();

          foreach (const KeyframeItemAndTime& keypair, selected_keys_) {
            KeyframeViewItem* item = keypair.key;

            keypair.key->key()->parent()->blockSignals(true);

            // Calculate the new time for this keyframe
            rational node_time = GetAdjustedTime(GetTimeTarget(),
                                                 keypair.key->key()->parent()->parentNode(),
                                                 CalculateNewTimeFromScreen(keypair.time, mouse_diff_scaled.x()),
                                                 NodeParam::kInput);



            // Commit movement

            // Since we overrode the cache signalling while dragging, we simulate here precisely the change that
            // occurred by first setting the keyframe to its original position, and then letting the input handle
            // the signalling once the undo command is pushed.
            item->key()->set_time(keypair.time);
            new NodeParamSetKeyframeTimeCommand(item->key(),
                                                node_time,
                                                keypair.time,
                                                command);

            // Commit value if we're setting a value
            if (IsYAxisEnabled()) {
              item->key()->set_value(keypair.value);
              new NodeParamSetKeyframeValueCommand(item->key(),
                                                   keypair.value - mouse_diff_scaled.y(),
                                                   keypair.value,
                                                   command);
            }

            keypair.key->key()->parent()->blockSignals(false);
          }

          Core::instance()->undo_stack()->push(command);
        }
      }

      selected_keys_.clear();
    }
  }
}

void KeyframeViewBase::ScaleChangedEvent(const double &scale)
{
  TimelineViewBase::ScaleChangedEvent(scale);

  QMap<NodeKeyframe*, KeyframeViewItem*>::const_iterator iterator;

  for (iterator=item_map_.begin();iterator!=item_map_.end();iterator++) {
    iterator.value()->SetScale(scale);
  }
}

const QMap<NodeKeyframe *, KeyframeViewItem *> &KeyframeViewBase::item_map() const
{
  return item_map_;
}

void KeyframeViewBase::KeyframeAboutToBeRemoved(NodeKeyframe *)
{
}

void KeyframeViewBase::TimeTargetChangedEvent(Node *target)
{
  QMap<NodeKeyframe*, KeyframeViewItem*>::const_iterator i;

  for (i=item_map_.begin();i!=item_map_.end();i++) {
    i.value()->SetTimeTarget(target);
  }
}

void KeyframeViewBase::SetKeyframeTrackVisible(int track, bool visible)
{
  if (!visible == hidden_tracks_.contains(track)) {
    return;
  }

  QMap<NodeKeyframe*, KeyframeViewItem*>::const_iterator i;

  for (i=item_map_.constBegin(); i!=item_map_.constEnd(); i++) {
    if (i.key()->track() == track) {
      i.value()->setVisible(visible);
    }
  }

  if (visible) {
    hidden_tracks_.removeOne(track);
  } else {
    hidden_tracks_.append(track);
  }
}

void KeyframeViewBase::ContextMenuEvent(Menu& m)
{
  Q_UNUSED(m)
}

rational KeyframeViewBase::CalculateNewTimeFromScreen(const rational &old_time, double cursor_diff)
{
  return rational::fromDouble(old_time.toDouble() + cursor_diff);
}

QPointF KeyframeViewBase::GenerateBezierControlPosition(const NodeKeyframe::BezierType mode, const QPointF &start_point, const QPointF &scaled_cursor_diff)
{
  QPointF new_bezier_pos = start_point;

  new_bezier_pos += scaled_cursor_diff;

  // LIMIT bezier handles from overlapping each other
  if (mode == NodeKeyframe::kInHandle) {
    if (new_bezier_pos.x() > 0) {
      new_bezier_pos.setX(0);
    }
  } else {
    if (new_bezier_pos.x() < 0) {
      new_bezier_pos.setX(0);
    }
  }

  return new_bezier_pos;
}

void KeyframeViewBase::ProcessBezierDrag(QPointF mouse_diff_scaled, bool include_opposing, bool undoable)
{
  // Flip the mouse Y because bezier control points are drawn bottom to top, not top to bottom
  mouse_diff_scaled.setY(-mouse_diff_scaled.y());

  QPointF new_bezier_pos = GenerateBezierControlPosition(dragging_bezier_point_->mode(),
                                                         dragging_bezier_point_start_,
                                                         mouse_diff_scaled);

  // If the user is NOT holding control, we set the other handle to the exact negative of this handle
  QPointF new_opposing_pos;
  NodeKeyframe::BezierType opposing_type = NodeKeyframe::get_opposing_bezier_type(dragging_bezier_point_->mode());

  if (include_opposing) {
    new_opposing_pos = GenerateBezierControlPosition(opposing_type,
                                                     dragging_bezier_point_opposing_start_,
                                                     -mouse_diff_scaled);
  } else {
    new_opposing_pos = dragging_bezier_point_opposing_start_;
  }

  //NodeInput* input_parent = dragging_bezier_point_->key()->parent();

  if (undoable) {
    QUndoCommand* command = new QUndoCommand();

    // Similar to the code in MouseRelease, we manipulated the signalling earlier and need to set the keys back to their
    // original position to allow the input to signal correctly when the undo command is pushed.

    //input_parent->blockSignals(true);

    dragging_bezier_point_->key()->set_bezier_control(dragging_bezier_point_->mode(),
                                                      dragging_bezier_point_start_);

    new KeyframeSetBezierControlPoint(dragging_bezier_point_->key(),
                                      dragging_bezier_point_->mode(),
                                      new_bezier_pos,
                                      dragging_bezier_point_start_,
                                      command);

    if (include_opposing) {
      dragging_bezier_point_->key()->set_bezier_control(opposing_type,
                                                        dragging_bezier_point_opposing_start_);

      new KeyframeSetBezierControlPoint(dragging_bezier_point_->key(),
                                        opposing_type,
                                        new_opposing_pos,
                                        dragging_bezier_point_opposing_start_,
                                        command);
    }

    //input_parent->blockSignals(false);

    Core::instance()->undo_stack()->push(command);
  } else {
    //input_parent->blockSignals(true);

    dragging_bezier_point_->key()->set_bezier_control(dragging_bezier_point_->mode(),
                                                      new_bezier_pos);

    dragging_bezier_point_->key()->set_bezier_control(opposing_type,
                                                      new_opposing_pos);

    //input_parent->blockSignals(false);

    //input_parent->parentNode()->InvalidateVisible(input_parent, input_parent);
  }
}

QPointF KeyframeViewBase::GetScaledCursorPos(const QPoint &cursor_pos)
{
  return QPointF(static_cast<double>(cursor_pos.x()) / GetScale(),
                 static_cast<double>(cursor_pos.y()) / GetYScale());
}

void KeyframeViewBase::ShowContextMenu()
{
  Menu m;

  MenuShared::instance()->AddItemsForEditMenu(&m, false);

  QAction* linear_key_action = nullptr;
  QAction* bezier_key_action = nullptr;
  QAction* hold_key_action = nullptr;

  QList<QGraphicsItem*> items = scene()->selectedItems();
  if (!items.isEmpty()) {
    bool all_keys_are_same_type = true;
    NodeKeyframe::Type type = static_cast<KeyframeViewItem*>(items.first())->key()->type();

    for (int i=1;i<items.size();i++) {
      KeyframeViewItem* key_item = static_cast<KeyframeViewItem*>(items.at(i));
      KeyframeViewItem* prev_item = static_cast<KeyframeViewItem*>(items.at(i-1));

      if (key_item->key()->type() != prev_item->key()->type()) {
        all_keys_are_same_type = false;
        break;
      }
    }

    m.addSeparator();

    linear_key_action = m.addAction(tr("Linear"));
    bezier_key_action = m.addAction(tr("Bezier"));
    hold_key_action = m.addAction(tr("Hold"));

    if (all_keys_are_same_type) {
      switch (type) {
      case NodeKeyframe::kLinear:
        linear_key_action->setChecked(true);
        break;
      case NodeKeyframe::kBezier:
        bezier_key_action->setChecked(true);
        break;
      case NodeKeyframe::kHold:
        hold_key_action->setChecked(true);
        break;
      }
    }
  }

  ContextMenuEvent(m);

  if (!items.isEmpty()) {
    m.addSeparator();

    QAction* properties_action = m.addAction(tr("P&roperties"));
    connect(properties_action, &QAction::triggered, this, &KeyframeViewBase::ShowKeyframePropertiesDialog);
  }

  QAction* selected = m.exec(QCursor::pos());

  // Process keyframe type changes
  if (!items.isEmpty()) {
    if (selected == linear_key_action
        || selected == bezier_key_action
        || selected == hold_key_action) {
      NodeKeyframe::Type new_type;

      if (selected == hold_key_action) {
        new_type = NodeKeyframe::kHold;
      } else if (selected == bezier_key_action) {
        new_type = NodeKeyframe::kBezier;
      } else {
        new_type = NodeKeyframe::kLinear;
      }

      QUndoCommand* command = new QUndoCommand();
      foreach (QGraphicsItem* item, items) {
        new KeyframeSetTypeCommand(static_cast<KeyframeViewItem*>(item)->key(),
                                   new_type,
                                   command);
      }
      Core::instance()->undo_stack()->pushIfHasChildren(command);
    }
  }
}

void KeyframeViewBase::ShowKeyframePropertiesDialog()
{
  QList<QGraphicsItem*> items = scene()->selectedItems();
  QList<NodeKeyframePtr> keys;

  foreach (QGraphicsItem* item, items) {
    keys.append(static_cast<KeyframeViewItem*>(item)->key());
  }

  if (!keys.isEmpty()) {
    KeyframePropertiesDialog kd(keys, timebase(), this);
    kd.exec();
  }
}

void KeyframeViewBase::AutoSelectKeyTimeNeighbors()
{
  if (currently_autoselecting_ || IsYAxisEnabled()) {
    return;
  }

  // Prevents infinite loop
  currently_autoselecting_ = true;

  QList<QGraphicsItem*> selected_items = scene()->selectedItems();

  foreach (QGraphicsItem* g, selected_items) {
    KeyframeViewItem* key_item = static_cast<KeyframeViewItem*>(g);

    rational key_time = key_item->key()->time();

    QList<NodeKeyframePtr> keys = key_item->key()->parent()->get_keyframe_at_time(key_time);

    foreach (NodeKeyframePtr k, keys) {
      if (k == key_item->key()) {
        continue;
      }

      // Ensure this key is not already selected
      KeyframeViewItem* item = item_map_.value(k.get());

      item->setSelected(true);
    }
  }

  currently_autoselecting_ = false;
}

OLIVE_NAMESPACE_EXIT