]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Ensure that all base constructors are explicitly called.
[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) :
36         QObject(),
37         QGraphicsItemGroup(),
38         m_fps(fps)
39 {
40     setZValue(2);
41     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
42     setAcceptDrops(true);
43 }
44
45 int AbstractGroupItem::type() const
46 {
47     return GROUPWIDGET;
48 }
49
50 int AbstractGroupItem::track() const
51 {
52     return (int)(scenePos().y() / KdenliveSettings::trackheight());
53 }
54
55 CustomTrackScene* AbstractGroupItem::projectScene()
56 {
57     if (scene()) return static_cast <CustomTrackScene*>(scene());
58     return NULL;
59 }
60
61 QPainterPath AbstractGroupItem::groupShape(QPointF offset)
62 {
63     QPainterPath path;
64     QList<QGraphicsItem *> children = childItems();
65     for (int i = 0; i < children.count(); i++) {
66         if (children.at(i)->type() == AVWIDGET) {
67             QRectF r(children.at(i)->sceneBoundingRect());
68             r.translate(offset);
69             path.addRect(r);
70         }
71     }
72     return path;
73 }
74
75 void AbstractGroupItem::addItem(QGraphicsItem * item)
76 {
77     addToGroup(item);
78     //fixItemRect();
79 }
80
81 void AbstractGroupItem::fixItemRect()
82 {
83     QPointF start = boundingRect().topLeft();
84     if (start != QPointF(0, 0)) {
85         translate(0 - start.x(), 0 - start.y());
86         setPos(start);
87     }
88 }
89
90 /*ItemInfo AbstractGroupItem::info() const {
91     ItemInfo itemInfo;
92     itemInfo.startPos = m_startPos;
93     itemInfo.track = m_track;
94     return itemInfo;
95 }*/
96
97 // virtual
98 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
99 {
100     const double scale = option->matrix.m11();
101     QRect clipRect = option->exposedRect.toRect();
102     clipRect.adjust(0, 0, 1 / scale + 0.5, 1);
103     p->fillRect(option->exposedRect, QColor(100, 100, 200, 100));
104     p->setClipRect(clipRect);
105     QPen pen = p->pen();
106     pen.setColor(QColor(200, 90, 90));
107     pen.setStyle(Qt::DashLine);
108     p->setPen(pen);
109     p->drawRect(boundingRect());
110 }
111
112 //virtual
113 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
114 {
115     if (change == ItemPositionChange && scene()) {
116         // calculate new position.
117         const int trackHeight = KdenliveSettings::trackheight();
118         QPointF start = sceneBoundingRect().topLeft();
119         QPointF newPos = value.toPointF();
120         //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
121         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
122
123         xpos = qMax(xpos, 0);
124         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
125         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
126
127         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
128
129         int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
130         int proposedTrack = newPos.y() / trackHeight;
131
132         int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
133         correctedTrack = qMax(correctedTrack, 0);
134
135         proposedTrack += (correctedTrack - realTrack);
136
137         // Check if top item is a clip or a transition
138         int offset = 0;
139         int topTrack = -1;
140         QList<QGraphicsItem *> children = childItems();
141         for (int i = 0; i < children.count(); i++) {
142             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
143             if (children.at(i)->type() == AVWIDGET) {
144                 if (topTrack == -1 || currentTrack <= topTrack) {
145                     offset = 0;
146                     topTrack = currentTrack;
147                 }
148             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
149                 if (topTrack == -1 || currentTrack < topTrack) {
150                     offset = (int)(trackHeight / 3 * 2 - 1);
151                     topTrack = currentTrack;
152                 }
153             }
154         }
155         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
156         //if (newPos == start) return start;
157
158         /*if (newPos.x() < 0) {
159             // If group goes below 0, adjust position to 0
160             return QPointF(pos().x() - start.x(), pos().y());
161         }*/
162         QPainterPath shape = groupShape(newPos - pos());
163         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
164         for (int i = 0; i < children.count(); i++) {
165             collindingItems.removeAll(children.at(i));
166         }
167
168         if (collindingItems.isEmpty()) return newPos;
169         else {
170             bool forwardMove = xpos > start.x();
171             int offset = 0;
172             for (int i = 0; i < collindingItems.count(); i++) {
173                 QGraphicsItem *collision = collindingItems.at(i);
174                 if (collision->type() == AVWIDGET) {
175                     // Collision
176                     //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
177                     if (newPos.y() != pos().y()) {
178                         // Track change results in collision, restore original position
179                         return pos();
180                     }
181                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
182                     if (forwardMove) {
183                         // Moving forward, determine best pos
184                         QPainterPath clipPath;
185                         clipPath.addRect(item->sceneBoundingRect());
186                         QPainterPath res = shape.intersected(clipPath);
187                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
188                     } else {
189                         // Moving backward, determine best pos
190                         QPainterPath clipPath;
191                         clipPath.addRect(item->sceneBoundingRect());
192                         QPainterPath res = shape.intersected(clipPath);
193                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
194                     }
195                 }
196             }
197             if (offset > 0) {
198                 if (forwardMove) {
199                     newPos.setX(newPos.x() - offset);
200                 } else {
201                     newPos.setX(newPos.x() + offset);
202                 }
203                 // If there is still a collision after our position adjust, restore original pos
204                 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
205                 for (int i = 0; i < children.count(); i++) {
206                     collindingItems.removeAll(children.at(i));
207                 }
208                 for (int i = 0; i < collindingItems.count(); i++)
209                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
210             }
211             return newPos;
212         }
213     }
214     return QGraphicsItemGroup::itemChange(change, value);
215 }
216
217 //virtual
218 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
219 {
220     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
221     QDomDocument doc;
222     doc.setContent(effects, true);
223     QDomElement e = doc.documentElement();
224     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
225     if (view) view->slotAddGroupEffect(e, this);
226 }
227
228 //virtual
229 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
230 {
231     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
232 }
233
234 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
235 {
236     Q_UNUSED(event);
237 }