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());
94 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
97 QList<QGraphicsItem *> children = childItems();
98 for (int i = 0; i < children.count(); i++) {
99 if (children.at(i)->type() == TRANSITIONWIDGET) {
100 QRectF r(children.at(i)->sceneBoundingRect());
108 void AbstractGroupItem::addItem(QGraphicsItem * item)
114 void AbstractGroupItem::fixItemRect()
116 QPointF start = boundingRect().topLeft();
117 if (start != QPointF(0, 0)) {
118 translate(0 - start.x(), 0 - start.y());
123 /*ItemInfo AbstractGroupItem::info() const {
125 itemInfo.startPos = m_startPos;
126 itemInfo.track = m_track;
131 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
133 const double scale = option->matrix.m11();
134 QColor bgcolor(100, 100, 200, 100);
135 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
136 p->setClipRect(bound);
137 p->fillRect(option->exposedRect, bgcolor);
139 pen.setColor(QColor(200, 90, 90));
140 pen.setStyle(Qt::DashLine);
142 //pen.setCosmetic(true);
144 p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
148 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
150 if (change == QGraphicsItem::ItemSelectedChange) {
151 if (value.toBool()) setZValue(10);
154 if (change == ItemPositionChange && scene() && parentItem() == 0) {
155 // calculate new position.
156 const int trackHeight = KdenliveSettings::trackheight();
157 QPointF start = sceneBoundingRect().topLeft();
158 QPointF newPos = value.toPointF();
159 //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
160 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
162 xpos = qMax(xpos, 0);
163 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
164 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
166 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
168 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
169 int proposedTrack = newPos.y() / trackHeight;
171 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
172 correctedTrack = qMax(correctedTrack, 0);
174 proposedTrack += (correctedTrack - realTrack);
176 // Check if top item is a clip or a transition
179 QList<QGraphicsItem *> children = childItems();
180 for (int i = 0; i < children.count(); i++) {
181 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
182 if (children.at(i)->type() == AVWIDGET) {
183 if (topTrack == -1 || currentTrack <= topTrack) {
185 topTrack = currentTrack;
187 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
188 if (topTrack == -1 || currentTrack < topTrack) {
189 offset = (int)(trackHeight / 3 * 2 - 1);
190 topTrack = currentTrack;
192 } else if (children.at(i)->type() == GROUPWIDGET) {
193 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
194 bool clipGroup = false;
195 for (int j = 0; j < subchildren.count(); j++) {
196 if (subchildren.at(j)->type() == AVWIDGET) {
202 if (topTrack == -1 || currentTrack <= topTrack) {
204 topTrack = currentTrack;
207 if (topTrack == -1 || currentTrack < topTrack) {
208 offset = (int)(trackHeight / 3 * 2 - 1);
209 topTrack = currentTrack;
214 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
215 //if (newPos == start) return start;
217 /*if (newPos.x() < 0) {
218 // If group goes below 0, adjust position to 0
219 return QPointF(pos().x() - start.x(), pos().y());
222 QList<QGraphicsItem*> collindingItems;
224 if (projectScene()->editMode() == NORMALEDIT) {
225 shape = clipGroupShape(newPos - pos());
226 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
227 for (int i = 0; i < children.count(); i++) {
228 collindingItems.removeAll(children.at(i));
231 if (!collindingItems.isEmpty()) {
232 bool forwardMove = xpos > start.x();
234 for (int i = 0; i < collindingItems.count(); i++) {
235 QGraphicsItem *collision = collindingItems.at(i);
236 if (collision->type() == AVWIDGET) {
238 if (newPos.y() != pos().y()) {
239 // Track change results in collision, restore original position
242 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
244 // Moving forward, determine best pos
245 QPainterPath clipPath;
246 clipPath.addRect(item->sceneBoundingRect());
247 QPainterPath res = shape.intersected(clipPath);
248 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
250 // Moving backward, determine best pos
251 QPainterPath clipPath;
252 clipPath.addRect(item->sceneBoundingRect());
253 QPainterPath res = shape.intersected(clipPath);
254 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
260 newPos.setX(newPos.x() - offset);
262 newPos.setX(newPos.x() + offset);
264 // If there is still a collision after our position adjust, restore original pos
265 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
266 for (int i = 0; i < children.count(); i++) {
267 collindingItems.removeAll(children.at(i));
269 for (int i = 0; i < collindingItems.count(); i++)
270 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
274 if (projectScene()->editMode() == NORMALEDIT) {
275 shape = transitionGroupShape(newPos - pos());
276 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
277 for (int i = 0; i < children.count(); i++) {
278 collindingItems.removeAll(children.at(i));
281 if (collindingItems.isEmpty()) return newPos;
283 bool forwardMove = xpos > start.x();
285 for (int i = 0; i < collindingItems.count(); i++) {
286 QGraphicsItem *collision = collindingItems.at(i);
287 if (collision->type() == TRANSITIONWIDGET) {
289 if (newPos.y() != pos().y()) {
290 // Track change results in collision, restore original position
293 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
295 // Moving forward, determine best pos
296 QPainterPath clipPath;
297 clipPath.addRect(item->sceneBoundingRect());
298 QPainterPath res = shape.intersected(clipPath);
299 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
301 // Moving backward, determine best pos
302 QPainterPath clipPath;
303 clipPath.addRect(item->sceneBoundingRect());
304 QPainterPath res = shape.intersected(clipPath);
305 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
311 newPos.setX(newPos.x() - offset);
313 newPos.setX(newPos.x() + offset);
315 // If there is still a collision after our position adjust, restore original pos
316 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
317 for (int i = 0; i < children.count(); i++) {
318 collindingItems.removeAll(children.at(i));
320 for (int i = 0; i < collindingItems.count(); i++)
321 if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
326 return QGraphicsItemGroup::itemChange(change, value);
330 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
332 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
334 doc.setContent(effects, true);
335 QDomElement e = doc.documentElement();
336 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
337 if (view) view->slotAddGroupEffect(e, this);
341 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
343 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
346 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
352 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
354 if (event->modifiers() & Qt::ShiftModifier) {
355 // User want to do a rectangle selection, so ignore the event to pass it to the view
357 } else QGraphicsItem::mousePressEvent(event);