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 QList<QGraphicsItem*> collindingItems;
203 if (projectScene()->editMode() == NORMALEDIT) {
204 shape = clipGroupShape(newPos - pos());
205 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
206 for (int i = 0; i < children.count(); i++) {
207 collindingItems.removeAll(children.at(i));
210 if (!collindingItems.isEmpty()) {
211 bool forwardMove = xpos > start.x();
213 for (int i = 0; i < collindingItems.count(); i++) {
214 QGraphicsItem *collision = collindingItems.at(i);
215 if (collision->type() == AVWIDGET) {
217 if (newPos.y() != pos().y()) {
218 // Track change results in collision, restore original position
221 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
223 // Moving forward, determine best pos
224 QPainterPath clipPath;
225 clipPath.addRect(item->sceneBoundingRect());
226 QPainterPath res = shape.intersected(clipPath);
227 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
229 // Moving backward, determine best pos
230 QPainterPath clipPath;
231 clipPath.addRect(item->sceneBoundingRect());
232 QPainterPath res = shape.intersected(clipPath);
233 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
239 newPos.setX(newPos.x() - offset);
241 newPos.setX(newPos.x() + offset);
243 // If there is still a collision after our position adjust, restore original pos
244 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
245 for (int i = 0; i < children.count(); i++) {
246 collindingItems.removeAll(children.at(i));
248 for (int i = 0; i < collindingItems.count(); i++)
249 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
253 if (projectScene()->editMode() == NORMALEDIT) {
254 shape = transitionGroupShape(newPos - pos());
255 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
256 for (int i = 0; i < children.count(); i++) {
257 collindingItems.removeAll(children.at(i));
260 if (collindingItems.isEmpty()) return newPos;
262 bool forwardMove = xpos > start.x();
264 for (int i = 0; i < collindingItems.count(); i++) {
265 QGraphicsItem *collision = collindingItems.at(i);
266 if (collision->type() == TRANSITIONWIDGET) {
268 if (newPos.y() != pos().y()) {
269 // Track change results in collision, restore original position
272 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
274 // Moving forward, 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));
280 // Moving backward, determine best pos
281 QPainterPath clipPath;
282 clipPath.addRect(item->sceneBoundingRect());
283 QPainterPath res = shape.intersected(clipPath);
284 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
290 newPos.setX(newPos.x() - offset);
292 newPos.setX(newPos.x() + offset);
294 // If there is still a collision after our position adjust, restore original pos
295 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
296 for (int i = 0; i < children.count(); i++) {
297 collindingItems.removeAll(children.at(i));
299 for (int i = 0; i < collindingItems.count(); i++)
300 if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
305 return QGraphicsItemGroup::itemChange(change, value);
309 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
311 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
313 doc.setContent(effects, true);
314 QDomElement e = doc.documentElement();
315 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
316 if (view) view->slotAddGroupEffect(e, this);
320 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
322 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
325 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
331 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
333 if (event->modifiers() & Qt::ShiftModifier) {
334 // User want to do a rectangle selection, so ignore the event to pass it to the view
336 } else QGraphicsItem::mousePressEvent(event);