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
82 return groupShape(AVWIDGET, offset);
85 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
87 return groupShape(TRANSITIONWIDGET, offset);
90 QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const
93 QList<QGraphicsItem *> children = childItems();
94 for (int i = 0; i < children.count(); i++) {
95 if (children.at(i)->type() == (int)type) {
96 QRectF r(children.at(i)->sceneBoundingRect());
99 } else if (children.at(i)->type() == GROUPWIDGET) {
100 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
101 for (int j = 0; j < subchildren.count(); j++) {
102 if (subchildren.at(j)->type() == (int)type) {
103 QRectF r(subchildren.at(j)->sceneBoundingRect());
113 void AbstractGroupItem::addItem(QGraphicsItem * item)
119 void AbstractGroupItem::fixItemRect()
121 QPointF start = boundingRect().topLeft();
122 if (start != QPointF(0, 0)) {
123 translate(0 - start.x(), 0 - start.y());
128 /*ItemInfo AbstractGroupItem::info() const {
130 itemInfo.startPos = m_startPos;
131 itemInfo.track = m_track;
136 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
138 const double scale = option->matrix.m11();
139 QColor bgcolor(100, 100, 200, 100);
140 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
141 p->setClipRect(bound);
142 p->fillRect(option->exposedRect, bgcolor);
144 pen.setColor(QColor(200, 90, 90));
145 pen.setStyle(Qt::DashLine);
147 //pen.setCosmetic(true);
149 p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
153 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
155 if (change == QGraphicsItem::ItemSelectedChange) {
156 if (value.toBool()) setZValue(10);
159 if (change == ItemPositionChange && scene() && parentItem() == 0) {
160 // calculate new position.
161 const int trackHeight = KdenliveSettings::trackheight();
162 QPointF start = sceneBoundingRect().topLeft();
163 QPointF newPos = value.toPointF();
164 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
166 xpos = qMax(xpos, 0);
167 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
168 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
170 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
172 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
173 int proposedTrack = newPos.y() / trackHeight;
175 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
176 correctedTrack = qMax(correctedTrack, 0);
178 proposedTrack += (correctedTrack - realTrack);
180 // Check if top item is a clip or a transition
183 QList<QGraphicsItem *> children = childItems();
184 for (int i = 0; i < children.count(); i++) {
185 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
186 if (children.at(i)->type() == AVWIDGET) {
187 if (topTrack == -1 || currentTrack <= topTrack) {
189 topTrack = currentTrack;
191 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
192 if (topTrack == -1 || currentTrack < topTrack) {
193 offset = (int)(trackHeight / 3 * 2 - 1);
194 topTrack = currentTrack;
196 } else if (children.at(i)->type() == GROUPWIDGET) {
197 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
198 bool clipGroup = false;
199 for (int j = 0; j < subchildren.count(); j++) {
200 if (subchildren.at(j)->type() == AVWIDGET) {
206 if (topTrack == -1 || currentTrack <= topTrack) {
208 topTrack = currentTrack;
211 if (topTrack == -1 || currentTrack < topTrack) {
212 offset = (int)(trackHeight / 3 * 2 - 1);
213 topTrack = currentTrack;
218 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
219 //if (newPos == start) return start;
221 /*if (newPos.x() < 0) {
222 // If group goes below 0, adjust position to 0
223 return QPointF(pos().x() - start.x(), pos().y());
226 QList<QGraphicsItem*> collidingItems;
228 if (projectScene()->editMode() == NORMALEDIT) {
229 shape = clipGroupShape(newPos - pos());
230 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
231 collidingItems.removeAll(this);
232 for (int i = 0; i < children.count(); i++) {
233 if (children.at(i)->type() == GROUPWIDGET) {
234 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
235 for (int j = 0; j < subchildren.count(); j++) {
236 collidingItems.removeAll(subchildren.at(j));
239 collidingItems.removeAll(children.at(i));
242 if (!collidingItems.isEmpty()) {
243 bool forwardMove = xpos > start.x();
245 for (int i = 0; i < collidingItems.count(); i++) {
246 QGraphicsItem *collision = collidingItems.at(i);
247 if (collision->type() == AVWIDGET) {
249 if (newPos.y() != pos().y()) {
250 // Track change results in collision, restore original position
253 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
255 // Moving forward, determine best pos
256 QPainterPath clipPath;
257 clipPath.addRect(item->sceneBoundingRect());
258 QPainterPath res = shape.intersected(clipPath);
259 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
261 // Moving backward, determine best pos
262 QPainterPath clipPath;
263 clipPath.addRect(item->sceneBoundingRect());
264 QPainterPath res = shape.intersected(clipPath);
265 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
271 newPos.setX(newPos.x() - offset);
273 newPos.setX(newPos.x() + offset);
275 // If there is still a collision after our position adjust, restore original pos
276 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
277 collidingItems.removeAll(this);
278 for (int i = 0; i < children.count(); i++) {
279 if (children.at(i)->type() == GROUPWIDGET) {
280 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
281 for (int j = 0; j < subchildren.count(); j++) {
282 collidingItems.removeAll(subchildren.at(j));
285 collidingItems.removeAll(children.at(i));
287 for (int i = 0; i < collidingItems.count(); i++)
288 if (collidingItems.at(i)->type() == AVWIDGET) return pos();
292 if (projectScene()->editMode() == NORMALEDIT) {
293 shape = transitionGroupShape(newPos - pos());
294 collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
295 collidingItems.removeAll(this);
296 for (int i = 0; i < children.count(); i++) {
297 if (children.at(i)->type() == GROUPWIDGET) {
298 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
299 for (int j = 0; j < subchildren.count(); j++) {
300 collidingItems.removeAll(subchildren.at(j));
303 collidingItems.removeAll(children.at(i));
306 if (collidingItems.isEmpty()) return newPos;
308 bool forwardMove = xpos > start.x();
310 for (int i = 0; i < collidingItems.count(); i++) {
311 QGraphicsItem *collision = collidingItems.at(i);
312 if (collision->type() == TRANSITIONWIDGET) {
314 if (newPos.y() != pos().y()) {
315 // Track change results in collision, restore original position
318 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
320 // Moving forward, determine best pos
321 QPainterPath clipPath;
322 clipPath.addRect(item->sceneBoundingRect());
323 QPainterPath res = shape.intersected(clipPath);
324 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
326 // Moving backward, determine best pos
327 QPainterPath clipPath;
328 clipPath.addRect(item->sceneBoundingRect());
329 QPainterPath res = shape.intersected(clipPath);
330 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
336 newPos.setX(newPos.x() - offset);
338 newPos.setX(newPos.x() + offset);
340 // If there is still a collision after our position adjust, restore original pos
341 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
342 for (int i = 0; i < children.count(); i++) {
343 collidingItems.removeAll(children.at(i));
345 for (int i = 0; i < collidingItems.count(); i++)
346 if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
351 return QGraphicsItemGroup::itemChange(change, value);
355 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
357 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
359 doc.setContent(effects, true);
360 QDomElement e = doc.documentElement();
361 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
362 if (view) view->slotAddGroupEffect(e, this);
366 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
368 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
371 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
377 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
379 if (event->modifiers() & Qt::ShiftModifier) {
380 // User want to do a rectangle selection, so ignore the event to pass it to the view
382 } else QGraphicsItem::mousePressEvent(event);