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::setCropStart(GenTime pos) {
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;
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);
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());
91 if (type() == AVWIDGET) m_cropStart += diff;
92 m_cropDuration = m_cropDuration - diff;
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;
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());
121 void AbstractClipItem::setFadeOut(int pos, double scale) {
125 void AbstractClipItem::setFadeIn(int pos, double scale) {
129 GenTime AbstractClipItem::duration() const {
130 return m_cropDuration;
133 GenTime AbstractClipItem::startPos() const {
137 void AbstractClipItem::setTrack(int track) {
141 double AbstractClipItem::fps() const {
145 int AbstractClipItem::fadeIn() const {
149 int AbstractClipItem::fadeOut() const {
153 GenTime AbstractClipItem::maxDuration() const {
154 return m_maxDuration;
157 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
158 QPainterPath roundRectPathUpper;
159 double roundingY = 20;
160 double roundingX = 20;
163 while (roundingX > br.width() / 2) {
164 roundingX = roundingX / 2;
165 roundingY = roundingY / 2;
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());
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);
179 return roundRectPathUpper;
182 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
183 QPainterPath roundRectPathLower;
184 double roundingY = 20;
185 double roundingX = 20;
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);
194 while (roundingX > br.width() / 2) {
195 roundingX = roundingX / 2;
196 roundingY = roundingY / 2;
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;
206 QRect AbstractClipItem::visibleRect() {
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();