]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
Start porting timeline to QGraphicsView
[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 "documentvideotrack.h"
29 #include "documentaudiotrack.h"
30 #include "headertrack.h"
31 #include "trackview.h"
32 #include "clipitem.h"
33 #include "trackpanelclipmovefunction.h"
34
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 {
38   setMouseTracking(true);
39   view = new Ui::TimeLine_UI();
40   view->setupUi(this);
41   m_ruler = new CustomRuler(doc->timecode());
42   QVBoxLayout *layout = new QVBoxLayout;
43   layout->addWidget(m_ruler);
44   view->ruler_frame->setLayout(layout);
45
46   m_scene = new QGraphicsScene();
47   m_trackview = new CustomTrackView(m_scene, this);
48   m_trackview->scale(FRAME_SIZE, 1);
49   m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
50   //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
51
52   m_headersLayout = new QVBoxLayout;
53   m_headersLayout->setContentsMargins (0, 0, 0, 0);
54   view->headers_frame->setLayout(m_headersLayout);
55
56   QVBoxLayout *tracksLayout = new QVBoxLayout;
57   tracksLayout->setContentsMargins (0, 0, 0, 0);
58   view->tracks_frame->setLayout(tracksLayout);
59   tracksLayout->addWidget(m_trackview);
60
61   parseDocument(doc->toXml());
62
63   TrackPanelClipMoveFunction *m_moveFunction = new TrackPanelClipMoveFunction(this);
64   registerFunction("move", m_moveFunction);
65   setEditMode("move");
66
67   view->horizontalSlider->setValue(0);
68   m_currentZoom = view->horizontalSlider->value();
69   connect(view->horizontalSlider, SIGNAL(valueChanged ( int )), this, SLOT(slotChangeZoom( int )));
70   connect(m_ruler, SIGNAL(cursorMoved ( int )), this, SLOT(slotCursorMoved( int )));
71   connect(m_trackview, SIGNAL(cursorMoved ( int )), this, SLOT(slotCursorMoved( int )));
72
73   m_cursorLine = m_scene->addLine(0, 0, 0, 200); //m_trackview->height());
74 }
75
76 void TrackView::registerFunction(const QString & name, TrackPanelFunction * function) 
77 {
78   m_factory.registerFunction(name, function);
79 }
80
81 int TrackView::duration()
82 {
83   return m_projectDuration;
84 }
85
86 int TrackView::tracksNumber()
87 {
88   return m_projectTracks;
89 }
90
91 void TrackView::parseDocument(QDomDocument doc)
92 {
93   QDomNodeList tracks = doc.elementsByTagName("playlist");
94   m_projectDuration = 300;
95   m_projectTracks = tracks.count();
96   int duration;
97   for (int i = 0; i < tracks.count(); i++)
98   {
99     if (tracks.item(i).toElement().attribute("hide", QString::null) == "video") {
100       // this is an audio track
101       duration = slotAddAudioTrack(i, tracks.item(i).toElement());
102     }
103     else if (!tracks.item(i).toElement().attribute("id", QString::null).isEmpty())
104       duration = slotAddVideoTrack(i, tracks.item(i).toElement());
105     if (duration > m_projectDuration) m_projectDuration = duration;
106   }
107   //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
108 }
109
110 void TrackView::slotCursorMoved(int pos)
111 {
112   kDebug()<<"///// CURSOR: "<<pos;
113   //m_cursorLine->setPos(pos, 0);
114   m_trackview->setCursorPos(pos);
115   m_trackview->invalidateScene(QRectF(), QGraphicsScene::ForegroundLayer);
116 }
117
118 void TrackView::slotChangeZoom(int factor)
119 {
120   m_ruler->setPixelPerMark(factor);
121   //m_scale = m_ruler->pixelPerMark();
122   m_scale = (double) m_ruler->comboScale[m_currentZoom] / m_ruler->comboScale[factor];
123   //else m_scale = (double) m_ruler->comboScale[m_currentZoom] / m_ruler->comboScale[factor];
124   m_currentZoom = factor;
125   kDebug()<<"///// ZOOMING: "<<m_scale;
126   m_trackview->scale(m_scale, 1);
127   /*
128   for (int i = 0; i < documentTracks.count(); i++) {
129     kDebug()<<"------REPAINTING OBJECT";
130     documentTracks.at(i)->update();
131     //documentTracks.at(i)->setFixedWidth(300 * zoomFactor());
132   }
133   m_scrollBox->setFixedWidth(( m_projectDuration + 300) * zoomFactor());*/
134   /*m_scrollArea->horizontalScrollBar()->setMaximum(300 * zoomFactor());
135   m_scrollArea->horizontalScrollBar()->setPageStep(FRAME_SIZE * zoomFactor());*/
136 }
137
138 const double TrackView::zoomFactor() const
139 {
140   return m_scale * FRAME_SIZE;
141 }
142
143 const int TrackView::mapLocalToValue(int x) const
144 {
145   return (int) x * zoomFactor();
146 }
147
148 KdenliveDoc *TrackView::document()
149 {
150   return m_doc;
151 }
152
153 int TrackView::slotAddAudioTrack(int ix, QDomElement xml)
154 {
155   m_trackview->addTrack();
156   DocumentTrack *track = new DocumentAudioTrack(xml, this, m_trackview);
157   HeaderTrack *header = new HeaderTrack();
158   //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
159   m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
160   documentTracks.insert(ix, track);
161   return track->duration();
162   //track->show();
163 }
164
165 int TrackView::slotAddVideoTrack(int ix, QDomElement xml)
166 {
167   m_trackview->addTrack();
168   DocumentTrack *track = new DocumentVideoTrack(xml, this, m_trackview);
169   HeaderTrack *header = new HeaderTrack();
170   int trackTop = 50 * ix;
171   int trackBottom = trackTop + 50;
172   // parse track
173   int position = 0;
174   for(QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling())
175   {
176     QDomElement elem = n.toElement();
177     if (elem.tagName() == "blank") {
178       position += elem.attribute("length", 0).toInt();
179     }
180     else if (elem.tagName() == "entry") {
181     int in = elem.attribute("in", 0).toInt();
182     int out = elem.attribute("out", 0).toInt() - in;
183     QString clipName = m_doc->producerName(elem.attribute("producer").toInt());
184     //kDebug()<<"++++++++++++++\n\n / / /ADDING CLIP: "<<clip.cropTime<<", out: "<<clip.duration<<", Producer: "<<clip.producer<<"\n\n++++++++++++++++++++";
185     ClipItem *item = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("producer").toInt(), QRectF(position, trackTop + 1, out, 49));
186     m_scene->addItem(item);
187     position += out;
188
189     //m_clipList.append(clip);
190    }
191   }
192   //m_trackDuration = position;
193
194   //m_tracksAreaLayout->addWidget(track); //, ix, Qt::AlignTop);
195   m_headersLayout->addWidget(header); //, ix, Qt::AlignTop);
196   documentTracks.insert(ix, track);
197   return position;
198   //track->show();
199 }
200
201 DocumentTrack *TrackView::panelAt(int y)
202 {
203   return NULL;
204 }
205
206 QGraphicsScene *TrackView::projectScene()
207 {
208   return m_scene;
209 }
210
211 CustomTrackView *TrackView::projectView()
212 {
213   return m_trackview;
214 }
215
216 void TrackView::setEditMode(const QString & editMode)
217 {
218   m_editMode = editMode;
219 }
220
221 const QString & TrackView::editMode() const
222 {
223   return m_editMode;
224 }
225
226 /** This event occurs when the mouse has been moved. */
227     void TrackView::mouseMoveEvent(QMouseEvent * event) {
228     kDebug()<<"--------  TRACKVIEW MOUSE MOVE EVENT -----";
229         if (m_panelUnderMouse) {
230             if (event->buttons() & Qt::LeftButton) {
231                 bool result = false;
232                 if (m_function)
233                     result =
234                         m_function->mouseMoved(m_panelUnderMouse, event);
235                 if (!result) {
236                     m_panelUnderMouse = 0;
237                     m_function = 0;
238                 }
239             } else {
240                 if (m_function) {
241                     m_function->mouseReleased(m_panelUnderMouse, event);
242                     m_function = 0;
243                 }
244                 m_panelUnderMouse = 0;
245             }
246         } else {
247             DocumentTrack *panel = panelAt(event->y());
248             if (panel) {
249                 QCursor result(Qt::ArrowCursor);
250
251                 TrackPanelFunction *function =
252                     getApplicableFunction(panel, editMode(),
253                     event);
254                 if (function)
255                     result = function->getMouseCursor(panel, event);
256
257                 setCursor(result);
258             } else {
259                 setCursor(QCursor(Qt::ArrowCursor));
260             }
261         }
262     }
263
264     TrackPanelFunction *TrackView::getApplicableFunction(DocumentTrack *
265         panel, const QString & editMode, QMouseEvent * event) {
266         TrackPanelFunction *function = 0;
267
268         QStringList list = panel->applicableFunctions(editMode);
269         QStringList::iterator itt = list.begin();
270
271         while (itt != list.end()) {
272             TrackPanelFunction *testFunction = m_factory.function(*itt);
273             if (testFunction) {
274                 if (testFunction->mouseApplies(panel, event)) {
275                     function = testFunction;
276                     break;
277                 }
278             }
279
280             ++itt;
281         }
282
283         return function;
284     }
285
286
287 #include "trackview.moc"