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