1 #include "abstractclipitem.h"
3 #include <QGraphicsScene>
4 #include <QGraphicsView>
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);
10 m_startPos = info.startPos;
11 m_cropDuration = info.endPos - info.startPos;
13 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack) {
14 double origX = rect().x();
15 double origY = rect().y();
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()) {
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;
31 kDebug() << "COLLISION, MOVING TO+++";
32 m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
33 origX = m_startPos.frames(m_fps) * scale;
36 setRect(origX, origY, rect().width(), rect().height());
45 m_startPos = GenTime(x, m_fps);
47 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
48 for (int i = 0; i < childrenList.size(); ++i) {
49 childrenList.at(i)->moveBy(rect().x() - origX , offset);
53 GenTime AbstractClipItem::endPos() const {
54 return m_startPos + m_cropDuration;
57 int AbstractClipItem::track() const {
61 GenTime AbstractClipItem::cropStart() const {
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;
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);
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());
87 if (type() == AVWIDGET) m_cropStart += diff;
88 m_cropDuration = m_cropDuration - diff;
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;
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());
117 void AbstractClipItem::setFadeOut(int pos, double scale) {
121 void AbstractClipItem::setFadeIn(int pos, double scale) {
125 GenTime AbstractClipItem::duration() const {
126 return m_cropDuration;
129 GenTime AbstractClipItem::startPos() const {
133 void AbstractClipItem::setTrack(int track) {
137 double AbstractClipItem::fps() const {
141 int AbstractClipItem::fadeIn() const {
145 int AbstractClipItem::fadeOut() const {
149 GenTime AbstractClipItem::maxDuration() const {
150 return m_maxDuration;
153 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
154 QPainterPath roundRectPathUpper;
155 double roundingY = 20;
156 double roundingX = 20;
159 while (roundingX > br.width() / 2) {
160 roundingX = roundingX / 2;
161 roundingY = roundingY / 2;
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());
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);
175 return roundRectPathUpper;
178 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
179 QPainterPath roundRectPathLower;
180 double roundingY = 20;
181 double roundingX = 20;
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);
190 while (roundingX > br.width() / 2) {
191 roundingX = roundingX / 2;
192 roundingY = roundingY / 2;
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;
202 QRect AbstractClipItem::visibleRect() {
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();