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