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>
35 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps)
38 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
42 int AbstractGroupItem::type() const
47 int AbstractGroupItem::track() const
49 return (int)(scenePos().y() / KdenliveSettings::trackheight());
52 CustomTrackScene* AbstractGroupItem::projectScene()
54 if (scene()) return static_cast <CustomTrackScene*>(scene());
58 QPainterPath AbstractGroupItem::groupShape(QPointF offset)
61 QList<QGraphicsItem *> children = childItems();
62 for (int i = 0; i < children.count(); i++) {
63 if (children.at(i)->type() == AVWIDGET) {
64 QRectF r(children.at(i)->sceneBoundingRect());
72 void AbstractGroupItem::addItem(QGraphicsItem * item)
78 void AbstractGroupItem::fixItemRect()
80 QPointF start = boundingRect().topLeft();
81 if (start != QPointF(0, 0)) {
82 translate(0 - start.x(), 0 - start.y());
87 /*ItemInfo AbstractGroupItem::info() const {
89 itemInfo.startPos = m_startPos;
90 itemInfo.track = m_track;
95 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
97 const double scale = option->matrix.m11();
98 QRect clipRect = option->exposedRect.toRect();
99 clipRect.adjust(0, 0, 1 / scale + 0.5, 1);
100 p->fillRect(option->exposedRect, QColor(100, 100, 200, 100));
101 p->setClipRect(clipRect);
103 pen.setColor(QColor(200, 90, 90));
104 pen.setStyle(Qt::DashLine);
106 p->drawRect(boundingRect());
110 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
112 if (change == ItemPositionChange && scene()) {
113 // calculate new position.
114 const int trackHeight = KdenliveSettings::trackheight();
115 QPointF start = sceneBoundingRect().topLeft();
116 QPointF newPos = value.toPointF();
117 //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
118 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
120 xpos = qMax(xpos, 0);
121 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
122 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
124 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
126 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
127 int proposedTrack = newPos.y() / trackHeight;
129 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
130 correctedTrack = qMax(correctedTrack, 0);
132 proposedTrack += (correctedTrack - realTrack);
134 // Check if top item is a clip or a transition
137 QList<QGraphicsItem *> children = childItems();
138 for (int i = 0; i < children.count(); i++) {
139 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
140 if (children.at(i)->type() == AVWIDGET) {
141 if (topTrack == -1 || currentTrack <= topTrack) {
143 topTrack = currentTrack;
145 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
146 if (topTrack == -1 || currentTrack < topTrack) {
147 offset = (int)(trackHeight / 3 * 2 - 1);
148 topTrack = currentTrack;
152 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
153 //if (newPos == start) return start;
155 /*if (newPos.x() < 0) {
156 // If group goes below 0, adjust position to 0
157 return QPointF(pos().x() - start.x(), pos().y());
159 QPainterPath shape = groupShape(newPos - pos());
160 QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
161 for (int i = 0; i < children.count(); i++) {
162 collindingItems.removeAll(children.at(i));
165 if (collindingItems.isEmpty()) return newPos;
167 bool forwardMove = xpos > start.x();
169 for (int i = 0; i < collindingItems.count(); i++) {
170 QGraphicsItem *collision = collindingItems.at(i);
171 if (collision->type() == AVWIDGET) {
173 //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
174 if (newPos.y() != pos().y()) {
175 // Track change results in collision, restore original position
178 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
180 // Moving forward, determine best pos
181 QPainterPath clipPath;
182 clipPath.addRect(item->sceneBoundingRect());
183 QPainterPath res = shape.intersected(clipPath);
184 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
186 // Moving backward, determine best pos
187 QPainterPath clipPath;
188 clipPath.addRect(item->sceneBoundingRect());
189 QPainterPath res = shape.intersected(clipPath);
190 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
196 newPos.setX(newPos.x() - offset);
198 newPos.setX(newPos.x() + offset);
200 // If there is still a collision after our position adjust, restore original pos
201 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
202 for (int i = 0; i < children.count(); i++) {
203 collindingItems.removeAll(children.at(i));
205 for (int i = 0; i < collindingItems.count(); i++)
206 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
211 return QGraphicsItemGroup::itemChange(change, value);
215 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
217 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
219 doc.setContent(effects, true);
220 QDomElement e = doc.documentElement();
221 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
222 if (view) view->slotAddGroupEffect(e, this);
226 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
228 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
231 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)