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