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);
47 int AbstractGroupItem::type() const
52 int AbstractGroupItem::track() const
54 return (int)(scenePos().y() / KdenliveSettings::trackheight());
57 void AbstractGroupItem::setItemLocked(bool locked)
61 setFlag(QGraphicsItem::ItemIsMovable, false);
62 setFlag(QGraphicsItem::ItemIsSelectable, false);
64 setFlag(QGraphicsItem::ItemIsMovable, true);
65 setFlag(QGraphicsItem::ItemIsSelectable, true);
69 bool AbstractGroupItem::isItemLocked() const
71 return !(flags() & (QGraphicsItem::ItemIsSelectable));
74 CustomTrackScene* AbstractGroupItem::projectScene()
76 if (scene()) return static_cast <CustomTrackScene*>(scene());
80 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
83 QList<QGraphicsItem *> children = childItems();
84 for (int i = 0; i < children.count(); i++) {
85 if (children.at(i)->type() == AVWIDGET) {
86 QRectF r(children.at(i)->sceneBoundingRect());
89 } else if (children.at(i)->type() == GROUPWIDGET) {
90 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
91 for (int j = 0; j < subchildren.count(); j++) {
92 if (subchildren.at(j)->type() == AVWIDGET) {
93 QRectF r(subchildren.at(j)->sceneBoundingRect());
103 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
106 QList<QGraphicsItem *> children = childItems();
107 for (int i = 0; i < children.count(); i++) {
108 if (children.at(i)->type() == TRANSITIONWIDGET) {
109 QRectF r(children.at(i)->sceneBoundingRect());
112 } else if (children.at(i)->type() == GROUPWIDGET) {
113 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
114 for (int j = 0; j < subchildren.count(); j++) {
115 if (subchildren.at(j)->type() == TRANSITIONWIDGET) {
116 QRectF r(subchildren.at(j)->sceneBoundingRect());
126 void AbstractGroupItem::addItem(QGraphicsItem * item)
132 void AbstractGroupItem::fixItemRect()
134 QPointF start = boundingRect().topLeft();
135 if (start != QPointF(0, 0)) {
136 translate(0 - start.x(), 0 - start.y());
141 /*ItemInfo AbstractGroupItem::info() const {
143 itemInfo.startPos = m_startPos;
144 itemInfo.track = m_track;
149 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
151 const double scale = option->matrix.m11();
152 QColor bgcolor(100, 100, 200, 100);
153 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
154 p->setClipRect(bound);
155 p->fillRect(option->exposedRect, bgcolor);
157 pen.setColor(QColor(200, 90, 90));
158 pen.setStyle(Qt::DashLine);
160 //pen.setCosmetic(true);
162 p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
166 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
168 if (change == QGraphicsItem::ItemSelectedChange) {
169 if (value.toBool()) setZValue(10);
172 if (change == ItemPositionChange && scene() && parentItem() == 0) {
173 // calculate new position.
174 const int trackHeight = KdenliveSettings::trackheight();
175 QPointF start = sceneBoundingRect().topLeft();
176 QPointF newPos = value.toPointF();
177 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
179 xpos = qMax(xpos, 0);
180 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
181 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
183 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
185 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
186 int proposedTrack = newPos.y() / trackHeight;
188 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
189 correctedTrack = qMax(correctedTrack, 0);
191 proposedTrack += (correctedTrack - realTrack);
193 // Check if top item is a clip or a transition
196 QList<QGraphicsItem *> children = childItems();
197 for (int i = 0; i < children.count(); i++) {
198 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
199 if (children.at(i)->type() == AVWIDGET) {
200 if (topTrack == -1 || currentTrack <= topTrack) {
202 topTrack = currentTrack;
204 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
205 if (topTrack == -1 || currentTrack < topTrack) {
206 offset = (int)(trackHeight / 3 * 2 - 1);
207 topTrack = currentTrack;
209 } else if (children.at(i)->type() == GROUPWIDGET) {
210 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
211 bool clipGroup = false;
212 for (int j = 0; j < subchildren.count(); j++) {
213 if (subchildren.at(j)->type() == AVWIDGET) {
219 if (topTrack == -1 || currentTrack <= topTrack) {
221 topTrack = currentTrack;
224 if (topTrack == -1 || currentTrack < topTrack) {
225 offset = (int)(trackHeight / 3 * 2 - 1);
226 topTrack = currentTrack;
231 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
232 //if (newPos == start) return start;
234 /*if (newPos.x() < 0) {
235 // If group goes below 0, adjust position to 0
236 return QPointF(pos().x() - start.x(), pos().y());
239 QList<QGraphicsItem*> collidingItems;
241 if (projectScene()->editMode() == NORMALEDIT) {
242 shape = clipGroupShape(newPos - pos());
243 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
244 collidingItems.removeAll(this);
245 for (int i = 0; i < children.count(); i++) {
246 if (children.at(i)->type() == GROUPWIDGET) {
247 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
248 for (int j = 0; j < subchildren.count(); j++) {
249 collidingItems.removeAll(subchildren.at(j));
252 collidingItems.removeAll(children.at(i));
255 if (!collidingItems.isEmpty()) {
256 bool forwardMove = xpos > start.x();
258 for (int i = 0; i < collidingItems.count(); i++) {
259 QGraphicsItem *collision = collidingItems.at(i);
260 if (collision->type() == AVWIDGET) {
262 if (newPos.y() != pos().y()) {
263 // Track change results in collision, restore original position
266 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
268 // Moving forward, determine best pos
269 QPainterPath clipPath;
270 clipPath.addRect(item->sceneBoundingRect());
271 QPainterPath res = shape.intersected(clipPath);
272 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
274 // Moving backward, determine best pos
275 QPainterPath clipPath;
276 clipPath.addRect(item->sceneBoundingRect());
277 QPainterPath res = shape.intersected(clipPath);
278 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
284 newPos.setX(newPos.x() - offset);
286 newPos.setX(newPos.x() + offset);
288 // If there is still a collision after our position adjust, restore original pos
289 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
290 collidingItems.removeAll(this);
291 for (int i = 0; i < children.count(); i++) {
292 if (children.at(i)->type() == GROUPWIDGET) {
293 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
294 for (int j = 0; j < subchildren.count(); j++) {
295 collidingItems.removeAll(subchildren.at(j));
298 collidingItems.removeAll(children.at(i));
300 for (int i = 0; i < collidingItems.count(); i++)
301 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
305 if (projectScene()->editMode() == NORMALEDIT) {
306 shape = transitionGroupShape(newPos - pos());
307 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
308 collidingItems.removeAll(this);
309 for (int i = 0; i < children.count(); i++) {
310 if (children.at(i)->type() == GROUPWIDGET) {
311 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
312 for (int j = 0; j < subchildren.count(); j++) {
313 collidingItems.removeAll(subchildren.at(j));
316 collidingItems.removeAll(children.at(i));
319 if (collidingItems.isEmpty()) return newPos;
321 bool forwardMove = xpos > start.x();
323 for (int i = 0; i < collidingItems.count(); i++) {
324 QGraphicsItem *collision = collidingItems.at(i);
325 if (collision->type() == TRANSITIONWIDGET) {
327 if (newPos.y() != pos().y()) {
328 // Track change results in collision, restore original position
331 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
333 // Moving forward, determine best pos
334 QPainterPath clipPath;
335 clipPath.addRect(item->sceneBoundingRect());
336 QPainterPath res = shape.intersected(clipPath);
337 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
339 // Moving backward, determine best pos
340 QPainterPath clipPath;
341 clipPath.addRect(item->sceneBoundingRect());
342 QPainterPath res = shape.intersected(clipPath);
343 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
349 newPos.setX(newPos.x() - offset);
351 newPos.setX(newPos.x() + offset);
353 // If there is still a collision after our position adjust, restore original pos
354 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
355 for (int i = 0; i < children.count(); i++) {
356 collidingItems.removeAll(children.at(i));
358 for (int i = 0; i < collidingItems.count(); i++)
359 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
364 return QGraphicsItemGroup::itemChange(change, value);
368 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
370 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
372 doc.setContent(effects, true);
373 QDomElement e = doc.documentElement();
374 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
375 if (view) view->slotAddGroupEffect(e, this);
379 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
381 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
384 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
390 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
392 if (event->modifiers() & Qt::ShiftModifier) {
393 // User want to do a rectangle selection, so ignore the event to pass it to the view
395 } else QGraphicsItem::mousePressEvent(event);