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