]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Fix timeline scrollbar and clips disappearing on drop
[kdenlive] / src / abstractclipitem.cpp
1 #include "abstractclipitem.h"
2 #include <KDebug>
3 #include <QGraphicsScene>
4 #include <QGraphicsView>
5 #include <QScrollBar>
6
7 AbstractClipItem::AbstractClipItem(const QRectF& rect): QGraphicsRectItem(rect), m_startFade(0), m_endFade(0) {
8     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
9 }
10 void AbstractClipItem::moveTo(int x, double scale, double offset, int newTrack) {
11     double origX = rect().x();
12     double origY = rect().y();
13     bool success = true;
14
15     if (x < 0) return;
16     setRect(x * scale, origY + offset, rect().width(), rect().height());
17     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
18     if (collisionList.size() == 0) m_track = newTrack;
19     for (int i = 0; i < collisionList.size(); ++i) {
20         QGraphicsItem *item = collisionList.at(i);
21         if (item->type() == type()) {
22             if (offset == 0) {
23                 QRectF other = ((QGraphicsRectItem *)item)->rect();
24                 if (x < m_startPos.frames(m_fps)) {
25                     kDebug() << "COLLISION, MOVING TO------";
26                     m_startPos = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps);
27                     origX = m_startPos.frames(m_fps) * scale;
28                 } else {
29                     kDebug() << "COLLISION, MOVING TO+++";
30                     m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
31                     origX = m_startPos.frames(m_fps) * scale;
32                 }
33             }
34             setRect(origX, origY, rect().width(), rect().height());
35             offset = 0;
36             origX = rect().x();
37             success = false;
38             break;
39         }
40     }
41     if (success) {
42         m_track = newTrack;
43         m_startPos = GenTime(x, m_fps);
44     }
45     /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
46         for (int i = 0; i < childrenList.size(); ++i) {
47           childrenList.at(i)->moveBy(rect().x() - origX , offset);
48         }*/
49 }
50
51 GenTime AbstractClipItem::endPos() const {
52     return m_startPos + m_cropDuration;
53 }
54
55 int AbstractClipItem::track() const {
56     return m_track;
57 }
58
59 GenTime AbstractClipItem::cropStart() const {
60     return m_cropStart;
61 }
62
63 void AbstractClipItem::resizeStart(int posx, double scale) {
64     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
65     if (durationDiff == GenTime()) return;
66     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
67
68     if (m_cropStart + durationDiff < GenTime() && type() == AVWIDGET) {
69         durationDiff = GenTime() - m_cropStart;
70     } else if (durationDiff >= m_cropDuration) {
71         durationDiff = m_cropDuration - GenTime(3, m_fps);
72     }
73
74     m_startPos += durationDiff;
75     m_cropStart += durationDiff;
76     m_cropDuration = m_cropDuration - durationDiff;
77     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
78     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
79     for (int i = 0; i < collisionList.size(); ++i) {
80         QGraphicsItem *item = collisionList.at(i);
81         if (item->type() == type()) {
82             GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
83             setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
84             m_startPos += diff;
85             m_cropStart += diff;
86             m_cropDuration = m_cropDuration - diff;
87             break;
88         }
89     }
90 }
91
92 void AbstractClipItem::resizeEnd(int posx, double scale) {
93     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
94     if (durationDiff == GenTime()) return;
95     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
96     if (m_cropDuration + durationDiff <= GenTime()) {
97         durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
98     } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
99         durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
100     }
101     m_cropDuration += durationDiff;
102     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
103     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
104     for (int i = 0; i < collisionList.size(); ++i) {
105         QGraphicsItem *item = collisionList.at(i);
106         if (item->type() == type()) {
107             GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
108             m_cropDuration = diff;
109             setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
110             break;
111         }
112     }
113 }
114
115 void AbstractClipItem::setFadeOut(int pos, double scale) {
116
117 }
118
119 void AbstractClipItem::setFadeIn(int pos, double scale) {
120
121 }
122
123 GenTime AbstractClipItem::duration() const {
124     return m_cropDuration;
125 }
126
127 GenTime AbstractClipItem::startPos() const {
128     return m_startPos;
129 }
130
131 void AbstractClipItem::setTrack(int track) {
132     m_track = track;
133 }
134
135 double AbstractClipItem::fps() const {
136     return m_fps;
137 }
138
139 int AbstractClipItem::fadeIn() const {
140     return m_startFade;
141 }
142
143 int AbstractClipItem::fadeOut() const {
144     return m_endFade;
145 }
146
147 GenTime AbstractClipItem::maxDuration() const {
148     return m_maxDuration;
149 }
150
151 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
152     QPainterPath roundRectPathUpper;
153     double roundingY = 20;
154     double roundingX = 20;
155     double offset = 1;
156
157     while (roundingX > br.width() / 2) {
158         roundingX = roundingX / 2;
159         roundingY = roundingY / 2;
160     }
161     int br_endx = (int)(br.x() + br .width() - offset);
162     int br_startx = (int)(br.x() + offset);
163     int br_starty = (int)(br.y());
164     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
165     int br_endy = (int)(br.y() + br.height());
166
167     roundRectPathUpper.moveTo(br_endx  , br_halfy);
168     roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
169     roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
170     roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
171     roundRectPathUpper.lineTo(br_startx , br_halfy);
172
173     return roundRectPathUpper;
174 }
175
176 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
177     QPainterPath roundRectPathLower;
178     double roundingY = 20;
179     double roundingX = 20;
180     double offset = 1;
181
182     int br_endx = (int)(br.x() + br .width() - offset);
183     int br_startx = (int)(br.x() + offset);
184     int br_starty = (int)(br.y());
185     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
186     int br_endy = (int)(br.y() + br.height() - 1);
187
188     while (roundingX > br.width() / 2) {
189         roundingX = roundingX / 2;
190         roundingY = roundingY / 2;
191     }
192     roundRectPathLower.moveTo(br_startx, br_halfy);
193     roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
194     roundRectPathLower.lineTo(br_endx - roundingX  , br_endy);
195     roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
196     roundRectPathLower.lineTo(br_endx  , br_halfy);
197     return roundRectPathLower;
198 }
199
200 QRect AbstractClipItem::visibleRect() {
201     QRect rectInView;
202     if (scene()->views().size() > 0) {
203         rectInView = scene()->views()[0]->viewport()->rect();
204         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
205         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
206         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
207     }
208     return rectInView;
209 }