]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
Autoscroll while playing
[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 "headertrack.h"
29 #include "trackview.h"
30 #include "clipitem.h"
31
32 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
33         : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0), m_projectDuration(0) {
34     setMouseTracking(true);
35     view = new Ui::TimeLine_UI();
36     view->setupUi(this);
37     m_ruler = new CustomRuler(doc->timecode());
38     QVBoxLayout *layout = new QVBoxLayout;
39     view->ruler_frame->setLayout(layout);
40     layout->addWidget(m_ruler);
41
42     m_scene = new QGraphicsScene();
43     m_trackview = new CustomTrackView(doc, m_scene, this);
44     m_trackview->scale(1, 1);
45     m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
46     //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
47
48     m_headersLayout = new QVBoxLayout;
49     m_headersLayout->setContentsMargins(0, 0, 0, 0);
50     view->headers_frame->setLayout(m_headersLayout);
51
52     QVBoxLayout *tracksLayout = new QVBoxLayout;
53     tracksLayout->setContentsMargins(0, 0, 0, 0);
54     view->tracks_frame->setLayout(tracksLayout);
55     tracksLayout->addWidget(m_trackview);
56
57     parseDocument(doc->toXml());
58     /*
59       TrackPanelClipMoveFunction *m_moveFunction = new TrackPanelClipMoveFunction(this);
60       registerFunction("move", m_moveFunction);
61       setEditMode("move");*/
62
63     connect(view->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeZoom(int)));
64     connect(m_ruler, SIGNAL(cursorMoved(int)), this, SLOT(setCursorPos(int)));
65     connect(m_trackview, SIGNAL(cursorMoved(int)), this, SLOT(slotCursorMoved(int)));
66     connect(m_trackview, SIGNAL(zoomIn()), this, SLOT(slotZoomIn()));
67     connect(m_trackview, SIGNAL(zoomOut()), this, SLOT(slotZoomOut()));
68     connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
69     connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
70     connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
71     view->horizontalSlider->setValue(4);
72     m_currentZoom = view->horizontalSlider->value();
73 }
74
75
76 int TrackView::duration() {
77     return m_projectDuration;
78 }
79
80 int TrackView::tracksNumber() {
81     return m_projectTracks;
82 }
83
84 void TrackView::slotClipItemSelected(ClipItem*c) {
85     emit clipItemSelected(c);
86 }
87
88 void TrackView::parseDocument(QDomDocument doc) {
89     int cursorPos = 0;
90     kDebug() << "//// DOCUMENT: " << doc.toString();
91     QDomNode props = doc.elementsByTagName("properties").item(0);
92     if (!props.isNull()) {
93         cursorPos = props.toElement().attribute("timeline_position").toInt();
94     }
95     QDomNodeList tracks = doc.elementsByTagName("playlist");
96     m_projectDuration = 300;
97     m_projectTracks = tracks.count();
98     int duration = 0;
99     kDebug() << "//////////// TIMELINE FOUND: " << m_projectTracks << " tracks";
100     for (int i = 0; i < m_projectTracks; i++) {
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         } else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
105             duration = slotAddVideoTrack(i, tracks.item(i).toElement());
106         kDebug() << " PRO DUR: " << m_projectDuration << ", TRACK DUR: " << duration;
107         if (duration > m_projectDuration) m_projectDuration = duration;
108     }
109     m_trackview->setDuration(m_projectDuration);
110     slotCursorMoved(cursorPos, true);
111     //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
112 }
113
114 void TrackView::slotDeleteClip(int clipId) {
115     m_trackview->deleteClip(clipId);
116 }
117
118 void TrackView::setCursorPos(int pos) {
119     emit cursorMoved();
120     m_trackview->setCursorPos(pos * m_scale);
121 }
122
123 void TrackView::moveCursorPos(int pos) {
124     m_trackview->setCursorPos(pos * m_scale, false);
125     m_ruler->slotNewValue(pos * FRAME_SIZE, false);
126 }
127
128 void TrackView::slotCursorMoved(int pos, bool emitSignal) {
129     m_ruler->slotNewValue(pos * FRAME_SIZE / m_scale, emitSignal); //(int) m_trackview->mapToScene(QPoint(pos, 0)).x());
130     //m_trackview->setCursorPos(pos);
131     //m_trackview->invalidateScene(QRectF(), QGraphicsScene::ForegroundLayer);
132 }
133
134 void TrackView::slotChangeZoom(int factor) {
135     double pos = m_trackview->cursorPos() / m_scale;
136     m_ruler->setPixelPerMark(factor);
137     m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
138     m_currentZoom = factor;
139     m_trackview->setScale(m_scale);
140     m_trackview->setCursorPos(pos * m_scale, false);
141     m_ruler->slotNewValue(pos * FRAME_SIZE, false);
142     m_trackview->centerOn(QPointF(m_trackview->cursorPos(), 50));
143 }
144
145 const double TrackView::zoomFactor() const {
146     return m_scale;
147 }
148
149 void TrackView::slotZoomIn() {
150     view->horizontalSlider->setValue(view->horizontalSlider->value() - 1);
151 }
152
153 void TrackView::slotZoomOut() {
154     view->horizontalSlider->setValue(view->horizontalSlider->value() + 1);
155 }
156
157 const int TrackView::mapLocalToValue(int x) const {
158     return (int) x * zoomFactor();
159 }
160
161 KdenliveDoc *TrackView::document() {
162     return m_doc;
163 }
164
165 void TrackView::refresh() {
166     m_trackview->viewport()->update();
167 }
168
169 int TrackView::slotAddAudioTrack(int ix, QDomElement xml) {
170     kDebug() << "*************  ADD AUDIO TRACK " << ix;
171     m_trackview->addTrack();
172     HeaderTrack *header = new HeaderTrack();
173     //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
174     m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
175     //documentTracks.insert(ix, track);
176     return 0;
177     //track->show();
178 }
179
180 int TrackView::slotAddVideoTrack(int ix, QDomElement xml) {
181     m_trackview->addTrack();
182     HeaderTrack *header = new HeaderTrack();
183     int trackTop = 50 * ix;
184     int trackBottom = trackTop + 50;
185     // parse track
186     int position = 0;
187     for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
188         QDomElement elem = n.toElement();
189         if (elem.tagName() == "blank") {
190             position += elem.attribute("length", 0).toInt();
191         } else if (elem.tagName() == "entry") {
192             int in = elem.attribute("in", 0).toInt();
193             int id = elem.attribute("producer", 0).toInt();
194             DocClipBase *clip = m_doc->clipManager()->getClipById(id);
195             int out = elem.attribute("out", 0).toInt() - in;
196             //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
197             ClipItem *item = new ClipItem(clip, ix, position, QRectF(position * m_scale, trackTop + 1, out * m_scale, 49), out);
198             m_scene->addItem(item);
199             position += out;
200
201             //m_clipList.append(clip);
202         }
203     }
204     //m_trackDuration = position;
205
206     //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
207     m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
208     //documentTracks.insert(ix, track);
209     kDebug() << "*************  ADD VIDEO TRACK " << ix << ", DURATION: " << position;
210     return position;
211     //track->show();
212 }
213
214 QGraphicsScene *TrackView::projectScene() {
215     return m_scene;
216 }
217
218 CustomTrackView *TrackView::projectView() {
219     return m_trackview;
220 }
221
222 void TrackView::setEditMode(const QString & editMode) {
223     m_editMode = editMode;
224 }
225
226 const QString & TrackView::editMode() const {
227     return m_editMode;
228 }
229
230 #include "trackview.moc"