1 #include "abstractclipitem.h"
3 #include <QGraphicsScene>
4 #include <QGraphicsView>
7 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps): QGraphicsRectItem(rect), 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 GenTime AbstractClipItem::duration() const {
123 return m_cropDuration;
126 GenTime AbstractClipItem::startPos() const {
130 void AbstractClipItem::setTrack(int track) {
134 double AbstractClipItem::fps() const {
138 GenTime AbstractClipItem::maxDuration() const {
139 return m_maxDuration;
142 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
143 QPainterPath roundRectPathUpper;
144 double roundingY = 20;
145 double roundingX = 20;
148 while (roundingX > br.width() / 2) {
149 roundingX = roundingX / 2;
150 roundingY = roundingY / 2;
152 int br_endx = (int)(br.x() + br .width() - offset);
153 int br_startx = (int)(br.x() + offset);
154 int br_starty = (int)(br.y());
155 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
156 int br_endy = (int)(br.y() + br.height());
158 roundRectPathUpper.moveTo(br_endx , br_halfy);
159 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
160 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
161 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
162 roundRectPathUpper.lineTo(br_startx , br_halfy);
164 return roundRectPathUpper;
167 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
168 QPainterPath roundRectPathLower;
169 double roundingY = 20;
170 double roundingX = 20;
173 int br_endx = (int)(br.x() + br .width() - offset);
174 int br_startx = (int)(br.x() + offset);
175 int br_starty = (int)(br.y());
176 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
177 int br_endy = (int)(br.y() + br.height() - 1);
179 while (roundingX > br.width() / 2) {
180 roundingX = roundingX / 2;
181 roundingY = roundingY / 2;
183 roundRectPathLower.moveTo(br_startx, br_halfy);
184 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
185 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
186 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
187 roundRectPathLower.lineTo(br_endx , br_halfy);
188 return roundRectPathLower;
191 QRect AbstractClipItem::visibleRect() {
193 if (scene()->views().size() > 0) {
194 rectInView = scene()->views()[0]->viewport()->rect();
195 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
196 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
197 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();