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