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 p->setBrush(bgcolor);
163 pen.setColor(QColor(200, 90, 90));
164 pen.setStyle(Qt::DashLine);
167 p->drawRoundedRect(boundingRect().adjusted(0, 0, -1, 0), 3, 3);
171 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
173 if (change == QGraphicsItem::ItemSelectedChange) {
174 if (value.toBool()) setZValue(10);
177 if (change == ItemPositionChange && scene() && parentItem() == 0) {
178 // calculate new position.
179 const int trackHeight = KdenliveSettings::trackheight();
180 QPointF start = sceneBoundingRect().topLeft();
181 QPointF newPos = value.toPointF();
182 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
184 xpos = qMax(xpos, 0);
185 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
186 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
187 QStringList lockedTracks = property("locked_tracks").toStringList();
188 int proposedTrack = (property("y_absolute").toInt() + newPos.y()) / trackHeight;
189 // Check if top item is a clip or a transition
192 QList<int> groupTracks;
193 QList<QGraphicsItem *> children = childItems();
194 for (int i = 0; i < children.count(); i++) {
195 int currentTrack = 0;
196 if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) {
197 currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
198 if (!groupTracks.contains(currentTrack)) groupTracks.append(currentTrack);
200 else if (children.at(i)->type() == GROUPWIDGET) {
201 currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
204 if (children.at(i)->type() == AVWIDGET) {
205 if (topTrack == -1 || currentTrack <= topTrack) {
207 topTrack = currentTrack;
209 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
210 if (topTrack == -1 || currentTrack < topTrack) {
211 offset = (int)(trackHeight / 3 * 2 - 1);
212 topTrack = currentTrack;
214 } else if (children.at(i)->type() == GROUPWIDGET) {
215 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
216 bool clipGroup = false;
217 for (int j = 0; j < subchildren.count(); j++) {
218 if (subchildren.at(j)->type() == AVWIDGET || subchildren.at(j)->type() == TRANSITIONWIDGET) {
219 int subTrack = static_cast <AbstractClipItem*> (subchildren.at(j))->track();
220 if (!groupTracks.contains(subTrack)) groupTracks.append(subTrack);
225 if (topTrack == -1 || currentTrack <= topTrack) {
227 topTrack = currentTrack;
230 if (topTrack == -1 || currentTrack < topTrack) {
231 offset = (int)(trackHeight / 3 * 2 - 1);
232 topTrack = currentTrack;
237 // Check no clip in the group goes outside of existing tracks
238 int maximumTrack = projectScene()->tracksCount() - 1;
240 for (int i = 0; i < groupTracks.count(); i++) {
241 int offset = groupTracks.at(i) - topTrack;
242 if (offset > groupHeight) groupHeight = offset;
244 maximumTrack -= groupHeight;
245 proposedTrack = qMin(proposedTrack, maximumTrack);
246 proposedTrack = qMax(proposedTrack, 0);
247 int groupOffset = proposedTrack - topTrack;
248 if (!lockedTracks.isEmpty()) {
249 for (int i = 0; i < groupTracks.count(); i++) {
250 if (lockedTracks.contains(QString::number(groupTracks.at(i) + groupOffset))) {
255 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
256 //if (newPos == start) return start;
258 /*if (newPos.x() < 0) {
259 // If group goes below 0, adjust position to 0
260 return QPointF(pos().x() - start.x(), pos().y());
263 QList<QGraphicsItem*> collidingItems;
265 if (projectScene()->editMode() == NORMALEDIT) {
266 shape = clipGroupShape(newPos - pos());
267 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
268 collidingItems.removeAll(this);
269 for (int i = 0; i < children.count(); i++) {
270 if (children.at(i)->type() == GROUPWIDGET) {
271 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
272 for (int j = 0; j < subchildren.count(); j++) {
273 collidingItems.removeAll(subchildren.at(j));
276 collidingItems.removeAll(children.at(i));
279 if (!collidingItems.isEmpty()) {
280 bool forwardMove = xpos > start.x();
282 for (int i = 0; i < collidingItems.count(); i++) {
283 QGraphicsItem *collision = collidingItems.at(i);
284 if (collision->type() == AVWIDGET) {
286 if (newPos.y() != pos().y()) {
287 // Track change results in collision, restore original position
290 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
292 // Moving forward, determine best pos
293 QPainterPath clipPath;
294 clipPath.addRect(item->sceneBoundingRect());
295 QPainterPath res = shape.intersected(clipPath);
296 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
298 // Moving backward, determine best pos
299 QPainterPath clipPath;
300 clipPath.addRect(item->sceneBoundingRect());
301 QPainterPath res = shape.intersected(clipPath);
302 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
308 newPos.setX(newPos.x() - offset);
310 newPos.setX(newPos.x() + offset);
312 // If there is still a collision after our position adjust, restore original pos
313 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
314 collidingItems.removeAll(this);
315 for (int i = 0; i < children.count(); i++) {
316 if (children.at(i)->type() == GROUPWIDGET) {
317 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
318 for (int j = 0; j < subchildren.count(); j++) {
319 collidingItems.removeAll(subchildren.at(j));
322 collidingItems.removeAll(children.at(i));
324 for (int i = 0; i < collidingItems.count(); i++)
325 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
329 if (projectScene()->editMode() == NORMALEDIT) {
330 shape = transitionGroupShape(newPos - pos());
331 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
332 collidingItems.removeAll(this);
333 for (int i = 0; i < children.count(); i++) {
334 if (children.at(i)->type() == GROUPWIDGET) {
335 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
336 for (int j = 0; j < subchildren.count(); j++) {
337 collidingItems.removeAll(subchildren.at(j));
340 collidingItems.removeAll(children.at(i));
343 if (collidingItems.isEmpty()) return newPos;
345 bool forwardMove = xpos > start.x();
347 for (int i = 0; i < collidingItems.count(); i++) {
348 QGraphicsItem *collision = collidingItems.at(i);
349 if (collision->type() == TRANSITIONWIDGET) {
351 if (newPos.y() != pos().y()) {
352 // Track change results in collision, restore original position
355 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
357 // Moving forward, determine best pos
358 QPainterPath clipPath;
359 clipPath.addRect(item->sceneBoundingRect());
360 QPainterPath res = shape.intersected(clipPath);
361 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
363 // Moving backward, determine best pos
364 QPainterPath clipPath;
365 clipPath.addRect(item->sceneBoundingRect());
366 QPainterPath res = shape.intersected(clipPath);
367 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
373 newPos.setX(newPos.x() - offset);
375 newPos.setX(newPos.x() + offset);
377 // If there is still a collision after our position adjust, restore original pos
378 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
379 for (int i = 0; i < children.count(); i++) {
380 collidingItems.removeAll(children.at(i));
382 for (int i = 0; i < collidingItems.count(); i++)
383 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
388 return QGraphicsItemGroup::itemChange(change, value);
392 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
394 QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
396 doc.setContent(effects, true);
397 QDomElement e = doc.documentElement();
398 e.setAttribute("kdenlive_ix", 0);
399 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
400 QPointF dropPos = event->scenePos();
401 QList<QGraphicsItem *> selection = scene()->items(dropPos);
402 AbstractClipItem *dropChild = NULL;
403 for (int i = 0; i < selection.count(); i++) {
404 if (selection.at(i)->type() == AVWIDGET) {
405 dropChild = (AbstractClipItem *) selection.at(i);
409 if (view) view->slotAddGroupEffect(e, this, dropChild);
413 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
415 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
418 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
424 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
426 if (event->modifiers() & Qt::ShiftModifier) {
427 // User want to do a rectangle selection, so ignore the event to pass it to the view
430 QList <QGraphicsItem *>list = scene()->items(event->scenePos());
431 // only allow group move if we click over an item in the group
432 foreach(const QGraphicsItem *item, list) {
433 if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) {
434 QGraphicsItem::mousePressEvent(event);
442 void AbstractGroupItem::resizeStart(int diff)
445 if (m_resizeInfos.isEmpty())
448 QList <QGraphicsItem *> children = childItems();
449 QList <AbstractClipItem *> items;
451 for (int i = 0; i < children.count(); ++i) {
452 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
453 if (item && item->type() == AVWIDGET) {
456 m_resizeInfos << item->info();
457 item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
458 int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
459 if (qAbs(itemdiff) < qAbs(maximum))
465 for (int i = 0; i < items.count(); ++i)
466 items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
469 void AbstractGroupItem::resizeEnd(int diff)
472 if (m_resizeInfos.isEmpty())
475 QList <QGraphicsItem *> children = childItems();
476 QList <AbstractClipItem *> items;
478 for (int i = 0; i < children.count(); ++i) {
479 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
480 if (item && item->type() == AVWIDGET) {
483 m_resizeInfos << item->info();
484 item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
485 int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
486 if (qAbs(itemdiff) < qAbs(maximum))
492 for (int i = 0; i < items.count(); ++i)
493 items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
496 QList< ItemInfo > AbstractGroupItem::resizeInfos()
498 return m_resizeInfos;
501 void AbstractGroupItem::clearResizeInfos()
503 // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
504 m_resizeInfos = QList <ItemInfo>();
507 GenTime AbstractGroupItem::duration()
509 QList <QGraphicsItem *> children = childItems();
510 GenTime start = GenTime(-1.0);
511 GenTime end = GenTime();
512 for (int i = 0; i < children.count(); ++i) {
513 if (children.at(i)->type() != GROUPWIDGET) {
514 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
516 if (start < GenTime() || item->startPos() < start)
517 start = item->startPos();
518 if (item->endPos() > end)
519 end = item->endPos();
522 children << children.at(i)->childItems();