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