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