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());
58 QList<QGraphicsItem *> children = childItems();
59 for (int i = 0; i < children.count(); ++i) {
60 if (children.at(i)->type() == GROUPWIDGET) {
61 children.append(children.at(i)->childItems());
64 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
65 if (item && (topTrack == -1 || topTrack > item->track())) {
66 topTrack = item->track();
72 void AbstractGroupItem::setItemLocked(bool locked)
77 setFlag(QGraphicsItem::ItemIsMovable, !locked);
78 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
80 foreach (QGraphicsItem *child, childItems())
81 ((AbstractClipItem *)child)->setItemLocked(locked);
84 bool AbstractGroupItem::isItemLocked() const
86 return !(flags() & (QGraphicsItem::ItemIsSelectable));
89 CustomTrackScene* AbstractGroupItem::projectScene()
91 if (scene()) return static_cast <CustomTrackScene*>(scene());
95 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
97 return groupShape(AVWIDGET, offset);
100 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
102 return groupShape(TRANSITIONWIDGET, offset);
105 QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const
108 QList<QGraphicsItem *> children = childItems();
109 for (int i = 0; i < children.count(); i++) {
110 if (children.at(i)->type() == (int)type) {
111 QRectF r(children.at(i)->sceneBoundingRect());
114 } else if (children.at(i)->type() == GROUPWIDGET) {
115 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
116 for (int j = 0; j < subchildren.count(); j++) {
117 if (subchildren.at(j)->type() == (int)type) {
118 QRectF r(subchildren.at(j)->sceneBoundingRect());
128 void AbstractGroupItem::addItem(QGraphicsItem * item)
131 item->setFlag(QGraphicsItem::ItemIsMovable, false);
134 void AbstractGroupItem::removeItem(QGraphicsItem * item)
136 removeFromGroup(item);
139 void AbstractGroupItem::fixItemRect()
141 QPointF start = boundingRect().topLeft();
142 if (start != QPointF(0, 0)) {
143 translate(0 - start.x(), 0 - start.y());
148 /*ItemInfo AbstractGroupItem::info() const {
150 itemInfo.startPos = m_startPos;
151 itemInfo.track = m_track;
156 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
158 QColor bgcolor(100, 100, 200, 100);
159 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
160 p->setClipRect(bound);
161 const QRectF mapped = p->worldTransform().mapRect(option->exposedRect);
162 p->setWorldMatrixEnabled(false);
163 p->setBrush(bgcolor);
165 pen.setColor(QColor(200, 90, 90));
166 pen.setStyle(Qt::DashLine);
169 p->drawRoundedRect(mapped, 3, 3);
173 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
175 if (change == QGraphicsItem::ItemSelectedChange) {
176 if (value.toBool()) setZValue(10);
179 if (change == ItemPositionChange && scene() && parentItem() == 0) {
180 // calculate new position.
181 const int trackHeight = KdenliveSettings::trackheight();
182 QPointF start = sceneBoundingRect().topLeft();
183 QPointF newPos = value.toPointF();
184 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
186 xpos = qMax(xpos, 0);
187 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
188 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
189 QStringList lockedTracks = property("locked_tracks").toStringList();
190 int proposedTrack = (property("y_absolute").toInt() + newPos.y()) / trackHeight;
191 // Check if top item is a clip or a transition
194 QList<int> groupTracks;
195 QList<QGraphicsItem *> children = childItems();
196 for (int i = 0; i < children.count(); i++) {
197 int currentTrack = 0;
198 if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) {
199 currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
200 if (!groupTracks.contains(currentTrack)) groupTracks.append(currentTrack);
202 else if (children.at(i)->type() == GROUPWIDGET) {
203 currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
206 if (children.at(i)->type() == AVWIDGET) {
207 if (topTrack == -1 || currentTrack <= topTrack) {
209 topTrack = currentTrack;
211 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
212 if (topTrack == -1 || currentTrack < topTrack) {
213 offset = (int)(trackHeight / 3 * 2 - 1);
214 topTrack = currentTrack;
216 } else if (children.at(i)->type() == GROUPWIDGET) {
217 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
218 bool clipGroup = false;
219 for (int j = 0; j < subchildren.count(); j++) {
220 if (subchildren.at(j)->type() == AVWIDGET || subchildren.at(j)->type() == TRANSITIONWIDGET) {
221 int subTrack = static_cast <AbstractClipItem*> (subchildren.at(j))->track();
222 if (!groupTracks.contains(subTrack)) groupTracks.append(subTrack);
227 if (topTrack == -1 || currentTrack <= topTrack) {
229 topTrack = currentTrack;
232 if (topTrack == -1 || currentTrack < topTrack) {
233 offset = (int)(trackHeight / 3 * 2 - 1);
234 topTrack = currentTrack;
239 // Check no clip in the group goes outside of existing tracks
240 int maximumTrack = projectScene()->tracksCount() - 1;
242 for (int i = 0; i < groupTracks.count(); i++) {
243 int offset = groupTracks.at(i) - topTrack;
244 if (offset > groupHeight) groupHeight = offset;
246 maximumTrack -= groupHeight;
247 proposedTrack = qMin(proposedTrack, maximumTrack);
248 proposedTrack = qMax(proposedTrack, 0);
249 int groupOffset = proposedTrack - topTrack;
250 if (!lockedTracks.isEmpty()) {
251 for (int i = 0; i < groupTracks.count(); i++) {
252 if (lockedTracks.contains(QString::number(groupTracks.at(i) + groupOffset))) {
257 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
258 //if (newPos == start) return start;
260 /*if (newPos.x() < 0) {
261 // If group goes below 0, adjust position to 0
262 return QPointF(pos().x() - start.x(), pos().y());
265 QList<QGraphicsItem*> collidingItems;
267 if (projectScene()->editMode() == NORMALEDIT) {
268 shape = clipGroupShape(newPos - pos());
269 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
270 collidingItems.removeAll(this);
271 for (int i = 0; i < children.count(); i++) {
272 if (children.at(i)->type() == GROUPWIDGET) {
273 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
274 for (int j = 0; j < subchildren.count(); j++) {
275 collidingItems.removeAll(subchildren.at(j));
278 collidingItems.removeAll(children.at(i));
281 if (!collidingItems.isEmpty()) {
282 bool forwardMove = xpos > start.x();
284 for (int i = 0; i < collidingItems.count(); i++) {
285 QGraphicsItem *collision = collidingItems.at(i);
286 if (collision->type() == AVWIDGET) {
288 if (newPos.y() != pos().y()) {
289 // Track change results in collision, restore original position
292 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
294 // Moving forward, determine best pos
295 QPainterPath clipPath;
296 clipPath.addRect(item->sceneBoundingRect());
297 QPainterPath res = shape.intersected(clipPath);
298 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
300 // Moving backward, determine best pos
301 QPainterPath clipPath;
302 clipPath.addRect(item->sceneBoundingRect());
303 QPainterPath res = shape.intersected(clipPath);
304 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
310 newPos.setX(newPos.x() - offset);
312 newPos.setX(newPos.x() + offset);
314 // If there is still a collision after our position adjust, restore original pos
315 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
316 collidingItems.removeAll(this);
317 for (int i = 0; i < children.count(); i++) {
318 if (children.at(i)->type() == GROUPWIDGET) {
319 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
320 for (int j = 0; j < subchildren.count(); j++) {
321 collidingItems.removeAll(subchildren.at(j));
324 collidingItems.removeAll(children.at(i));
326 for (int i = 0; i < collidingItems.count(); i++)
327 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
331 if (projectScene()->editMode() == NORMALEDIT) {
332 shape = transitionGroupShape(newPos - pos());
333 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
334 collidingItems.removeAll(this);
335 for (int i = 0; i < children.count(); i++) {
336 if (children.at(i)->type() == GROUPWIDGET) {
337 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
338 for (int j = 0; j < subchildren.count(); j++) {
339 collidingItems.removeAll(subchildren.at(j));
342 collidingItems.removeAll(children.at(i));
345 if (collidingItems.isEmpty()) return newPos;
347 bool forwardMove = xpos > start.x();
349 for (int i = 0; i < collidingItems.count(); i++) {
350 QGraphicsItem *collision = collidingItems.at(i);
351 if (collision->type() == TRANSITIONWIDGET) {
353 if (newPos.y() != pos().y()) {
354 // Track change results in collision, restore original position
357 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
359 // Moving forward, determine best pos
360 QPainterPath clipPath;
361 clipPath.addRect(item->sceneBoundingRect());
362 QPainterPath res = shape.intersected(clipPath);
363 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
365 // Moving backward, determine best pos
366 QPainterPath clipPath;
367 clipPath.addRect(item->sceneBoundingRect());
368 QPainterPath res = shape.intersected(clipPath);
369 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
375 newPos.setX(newPos.x() - offset);
377 newPos.setX(newPos.x() + offset);
379 // If there is still a collision after our position adjust, restore original pos
380 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
381 for (int i = 0; i < children.count(); i++) {
382 collidingItems.removeAll(children.at(i));
384 for (int i = 0; i < collidingItems.count(); i++)
385 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
390 return QGraphicsItemGroup::itemChange(change, value);
394 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
396 QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
398 doc.setContent(effects, true);
399 QDomElement e = doc.documentElement();
400 e.setAttribute("kdenlive_ix", 0);
401 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
402 QPointF dropPos = event->scenePos();
403 QList<QGraphicsItem *> selection = scene()->items(dropPos);
404 AbstractClipItem *dropChild = NULL;
405 for (int i = 0; i < selection.count(); i++) {
406 if (selection.at(i)->type() == AVWIDGET) {
407 dropChild = (AbstractClipItem *) selection.at(i);
411 if (view) view->slotAddGroupEffect(e, this, dropChild);
415 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
417 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
420 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
426 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
428 if (event->modifiers() & Qt::ShiftModifier) {
429 // User want to do a rectangle selection, so ignore the event to pass it to the view
432 QList <QGraphicsItem *>list = scene()->items(event->scenePos());
433 // only allow group move if we click over an item in the group
434 foreach(const QGraphicsItem *item, list) {
435 if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) {
436 QGraphicsItem::mousePressEvent(event);
444 void AbstractGroupItem::resizeStart(int diff)
447 if (m_resizeInfos.isEmpty())
450 QList <QGraphicsItem *> children = childItems();
451 QList <AbstractClipItem *> items;
453 for (int i = 0; i < children.count(); ++i) {
454 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
455 if (item && item->type() == AVWIDGET) {
458 m_resizeInfos << item->info();
459 item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
460 int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
461 if (qAbs(itemdiff) < qAbs(maximum))
467 for (int i = 0; i < items.count(); ++i)
468 items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
471 void AbstractGroupItem::resizeEnd(int diff)
474 if (m_resizeInfos.isEmpty())
477 QList <QGraphicsItem *> children = childItems();
478 QList <AbstractClipItem *> items;
480 for (int i = 0; i < children.count(); ++i) {
481 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
482 if (item && item->type() == AVWIDGET) {
485 m_resizeInfos << item->info();
486 item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
487 int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
488 if (qAbs(itemdiff) < qAbs(maximum))
494 for (int i = 0; i < items.count(); ++i)
495 items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
498 QList< ItemInfo > AbstractGroupItem::resizeInfos()
500 return m_resizeInfos;
503 void AbstractGroupItem::clearResizeInfos()
505 // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
506 m_resizeInfos = QList <ItemInfo>();
509 GenTime AbstractGroupItem::duration()
511 QList <QGraphicsItem *> children = childItems();
512 GenTime start = GenTime(-1.0);
513 GenTime end = GenTime();
514 for (int i = 0; i < children.count(); ++i) {
515 if (children.at(i)->type() != GROUPWIDGET) {
516 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
518 if (start < GenTime() || item->startPos() < start)
519 start = item->startPos();
520 if (item->endPos() > end)
521 end = item->endPos();
524 children << children.at(i)->childItems();