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