]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
reindent + nice (working) progress bar for document loading
[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 #include <QGraphicsSceneMouseEvent>
34
35 AbstractGroupItem::AbstractGroupItem(double /* fps */) :
36         QObject(),
37         QGraphicsItemGroup()
38 {
39     setZValue(1);
40     setFlags(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::clipGroupShape(QPointF offset) const
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 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
75 {
76     QPainterPath path;
77     QList<QGraphicsItem *> children = childItems();
78     for (int i = 0; i < children.count(); i++) {
79         if (children.at(i)->type() == TRANSITIONWIDGET) {
80             QRectF r(children.at(i)->sceneBoundingRect());
81             r.translate(offset);
82             path.addRect(r);
83         }
84     }
85     return path;
86 }
87
88 void AbstractGroupItem::addItem(QGraphicsItem * item)
89 {
90     addToGroup(item);
91     //fixItemRect();
92 }
93
94 void AbstractGroupItem::fixItemRect()
95 {
96     QPointF start = boundingRect().topLeft();
97     if (start != QPointF(0, 0)) {
98         translate(0 - start.x(), 0 - start.y());
99         setPos(start);
100     }
101 }
102
103 /*ItemInfo AbstractGroupItem::info() const {
104     ItemInfo itemInfo;
105     itemInfo.startPos = m_startPos;
106     itemInfo.track = m_track;
107     return itemInfo;
108 }*/
109
110 // virtual
111 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
112 {
113     const double scale = option->matrix.m11();
114     QColor bgcolor(100, 100, 200, 100);
115     QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
116     p->setClipRect(bound);
117     p->fillRect(option->exposedRect, bgcolor);
118     QPen pen = p->pen();
119     pen.setColor(QColor(200, 90, 90));
120     pen.setStyle(Qt::DashLine);
121     pen.setWidthF(0.0);
122     //pen.setCosmetic(true);
123     p->setPen(pen);
124     p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
125 }
126
127 //virtual
128 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
129 {
130     if (change == ItemPositionChange && scene() && parentItem() == 0) {
131         // calculate new position.
132         const int trackHeight = KdenliveSettings::trackheight();
133         QPointF start = sceneBoundingRect().topLeft();
134         QPointF newPos = value.toPointF();
135         //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
136         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
137
138         xpos = qMax(xpos, 0);
139         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
140         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
141
142         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
143
144         int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
145         int proposedTrack = newPos.y() / trackHeight;
146
147         int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
148         correctedTrack = qMax(correctedTrack, 0);
149
150         proposedTrack += (correctedTrack - realTrack);
151
152         // Check if top item is a clip or a transition
153         int offset = 0;
154         int topTrack = -1;
155         QList<QGraphicsItem *> children = childItems();
156         for (int i = 0; i < children.count(); i++) {
157             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
158             if (children.at(i)->type() == AVWIDGET) {
159                 if (topTrack == -1 || currentTrack <= topTrack) {
160                     offset = 0;
161                     topTrack = currentTrack;
162                 }
163             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
164                 if (topTrack == -1 || currentTrack < topTrack) {
165                     offset = (int)(trackHeight / 3 * 2 - 1);
166                     topTrack = currentTrack;
167                 }
168             } else if (children.at(i)->type() == GROUPWIDGET) {
169                 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
170                 bool clipGroup = false;
171                 for (int j = 0; j < subchildren.count(); j++) {
172                     if (subchildren.at(j)->type() == AVWIDGET) {
173                         clipGroup = true;
174                         break;
175                     }
176                 }
177                 if (clipGroup) {
178                     if (topTrack == -1 || currentTrack <= topTrack) {
179                         offset = 0;
180                         topTrack = currentTrack;
181                     }
182                 } else {
183                     if (topTrack == -1 || currentTrack < topTrack) {
184                         offset = (int)(trackHeight / 3 * 2 - 1);
185                         topTrack = currentTrack;
186                     }
187                 }
188             }
189         }
190         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
191         //if (newPos == start) return start;
192
193         /*if (newPos.x() < 0) {
194             // If group goes below 0, adjust position to 0
195             return QPointF(pos().x() - start.x(), pos().y());
196         }*/
197
198         QPainterPath shape = clipGroupShape(newPos - pos());
199         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
200         for (int i = 0; i < children.count(); i++) {
201             collindingItems.removeAll(children.at(i));
202         }
203
204         if (!collindingItems.isEmpty()) {
205             bool forwardMove = xpos > start.x();
206             int offset = 0;
207             for (int i = 0; i < collindingItems.count(); i++) {
208                 QGraphicsItem *collision = collindingItems.at(i);
209                 if (collision->type() == AVWIDGET) {
210                     // Collision
211                     if (newPos.y() != pos().y()) {
212                         // Track change results in collision, restore original position
213                         return pos();
214                     }
215                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
216                     if (forwardMove) {
217                         // Moving forward, determine best pos
218                         QPainterPath clipPath;
219                         clipPath.addRect(item->sceneBoundingRect());
220                         QPainterPath res = shape.intersected(clipPath);
221                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
222                     } else {
223                         // Moving backward, determine best pos
224                         QPainterPath clipPath;
225                         clipPath.addRect(item->sceneBoundingRect());
226                         QPainterPath res = shape.intersected(clipPath);
227                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
228                     }
229                 }
230             }
231             if (offset > 0) {
232                 if (forwardMove) {
233                     newPos.setX(newPos.x() - offset);
234                 } else {
235                     newPos.setX(newPos.x() + offset);
236                 }
237                 // If there is still a collision after our position adjust, restore original pos
238                 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
239                 for (int i = 0; i < children.count(); i++) {
240                     collindingItems.removeAll(children.at(i));
241                 }
242                 for (int i = 0; i < collindingItems.count(); i++)
243                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
244             }
245         }
246
247
248         shape = transitionGroupShape(newPos - pos());
249         collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
250         for (int i = 0; i < children.count(); i++) {
251             collindingItems.removeAll(children.at(i));
252         }
253         if (collindingItems.isEmpty()) return newPos;
254         else {
255             bool forwardMove = xpos > start.x();
256             int offset = 0;
257             for (int i = 0; i < collindingItems.count(); i++) {
258                 QGraphicsItem *collision = collindingItems.at(i);
259                 if (collision->type() == TRANSITIONWIDGET) {
260                     // Collision
261                     if (newPos.y() != pos().y()) {
262                         // Track change results in collision, restore original position
263                         return pos();
264                     }
265                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
266                     if (forwardMove) {
267                         // Moving forward, determine best pos
268                         QPainterPath clipPath;
269                         clipPath.addRect(item->sceneBoundingRect());
270                         QPainterPath res = shape.intersected(clipPath);
271                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
272                     } else {
273                         // Moving backward, determine best pos
274                         QPainterPath clipPath;
275                         clipPath.addRect(item->sceneBoundingRect());
276                         QPainterPath res = shape.intersected(clipPath);
277                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
278                     }
279                 }
280             }
281             if (offset > 0) {
282                 if (forwardMove) {
283                     newPos.setX(newPos.x() - offset);
284                 } else {
285                     newPos.setX(newPos.x() + offset);
286                 }
287                 // If there is still a collision after our position adjust, restore original pos
288                 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
289                 for (int i = 0; i < children.count(); i++) {
290                     collindingItems.removeAll(children.at(i));
291                 }
292                 for (int i = 0; i < collindingItems.count(); i++)
293                     if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
294             }
295         }
296         return newPos;
297     }
298     return QGraphicsItemGroup::itemChange(change, value);
299 }
300
301 //virtual
302 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
303 {
304     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
305     QDomDocument doc;
306     doc.setContent(effects, true);
307     QDomElement e = doc.documentElement();
308     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
309     if (view) view->slotAddGroupEffect(e, this);
310 }
311
312 //virtual
313 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
314 {
315     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
316 }
317
318 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
319 {
320     Q_UNUSED(event);
321 }
322
323 // virtual
324 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
325 {
326     if (event->modifiers() & Qt::ShiftModifier) {
327         // User want to do a rectangle selection, so ignore the event to pass it to the view
328         event->ignore();
329     } else QGraphicsItem::mousePressEvent(event);
330 }