1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
27 #include "definitions.h"
28 #include "documentvideotrack.h"
29 #include "documentaudiotrack.h"
30 #include "headertrack.h"
31 #include "trackview.h"
33 #include "trackpanelclipmovefunction.h"
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)
38 setMouseTracking(true);
39 view = new Ui::TimeLine_UI();
41 m_ruler = new CustomRuler(doc->timecode());
42 QVBoxLayout *layout = new QVBoxLayout;
43 view->ruler_frame->setLayout(layout);
44 layout->addWidget(m_ruler);
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));
52 m_headersLayout = new QVBoxLayout;
53 m_headersLayout->setContentsMargins (0, 0, 0, 0);
54 view->headers_frame->setLayout(m_headersLayout);
56 QVBoxLayout *tracksLayout = new QVBoxLayout;
57 tracksLayout->setContentsMargins (0, 0, 0, 0);
58 view->tracks_frame->setLayout(tracksLayout);
59 tracksLayout->addWidget(m_trackview);
61 parseDocument(doc->toXml());
63 TrackPanelClipMoveFunction *m_moveFunction = new TrackPanelClipMoveFunction(this);
64 registerFunction("move", m_moveFunction);
65 setEditMode("move");*/
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 connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)),this,SLOT(slotClipItemSelected(ClipItem*)));
75 view->horizontalSlider->setValue(4);
76 m_currentZoom = view->horizontalSlider->value();
77 m_trackview->initView();
80 void TrackView::registerFunction(const QString & name, TrackPanelFunction * function)
82 m_factory.registerFunction(name, function);
85 int TrackView::duration()
87 return m_projectDuration;
90 int TrackView::tracksNumber()
92 return m_projectTracks;
95 void TrackView::slotClipItemSelected(ClipItem*c){
96 emit clipItemSelected(c);
99 void TrackView::parseDocument(QDomDocument doc)
102 kDebug()<<"//// DOCUMENT: "<<doc.toString();
103 QDomNode props = doc.elementsByTagName("properties").item(0);
104 if (!props.isNull()) {
105 cursorPos = props.toElement().attribute("timeline_position").toInt();
107 QDomNodeList tracks = doc.elementsByTagName("playlist");
108 m_projectDuration = 300;
109 m_projectTracks = tracks.count();
111 kDebug()<<"//////////// TIMELINE FOUND: "<<m_projectTracks<<" tracks";
112 for (int i = 0; i < m_projectTracks; i++)
114 if (tracks.item(i).toElement().attribute("hide", QString::null) == "video") {
115 // this is an audio track
116 duration = slotAddAudioTrack(i, tracks.item(i).toElement());
118 else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
119 duration = slotAddVideoTrack(i, tracks.item(i).toElement());
120 kDebug()<<" PRO DUR: "<<m_projectDuration<<", TRACK DUR: "<<duration;
121 if (duration > m_projectDuration) m_projectDuration = duration;
123 m_trackview->setDuration(m_projectDuration);
124 slotCursorMoved(cursorPos, true);
125 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
128 void TrackView::slotDeleteClip(int clipId)
130 m_trackview->deleteClip(clipId);
133 void TrackView::setCursorPos(int pos)
136 m_trackview->setCursorPos(pos * m_scale);
139 void TrackView::moveCursorPos(int pos)
141 m_trackview->setCursorPos(pos * m_scale, false);
142 m_ruler->slotNewValue(pos * FRAME_SIZE, false);
145 void TrackView::slotCursorMoved(int pos, bool emitSignal)
147 m_ruler->slotNewValue(pos * FRAME_SIZE / m_scale, emitSignal); //(int) m_trackview->mapToScene(QPoint(pos, 0)).x());
148 //m_trackview->setCursorPos(pos);
149 //m_trackview->invalidateScene(QRectF(), QGraphicsScene::ForegroundLayer);
152 void TrackView::slotChangeZoom(int factor)
154 double pos = m_trackview->cursorPos() / m_scale;
155 m_ruler->setPixelPerMark(factor);
156 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
157 m_currentZoom = factor;
158 m_trackview->setScale(m_scale);
159 m_trackview->setCursorPos(pos * m_scale, false);
160 m_ruler->slotNewValue(pos * FRAME_SIZE, false);
161 m_trackview->centerOn(QPointF(m_trackview->cursorPos(), 50));
164 const double TrackView::zoomFactor() const
169 void TrackView::slotZoomIn()
171 view->horizontalSlider->setValue(view->horizontalSlider->value() - 1);
174 void TrackView::slotZoomOut()
176 view->horizontalSlider->setValue(view->horizontalSlider->value() + 1);
179 const int TrackView::mapLocalToValue(int x) const
181 return (int) x * zoomFactor();
184 KdenliveDoc *TrackView::document()
189 int TrackView::slotAddAudioTrack(int ix, QDomElement xml)
191 kDebug()<<"************* ADD AUDIO TRACK "<<ix;
192 m_trackview->addTrack();
193 //DocumentTrack *track = new DocumentAudioTrack(xml, this, m_trackview);
194 HeaderTrack *header = new HeaderTrack();
195 //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
196 m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
197 //documentTracks.insert(ix, track);
202 int TrackView::slotAddVideoTrack(int ix, QDomElement xml)
204 m_trackview->addTrack();
205 //DocumentTrack *track = new DocumentVideoTrack(xml, this, m_trackview);
206 HeaderTrack *header = new HeaderTrack();
207 int trackTop = 50 * ix;
208 int trackBottom = trackTop + 50;
211 for(QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling())
213 QDomElement elem = n.toElement();
214 if (elem.tagName() == "blank") {
215 position += elem.attribute("length", 0).toInt();
217 else if (elem.tagName() == "entry") {
218 int in = elem.attribute("in", 0).toInt();
219 int id = elem.attribute("producer", 0).toInt();
220 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
221 int out = elem.attribute("out", 0).toInt() - in;
222 //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
223 ClipItem *item = new ClipItem(clip, ix, position, QRectF(position * m_scale, trackTop + 1, out * m_scale, 49), out);
224 m_scene->addItem(item);
227 //m_clipList.append(clip);
230 //m_trackDuration = position;
232 //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
233 m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
234 //documentTracks.insert(ix, track);
235 kDebug()<<"************* ADD VIDEO TRACK "<<ix<<", DURATION: "<<position;
240 DocumentTrack *TrackView::panelAt(int y)
245 QGraphicsScene *TrackView::projectScene()
250 CustomTrackView *TrackView::projectView()
255 void TrackView::setEditMode(const QString & editMode)
257 m_editMode = editMode;
260 const QString & TrackView::editMode() const
265 /** This event occurs when the mouse has been moved. */
266 void TrackView::mouseMoveEvent(QMouseEvent * event) {
267 if (m_panelUnderMouse) {
268 if (event->buttons() & Qt::LeftButton) {
272 m_function->mouseMoved(m_panelUnderMouse, event);
274 m_panelUnderMouse = 0;
279 m_function->mouseReleased(m_panelUnderMouse, event);
282 m_panelUnderMouse = 0;
285 DocumentTrack *panel = panelAt(event->y());
287 QCursor result(Qt::ArrowCursor);
289 TrackPanelFunction *function =
290 getApplicableFunction(panel, editMode(),
293 result = function->getMouseCursor(panel, event);
297 setCursor(QCursor(Qt::ArrowCursor));
302 TrackPanelFunction *TrackView::getApplicableFunction(DocumentTrack *
303 panel, const QString & editMode, QMouseEvent * event) {
304 TrackPanelFunction *function = 0;
306 QStringList list = panel->applicableFunctions(editMode);
307 QStringList::iterator itt = list.begin();
309 while (itt != list.end()) {
310 TrackPanelFunction *testFunction = m_factory.function(*itt);
312 if (testFunction->mouseApplies(panel, event)) {
313 function = testFunction;
325 #include "trackview.moc"