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