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