]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
* Revert to an easier MLT playlist manipulation now that MLT allows easy transition...
[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 #include "kdenlivesettings.h"
32 #include "clipmanager.h"
33 #include "customruler.h"
34 #include "kdenlivedoc.h"
35 #include "customtrackview.h"
36
37 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
38         : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0), m_currentZoom(4) {
39
40     view = new Ui::TimeLine_UI();
41     view->setupUi(this);
42
43     m_scene = new QGraphicsScene();
44     m_trackview = new CustomTrackView(doc, m_scene, parent);
45     m_trackview->scale(1, 1);
46     m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
47     //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
48
49     m_ruler = new CustomRuler(doc->timecode(), m_trackview);
50     QHBoxLayout *layout = new QHBoxLayout;
51     view->ruler_frame->setLayout(layout);
52     int left_margin;
53     int right_margin;
54     layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
55     layout->setContentsMargins(left_margin, 0, right_margin, 0);
56     layout->addWidget(m_ruler);
57
58     QHBoxLayout *tracksLayout = new QHBoxLayout;
59     tracksLayout->setContentsMargins(0, 0, 0, 0);
60     view->tracks_frame->setLayout(tracksLayout);
61
62     view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63     view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64
65     m_headersLayout = new QVBoxLayout;
66     m_headersLayout->setContentsMargins(0, 0, 0, 0);
67     m_headersLayout->setSpacing(0);
68     view->headers_container->setLayout(m_headersLayout);
69
70     connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
71
72     tracksLayout->addWidget(m_trackview);
73
74     connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
75     connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
76
77     parseDocument(doc->toXml());
78
79     connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
80     connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
81     connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
82     connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
83     connect(m_trackview, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotTransitionItemSelected(Transition*)));
84     slotChangeZoom(m_currentZoom);
85 }
86
87 int TrackView::currentZoom() const {
88     return m_currentZoom;
89 }
90
91 int TrackView::duration() const {
92     return m_trackview->duration();
93 }
94
95 int TrackView::tracksNumber() const {
96     return m_projectTracks;
97 }
98
99 int TrackView::inPoint() const {
100     return m_ruler->inPoint();
101 }
102
103 int TrackView::outPoint() const {
104     return m_ruler->outPoint();
105 }
106
107 void TrackView::slotClipItemSelected(ClipItem*c) {
108     emit clipItemSelected(c);
109 }
110
111 void TrackView::slotTransitionItemSelected(Transition *t) {
112     emit transitionItemSelected(t);
113 }
114
115 void TrackView::setDuration(int dur) {
116     m_trackview->setDuration(dur);
117     m_ruler->setDuration(dur);
118 }
119
120 void TrackView::parseDocument(QDomDocument doc) {
121     int cursorPos = 0;
122     //kDebug() << "//// DOCUMENT: " << doc.toString();
123     QDomNode props = doc.elementsByTagName("properties").item(0);
124     if (!props.isNull()) {
125         cursorPos = props.toElement().attribute("timeline_position").toInt();
126     }
127     QDomNodeList tracks = doc.elementsByTagName("track");
128     QDomNodeList playlists = doc.elementsByTagName("playlist");
129     int duration = 300;
130     m_projectTracks = tracks.count();
131     int trackduration = 0;
132     QDomElement e;
133     QDomElement p;
134     bool videotrack;
135     kDebug() << "//////////// TIMELINE FOUND: " << m_projectTracks << " tracks";
136     int pos = m_projectTracks - 1;
137     for (int i = 0; i < m_projectTracks; i++) {
138         e = tracks.item(i).toElement();
139         QString playlist_name = e.attribute("producer");
140         if (playlist_name != "black_track" && playlist_name != "playlistmain") {
141             // find playlist related to this track
142             p = QDomElement();
143             for (int j = 0; j < m_projectTracks; j++) {
144                 p = playlists.item(j).toElement();
145                 if (p.attribute("id") == playlist_name) break;
146             }
147             videotrack = (e.attribute("hide") != "video");
148             trackduration = slotAddProjectTrack(pos, p, videotrack);
149             pos--;
150             kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
151             if (trackduration > duration) duration = trackduration;
152         } else pos--;
153     }
154     m_trackview->setDuration(duration);
155     slotRebuildTrackHeaders();
156     //m_trackview->setCursorPos(cursorPos);
157     //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
158 }
159
160 void TrackView::slotDeleteClip(int clipId) {
161     m_trackview->deleteClip(clipId);
162 }
163
164 void TrackView::setCursorPos(int pos) {
165     m_trackview->setCursorPos(pos);
166 }
167
168 void TrackView::moveCursorPos(int pos) {
169     m_trackview->setCursorPos(pos, false);
170 }
171
172 void TrackView::slotChangeZoom(int factor) {
173
174     m_ruler->setPixelPerMark(factor);
175     m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
176     m_currentZoom = factor;
177     m_trackview->setScale(m_scale);
178 }
179
180 int TrackView::fitZoom() const {
181     int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
182     int i;
183     for (i = 0; i < 13; i++)
184         if (m_ruler->comboScale[i] > zoom) break;
185     return i;
186 }
187
188 const double TrackView::zoomFactor() const {
189     return m_scale;
190 }
191
192 const int TrackView::mapLocalToValue(int x) const {
193     return (int)(x * zoomFactor());
194 }
195
196 KdenliveDoc *TrackView::document() {
197     return m_doc;
198 }
199
200 void TrackView::refresh() {
201     m_trackview->viewport()->update();
202 }
203
204 void TrackView::slotRebuildTrackHeaders() {
205     QList <TrackInfo> list = m_trackview->tracksList();
206     QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
207     for (int i = 0; i < widgets.count(); i++)
208         delete widgets.at(i);
209     int max = list.count();
210     for (int i = 0; i < max; i++) {
211         HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
212         connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
213         connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
214         m_headersLayout->addWidget(header);
215     }
216     view->headers_container->adjustSize();
217 }
218
219 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
220     TrackInfo info;
221     if (videotrack) {
222         info.type = VIDEOTRACK;
223         info.isMute = false;
224         info.isBlind = false;
225     } else {
226         info.type = AUDIOTRACK;
227         info.isMute = false;
228         info.isBlind = false;
229     }
230
231     m_trackview->addTrack(info);
232
233     int trackTop = KdenliveSettings::trackheight() * ix;
234     // parse track
235     int position = 0;
236
237     for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
238         QDomElement elem = n.toElement();
239         if (elem.tagName() == "blank") {
240             position += elem.attribute("length").toInt();
241         } else if (elem.tagName() == "entry") {
242             int in = elem.attribute("in").toInt();
243             int id = elem.attribute("producer").toInt();
244             DocClipBase *clip = m_doc->clipManager()->getClipById(id);
245             if (clip != NULL) {
246                 int out = elem.attribute("out").toInt();
247
248                 ItemInfo clipinfo;
249                 clipinfo.startPos = GenTime(position, m_doc->fps());
250                 clipinfo.endPos = GenTime(out, m_doc->fps());
251                 clipinfo.track = ix;
252                 kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
253                 ClipItem *item = new ClipItem(clip, clipinfo, m_scale, m_doc->fps());
254                 m_scene->addItem(item);
255                 position += out;
256             } else kWarning() << "CANNOT INSERT CLIP " << id;
257             //m_clipList.append(clip);
258         }
259     }
260     //m_trackDuration = position;
261
262
263     //documentTracks.insert(ix, track);
264     kDebug() << "*************  ADD DOC TRACK " << ix << ", DURATION: " << position;
265     return position;
266     //track->show();
267 }
268
269 QGraphicsScene *TrackView::projectScene() {
270     return m_scene;
271 }
272
273 CustomTrackView *TrackView::projectView() {
274     return m_trackview;
275 }
276
277 void TrackView::setEditMode(const QString & editMode) {
278     m_editMode = editMode;
279 }
280
281 const QString & TrackView::editMode() const {
282     return m_editMode;
283 }
284
285 #include "trackview.moc"