]> git.sesse.net Git - kdenlive/blob - src/effectstack/effectstackview2.cpp
Fix broken keyframes when dropping an effect on another clip
[kdenlive] / src / effectstack / effectstackview2.cpp
1 /***************************************************************************
2                           effecstackview.cpp2  -  description
3                              -------------------
4     begin                : Feb 15 2008
5     copyright            : (C) 2008 by Marco Gittler (g.marco@freenet.de)
6     copyright            : (C) 2012 by Jean-Baptiste Mardelle (jb@kdenlive.org)
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18
19 #include "effectstackview2.h"
20 #include "effectslist.h"
21 #include "clipitem.h"
22 #include "mainwindow.h"
23 #include "docclipbase.h"
24 #include "projectlist.h"
25 #include "kthumb.h"
26 #include "monitoreditwidget.h"
27 #include "monitorscene.h"
28 #include "kdenlivesettings.h"
29 #include "collapsibleeffect.h"
30 #include "collapsiblegroup.h"
31
32 #include <KDebug>
33 #include <KLocale>
34 #include <KMessageBox>
35 #include <KStandardDirs>
36 #include <KFileDialog>
37 #include <KColorScheme>
38 #include <KColorUtils>
39
40 #include <QMenu>
41 #include <QTextStream>
42 #include <QFile>
43 #include <QInputDialog>
44 #include <QScrollBar>
45
46
47 EffectStackView2::EffectStackView2(Monitor *monitor, QWidget *parent) :
48         QWidget(parent),
49         m_clipref(NULL),
50         m_trackindex(-1),
51         m_draggedEffect(NULL),
52         m_draggedGroup(NULL),
53         m_groupIndex(0),
54         m_monitorSceneWanted(false)
55 {
56     m_effectMetaInfo.trackMode = false;
57     m_effectMetaInfo.monitor = monitor;
58     m_effects = QList <CollapsibleEffect*>();
59     setAcceptDrops(true);
60
61     m_ui.setupUi(this);
62     setFont(KGlobalSettings::smallestReadableFont());
63     m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
64     m_ui.buttonShowComments->setIcon(KIcon("help-about"));
65     m_ui.buttonShowComments->setToolTip(i18n("Show additional information for the parameters"));
66
67     connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
68     connect(m_ui.buttonShowComments, SIGNAL(clicked()), this, SLOT(slotShowComments()));
69     m_ui.labelComment->setHidden(true);
70
71     setEnabled(false);
72
73
74     setStyleSheet(getStyleSheet());
75 }
76
77 EffectStackView2::~EffectStackView2()
78 {
79 }
80
81 void EffectStackView2::updatePalette()
82 {
83     setStyleSheet(getStyleSheet());
84 }
85
86 void EffectStackView2::slotRenderPos(int pos)
87 {
88     if (m_effects.isEmpty()) return;
89     if (m_monitorSceneWanted) slotCheckMonitorPosition(pos);
90     if (!m_effectMetaInfo.trackMode && m_clipref) pos = pos - m_clipref->startPos().frames(KdenliveSettings::project_fps());
91
92     for (int i = 0; i< m_effects.count(); i++)
93         m_effects.at(i)->slotSyncEffectsPos(pos);
94 }
95
96 void EffectStackView2::slotClipItemSelected(ClipItem* c)
97 {
98     if (c && !c->isEnabled()) return;
99     if (c && c == m_clipref) {
100
101     } else {
102         m_clipref = c;
103         if (c) {
104             QString cname = m_clipref->clipName();
105             if (cname.length() > 30) {
106                 m_ui.checkAll->setToolTip(i18n("Effects for %1", cname));
107                 cname.truncate(27);
108                 m_ui.checkAll->setText(i18n("Effects for %1", cname) + "...");
109             } else {
110                 m_ui.checkAll->setToolTip(QString());
111                 m_ui.checkAll->setText(i18n("Effects for %1", cname));
112             }
113             m_ui.checkAll->setEnabled(true);
114             QString size = c->baseClip()->getProperty("frame_size");
115             double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
116             m_effectMetaInfo.frameSize = QPoint((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
117         }
118     }
119     if (m_clipref == NULL) {
120         //TODO: clear list, reset paramdesc and info
121         // If monitor scene is displayed, hide it
122         if (m_monitorSceneWanted) {
123             m_effectMetaInfo.monitor->slotShowEffectScene(false);
124         }
125         m_monitorSceneWanted = false;
126         clear();
127         return;
128     }
129     setEnabled(true);
130     m_effectMetaInfo.trackMode = false;
131     m_currentEffectList = m_clipref->effectList();
132     setupListView();
133 }
134
135 void EffectStackView2::slotTrackItemSelected(int ix, const TrackInfo info)
136 {
137     m_clipref = NULL;
138     m_effectMetaInfo.trackMode = true;
139     m_currentEffectList = info.effectsList;
140     m_trackInfo = info;
141     setEnabled(true);
142     m_ui.checkAll->setToolTip(QString());
143     m_ui.checkAll->setText(i18n("Effects for track %1", info.trackName.isEmpty() ? QString::number(ix) : info.trackName));
144     m_ui.checkAll->setEnabled(true);
145     m_trackindex = ix;
146     setupListView();
147 }
148
149
150 void EffectStackView2::setupListView()
151 {
152     blockSignals(true);
153     m_monitorSceneWanted = false;
154     m_draggedEffect = NULL;
155     m_draggedGroup = NULL;
156     disconnect(m_effectMetaInfo.monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
157     m_effects.clear();
158     m_groupIndex = 0;
159     QWidget *view = m_ui.container->takeWidget();
160     if (view) {
161         delete view;
162     }
163     blockSignals(false);
164     view = new QWidget(m_ui.container);
165     m_ui.container->setWidget(view);
166
167     QVBoxLayout *vbox1 = new QVBoxLayout(view);
168     vbox1->setContentsMargins(0, 0, 0, 0);
169     vbox1->setSpacing(0);
170
171     int effectsCount = m_currentEffectList.count();
172
173     // Make sure we always have one effect selected
174     if (!m_effectMetaInfo.trackMode) {
175         int selectedEffect = m_clipref->selectedEffectIndex();
176         if (selectedEffect < 1 && effectsCount > 0) m_clipref->setSelectedEffect(1);
177         else if (selectedEffect > effectsCount) m_clipref->setSelectedEffect(effectsCount);
178     }
179
180     for (int i = 0; i < effectsCount; i++) {
181         QDomElement d = m_currentEffectList.at(i).cloneNode().toElement();
182         if (d.isNull()) {
183             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
184             continue;
185         }
186
187         CollapsibleGroup *group = NULL;
188         EffectInfo effectInfo;
189         effectInfo.fromString(d.attribute("kdenlive_info"));
190         if (effectInfo.groupIndex >= 0) {
191             // effect is in a group
192             for (int j = 0; j < vbox1->count(); j++) {
193                 CollapsibleGroup *eff = static_cast<CollapsibleGroup *>(vbox1->itemAt(j)->widget());
194                 if (eff->isGroup() &&  eff->groupIndex() == effectInfo.groupIndex) {
195                     group = eff;
196                     break;
197                 }
198             }
199
200             if (group == NULL) {
201                 group = new CollapsibleGroup(effectInfo.groupIndex, i == 0, i == effectsCount - 1, effectInfo, m_ui.container->widget());
202                 connectGroup(group);
203                 vbox1->addWidget(group);
204                 group->installEventFilter( this );
205             }
206             if (effectInfo.groupIndex >= m_groupIndex) m_groupIndex = effectInfo.groupIndex + 1;
207         }
208
209         /*QDomDocument doc;
210         doc.appendChild(doc.importNode(d, true));
211         kDebug() << "IMPORTED STK: " << doc.toString();*/
212
213         ItemInfo info;
214         bool isSelected = false;
215         if (m_effectMetaInfo.trackMode) {
216             info.track = m_trackInfo.type;
217             info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
218             info.cropStart = GenTime(0);
219             info.startPos = GenTime(-1);
220             info.track = 0;
221         }
222         else {
223             info = m_clipref->info();
224         }
225
226         CollapsibleEffect *currentEffect = new CollapsibleEffect(d, m_currentEffectList.at(i), info, &m_effectMetaInfo, i == effectsCount - 1, view);
227         if (m_effectMetaInfo.trackMode) {
228             isSelected = currentEffect->effectIndex() == 1;
229         }
230         else {
231             isSelected = currentEffect->effectIndex() == m_clipref->selectedEffectIndex();
232         }
233         if (isSelected) {
234             currentEffect->setActive(true);
235             if (currentEffect->needsMonitorEffectScene()) m_monitorSceneWanted = true;
236         }
237         m_effects.append(currentEffect);
238         if (group) {
239             group->addGroupEffect(currentEffect);
240         } else {
241             vbox1->addWidget(currentEffect);
242         }
243         connectEffect(currentEffect);
244     }
245
246     if (m_currentEffectList.isEmpty()) {
247         m_ui.labelComment->setHidden(true);
248     }
249     else {
250         // Adjust group effects (up / down buttons)
251         QList<CollapsibleGroup *> allGroups = m_ui.container->widget()->findChildren<CollapsibleGroup *>();
252         for (int i = 0; i < allGroups.count(); i++) {
253             allGroups.at(i)->adjustEffects();
254         }
255         connect(m_effectMetaInfo.monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
256     }
257
258     vbox1->addStretch(10);
259     slotUpdateCheckAllButton();
260     if (!m_monitorSceneWanted) {
261         // monitor scene not wanted
262         m_effectMetaInfo.monitor->slotShowEffectScene(false);
263     }
264
265     // Wait a little bit for the new layout to be ready, then check if we have a scrollbar
266     QTimer::singleShot(200, this, SLOT(slotCheckWheelEventFilter()));
267 }
268
269 void EffectStackView2::connectEffect(CollapsibleEffect *currentEffect)
270 {
271     // Check drag & drop
272     currentEffect->installEventFilter( this );
273     connect(currentEffect, SIGNAL(parameterChanged(const QDomElement, const QDomElement, int)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement, int)));
274     connect(currentEffect, SIGNAL(startFilterJob(QString,QString,QString,QString,QString,QString)), this , SLOT(slotStartFilterJob(QString,QString,QString,QString,QString,QString)));
275     connect(currentEffect, SIGNAL(deleteEffect(const QDomElement)), this , SLOT(slotDeleteEffect(const QDomElement)));
276     connect(currentEffect, SIGNAL(reloadEffects()), this , SIGNAL(reloadEffects()));
277     connect(currentEffect, SIGNAL(resetEffect(int)), this , SLOT(slotResetEffect(int)));
278     connect(currentEffect, SIGNAL(changeEffectPosition(QList <int>,bool)), this , SLOT(slotMoveEffectUp(QList <int>,bool)));
279     connect(currentEffect, SIGNAL(effectStateChanged(bool,int,bool)), this, SLOT(slotUpdateEffectState(bool,int,bool)));
280     connect(currentEffect, SIGNAL(activateEffect(int)), this, SLOT(slotSetCurrentEffect(int)));
281     connect(currentEffect, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
282     connect(currentEffect, SIGNAL(createGroup(int)), this , SLOT(slotCreateGroup(int)));
283     connect(currentEffect, SIGNAL(moveEffect(QList<int>,int,int,QString)), this , SLOT(slotMoveEffect(QList<int>,int,int,QString)));
284     connect(currentEffect, SIGNAL(addEffect(QDomElement)), this , SLOT(slotAddEffect(QDomElement)));
285     connect(currentEffect, SIGNAL(createRegion(int,KUrl)), this, SLOT(slotCreateRegion(int,KUrl)));
286     connect(currentEffect, SIGNAL(deleteGroup(QDomDocument)), this , SLOT(slotDeleteGroup(QDomDocument)));
287 }
288
289 void EffectStackView2::slotCheckWheelEventFilter()
290 {
291     // If the effect stack widget has no scrollbar, we will not filter the
292     // mouse wheel events, so that user can easily adjust effect params
293     bool filterWheelEvent = false;
294     if (m_ui.container->verticalScrollBar() && m_ui.container->verticalScrollBar()->isVisible()) {
295         // widget has scroll bar,
296         filterWheelEvent = true;
297     }
298     for (int i = 0; i < m_effects.count(); i++) {
299         m_effects.at(i)->filterWheelEvent = filterWheelEvent;
300     }
301 }
302
303 void EffectStackView2::resizeEvent ( QResizeEvent * event )
304 {
305     slotCheckWheelEventFilter();
306     QWidget::resizeEvent(event);
307 }
308
309 bool EffectStackView2::eventFilter( QObject * o, QEvent * e )
310 {
311     // Check if user clicked in an effect's top bar to start dragging it
312     if (e->type() == QEvent::MouseButtonPress)  {
313         m_draggedEffect = qobject_cast<CollapsibleEffect*>(o);
314         if (m_draggedEffect) {
315             QMouseEvent *me = static_cast<QMouseEvent *>(e);
316             if (me->button() == Qt::LeftButton && (m_draggedEffect->frame->underMouse() || m_draggedEffect->title->underMouse())) {
317                 m_clickPoint = me->globalPos();
318             }
319             else {
320                 m_clickPoint = QPoint();
321                 m_draggedEffect = NULL;
322             }
323             e->accept();
324             return true;
325         }
326         m_draggedGroup = qobject_cast<CollapsibleGroup*>(o);
327         if (m_draggedGroup) {
328             QMouseEvent *me = static_cast<QMouseEvent *>(e);
329             if (me->button() == Qt::LeftButton && (m_draggedGroup->frame->underMouse() || m_draggedGroup->title()->underMouse()))
330                 m_clickPoint = me->globalPos();
331             else {
332                 m_clickPoint = QPoint();
333                 m_draggedGroup = NULL;
334             }
335             e->accept();
336             return true;
337         }
338     }
339     /*if (e->type() == QEvent::MouseMove)  {
340     if (qobject_cast<CollapsibleEffect*>(o)) {
341         QMouseEvent *me = static_cast<QMouseEvent *>(e);
342         if (me->buttons() != Qt::LeftButton) {
343         e->accept();
344         return false;
345         }
346         else {
347         e->ignore();
348         return true;
349         }
350     }
351     }*/
352     return QWidget::eventFilter(o, e);
353 }
354
355 void EffectStackView2::mouseMoveEvent(QMouseEvent * event)
356 {
357     if (m_draggedEffect || m_draggedGroup) {
358         if ((event->buttons() & Qt::LeftButton) && (m_clickPoint != QPoint()) && ((event->globalPos() - m_clickPoint).manhattanLength() >= QApplication::startDragDistance())) {
359             startDrag();
360         }
361     }
362 }
363
364 void EffectStackView2::mouseReleaseEvent(QMouseEvent * event)
365 {
366     m_draggedEffect = NULL;
367     m_draggedGroup = NULL;
368     QWidget::mouseReleaseEvent(event);
369 }
370
371 void EffectStackView2::startDrag()
372 {
373     // The data to be transferred by the drag and drop operation is contained in a QMimeData object
374     QDomDocument doc;
375     QPixmap pixmap;
376     if (m_draggedEffect) {
377         QDomElement effect = m_draggedEffect->effect().cloneNode().toElement();
378         // Keep clip crop start in case we want to paste effect 
379         effect.setAttribute("clipstart", m_clipref->cropStart().frames(KdenliveSettings::project_fps()));
380         doc.appendChild(doc.importNode(effect, true));
381         pixmap = QPixmap::grabWidget(m_draggedEffect->title);
382     }
383     else if (m_draggedGroup) {
384         doc = m_draggedGroup->effectsData();
385         pixmap = QPixmap::grabWidget(m_draggedGroup->title());
386     }
387     else return;
388     QDrag *drag = new QDrag(this);
389     drag->setPixmap(pixmap);
390     QMimeData *mime = new QMimeData;
391     QByteArray data;
392     data.append(doc.toString().toUtf8());
393     mime->setData("kdenlive/effectslist", data);
394
395     // Assign ownership of the QMimeData object to the QDrag object.
396     drag->setMimeData(mime);
397     // Start the drag and drop operation
398     drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
399 }
400
401
402 void EffectStackView2::slotUpdateEffectState(bool disable, int index, bool needsMonitorEffectScene)
403 {
404     if (m_monitorSceneWanted && disable) {
405         m_effectMetaInfo.monitor->slotShowEffectScene(false);
406         m_monitorSceneWanted = false;
407     }
408     else if (!disable && !m_monitorSceneWanted && needsMonitorEffectScene) {
409         m_effectMetaInfo.monitor->slotShowEffectScene(true);
410         m_monitorSceneWanted = true;
411     }
412     if (m_effectMetaInfo.trackMode)
413         emit changeEffectState(NULL, m_trackindex, QList <int>() << index, disable);
414     else
415         emit changeEffectState(m_clipref, -1, QList <int>() <<index, disable);
416     slotUpdateCheckAllButton();
417 }
418
419
420 void EffectStackView2::raiseWindow(QWidget* dock)
421 {
422     if ((m_clipref || m_effectMetaInfo.trackMode) && dock)
423         dock->raise();
424 }
425
426
427 void EffectStackView2::slotSeekTimeline(int pos)
428 {
429     if (m_effectMetaInfo.trackMode) {
430         emit seekTimeline(pos);
431     } else if (m_clipref) {
432         emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
433     }
434 }
435
436
437 /*void EffectStackView2::slotRegionChanged()
438 {
439     if (!m_trackMode) emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
440 }*/
441
442 void EffectStackView2::slotCheckMonitorPosition(int renderPos)
443 {
444     if (m_monitorSceneWanted) {
445         if (m_effectMetaInfo.trackMode || (m_clipref && renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
446             if (!m_effectMetaInfo.monitor->effectSceneDisplayed()) {
447                 m_effectMetaInfo.monitor->slotShowEffectScene(true);
448             }
449         } else {
450             m_effectMetaInfo.monitor->slotShowEffectScene(false);
451         }
452     }
453     else {
454         m_effectMetaInfo.monitor->slotShowEffectScene(false);
455     }
456 }
457
458 int EffectStackView2::isTrackMode(bool *ok) const
459 {
460     *ok = m_effectMetaInfo.trackMode;
461     return m_trackindex;
462 }
463
464 void EffectStackView2::clear()
465 {
466     m_effects.clear();
467     m_monitorSceneWanted = false;
468     QWidget *view = m_ui.container->takeWidget();
469     if (view) {
470         delete view;
471     }
472     m_ui.checkAll->setToolTip(QString());
473     m_ui.checkAll->setText(QString());
474     m_ui.checkAll->setEnabled(false);
475     m_ui.labelComment->setText(QString());
476     setEnabled(false);
477 }
478
479 void EffectStackView2::slotCheckAll(int state)
480 {
481     if (state == Qt::PartiallyChecked) {
482         state = Qt::Checked;
483         m_ui.checkAll->blockSignals(true);
484         m_ui.checkAll->setCheckState(Qt::Checked);
485         m_ui.checkAll->blockSignals(false);
486     }
487
488     bool disabled = state == Qt::Unchecked;
489     // Disable all effects
490     QList <int> indexes;
491     for (int i = 0; i < m_effects.count(); i++) {
492         m_effects.at(i)->slotEnable(disabled, false);
493         indexes << m_effects.at(i)->effectIndex();
494     }
495     // Take care of groups
496     QList<CollapsibleGroup *> allGroups = m_ui.container->widget()->findChildren<CollapsibleGroup *>();
497     for (int i = 0; i < allGroups.count(); i++) {
498         allGroups.at(i)->slotEnable(disabled, false);
499     }
500
501     if (m_effectMetaInfo.trackMode)
502         emit changeEffectState(NULL, m_trackindex, indexes, disabled);
503     else
504         emit changeEffectState(m_clipref, -1, indexes, disabled);
505 }
506
507 void EffectStackView2::slotUpdateCheckAllButton()
508 {
509     bool hasEnabled = false;
510     bool hasDisabled = false;
511
512     for (int i = 0; i < m_effects.count(); i++) {
513         if (!m_effects.at(i)->enabledButton->isChecked()) hasEnabled = true;
514         else hasDisabled = true;
515     }
516
517     m_ui.checkAll->blockSignals(true);
518     if (hasEnabled && hasDisabled)
519         m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
520     else if (hasEnabled)
521         m_ui.checkAll->setCheckState(Qt::Checked);
522     else
523         m_ui.checkAll->setCheckState(Qt::Unchecked);
524     m_ui.checkAll->blockSignals(false);
525 }
526
527 void EffectStackView2::deleteCurrentEffect()
528 {
529     for (int i = 0; i < m_effects.count(); i++) {
530         if (m_effects.at(i)->isActive()) {
531             slotDeleteEffect(m_effects.at(i)->effect());
532             break;
533         }
534     }
535 }
536
537 void EffectStackView2::updateProjectFormat(MltVideoProfile profile, Timecode t)
538 {
539     m_effectMetaInfo.profile = profile;
540     m_effectMetaInfo.timecode = t;
541 }
542
543 void EffectStackView2::updateTimecodeFormat()
544 {
545     for (int i = 0; i< m_effects.count(); i++)
546         m_effects.at(i)->updateTimecodeFormat();
547 }
548
549 CollapsibleEffect *EffectStackView2::getEffectByIndex(int ix)
550 {
551     for (int i = 0; i< m_effects.count(); i++) {
552         if (m_effects.at(i)->effectIndex() == ix) {
553             return m_effects.at(i);
554         }
555     }
556     return NULL;
557 }
558
559 void EffectStackView2::slotUpdateEffectParams(const QDomElement old, const QDomElement e, int ix)
560 {
561     if (m_effectMetaInfo.trackMode)
562         emit updateEffect(NULL, m_trackindex, old, e, ix,false);
563     else if (m_clipref) {
564         emit updateEffect(m_clipref, -1, old, e, ix, false);
565         // Make sure the changed effect is currently displayed
566         slotSetCurrentEffect(ix);
567     }
568     QTimer::singleShot(200, this, SLOT(slotCheckWheelEventFilter()));
569 }
570
571 void EffectStackView2::slotSetCurrentEffect(int ix)
572 {
573     if (m_clipref && ix != m_clipref->selectedEffectIndex()) {
574         m_clipref->setSelectedEffect(ix);
575         for (int i = 0; i < m_effects.count(); i++) {
576             if (m_effects.at(i)->effectIndex() == ix) {
577                 if (m_effects.at(i)->isActive()) return;
578                 m_effects.at(i)->setActive(true);
579                 m_monitorSceneWanted = m_effects.at(i)->needsMonitorEffectScene();
580                 slotCheckMonitorPosition(m_effectMetaInfo.monitor->render->seekFramePosition());
581                 m_ui.labelComment->setText(i18n(m_effects.at(i)->effect().firstChildElement("description").firstChildElement("full").text().toUtf8().data()));
582                 m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || m_ui.labelComment->text().isEmpty());
583             }
584             else m_effects.at(i)->setActive(false);
585         }
586     }
587 }
588
589 void EffectStackView2::slotDeleteGroup(QDomDocument doc)
590 {
591     QDomNodeList effects = doc.elementsByTagName("effect");
592     ClipItem * clip = NULL;
593     int ix;
594     if (m_effectMetaInfo.trackMode) {
595         ix = m_trackindex;
596     }
597     else {
598         clip = m_clipref;
599         ix = -1;
600     }
601
602     for (int i = 0; i < effects.count(); i++)
603         emit removeEffect(clip, ix, effects.at(i).toElement());
604 }
605
606 void EffectStackView2::slotDeleteEffect(const QDomElement effect)
607 {
608     if (m_effectMetaInfo.trackMode)
609         emit removeEffect(NULL, m_trackindex, effect);
610     else
611         emit removeEffect(m_clipref, -1, effect);
612 }
613
614 void EffectStackView2::slotAddEffect(QDomElement effect)
615 {
616     emit addEffect(m_clipref, effect);
617 }
618
619 void EffectStackView2::slotMoveEffectUp(QList <int> indexes, bool up)
620 {
621     if (up && indexes.first() <= 1) return;
622     if (!up && indexes.last() >= m_currentEffectList.count()) return;
623     int endPos;
624     if (up) {
625         endPos = indexes.first() - 1;
626     }
627     else {
628         endPos =  indexes.last() + 1;
629     }
630     if (m_effectMetaInfo.trackMode) emit changeEffectPosition(NULL, m_trackindex, indexes, endPos);
631     else emit changeEffectPosition(m_clipref, -1, indexes, endPos);
632 }
633
634 void EffectStackView2::slotStartFilterJob(const QString&filterName, const QString&filterParams, const QString&finalFilterName, const QString&consumer, const QString&consumerParams, const QString&properties)
635 {
636     if (!m_clipref) return;
637     emit startFilterJob(m_clipref->info(), m_clipref->clipProducer(), filterName, filterParams, finalFilterName, consumer, consumerParams, properties);
638 }
639
640 void EffectStackView2::slotResetEffect(int ix)
641 {
642     QDomElement old = m_currentEffectList.itemFromIndex(ix);
643     QDomElement dom;
644     QString effectId = old.attribute("id");
645     QMap<QString, EffectsList*> effectLists;
646     effectLists["audio"] = &MainWindow::audioEffects;
647     effectLists["video"] = &MainWindow::videoEffects;
648     effectLists["custom"] = &MainWindow::customEffects;
649     foreach(const QString &type, effectLists.keys()) {
650         EffectsList *list = effectLists[type];
651         dom = list->getEffectByTag(QString(), effectId).cloneNode().toElement();
652         if (!dom.isNull()) break;
653     }
654     if (!dom.isNull()) {
655         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
656         if (m_effectMetaInfo.trackMode) {
657             EffectsList::setParameter(dom, "in", QString::number(0));
658             EffectsList::setParameter(dom, "out", QString::number(m_trackInfo.duration));
659             ItemInfo info;
660             info.track = m_trackInfo.type;
661             info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
662             info.cropStart = GenTime(0);
663             info.startPos = GenTime(-1);
664             info.track = 0;
665             for (int i = 0; i < m_effects.count(); i++) {
666                 if (m_effects.at(i)->effectIndex() == ix) {
667                     m_effects.at(i)->updateWidget(info, dom, &m_effectMetaInfo);
668                     break;
669                 }
670             }
671             emit updateEffect(NULL, m_trackindex, old, dom, ix,false);
672         } else {
673             m_clipref->initEffect(dom);
674             for (int i = 0; i < m_effects.count(); i++) {
675                 if (m_effects.at(i)->effectIndex() == ix) {
676                     m_effects.at(i)->updateWidget(m_clipref->info(), dom, &m_effectMetaInfo);
677                     break;
678                 }
679             }
680             //m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
681             emit updateEffect(m_clipref, -1, old, dom, ix,false);
682         }
683     }
684
685     emit showComments(m_ui.buttonShowComments->isChecked());
686     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || m_ui.labelComment->text().isEmpty());
687 }
688
689 void EffectStackView2::slotShowComments()
690 {
691     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || m_ui.labelComment->text().isEmpty());
692     emit showComments(m_ui.buttonShowComments->isChecked());
693 }
694
695 void EffectStackView2::slotCreateRegion(int ix, KUrl url)
696 {
697     QDomElement oldeffect = m_currentEffectList.itemFromIndex(ix);
698     QDomElement neweffect = oldeffect.cloneNode().toElement();
699     QDomElement region = MainWindow::videoEffects.getEffectByTag("region", "region").cloneNode().toElement();
700     region.appendChild(region.ownerDocument().importNode(neweffect, true));
701     region.setAttribute("kdenlive_ix", ix);
702     EffectsList::setParameter(region, "resource", url.path());
703     if (m_effectMetaInfo.trackMode)
704         emit updateEffect(NULL, m_trackindex, oldeffect, region, ix,false);
705     else if (m_clipref) {
706         emit updateEffect(m_clipref, -1, oldeffect, region, ix, false);
707         // Make sure the changed effect is currently displayed
708         //slotSetCurrentEffect(ix);
709     }
710     // refresh effect stack
711     ItemInfo info;
712     bool isSelected = false;
713     if (m_effectMetaInfo.trackMode) {
714         info.track = m_trackInfo.type;
715         info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
716         info.cropStart = GenTime(0);
717         info.startPos = GenTime(-1);
718         info.track = 0;
719     }
720     else {
721         info = m_clipref->info();
722     }
723     CollapsibleEffect *current = getEffectByIndex(ix);
724     m_effects.removeAll(current);
725     current->setEnabled(false);
726     m_currentEffectList.removeAt(ix);
727     m_currentEffectList.insert(region);
728     current->deleteLater();
729     CollapsibleEffect *currentEffect = new CollapsibleEffect(region, m_currentEffectList.itemFromIndex(ix), info, &m_effectMetaInfo, ix == m_currentEffectList.count() - 1, m_ui.container->widget());
730     connectEffect(currentEffect);
731
732     if (m_effectMetaInfo.trackMode) {
733         isSelected = currentEffect->effectIndex() == 1;
734     }
735     else {
736         isSelected = currentEffect->effectIndex() == m_clipref->selectedEffectIndex();
737     }
738     if (isSelected) currentEffect->setActive(true);
739     m_effects.append(currentEffect);
740     // TODO: region in group?
741     //if (group) {
742     //  group->addGroupEffect(currentEffect);
743     //} else {
744     QVBoxLayout *vbox = static_cast <QVBoxLayout *> (m_ui.container->widget()->layout());
745     vbox->insertWidget(ix, currentEffect);
746     //}
747
748     // Check drag & drop
749     currentEffect->installEventFilter( this );
750
751     QTimer::singleShot(200, this, SLOT(slotCheckWheelEventFilter()));
752
753 }
754
755 void EffectStackView2::slotCreateGroup(int ix)
756 {
757     QDomElement oldeffect = m_currentEffectList.itemFromIndex(ix);
758     QDomElement neweffect = oldeffect.cloneNode().toElement();
759     EffectInfo effectinfo;
760     effectinfo.fromString(oldeffect.attribute("kdenlive_info"));
761     effectinfo.groupIndex = m_groupIndex;
762     neweffect.setAttribute("kdenlive_info", effectinfo.toString());
763
764     ItemInfo info;
765     if (m_effectMetaInfo.trackMode) {
766         info.track = m_trackInfo.type;
767         info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
768         info.cropStart = GenTime(0);
769         info.startPos = GenTime(-1);
770         info.track = 0;
771         emit updateEffect(NULL, m_trackindex, oldeffect, neweffect, ix, false);
772     } else {
773         emit updateEffect(m_clipref, -1, oldeffect, neweffect, ix, false);
774     }
775
776     QVBoxLayout *l = static_cast<QVBoxLayout *>(m_ui.container->widget()->layout());
777     int groupPos = 0;
778     CollapsibleEffect *effectToMove = NULL;
779     for (int i = 0; i < m_effects.count(); i++) {
780         if (m_effects.at(i)->effectIndex() == ix) {
781             effectToMove = m_effects.at(i);
782             groupPos = l->indexOf(effectToMove);
783             l->removeWidget(effectToMove);
784             break;
785         }
786     }
787
788     CollapsibleGroup *group = new CollapsibleGroup(m_groupIndex, ix == 1, ix == m_currentEffectList.count() - 2, effectinfo, m_ui.container->widget());
789     m_groupIndex++;
790     connectGroup(group);
791     l->insertWidget(groupPos, group);
792     group->installEventFilter( this );
793     group->addGroupEffect(effectToMove);
794 }
795
796 void EffectStackView2::connectGroup(CollapsibleGroup *group)
797 {
798     connect(group, SIGNAL(moveEffect(QList<int>,int,int,QString)), this , SLOT(slotMoveEffect(QList<int>,int,int,QString)));
799     connect(group, SIGNAL(addEffect(QDomElement)), this , SLOT(slotAddEffect(QDomElement)));
800     connect(group, SIGNAL(unGroup(CollapsibleGroup*)), this , SLOT(slotUnGroup(CollapsibleGroup*)));
801     connect(group, SIGNAL(groupRenamed(CollapsibleGroup *)), this , SLOT(slotRenameGroup(CollapsibleGroup*)));
802     connect(group, SIGNAL(reloadEffects()), this , SIGNAL(reloadEffects()));
803     connect(group, SIGNAL(deleteGroup(QDomDocument)), this , SLOT(slotDeleteGroup(QDomDocument)));
804     connect(group, SIGNAL(changeEffectPosition(QList <int>,bool)), this , SLOT(slotMoveEffectUp(QList <int>,bool)));
805 }
806
807 void EffectStackView2::slotMoveEffect(QList <int> currentIndexes, int newIndex, int groupIndex, QString groupName)
808 {
809     if (currentIndexes.count() == 1) {
810         CollapsibleEffect *effectToMove = getEffectByIndex(currentIndexes.at(0));
811         if (effectToMove == NULL) return;
812
813         QDomElement oldeffect = effectToMove->effect();
814         QDomElement neweffect = oldeffect.cloneNode().toElement();
815
816         EffectInfo effectinfo;
817         effectinfo.fromString(oldeffect.attribute("kdenlive_info"));
818         effectinfo.groupIndex = groupIndex;
819         effectinfo.groupName = groupName;
820         neweffect.setAttribute("kdenlive_info", effectinfo.toString());
821
822         if (oldeffect.attribute("kdenlive_info") != effectinfo.toString()) {
823             // effect's group info or collapsed state changed
824             ItemInfo info;
825             if (m_effectMetaInfo.trackMode) {
826                 info.track = m_trackInfo.type;
827                 info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
828                 info.cropStart = GenTime(0);
829                 info.startPos = GenTime(-1);
830                 info.track = 0;
831                 emit updateEffect(NULL, m_trackindex, oldeffect, neweffect, effectToMove->effectIndex(),false);
832             } else {
833                 emit updateEffect(m_clipref, -1, oldeffect, neweffect, effectToMove->effectIndex(),false);
834             }
835         }
836     }
837
838     // Update effect index with new position
839     if (m_effectMetaInfo.trackMode) {
840         emit changeEffectPosition(NULL, m_trackindex, currentIndexes, newIndex);
841     }
842     else {
843         emit changeEffectPosition(m_clipref, -1, currentIndexes, newIndex);
844     }
845 }
846
847 void EffectStackView2::slotUnGroup(CollapsibleGroup* group)
848 {
849     QVBoxLayout *l = static_cast<QVBoxLayout *>(m_ui.container->widget()->layout());
850     int ix = l->indexOf(group);
851     group->removeGroup(ix, l);
852     group->deleteLater();
853 }
854
855 void EffectStackView2::slotRenameGroup(CollapsibleGroup *group)
856 {
857     QList <CollapsibleEffect*> effects = group->effects();
858     for (int i = 0; i < effects.count(); i++) {
859         QDomElement origin = effects.at(i)->effect();
860         QDomElement changed = origin.cloneNode().toElement();
861         changed.setAttribute("kdenlive_info", effects.at(i)->infoString());
862         if (m_effectMetaInfo.trackMode) {
863             emit updateEffect(NULL, m_trackindex, origin, changed, effects.at(i)->effectIndex(),false);
864         } else {
865             emit updateEffect(m_clipref, -1, origin, changed, effects.at(i)->effectIndex(),false);
866         }
867     }
868 }
869
870 void EffectStackView2::dragEnterEvent(QDragEnterEvent *event)
871 {
872     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
873         event->acceptProposedAction();
874     }
875 }
876
877 void EffectStackView2::processDroppedEffect(QDomElement e, QDropEvent *event)
878 {
879     int ix = e.attribute("kdenlive_ix").toInt();
880     if (e.tagName() == "effectgroup") {
881         // We are dropping a group, all effects in group should be moved
882         QDomNodeList effects = e.elementsByTagName("effect");
883         if (effects.count() == 0) {
884             event->ignore();
885             return;
886         }
887         EffectInfo info;
888         info.fromString(effects.at(0).toElement().attribute("kdenlive_info"));
889         if (info.groupIndex < 0) {
890             kDebug()<<"// ADDING EFFECT!!!";
891             // Adding a new group effect to the stack
892             event->setDropAction(Qt::CopyAction);
893             event->accept();
894             slotAddEffect(e);
895             return;
896         }
897         // Moving group: delete all effects and re-add them
898         QList <int> indexes;
899         for (int i = 0; i < effects.count(); i++) {
900             QDomElement effect = effects.at(i).cloneNode().toElement();
901             indexes << effect.attribute("kdenlive_ix").toInt();
902         }
903         kDebug()<<"// Moving: "<<indexes<<" TO "<<m_currentEffectList.count();
904         slotMoveEffect(indexes, m_currentEffectList.count(), info.groupIndex, info.groupName);
905     }
906     else if (ix == 0) {
907         // effect dropped from effects list, add it
908         e.setAttribute("kdenlive_ix", m_currentEffectList.count() + 1);
909         event->setDropAction(Qt::CopyAction);
910         event->accept();
911         slotAddEffect(e);
912         return;
913     }
914     else {
915         // User is moving an effect
916         slotMoveEffect(QList<int> () << ix, m_currentEffectList.count() + 1, -1);
917     }
918     event->setDropAction(Qt::MoveAction);
919     event->accept();
920 }
921
922 void EffectStackView2::dropEvent(QDropEvent *event)
923 {
924     const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
925     //event->acceptProposedAction();
926     QDomDocument doc;
927     doc.setContent(effects, true);
928     processDroppedEffect(doc.documentElement(), event);
929 }
930
931 //static
932 const QString EffectStackView2::getStyleSheet()
933 {
934     KColorScheme scheme(QApplication::palette().currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
935     QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
936     QColor hgh = KColorUtils::mix(QApplication::palette().window().color(), selected_bg, 0.2);
937     QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
938     QColor light_bg = scheme.shade(KColorScheme::LightShade);
939     QColor alt_bg = scheme.background(KColorScheme::NormalBackground).color();
940
941     QString stylesheet;
942
943     // effect background
944     stylesheet.append(QString("QFrame#decoframe {border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:2px solid palette(mid);border-top:1px solid palette(light);} QFrame#decoframe[active=\"true\"] {background: %1;}").arg(hgh.name()));
945
946     // effect in group background
947     stylesheet.append(QString("QFrame#decoframesub {border-top:1px solid palette(light);}  QFrame#decoframesub[active=\"true\"] {background: %1;}").arg(hgh.name()));
948
949     // group background
950     stylesheet.append(QString("QFrame#decoframegroup {border-top-left-radius:5px;border-top-right-radius:5px;border:2px solid palette(dark);margin:0px;margin-top:2px;} "));
951
952     // effect title bar
953     stylesheet.append(QString("QFrame#frame {margin-bottom:2px;border-top-left-radius:5px;border-top-right-radius:5px;}  QFrame#frame[target=\"true\"] {background: palette(highlight);}"));
954
955     // group effect title bar
956     stylesheet.append(QString("QFrame#framegroup {border-top-left-radius:2px;border-top-right-radius:2px;background: palette(dark);}  QFrame#framegroup[target=\"true\"] {background: palette(highlight);} "));
957
958     // draggable effect bar content
959     stylesheet.append(QString("QProgressBar::chunk:horizontal {background: palette(button);border-top-left-radius: 4px;border-bottom-left-radius: 4px;} QProgressBar::chunk:horizontal#dragOnly {background: %1;border-top-left-radius: 4px;border-bottom-left-radius: 4px;} QProgressBar::chunk:horizontal:hover {background: %2;}").arg(alt_bg.name()).arg(selected_bg.name()));
960
961     // draggable effect bar
962     stylesheet.append(QString("QProgressBar:horizontal {border: 1px solid palette(dark);border-top-left-radius: 4px;border-bottom-left-radius: 4px;border-right:0px;background:%3;padding: 0px;text-align:left center} QProgressBar:horizontal:disabled {border: 1px solid palette(button)} QProgressBar:horizontal#dragOnly {background: %3} QProgressBar:horizontal[inTimeline=\"true\"] { border: 1px solid %1;border-right: 0px;background: %2;padding: 0px;text-align:left center } QProgressBar::chunk:horizontal[inTimeline=\"true\"] {background: %1;}").arg(hover_bg.name()).arg(light_bg.name()).arg(alt_bg.name()));
963
964     // spin box for draggable widget
965     stylesheet.append(QString("QAbstractSpinBox#dragBox {border: 1px solid palette(dark);border-top-right-radius: 4px;border-bottom-right-radius: 4px;padding-right:0px;} QAbstractSpinBox::down-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox:disabled#dragBox {border: 1px solid palette(button);} QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %1;} QAbstractSpinBox:hover#dragBox {border: 1px solid %2;} ").arg(hover_bg.name()).arg(selected_bg.name()));
966
967     // group editable labels
968     stylesheet.append(QString("MyEditableLabel { background-color: transparent; color: palette(bright-text); border-radius: 2px;border: 1px solid transparent;} MyEditableLabel:hover {border: 1px solid palette(highlight);} "));
969
970     return stylesheet;
971 }
972
973 #include "effectstackview2.moc"
974