]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
Fixes to clip fades
[kdenlive] / src / trackview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23 #include <QScrollBar>
24
25 #include <KDebug>
26
27 #include "definitions.h"
28 #include "documentvideotrack.h"
29 #include "documentaudiotrack.h"
30 #include "headertrack.h"
31 #include "trackview.h"
32 #include "clipitem.h"
33 #include "trackpanelclipmovefunction.h"
34
35 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
36     : QWidget(parent), m_doc(doc), m_scale(1.0), m_panelUnderMouse(NULL), m_function(NULL), m_projectTracks(0), m_projectDuration(0)
37 {
38   setMouseTracking(true);
39   view = new Ui::TimeLine_UI();
40   view->setupUi(this);
41   m_ruler = new CustomRuler(doc->timecode());
42   QVBoxLayout *layout = new QVBoxLayout;
43   layout->addWidget(m_ruler);
44   view->ruler_frame->setLayout(layout);
45
46   m_scene = new QGraphicsScene();
47   m_trackview = new CustomTrackView(m_doc->commandStack(), m_scene, this);
48   m_trackview->scale(1, 1);
49   m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
50   //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
51
52   m_headersLayout = new QVBoxLayout;
53   m_headersLayout->setContentsMargins (0, 0, 0, 0);
54   view->headers_frame->setLayout(m_headersLayout);
55
56   QVBoxLayout *tracksLayout = new QVBoxLayout;
57   tracksLayout->setContentsMargins (0, 0, 0, 0);
58   view->tracks_frame->setLayout(tracksLayout);
59   tracksLayout->addWidget(m_trackview);
60
61   parseDocument(doc->toXml());
62
63   TrackPanelClipMoveFunction *m_moveFunction = new TrackPanelClipMoveFunction(this);
64   registerFunction("move", m_moveFunction);
65   setEditMode("move");
66
67   connect(view->horizontalSlider, SIGNAL(valueChanged ( int )), this, SLOT(slotChangeZoom( int )));
68   connect(m_ruler, SIGNAL(cursorMoved ( int )), m_trackview, SLOT(setCursorPos( int )));
69   connect(m_trackview, SIGNAL(cursorMoved ( int )), this, SLOT(slotCursorMoved( int )));
70   connect(m_trackview, SIGNAL(zoomIn ()), this, SLOT(slotZoomIn()));
71   connect(m_trackview, SIGNAL(zoomOut ()), this, SLOT(slotZoomOut()));
72   connect(m_trackview->horizontalScrollBar(), SIGNAL(sliderMoved( int )), m_ruler, SLOT(slotMoveRuler( int )));
73
74   view->horizontalSlider->setValue(4);
75   m_currentZoom = view->horizontalSlider->value();
76   m_trackview->initView();
77 }
78
79 void TrackView::registerFunction(const QString & name, TrackPanelFunction * function) 
80 {
81   m_factory.registerFunction(name, function);
82 }
83
84 int TrackView::duration()
85 {
86   return m_projectDuration;
87 }
88
89 int TrackView::tracksNumber()
90 {
91   return m_projectTracks;
92 }
93
94 void TrackView::parseDocument(QDomDocument doc)
95 {
96   int cursorPos = 0;
97   QDomNode props = doc.elementsByTagName("properties").item(0);
98   if (!props.isNull()) {
99     cursorPos = props.toElement().attribute("timeline_position").toInt();
100   }
101   QDomNodeList tracks = doc.elementsByTagName("playlist");
102   m_projectDuration = 300;
103   m_projectTracks = tracks.count();
104   int duration = 0;
105   kDebug()<<"//////////// TIMELINE FOUND: "<<m_projectTracks<<" tracks";
106   for (int i = 0; i < m_projectTracks; i++)
107   {
108     if (tracks.item(i).toElement().attribute("hide", QString::null) == "video") {
109       // this is an audio track
110       duration = slotAddAudioTrack(i, tracks.item(i).toElement());
111     }
112     else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
113       duration = slotAddVideoTrack(i, tracks.item(i).toElement());
114     kDebug()<<" PRO DUR: "<<m_projectDuration<<", TRACK DUR: "<<duration;
115     if (duration > m_projectDuration) m_projectDuration = duration;
116   }
117   m_trackview->setDuration(m_projectDuration);
118   slotCursorMoved(cursorPos, true);
119   //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
120 }
121
122 void TrackView::slotCursorMoved(int pos, bool emitSignal)
123 {
124   kDebug()<<"///// CURSOR: "<<pos;
125   m_ruler->slotNewValue(pos * FRAME_SIZE, emitSignal); //(int) m_trackview->mapToScene(QPoint(pos, 0)).x());
126   //m_trackview->setCursorPos(pos);
127   //m_trackview->invalidateScene(QRectF(), QGraphicsScene::ForegroundLayer);
128 }
129
130 void TrackView::slotChangeZoom(int factor)
131 {
132   m_ruler->setPixelPerMark(factor);
133   m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] / 
134   m_currentZoom = factor;
135   m_trackview->setScale(m_scale);
136   m_trackview->centerOn(QPointF(m_trackview->cursorPos(), 50));
137 }
138
139 const double TrackView::zoomFactor() const
140 {
141   return m_scale;
142 }
143
144 void TrackView::slotZoomIn()
145 {
146   view->horizontalSlider->setValue(view->horizontalSlider->value() - 1);
147 }
148
149 void TrackView::slotZoomOut()
150 {
151   view->horizontalSlider->setValue(view->horizontalSlider->value() + 1);
152 }
153
154 const int TrackView::mapLocalToValue(int x) const
155 {
156   return (int) x * zoomFactor();
157 }
158
159 KdenliveDoc *TrackView::document()
160 {
161   return m_doc;
162 }
163
164 int TrackView::slotAddAudioTrack(int ix, QDomElement xml)
165 {
166   kDebug()<<"*************  ADD AUDIO TRACK "<<ix;
167   m_trackview->addTrack();
168   DocumentTrack *track = new DocumentAudioTrack(xml, this, m_trackview);
169   HeaderTrack *header = new HeaderTrack();
170   //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
171   m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
172   documentTracks.insert(ix, track);
173   return 0;
174   //track->show();
175 }
176
177 int TrackView::slotAddVideoTrack(int ix, QDomElement xml)
178 {
179   m_trackview->addTrack();
180   DocumentTrack *track = new DocumentVideoTrack(xml, this, m_trackview);
181   HeaderTrack *header = new HeaderTrack();
182   int trackTop = 50 * ix;
183   int trackBottom = trackTop + 50;
184   // parse track
185   int position = 0;
186   for(QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling())
187   {
188     QDomElement elem = n.toElement();
189     if (elem.tagName() == "blank") {
190       position += elem.attribute("length", 0).toInt();
191     }
192     else if (elem.tagName() == "entry") {
193     int in = elem.attribute("in", 0).toInt();
194     int out = elem.attribute("out", 0).toInt() - in;
195     //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
196     ClipItem *item = new ClipItem(elem, ix, in, QRectF(position * m_scale, trackTop + 1, out * m_scale, 49), out);
197     m_scene->addItem(item);
198     position += out;
199
200     //m_clipList.append(clip);
201    }
202   }
203   //m_trackDuration = position;
204
205   //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
206   m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
207   documentTracks.insert(ix, track);
208   kDebug()<<"*************  ADD VIDEO TRACK "<<ix<<", DURATION: "<<position;
209   return position;
210   //track->show();
211 }
212
213 DocumentTrack *TrackView::panelAt(int y)
214 {
215   return NULL;
216 }
217
218 QGraphicsScene *TrackView::projectScene()
219 {
220   return m_scene;
221 }
222
223 CustomTrackView *TrackView::projectView()
224 {
225   return m_trackview;
226 }
227
228 void TrackView::setEditMode(const QString & editMode)
229 {
230   m_editMode = editMode;
231 }
232
233 const QString & TrackView::editMode() const
234 {
235   return m_editMode;
236 }
237
238 /** This event occurs when the mouse has been moved. */
239     void TrackView::mouseMoveEvent(QMouseEvent * event) {
240         if (m_panelUnderMouse) {
241             if (event->buttons() & Qt::LeftButton) {
242                 bool result = false;
243                 if (m_function)
244                     result =
245                         m_function->mouseMoved(m_panelUnderMouse, event);
246                 if (!result) {
247                     m_panelUnderMouse = 0;
248                     m_function = 0;
249                 }
250             } else {
251                 if (m_function) {
252                     m_function->mouseReleased(m_panelUnderMouse, event);
253                     m_function = 0;
254                 }
255                 m_panelUnderMouse = 0;
256             }
257         } else {
258             DocumentTrack *panel = panelAt(event->y());
259             if (panel) {
260                 QCursor result(Qt::ArrowCursor);
261
262                 TrackPanelFunction *function =
263                     getApplicableFunction(panel, editMode(),
264                     event);
265                 if (function)
266                     result = function->getMouseCursor(panel, event);
267
268                 setCursor(result);
269             } else {
270                 setCursor(QCursor(Qt::ArrowCursor));
271             }
272         }
273     }
274
275     TrackPanelFunction *TrackView::getApplicableFunction(DocumentTrack *
276         panel, const QString & editMode, QMouseEvent * event) {
277         TrackPanelFunction *function = 0;
278
279         QStringList list = panel->applicableFunctions(editMode);
280         QStringList::iterator itt = list.begin();
281
282         while (itt != list.end()) {
283             TrackPanelFunction *testFunction = m_factory.function(*itt);
284             if (testFunction) {
285                 if (testFunction->mouseApplies(panel, event)) {
286                     function = testFunction;
287                     break;
288                 }
289             }
290
291             ++itt;
292         }
293
294         return function;
295     }
296
297
298 #include "trackview.moc"