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());
80 /*ItemInfo AbstractGroupItem::info() const {
82 itemInfo.startPos = m_startPos;
83 itemInfo.track = m_track;
88 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) {
89 const double scale = option->matrix.m11();
90 QRect clipRect = option->exposedRect.toRect();
91 clipRect.adjust(0, 0, 1 / scale + 0.5, 1);
92 p->fillRect(option->exposedRect, QColor(100, 100, 200, 100));
93 p->setClipRect(clipRect);
95 pen.setColor(QColor(200, 90, 90));
96 pen.setStyle(Qt::DashLine);
98 p->drawRect(boundingRect());
102 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
103 if (change == ItemPositionChange && scene()) {
104 // calculate new position.
105 const int trackHeight = KdenliveSettings::trackheight();
106 QPointF start = sceneBoundingRect().topLeft();
107 QPointF newPos = value.toPointF();
108 //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
109 int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
111 xpos = qMax(xpos, 0);
112 //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
113 newPos.setX((int)(pos().x() + xpos - (int) start.x()));
115 //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
117 int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
118 int proposedTrack = newPos.y() / trackHeight;
120 int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
121 correctedTrack = qMax(correctedTrack, 0);
123 proposedTrack += (correctedTrack - realTrack);
125 // Check if top item is a clip or a transition
128 QList<QGraphicsItem *> children = childItems();
129 for (int i = 0; i < children.count(); i++) {
130 int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
131 if (children.at(i)->type() == AVWIDGET) {
132 if (topTrack == -1 || currentTrack <= topTrack) {
134 topTrack = currentTrack;
136 } else if (children.at(i)->type() == TRANSITIONWIDGET) {
137 if (topTrack == -1 || currentTrack < topTrack) {
138 offset = (int)(trackHeight / 3 * 2 - 1);
139 topTrack = currentTrack;
143 newPos.setY((int)((proposedTrack) * trackHeight) + offset);
144 //if (newPos == start) return start;
146 /*if (newPos.x() < 0) {
147 // If group goes below 0, adjust position to 0
148 return QPointF(pos().x() - start.x(), pos().y());
150 QPainterPath shape = groupShape(newPos - pos());
151 QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
152 for (int i = 0; i < children.count(); i++) {
153 collindingItems.removeAll(children.at(i));
156 if (collindingItems.isEmpty()) return newPos;
158 bool forwardMove = xpos > start.x();
160 for (int i = 0; i < collindingItems.count(); i++) {
161 QGraphicsItem *collision = collindingItems.at(i);
162 if (collision->type() == AVWIDGET) {
164 //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
165 if (newPos.y() != pos().y()) {
166 // Track change results in collision, restore original position
169 AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
171 // Moving forward, determine best pos
172 QPainterPath clipPath;
173 clipPath.addRect(item->sceneBoundingRect());
174 QPainterPath res = shape.intersected(clipPath);
175 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
177 // Moving backward, determine best pos
178 QPainterPath clipPath;
179 clipPath.addRect(item->sceneBoundingRect());
180 QPainterPath res = shape.intersected(clipPath);
181 offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
187 newPos.setX(newPos.x() - offset);
189 newPos.setX(newPos.x() + offset);
191 // If there is still a collision after our position adjust, restore original pos
192 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
193 for (int i = 0; i < children.count(); i++) {
194 collindingItems.removeAll(children.at(i));
196 for (int i = 0; i < collindingItems.count(); i++)
197 if (collindingItems.at(i)->type() == AVWIDGET) return pos();
202 return QGraphicsItemGroup::itemChange(change, value);
206 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
207 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
209 doc.setContent(effects, true);
210 QDomElement e = doc.documentElement();
211 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
212 if (view) view->slotAddGroupEffect(e, this);
216 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
217 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
220 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {