]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Reindent the codebase using 'linux' bracket placement.
[kdenlive] / src / abstractgroupitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de)              *
3  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21 #include "abstractgroupitem.h"
22 #include "abstractclipitem.h"
23 #include "kdenlivesettings.h"
24 #include "customtrackscene.h"
25 #include "customtrackview.h"
26
27 #include <KDebug>
28
29 #include <QPainter>
30 #include <QStyleOptionGraphicsItem>
31 #include <QDomDocument>
32 #include <QMimeData>
33
34
35 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps)
36 {
37     setZValue(2);
38     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
39     setAcceptDrops(true);
40 }
41
42 int AbstractGroupItem::type() const
43 {
44     return GROUPWIDGET;
45 }
46
47 int AbstractGroupItem::track() const
48 {
49     return (int)(scenePos().y() / KdenliveSettings::trackheight());
50 }
51
52 CustomTrackScene* AbstractGroupItem::projectScene()
53 {
54     if (scene()) return static_cast <CustomTrackScene*>(scene());
55     return NULL;
56 }
57
58 QPainterPath AbstractGroupItem::groupShape(QPointF offset)
59 {
60     QPainterPath path;
61     QList<QGraphicsItem *> children = childItems();
62     for (int i = 0; i < children.count(); i++) {
63         if (children.at(i)->type() == AVWIDGET) {
64             QRectF r(children.at(i)->sceneBoundingRect());
65             r.translate(offset);
66             path.addRect(r);
67         }
68     }
69     return path;
70 }
71
72 void AbstractGroupItem::addItem(QGraphicsItem * item)
73 {
74     addToGroup(item);
75     //fixItemRect();
76 }
77
78 void AbstractGroupItem::fixItemRect()
79 {
80     QPointF start = boundingRect().topLeft();
81     if (start != QPointF(0, 0)) {
82         translate(0 - start.x(), 0 - start.y());
83         setPos(start);
84     }
85 }
86
87 /*ItemInfo AbstractGroupItem::info() const {
88     ItemInfo itemInfo;
89     itemInfo.startPos = m_startPos;
90     itemInfo.track = m_track;
91     return itemInfo;
92 }*/
93
94 // virtual
95 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
96 {
97     const double scale = option->matrix.m11();
98     QRect clipRect = option->exposedRect.toRect();
99     clipRect.adjust(0, 0, 1 / scale + 0.5, 1);
100     p->fillRect(option->exposedRect, QColor(100, 100, 200, 100));
101     p->setClipRect(clipRect);
102     QPen pen = p->pen();
103     pen.setColor(QColor(200, 90, 90));
104     pen.setStyle(Qt::DashLine);
105     p->setPen(pen);
106     p->drawRect(boundingRect());
107 }
108
109 //virtual
110 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
111 {
112     if (change == ItemPositionChange && scene()) {
113         // calculate new position.
114         const int trackHeight = KdenliveSettings::trackheight();
115         QPointF start = sceneBoundingRect().topLeft();
116         QPointF newPos = value.toPointF();
117         //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
118         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
119
120         xpos = qMax(xpos, 0);
121         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
122         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
123
124         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
125
126         int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
127         int proposedTrack = newPos.y() / trackHeight;
128
129         int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
130         correctedTrack = qMax(correctedTrack, 0);
131
132         proposedTrack += (correctedTrack - realTrack);
133
134         // Check if top item is a clip or a transition
135         int offset = 0;
136         int topTrack = -1;
137         QList<QGraphicsItem *> children = childItems();
138         for (int i = 0; i < children.count(); i++) {
139             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
140             if (children.at(i)->type() == AVWIDGET) {
141                 if (topTrack == -1 || currentTrack <= topTrack) {
142                     offset = 0;
143                     topTrack = currentTrack;
144                 }
145             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
146                 if (topTrack == -1 || currentTrack < topTrack) {
147                     offset = (int)(trackHeight / 3 * 2 - 1);
148                     topTrack = currentTrack;
149                 }
150             }
151         }
152         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
153         //if (newPos == start) return start;
154
155         /*if (newPos.x() < 0) {
156             // If group goes below 0, adjust position to 0
157             return QPointF(pos().x() - start.x(), pos().y());
158         }*/
159         QPainterPath shape = groupShape(newPos - pos());
160         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
161         for (int i = 0; i < children.count(); i++) {
162             collindingItems.removeAll(children.at(i));
163         }
164
165         if (collindingItems.isEmpty()) return newPos;
166         else {
167             bool forwardMove = xpos > start.x();
168             int offset = 0;
169             for (int i = 0; i < collindingItems.count(); i++) {
170                 QGraphicsItem *collision = collindingItems.at(i);
171                 if (collision->type() == AVWIDGET) {
172                     // Collision
173                     //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
174                     if (newPos.y() != pos().y()) {
175                         // Track change results in collision, restore original position
176                         return pos();
177                     }
178                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
179                     if (forwardMove) {
180                         // Moving forward, determine best pos
181                         QPainterPath clipPath;
182                         clipPath.addRect(item->sceneBoundingRect());
183                         QPainterPath res = shape.intersected(clipPath);
184                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
185                     } else {
186                         // Moving backward, determine best pos
187                         QPainterPath clipPath;
188                         clipPath.addRect(item->sceneBoundingRect());
189                         QPainterPath res = shape.intersected(clipPath);
190                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
191                     }
192                 }
193             }
194             if (offset > 0) {
195                 if (forwardMove) {
196                     newPos.setX(newPos.x() - offset);
197                 } else {
198                     newPos.setX(newPos.x() + offset);
199                 }
200                 // If there is still a collision after our position adjust, restore original pos
201                 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
202                 for (int i = 0; i < children.count(); i++) {
203                     collindingItems.removeAll(children.at(i));
204                 }
205                 for (int i = 0; i < collindingItems.count(); i++)
206                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
207             }
208             return newPos;
209         }
210     }
211     return QGraphicsItemGroup::itemChange(change, value);
212 }
213
214 //virtual
215 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
216 {
217     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
218     QDomDocument doc;
219     doc.setContent(effects, true);
220     QDomElement e = doc.documentElement();
221     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
222     if (view) view->slotAddGroupEffect(e, this);
223 }
224
225 //virtual
226 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
227 {
228     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
229 }
230
231 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
232 {
233     Q_UNUSED(event);
234 }