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