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);
44 int AbstractGroupItem::type() const
49 int AbstractGroupItem::track() const
51 return (int)(scenePos().y() / KdenliveSettings::trackheight());
54 CustomTrackScene* AbstractGroupItem::projectScene()
56 if (scene()) return static_cast <CustomTrackScene*>(scene());
60 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
63 QList<QGraphicsItem *> children = childItems();
64 for (int i = 0; i < children.count(); i++) {
65 if (children.at(i)->type() == AVWIDGET) {
66 QRectF r(children.at(i)->sceneBoundingRect());
74 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
77 QList<QGraphicsItem *> children = childItems();
78 for (int i = 0; i < children.count(); i++) {
79 if (children.at(i)->type() == TRANSITIONWIDGET) {
80 QRectF r(children.at(i)->sceneBoundingRect());
88 void AbstractGroupItem::addItem(QGraphicsItem * item)
94 void AbstractGroupItem::fixItemRect()
96 QPointF start = boundingRect().topLeft();
97 if (start != QPointF(0, 0)) {
98 translate(0 - start.x(), 0 - start.y());
103 /*ItemInfo AbstractGroupItem::info() const {
105 itemInfo.startPos = m_startPos;
106 itemInfo.track = m_track;
111 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
113 const double scale = option->matrix.m11();
114 QColor bgcolor(100, 100, 200, 100);
115 QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
116 p->setClipRect(bound);
117 p->fillRect(option->exposedRect, bgcolor);
119 pen.setColor(QColor(200, 90, 90));
120 pen.setStyle(Qt::DashLine);
122 //pen.setCosmetic(true);
124 p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
128 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
130 if (change == ItemPositionChange && scene() && parentItem() == 0) {
131 // calculate new position.
132 const int trackHeight = KdenliveSettings::trackheight();
133 QPointF start = sceneBoundingRect().topLeft();
134 QPointF newPos = value.toPointF();
135 //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
136 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
138 xpos = qMax(xpos, 0);
139 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
140 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
142 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
144 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
145 int proposedTrack = newPos.y() / trackHeight;
147 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
148 correctedTrack = qMax(correctedTrack, 0);
150 proposedTrack += (correctedTrack - realTrack);
152 // Check if top item is a clip or a transition
155 QList<QGraphicsItem *> children = childItems();
156 for (int i = 0; i < children.count(); i++) {
157 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
158 if (children.at(i)->type() == AVWIDGET) {
159 if (topTrack == -1 || currentTrack <= topTrack) {
161 topTrack = currentTrack;
163 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
164 if (topTrack == -1 || currentTrack < topTrack) {
165 offset = (int)(trackHeight / 3 * 2 - 1);
166 topTrack = currentTrack;
168 } else if (children.at(i)->type() == GROUPWIDGET) {
169 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
170 bool clipGroup = false;
171 for (int j = 0; j < subchildren.count(); j++) {
172 if (subchildren.at(j)->type() == AVWIDGET) {
178 if (topTrack == -1 || currentTrack <= topTrack) {
180 topTrack = currentTrack;
183 if (topTrack == -1 || currentTrack < topTrack) {
184 offset = (int)(trackHeight / 3 * 2 - 1);
185 topTrack = currentTrack;
190 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
191 //if (newPos == start) return start;
193 /*if (newPos.x() < 0) {
194 // If group goes below 0, adjust position to 0
195 return QPointF(pos().x() - start.x(), pos().y());
198 QPainterPath shape = clipGroupShape(newPos - pos());
199 QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
200 for (int i = 0; i < children.count(); i++) {
201 collindingItems.removeAll(children.at(i));
204 if (!collindingItems.isEmpty()) {
205 bool forwardMove = xpos > start.x();
207 for (int i = 0; i < collindingItems.count(); i++) {
208 QGraphicsItem *collision = collindingItems.at(i);
209 if (collision->type() == AVWIDGET) {
211 if (newPos.y() != pos().y()) {
212 // Track change results in collision, restore original position
215 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
217 // Moving forward, determine best pos
218 QPainterPath clipPath;
219 clipPath.addRect(item->sceneBoundingRect());
220 QPainterPath res = shape.intersected(clipPath);
221 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
223 // Moving backward, 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));
233 newPos.setX(newPos.x() - offset);
235 newPos.setX(newPos.x() + offset);
237 // If there is still a collision after our position adjust, restore original pos
238 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
239 for (int i = 0; i < children.count(); i++) {
240 collindingItems.removeAll(children.at(i));
242 for (int i = 0; i < collindingItems.count(); i++)
243 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
248 shape = transitionGroupShape(newPos - pos());
249 collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
250 for (int i = 0; i < children.count(); i++) {
251 collindingItems.removeAll(children.at(i));
253 if (collindingItems.isEmpty()) return newPos;
255 bool forwardMove = xpos > start.x();
257 for (int i = 0; i < collindingItems.count(); i++) {
258 QGraphicsItem *collision = collindingItems.at(i);
259 if (collision->type() == TRANSITIONWIDGET) {
261 if (newPos.y() != pos().y()) {
262 // Track change results in collision, restore original position
265 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
267 // Moving forward, determine best pos
268 QPainterPath clipPath;
269 clipPath.addRect(item->sceneBoundingRect());
270 QPainterPath res = shape.intersected(clipPath);
271 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
273 // Moving backward, determine best pos
274 QPainterPath clipPath;
275 clipPath.addRect(item->sceneBoundingRect());
276 QPainterPath res = shape.intersected(clipPath);
277 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
283 newPos.setX(newPos.x() - offset);
285 newPos.setX(newPos.x() + offset);
287 // If there is still a collision after our position adjust, restore original pos
288 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
289 for (int i = 0; i < children.count(); i++) {
290 collindingItems.removeAll(children.at(i));
292 for (int i = 0; i < collindingItems.count(); i++)
293 if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
298 return QGraphicsItemGroup::itemChange(change, value);
302 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
304 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
306 doc.setContent(effects, true);
307 QDomElement e = doc.documentElement();
308 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
309 if (view) view->slotAddGroupEffect(e, this);
313 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
315 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
318 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
324 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
326 if (event->modifiers() & Qt::ShiftModifier) {
327 // User want to do a rectangle selection, so ignore the event to pass it to the view
329 } else QGraphicsItem::mousePressEvent(event);