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