]> git.sesse.net Git - kdenlive/blob - src/effectstack/effectstackview2.cpp
Fix track effect checkbox always disabled
[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").arg(cname));
107                 cname.truncate(27);
108                 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname) + "...");
109             } else {
110                 m_ui.checkAll->setToolTip(QString());
111                 m_ui.checkAll->setText(i18n("Effects for %1").arg(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").arg(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,QString)), this , SLOT(slotStartFilterJob(QString,QString,QString,QString,QString,QString)));
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 }
288
289 void EffectStackView2::slotCheckWheelEventFilter()
290 {
291     // If the effect stack widget has no scrollbar, we will not filter the
292     // mouse wheel events, so that user can easily adjust effect params
293     bool filterWheelEvent = false;
294     if (m_ui.container->verticalScrollBar() && m_ui.container->verticalScrollBar()->isVisible()) {
295         // widget has scroll bar,
296         filterWheelEvent = true;
297     }
298     for (int i = 0; i < m_effects.count(); i++) {
299         m_effects.at(i)->filterWheelEvent = filterWheelEvent;
300     }
301 }
302
303 void EffectStackView2::resizeEvent ( QResizeEvent * event )
304 {
305     slotCheckWheelEventFilter();
306     QWidget::resizeEvent(event);
307 }
308
309 bool EffectStackView2::eventFilter( QObject * o, QEvent * e )
310 {
311     // Check if user clicked in an effect's top bar to start dragging it
312     if (e->type() == QEvent::MouseButtonPress)  {
313         m_draggedEffect = qobject_cast<CollapsibleEffect*>(o);
314         if (m_draggedEffect) {
315             QMouseEvent *me = static_cast<QMouseEvent *>(e);
316             if (me->button() == Qt::LeftButton && (m_draggedEffect->frame->underMouse() || m_draggedEffect->title->underMouse())) {
317                 m_clickPoint = me->globalPos();
318             }
319             else {
320                 m_clickPoint = QPoint();
321                 m_draggedEffect = NULL;
322             }
323             e->accept();
324             return true;
325         }
326         m_draggedGroup = qobject_cast<CollapsibleGroup*>(o);
327         if (m_draggedGroup) {
328             QMouseEvent *me = static_cast<QMouseEvent *>(e);
329             if (me->button() == Qt::LeftButton && (m_draggedGroup->frame->underMouse() || m_draggedGroup->title()->underMouse()))
330                 m_clickPoint = me->globalPos();
331             else {
332                 m_clickPoint = QPoint();
333                 m_draggedGroup = NULL;
334             }
335             e->accept();
336             return true;
337         }
338     }
339     /*if (e->type() == QEvent::MouseMove)  {
340     if (qobject_cast<CollapsibleEffect*>(o)) {
341         QMouseEvent *me = static_cast<QMouseEvent *>(e);
342         if (me->buttons() != Qt::LeftButton) {
343         e->accept();
344         return false;
345         }
346         else {
347         e->ignore();
348         return true;
349         }
350     }
351     }*/
352     return QWidget::eventFilter(o, e);
353 }
354
355 void EffectStackView2::mouseMoveEvent(QMouseEvent * event)
356 {
357     if (m_draggedEffect || m_draggedGroup) {
358         if ((event->buttons() & Qt::LeftButton) && (m_clickPoint != QPoint()) && ((event->globalPos() - m_clickPoint).manhattanLength() >= QApplication::startDragDistance())) {
359             startDrag();
360         }
361     }
362 }
363
364 void EffectStackView2::mouseReleaseEvent(QMouseEvent * event)
365 {
366     m_draggedEffect = NULL;
367     m_draggedGroup = NULL;
368     QWidget::mouseReleaseEvent(event);
369 }
370
371 void EffectStackView2::startDrag()
372 {
373     // The data to be transferred by the drag and drop operation is contained in a QMimeData object
374     QDomDocument doc;
375     QPixmap pixmap;
376     if (m_draggedEffect) {
377         QDomElement effect = m_draggedEffect->effect().cloneNode().toElement();
378         doc.appendChild(doc.importNode(effect, true));
379         pixmap = QPixmap::grabWidget(m_draggedEffect->title);
380     }
381     else if (m_draggedGroup) {
382         doc = m_draggedGroup->effectsData();
383         pixmap = QPixmap::grabWidget(m_draggedGroup->title());
384     }
385     else return;
386     QDrag *drag = new QDrag(this);
387     drag->setPixmap(pixmap);
388     QMimeData *mime = new QMimeData;
389     QByteArray data;
390     data.append(doc.toString().toUtf8());
391     mime->setData("kdenlive/effectslist", data);
392
393     // Assign ownership of the QMimeData object to the QDrag object.
394     drag->setMimeData(mime);
395     // Start the drag and drop operation
396     drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
397 }
398
399
400 void EffectStackView2::slotUpdateEffectState(bool disable, int index, bool needsMonitorEffectScene)
401 {
402     if (m_monitorSceneWanted && disable) {
403         m_effectMetaInfo.monitor->slotShowEffectScene(false);
404         m_monitorSceneWanted = false;
405     }
406     else if (!disable && !m_monitorSceneWanted && needsMonitorEffectScene) {
407         m_effectMetaInfo.monitor->slotShowEffectScene(true);
408         m_monitorSceneWanted = true;
409     }
410     if (m_effectMetaInfo.trackMode)
411         emit changeEffectState(NULL, m_trackindex, QList <int>() << index, disable);
412     else
413         emit changeEffectState(m_clipref, -1, QList <int>() <<index, disable);
414     slotUpdateCheckAllButton();
415 }
416
417
418 void EffectStackView2::raiseWindow(QWidget* dock)
419 {
420     if ((m_clipref || m_effectMetaInfo.trackMode) && dock)
421         dock->raise();
422 }
423
424
425 void EffectStackView2::slotSeekTimeline(int pos)
426 {
427     if (m_effectMetaInfo.trackMode) {
428         emit seekTimeline(pos);
429     } else if (m_clipref) {
430         emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
431     }
432 }
433
434
435 /*void EffectStackView2::slotRegionChanged()
436 {
437     if (!m_trackMode) emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
438 }*/
439
440 void EffectStackView2::slotCheckMonitorPosition(int renderPos)
441 {
442     if (m_monitorSceneWanted) {
443         if (m_effectMetaInfo.trackMode || (m_clipref && renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
444             if (!m_effectMetaInfo.monitor->effectSceneDisplayed()) {
445                 m_effectMetaInfo.monitor->slotShowEffectScene(true);
446             }
447         } else {
448             m_effectMetaInfo.monitor->slotShowEffectScene(false);
449         }
450     }
451     else {
452         m_effectMetaInfo.monitor->slotShowEffectScene(false);
453     }
454 }
455
456 int EffectStackView2::isTrackMode(bool *ok) const
457 {
458     *ok = m_effectMetaInfo.trackMode;
459     return m_trackindex;
460 }
461
462 void EffectStackView2::clear()
463 {
464     m_effects.clear();
465     m_monitorSceneWanted = false;
466     QWidget *view = m_ui.container->takeWidget();
467     if (view) {
468         delete view;
469     }
470     m_ui.checkAll->setToolTip(QString());
471     m_ui.checkAll->setText(QString());
472     m_ui.checkAll->setEnabled(false);
473     m_ui.labelComment->setText(QString());
474     setEnabled(false);
475 }
476
477 void EffectStackView2::slotCheckAll(int state)
478 {
479     if (state == Qt::PartiallyChecked) {
480         state = Qt::Checked;
481         m_ui.checkAll->blockSignals(true);
482         m_ui.checkAll->setCheckState(Qt::Checked);
483         m_ui.checkAll->blockSignals(false);
484     }
485
486     bool disabled = state == Qt::Unchecked;
487     // Disable all effects
488     QList <int> indexes;
489     for (int i = 0; i < m_effects.count(); i++) {
490         m_effects.at(i)->slotEnable(disabled, false);
491         indexes << m_effects.at(i)->effectIndex();
492     }
493     // Take care of groups
494     QList<CollapsibleGroup *> allGroups = m_ui.container->widget()->findChildren<CollapsibleGroup *>();
495     for (int i = 0; i < allGroups.count(); i++) {
496         allGroups.at(i)->slotEnable(disabled, false);
497     }
498
499     if (m_effectMetaInfo.trackMode)
500         emit changeEffectState(NULL, m_trackindex, indexes, disabled);
501     else
502         emit changeEffectState(m_clipref, -1, indexes, disabled);
503 }
504
505 void EffectStackView2::slotUpdateCheckAllButton()
506 {
507     bool hasEnabled = false;
508     bool hasDisabled = false;
509
510     for (int i = 0; i < m_effects.count(); i++) {
511         if (!m_effects.at(i)->enabledButton->isChecked()) hasEnabled = true;
512         else hasDisabled = true;
513     }
514
515     m_ui.checkAll->blockSignals(true);
516     if (hasEnabled && hasDisabled)
517         m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
518     else if (hasEnabled)
519         m_ui.checkAll->setCheckState(Qt::Checked);
520     else
521         m_ui.checkAll->setCheckState(Qt::Unchecked);
522     m_ui.checkAll->blockSignals(false);
523 }
524
525 void EffectStackView2::deleteCurrentEffect()
526 {
527     for (int i = 0; i < m_effects.count(); i++) {
528         if (m_effects.at(i)->isActive()) {
529             slotDeleteEffect(m_effects.at(i)->effect());
530             break;
531         }
532     }
533 }
534
535 void EffectStackView2::updateProjectFormat(MltVideoProfile profile, Timecode t)
536 {
537     m_effectMetaInfo.profile = profile;
538     m_effectMetaInfo.timecode = t;
539 }
540
541 void EffectStackView2::updateTimecodeFormat()
542 {
543     for (int i = 0; i< m_effects.count(); i++)
544         m_effects.at(i)->updateTimecodeFormat();
545 }
546
547 CollapsibleEffect *EffectStackView2::getEffectByIndex(int ix)
548 {
549     for (int i = 0; i< m_effects.count(); i++) {
550         if (m_effects.at(i)->effectIndex() == ix) {
551             return m_effects.at(i);
552         }
553     }
554     return NULL;
555 }
556
557 void EffectStackView2::slotUpdateEffectParams(const QDomElement old, const QDomElement e, int ix)
558 {
559     if (m_effectMetaInfo.trackMode)
560         emit updateEffect(NULL, m_trackindex, old, e, ix,false);
561     else if (m_clipref) {
562         emit updateEffect(m_clipref, -1, old, e, ix, false);
563         // Make sure the changed effect is currently displayed
564         slotSetCurrentEffect(ix);
565     }
566     QTimer::singleShot(200, this, SLOT(slotCheckWheelEventFilter()));
567 }
568
569 void EffectStackView2::slotSetCurrentEffect(int ix)
570 {
571     if (m_clipref && ix != m_clipref->selectedEffectIndex()) {
572         m_clipref->setSelectedEffect(ix);
573         for (int i = 0; i < m_effects.count(); i++) {
574             if (m_effects.at(i)->effectIndex() == ix) {
575                 if (m_effects.at(i)->isActive()) return;
576                 m_effects.at(i)->setActive(true);
577                 m_monitorSceneWanted = m_effects.at(i)->needsMonitorEffectScene();
578                 slotCheckMonitorPosition(m_effectMetaInfo.monitor->render->seekFramePosition());
579                 m_ui.labelComment->setText(i18n(m_effects.at(i)->effect().firstChildElement("description").firstChildElement("full").text().toUtf8().data()));
580                 m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || m_ui.labelComment->text().isEmpty());
581             }
582             else m_effects.at(i)->setActive(false);
583         }
584     }
585 }
586
587 void EffectStackView2::slotDeleteGroup(QDomDocument doc)
588 {
589     QDomNodeList effects = doc.elementsByTagName("effect");
590     ClipItem * clip = NULL;
591     int ix;
592     if (m_effectMetaInfo.trackMode) {
593         ix = m_trackindex;
594     }
595     else {
596         clip = m_clipref;
597         ix = -1;
598     }
599
600     for (int i = 0; i < effects.count(); i++)
601         emit removeEffect(clip, ix, effects.at(i).toElement());
602 }
603
604 void EffectStackView2::slotDeleteEffect(const QDomElement effect)
605 {
606     if (m_effectMetaInfo.trackMode)
607         emit removeEffect(NULL, m_trackindex, effect);
608     else
609         emit removeEffect(m_clipref, -1, effect);
610 }
611
612 void EffectStackView2::slotAddEffect(QDomElement effect)
613 {
614     emit addEffect(m_clipref, effect);
615 }
616
617 void EffectStackView2::slotMoveEffectUp(QList <int> indexes, bool up)
618 {
619     if (up && indexes.first() <= 1) return;
620     if (!up && indexes.last() >= m_currentEffectList.count()) return;
621     int endPos;
622     if (up) {
623         endPos = indexes.first() - 1;
624     }
625     else {
626         endPos =  indexes.last() + 1;
627     }
628     if (m_effectMetaInfo.trackMode) emit changeEffectPosition(NULL, m_trackindex, indexes, endPos);
629     else emit changeEffectPosition(m_clipref, -1, indexes, endPos);
630 }
631
632 void EffectStackView2::slotStartFilterJob(const QString&filterName, const QString&filterParams, const QString&finalFilterName, const QString&consumer, const QString&consumerParams, const QString&properties)
633 {
634     if (!m_clipref) return;
635     emit startFilterJob(m_clipref->info(), m_clipref->clipProducer(), filterName, filterParams, finalFilterName, consumer, consumerParams, properties);
636 }
637
638 void EffectStackView2::slotResetEffect(int ix)
639 {
640     QDomElement old = m_currentEffectList.itemFromIndex(ix);
641     QDomElement dom;
642     QString effectId = old.attribute("id");
643     QMap<QString, EffectsList*> effectLists;
644     effectLists["audio"] = &MainWindow::audioEffects;
645     effectLists["video"] = &MainWindow::videoEffects;
646     effectLists["custom"] = &MainWindow::customEffects;
647     foreach(const QString &type, effectLists.keys()) {
648         EffectsList *list = effectLists[type];
649         dom = list->getEffectByTag(QString(), effectId).cloneNode().toElement();
650         if (!dom.isNull()) break;
651     }
652     if (!dom.isNull()) {
653         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
654         if (m_effectMetaInfo.trackMode) {
655             EffectsList::setParameter(dom, "in", QString::number(0));
656             EffectsList::setParameter(dom, "out", QString::number(m_trackInfo.duration));
657             ItemInfo info;
658             info.track = m_trackInfo.type;
659             info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
660             info.cropStart = GenTime(0);
661             info.startPos = GenTime(-1);
662             info.track = 0;
663             for (int i = 0; i < m_effects.count(); i++) {
664                 if (m_effects.at(i)->effectIndex() == ix) {
665                     m_effects.at(i)->updateWidget(info, dom, &m_effectMetaInfo);
666                     break;
667                 }
668             }
669             emit updateEffect(NULL, m_trackindex, old, dom, ix,false);
670         } else {
671             m_clipref->initEffect(dom);
672             for (int i = 0; i < m_effects.count(); i++) {
673                 if (m_effects.at(i)->effectIndex() == ix) {
674                     m_effects.at(i)->updateWidget(m_clipref->info(), dom, &m_effectMetaInfo);
675                     break;
676                 }
677             }
678             //m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
679             emit updateEffect(m_clipref, -1, old, dom, ix,false);
680         }
681     }
682
683     emit showComments(m_ui.buttonShowComments->isChecked());
684     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || m_ui.labelComment->text().isEmpty());
685 }
686
687 void EffectStackView2::slotShowComments()
688 {
689     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || m_ui.labelComment->text().isEmpty());
690     emit showComments(m_ui.buttonShowComments->isChecked());
691 }
692
693 void EffectStackView2::slotCreateRegion(int ix, KUrl url)
694 {
695     QDomElement oldeffect = m_currentEffectList.itemFromIndex(ix);
696     QDomElement neweffect = oldeffect.cloneNode().toElement();
697     QDomElement region = MainWindow::videoEffects.getEffectByTag("region", "region").cloneNode().toElement();
698     region.appendChild(region.ownerDocument().importNode(neweffect, true));
699     region.setAttribute("kdenlive_ix", ix);
700     EffectsList::setParameter(region, "resource", url.path());
701     if (m_effectMetaInfo.trackMode)
702         emit updateEffect(NULL, m_trackindex, oldeffect, region, ix,false);
703     else if (m_clipref) {
704         emit updateEffect(m_clipref, -1, oldeffect, region, ix, false);
705         // Make sure the changed effect is currently displayed
706         //slotSetCurrentEffect(ix);
707     }
708     // refresh effect stack
709     ItemInfo info;
710     bool isSelected = false;
711     if (m_effectMetaInfo.trackMode) {
712         info.track = m_trackInfo.type;
713         info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
714         info.cropStart = GenTime(0);
715         info.startPos = GenTime(-1);
716         info.track = 0;
717     }
718     else {
719         info = m_clipref->info();
720     }
721     CollapsibleEffect *current = getEffectByIndex(ix);
722     m_effects.removeAll(current);
723     current->setEnabled(false);
724     m_currentEffectList.removeAt(ix);
725     m_currentEffectList.insert(region);
726     current->deleteLater();
727     CollapsibleEffect *currentEffect = new CollapsibleEffect(region, m_currentEffectList.itemFromIndex(ix), info, &m_effectMetaInfo, ix == m_currentEffectList.count() - 1, m_ui.container->widget());
728     connectEffect(currentEffect);
729
730     if (m_effectMetaInfo.trackMode) {
731         isSelected = currentEffect->effectIndex() == 1;
732     }
733     else {
734         isSelected = currentEffect->effectIndex() == m_clipref->selectedEffectIndex();
735     }
736     if (isSelected) currentEffect->setActive(true);
737     m_effects.append(currentEffect);
738     // TODO: region in group?
739     //if (group) {
740     //  group->addGroupEffect(currentEffect);
741     //} else {
742     QVBoxLayout *vbox = static_cast <QVBoxLayout *> (m_ui.container->widget()->layout());
743     vbox->insertWidget(ix, currentEffect);
744     //}
745
746     // Check drag & drop
747     currentEffect->installEventFilter( this );
748
749     QTimer::singleShot(200, this, SLOT(slotCheckWheelEventFilter()));
750
751 }
752
753 void EffectStackView2::slotCreateGroup(int ix)
754 {
755     QDomElement oldeffect = m_currentEffectList.itemFromIndex(ix);
756     QDomElement neweffect = oldeffect.cloneNode().toElement();
757     EffectInfo effectinfo;
758     effectinfo.fromString(oldeffect.attribute("kdenlive_info"));
759     effectinfo.groupIndex = m_groupIndex;
760     neweffect.setAttribute("kdenlive_info", effectinfo.toString());
761
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, ix, false);
770     } else {
771         emit updateEffect(m_clipref, -1, oldeffect, neweffect, ix, false);
772     }
773
774     QVBoxLayout *l = static_cast<QVBoxLayout *>(m_ui.container->widget()->layout());
775     int groupPos = 0;
776     CollapsibleEffect *effectToMove = NULL;
777     for (int i = 0; i < m_effects.count(); i++) {
778         if (m_effects.at(i)->effectIndex() == ix) {
779             effectToMove = m_effects.at(i);
780             groupPos = l->indexOf(effectToMove);
781             l->removeWidget(effectToMove);
782             break;
783         }
784     }
785
786     CollapsibleGroup *group = new CollapsibleGroup(m_groupIndex, ix == 1, ix == m_currentEffectList.count() - 2, effectinfo, m_ui.container->widget());
787     m_groupIndex++;
788     connectGroup(group);
789     l->insertWidget(groupPos, group);
790     group->installEventFilter( this );
791     group->addGroupEffect(effectToMove);
792 }
793
794 void EffectStackView2::connectGroup(CollapsibleGroup *group)
795 {
796     connect(group, SIGNAL(moveEffect(QList<int>,int,int,QString)), this , SLOT(slotMoveEffect(QList<int>,int,int,QString)));
797     connect(group, SIGNAL(addEffect(QDomElement)), this , SLOT(slotAddEffect(QDomElement)));
798     connect(group, SIGNAL(unGroup(CollapsibleGroup*)), this , SLOT(slotUnGroup(CollapsibleGroup*)));
799     connect(group, SIGNAL(groupRenamed(CollapsibleGroup *)), this , SLOT(slotRenameGroup(CollapsibleGroup*)));
800     connect(group, SIGNAL(reloadEffects()), this , SIGNAL(reloadEffects()));
801     connect(group, SIGNAL(deleteGroup(QDomDocument)), this , SLOT(slotDeleteGroup(QDomDocument)));
802     connect(group, SIGNAL(changeEffectPosition(QList <int>,bool)), this , SLOT(slotMoveEffectUp(QList <int>,bool)));
803 }
804
805 void EffectStackView2::slotMoveEffect(QList <int> currentIndexes, int newIndex, int groupIndex, QString groupName)
806 {
807     if (currentIndexes.count() == 1) {
808         CollapsibleEffect *effectToMove = getEffectByIndex(currentIndexes.at(0));
809         if (effectToMove == NULL) return;
810
811         QDomElement oldeffect = effectToMove->effect();
812         QDomElement neweffect = oldeffect.cloneNode().toElement();
813
814         EffectInfo effectinfo;
815         effectinfo.fromString(oldeffect.attribute("kdenlive_info"));
816         effectinfo.groupIndex = groupIndex;
817         effectinfo.groupName = groupName;
818         neweffect.setAttribute("kdenlive_info", effectinfo.toString());
819
820         if (oldeffect.attribute("kdenlive_info") != effectinfo.toString()) {
821             // effect's group info or collapsed state changed
822             ItemInfo info;
823             if (m_effectMetaInfo.trackMode) {
824                 info.track = m_trackInfo.type;
825                 info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
826                 info.cropStart = GenTime(0);
827                 info.startPos = GenTime(-1);
828                 info.track = 0;
829                 emit updateEffect(NULL, m_trackindex, oldeffect, neweffect, effectToMove->effectIndex(),false);
830             } else {
831                 emit updateEffect(m_clipref, -1, oldeffect, neweffect, effectToMove->effectIndex(),false);
832             }
833         }
834     }
835
836     // Update effect index with new position
837     if (m_effectMetaInfo.trackMode) {
838         emit changeEffectPosition(NULL, m_trackindex, currentIndexes, newIndex);
839     }
840     else {
841         emit changeEffectPosition(m_clipref, -1, currentIndexes, newIndex);
842     }
843 }
844
845 void EffectStackView2::slotUnGroup(CollapsibleGroup* group)
846 {
847     QVBoxLayout *l = static_cast<QVBoxLayout *>(m_ui.container->widget()->layout());
848     int ix = l->indexOf(group);
849     group->removeGroup(ix, l);
850     group->deleteLater();
851 }
852
853 void EffectStackView2::slotRenameGroup(CollapsibleGroup *group)
854 {
855     QList <CollapsibleEffect*> effects = group->effects();
856     for (int i = 0; i < effects.count(); i++) {
857         QDomElement origin = effects.at(i)->effect();
858         QDomElement changed = origin.cloneNode().toElement();
859         changed.setAttribute("kdenlive_info", effects.at(i)->infoString());
860         if (m_effectMetaInfo.trackMode) {
861             emit updateEffect(NULL, m_trackindex, origin, changed, effects.at(i)->effectIndex(),false);
862         } else {
863             emit updateEffect(m_clipref, -1, origin, changed, effects.at(i)->effectIndex(),false);
864         }
865     }
866 }
867
868 void EffectStackView2::dragEnterEvent(QDragEnterEvent *event)
869 {
870     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
871         event->acceptProposedAction();
872     }
873 }
874
875 void EffectStackView2::processDroppedEffect(QDomElement e, QDropEvent *event)
876 {
877     int ix = e.attribute("kdenlive_ix").toInt();
878     if (e.tagName() == "effectgroup") {
879         // We are dropping a group, all effects in group should be moved
880         QDomNodeList effects = e.elementsByTagName("effect");
881         if (effects.count() == 0) {
882             event->ignore();
883             return;
884         }
885         EffectInfo info;
886         info.fromString(effects.at(0).toElement().attribute("kdenlive_info"));
887         if (info.groupIndex < 0) {
888             kDebug()<<"// ADDING EFFECT!!!";
889             // Adding a new group effect to the stack
890             event->setDropAction(Qt::CopyAction);
891             event->accept();
892             slotAddEffect(e);
893             return;
894         }
895         // Moving group: delete all effects and re-add them
896         QList <int> indexes;
897         for (int i = 0; i < effects.count(); i++) {
898             QDomElement effect = effects.at(i).cloneNode().toElement();
899             indexes << effect.attribute("kdenlive_ix").toInt();
900         }
901         kDebug()<<"// Moving: "<<indexes<<" TO "<<m_currentEffectList.count();
902         slotMoveEffect(indexes, m_currentEffectList.count(), info.groupIndex, info.groupName);
903     }
904     else if (ix == 0) {
905         // effect dropped from effects list, add it
906         e.setAttribute("kdenlive_ix", m_currentEffectList.count() + 1);
907         event->setDropAction(Qt::CopyAction);
908         event->accept();
909         slotAddEffect(e);
910         return;
911     }
912     else {
913         // User is moving an effect
914         slotMoveEffect(QList<int> () << ix, m_currentEffectList.count() + 1, -1);
915     }
916     event->setDropAction(Qt::MoveAction);
917     event->accept();
918 }
919
920 void EffectStackView2::dropEvent(QDropEvent *event)
921 {
922     const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
923     //event->acceptProposedAction();
924     QDomDocument doc;
925     doc.setContent(effects, true);
926     processDroppedEffect(doc.documentElement(), event);
927 }
928
929 //static
930 const QString EffectStackView2::getStyleSheet()
931 {
932     KColorScheme scheme(QApplication::palette().currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
933     QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
934     QColor hgh = KColorUtils::mix(QApplication::palette().window().color(), selected_bg, 0.2);
935     QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
936     QColor light_bg = scheme.shade(KColorScheme::LightShade);
937     QColor alt_bg = scheme.background(KColorScheme::NormalBackground).color();
938
939     QString stylesheet;
940
941     // effect background
942     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()));
943
944     // effect in group background
945     stylesheet.append(QString("QFrame#decoframesub {border-top:1px solid palette(light);}  QFrame#decoframesub[active=\"true\"] {background: %1;}").arg(hgh.name()));
946
947     // group background
948     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;} "));
949
950     // effect title bar
951     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);}"));
952
953     // group effect title bar
954     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);} "));
955
956     // draggable effect bar content
957     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()));
958
959     // draggable effect bar
960     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()));
961
962     // spin box for draggable widget
963     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()));
964
965     // group editable labels
966     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);} "));
967
968     return stylesheet;
969 }
970
971 #include "effectstackview2.moc"
972