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