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