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 layout->addWidget(m_ruler);
44 view->ruler_frame->setLayout(layout);
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));
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);
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 )), this, SLOT(slotCursorMoved( 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 m_trackview->initView();
77 void TrackView::registerFunction(const QString & name, TrackPanelFunction * function)
79 m_factory.registerFunction(name, function);
82 int TrackView::duration()
84 return m_projectDuration;
87 int TrackView::tracksNumber()
89 return m_projectTracks;
92 void TrackView::parseDocument(QDomDocument doc)
94 QDomNodeList tracks = doc.elementsByTagName("playlist");
95 m_projectDuration = 300;
96 m_projectTracks = tracks.count();
98 for (int i = 0; i < tracks.count(); i++)
100 if (tracks.item(i).toElement().attribute("hide", QString::null) == "video") {
101 // this is an audio track
102 duration = slotAddAudioTrack(i, tracks.item(i).toElement());
104 else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
105 duration = slotAddVideoTrack(i, tracks.item(i).toElement());
106 if (duration > m_projectDuration) m_projectDuration = duration;
108 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
111 void TrackView::slotCursorMoved(int pos)
113 //kDebug()<<"///// CURSOR: "<<pos;
114 m_ruler->slotNewValue(m_trackview->mapToScene(QPoint(pos, 0)).x());
115 m_trackview->setCursorPos(pos);
116 //m_trackview->invalidateScene(QRectF(), QGraphicsScene::ForegroundLayer);
119 void TrackView::slotChangeZoom(int factor)
121 m_ruler->setPixelPerMark(factor);
122 m_scale = (double) m_ruler->comboScale[m_currentZoom] / m_ruler->comboScale[factor];
123 m_currentZoom = factor;
124 m_trackview->scale(m_scale, 1);
125 m_trackview->centerOn(QPointF(m_trackview->cursorPos(), 50));
128 const double TrackView::zoomFactor() const
130 return m_scale * FRAME_SIZE;
133 void TrackView::slotZoomIn()
135 view->horizontalSlider->setValue(view->horizontalSlider->value() - 1);
138 void TrackView::slotZoomOut()
140 view->horizontalSlider->setValue(view->horizontalSlider->value() + 1);
143 const int TrackView::mapLocalToValue(int x) const
145 return (int) x * zoomFactor();
148 KdenliveDoc *TrackView::document()
153 int TrackView::slotAddAudioTrack(int ix, QDomElement xml)
155 m_trackview->addTrack();
156 DocumentTrack *track = new DocumentAudioTrack(xml, this, m_trackview);
157 HeaderTrack *header = new HeaderTrack();
158 //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
159 m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
160 documentTracks.insert(ix, track);
161 return track->duration();
165 int TrackView::slotAddVideoTrack(int ix, QDomElement xml)
167 m_trackview->addTrack();
168 DocumentTrack *track = new DocumentVideoTrack(xml, this, m_trackview);
169 HeaderTrack *header = new HeaderTrack();
170 int trackTop = 50 * ix;
171 int trackBottom = trackTop + 50;
174 for(QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling())
176 QDomElement elem = n.toElement();
177 if (elem.tagName() == "blank") {
178 position += elem.attribute("length", 0).toInt();
180 else if (elem.tagName() == "entry") {
181 int in = elem.attribute("in", 0).toInt();
182 int out = elem.attribute("out", 0).toInt() - in;
183 QString clipName = m_doc->producerName(elem.attribute("producer").toInt());
184 int clipMaxDuration = m_doc->getProducerDuration(elem.attribute("producer").toInt());
185 //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
186 ClipItem *item = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("producer").toInt(), clipMaxDuration, QRectF(position, trackTop + 1, out, 49));
187 m_scene->addItem(item);
190 //m_clipList.append(clip);
193 //m_trackDuration = position;
195 //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
196 m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
197 documentTracks.insert(ix, track);
202 DocumentTrack *TrackView::panelAt(int y)
207 QGraphicsScene *TrackView::projectScene()
212 CustomTrackView *TrackView::projectView()
217 void TrackView::setEditMode(const QString & editMode)
219 m_editMode = editMode;
222 const QString & TrackView::editMode() const
227 /** This event occurs when the mouse has been moved. */
228 void TrackView::mouseMoveEvent(QMouseEvent * event) {
229 kDebug()<<"-------- TRACKVIEW MOUSE MOVE EVENT -----";
230 if (m_panelUnderMouse) {
231 if (event->buttons() & Qt::LeftButton) {
235 m_function->mouseMoved(m_panelUnderMouse, event);
237 m_panelUnderMouse = 0;
242 m_function->mouseReleased(m_panelUnderMouse, event);
245 m_panelUnderMouse = 0;
248 DocumentTrack *panel = panelAt(event->y());
250 QCursor result(Qt::ArrowCursor);
252 TrackPanelFunction *function =
253 getApplicableFunction(panel, editMode(),
256 result = function->getMouseCursor(panel, event);
260 setCursor(QCursor(Qt::ArrowCursor));
265 TrackPanelFunction *TrackView::getApplicableFunction(DocumentTrack *
266 panel, const QString & editMode, QMouseEvent * event) {
267 TrackPanelFunction *function = 0;
269 QStringList list = panel->applicableFunctions(editMode);
270 QStringList::iterator itt = list.begin();
272 while (itt != list.end()) {
273 TrackPanelFunction *testFunction = m_factory.function(*itt);
275 if (testFunction->mouseApplies(panel, event)) {
276 function = testFunction;
288 #include "trackview.moc"