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