1 #include "abstractclipitem.h"
3 #include <QGraphicsScene>
4 #include <QGraphicsView>
7 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps): QGraphicsRectItem(rect), m_startFade(0), m_endFade(0), m_track(0), m_fps(fps) {
8 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
10 m_startPos = info.startPos;
11 m_cropDuration = info.endPos - info.startPos;
14 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack) {
15 double origX = rect().x();
16 double origY = rect().y();
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()) {
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;
37 setRect(origX, origY, rect().width(), rect().height());
46 m_startPos = GenTime(x, m_fps);
48 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
49 for (int i = 0; i < childrenList.size(); ++i) {
50 childrenList.at(i)->moveBy(rect().x() - origX , offset);
54 GenTime AbstractClipItem::endPos() const {
55 return m_startPos + m_cropDuration;
58 int AbstractClipItem::track() const {
62 GenTime AbstractClipItem::cropStart() const {
66 void AbstractClipItem::setCropStart(GenTime pos) {
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;
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);
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());
92 if (type() == AVWIDGET) m_cropStart += diff;
93 m_cropDuration = m_cropDuration - diff;
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;
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());
122 void AbstractClipItem::setFadeOut(int pos, double scale) {
126 void AbstractClipItem::setFadeIn(int pos, double scale) {
130 GenTime AbstractClipItem::duration() const {
131 return m_cropDuration;
134 GenTime AbstractClipItem::startPos() const {
138 void AbstractClipItem::setTrack(int track) {
142 double AbstractClipItem::fps() const {
146 int AbstractClipItem::fadeIn() const {
150 int AbstractClipItem::fadeOut() const {
154 GenTime AbstractClipItem::maxDuration() const {
155 return m_maxDuration;
158 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
159 QPainterPath roundRectPathUpper;
160 double roundingY = 20;
161 double roundingX = 20;
164 while (roundingX > br.width() / 2) {
165 roundingX = roundingX / 2;
166 roundingY = roundingY / 2;
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());
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);
180 return roundRectPathUpper;
183 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
184 QPainterPath roundRectPathLower;
185 double roundingY = 20;
186 double roundingX = 20;
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);
195 while (roundingX > br.width() / 2) {
196 roundingX = roundingX / 2;
197 roundingY = roundingY / 2;
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;
207 QRect AbstractClipItem::visibleRect() {
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();