]> git.sesse.net Git - kdenlive/blob - src/effectstack/effectstackview2.cpp
Start work on effect groups
[kdenlive] / src / effectstack / effectstackview2.cpp
1 /***************************************************************************
2                           effecstackview.cpp2  -  description
3                              -------------------
4     begin                : Feb 15 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
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
30 #include <KDebug>
31 #include <KLocale>
32 #include <KMessageBox>
33 #include <KStandardDirs>
34 #include <KFileDialog>
35 #include <KColorScheme>
36
37 #include <QMenu>
38 #include <QTextStream>
39 #include <QFile>
40 #include <QInputDialog>
41
42
43 EffectStackView2::EffectStackView2(Monitor *monitor, QWidget *parent) :
44         QWidget(parent),
45         m_clipref(NULL),
46         m_trackindex(-1),
47         m_draggedEffect(NULL),
48         m_groupIndex(0)
49 {
50     m_effectMetaInfo.trackMode = false;
51     m_effectMetaInfo.monitor = monitor;
52
53     m_ui.setupUi(this);
54     setFont(KGlobalSettings::smallestReadableFont());
55     m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
56     m_ui.buttonShowComments->setIcon(KIcon("help-about"));
57     m_ui.buttonShowComments->setToolTip(i18n("Show additional information for the parameters"));
58
59
60     setEnabled(false);
61
62     QPalette p = palette();
63     KColorScheme scheme(p.currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
64     QColor dark_bg = scheme.shade(KColorScheme::DarkShade);
65     QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
66     QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
67     QColor light_bg = scheme.shade(KColorScheme::LightShade);
68     
69     QString stylesheet(QString("QProgressBar:horizontal {border: 1px solid %1;border-radius:0px;border-top-left-radius: 4px;border-bottom-left-radius: 4px;border-right: 0px;background:%4;padding: 0px;text-align:left center}\
70                                 QProgressBar:horizontal#dragOnly {background: %1} QProgressBar:horizontal:hover#dragOnly {background: %3} QProgressBar:horizontal:hover {border: 1px solid %3;border-right: 0px;}\
71                                 QProgressBar::chunk:horizontal {background: %1;} QProgressBar::chunk:horizontal:hover {background: %3;}\
72                                 QProgressBar:horizontal[inTimeline=\"true\"] { border: 1px solid %2;border-right: 0px;background: %4;padding: 0px;text-align:left center } QProgressBar::chunk:horizontal[inTimeline=\"true\"] {background: %2;}\
73                                 QAbstractSpinBox#dragBox {border: 1px solid %1;border-top-right-radius: 4px;border-bottom-right-radius: 4px;padding-right:0px;} QAbstractSpinBox::down-button#dragBox {width:0px;padding:0px;}\
74                                 QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %2;} QAbstractSpinBox:hover#dragBox {border: 1px solid %3;} ")
75                                 .arg(dark_bg.name()).arg(selected_bg.name()).arg(hover_bg.name()).arg(light_bg.name()));
76     setStyleSheet(stylesheet);
77 }
78
79 EffectStackView2::~EffectStackView2()
80 {
81 }
82
83
84 void EffectStackView2::slotRenderPos(int pos)
85 {
86     if (m_effects.isEmpty()) return;
87     if (!m_effectMetaInfo.trackMode && m_clipref) pos = pos - m_clipref->startPos().frames(KdenliveSettings::project_fps());
88
89     for (int i = 0; i< m_effects.count(); i++)
90         m_effects.at(i)->slotSyncEffectsPos(pos);
91 }
92
93 void EffectStackView2::setMenu(QMenu *menu)
94 {
95     //m_ui.buttonNew->setMenu(menu);
96 }
97
98 void EffectStackView2::slotClipItemSelected(ClipItem* c, int ix)
99 {
100     if (c && !c->isEnabled()) return;
101     if (c && c == m_clipref) {
102         
103     } else {
104         m_clipref = c;
105         if (c) {
106             QString cname = m_clipref->clipName();
107             if (cname.length() > 30) {
108                 m_ui.checkAll->setToolTip(i18n("Effects for %1").arg(cname));
109                 cname.truncate(27);
110                 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname) + "...");
111             } else {
112                 m_ui.checkAll->setToolTip(QString());
113                 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname));
114             }
115             ix = c->selectedEffectIndex();
116             QString size = c->baseClip()->getProperty("frame_size");
117             double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
118             m_effectMetaInfo.frameSize = QPoint((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
119         } else {
120             ix = 0;
121         }
122     }
123     if (m_clipref == NULL) {
124         //TODO: clear list, reset paramdesc and info
125         //ItemInfo info;
126         //m_effectedit->transferParamDesc(QDomElement(), info);
127         m_effects.clear();
128         QWidget *view = m_ui.container->takeWidget();
129         if (view) {
130             delete view;
131         }
132         m_ui.checkAll->setToolTip(QString());
133         m_ui.checkAll->setText(QString());
134         setEnabled(false);
135         return;
136     }
137     setEnabled(true);
138     m_effectMetaInfo.trackMode = false;
139     m_currentEffectList = m_clipref->effectList();
140     setupListView(ix);
141 }
142
143 void EffectStackView2::slotTrackItemSelected(int ix, const TrackInfo info)
144 {
145     m_clipref = NULL;
146     m_effectMetaInfo.trackMode = true;
147     m_currentEffectList = info.effectsList;
148     m_trackInfo = info;
149     setEnabled(true);
150     m_ui.checkAll->setToolTip(QString());
151     m_ui.checkAll->setText(i18n("Effects for track %1").arg(info.trackName.isEmpty() ? QString::number(ix) : info.trackName));
152     m_trackindex = ix;
153     setupListView(0);
154 }
155
156
157 void EffectStackView2::setupListView(int ix)
158 {
159     //TODO: clear list
160
161     blockSignals(true);
162     m_draggedEffect = NULL;
163     disconnect(m_effectMetaInfo.monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
164     m_effects.clear();
165     m_groupIndex = 0;
166     QWidget *view = m_ui.container->takeWidget();
167     if (view) {
168         delete view;
169         //view->deleteLater();
170     }
171     blockSignals(false);
172     view = new QWidget(m_ui.container);
173     m_ui.container->setWidget(view);
174
175     QVBoxLayout *vbox1 = new QVBoxLayout(view);
176     vbox1->setContentsMargins(0, 0, 0, 0);
177     vbox1->setSpacing(0);
178
179     for (int i = 0; i < m_currentEffectList.count(); i++) {
180         QDomElement d = m_currentEffectList.at(i).cloneNode().toElement();
181         if (d.isNull()) {
182             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
183             continue;
184         }
185         
186         CollapsibleEffect *group = NULL;
187         QString groupName = EffectsList::property(d, "kdenlive_group");
188         if (!groupName.isEmpty()) {
189             kDebug()<<"// CREATING EWFFECT GRP: "<<groupName;
190             group = new CollapsibleEffect(QDomElement(), QDomElement(), ItemInfo(), groupName.toInt(), &m_effectMetaInfo, false, true, m_ui.container->widget());
191             connect(group, SIGNAL(moveEffect(int, CollapsibleEffect*)), this , SLOT(slotMoveEffectToGroup(int, CollapsibleEffect*)));
192             vbox1->addWidget(group);
193         }
194
195         /*QDomDocument doc;
196         doc.appendChild(doc.importNode(d, true));
197         kDebug() << "IMPORTED STK: " << doc.toString();*/
198         
199         ItemInfo info;
200         if (m_effectMetaInfo.trackMode) { 
201             info.track = m_trackInfo.type;
202             info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
203             info.cropStart = GenTime(0);
204             info.startPos = GenTime(-1);
205             info.track = 0;
206         }
207         else info = m_clipref->info();
208
209         CollapsibleEffect *currentEffect = new CollapsibleEffect(d, m_currentEffectList.at(i), info, i, &m_effectMetaInfo, i == m_currentEffectList.count() - 1, false, view);
210         m_effects.append(currentEffect);
211         if (group) {
212             group->addGroupEffect(currentEffect);
213         } else {
214             vbox1->addWidget(currentEffect);
215         }
216
217         // Check drag & drop
218         currentEffect->installEventFilter( this );
219
220         connect(currentEffect, SIGNAL(parameterChanged(const QDomElement, const QDomElement, int)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement, int)));
221         connect(currentEffect, SIGNAL(startFilterJob(QString,QString,QString,QString,QString,QString)), this , SLOT(slotStartFilterJob(QString,QString,QString,QString,QString,QString)));
222         connect(currentEffect, SIGNAL(deleteEffect(const QDomElement, int)), this , SLOT(slotDeleteEffect(const QDomElement, int)));
223         connect(currentEffect, SIGNAL(reloadEffects()), this , SIGNAL(reloadEffects()));
224         connect(currentEffect, SIGNAL(resetEffect(int)), this , SLOT(slotResetEffect(int)));
225         connect(currentEffect, SIGNAL(changeEffectPosition(int,bool)), this , SLOT(slotMoveEffect(int , bool)));
226         connect(currentEffect, SIGNAL(effectStateChanged(bool, int)), this, SLOT(slotUpdateEffectState(bool, int)));
227         connect(currentEffect, SIGNAL(activateEffect(int)), this, SLOT(slotSetCurrentEffect(int)));
228         connect(currentEffect, SIGNAL(checkMonitorPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
229         connect(currentEffect, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
230         connect(currentEffect, SIGNAL(createGroup(int)), this , SLOT(slotCreateGroup(int)));
231         connect(currentEffect, SIGNAL(moveEffect(int, CollapsibleEffect*)), this , SLOT(slotMoveEffectToGroup(int, CollapsibleEffect*)));
232         
233         //ui.title->setPixmap(icon.pixmap(QSize(12, 12)));
234     }
235     vbox1->addStretch(10);
236     connect(m_effectMetaInfo.monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
237 }
238
239 bool EffectStackView2::eventFilter( QObject * o, QEvent * e ) 
240 {
241     // Check if user clicked in an effect's top bar to start dragging it
242     if (e->type() == QEvent::MouseButtonPress)  {
243         m_draggedEffect = qobject_cast<CollapsibleEffect*>(o);
244         if (m_draggedEffect) {
245             QMouseEvent *me = static_cast<QMouseEvent *>(e);
246             if (me->button() == Qt::LeftButton && (m_draggedEffect->frame->underMouse() || m_draggedEffect->title->underMouse()))
247                 m_clickPoint = me->pos();
248             else {
249                 m_clickPoint = QPoint();
250                 m_draggedEffect = NULL;
251             }
252             e->accept();
253             return false;
254         }
255     }  
256     if (e->type() == QEvent::MouseMove)  {
257         if (qobject_cast<CollapsibleEffect*>(o)) {
258             CollapsibleEffect *effect = qobject_cast<CollapsibleEffect*>(o);
259             QMouseEvent *me = static_cast<QMouseEvent *>(e);
260             if (me->buttons() != Qt::LeftButton) {
261                 e->accept();
262                 return false;
263             }
264             else {
265                 e->ignore();
266                 return true;
267             }
268         }
269     }
270     return QWidget::eventFilter(o, e);
271 }
272
273 void EffectStackView2::mouseMoveEvent(QMouseEvent * event)
274 {
275     if (m_draggedEffect && (event->buttons() & Qt::LeftButton) && (m_clickPoint != QPoint()) && ((event->pos() - m_clickPoint).manhattanLength() >= QApplication::startDragDistance()))
276         startDrag();
277 }
278
279 void EffectStackView2::mouseReleaseEvent(QMouseEvent * event)
280 {
281     m_draggedEffect = NULL;
282     QWidget::mouseReleaseEvent(event);
283 }
284
285 void EffectStackView2::startDrag()
286 {
287     QDrag *drag = new QDrag(this);
288     // The data to be transferred by the drag and drop operation is contained in a QMimeData object
289     QDomElement effect = m_draggedEffect->effect().cloneNode().toElement();
290     QPixmap pixmap = QPixmap::grabWidget(m_draggedEffect->title);
291     drag->setPixmap(pixmap);
292     QDomDocument doc;
293     doc.appendChild(doc.importNode(effect, true));
294     QMimeData *mime = new QMimeData;
295     QByteArray data;
296     data.append(doc.toString().toUtf8());
297     mime->setData("kdenlive/effectslist", data);
298
299     // Assign ownership of the QMimeData object to the QDrag object.
300     drag->setMimeData(mime);
301     // Start the drag and drop operation
302     drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
303 }
304
305
306 void EffectStackView2::slotUpdateEffectState(bool disable, int index)
307 {
308     if (m_effectMetaInfo.trackMode)
309         emit changeEffectState(NULL, m_trackindex, index, disable);
310     else
311         emit changeEffectState(m_clipref, -1, index, disable);
312 }
313
314
315 void EffectStackView2::raiseWindow(QWidget* dock)
316 {
317     if ((m_clipref || m_effectMetaInfo.trackMode) && dock)
318         dock->raise();
319 }
320
321
322 void EffectStackView2::slotSeekTimeline(int pos)
323 {
324     if (m_effectMetaInfo.trackMode) {
325         emit seekTimeline(pos);
326     } else if (m_clipref) {
327         emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
328     }
329 }
330
331
332 /*void EffectStackView2::slotRegionChanged()
333 {
334     if (!m_trackMode) emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
335 }*/
336
337 void EffectStackView2::slotCheckMonitorPosition(int renderPos)
338 {
339     if (m_effectMetaInfo.trackMode || (m_clipref && renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
340         if (!m_effectMetaInfo.monitor->getEffectEdit()->getScene()->views().at(0)->isVisible())
341             m_effectMetaInfo.monitor->slotEffectScene(true);
342     } else {
343         m_effectMetaInfo.monitor->slotEffectScene(false);
344     }
345 }
346
347 int EffectStackView2::isTrackMode(bool *ok) const
348 {
349     *ok = m_effectMetaInfo.trackMode;
350     return m_trackindex;
351 }
352
353 void EffectStackView2::clear()
354 {
355 }
356
357 void EffectStackView2::updateProjectFormat(MltVideoProfile profile, Timecode t)
358 {
359     m_effectMetaInfo.profile = profile;
360     m_effectMetaInfo.timecode = t;
361 }
362
363 void EffectStackView2::slotItemDel()
364 {
365
366 }
367
368 void EffectStackView2::updateTimecodeFormat()
369 {
370     for (int i = 0; i< m_effects.count(); i++)
371         m_effects.at(i)->updateTimecodeFormat();
372 }
373
374 CollapsibleEffect *EffectStackView2::getEffectByIndex(int ix)
375 {
376     for (int i = 0; i< m_effects.count(); i++) {
377         if (m_effects.at(i)->effectIndex() == ix) {
378             return m_effects.at(i);
379         }
380     }
381     return NULL;
382 }
383
384 void EffectStackView2::slotUpdateEffectParams(const QDomElement old, const QDomElement e, int ix)
385 {
386     if (m_effectMetaInfo.trackMode)
387         emit updateEffect(NULL, m_trackindex, old, e, ix);
388     else if (m_clipref) {
389         emit updateEffect(m_clipref, -1, old, e, ix);
390         // Make sure the changed effect is currently displayed
391         slotSetCurrentEffect(ix);
392     }
393 }
394
395 void EffectStackView2::slotSetCurrentEffect(int ix)
396 {
397     if (m_clipref && ix != m_clipref->selectedEffectIndex())
398         m_clipref->setSelectedEffect(ix);
399     for (int i = 0; i < m_effects.count(); i++) {
400         m_effects.at(i)->setActive(i == ix);
401     }
402 }
403
404 void EffectStackView2::slotDeleteEffect(const QDomElement effect, int index)
405 {
406     
407     if (m_effectMetaInfo.trackMode)
408         emit removeEffect(NULL, m_trackindex, effect);
409     else
410         emit removeEffect(m_clipref, -1, effect);
411 }
412
413 void EffectStackView2::slotMoveEffect(int index, bool up)
414 {
415     if (up && index <= 0) return;
416     if (!up && index >= m_currentEffectList.count() - 1) return;
417     int startPos;
418     int endPos;
419     if (up) {
420         startPos =  index + 1;
421         endPos = index;
422     }
423     else {
424         startPos =  index + 1;
425         endPos =  index + 2;
426     }
427     if (m_effectMetaInfo.trackMode) emit changeEffectPosition(NULL, m_trackindex, startPos, endPos);
428     else emit changeEffectPosition(m_clipref, -1, startPos, endPos);
429 }
430
431 void EffectStackView2::slotStartFilterJob(const QString&filterName, const QString&filterParams, const QString&finalFilterName, const QString&consumer, const QString&consumerParams, const QString&properties)
432 {
433     if (!m_clipref) return;
434     emit startFilterJob(m_clipref->info(), m_clipref->clipProducer(), filterName, filterParams, finalFilterName, consumer, consumerParams, properties);
435 }
436
437 void EffectStackView2::slotResetEffect(int ix)
438 {
439     QDomElement old = m_currentEffectList.at(ix);
440     QDomElement dom;
441     QString effectId = old.attribute("id");
442     QMap<QString, EffectsList*> effectLists;
443     effectLists["audio"] = &MainWindow::audioEffects;
444     effectLists["video"] = &MainWindow::videoEffects;
445     effectLists["custom"] = &MainWindow::customEffects;
446     foreach(const QString &type, effectLists.keys()) {
447         EffectsList *list = effectLists[type];
448         dom = list->getEffectByTag(QString(), effectId).cloneNode().toElement();
449         if (!dom.isNull()) break;
450     }
451     if (!dom.isNull()) {
452         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
453         if (m_effectMetaInfo.trackMode) {
454             EffectsList::setParameter(dom, "in", QString::number(0));
455             EffectsList::setParameter(dom, "out", QString::number(m_trackInfo.duration));
456             ItemInfo info;
457             info.track = m_trackInfo.type;
458             info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
459             info.cropStart = GenTime(0);
460             info.startPos = GenTime(-1);
461             info.track = 0;
462             m_effects.at(ix)->updateWidget(info, ix, dom, &m_effectMetaInfo);
463             emit updateEffect(NULL, m_trackindex, old, dom, ix);
464         } else {
465             m_clipref->initEffect(dom);
466             m_effects.at(ix)->updateWidget(m_clipref->info(), ix, dom, &m_effectMetaInfo);
467             //m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
468             emit updateEffect(m_clipref, -1, old, dom, ix);
469         }
470     }
471
472     /*emit showComments(m_ui.buttonShowComments->isChecked());
473     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || !m_ui.labelComment->text().count());*/
474 }
475
476
477 void EffectStackView2::slotCreateGroup(int ix)
478 {
479     QDomElement oldeffect = m_currentEffectList.at(ix);
480     QDomElement neweffect = oldeffect.cloneNode().toElement();
481     QString groupName = QString::number(m_groupIndex);
482     m_groupIndex++;
483     EffectsList::setProperty(neweffect, "kdenlive_group", groupName);
484
485     ItemInfo info;
486     if (m_effectMetaInfo.trackMode) { 
487         info.track = m_trackInfo.type;
488         info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
489         info.cropStart = GenTime(0);
490         info.startPos = GenTime(-1);
491         info.track = 0;
492         emit updateEffect(NULL, m_trackindex, oldeffect, neweffect, ix);
493     } else {
494         emit updateEffect(m_clipref, -1, oldeffect, neweffect, ix);
495     }
496     
497     QVBoxLayout *l = static_cast<QVBoxLayout *>(m_ui.container->widget()->layout());
498     int groupPos = l->indexOf(m_effects.at(ix));
499     
500     CollapsibleEffect *group = new CollapsibleEffect(QDomElement(), QDomElement(), ItemInfo(), m_groupIndex, &m_effectMetaInfo, false, true, m_ui.container->widget());
501     connect(group, SIGNAL(moveEffect(int, CollapsibleEffect*)), this , SLOT(slotMoveEffectToGroup(int, CollapsibleEffect*)));
502     CollapsibleEffect *w = static_cast<CollapsibleEffect*>(l->takeAt(groupPos)->widget());
503     l->insertWidget(groupPos, group);
504     group->addGroupEffect(w);
505 }
506
507 void EffectStackView2::slotMoveEffectToGroup(int ix, CollapsibleEffect* group)
508 {
509     QVBoxLayout *l = static_cast<QVBoxLayout *>(m_ui.container->widget()->layout());
510     CollapsibleEffect *effectToMove = getEffectByIndex(ix);
511     if (effectToMove == NULL) return;
512     l->removeWidget(effectToMove);
513     group->addGroupEffect(effectToMove);
514 }
515
516 #include "effectstackview2.moc"