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>
35 AbstractGroupItem::AbstractGroupItem(double /* fps */) :
40 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
41 #if QT_VERSION >= 0x040600
42 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
45 m_resizeInfos = QList <ItemInfo>();
48 int AbstractGroupItem::type() const
53 int AbstractGroupItem::track() const
55 return (int)(scenePos().y() / KdenliveSettings::trackheight());
58 void AbstractGroupItem::setItemLocked(bool locked)
63 setFlag(QGraphicsItem::ItemIsMovable, !locked);
64 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
66 foreach (QGraphicsItem *child, childItems())
67 ((AbstractClipItem *)child)->setItemLocked(locked);
70 bool AbstractGroupItem::isItemLocked() const
72 return !(flags() & (QGraphicsItem::ItemIsSelectable));
75 CustomTrackScene* AbstractGroupItem::projectScene()
77 if (scene()) return static_cast <CustomTrackScene*>(scene());
81 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
83 return groupShape(AVWIDGET, offset);
86 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
88 return groupShape(TRANSITIONWIDGET, offset);
91 QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const
94 QList<QGraphicsItem *> children = childItems();
95 for (int i = 0; i < children.count(); i++) {
96 if (children.at(i)->type() == (int)type) {
97 QRectF r(children.at(i)->sceneBoundingRect());
100 } else if (children.at(i)->type() == GROUPWIDGET) {
101 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
102 for (int j = 0; j < subchildren.count(); j++) {
103 if (subchildren.at(j)->type() == (int)type) {
104 QRectF r(subchildren.at(j)->sceneBoundingRect());
114 void AbstractGroupItem::addItem(QGraphicsItem * item)
120 void AbstractGroupItem::fixItemRect()
122 QPointF start = boundingRect().topLeft();
123 if (start != QPointF(0, 0)) {
124 translate(0 - start.x(), 0 - start.y());
129 /*ItemInfo AbstractGroupItem::info() const {
131 itemInfo.startPos = m_startPos;
132 itemInfo.track = m_track;
137 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
139 const double scale = option->matrix.m11();
140 QColor bgcolor(100, 100, 200, 100);
141 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
142 p->setClipRect(bound);
143 p->fillRect(option->exposedRect, bgcolor);
145 pen.setColor(QColor(200, 90, 90));
146 pen.setStyle(Qt::DashLine);
148 //pen.setCosmetic(true);
150 p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
154 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
156 if (change == QGraphicsItem::ItemSelectedChange) {
157 if (value.toBool()) setZValue(10);
160 if (change == ItemPositionChange && scene() && parentItem() == 0) {
161 // calculate new position.
162 const int trackHeight = KdenliveSettings::trackheight();
163 QPointF start = sceneBoundingRect().topLeft();
164 QPointF newPos = value.toPointF();
165 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
167 xpos = qMax(xpos, 0);
168 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
169 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
171 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
173 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
174 int proposedTrack = newPos.y() / trackHeight;
176 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
177 correctedTrack = qMax(correctedTrack, 0);
179 proposedTrack += (correctedTrack - realTrack);
181 // Check if top item is a clip or a transition
184 QList<QGraphicsItem *> children = childItems();
185 for (int i = 0; i < children.count(); i++) {
186 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
187 if (children.at(i)->type() == AVWIDGET) {
188 if (topTrack == -1 || currentTrack <= topTrack) {
190 topTrack = currentTrack;
192 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
193 if (topTrack == -1 || currentTrack < topTrack) {
194 offset = (int)(trackHeight / 3 * 2 - 1);
195 topTrack = currentTrack;
197 } else if (children.at(i)->type() == GROUPWIDGET) {
198 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
199 bool clipGroup = false;
200 for (int j = 0; j < subchildren.count(); j++) {
201 if (subchildren.at(j)->type() == AVWIDGET) {
207 if (topTrack == -1 || currentTrack <= topTrack) {
209 topTrack = currentTrack;
212 if (topTrack == -1 || currentTrack < topTrack) {
213 offset = (int)(trackHeight / 3 * 2 - 1);
214 topTrack = currentTrack;
219 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
220 //if (newPos == start) return start;
222 /*if (newPos.x() < 0) {
223 // If group goes below 0, adjust position to 0
224 return QPointF(pos().x() - start.x(), pos().y());
227 QList<QGraphicsItem*> collidingItems;
229 if (projectScene()->editMode() == NORMALEDIT) {
230 shape = clipGroupShape(newPos - pos());
231 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
232 collidingItems.removeAll(this);
233 for (int i = 0; i < children.count(); i++) {
234 if (children.at(i)->type() == GROUPWIDGET) {
235 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
236 for (int j = 0; j < subchildren.count(); j++) {
237 collidingItems.removeAll(subchildren.at(j));
240 collidingItems.removeAll(children.at(i));
243 if (!collidingItems.isEmpty()) {
244 bool forwardMove = xpos > start.x();
246 for (int i = 0; i < collidingItems.count(); i++) {
247 QGraphicsItem *collision = collidingItems.at(i);
248 if (collision->type() == AVWIDGET) {
250 if (newPos.y() != pos().y()) {
251 // Track change results in collision, restore original position
254 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
256 // Moving forward, determine best pos
257 QPainterPath clipPath;
258 clipPath.addRect(item->sceneBoundingRect());
259 QPainterPath res = shape.intersected(clipPath);
260 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
262 // Moving backward, determine best pos
263 QPainterPath clipPath;
264 clipPath.addRect(item->sceneBoundingRect());
265 QPainterPath res = shape.intersected(clipPath);
266 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
272 newPos.setX(newPos.x() - offset);
274 newPos.setX(newPos.x() + offset);
276 // If there is still a collision after our position adjust, restore original pos
277 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
278 collidingItems.removeAll(this);
279 for (int i = 0; i < children.count(); i++) {
280 if (children.at(i)->type() == GROUPWIDGET) {
281 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
282 for (int j = 0; j < subchildren.count(); j++) {
283 collidingItems.removeAll(subchildren.at(j));
286 collidingItems.removeAll(children.at(i));
288 for (int i = 0; i < collidingItems.count(); i++)
289 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
293 if (projectScene()->editMode() == NORMALEDIT) {
294 shape = transitionGroupShape(newPos - pos());
295 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
296 collidingItems.removeAll(this);
297 for (int i = 0; i < children.count(); i++) {
298 if (children.at(i)->type() == GROUPWIDGET) {
299 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
300 for (int j = 0; j < subchildren.count(); j++) {
301 collidingItems.removeAll(subchildren.at(j));
304 collidingItems.removeAll(children.at(i));
307 if (collidingItems.isEmpty()) return newPos;
309 bool forwardMove = xpos > start.x();
311 for (int i = 0; i < collidingItems.count(); i++) {
312 QGraphicsItem *collision = collidingItems.at(i);
313 if (collision->type() == TRANSITIONWIDGET) {
315 if (newPos.y() != pos().y()) {
316 // Track change results in collision, restore original position
319 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
321 // Moving forward, determine best pos
322 QPainterPath clipPath;
323 clipPath.addRect(item->sceneBoundingRect());
324 QPainterPath res = shape.intersected(clipPath);
325 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
327 // Moving backward, determine best pos
328 QPainterPath clipPath;
329 clipPath.addRect(item->sceneBoundingRect());
330 QPainterPath res = shape.intersected(clipPath);
331 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
337 newPos.setX(newPos.x() - offset);
339 newPos.setX(newPos.x() + offset);
341 // If there is still a collision after our position adjust, restore original pos
342 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
343 for (int i = 0; i < children.count(); i++) {
344 collidingItems.removeAll(children.at(i));
346 for (int i = 0; i < collidingItems.count(); i++)
347 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
352 return QGraphicsItemGroup::itemChange(change, value);
356 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
358 QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
360 doc.setContent(effects, true);
361 QDomElement e = doc.documentElement();
362 e.setAttribute("kdenlive_ix", 0);
363 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
364 if (view) view->slotAddGroupEffect(e, this);
368 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
370 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
373 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
379 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
381 if (event->modifiers() & Qt::ShiftModifier) {
382 // User want to do a rectangle selection, so ignore the event to pass it to the view
384 } else QGraphicsItem::mousePressEvent(event);
387 void AbstractGroupItem::resizeStart(int diff)
390 if (m_resizeInfos.isEmpty())
393 QList <QGraphicsItem *> children = childItems();
394 QList <AbstractClipItem *> items;
396 for (int i = 0; i < children.count(); ++i) {
397 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
398 if (item && item->type() == AVWIDGET) {
401 m_resizeInfos << item->info();
402 item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
403 int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
404 if (qAbs(itemdiff) < qAbs(maximum))
410 for (int i = 0; i < items.count(); ++i)
411 items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
414 void AbstractGroupItem::resizeEnd(int diff)
417 if (m_resizeInfos.isEmpty())
420 QList <QGraphicsItem *> children = childItems();
421 QList <AbstractClipItem *> items;
423 for (int i = 0; i < children.count(); ++i) {
424 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
425 if (item && item->type() == AVWIDGET) {
428 m_resizeInfos << item->info();
429 item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
430 int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
431 if (qAbs(itemdiff) < qAbs(maximum))
437 for (int i = 0; i < items.count(); ++i)
438 items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
441 QList< ItemInfo > AbstractGroupItem::resizeInfos()
443 return m_resizeInfos;
446 void AbstractGroupItem::clearResizeInfos()
448 // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
449 m_resizeInfos = QList <ItemInfo>();
452 GenTime AbstractGroupItem::duration()
454 QList <QGraphicsItem *> children = childItems();
455 GenTime start = GenTime(-1.0);
456 GenTime end = GenTime();
457 for (int i = 0; i < children.count(); ++i) {
458 if (children.at(i)->type() != GROUPWIDGET) {
459 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
461 if (start < GenTime() || item->startPos() < start)
462 start = item->startPos();
463 if (item->endPos() > end)
464 end = item->endPos();
467 children << children.at(i)->childItems();