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