]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Rewrote the handling of timeline in QGraphicsView. Now we use the built in zoom featu...
[kdenlive] / src / abstractgroupitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de)              *
3  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QGraphicsScene>
22 #include <QGraphicsView>
23 #include <QScrollBar>
24 #include <QToolTip>
25
26 #include <KDebug>
27 #include <KLocale>
28
29 #include "abstractgroupitem.h"
30 #include "abstractclipitem.h"
31 #include "kdenlivesettings.h"
32 #include "customtrackscene.h"
33
34 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
35     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
36 }
37
38 int AbstractGroupItem::type() const {
39     return GROUPWIDGET;
40 }
41
42 CustomTrackScene* AbstractGroupItem::projectScene() {
43     return static_cast <CustomTrackScene*>(scene());
44 }
45
46
47 QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
48     QList<QGraphicsItem *> children = childItems();
49     QPainterPath path;
50     for (int i = 0; i < children.count(); i++) {
51         QRectF r = children.at(i)->sceneBoundingRect();
52         //kDebug()<<"// GROUP CHild: "<<r;
53         //r.translate(offset);
54         path.addRect(r);
55     }
56     return path;
57 }
58
59 void AbstractGroupItem::addItem(QGraphicsItem * item) {
60     addToGroup(item);
61     //fixItemRect();
62 }
63
64 void AbstractGroupItem::fixItemRect() {
65     QPointF start = boundingRect().topLeft();
66     if (start != QPointF(0, 0)) {
67         translate(0 - start.x(), 0 - start.y());
68         setPos(start);
69     }
70 }
71
72 // virtual
73 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) {
74     p->fillRect(boundingRect(), QColor(200, 100, 100, 100));
75 }
76
77 //virtual
78 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
79     if (change == ItemPositionChange && scene()) {
80         // calculate new position.
81         QPointF newPos = value.toPointF();
82         QPainterPath sceneShape = groupShape(newPos);
83         QPointF start = sceneBoundingRect().topLeft();
84         QPointF sc = mapToScene(pos());
85         int posx = start.x() + newPos.x(); //projectScene()->getSnapPointForPos(start.x() + sc.x(), KdenliveSettings::snaptopoints());
86         //int startx = projectScene()->getSnapPointForPos(start.x(), false);
87         //int startx = projectScene()->getSnapPointForPos(start.x(), false);
88         kDebug() << "------------------------------------";
89         kDebug() << "BRect: " << start.x() << "diff: " << newPos.x() << ",mapd: " << start.x() - sc.x();
90         return newPos;
91         //kDebug()<<"BR: "<<start.x()<<",NP: "<<newPos.x()<<",MAPD: "<<sc.x()<<",POS: "<<pos().x();
92         if (start.x() <= 0) {
93             //kDebug()<<"/// GOING UNDER 0, POS: "<<posx<<", ADJUSTED: items.at(i)->sceneBoundingRect();
94             return pos();
95         }
96         //else posx -= startx;
97         //posx = qMax(posx, 0);
98         newPos.setX(posx);
99         //kDebug()<<"Y POS: "<<start.y() + newPos.y()<<"SCN MP: "<<sc;
100         int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
101         int oldTrack = (start.y() + pos().y()) / KdenliveSettings::trackheight();
102         newPos.setY((newTrack) * KdenliveSettings::trackheight() - start.y() + 1);
103
104
105         //if (start.y() + newPos.y() < 1)  newTrack = oldTrack;
106
107         return newPos;
108
109         // Only one clip is moving
110
111         QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
112
113         QList<QGraphicsItem *> children = childItems();
114         for (int i = 0; i < children.count(); i++) {
115             items.removeAll(children.at(i));
116         }
117
118
119
120         if (!items.isEmpty()) {
121             for (int i = 0; i < items.count(); i++) {
122                 if (items.at(i)->type() == AVWIDGET) {
123                     // Collision!
124                     //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->sceneBoundingRect();
125                     return pos();
126                     QPointF otherPos = items.at(i)->pos();
127                     if ((int) otherPos.y() != (int) pos().y()) return pos();
128                     if (pos().x() < otherPos.x()) {
129                         // move clip just before colliding clip
130                         int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos()).frames(m_fps) - sceneBoundingRect().width();
131                         newPos.setX(npos);
132                     } else {
133                         // get pos just after colliding clip
134                         int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
135                         newPos.setX(npos);
136                     }
137                     return newPos;
138                 }
139             }
140         }
141         return newPos;
142     }
143     return QGraphicsItem::itemChange(change, value);
144 }
145