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) {
37 setMouseTracking(true);
38 view = new Ui::TimeLine_UI();
40 m_ruler = new CustomRuler(doc->timecode());
41 QVBoxLayout *layout = new QVBoxLayout;
42 view->ruler_frame->setLayout(layout);
43 layout->addWidget(m_ruler);
45 m_scene = new QGraphicsScene();
46 m_trackview = new CustomTrackView(doc, m_scene, this);
47 m_trackview->scale(1, 1);
48 m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
49 //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
51 m_headersLayout = new QVBoxLayout;
52 m_headersLayout->setContentsMargins(0, 0, 0, 0);
53 view->headers_frame->setLayout(m_headersLayout);
55 QVBoxLayout *tracksLayout = new QVBoxLayout;
56 tracksLayout->setContentsMargins(0, 0, 0, 0);
57 view->tracks_frame->setLayout(tracksLayout);
58 tracksLayout->addWidget(m_trackview);
60 parseDocument(doc->toXml());
62 TrackPanelClipMoveFunction *m_moveFunction = new TrackPanelClipMoveFunction(this);
63 registerFunction("move", m_moveFunction);
64 setEditMode("move");*/
66 connect(view->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeZoom(int)));
67 connect(m_ruler, SIGNAL(cursorMoved(int)), this, SLOT(setCursorPos(int)));
68 connect(m_trackview, SIGNAL(cursorMoved(int)), this, SLOT(slotCursorMoved(int)));
69 connect(m_trackview, SIGNAL(zoomIn()), this, SLOT(slotZoomIn()));
70 connect(m_trackview, SIGNAL(zoomOut()), this, SLOT(slotZoomOut()));
71 connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
72 connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
73 connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
74 view->horizontalSlider->setValue(4);
75 m_currentZoom = view->horizontalSlider->value();
76 m_trackview->initView();
79 void TrackView::registerFunction(const QString & name, TrackPanelFunction * function) {
80 m_factory.registerFunction(name, function);
83 int TrackView::duration() {
84 return m_projectDuration;
87 int TrackView::tracksNumber() {
88 return m_projectTracks;
91 void TrackView::slotClipItemSelected(ClipItem*c) {
92 emit clipItemSelected(c);
95 void TrackView::parseDocument(QDomDocument doc) {
97 kDebug() << "//// DOCUMENT: " << doc.toString();
98 QDomNode props = doc.elementsByTagName("properties").item(0);
99 if (!props.isNull()) {
100 cursorPos = props.toElement().attribute("timeline_position").toInt();
102 QDomNodeList tracks = doc.elementsByTagName("playlist");
103 m_projectDuration = 300;
104 m_projectTracks = tracks.count();
106 kDebug() << "//////////// TIMELINE FOUND: " << m_projectTracks << " tracks";
107 for (int i = 0; i < m_projectTracks; i++) {
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 } else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
112 duration = slotAddVideoTrack(i, tracks.item(i).toElement());
113 kDebug() << " PRO DUR: " << m_projectDuration << ", TRACK DUR: " << duration;
114 if (duration > m_projectDuration) m_projectDuration = duration;
116 m_trackview->setDuration(m_projectDuration);
117 slotCursorMoved(cursorPos, true);
118 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
121 void TrackView::slotDeleteClip(int clipId) {
122 m_trackview->deleteClip(clipId);
125 void TrackView::setCursorPos(int pos) {
127 m_trackview->setCursorPos(pos * m_scale);
130 void TrackView::moveCursorPos(int pos) {
131 m_trackview->setCursorPos(pos * m_scale, false);
132 m_ruler->slotNewValue(pos * FRAME_SIZE, false);
135 void TrackView::slotCursorMoved(int pos, bool emitSignal) {
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);
141 void TrackView::slotChangeZoom(int factor) {
142 double pos = m_trackview->cursorPos() / m_scale;
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->setCursorPos(pos * m_scale, false);
148 m_ruler->slotNewValue(pos * FRAME_SIZE, false);
149 m_trackview->centerOn(QPointF(m_trackview->cursorPos(), 50));
152 const double TrackView::zoomFactor() const {
156 void TrackView::slotZoomIn() {
157 view->horizontalSlider->setValue(view->horizontalSlider->value() - 1);
160 void TrackView::slotZoomOut() {
161 view->horizontalSlider->setValue(view->horizontalSlider->value() + 1);
164 const int TrackView::mapLocalToValue(int x) const {
165 return (int) x * zoomFactor();
168 KdenliveDoc *TrackView::document() {
172 int TrackView::slotAddAudioTrack(int ix, QDomElement xml) {
173 kDebug() << "************* ADD AUDIO TRACK " << ix;
174 m_trackview->addTrack();
175 //DocumentTrack *track = new DocumentAudioTrack(xml, this, m_trackview);
176 HeaderTrack *header = new HeaderTrack();
177 //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
178 m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
179 //documentTracks.insert(ix, track);
184 int TrackView::slotAddVideoTrack(int ix, QDomElement xml) {
185 m_trackview->addTrack();
186 //DocumentTrack *track = new DocumentVideoTrack(xml, this, m_trackview);
187 HeaderTrack *header = new HeaderTrack();
188 int trackTop = 50 * ix;
189 int trackBottom = trackTop + 50;
192 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
193 QDomElement elem = n.toElement();
194 if (elem.tagName() == "blank") {
195 position += elem.attribute("length", 0).toInt();
196 } else if (elem.tagName() == "entry") {
197 int in = elem.attribute("in", 0).toInt();
198 int id = elem.attribute("producer", 0).toInt();
199 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
200 int out = elem.attribute("out", 0).toInt() - in;
201 //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
202 ClipItem *item = new ClipItem(clip, ix, position, QRectF(position * m_scale, trackTop + 1, out * m_scale, 49), out);
203 m_scene->addItem(item);
206 //m_clipList.append(clip);
209 //m_trackDuration = position;
211 //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
212 m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
213 //documentTracks.insert(ix, track);
214 kDebug() << "************* ADD VIDEO TRACK " << ix << ", DURATION: " << position;
219 DocumentTrack *TrackView::panelAt(int y) {
223 QGraphicsScene *TrackView::projectScene() {
227 CustomTrackView *TrackView::projectView() {
231 void TrackView::setEditMode(const QString & editMode) {
232 m_editMode = editMode;
235 const QString & TrackView::editMode() const {
239 /** This event occurs when the mouse has been moved. */
240 void TrackView::mouseMoveEvent(QMouseEvent * event) {
241 if (m_panelUnderMouse) {
242 if (event->buttons() & Qt::LeftButton) {
246 m_function->mouseMoved(m_panelUnderMouse, event);
248 m_panelUnderMouse = 0;
253 m_function->mouseReleased(m_panelUnderMouse, event);
256 m_panelUnderMouse = 0;
259 DocumentTrack *panel = panelAt(event->y());
261 QCursor result(Qt::ArrowCursor);
263 TrackPanelFunction *function =
264 getApplicableFunction(panel, editMode(),
267 result = function->getMouseCursor(panel, event);
271 setCursor(QCursor(Qt::ArrowCursor));
276 TrackPanelFunction *TrackView::getApplicableFunction(DocumentTrack *
277 panel, const QString & editMode, QMouseEvent * event) {
278 TrackPanelFunction *function = 0;
280 QStringList list = panel->applicableFunctions(editMode);
281 QStringList::iterator itt = list.begin();
283 while (itt != list.end()) {
284 TrackPanelFunction *testFunction = m_factory.function(*itt);
286 if (testFunction->mouseApplies(panel, event)) {
287 function = testFunction;
299 #include "trackview.moc"