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::fixItemRect()
123 QPointF start = boundingRect().topLeft();
124 if (start != QPointF(0, 0)) {
125 translate(0 - start.x(), 0 - start.y());
130 /*ItemInfo AbstractGroupItem::info() const {
132 itemInfo.startPos = m_startPos;
133 itemInfo.track = m_track;
138 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
140 QColor bgcolor(100, 100, 200, 100);
141 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
142 p->setClipRect(bound);
143 const QRectF mapped = p->worldTransform().mapRect(option->exposedRect);
144 p->setWorldMatrixEnabled(false);
145 p->setBrush(bgcolor);
147 pen.setColor(QColor(200, 90, 90));
148 pen.setStyle(Qt::DashLine);
151 p->drawRoundedRect(mapped, 3, 3);
155 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
157 if (change == QGraphicsItem::ItemSelectedChange) {
158 if (value.toBool()) setZValue(10);
161 if (change == ItemPositionChange && scene() && parentItem() == 0) {
162 // calculate new position.
163 const int trackHeight = KdenliveSettings::trackheight();
164 QPointF start = sceneBoundingRect().topLeft();
165 QPointF newPos = value.toPointF();
166 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
168 xpos = qMax(xpos, 0);
169 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
170 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
172 int yOffset = property("y_absolute").toInt() + newPos.y();
173 int proposedTrack = yOffset / trackHeight;
175 // Check if top item is a clip or a transition
178 QList<QGraphicsItem *> children = childItems();
179 for (int i = 0; i < children.count(); i++) {
180 int currentTrack = 0;
181 if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
182 else if (children.at(i)->type() == GROUPWIDGET) currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
184 if (children.at(i)->type() == AVWIDGET) {
185 if (topTrack == -1 || currentTrack <= topTrack) {
187 topTrack = currentTrack;
189 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
190 if (topTrack == -1 || currentTrack < topTrack) {
191 offset = (int)(trackHeight / 3 * 2 - 1);
192 topTrack = currentTrack;
194 } else if (children.at(i)->type() == GROUPWIDGET) {
195 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
196 bool clipGroup = false;
197 for (int j = 0; j < subchildren.count(); j++) {
198 if (subchildren.at(j)->type() == AVWIDGET) {
204 if (topTrack == -1 || currentTrack <= topTrack) {
206 topTrack = currentTrack;
209 if (topTrack == -1 || currentTrack < topTrack) {
210 offset = (int)(trackHeight / 3 * 2 - 1);
211 topTrack = currentTrack;
216 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
217 //if (newPos == start) return start;
219 /*if (newPos.x() < 0) {
220 // If group goes below 0, adjust position to 0
221 return QPointF(pos().x() - start.x(), pos().y());
224 QList<QGraphicsItem*> collidingItems;
226 if (projectScene()->editMode() == NORMALEDIT) {
227 shape = clipGroupShape(newPos - pos());
228 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
229 collidingItems.removeAll(this);
230 for (int i = 0; i < children.count(); i++) {
231 if (children.at(i)->type() == GROUPWIDGET) {
232 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
233 for (int j = 0; j < subchildren.count(); j++) {
234 collidingItems.removeAll(subchildren.at(j));
237 collidingItems.removeAll(children.at(i));
240 if (!collidingItems.isEmpty()) {
241 bool forwardMove = xpos > start.x();
243 for (int i = 0; i < collidingItems.count(); i++) {
244 QGraphicsItem *collision = collidingItems.at(i);
245 if (collision->type() == AVWIDGET) {
247 if (newPos.y() != pos().y()) {
248 // Track change results in collision, restore original position
251 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
253 // Moving forward, determine best pos
254 QPainterPath clipPath;
255 clipPath.addRect(item->sceneBoundingRect());
256 QPainterPath res = shape.intersected(clipPath);
257 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
259 // Moving backward, determine best pos
260 QPainterPath clipPath;
261 clipPath.addRect(item->sceneBoundingRect());
262 QPainterPath res = shape.intersected(clipPath);
263 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
269 newPos.setX(newPos.x() - offset);
271 newPos.setX(newPos.x() + offset);
273 // If there is still a collision after our position adjust, restore original pos
274 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
275 collidingItems.removeAll(this);
276 for (int i = 0; i < children.count(); i++) {
277 if (children.at(i)->type() == GROUPWIDGET) {
278 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
279 for (int j = 0; j < subchildren.count(); j++) {
280 collidingItems.removeAll(subchildren.at(j));
283 collidingItems.removeAll(children.at(i));
285 for (int i = 0; i < collidingItems.count(); i++)
286 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
290 if (projectScene()->editMode() == NORMALEDIT) {
291 shape = transitionGroupShape(newPos - pos());
292 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
293 collidingItems.removeAll(this);
294 for (int i = 0; i < children.count(); i++) {
295 if (children.at(i)->type() == GROUPWIDGET) {
296 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
297 for (int j = 0; j < subchildren.count(); j++) {
298 collidingItems.removeAll(subchildren.at(j));
301 collidingItems.removeAll(children.at(i));
304 if (collidingItems.isEmpty()) return newPos;
306 bool forwardMove = xpos > start.x();
308 for (int i = 0; i < collidingItems.count(); i++) {
309 QGraphicsItem *collision = collidingItems.at(i);
310 if (collision->type() == TRANSITIONWIDGET) {
312 if (newPos.y() != pos().y()) {
313 // Track change results in collision, restore original position
316 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
318 // Moving forward, determine best pos
319 QPainterPath clipPath;
320 clipPath.addRect(item->sceneBoundingRect());
321 QPainterPath res = shape.intersected(clipPath);
322 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
324 // Moving backward, determine best pos
325 QPainterPath clipPath;
326 clipPath.addRect(item->sceneBoundingRect());
327 QPainterPath res = shape.intersected(clipPath);
328 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
334 newPos.setX(newPos.x() - offset);
336 newPos.setX(newPos.x() + offset);
338 // If there is still a collision after our position adjust, restore original pos
339 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
340 for (int i = 0; i < children.count(); i++) {
341 collidingItems.removeAll(children.at(i));
343 for (int i = 0; i < collidingItems.count(); i++)
344 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
349 return QGraphicsItemGroup::itemChange(change, value);
353 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
355 QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
357 doc.setContent(effects, true);
358 QDomElement e = doc.documentElement();
359 e.setAttribute("kdenlive_ix", 0);
360 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
361 QPointF dropPos = event->scenePos();
362 QList<QGraphicsItem *> selection = scene()->items(dropPos);
363 AbstractClipItem *dropChild = NULL;
364 for (int i = 0; i < selection.count(); i++) {
365 if (selection.at(i)->type() == AVWIDGET) {
366 dropChild = (AbstractClipItem *) selection.at(i);
370 if (view) view->slotAddGroupEffect(e, this, dropChild);
374 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
376 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
379 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
385 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
387 if (event->modifiers() & Qt::ShiftModifier) {
388 // User want to do a rectangle selection, so ignore the event to pass it to the view
390 } else QGraphicsItem::mousePressEvent(event);
393 void AbstractGroupItem::resizeStart(int diff)
396 if (m_resizeInfos.isEmpty())
399 QList <QGraphicsItem *> children = childItems();
400 QList <AbstractClipItem *> items;
402 for (int i = 0; i < children.count(); ++i) {
403 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
404 if (item && item->type() == AVWIDGET) {
407 m_resizeInfos << item->info();
408 item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
409 int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
410 if (qAbs(itemdiff) < qAbs(maximum))
416 for (int i = 0; i < items.count(); ++i)
417 items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
420 void AbstractGroupItem::resizeEnd(int diff)
423 if (m_resizeInfos.isEmpty())
426 QList <QGraphicsItem *> children = childItems();
427 QList <AbstractClipItem *> items;
429 for (int i = 0; i < children.count(); ++i) {
430 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
431 if (item && item->type() == AVWIDGET) {
434 m_resizeInfos << item->info();
435 item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
436 int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
437 if (qAbs(itemdiff) < qAbs(maximum))
443 for (int i = 0; i < items.count(); ++i)
444 items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
447 QList< ItemInfo > AbstractGroupItem::resizeInfos()
449 return m_resizeInfos;
452 void AbstractGroupItem::clearResizeInfos()
454 // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
455 m_resizeInfos = QList <ItemInfo>();
458 GenTime AbstractGroupItem::duration()
460 QList <QGraphicsItem *> children = childItems();
461 GenTime start = GenTime(-1.0);
462 GenTime end = GenTime();
463 for (int i = 0; i < children.count(); ++i) {
464 if (children.at(i)->type() != GROUPWIDGET) {
465 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
467 if (start < GenTime() || item->startPos() < start)
468 start = item->startPos();
469 if (item->endPos() > end)
470 end = item->endPos();
473 children << children.at(i)->childItems();