-
Notifications
You must be signed in to change notification settings - Fork 322
/
DopeSheetView.cpp
3728 lines (3043 loc) · 131 KB
/
DopeSheetView.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
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron 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 2 of the License, or
* (at your option) any later version.
*
* Natron 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 Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "DopeSheetView.h"
#include <algorithm> // min, max
#include <limits>
#include <stdexcept>
// Qt includes
#include <QApplication>
#include <QMouseEvent>
#include <QScrollBar>
#include <QtCore/QThread>
#include <QToolButton>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QWindow>
#endif
// Natron includes
#include "Engine/Curve.h"
#include "Engine/Image.h"
#include "Engine/Node.h"
#include "Engine/NodeGroup.h"
#include "Engine/Project.h"
#include "Engine/Settings.h"
#include "Engine/ViewIdx.h"
#include "Global/Enums.h"
#include "Gui/ActionShortcuts.h"
#include "Gui/CurveEditor.h"
#include "Gui/CurveWidget.h"
#include "Gui/DockablePanel.h"
#include "Gui/DopeSheet.h"
#include "Gui/DopeSheetEditor.h"
#include "Gui/DopeSheetEditorUndoRedo.h"
#include "Gui/DopeSheetHierarchyView.h"
#include "Gui/Gui.h"
#include "Gui/GuiApplicationManager.h"
#include "Gui/GuiAppInstance.h"
#include "Gui/GuiDefines.h"
#include "Gui/GuiMacros.h"
#include "Gui/Histogram.h"
#include "Gui/KnobGui.h"
#include "Gui/Menu.h"
#include "Gui/NodeGraph.h"
#include "Gui/NodeGui.h"
#include "Gui/TextRenderer.h"
#include "Gui/ticks.h"
#include "Gui/ViewerGL.h"
#include "Gui/ViewerTab.h"
#include "Gui/ZoomContext.h"
#include "Gui/TabWidget.h"
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
#include <QOpenGLContext>
#endif
#define NATRON_DOPESHEET_MIN_RANGE_FIT 10
NATRON_NAMESPACE_ENTER
NATRON_NAMESPACE_ANONYMOUS_ENTER
//Protect declarations in an anonymous namespace
// Typedefs
typedef std::set<double> TimeSet;
typedef std::pair<double, double> FrameRange;
typedef std::map<KnobIWPtr, KnobGui *> KnobsAndGuis;
// Constants
static const int KF_TEXTURES_COUNT = 18;
static const int KF_PIXMAP_SIZE = 14;
static const int KF_X_OFFSET = KF_PIXMAP_SIZE / 2;
static const int DISTANCE_ACCEPTANCE_FROM_KEYFRAME = 5;
static const int DISTANCE_ACCEPTANCE_FROM_READER_EDGE = 14;
static const int DISTANCE_ACCEPTANCE_FROM_READER_BOTTOM = 8;
////////////////////////// Helpers //////////////////////////
void
running_in_main_thread()
{
assert( qApp && qApp->thread() == QThread::currentThread() );
}
void
running_in_main_context(const QOpenGLWidget *glWidget)
{
assert( glWidget->context() == QOpenGLContext::currentContext() );
Q_UNUSED(glWidget);
}
void
running_in_main_thread_and_context(const QOpenGLWidget *glWidget)
{
running_in_main_thread();
running_in_main_context(glWidget);
}
NATRON_NAMESPACE_ANONYMOUS_EXIT
////////////////////////// DopeSheetView //////////////////////////
class DopeSheetViewPrivate
{
Q_DECLARE_TR_FUNCTIONS(DopeSheetView)
public:
enum KeyframeTexture
{
kfTextureNone = -2,
kfTextureInterpConstant = 0,
kfTextureInterpConstantSelected,
kfTextureInterpLinear,
kfTextureInterpLinearSelected,
kfTextureInterpCurve,
kfTextureInterpCurveSelected,
kfTextureInterpBreak,
kfTextureInterpBreakSelected,
kfTextureInterpCurveC,
kfTextureInterpCurveCSelected,
kfTextureInterpCurveH,
kfTextureInterpCurveHSelected,
kfTextureInterpCurveR,
kfTextureInterpCurveRSelected,
kfTextureInterpCurveZ,
kfTextureInterpCurveZSelected,
kfTextureMaster,
kfTextureMasterSelected,
};
DopeSheetViewPrivate(DopeSheetView *qq);
~DopeSheetViewPrivate();
/* functions */
// Helpers
// This function is just wrong and confusing, it takes keyTime which is in zoom Coordinates and y which is in widget coordinates
//QRectF keyframeRect(double keyTime, double y) const;
// It is now much more explicit in which coord. system each value lies. The returned rectangle is in zoom coordinates.
RectD getKeyFrameBoundingRectZoomCoords(double keyframeTimeZoomCoords, double keyframeYCenterWidgetCoords) const;
/*
QRectF and Qt coordinate system has its y axis top-down, whereas in Natron
the coordinate system is bottom-up. When using QRectF, top-left is in fact (0,0)
while in Natron it is (0,h - 1).
Hence we use different data types to identify the 2 different coordinate systems.
*/
RectD rectToZoomCoordinates(const QRectF &rect) const;
QRectF rectToWidgetCoordinates(const RectD &rect) const;
QRectF nameItemRectToRowRect(const QRectF &rect) const;
Qt::CursorShape getCursorDuringHover(const QPointF &widgetCoords) const;
Qt::CursorShape getCursorForEventState(DopeSheetView::EventStateEnum es) const;
bool isNearByClipRectLeft(const QPointF& zoomCoordPos, const RectD &clipRect) const;
bool isNearByClipRectRight(const QPointF& zoomCoordPos, const RectD &clipRect) const;
bool isNearByClipRectBottom(const QPointF& zoomCoordPos, const RectD &clipRect) const;
bool isNearByCurrentFrameIndicatorBottom(const QPointF &zoomCoords) const;
bool isNearbySelectedKeysBRectRightEdge(const QPointF& widgetPos) const;
bool isNearbySelectedKeysBRectLeftEdge(const QPointF& widgetPos) const;
bool isNearbySelectedKeysBRec(const QPointF& widgetPos) const;
std::vector<DopeSheetKey> isNearByKeyframe(const DSKnobPtr &dsKnob, const QPointF &widgetCoords) const;
std::vector<DopeSheetKey> isNearByKeyframe(DSNodePtr dsNode, const QPointF &widgetCoords) const;
double clampedMouseOffset(double fromTime, double toTime);
// Textures
void generateKeyframeTextures();
DopeSheetViewPrivate::KeyframeTexture kfTextureFromKeyframeType(KeyframeTypeEnum kfType, bool selected) const;
// Drawing
void drawScale() const;
void drawRows() const;
void drawNodeRow(const DSNodePtr dsNode) const;
void drawKnobRow(const DSKnobPtr dsKnob) const;
void drawNodeRowSeparation(const DSNodePtr dsNode) const;
void drawRange(const DSNodePtr &dsNode) const;
void drawKeyframes(const DSNodePtr &dsNode) const;
void drawTexturedKeyframe(DopeSheetViewPrivate::KeyframeTexture textureType,
bool drawTime,
double time,
const QColor& textColor,
const RectD &rect) const;
void drawGroupOverlay(const DSNodePtr &dsNode, const DSNodePtr &group) const;
void drawProjectBounds() const;
void drawCurrentFrameIndicator();
void drawSelectionRect() const;
void drawSelectedKeysBRect() const;
void renderText(double x, double y,
const QString &text,
const QColor &color,
const QFont &font,
int flags = 0) const; //!< see http://doc.qt.io/qt-4.8/qpainter.html#drawText-10
// After or during an user interaction
void computeTimelinePositions();
void computeSelectionRect(const QPointF &origin, const QPointF ¤t);
void computeSelectedKeysBRect();
void computeRangesBelow(DSNode * dsNode);
void computeNodeRange(DSNode *dsNode);
void computeReaderRange(DSNode *reader);
void computeRetimeRange(DSNode *retimer);
void computeTimeOffsetRange(DSNode *timeOffset);
void computeFRRange(DSNode *frameRange);
void computeGroupRange(DSNode *group);
// User interaction
void onMouseLeftButtonDrag(QMouseEvent *e);
void createSelectionFromRect(const RectD &rect, std::vector<DopeSheetKey> *result, std::vector<DSNodePtr>* selectedNodes);
void moveCurrentFrameIndicator(double dt);
void createContextMenu();
void updateCurveWidgetFrameRange();
/* attributes */
DopeSheetView *q_ptr;
DopeSheet *model;
HierarchyView *hierarchyView;
Gui *gui;
// necessary to retrieve some useful values for drawing
TimeLinePtr timeline;
//
std::map<DSNode *, FrameRange > nodeRanges;
std::list<DSNode*> nodeRangesBeingComputed; // to avoid recursion in groups
int rangeComputationRecursion;
// for rendering
double _screenPixelRatio;
boost::scoped_ptr<QFont> _textFont;
TextRenderer textRenderer;
// for textures
GLuint kfTexturesIDs[KF_TEXTURES_COUNT];
// for navigating
ZoomContext zoomContext;
bool zoomOrPannedSinceLastFit;
// for current time indicator
QPolygonF currentFrameIndicatorBottomPoly;
// for keyframe selection
// This is in zoom coordinates...
RectD selectionRect;
// keyframe selection rect
// This is in zoom coordinates
RectD selectedKeysBRect;
// for various user interaction
QPointF lastPosOnMousePress;
QPointF lastPosOnMouseMove;
double keyDragLastMovement;
DopeSheetView::EventStateEnum eventState;
// for clip (Reader, Time nodes) user interaction
DSNodePtr currentEditedReader;
// UI
Menu *contextMenu;
};
DopeSheetViewPrivate::DopeSheetViewPrivate(DopeSheetView *qq)
: q_ptr(qq)
, model(0)
, hierarchyView(0)
, gui(0)
, timeline()
, nodeRanges()
, nodeRangesBeingComputed()
, rangeComputationRecursion(0)
, _screenPixelRatio(0)
, _textFont()
, textRenderer()
, kfTexturesIDs()
, zoomContext(0.01, 100.)
, zoomOrPannedSinceLastFit(false)
, selectionRect()
, selectedKeysBRect()
, lastPosOnMousePress()
, lastPosOnMouseMove()
, keyDragLastMovement()
, eventState(DopeSheetView::esNoEditingState)
, currentEditedReader()
, contextMenu( new Menu(q_ptr) )
{
}
DopeSheetViewPrivate::~DopeSheetViewPrivate()
{
glDeleteTextures(KF_TEXTURES_COUNT, kfTexturesIDs);
}
/*
This function is just wrong and confusing, it takes keyTime which is in zoom Coordinates and y which is in widget coordinates
Do not uncomment
QRectF DopeSheetViewPrivate::keyframeRect(double keyTime, double y) const
{
QPointF p = zoomContext.toZoomCoordinates(keyTime, y);
QRectF ret;
ret.setHeight(KF_PIXMAP_SIZE);
ret.setLeft(zoomContext.toZoomCoordinates(keyTime - KF_X_OFFSET, y).x());
ret.setRight(zoomContext.toZoomCoordinates(keyTime + KF_X_OFFSET, y).x());
ret.moveCenter(zoomContext.toWidgetCoordinates(p.x(), p.y()));
return ret;
}
*/
RectD
DopeSheetViewPrivate::getKeyFrameBoundingRectZoomCoords(double keyframeTimeZoomCoords,
double keyframeYCenterWidgetCoords) const
{
double keyframeTimeWidget = zoomContext.toWidgetCoordinates(keyframeTimeZoomCoords, 0).x();
RectD ret;
QPointF x1y1 = zoomContext.toZoomCoordinates(keyframeTimeWidget - KF_X_OFFSET, keyframeYCenterWidgetCoords + KF_PIXMAP_SIZE / 2);
QPointF x2y2 = zoomContext.toZoomCoordinates(keyframeTimeWidget + KF_X_OFFSET, keyframeYCenterWidgetCoords - KF_PIXMAP_SIZE / 2);
ret.x1 = x1y1.x();
ret.y1 = x1y1.y();
ret.x2 = x2y2.x();
ret.y2 = x2y2.y();
return ret;
}
/**
* @brief Converts the given (x,y) coordinates which are in OpenGL canonical coordinates to widget coordinates.
**/
void
DopeSheetView::toWidgetCoordinates(double *x,
double *y) const
{
QPointF p = _imp->zoomContext.toWidgetCoordinates(*x, *y);
*x = p.x();
*y = p.y();
}
/**
* @brief Converts the given (x,y) coordinates which are in widget coordinates to OpenGL canonical coordinates
**/
void
DopeSheetView::toCanonicalCoordinates(double *x,
double *y) const
{
QPointF p = _imp->zoomContext.toZoomCoordinates(*x, *y);
*x = p.x();
*y = p.y();
}
/**
* @brief Returns the font height, i.e: the height of the highest letter for this font
**/
int
DopeSheetView::getWidgetFontHeight() const
{
return fontMetrics().height();
}
/**
* @brief Returns for a string the estimated pixel size it would take on the widget
**/
int
DopeSheetView::getStringWidthForCurrentFont(const std::string& string) const
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
return fontMetrics().horizontalAdvance( QString::fromUtf8( string.c_str() ) );
#else
return fontMetrics().width( QString::fromUtf8( string.c_str() ) );
#endif
}
/*
QRectF and Qt coordinate system has its y axis top-down, whereas in Natron
the coordinate system is bottom-up. When using QRectF, top-left is in fact (0,0)
while in Natron it is (0,h - 1).
Hence we use different data types to identify the 2 different coordinate systems.
*/
RectD
DopeSheetViewPrivate::rectToZoomCoordinates(const QRectF &rect) const
{
QPointF topLeft = rect.topLeft();
topLeft = zoomContext.toZoomCoordinates( topLeft.x(), topLeft.y() );
RectD ret;
ret.x1 = topLeft.x();
ret.y2 = topLeft.y();
QPointF bottomRight = rect.bottomRight();
bottomRight = zoomContext.toZoomCoordinates( bottomRight.x(), bottomRight.y() );
ret.x2 = bottomRight.x();
ret.y1 = bottomRight.y();
assert(ret.y2 >= ret.y1 && ret.x2 >= ret.x1);
return ret;
}
/*
QRectF and Qt coordinate system has its y axis top-down, whereas in Natron
the coordinate system is bottom-up. When using QRectF, top-left is in fact (0,0)
while in Natron it is (0,h - 1).
Hence we use different data types to identify the 2 different coordinate systems.
*/
QRectF
DopeSheetViewPrivate::rectToWidgetCoordinates(const RectD &rect) const
{
QPointF topLeft = zoomContext.toWidgetCoordinates(rect.x1, rect.y2);
QPointF bottomRight = zoomContext.toWidgetCoordinates(rect.x2, rect.y1);
return QRectF( topLeft.x(), topLeft.y(), bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y() );
}
QRectF
DopeSheetViewPrivate::nameItemRectToRowRect(const QRectF &rect) const
{
QPointF topLeft = rect.topLeft();
topLeft = zoomContext.toZoomCoordinates( topLeft.x(), topLeft.y() );
QPointF bottomRight = rect.bottomRight();
bottomRight = zoomContext.toZoomCoordinates( bottomRight.x(), bottomRight.y() );
return QRectF( QPointF( zoomContext.left(), topLeft.y() ),
QPointF( zoomContext.right(), bottomRight.y() ) );
}
Qt::CursorShape
DopeSheetViewPrivate::getCursorDuringHover(const QPointF &widgetCoords) const
{
QPointF clickZoomCoords = zoomContext.toZoomCoordinates( widgetCoords.x(), widgetCoords.y() );
if ( isNearbySelectedKeysBRec(widgetCoords) ) {
return getCursorForEventState(DopeSheetView::esMoveKeyframeSelection);
} else if ( isNearbySelectedKeysBRectRightEdge(widgetCoords) ) {
return getCursorForEventState(DopeSheetView::esTransformingKeyframesMiddleRight);
} else if ( isNearbySelectedKeysBRectLeftEdge(widgetCoords) ) {
return getCursorForEventState(DopeSheetView::esTransformingKeyframesMiddleLeft);
} else if ( isNearByCurrentFrameIndicatorBottom(clickZoomCoords) ) {
return getCursorForEventState(DopeSheetView::esMoveCurrentFrameIndicator);
}
///Look for a range node
DSTreeItemNodeMap nodes = model->getItemNodeMap();
for (DSTreeItemNodeMap::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( it->second->isRangeDrawingEnabled() ) {
std::map<DSNode *, FrameRange >::const_iterator foundRange = nodeRanges.find( it->second.get() );
if ( foundRange == nodeRanges.end() ) {
continue;
}
QRectF treeItemRect = hierarchyView->visualItemRect(it->first);
const FrameRange& range = foundRange->second;
RectD nodeClipRect;
nodeClipRect.x1 = range.first;
nodeClipRect.x2 = range.second;
nodeClipRect.y2 = zoomContext.toZoomCoordinates( 0, treeItemRect.top() ).y();
nodeClipRect.y1 = zoomContext.toZoomCoordinates( 0, treeItemRect.bottom() ).y();
DopeSheetItemType nodeType = it->second->getItemType();
if ( nodeClipRect.contains( clickZoomCoords.x(), clickZoomCoords.y() ) ) {
if ( (nodeType == eDopeSheetItemTypeGroup) ||
( nodeType == eDopeSheetItemTypeReader) ||
( nodeType == eDopeSheetItemTypeTimeOffset) ||
( nodeType == eDopeSheetItemTypeFrameRange) ) {
return getCursorForEventState(DopeSheetView::esMoveKeyframeSelection);
}
}
if (nodeType == eDopeSheetItemTypeReader) {
if ( isNearByClipRectLeft(clickZoomCoords, nodeClipRect) ) {
return getCursorForEventState(DopeSheetView::esReaderLeftTrim);
} else if ( isNearByClipRectRight(clickZoomCoords, nodeClipRect) ) {
return getCursorForEventState(DopeSheetView::esReaderRightTrim);
} else if ( model->canSlipReader(it->second) && isNearByClipRectBottom(clickZoomCoords, nodeClipRect) ) {
return getCursorForEventState(DopeSheetView::esReaderSlip);
}
}
}
} // for (DSTreeItemNodeMap::iterator it = nodes.begin(); it!=nodes.end(); ++it) {
QTreeWidgetItem *treeItem = hierarchyView->itemAt( 0, widgetCoords.y() );
//Did not find a range node, look for keyframes
if (treeItem) {
DSTreeItemNodeMap dsNodeItems = model->getItemNodeMap();
DSTreeItemNodeMap::const_iterator foundDsNode = dsNodeItems.find(treeItem);
if ( foundDsNode != dsNodeItems.end() ) {
const DSNodePtr &dsNode = (*foundDsNode).second;
DopeSheetItemType nodeType = dsNode->getItemType();
if (nodeType == eDopeSheetItemTypeCommon) {
std::vector<DopeSheetKey> keysUnderMouse = isNearByKeyframe(dsNode, widgetCoords);
if ( !keysUnderMouse.empty() ) {
return getCursorForEventState(DopeSheetView::esPickKeyframe);
}
}
} else { // if (foundDsNode != dsNodeItems.end()) {
//We may be on a knob row
DSKnobPtr dsKnob = hierarchyView->getDSKnobAt( widgetCoords.y() );
if (dsKnob) {
std::vector<DopeSheetKey> keysUnderMouse = isNearByKeyframe(dsKnob, widgetCoords);
if ( !keysUnderMouse.empty() ) {
return getCursorForEventState(DopeSheetView::esPickKeyframe);
}
}
}
} // if (treeItem) {
return getCursorForEventState(DopeSheetView::esNoEditingState);
} // DopeSheetViewPrivate::getCursorDuringHover
Qt::CursorShape
DopeSheetViewPrivate::getCursorForEventState(DopeSheetView::EventStateEnum es) const
{
Qt::CursorShape cursorShape;
switch (es) {
case DopeSheetView::esPickKeyframe:
cursorShape = Qt::CrossCursor;
break;
case DopeSheetView::esMoveKeyframeSelection:
cursorShape = Qt::OpenHandCursor;
break;
case DopeSheetView::esReaderLeftTrim:
case DopeSheetView::esMoveCurrentFrameIndicator:
cursorShape = Qt::SplitHCursor;
break;
case DopeSheetView::esReaderRightTrim:
cursorShape = Qt::SplitHCursor;
break;
case DopeSheetView::esReaderSlip:
cursorShape = Qt::SizeHorCursor;
break;
case DopeSheetView::esTransformingKeyframesMiddleLeft:
case DopeSheetView::esTransformingKeyframesMiddleRight:
cursorShape = Qt::SplitHCursor;
break;
case DopeSheetView::esNoEditingState:
default:
cursorShape = Qt::ArrowCursor;
break;
}
return cursorShape;
}
bool
DopeSheetViewPrivate::isNearByClipRectLeft(const QPointF& zoomCoordPos,
const RectD &clipRect) const
{
QPointF widgetPos = zoomContext.toWidgetCoordinates( zoomCoordPos.x(), zoomCoordPos.y() );
QPointF rectx1y1 = zoomContext.toWidgetCoordinates( clipRect.left(), clipRect.bottom() );
QPointF rectx2y2 = zoomContext.toWidgetCoordinates( clipRect.right(), clipRect.top() );
return ( (widgetPos.x() >= rectx1y1.x() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
( widgetPos.x() <= rectx1y1.x() ) &&
(widgetPos.y() <= rectx1y1.y() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
(widgetPos.y() >= rectx2y2.y() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) );
}
bool
DopeSheetViewPrivate::isNearByClipRectRight(const QPointF& zoomCoordPos,
const RectD &clipRect) const
{
QPointF widgetPos = zoomContext.toWidgetCoordinates( zoomCoordPos.x(), zoomCoordPos.y() );
QPointF rectx1y1 = zoomContext.toWidgetCoordinates( clipRect.left(), clipRect.bottom() );
QPointF rectx2y2 = zoomContext.toWidgetCoordinates( clipRect.right(), clipRect.top() );
return ( ( widgetPos.x() >= rectx2y2.x() ) &&
(widgetPos.x() <= rectx2y2.x() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
(widgetPos.y() <= rectx1y1.y() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
(widgetPos.y() >= rectx2y2.y() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) );
}
bool
DopeSheetViewPrivate::isNearByClipRectBottom(const QPointF& zoomCoordPos,
const RectD &clipRect) const
{
QPointF widgetPos = zoomContext.toWidgetCoordinates( zoomCoordPos.x(), zoomCoordPos.y() );
QPointF rectx1y1 = zoomContext.toWidgetCoordinates( clipRect.left(), clipRect.bottom() );
QPointF rectx2y2 = zoomContext.toWidgetCoordinates( clipRect.right(), clipRect.top() );
return ( (widgetPos.x() >= rectx1y1.x() - DISTANCE_ACCEPTANCE_FROM_READER_BOTTOM) &&
(widgetPos.x() <= rectx2y2.x() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
(widgetPos.y() <= rectx1y1.y() + DISTANCE_ACCEPTANCE_FROM_READER_BOTTOM) &&
(widgetPos.y() >= rectx1y1.y() - DISTANCE_ACCEPTANCE_FROM_READER_BOTTOM) );
}
bool
DopeSheetViewPrivate::isNearByCurrentFrameIndicatorBottom(const QPointF &zoomCoords) const
{
return ( currentFrameIndicatorBottomPoly.containsPoint(zoomCoords, Qt::OddEvenFill) );
}
bool
DopeSheetViewPrivate::isNearbySelectedKeysBRectRightEdge(const QPointF& widgetPos) const
{
QPointF topLeft = zoomContext.toWidgetCoordinates(selectedKeysBRect.x1, selectedKeysBRect.y2);
QPointF bottomRight = zoomContext.toWidgetCoordinates(selectedKeysBRect.x2, selectedKeysBRect.y1);
return ( widgetPos.x() >= ( bottomRight.x() ) &&
widgetPos.x() <= (bottomRight.x() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
widgetPos.y() <= (bottomRight.y() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
widgetPos.y() >= (topLeft.y() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) );
}
bool
DopeSheetViewPrivate::isNearbySelectedKeysBRectLeftEdge(const QPointF& widgetPos) const
{
QPointF topLeft = zoomContext.toWidgetCoordinates(selectedKeysBRect.x1, selectedKeysBRect.y2);
QPointF bottomRight = zoomContext.toWidgetCoordinates(selectedKeysBRect.x2, selectedKeysBRect.y1);
return ( widgetPos.x() >= (topLeft.x() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
widgetPos.x() <= ( topLeft.x() ) &&
widgetPos.y() <= (bottomRight.y() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
widgetPos.y() >= (topLeft.y() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) );
}
bool
DopeSheetViewPrivate::isNearbySelectedKeysBRec(const QPointF& widgetPos) const
{
QPointF topLeft = zoomContext.toWidgetCoordinates(selectedKeysBRect.x1, selectedKeysBRect.y2);
QPointF bottomRight = zoomContext.toWidgetCoordinates(selectedKeysBRect.x2, selectedKeysBRect.y1);
return ( widgetPos.x() >= ( topLeft.x() ) &&
widgetPos.x() <= ( bottomRight.x() ) &&
widgetPos.y() <= (bottomRight.y() + DISTANCE_ACCEPTANCE_FROM_READER_EDGE) &&
widgetPos.y() >= (topLeft.y() - DISTANCE_ACCEPTANCE_FROM_READER_EDGE) );
}
std::vector<DopeSheetKey> DopeSheetViewPrivate::isNearByKeyframe(const DSKnobPtr &dsKnob,
const QPointF &widgetCoords) const
{
assert(dsKnob);
std::vector<DopeSheetKey> ret;
KnobIPtr knob = dsKnob->getKnobGui()->getKnob();
int dim = dsKnob->getDimension();
int startDim = 0;
int endDim = knob->getDimension();
if (dim > -1) {
startDim = dim;
endDim = dim + 1;
}
for (int i = startDim; i < endDim; ++i) {
KeyFrameSet keyframes = knob->getCurve(ViewIdx(0), i)->getKeyFrames_mt_safe();
for (KeyFrameSet::const_iterator kIt = keyframes.begin();
kIt != keyframes.end();
++kIt) {
KeyFrame kf = (*kIt);
QPointF keyframeWidgetPos = zoomContext.toWidgetCoordinates(kf.getTime(), 0);
if (std::abs( widgetCoords.x() - keyframeWidgetPos.x() ) < DISTANCE_ACCEPTANCE_FROM_KEYFRAME) {
DSKnobPtr context;
if (dim == -1) {
QTreeWidgetItem *childItem = dsKnob->findDimTreeItem(i);
context = model->mapNameItemToDSKnob(childItem);
} else {
context = dsKnob;
}
ret.push_back( DopeSheetKey(context, kf) );
}
}
}
return ret;
}
std::vector<DopeSheetKey> DopeSheetViewPrivate::isNearByKeyframe(DSNodePtr dsNode,
const QPointF &widgetCoords) const
{
std::vector<DopeSheetKey> ret;
const DSTreeItemKnobMap& dsKnobs = dsNode->getItemKnobMap();
for (DSTreeItemKnobMap::const_iterator it = dsKnobs.begin(); it != dsKnobs.end(); ++it) {
DSKnobPtr dsKnob = (*it).second;
KnobGuiPtr knobGui = dsKnob->getKnobGui();
assert(knobGui);
int dim = dsKnob->getDimension();
if (dim == -1) {
continue;
}
KeyFrameSet keyframes = knobGui->getCurve(ViewIdx(0), dim)->getKeyFrames_mt_safe();
for (KeyFrameSet::const_iterator kIt = keyframes.begin();
kIt != keyframes.end();
++kIt) {
KeyFrame kf = (*kIt);
QPointF keyframeWidgetPos = zoomContext.toWidgetCoordinates(kf.getTime(), 0);
if (std::abs( widgetCoords.x() - keyframeWidgetPos.x() ) < DISTANCE_ACCEPTANCE_FROM_KEYFRAME) {
ret.push_back( DopeSheetKey(dsKnob, kf) );
}
}
}
return ret;
}
double
DopeSheetViewPrivate::clampedMouseOffset(double fromTime,
double toTime)
{
double totalMovement = toTime - fromTime;
// Clamp the motion to the nearet integer
totalMovement = std::floor(totalMovement + 0.5);
double dt = totalMovement - keyDragLastMovement;
// Update the last drag movement
keyDragLastMovement = totalMovement;
return dt;
}
void
DopeSheetViewPrivate::generateKeyframeTextures()
{
QImage kfTexturesImages[KF_TEXTURES_COUNT];
kfTexturesImages[0].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_constant.png") );
kfTexturesImages[1].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_constant_selected.png") );
kfTexturesImages[2].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_linear.png") );
kfTexturesImages[3].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_linear_selected.png") );
kfTexturesImages[4].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve.png") );
kfTexturesImages[5].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_selected.png") );
kfTexturesImages[6].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_break.png") );
kfTexturesImages[7].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_break_selected.png") );
kfTexturesImages[8].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_c.png") );
kfTexturesImages[9].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_c_selected.png") );
kfTexturesImages[10].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_h.png") );
kfTexturesImages[11].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_h_selected.png") );
kfTexturesImages[12].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_r.png") );
kfTexturesImages[13].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_r_selected.png") );
kfTexturesImages[14].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_z.png") );
kfTexturesImages[15].load( QString::fromUtf8(NATRON_IMAGES_PATH "interp_curve_z_selected.png") );
kfTexturesImages[16].load( QString::fromUtf8(NATRON_IMAGES_PATH "keyframe_node_root.png") );
kfTexturesImages[17].load( QString::fromUtf8(NATRON_IMAGES_PATH "keyframe_node_root_selected.png") );
glGenTextures(KF_TEXTURES_COUNT, kfTexturesIDs);
glEnable(GL_TEXTURE_2D);
for (int i = 0; i < KF_TEXTURES_COUNT; ++i) {
if (std::max( kfTexturesImages[i].width(), kfTexturesImages[i].height() ) != KF_PIXMAP_SIZE) {
kfTexturesImages[i] = kfTexturesImages[i].scaled(KF_PIXMAP_SIZE, KF_PIXMAP_SIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
#if QT_VERSION < QT_VERSION_CHECK(5, 4, 0)
kfTexturesImages[i] = QOpenGLWidget::convertToGLFormat(kfTexturesImages[i]);
#endif
glBindTexture(GL_TEXTURE_2D, kfTexturesIDs[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, KF_PIXMAP_SIZE, KF_PIXMAP_SIZE, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, kfTexturesImages[i].bits() );
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
DopeSheetViewPrivate::KeyframeTexture
DopeSheetViewPrivate::kfTextureFromKeyframeType(KeyframeTypeEnum kfType,
bool selected) const
{
DopeSheetViewPrivate::KeyframeTexture ret = DopeSheetViewPrivate::kfTextureNone;
switch (kfType) {
case eKeyframeTypeConstant:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpConstantSelected : DopeSheetViewPrivate::kfTextureInterpConstant;
break;
case eKeyframeTypeLinear:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpLinearSelected : DopeSheetViewPrivate::kfTextureInterpLinear;
break;
case eKeyframeTypeBroken:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpBreakSelected : DopeSheetViewPrivate::kfTextureInterpBreak;
break;
case eKeyframeTypeFree:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpCurveSelected : DopeSheetViewPrivate::kfTextureInterpCurve;
break;
case eKeyframeTypeSmooth:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpCurveZSelected : DopeSheetViewPrivate::kfTextureInterpCurveZ;
break;
case eKeyframeTypeCatmullRom:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpCurveRSelected : DopeSheetViewPrivate::kfTextureInterpCurveR;
break;
case eKeyframeTypeCubic:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpCurveCSelected : DopeSheetViewPrivate::kfTextureInterpCurveC;
break;
case eKeyframeTypeHorizontal:
ret = (selected) ? DopeSheetViewPrivate::kfTextureInterpCurveHSelected : DopeSheetViewPrivate::kfTextureInterpCurveH;
break;
default:
ret = DopeSheetViewPrivate::kfTextureNone;
break;
}
return ret;
}
/**
* @brief DopeSheetViewPrivate::drawScale
*
* Draws the dope sheet's grid and time indicators.
*/
void
DopeSheetViewPrivate::drawScale() const
{
running_in_main_thread_and_context(q_ptr);
QPointF bottomLeft = zoomContext.toZoomCoordinates(0, q_ptr->height() - 1);
QPointF topRight = zoomContext.toZoomCoordinates(q_ptr->width() - 1, 0);
// Don't attempt to draw a scale on a widget with an invalid height
if (q_ptr->height() <= 1) {
return;
}
QFontMetrics fm(*_textFont);
const double smallestTickSizePixel = 5.; // tick size (in pixels) for alpha = 0.
const double largestTickSizePixel = 1000.; // tick size (in pixels) for alpha = 1.
// Retrieve the appropriate settings for drawing
SettingsPtr settings = appPTR->getCurrentSettings();
double scaleR, scaleG, scaleB;
settings->getDopeSheetEditorScaleColor(&scaleR, &scaleG, &scaleB);
QColor scaleColor;
scaleColor.setRgbF( Image::clamp(scaleR, 0., 1.),
Image::clamp(scaleG, 0., 1.),
Image::clamp(scaleB, 0., 1.) );
// Perform drawing
{
GLProtectAttrib a(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
const double rangePixel = q_ptr->width();
const double range_min = bottomLeft.x();
const double range_max = topRight.x();
const double range = range_max - range_min;
double smallTickSize;
bool half_tick;
ticks_size(range_min, range_max, rangePixel, smallestTickSizePixel, &smallTickSize, &half_tick);
// nothing happens in the dopesheet at a scale smaller than 1 frame
if (smallTickSize < 1.) {
smallTickSize = 1;
half_tick = false;
}
int m1, m2;
const int ticks_max = 1000;
double offset;
ticks_bounds(range_min, range_max, smallTickSize, half_tick, ticks_max, &offset, &m1, &m2);
std::vector<int> ticks;
ticks_fill(half_tick, ticks_max, m1, m2, &ticks);
const double smallestTickSize = range * smallestTickSizePixel / rangePixel;
const double largestTickSize = range * largestTickSizePixel / rangePixel;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const double minTickSizeTextPixel = fm.horizontalAdvance( QString::fromUtf8("00") ) / _screenPixelRatio;
#else
const double minTickSizeTextPixel = fm.width( QString::fromUtf8("00") ) / _screenPixelRatio;
#endif
const double minTickSizeText = range * minTickSizeTextPixel / rangePixel;
glCheckError();
for (int i = m1; i <= m2; ++i) {
double value = i * smallTickSize + offset;
const double tickSize = ticks[i - m1] * smallTickSize;
const double alpha = ticks_alpha(smallestTickSize, largestTickSize, tickSize);
glColor4f(scaleColor.redF(), scaleColor.greenF(), scaleColor.blueF(), alpha);
// Draw the vertical lines belonging to the grid
glLineWidth(1. * _screenPixelRatio);
glBegin(GL_LINES);
glVertex2f( value, bottomLeft.y() );
glVertex2f( value, topRight.y() );
glEnd();
glCheckErrorIgnoreOSXBug();
// Draw the time indicators
if (tickSize > minTickSizeText) {
const int tickSizePixel = rangePixel * tickSize / range;
const QString s = QString::number(value);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const int sSizePixel = fm.horizontalAdvance(s) / _screenPixelRatio;
#else
const int sSizePixel = fm.width(s) / _screenPixelRatio;
#endif
if (tickSizePixel > sSizePixel) {
const int sSizeFullPixel = sSizePixel + minTickSizeTextPixel;
double alphaText = 1.0; //alpha;
if (tickSizePixel < sSizeFullPixel) {
// when the text size is between sSizePixel and sSizeFullPixel,
// draw it with a lower alpha
alphaText *= (tickSizePixel - sSizePixel) / (double)minTickSizeTextPixel;
}
QColor c = scaleColor;
c.setAlpha(255 * alphaText);
renderText(value, bottomLeft.y(), s, c, *_textFont, Qt::AlignHCenter);
// Uncomment the line below to draw the indicator on top too
// parent->renderText(value, topRight.y() - 20, s, c, *font);
}
}
}
}
} // DopeSheetViewPrivate::drawScale
/**
* @brief DopeSheetViewPrivate::drawRows
*
*
*
* These rows have the same height as an item from the hierarchy view.
*/