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 == ItemPositionChange && scene() && parentItem() == 0) {
151 // calculate new position.
152 const int trackHeight = KdenliveSettings::trackheight();
153 QPointF start = sceneBoundingRect().topLeft();
154 QPointF newPos = value.toPointF();
155 //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
156 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
158 xpos = qMax(xpos, 0);
159 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
160 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
162 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
164 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
165 int proposedTrack = newPos.y() / trackHeight;
167 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
168 correctedTrack = qMax(correctedTrack, 0);
170 proposedTrack += (correctedTrack - realTrack);
172 // Check if top item is a clip or a transition
175 QList<QGraphicsItem *> children = childItems();
176 for (int i = 0; i < children.count(); i++) {
177 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
178 if (children.at(i)->type() == AVWIDGET) {
179 if (topTrack == -1 || currentTrack <= topTrack) {
181 topTrack = currentTrack;
183 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
184 if (topTrack == -1 || currentTrack < topTrack) {
185 offset = (int)(trackHeight / 3 * 2 - 1);
186 topTrack = currentTrack;
188 } else if (children.at(i)->type() == GROUPWIDGET) {
189 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
190 bool clipGroup = false;
191 for (int j = 0; j < subchildren.count(); j++) {
192 if (subchildren.at(j)->type() == AVWIDGET) {
198 if (topTrack == -1 || currentTrack <= topTrack) {
200 topTrack = currentTrack;
203 if (topTrack == -1 || currentTrack < topTrack) {
204 offset = (int)(trackHeight / 3 * 2 - 1);
205 topTrack = currentTrack;
210 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
211 //if (newPos == start) return start;
213 /*if (newPos.x() < 0) {
214 // If group goes below 0, adjust position to 0
215 return QPointF(pos().x() - start.x(), pos().y());
218 QList<QGraphicsItem*> collindingItems;
220 if (projectScene()->editMode() == NORMALEDIT) {
221 shape = clipGroupShape(newPos - pos());
222 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
223 for (int i = 0; i < children.count(); i++) {
224 collindingItems.removeAll(children.at(i));
227 if (!collindingItems.isEmpty()) {
228 bool forwardMove = xpos > start.x();
230 for (int i = 0; i < collindingItems.count(); i++) {
231 QGraphicsItem *collision = collindingItems.at(i);
232 if (collision->type() == AVWIDGET) {
234 if (newPos.y() != pos().y()) {
235 // Track change results in collision, restore original position
238 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
240 // Moving forward, determine best pos
241 QPainterPath clipPath;
242 clipPath.addRect(item->sceneBoundingRect());
243 QPainterPath res = shape.intersected(clipPath);
244 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
246 // Moving backward, determine best pos
247 QPainterPath clipPath;
248 clipPath.addRect(item->sceneBoundingRect());
249 QPainterPath res = shape.intersected(clipPath);
250 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
256 newPos.setX(newPos.x() - offset);
258 newPos.setX(newPos.x() + offset);
260 // If there is still a collision after our position adjust, restore original pos
261 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
262 for (int i = 0; i < children.count(); i++) {
263 collindingItems.removeAll(children.at(i));
265 for (int i = 0; i < collindingItems.count(); i++)
266 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
270 if (projectScene()->editMode() == NORMALEDIT) {
271 shape = transitionGroupShape(newPos - pos());
272 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
273 for (int i = 0; i < children.count(); i++) {
274 collindingItems.removeAll(children.at(i));
277 if (collindingItems.isEmpty()) return newPos;
279 bool forwardMove = xpos > start.x();
281 for (int i = 0; i < collindingItems.count(); i++) {
282 QGraphicsItem *collision = collindingItems.at(i);
283 if (collision->type() == TRANSITIONWIDGET) {
285 if (newPos.y() != pos().y()) {
286 // Track change results in collision, restore original position
289 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
291 // Moving forward, determine best pos
292 QPainterPath clipPath;
293 clipPath.addRect(item->sceneBoundingRect());
294 QPainterPath res = shape.intersected(clipPath);
295 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
297 // Moving backward, determine best pos
298 QPainterPath clipPath;
299 clipPath.addRect(item->sceneBoundingRect());
300 QPainterPath res = shape.intersected(clipPath);
301 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
307 newPos.setX(newPos.x() - offset);
309 newPos.setX(newPos.x() + offset);
311 // If there is still a collision after our position adjust, restore original pos
312 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
313 for (int i = 0; i < children.count(); i++) {
314 collindingItems.removeAll(children.at(i));
316 for (int i = 0; i < collindingItems.count(); i++)
317 if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
322 return QGraphicsItemGroup::itemChange(change, value);
326 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
328 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
330 doc.setContent(effects, true);
331 QDomElement e = doc.documentElement();
332 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
333 if (view) view->slotAddGroupEffect(e, this);
337 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
339 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
342 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
348 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
350 if (event->modifiers() & Qt::ShiftModifier) {
351 // User want to do a rectangle selection, so ignore the event to pass it to the view
353 } else QGraphicsItem::mousePressEvent(event);