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