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) {
37 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
41 int AbstractGroupItem::type() const {
45 int AbstractGroupItem::track() const {
46 return (int)(scenePos().y() / KdenliveSettings::trackheight());
49 CustomTrackScene* AbstractGroupItem::projectScene() {
50 if (scene()) return static_cast <CustomTrackScene*>(scene());
54 QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
56 QList<QGraphicsItem *> children = childItems();
57 for (int i = 0; i < children.count(); i++) {
58 if (children.at(i)->type() == AVWIDGET) {
59 QRectF r(children.at(i)->sceneBoundingRect());
67 void AbstractGroupItem::addItem(QGraphicsItem * item) {
72 void AbstractGroupItem::fixItemRect() {
73 QPointF start = boundingRect().topLeft();
74 if (start != QPointF(0, 0)) {
75 translate(0 - start.x(), 0 - start.y());
81 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) {
82 p->fillRect(option->exposedRect, QColor(200, 100, 100, 100));
86 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
87 if (change == ItemPositionChange && scene()) {
88 // calculate new position.
89 const int trackHeight = KdenliveSettings::trackheight();
90 QPointF newPos = value.toPointF();
91 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
95 QPointF start = pos();
96 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
97 int newTrack = (newPos.y()) / trackHeight;
98 //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
99 newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
100 newTrack = qMax(newTrack, 0);
102 // Check if top item is a clip or a transition
105 QList<QGraphicsItem *> children = childItems();
106 for (int i = 0; i < children.count(); i++) {
107 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
108 if (children.at(i)->type() == AVWIDGET) {
109 if (topTrack == -1 || currentTrack <= topTrack) {
111 topTrack = currentTrack;
113 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
114 if (topTrack == -1 || currentTrack < topTrack) {
115 offset = (int)(trackHeight / 3 * 2 - 1);
116 topTrack = currentTrack;
120 newPos.setY((int)((newTrack) * trackHeight) + offset);
121 if (newPos == start) return start;
123 if (newPos.x() < 0) {
124 // If group goes below 0, adjust position to 0
125 return QPointF(pos().x() - start.x(), pos().y());
128 QPainterPath shape = groupShape(newPos - pos());
129 QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
130 for (int i = 0; i < children.count(); i++) {
131 collindingItems.removeAll(children.at(i));
133 if (collindingItems.isEmpty()) return newPos;
135 bool forwardMove = newPos.x() > start.x();
137 for (int i = 0; i < collindingItems.count(); i++) {
138 QGraphicsItem *collision = collindingItems.at(i);
139 if (collision->type() == AVWIDGET) {
141 //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
142 if (newPos.y() != start.y()) {
143 // Track change results in collision, restore original position
146 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
148 // Moving forward, determine best pos
149 QPainterPath clipPath;
150 clipPath.addRect(item->sceneBoundingRect());
151 QPainterPath res = shape.intersected(clipPath);
152 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
154 // Moving backward, determine best pos
155 QPainterPath clipPath;
156 clipPath.addRect(item->sceneBoundingRect());
157 QPainterPath res = shape.intersected(clipPath);
158 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
164 newPos.setX(newPos.x() - offset);
166 newPos.setX(newPos.x() + offset);
168 // If there is still a collision after our position adjust, restore original pos
169 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
170 for (int i = 0; i < children.count(); i++) {
171 collindingItems.removeAll(children.at(i));
173 for (int i = 0; i < collindingItems.count(); i++)
174 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
179 return QGraphicsItemGroup::itemChange(change, value);
183 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
184 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
186 doc.setContent(effects, true);
187 QDomElement e = doc.documentElement();
188 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
189 if (view) view->slotAddGroupEffect(e, this);
193 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
194 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
197 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {