]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
Start fixing timeline view/ruler sync
[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(FRAME_SIZE, 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   view->horizontalSlider->setValue(0);
68   m_currentZoom = view->horizontalSlider->value();
69   connect(view->horizontalSlider, SIGNAL(valueChanged ( int )), this, SLOT(slotChangeZoom( int )));
70   connect(m_ruler, SIGNAL(cursorMoved ( int )), m_trackview, SLOT(setCursorPos( int )));
71   connect(m_trackview, SIGNAL(cursorMoved ( int )), this, SLOT(slotCursorMoved( int )));
72   connect(m_trackview, SIGNAL(zoomIn ()), this, SLOT(slotZoomIn()));
73   connect(m_trackview, SIGNAL(zoomOut ()), this, SLOT(slotZoomOut()));
74   connect(m_trackview->horizontalScrollBar(), SIGNAL(sliderMoved( int )), m_ruler, SLOT(slotMoveRuler( int )));
75   m_trackview->initView();
76 }
77
78 void TrackView::registerFunction(const QString & name, TrackPanelFunction * function) 
79 {
80   m_factory.registerFunction(name, function);
81 }
82
83 int TrackView::duration()
84 {
85   return m_projectDuration;
86 }
87
88 int TrackView::tracksNumber()
89 {
90   return m_projectTracks;
91 }
92
93 void TrackView::parseDocument(QDomDocument doc)
94 {
95   QDomNodeList tracks = doc.elementsByTagName("playlist");
96   m_projectDuration = 300;
97   m_projectTracks = tracks.count();
98   int duration;
99   for (int i = 0; i < tracks.count(); i++)
100   {
101     if (tracks.item(i).toElement().attribute("hide", QString::null) == "video") {
102       // this is an audio track
103       duration = slotAddAudioTrack(i, tracks.item(i).toElement());
104     }
105     else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
106       duration = slotAddVideoTrack(i, tracks.item(i).toElement());
107     if (duration > m_projectDuration) m_projectDuration = duration;
108   }
109   //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
110 }
111
112 void TrackView::slotCursorMoved(int pos)
113 {
114   kDebug()<<"///// CURSOR: "<<pos;
115   m_ruler->slotNewValue(pos * FRAME_SIZE); //(int) m_trackview->mapToScene(QPoint(pos, 0)).x());
116   //m_trackview->setCursorPos(pos);
117   //m_trackview->invalidateScene(QRectF(), QGraphicsScene::ForegroundLayer);
118 }
119
120 void TrackView::slotChangeZoom(int factor)
121 {
122   m_ruler->setPixelPerMark(factor);
123   m_scale = (double) m_ruler->comboScale[m_currentZoom] / m_ruler->comboScale[factor];
124   m_currentZoom = factor;
125   m_trackview->scale(m_scale, 1);
126   m_trackview->centerOn(QPointF(m_trackview->cursorPos(), 50));
127 }
128
129 const double TrackView::zoomFactor() const
130 {
131   return m_scale * FRAME_SIZE;
132 }
133
134 void TrackView::slotZoomIn()
135 {
136   view->horizontalSlider->setValue(view->horizontalSlider->value() - 1);
137 }
138
139 void TrackView::slotZoomOut()
140 {
141   view->horizontalSlider->setValue(view->horizontalSlider->value() + 1);
142 }
143
144 const int TrackView::mapLocalToValue(int x) const
145 {
146   return (int) x * zoomFactor();
147 }
148
149 KdenliveDoc *TrackView::document()
150 {
151   return m_doc;
152 }
153
154 int TrackView::slotAddAudioTrack(int ix, QDomElement xml)
155 {
156   m_trackview->addTrack();
157   DocumentTrack *track = new DocumentAudioTrack(xml, this, m_trackview);
158   HeaderTrack *header = new HeaderTrack();
159   //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
160   m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
161   documentTracks.insert(ix, track);
162   return track->duration();
163   //track->show();
164 }
165
166 int TrackView::slotAddVideoTrack(int ix, QDomElement xml)
167 {
168   m_trackview->addTrack();
169   DocumentTrack *track = new DocumentVideoTrack(xml, this, m_trackview);
170   HeaderTrack *header = new HeaderTrack();
171   int trackTop = 50 * ix;
172   int trackBottom = trackTop + 50;
173   // parse track
174   int position = 0;
175   for(QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling())
176   {
177     QDomElement elem = n.toElement();
178     if (elem.tagName() == "blank") {
179       position += elem.attribute("length", 0).toInt();
180     }
181     else if (elem.tagName() == "entry") {
182     int in = elem.attribute("in", 0).toInt();
183     int out = elem.attribute("out", 0).toInt() - in;
184     QString clipName = m_doc->producerName(elem.attribute("producer").toInt());
185     int clipMaxDuration = m_doc->getProducerDuration(elem.attribute("producer").toInt());
186     //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
187     ClipItem *item = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("producer").toInt(), clipMaxDuration, QRectF(position, trackTop + 1, out, 49));
188     m_scene->addItem(item);
189     position += out;
190
191     //m_clipList.append(clip);
192    }
193   }
194   //m_trackDuration = position;
195
196   //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
197   m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
198   documentTracks.insert(ix, track);
199   return position;
200   //track->show();
201 }
202
203 DocumentTrack *TrackView::panelAt(int y)
204 {
205   return NULL;
206 }
207
208 QGraphicsScene *TrackView::projectScene()
209 {
210   return m_scene;
211 }
212
213 CustomTrackView *TrackView::projectView()
214 {
215   return m_trackview;
216 }
217
218 void TrackView::setEditMode(const QString & editMode)
219 {
220   m_editMode = editMode;
221 }
222
223 const QString & TrackView::editMode() const
224 {
225   return m_editMode;
226 }
227
228 /** This event occurs when the mouse has been moved. */
229     void TrackView::mouseMoveEvent(QMouseEvent * event) {
230     kDebug()<<"--------  TRACKVIEW MOUSE MOVE EVENT -----";
231         if (m_panelUnderMouse) {
232             if (event->buttons() & Qt::LeftButton) {
233                 bool result = false;
234                 if (m_function)
235                     result =
236                         m_function->mouseMoved(m_panelUnderMouse, event);
237                 if (!result) {
238                     m_panelUnderMouse = 0;
239                     m_function = 0;
240                 }
241             } else {
242                 if (m_function) {
243                     m_function->mouseReleased(m_panelUnderMouse, event);
244                     m_function = 0;
245                 }
246                 m_panelUnderMouse = 0;
247             }
248         } else {
249             DocumentTrack *panel = panelAt(event->y());
250             if (panel) {
251                 QCursor result(Qt::ArrowCursor);
252
253                 TrackPanelFunction *function =
254                     getApplicableFunction(panel, editMode(),
255                     event);
256                 if (function)
257                     result = function->getMouseCursor(panel, event);
258
259                 setCursor(result);
260             } else {
261                 setCursor(QCursor(Qt::ArrowCursor));
262             }
263         }
264     }
265
266     TrackPanelFunction *TrackView::getApplicableFunction(DocumentTrack *
267         panel, const QString & editMode, QMouseEvent * event) {
268         TrackPanelFunction *function = 0;
269
270         QStringList list = panel->applicableFunctions(editMode);
271         QStringList::iterator itt = list.begin();
272
273         while (itt != list.end()) {
274             TrackPanelFunction *testFunction = m_factory.function(*itt);
275             if (testFunction) {
276                 if (testFunction->mouseApplies(panel, event)) {
277                     function = testFunction;
278                     break;
279                 }
280             }
281
282             ++itt;
283         }
284
285         return function;
286     }
287
288
289 #include "trackview.moc"