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 CustomTrackScene* AbstractGroupItem::projectScene()
59 if (scene()) return static_cast <CustomTrackScene*>(scene());
63 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
66 QList<QGraphicsItem *> children = childItems();
67 for (int i = 0; i < children.count(); i++) {
68 if (children.at(i)->type() == AVWIDGET) {
69 QRectF r(children.at(i)->sceneBoundingRect());
77 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
80 QList<QGraphicsItem *> children = childItems();
81 for (int i = 0; i < children.count(); i++) {
82 if (children.at(i)->type() == TRANSITIONWIDGET) {
83 QRectF r(children.at(i)->sceneBoundingRect());
91 void AbstractGroupItem::addItem(QGraphicsItem * item)
97 void AbstractGroupItem::fixItemRect()
99 QPointF start = boundingRect().topLeft();
100 if (start != QPointF(0, 0)) {
101 translate(0 - start.x(), 0 - start.y());
106 /*ItemInfo AbstractGroupItem::info() const {
108 itemInfo.startPos = m_startPos;
109 itemInfo.track = m_track;
114 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
116 const double scale = option->matrix.m11();
117 QColor bgcolor(100, 100, 200, 100);
118 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
119 p->setClipRect(bound);
120 p->fillRect(option->exposedRect, bgcolor);
122 pen.setColor(QColor(200, 90, 90));
123 pen.setStyle(Qt::DashLine);
125 //pen.setCosmetic(true);
127 p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
131 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
133 if (change == ItemPositionChange && scene() && parentItem() == 0) {
134 // calculate new position.
135 const int trackHeight = KdenliveSettings::trackheight();
136 QPointF start = sceneBoundingRect().topLeft();
137 QPointF newPos = value.toPointF();
138 //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
139 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
141 xpos = qMax(xpos, 0);
142 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
143 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
145 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
147 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
148 int proposedTrack = newPos.y() / trackHeight;
150 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
151 correctedTrack = qMax(correctedTrack, 0);
153 proposedTrack += (correctedTrack - realTrack);
155 // Check if top item is a clip or a transition
158 QList<QGraphicsItem *> children = childItems();
159 for (int i = 0; i < children.count(); i++) {
160 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
161 if (children.at(i)->type() == AVWIDGET) {
162 if (topTrack == -1 || currentTrack <= topTrack) {
164 topTrack = currentTrack;
166 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
167 if (topTrack == -1 || currentTrack < topTrack) {
168 offset = (int)(trackHeight / 3 * 2 - 1);
169 topTrack = currentTrack;
171 } else if (children.at(i)->type() == GROUPWIDGET) {
172 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
173 bool clipGroup = false;
174 for (int j = 0; j < subchildren.count(); j++) {
175 if (subchildren.at(j)->type() == AVWIDGET) {
181 if (topTrack == -1 || currentTrack <= topTrack) {
183 topTrack = currentTrack;
186 if (topTrack == -1 || currentTrack < topTrack) {
187 offset = (int)(trackHeight / 3 * 2 - 1);
188 topTrack = currentTrack;
193 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
194 //if (newPos == start) return start;
196 /*if (newPos.x() < 0) {
197 // If group goes below 0, adjust position to 0
198 return QPointF(pos().x() - start.x(), pos().y());
201 QPainterPath shape = clipGroupShape(newPos - pos());
202 QList<QGraphicsItem*> collindingItems;
203 if (projectScene()->editMode() == NORMALEDIT) {
204 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
205 for (int i = 0; i < children.count(); i++) {
206 collindingItems.removeAll(children.at(i));
209 if (!collindingItems.isEmpty()) {
210 bool forwardMove = xpos > start.x();
212 for (int i = 0; i < collindingItems.count(); i++) {
213 QGraphicsItem *collision = collindingItems.at(i);
214 if (collision->type() == AVWIDGET) {
216 if (newPos.y() != pos().y()) {
217 // Track change results in collision, restore original position
220 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
222 // Moving forward, determine best pos
223 QPainterPath clipPath;
224 clipPath.addRect(item->sceneBoundingRect());
225 QPainterPath res = shape.intersected(clipPath);
226 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
228 // Moving backward, determine best pos
229 QPainterPath clipPath;
230 clipPath.addRect(item->sceneBoundingRect());
231 QPainterPath res = shape.intersected(clipPath);
232 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
238 newPos.setX(newPos.x() - offset);
240 newPos.setX(newPos.x() + offset);
242 // If there is still a collision after our position adjust, restore original pos
243 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
244 for (int i = 0; i < children.count(); i++) {
245 collindingItems.removeAll(children.at(i));
247 for (int i = 0; i < collindingItems.count(); i++)
248 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
253 shape = transitionGroupShape(newPos - pos());
254 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
255 for (int i = 0; i < children.count(); i++) {
256 collindingItems.removeAll(children.at(i));
258 if (collindingItems.isEmpty()) return newPos;
260 bool forwardMove = xpos > start.x();
262 for (int i = 0; i < collindingItems.count(); i++) {
263 QGraphicsItem *collision = collindingItems.at(i);
264 if (collision->type() == TRANSITIONWIDGET) {
266 if (newPos.y() != pos().y()) {
267 // Track change results in collision, restore original position
270 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
272 // Moving forward, determine best pos
273 QPainterPath clipPath;
274 clipPath.addRect(item->sceneBoundingRect());
275 QPainterPath res = shape.intersected(clipPath);
276 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
278 // Moving backward, determine best pos
279 QPainterPath clipPath;
280 clipPath.addRect(item->sceneBoundingRect());
281 QPainterPath res = shape.intersected(clipPath);
282 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
288 newPos.setX(newPos.x() - offset);
290 newPos.setX(newPos.x() + offset);
292 // If there is still a collision after our position adjust, restore original pos
293 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
294 for (int i = 0; i < children.count(); i++) {
295 collindingItems.removeAll(children.at(i));
297 for (int i = 0; i < collindingItems.count(); i++)
298 if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
303 return QGraphicsItemGroup::itemChange(change, value);
307 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
309 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
311 doc.setContent(effects, true);
312 QDomElement e = doc.documentElement();
313 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
314 if (view) view->slotAddGroupEffect(e, this);
318 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
320 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
323 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
329 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
331 if (event->modifiers() & Qt::ShiftModifier) {
332 // User want to do a rectangle selection, so ignore the event to pass it to the view
334 } else QGraphicsItem::mousePressEvent(event);