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