]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
Reindent all source files
[kdenlive] / src / customtrackview.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 #include <QMouseEvent>
21 #include <QStylePainter>
22 #include <QGraphicsItem>
23 #include <QDomDocument>
24 #include <QScrollBar>
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KUrl>
29
30 #include "customtrackview.h"
31 #include "clipitem.h"
32 #include "definitions.h"
33 #include "moveclipcommand.h"
34 #include "resizeclipcommand.h"
35 #include "addtimelineclipcommand.h"
36 #include "addeffectcommand.h"
37 #include "editeffectcommand.h"
38
39 CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent)
40         : QGraphicsView(projectscene, parent), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(NONE), m_startPos(QPointF()), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_scale(1.0), m_clickPoint(0), m_document(doc) {
41     if (doc) m_commandStack = doc->commandStack();
42     else m_commandStack == NULL;
43     setMouseTracking(true);
44     setAcceptDrops(true);
45     m_animationTimer = new QTimeLine(800);
46     m_animationTimer->setFrameRange(0, 5);
47     m_animationTimer->setUpdateInterval(100);
48     m_animationTimer->setLoopCount(0);
49     m_tipColor = QColor(0, 192, 0, 200);
50     QColor border = QColor(255, 255, 255, 100);
51     m_tipPen.setColor(border);
52     m_tipPen.setWidth(3);
53
54     setContentsMargins(0, 0, 0, 0);
55     if (projectscene) {
56         m_cursorLine = projectscene->addLine(0, 0, 0, 50);
57         m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
58         m_cursorLine->setZValue(1000);
59     }
60 }
61
62 void CustomTrackView::initView() {
63
64 }
65
66 // virtual
67 void CustomTrackView::resizeEvent(QResizeEvent * event) {
68     QGraphicsView::resizeEvent(event);
69 }
70
71 // virtual
72 void CustomTrackView::wheelEvent(QWheelEvent * e) {
73     if (e->modifiers() == Qt::ControlModifier) {
74         if (e->delta() > 0) emit zoomIn();
75         else emit zoomOut();
76     } else {
77         if (e->delta() > 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
78         else  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
79     }
80 }
81
82
83 // virtual
84 void CustomTrackView::mouseMoveEvent(QMouseEvent * event) {
85     int pos = event->x();
86     emit mousePosition(mapToScene(event->pos()).x() / m_scale);
87     /*if (event->modifiers() == Qt::ControlModifier)
88       setDragMode(QGraphicsView::ScrollHandDrag);
89     else if (event->modifiers() == Qt::ShiftModifier)
90       setDragMode(QGraphicsView::RubberBandDrag);
91     else*/
92     {
93
94         if (m_dragItem) { //event->button() == Qt::LeftButton) {
95             // a button was pressed, delete visual tips
96             if (m_operationMode == MOVE) {
97                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
98                 double moveX = snappedPos; //mapToScene(event->pos()).x();
99                 //kDebug()<<"///////  MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
100                 int moveTrack = (int)  mapToScene(event->pos()).y() / 50;
101                 int currentTrack = m_dragItem->track();
102
103                 if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
104                 else if (moveTrack < 0) moveTrack = 0;
105
106                 int offset = moveTrack - currentTrack;
107                 if (offset != 0) offset = 50 * offset;
108                 m_dragItem->moveTo(moveX / m_scale, m_scale, offset, moveTrack);
109             } else if (m_operationMode == RESIZESTART) {
110                 int pos = mapToScene(event->pos()).x();
111                 m_dragItem->resizeStart(pos / m_scale, m_scale);
112             } else if (m_operationMode == RESIZEEND) {
113                 int pos = mapToScene(event->pos()).x();
114                 m_dragItem->resizeEnd(pos / m_scale, m_scale);
115             } else if (m_operationMode == FADEIN) {
116                 int pos = mapToScene(event->pos()).x() / m_scale;
117                 m_dragItem->setFadeIn(pos - m_dragItem->startPos(), m_scale);
118             } else if (m_operationMode == FADEOUT) {
119                 int pos = mapToScene(event->pos()).x() / m_scale;
120                 m_dragItem->setFadeOut(m_dragItem->endPos() - pos, m_scale);
121             }
122
123             if (m_animation) delete m_animation;
124             m_animation = NULL;
125             if (m_visualTip) delete m_visualTip;
126             m_visualTip = NULL;
127             QGraphicsView::mouseMoveEvent(event);
128             return;
129         }
130
131         QList<QGraphicsItem *> itemList = items(event->pos());
132         int i = 0;
133         QGraphicsItem *item = NULL;
134         for (int i = 0; i < itemList.count(); i++) {
135             if (itemList.at(i)->type() == 70000) {
136                 item = itemList.at(i);
137                 break;
138             }
139         }
140         if (item && event->buttons() == Qt::NoButton) {
141             ClipItem *clip = (ClipItem*) item;
142             double size = 8;
143             OPERATIONTYPE opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
144             if (opMode == m_moveOpMode) {
145                 QGraphicsView::mouseMoveEvent(event);
146                 return;
147             } else {
148                 if (m_visualTip) {
149                     if (m_animation) delete m_animation;
150                     m_animation = NULL;
151                     m_animationTimer->stop();
152                     delete m_visualTip;
153                     m_visualTip = NULL;
154                 }
155             }
156             m_moveOpMode = opMode;
157             if (opMode == MOVE) {
158                 setCursor(Qt::OpenHandCursor);
159             } else if (opMode == RESIZESTART) {
160                 kDebug() << "********  RESIZE CLIP START; WIDTH: " << size;
161                 if (m_visualTip == NULL) {
162                     QPolygon polygon;
163                     polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
164                     polygon << QPoint(clip->rect().x() + size * 2, clip->rect().y() + clip->rect().height() / 2);
165                     polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
166                     polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
167
168                     m_visualTip = new QGraphicsPolygonItem(polygon);
169                     ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
170                     ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
171                     m_visualTip->setZValue(100);
172                     m_animation = new QGraphicsItemAnimation;
173                     m_animation->setItem(m_visualTip);
174                     m_animation->setTimeLine(m_animationTimer);
175                     m_visualTip->setPos(0, 0);
176                     double scale = 2.0;
177                     m_animation->setScaleAt(.5, scale, 1);
178                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
179                     scale = 1.0;
180                     m_animation->setScaleAt(1, scale, 1);
181                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
182                     scene()->addItem(m_visualTip);
183                     m_animationTimer->start();
184                 }
185                 setCursor(Qt::SizeHorCursor);
186             } else if (opMode == RESIZEEND) {
187                 if (m_visualTip == NULL) {
188                     QPolygon polygon;
189                     polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
190                     polygon << QPoint(clip->rect().x() + clip->rect().width() - size * 2, clip->rect().y() + clip->rect().height() / 2);
191                     polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
192                     polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
193
194                     m_visualTip = new QGraphicsPolygonItem(polygon);
195                     ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
196                     ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
197
198                     m_visualTip->setZValue(100);
199                     m_animation = new QGraphicsItemAnimation;
200                     m_animation->setItem(m_visualTip);
201                     m_animation->setTimeLine(m_animationTimer);
202                     m_visualTip->setPos(0, 0);
203                     double scale = 2.0;
204                     m_animation->setScaleAt(.5, scale, 1);
205                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
206                     scale = 1.0;
207                     m_animation->setScaleAt(1, scale, 1);
208                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
209                     scene()->addItem(m_visualTip);
210                     m_animationTimer->start();
211                 }
212                 setCursor(Qt::SizeHorCursor);
213             } else if (opMode == FADEIN) {
214                 if (m_visualTip == NULL) {
215                     m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->fadeIn() * m_scale - size, clip->rect().y() - 8, size * 2, 16);
216                     ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
217                     ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
218                     m_visualTip->setZValue(100);
219                     m_animation = new QGraphicsItemAnimation;
220                     m_animation->setItem(m_visualTip);
221                     m_animation->setTimeLine(m_animationTimer);
222                     m_visualTip->setPos(0, 0);
223                     double scale = 2.0;
224                     m_animation->setScaleAt(.5, scale, scale);
225                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale -  clip->fadeIn() * m_scale, clip->rect().y() - clip->rect().y() * scale));
226                     scale = 1.0;
227                     m_animation->setScaleAt(1, scale, scale);
228                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
229                     scene()->addItem(m_visualTip);
230                     m_animationTimer->start();
231                 }
232                 setCursor(Qt::PointingHandCursor);
233             } else if (opMode == FADEOUT) {
234                 if (m_visualTip == NULL) {
235                     m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - clip->fadeOut() * m_scale - size, clip->rect().y() - 8, size*2, 16);
236                     ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
237                     ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
238                     m_visualTip->setZValue(100);
239                     m_animation = new QGraphicsItemAnimation;
240                     m_animation->setItem(m_visualTip);
241                     m_animation->setTimeLine(m_animationTimer);
242                     m_visualTip->setPos(0, 0);
243                     double scale = 2.0;
244                     m_animation->setScaleAt(.5, scale, scale);
245                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width() + clip->fadeOut() * m_scale, clip->rect().y() - clip->rect().y() * scale));
246                     scale = 1.0;
247                     m_animation->setScaleAt(1, scale, scale);
248                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
249                     scene()->addItem(m_visualTip);
250                     m_animationTimer->start();
251                 }
252                 setCursor(Qt::PointingHandCursor);
253             }
254         } else {
255             m_moveOpMode = NONE;
256             if (event->buttons() != Qt::NoButton) {
257                 setCursorPos((int) mapToScene(event->pos().x(), 0).x());
258                 emit cursorMoved(cursorPos());
259             }
260             if (m_visualTip) {
261                 if (m_animation) delete m_animation;
262                 m_animationTimer->stop();
263                 m_animation = NULL;
264                 delete m_visualTip;
265                 m_visualTip = NULL;
266
267             }
268             setCursor(Qt::ArrowCursor);
269         }
270     }
271     QGraphicsView::mouseMoveEvent(event);
272 }
273
274 // virtual
275 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
276     int pos = event->x();
277     if (event->modifiers() == Qt::ControlModifier) {
278         setDragMode(QGraphicsView::ScrollHandDrag);
279         QGraphicsView::mousePressEvent(event);
280         return;
281     } else if (event->modifiers() == Qt::ShiftModifier) {
282         setDragMode(QGraphicsView::RubberBandDrag);
283         QGraphicsView::mousePressEvent(event);
284         return;
285     } else {
286         bool collision = false;
287         QList<QGraphicsItem *> collisionList = items(event->pos());
288         for (int i = 0; i < collisionList.size(); ++i) {
289             QGraphicsItem *item = collisionList.at(i);
290             if (item->type() == 70000) {
291                 // select item
292                 if (!item->isSelected()) {
293                     QList<QGraphicsItem *> itemList = items();
294                     for (int i = 0; i < itemList.count(); i++)
295                         itemList.at(i)->setSelected(false);
296                     item->setSelected(true);
297                     update();
298                 }
299                 m_dragItem = (ClipItem *) item;
300                 emit clipItemSelected(m_dragItem);
301                 m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos() * m_scale;
302                 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
303                 if (m_operationMode == MOVE || m_operationMode == RESIZESTART)
304                     m_startPos = QPointF(m_dragItem->startPos(), m_dragItem->track());
305                 else if (m_operationMode == RESIZEEND)
306                     m_startPos = QPointF(m_dragItem->endPos(), m_dragItem->track());
307                 kDebug() << "//////// ITEM CLICKED: " << m_startPos;
308                 collision = true;
309                 break;
310             }
311         }
312         if (!collision) {
313             kDebug() << "//////// NO ITEM FOUND ON CLICK";
314             m_dragItem = NULL;
315             setCursor(Qt::ArrowCursor);
316             QList<QGraphicsItem *> itemList = items();
317             for (int i = 0; i < itemList.count(); i++)
318                 itemList.at(i)->setSelected(false);
319             emit clipItemSelected(NULL);
320             setCursorPos((int) mapToScene(event->x(), 0).x());
321             emit cursorMoved(cursorPos());
322         }
323     }
324     updateSnapPoints(m_dragItem);
325     //kDebug()<<pos;
326     //QGraphicsView::mousePressEvent(event);
327 }
328
329 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
330     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
331         kDebug() << "///////////////  DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
332         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
333         //TODO: drop of several clips
334         for (int i = 0; i < ids.size(); ++i) {
335         }
336         DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
337         if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
338         addItem(clip, event->pos());
339         event->acceptProposedAction();
340     } else QGraphicsView::dragEnterEvent(event);
341 }
342
343 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
344     int track = m_tracksCount - clip->track();
345     GenTime pos = GenTime(clip->startPos(), m_document->fps());
346     m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
347     for (int i = 0; i < clip->effectsCount(); i++) {
348         m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
349     }
350     m_document->renderer()->doRefresh();
351 }
352
353 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
354     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
355     if (clip) {
356         QMap <QString, QString> effectParams = clip->addEffect(effect);
357         m_document->renderer()->mltAddEffect(track, pos, effectParams);
358         emit clipItemSelected(clip);
359     }
360 }
361
362 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
363     QString index = effect.attribute("kdenlive_ix");
364     m_document->renderer()->mltRemoveEffect(track, pos, index);
365     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
366     if (clip) {
367         clip->deleteEffect(index);
368         emit clipItemSelected(clip);
369     }
370 }
371
372 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
373     QList<QGraphicsItem *> itemList;
374     if (track == -1)
375         itemList = items();
376     else {
377         ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track);
378         if (clip) itemList.append(clip);
379         else kDebug() << "------   wrning, clip eff not found";
380     }
381     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track;
382     for (int i = 0; i < itemList.count(); i++) {
383         if (itemList.at(i)->type() == 70000 && (itemList.at(i)->isSelected() || track != -1)) {
384             ClipItem *item = (ClipItem *)itemList.at(i);
385             // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
386             // not be changed
387             if (effect.attribute("kdenlive_ix").toInt() == 0)
388                 effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
389             AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(), GenTime(item->startPos(), m_document->fps()), effect, true);
390             m_commandStack->push(command);
391         }
392     }
393 }
394
395 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
396     AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effect, false);
397     m_commandStack->push(command);
398 }
399
400 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) {
401     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
402     if (clip) {
403         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
404         if (effectParams["disabled"] == "1") {
405             QString index = effectParams["kdenlive_ix"];
406             m_document->renderer()->mltRemoveEffect(track, pos, index);
407         } else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effectParams);
408     }
409 }
410
411 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) {
412     QDomElement oldEffect = effect.cloneNode().toElement();
413     effect.setAttribute("disabled", disable);
414     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldEffect, effect, true);
415     m_commandStack->push(command);
416 }
417
418 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) {
419     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldeffect, effect, true);
420     m_commandStack->push(command);
421 }
422
423
424 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
425     int in = 0;
426     int out = clip->duration().frames(m_document->fps());
427     //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
428     int trackTop = ((int) mapToScene(pos).y() / 50) * 50 + 1;
429     m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y() / 50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
430     scene()->addItem(m_dropItem);
431 }
432
433
434 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
435     event->setDropAction(Qt::IgnoreAction);
436     //kDebug()<<"+++++++++++++   DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
437     if (m_dropItem) {
438         int track = (int) mapToScene(event->pos()).y() / 50; //) * (m_scale * 50) + m_scale;
439         m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
440         event->setDropAction(Qt::MoveAction);
441         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
442             event->acceptProposedAction();
443         }
444     } else {
445         QGraphicsView::dragMoveEvent(event);
446     }
447 }
448
449 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
450     if (m_dropItem) {
451         delete m_dropItem;
452         m_dropItem = NULL;
453     } else QGraphicsView::dragLeaveEvent(event);
454 }
455
456 void CustomTrackView::dropEvent(QDropEvent * event) {
457     if (m_dropItem) {
458         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false, false);
459         m_commandStack->push(command);
460         m_dropItem->baseClip()->addReference();
461         m_document->updateClip(m_dropItem->baseClip()->getId());
462         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
463         m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), GenTime(m_dropItem->startPos(), m_document->fps()), m_dropItem->xml());
464     } else QGraphicsView::dropEvent(event);
465     m_dropItem = NULL;
466 }
467
468
469 QStringList CustomTrackView::mimeTypes() const {
470     QStringList qstrList;
471     // list of accepted mime types for drop
472     qstrList.append("text/plain");
473     qstrList.append("kdenlive/producerslist");
474     return qstrList;
475 }
476
477 Qt::DropActions CustomTrackView::supportedDropActions() const {
478     // returns what actions are supported when dropping
479     return Qt::MoveAction;
480 }
481
482 void CustomTrackView::setDuration(int duration) {
483     kDebug() << "/////////////  PRO DUR: " << duration << ", height: " << 50 * m_tracksCount;
484     m_projectDuration = duration;
485     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
486 }
487
488
489 void CustomTrackView::addTrack() {
490     m_tracksCount++;
491     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
492     //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
493     //verticalScrollBar()->setMaximum(50 * m_tracksCount);
494     //setFixedHeight(50 * m_tracksCount);
495 }
496
497 void CustomTrackView::removeTrack() {
498     m_tracksCount--;
499     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
500 }
501
502 void CustomTrackView::deleteClip(int clipId) {
503     QList<QGraphicsItem *> itemList = items();
504     for (int i = 0; i < itemList.count(); i++) {
505         if (itemList.at(i)->type() == 70000) {
506             ClipItem *item = (ClipItem *)itemList.at(i);
507             if (item->clipProducer() == clipId) {
508                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
509                 m_commandStack->push(command);
510                 //delete item;
511             }
512         }
513     }
514 }
515
516 void CustomTrackView::setCursorPos(int pos, bool seek) {
517     m_cursorPos = pos;
518     m_cursorLine->setPos(pos, 0);
519     int frame =  pos / m_scale;
520     if (seek) m_document->renderer()->seek(GenTime(frame, m_document->fps()));
521 }
522
523 int CustomTrackView::cursorPos() {
524     return m_cursorPos;
525 }
526
527 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
528     QGraphicsView::mouseReleaseEvent(event);
529     setDragMode(QGraphicsView::NoDrag);
530     if (m_dragItem == NULL) return;
531     //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
532     if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos()) {
533         // move clip
534         MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
535         m_commandStack->push(command);
536         m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos());
537     } else if (m_operationMode == RESIZESTART) {
538         // resize start
539         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
540
541         m_document->renderer()->mltResizeClipStart(m_tracksCount - m_dragItem->track(), GenTime(m_dragItem->endPos(), m_document->fps()), GenTime(m_dragItem->startPos(), m_document->fps()), GenTime(m_startPos.x(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()) + GenTime(m_dragItem->endPos(), m_document->fps()) - GenTime(m_dragItem->startPos(), m_document->fps()));
542         m_commandStack->push(command);
543         m_document->renderer()->doRefresh();
544     } else if (m_operationMode == RESIZEEND) {
545         // resize end
546         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
547
548         m_document->renderer()->mltResizeClipEnd(m_tracksCount - m_dragItem->track(), GenTime(m_dragItem->startPos(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()) + GenTime(m_dragItem->endPos(), m_document->fps()) - GenTime(m_dragItem->startPos(), m_document->fps()));
549         m_commandStack->push(command);
550         m_document->renderer()->doRefresh();
551     }
552     m_operationMode = NONE;
553     m_dragItem = NULL;
554 }
555
556 void CustomTrackView::deleteClip(int track, int startpos, const QRectF &rect) {
557     ClipItem *item = getClipItemAt(startpos, track);
558     if (!item) {
559         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << rect.x();
560         return;
561     }
562     item->baseClip()->removeReference();
563     m_document->updateClip(item->baseClip()->getId());
564     delete item;
565     m_document->renderer()->mltRemoveClip(m_tracksCount - track, GenTime(startpos, m_document->fps()));
566     m_document->renderer()->doRefresh();
567 }
568
569 void CustomTrackView::addClip(QDomElement xml, int clipId, int track, int startpos, const QRectF &rect, int duration) {
570     QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49);
571     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
572     ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration);
573     scene()->addItem(item);
574     baseclip->addReference();
575     m_document->updateClip(baseclip->getId());
576     m_document->renderer()->mltInsertClip(m_tracksCount - track, GenTime(startpos, m_document->fps()), xml);
577     m_document->renderer()->doRefresh();
578 }
579
580 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
581     return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
582 }
583
584 void CustomTrackView::moveClip(const QPointF &startPos, const QPointF &endPos) {
585     ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
586     if (!item) {
587         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * 50 + 25;
588         return;
589     }
590     kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x();
591     item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
592     m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
593 }
594
595 void CustomTrackView::resizeClip(const QPointF &startPos, const QPointF &endPos, bool resizeClipStart) {
596     int offset;
597     if (resizeClipStart) offset = 1;
598     else offset = -1;
599     ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
600     if (!item) {
601         kDebug() << "----------------  ERROR, CANNOT find clip to resize at: " << startPos;
602         return;
603     }
604     qreal diff = endPos.x() - startPos.x();
605     if (resizeClipStart) {
606         m_document->renderer()->mltResizeClipStart(m_tracksCount - item->track(), GenTime(item->endPos(), m_document->fps()), GenTime(endPos.x(), m_document->fps()), GenTime(item->startPos(), m_document->fps()), GenTime(item->cropStart() + diff, m_document->fps()), GenTime(item->cropStart() + diff, m_document->fps()) + GenTime(item->endPos(), m_document->fps()) - GenTime(endPos.x(), m_document->fps()));
607         item->resizeStart(endPos.x(), m_scale);
608     } else {
609         m_document->renderer()->mltResizeClipEnd(m_tracksCount - item->track(), GenTime(item->startPos(), m_document->fps()), GenTime(item->cropStart(), m_document->fps()), GenTime(item->cropStart(), m_document->fps()) + GenTime(endPos.x(), m_document->fps()) - GenTime(item->startPos(), m_document->fps()));
610         item->resizeEnd(endPos.x(), m_scale);
611     }
612     m_document->renderer()->doRefresh();
613 }
614
615 double CustomTrackView::getSnapPointForPos(double pos) {
616     for (int i = 0; i < m_snapPoints.size(); ++i) {
617         //kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
618         if (abs(pos - m_snapPoints.at(i) * m_scale) < 6 * m_scale) {
619             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
620             return m_snapPoints.at(i) * m_scale + 0.5;
621         }
622         if (m_snapPoints.at(i) > pos) break;
623     }
624     return pos;
625 }
626
627 void CustomTrackView::updateSnapPoints(ClipItem *selected) {
628     m_snapPoints.clear();
629     int offset = 0;
630     if (selected) offset = selected->duration();
631     QList<QGraphicsItem *> itemList = items();
632     for (int i = 0; i < itemList.count(); i++) {
633         if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
634             ClipItem *item = (ClipItem *)itemList.at(i);
635             int start = item->startPos();
636             int fadein = item->fadeIn() + start;
637             int end = item->endPos();
638             int fadeout = end - item->fadeOut();
639             m_snapPoints.append(start);
640             if (fadein != start) m_snapPoints.append(fadein);
641             m_snapPoints.append(end);
642             if (fadeout != end) m_snapPoints.append(fadeout);
643             if (offset != 0) {
644                 m_snapPoints.append(start - offset);
645                 if (fadein != start) m_snapPoints.append(fadein - offset);
646                 m_snapPoints.append(end - offset);
647                 if (fadeout != end) m_snapPoints.append(fadeout - offset);
648             }
649         }
650     }
651     kDebug() << " GOT SNAPPOINTS TOTAL: " << m_snapPoints.count();
652     qSort(m_snapPoints);
653     for (int i = 0; i < m_snapPoints.size(); ++i)
654         kDebug() << "SNAP POINT: " << m_snapPoints.at(i);
655 }
656
657
658 void CustomTrackView::setScale(double scaleFactor) {
659     //scale(scaleFactor, scaleFactor);
660     m_scale = scaleFactor;
661     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
662     QList<QGraphicsItem *> itemList = items();
663     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 *
664
665     for (int i = 0; i < itemList.count(); i++) {
666         if (itemList.at(i)->type() == 70000) {
667             ClipItem *clip = (ClipItem *)itemList.at(i);
668             clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
669         }
670         /*else if (itemList.at(i)->type() == 70001) {
671         LabelItem *label = (LabelItem *)itemList.at(i);
672         QGraphicsItem *parent = label->parentItem();
673         QRectF r = label->boundingRect();
674         QRectF p = parent->boundingRect();
675         label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
676         //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
677         }*/
678     }
679 }
680
681 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
682     QRect rectInView;//this is the rect that is visible by the user
683     if (scene()->views().size() > 0) {
684         rectInView = scene()->views()[0]->viewport()->rect();
685         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
686     }
687     if (rectInView.isNull()) return;
688
689     QColor base = palette().button().color();
690     painter->setClipRect(rect);
691     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
692     for (uint i = 0; i < m_tracksCount;i++) {
693         painter->drawLine(rectInView.left(), 50 * (i + 1), rectInView.right(), 50 * (i + 1));
694         painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
695     }
696     int lowerLimit = 50 * m_tracksCount + 1;
697     if (height() > lowerLimit)
698         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
699 }
700 /*
701 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
702 {
703   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
704   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
705   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
706 }
707 */
708 #include "customtrackview.moc"