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