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::clipGroupSpacerShape(QPointF offset) const
97 return spacerGroupShape(AVWIDGET, offset);
100 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
102 return groupShape(AVWIDGET, offset);
105 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
107 return groupShape(TRANSITIONWIDGET, offset);
110 QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const
113 QList<QGraphicsItem *> children = childItems();
114 for (int i = 0; i < children.count(); i++) {
115 if (children.at(i)->type() == (int)type) {
116 QRectF r(children.at(i)->sceneBoundingRect());
119 } else if (children.at(i)->type() == GROUPWIDGET) {
120 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
121 for (int j = 0; j < subchildren.count(); j++) {
122 if (subchildren.at(j)->type() == (int)type) {
123 QRectF r(subchildren.at(j)->sceneBoundingRect());
133 QPainterPath AbstractGroupItem::spacerGroupShape(GRAPHICSRECTITEM type, QPointF offset) const
136 QList<QGraphicsItem *> children = childItems();
137 for (int i = 0; i < children.count(); i++) {
138 if (children.at(i)->type() == (int)type) {
139 QRectF r(children.at(i)->sceneBoundingRect());
141 r.setRight(scene()->width());
143 } else if (children.at(i)->type() == GROUPWIDGET) {
144 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
145 for (int j = 0; j < subchildren.count(); j++) {
146 if (subchildren.at(j)->type() == (int)type) {
147 QRectF r(subchildren.at(j)->sceneBoundingRect());
149 r.setRight(scene()->width());
158 void AbstractGroupItem::addItem(QGraphicsItem * item)
161 item->setFlag(QGraphicsItem::ItemIsMovable, false);
164 void AbstractGroupItem::removeItem(QGraphicsItem * item)
166 removeFromGroup(item);
169 void AbstractGroupItem::fixItemRect()
171 QPointF start = boundingRect().topLeft();
172 if (start != QPointF(0, 0)) {
173 translate(0 - start.x(), 0 - start.y());
178 /*ItemInfo AbstractGroupItem::info() const {
180 itemInfo.startPos = m_startPos;
181 itemInfo.track = m_track;
186 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
188 QColor bgcolor(100, 100, 200, 100);
189 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
190 p->setClipRect(bound);
191 p->setBrush(bgcolor);
193 pen.setColor(QColor(200, 90, 90));
194 pen.setStyle(Qt::DashLine);
197 p->drawRoundedRect(boundingRect().adjusted(0, 0, -1, 0), 3, 3);
201 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
203 if (change == QGraphicsItem::ItemSelectedChange) {
204 if (value.toBool()) setZValue(10);
207 if (change == ItemPositionChange && scene() && parentItem() == 0) {
208 // calculate new position.
209 const int trackHeight = KdenliveSettings::trackheight();
210 QPointF start = sceneBoundingRect().topLeft();
211 QPointF newPos = value.toPointF();
212 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
214 xpos = qMax(xpos, 0);
215 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
216 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
217 QStringList lockedTracks = property("locked_tracks").toStringList();
218 int proposedTrack = (property("y_absolute").toInt() + newPos.y()) / trackHeight;
219 // Check if top item is a clip or a transition
222 QList<int> groupTracks;
223 QList<QGraphicsItem *> children = childItems();
224 for (int i = 0; i < children.count(); i++) {
225 int currentTrack = 0;
226 if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) {
227 currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
228 if (!groupTracks.contains(currentTrack)) groupTracks.append(currentTrack);
230 else if (children.at(i)->type() == GROUPWIDGET) {
231 currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
234 if (children.at(i)->type() == AVWIDGET) {
235 if (topTrack == -1 || currentTrack <= topTrack) {
237 topTrack = currentTrack;
239 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
240 if (topTrack == -1 || currentTrack < topTrack) {
241 offset = (int)(trackHeight / 3 * 2 - 1);
242 topTrack = currentTrack;
244 } else if (children.at(i)->type() == GROUPWIDGET) {
245 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
246 bool clipGroup = false;
247 for (int j = 0; j < subchildren.count(); j++) {
248 if (subchildren.at(j)->type() == AVWIDGET || subchildren.at(j)->type() == TRANSITIONWIDGET) {
249 int subTrack = static_cast <AbstractClipItem*> (subchildren.at(j))->track();
250 if (!groupTracks.contains(subTrack)) groupTracks.append(subTrack);
255 if (topTrack == -1 || currentTrack <= topTrack) {
257 topTrack = currentTrack;
260 if (topTrack == -1 || currentTrack < topTrack) {
261 offset = (int)(trackHeight / 3 * 2 - 1);
262 topTrack = currentTrack;
267 // Check no clip in the group goes outside of existing tracks
268 int maximumTrack = projectScene()->tracksCount() - 1;
270 for (int i = 0; i < groupTracks.count(); i++) {
271 int offset = groupTracks.at(i) - topTrack;
272 if (offset > groupHeight) groupHeight = offset;
274 maximumTrack -= groupHeight;
275 proposedTrack = qMin(proposedTrack, maximumTrack);
276 proposedTrack = qMax(proposedTrack, 0);
277 int groupOffset = proposedTrack - topTrack;
278 if (!lockedTracks.isEmpty()) {
279 for (int i = 0; i < groupTracks.count(); i++) {
280 if (lockedTracks.contains(QString::number(groupTracks.at(i) + groupOffset))) {
285 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
286 //if (newPos == start) return start;
288 /*if (newPos.x() < 0) {
289 // If group goes below 0, adjust position to 0
290 return QPointF(pos().x() - start.x(), pos().y());
293 QList<QGraphicsItem*> collidingItems;
295 if (projectScene()->editMode() == NORMALEDIT) {
296 shape = clipGroupShape(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()) {
310 bool forwardMove = xpos > start.x();
312 for (int i = 0; i < collidingItems.count(); i++) {
313 QGraphicsItem *collision = collidingItems.at(i);
314 if (collision->type() == AVWIDGET) {
316 if (newPos.y() != pos().y()) {
317 // Track change results in collision, restore original position
320 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
322 // Moving forward, determine best pos
323 QPainterPath clipPath;
324 clipPath.addRect(item->sceneBoundingRect());
325 QPainterPath res = shape.intersected(clipPath);
326 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
328 // Moving backward, determine best pos
329 QPainterPath clipPath;
330 clipPath.addRect(item->sceneBoundingRect());
331 QPainterPath res = shape.intersected(clipPath);
332 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
338 newPos.setX(newPos.x() - offset);
340 newPos.setX(newPos.x() + offset);
342 // If there is still a collision after our position adjust, restore original pos
343 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
344 collidingItems.removeAll(this);
345 for (int i = 0; i < children.count(); i++) {
346 if (children.at(i)->type() == GROUPWIDGET) {
347 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
348 for (int j = 0; j < subchildren.count(); j++) {
349 collidingItems.removeAll(subchildren.at(j));
352 collidingItems.removeAll(children.at(i));
354 for (int i = 0; i < collidingItems.count(); i++)
355 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
359 if (projectScene()->editMode() == NORMALEDIT) {
360 shape = transitionGroupShape(newPos - pos());
361 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
362 collidingItems.removeAll(this);
363 for (int i = 0; i < children.count(); i++) {
364 if (children.at(i)->type() == GROUPWIDGET) {
365 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
366 for (int j = 0; j < subchildren.count(); j++) {
367 collidingItems.removeAll(subchildren.at(j));
370 collidingItems.removeAll(children.at(i));
373 if (collidingItems.isEmpty()) return newPos;
375 bool forwardMove = xpos > start.x();
377 for (int i = 0; i < collidingItems.count(); i++) {
378 QGraphicsItem *collision = collidingItems.at(i);
379 if (collision->type() == TRANSITIONWIDGET) {
381 if (newPos.y() != pos().y()) {
382 // Track change results in collision, restore original position
385 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
387 // Moving forward, determine best pos
388 QPainterPath clipPath;
389 clipPath.addRect(item->sceneBoundingRect());
390 QPainterPath res = shape.intersected(clipPath);
391 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
393 // Moving backward, determine best pos
394 QPainterPath clipPath;
395 clipPath.addRect(item->sceneBoundingRect());
396 QPainterPath res = shape.intersected(clipPath);
397 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
403 newPos.setX(newPos.x() - offset);
405 newPos.setX(newPos.x() + offset);
407 // If there is still a collision after our position adjust, restore original pos
408 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
409 for (int i = 0; i < children.count(); i++) {
410 collidingItems.removeAll(children.at(i));
412 for (int i = 0; i < collidingItems.count(); i++)
413 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
418 return QGraphicsItemGroup::itemChange(change, value);
422 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
424 QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
426 doc.setContent(effects, true);
427 QDomElement e = doc.documentElement();
428 e.setAttribute("kdenlive_ix", 0);
429 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
430 QPointF dropPos = event->scenePos();
431 QList<QGraphicsItem *> selection = scene()->items(dropPos);
432 AbstractClipItem *dropChild = NULL;
433 for (int i = 0; i < selection.count(); i++) {
434 if (selection.at(i)->type() == AVWIDGET) {
435 dropChild = (AbstractClipItem *) selection.at(i);
439 if (view) view->slotAddGroupEffect(e, this, dropChild);
443 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
445 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
448 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
454 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
456 if (event->modifiers() & Qt::ShiftModifier) {
457 // User want to do a rectangle selection, so ignore the event to pass it to the view
460 QList <QGraphicsItem *>list = scene()->items(event->scenePos());
461 // only allow group move if we click over an item in the group
462 foreach(const QGraphicsItem *item, list) {
463 if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) {
464 QGraphicsItem::mousePressEvent(event);
472 void AbstractGroupItem::resizeStart(int diff)
475 if (m_resizeInfos.isEmpty())
478 QList <QGraphicsItem *> children = childItems();
479 QList <AbstractClipItem *> items;
481 for (int i = 0; i < children.count(); ++i) {
482 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
483 if (item && item->type() == AVWIDGET) {
486 m_resizeInfos << item->info();
487 item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
488 int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
489 if (qAbs(itemdiff) < qAbs(maximum))
495 for (int i = 0; i < items.count(); ++i)
496 items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
499 void AbstractGroupItem::resizeEnd(int diff)
502 if (m_resizeInfos.isEmpty())
505 QList <QGraphicsItem *> children = childItems();
506 QList <AbstractClipItem *> items;
508 for (int i = 0; i < children.count(); ++i) {
509 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
510 if (item && item->type() == AVWIDGET) {
513 m_resizeInfos << item->info();
514 item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
515 int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
516 if (qAbs(itemdiff) < qAbs(maximum))
522 for (int i = 0; i < items.count(); ++i)
523 items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
526 QList< ItemInfo > AbstractGroupItem::resizeInfos()
528 return m_resizeInfos;
531 void AbstractGroupItem::clearResizeInfos()
533 // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
534 m_resizeInfos = QList <ItemInfo>();
537 GenTime AbstractGroupItem::duration()
539 QList <QGraphicsItem *> children = childItems();
540 GenTime start = GenTime(-1.0);
541 GenTime end = GenTime();
542 for (int i = 0; i < children.count(); ++i) {
543 if (children.at(i)->type() != GROUPWIDGET) {
544 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
546 if (start < GenTime() || item->startPos() < start)
547 start = item->startPos();
548 if (item->endPos() > end)
549 end = item->endPos();
552 children << children.at(i)->childItems();