]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Start of tools:
[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 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::setCropStart(GenTime pos) {
66     m_cropStart = pos;
67 }
68
69 void AbstractClipItem::resizeStart(int posx, double scale) {
70     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
71     if (durationDiff == GenTime()) return;
72     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
73
74     if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
75         durationDiff = GenTime() - m_cropStart;
76     } else if (durationDiff >= m_cropDuration) {
77         durationDiff = m_cropDuration - GenTime(3, m_fps);
78     }
79
80     m_startPos += durationDiff;
81     if (type() == AVWIDGET) m_cropStart += durationDiff;
82     m_cropDuration = m_cropDuration - durationDiff;
83     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
84     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
85     for (int i = 0; i < collisionList.size(); ++i) {
86         QGraphicsItem *item = collisionList.at(i);
87         if (item->type() == type()) {
88             GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
89             setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
90             m_startPos += diff;
91             if (type() == AVWIDGET) m_cropStart += diff;
92             m_cropDuration = m_cropDuration - diff;
93             break;
94         }
95     }
96 }
97
98 void AbstractClipItem::resizeEnd(int posx, double scale) {
99     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
100     if (durationDiff == GenTime()) return;
101     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
102     if (m_cropDuration + durationDiff <= GenTime()) {
103         durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
104     } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
105         durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
106     }
107     m_cropDuration += durationDiff;
108     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
109     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
110     for (int i = 0; i < collisionList.size(); ++i) {
111         QGraphicsItem *item = collisionList.at(i);
112         if (item->type() == type()) {
113             GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
114             m_cropDuration = diff;
115             setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
116             break;
117         }
118     }
119 }
120
121 void AbstractClipItem::setFadeOut(int pos, double scale) {
122
123 }
124
125 void AbstractClipItem::setFadeIn(int pos, double scale) {
126
127 }
128
129 GenTime AbstractClipItem::duration() const {
130     return m_cropDuration;
131 }
132
133 GenTime AbstractClipItem::startPos() const {
134     return m_startPos;
135 }
136
137 void AbstractClipItem::setTrack(int track) {
138     m_track = track;
139 }
140
141 double AbstractClipItem::fps() const {
142     return m_fps;
143 }
144
145 int AbstractClipItem::fadeIn() const {
146     return m_startFade;
147 }
148
149 int AbstractClipItem::fadeOut() const {
150     return m_endFade;
151 }
152
153 GenTime AbstractClipItem::maxDuration() const {
154     return m_maxDuration;
155 }
156
157 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
158     QPainterPath roundRectPathUpper;
159     double roundingY = 20;
160     double roundingX = 20;
161     double offset = 1;
162
163     while (roundingX > br.width() / 2) {
164         roundingX = roundingX / 2;
165         roundingY = roundingY / 2;
166     }
167     int br_endx = (int)(br.x() + br .width() - offset);
168     int br_startx = (int)(br.x() + offset);
169     int br_starty = (int)(br.y());
170     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
171     int br_endy = (int)(br.y() + br.height());
172
173     roundRectPathUpper.moveTo(br_endx  , br_halfy);
174     roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
175     roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
176     roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
177     roundRectPathUpper.lineTo(br_startx , br_halfy);
178
179     return roundRectPathUpper;
180 }
181
182 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
183     QPainterPath roundRectPathLower;
184     double roundingY = 20;
185     double roundingX = 20;
186     double offset = 1;
187
188     int br_endx = (int)(br.x() + br .width() - offset);
189     int br_startx = (int)(br.x() + offset);
190     int br_starty = (int)(br.y());
191     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
192     int br_endy = (int)(br.y() + br.height() - 1);
193
194     while (roundingX > br.width() / 2) {
195         roundingX = roundingX / 2;
196         roundingY = roundingY / 2;
197     }
198     roundRectPathLower.moveTo(br_startx, br_halfy);
199     roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
200     roundRectPathLower.lineTo(br_endx - roundingX  , br_endy);
201     roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
202     roundRectPathLower.lineTo(br_endx  , br_halfy);
203     return roundRectPathLower;
204 }
205
206 QRect AbstractClipItem::visibleRect() {
207     QRect rectInView;
208     if (scene()->views().size() > 0) {
209         rectInView = scene()->views()[0]->viewport()->rect();
210         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
211         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
212         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
213     }
214     return rectInView;
215 }