1 /***************************************************************************
2 * Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de) *
3 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "abstractgroupitem.h"
22 #include "abstractclipitem.h"
23 #include "kdenlivesettings.h"
24 #include "customtrackscene.h"
25 #include "customtrackview.h"
30 #include <QStyleOptionGraphicsItem>
31 #include <QDomDocument>
33 #include <QGraphicsSceneMouseEvent>
36 AbstractGroupItem::AbstractGroupItem(double /* fps */) :
41 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
42 #if QT_VERSION >= 0x040600
43 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
46 m_resizeInfos = QList <ItemInfo>();
49 int AbstractGroupItem::type() const
54 int AbstractGroupItem::track() const
56 return (int)(scenePos().y() / KdenliveSettings::trackheight());
59 void AbstractGroupItem::setItemLocked(bool locked)
64 setFlag(QGraphicsItem::ItemIsMovable, !locked);
65 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
67 foreach (QGraphicsItem *child, childItems())
68 ((AbstractClipItem *)child)->setItemLocked(locked);
71 bool AbstractGroupItem::isItemLocked() const
73 return !(flags() & (QGraphicsItem::ItemIsSelectable));
76 CustomTrackScene* AbstractGroupItem::projectScene()
78 if (scene()) return static_cast <CustomTrackScene*>(scene());
82 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
84 return groupShape(AVWIDGET, offset);
87 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
89 return groupShape(TRANSITIONWIDGET, offset);
92 QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const
95 QList<QGraphicsItem *> children = childItems();
96 for (int i = 0; i < children.count(); i++) {
97 if (children.at(i)->type() == (int)type) {
98 QRectF r(children.at(i)->sceneBoundingRect());
101 } else if (children.at(i)->type() == GROUPWIDGET) {
102 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
103 for (int j = 0; j < subchildren.count(); j++) {
104 if (subchildren.at(j)->type() == (int)type) {
105 QRectF r(subchildren.at(j)->sceneBoundingRect());
115 void AbstractGroupItem::addItem(QGraphicsItem * item)
118 item->setFlag(QGraphicsItem::ItemIsMovable, false);
121 void AbstractGroupItem::removeItem(QGraphicsItem * item)
123 removeFromGroup(item);
126 void AbstractGroupItem::fixItemRect()
128 QPointF start = boundingRect().topLeft();
129 if (start != QPointF(0, 0)) {
130 translate(0 - start.x(), 0 - start.y());
135 /*ItemInfo AbstractGroupItem::info() const {
137 itemInfo.startPos = m_startPos;
138 itemInfo.track = m_track;
143 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
145 QColor bgcolor(100, 100, 200, 100);
146 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
147 p->setClipRect(bound);
148 const QRectF mapped = p->worldTransform().mapRect(option->exposedRect);
149 p->setWorldMatrixEnabled(false);
150 p->setBrush(bgcolor);
152 pen.setColor(QColor(200, 90, 90));
153 pen.setStyle(Qt::DashLine);
156 p->drawRoundedRect(mapped, 3, 3);
160 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
162 if (change == QGraphicsItem::ItemSelectedChange) {
163 if (value.toBool()) setZValue(10);
166 if (change == ItemPositionChange && scene() && parentItem() == 0) {
167 // calculate new position.
168 const int trackHeight = KdenliveSettings::trackheight();
169 QPointF start = sceneBoundingRect().topLeft();
170 QPointF newPos = value.toPointF();
171 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
173 xpos = qMax(xpos, 0);
174 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
175 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
177 int yOffset = property("y_absolute").toInt() + newPos.y();
178 int proposedTrack = yOffset / trackHeight;
180 // Check if top item is a clip or a transition
183 QList<QGraphicsItem *> children = childItems();
184 for (int i = 0; i < children.count(); i++) {
185 int currentTrack = 0;
186 if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
187 else if (children.at(i)->type() == GROUPWIDGET) currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
189 if (children.at(i)->type() == AVWIDGET) {
190 if (topTrack == -1 || currentTrack <= topTrack) {
192 topTrack = currentTrack;
194 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
195 if (topTrack == -1 || currentTrack < topTrack) {
196 offset = (int)(trackHeight / 3 * 2 - 1);
197 topTrack = currentTrack;
199 } else if (children.at(i)->type() == GROUPWIDGET) {
200 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
201 bool clipGroup = false;
202 for (int j = 0; j < subchildren.count(); j++) {
203 if (subchildren.at(j)->type() == AVWIDGET) {
209 if (topTrack == -1 || currentTrack <= topTrack) {
211 topTrack = currentTrack;
214 if (topTrack == -1 || currentTrack < topTrack) {
215 offset = (int)(trackHeight / 3 * 2 - 1);
216 topTrack = currentTrack;
221 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
222 //if (newPos == start) return start;
224 /*if (newPos.x() < 0) {
225 // If group goes below 0, adjust position to 0
226 return QPointF(pos().x() - start.x(), pos().y());
229 QList<QGraphicsItem*> collidingItems;
231 if (projectScene()->editMode() == NORMALEDIT) {
232 shape = clipGroupShape(newPos - pos());
233 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
234 collidingItems.removeAll(this);
235 for (int i = 0; i < children.count(); i++) {
236 if (children.at(i)->type() == GROUPWIDGET) {
237 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
238 for (int j = 0; j < subchildren.count(); j++) {
239 collidingItems.removeAll(subchildren.at(j));
242 collidingItems.removeAll(children.at(i));
245 if (!collidingItems.isEmpty()) {
246 bool forwardMove = xpos > start.x();
248 for (int i = 0; i < collidingItems.count(); i++) {
249 QGraphicsItem *collision = collidingItems.at(i);
250 if (collision->type() == AVWIDGET) {
252 if (newPos.y() != pos().y()) {
253 // Track change results in collision, restore original position
256 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
258 // Moving forward, determine best pos
259 QPainterPath clipPath;
260 clipPath.addRect(item->sceneBoundingRect());
261 QPainterPath res = shape.intersected(clipPath);
262 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
264 // Moving backward, determine best pos
265 QPainterPath clipPath;
266 clipPath.addRect(item->sceneBoundingRect());
267 QPainterPath res = shape.intersected(clipPath);
268 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
274 newPos.setX(newPos.x() - offset);
276 newPos.setX(newPos.x() + offset);
278 // If there is still a collision after our position adjust, restore original pos
279 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
280 collidingItems.removeAll(this);
281 for (int i = 0; i < children.count(); i++) {
282 if (children.at(i)->type() == GROUPWIDGET) {
283 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
284 for (int j = 0; j < subchildren.count(); j++) {
285 collidingItems.removeAll(subchildren.at(j));
288 collidingItems.removeAll(children.at(i));
290 for (int i = 0; i < collidingItems.count(); i++)
291 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
295 if (projectScene()->editMode() == NORMALEDIT) {
296 shape = transitionGroupShape(newPos - pos());
297 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
298 collidingItems.removeAll(this);
299 for (int i = 0; i < children.count(); i++) {
300 if (children.at(i)->type() == GROUPWIDGET) {
301 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
302 for (int j = 0; j < subchildren.count(); j++) {
303 collidingItems.removeAll(subchildren.at(j));
306 collidingItems.removeAll(children.at(i));
309 if (collidingItems.isEmpty()) return newPos;
311 bool forwardMove = xpos > start.x();
313 for (int i = 0; i < collidingItems.count(); i++) {
314 QGraphicsItem *collision = collidingItems.at(i);
315 if (collision->type() == TRANSITIONWIDGET) {
317 if (newPos.y() != pos().y()) {
318 // Track change results in collision, restore original position
321 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
323 // Moving forward, determine best pos
324 QPainterPath clipPath;
325 clipPath.addRect(item->sceneBoundingRect());
326 QPainterPath res = shape.intersected(clipPath);
327 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
329 // Moving backward, determine best pos
330 QPainterPath clipPath;
331 clipPath.addRect(item->sceneBoundingRect());
332 QPainterPath res = shape.intersected(clipPath);
333 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
339 newPos.setX(newPos.x() - offset);
341 newPos.setX(newPos.x() + offset);
343 // If there is still a collision after our position adjust, restore original pos
344 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
345 for (int i = 0; i < children.count(); i++) {
346 collidingItems.removeAll(children.at(i));
348 for (int i = 0; i < collidingItems.count(); i++)
349 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
354 return QGraphicsItemGroup::itemChange(change, value);
358 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
360 QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
362 doc.setContent(effects, true);
363 QDomElement e = doc.documentElement();
364 e.setAttribute("kdenlive_ix", 0);
365 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
366 QPointF dropPos = event->scenePos();
367 QList<QGraphicsItem *> selection = scene()->items(dropPos);
368 AbstractClipItem *dropChild = NULL;
369 for (int i = 0; i < selection.count(); i++) {
370 if (selection.at(i)->type() == AVWIDGET) {
371 dropChild = (AbstractClipItem *) selection.at(i);
375 if (view) view->slotAddGroupEffect(e, this, dropChild);
379 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
381 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
384 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
390 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
392 if (event->modifiers() & Qt::ShiftModifier) {
393 // User want to do a rectangle selection, so ignore the event to pass it to the view
396 QList <QGraphicsItem *>list = scene()->items(event->scenePos());
397 // only allow group move if we click over an item in the group
398 foreach(const QGraphicsItem *item, list) {
399 if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) {
400 QGraphicsItem::mousePressEvent(event);
408 void AbstractGroupItem::resizeStart(int diff)
411 if (m_resizeInfos.isEmpty())
414 QList <QGraphicsItem *> children = childItems();
415 QList <AbstractClipItem *> items;
417 for (int i = 0; i < children.count(); ++i) {
418 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
419 if (item && item->type() == AVWIDGET) {
422 m_resizeInfos << item->info();
423 item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
424 int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
425 if (qAbs(itemdiff) < qAbs(maximum))
431 for (int i = 0; i < items.count(); ++i)
432 items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
435 void AbstractGroupItem::resizeEnd(int diff)
438 if (m_resizeInfos.isEmpty())
441 QList <QGraphicsItem *> children = childItems();
442 QList <AbstractClipItem *> items;
444 for (int i = 0; i < children.count(); ++i) {
445 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
446 if (item && item->type() == AVWIDGET) {
449 m_resizeInfos << item->info();
450 item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
451 int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
452 if (qAbs(itemdiff) < qAbs(maximum))
458 for (int i = 0; i < items.count(); ++i)
459 items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
462 QList< ItemInfo > AbstractGroupItem::resizeInfos()
464 return m_resizeInfos;
467 void AbstractGroupItem::clearResizeInfos()
469 // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
470 m_resizeInfos = QList <ItemInfo>();
473 GenTime AbstractGroupItem::duration()
475 QList <QGraphicsItem *> children = childItems();
476 GenTime start = GenTime(-1.0);
477 GenTime end = GenTime();
478 for (int i = 0; i < children.count(); ++i) {
479 if (children.at(i)->type() != GROUPWIDGET) {
480 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
482 if (start < GenTime() || item->startPos() < start)
483 start = item->startPos();
484 if (item->endPos() > end)
485 end = item->endPos();
488 children << children.at(i)->childItems();