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