]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
Improve keyframe editor, fix random keyframe sometimes inserted
[kdenlive] / src / effectstackedit.cpp
1 /***************************************************************************
2                           effecstackview.cpp  -  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 #include "effectstackedit.h"
19 #include "ui_constval_ui.h"
20 #include "ui_listval_ui.h"
21 #include "ui_boolval_ui.h"
22 #include "ui_colorval_ui.h"
23 #include "ui_wipeval_ui.h"
24 #include "ui_keyframeeditor_ui.h"
25 #include "complexparameter.h"
26 #include "geometryval.h"
27 #include "keyframeedit.h"
28 #include "positionedit.h"
29 #include "effectslist.h"
30 #include "kdenlivesettings.h"
31 #include "profilesdialog.h"
32
33 #include <KDebug>
34 #include <KLocale>
35
36 #include <QVBoxLayout>
37 #include <QSlider>
38 #include <QLabel>
39 #include <QPushButton>
40 #include <QCheckBox>
41 #include <QScrollArea>
42
43
44 class Boolval: public QWidget, public Ui::Boolval_UI
45 {
46 };
47
48 class Colorval: public QWidget, public Ui::Colorval_UI
49 {
50 };
51
52 class Constval: public QWidget, public Ui::Constval_UI
53 {
54 };
55
56 class Listval: public QWidget, public Ui::Listval_UI
57 {
58 };
59
60 class Wipeval: public QWidget, public Ui::Wipeval_UI
61 {
62 };
63
64
65 QMap<QString, QImage> EffectStackEdit::iconCache;
66
67 EffectStackEdit::EffectStackEdit(QWidget *parent) :
68         QScrollArea(parent),
69         m_in(0),
70         m_out(0),
71         m_frameSize(QPoint())
72 {
73     m_baseWidget = new QWidget(this);
74     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
75     setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
76     setFrameStyle(QFrame::NoFrame);
77     setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
78
79     setWidget(m_baseWidget);
80     setWidgetResizable(true);
81     m_vbox = new QVBoxLayout(m_baseWidget);
82     m_vbox->setContentsMargins(0, 0, 0, 0);
83     m_vbox->setSpacing(0);
84     //wid->show();
85 }
86
87 EffectStackEdit::~EffectStackEdit()
88 {
89     iconCache.clear();
90 }
91
92 void EffectStackEdit::setFrameSize(QPoint p)
93 {
94     m_frameSize = p;
95     QDomNodeList namenode = m_params.elementsByTagName("parameter");
96     for (int i = 0; i < namenode.count() ; i++) {
97         QDomNode pa = namenode.item(i);
98         QDomNode na = pa.firstChildElement("name");
99         QString type = pa.attributes().namedItem("type").nodeValue();
100         QString paramName = i18n(na.toElement().text().toUtf8().data());
101
102         if (type == "geometry") {
103             Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
104             geom->setFrameSize(m_frameSize);
105             break;
106         }
107     }
108
109 }
110
111 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
112 {
113     m_profile = profile;
114     m_timecode = t;
115 }
116
117 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
118 {
119     m_params.setAttribute(name, value);
120 }
121
122 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
123 {
124     clearAllItems();
125     m_params = d;
126     m_in = in;
127     m_out = out;
128     if (m_params.isNull()) {
129         kDebug() << "// EMPTY EFFECT STACK";
130         return;
131     }
132
133     /*QDomDocument doc;
134     doc.appendChild(doc.importNode(m_params, true));
135     kDebug() << "IMPORTED TRANS: " << doc.toString();*/
136
137     QDomNodeList namenode = m_params.elementsByTagName("parameter");
138     QDomElement e = m_params.toElement();
139     const int minFrame = e.attribute("start").toInt();
140     const int maxFrame = e.attribute("end").toInt();
141
142
143     for (int i = 0; i < namenode.count() ; i++) {
144         QDomElement pa = namenode.item(i).toElement();
145         QDomNode na = pa.firstChildElement("name");
146         QString type = pa.attribute("type");
147         QString paramName = i18n(na.toElement().text().toUtf8().data());
148         QWidget * toFillin = new QWidget(m_baseWidget);
149         QString value = pa.attribute("value").isNull() ?
150                         pa.attribute("default") : pa.attribute("value");
151
152         /** Currently supported parameter types are:
153             * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
154             * list: a combobox containing a list of values to choose
155             * bool: a checkbox
156             * complex: designed for keyframe parameters, but old and not finished, do not use
157             * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
158             * keyframe: a list widget with a list of entries (position and value)
159             * color: a color chooser button
160             * position: a slider representing the position of a frame in the current clip
161             * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
162         */
163
164         if (type == "double" || type == "constant") {
165             int min;
166             int max;
167             if (pa.attribute("min").startsWith('%')) {
168                 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
169             } else min = pa.attribute("min").toInt();
170             if (pa.attribute("max").startsWith('%')) {
171                 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
172             } else max = pa.attribute("max").toInt();
173             createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
174             delete toFillin;
175             toFillin = NULL;
176         } else if (type == "list") {
177             Listval *lsval = new Listval;
178             lsval->setupUi(toFillin);
179             QStringList listitems = pa.attribute("paramlist").split(',');
180             QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
181             if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
182             //lsval->list->addItems(listitems);
183             lsval->list->setIconSize(QSize(30, 30));
184             for (int i = 0; i < listitems.count(); i++) {
185                 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
186                 QString entry = listitems.at(i);
187                 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
188                     if (!EffectStackEdit::iconCache.contains(entry)) {
189                         QImage pix(entry);
190                         EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
191                     }
192                     lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
193                 }
194             }
195             if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
196             lsval->title->setTitle(paramName);
197             m_valueItems[paramName] = lsval;
198             connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
199             m_uiItems.append(lsval);
200         } else if (type == "bool") {
201             Boolval *bval = new Boolval;
202             bval->setupUi(toFillin);
203             bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
204             bval->checkBox->setText(paramName);
205             m_valueItems[paramName] = bval;
206             connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
207             m_uiItems.append(bval);
208         } else if (type == "complex") {
209             /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
210             QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
211             QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
212             QStringList val=value.split(';');
213             kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size()  ;
214             if ( (names.size() == max.size() ) &&
215                  (names.size()== min.size()) &&
216                  (names.size()== val.size()) )
217             {
218              for (int i=0;i< names.size();i++){
219               createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
220              };
221             }*/
222             ComplexParameter *pl = new ComplexParameter;
223             pl->setupParam(d, pa.attribute("name"), 0, 100);
224             m_vbox->addWidget(pl);
225             m_valueItems[paramName+"complex"] = pl;
226             connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
227         } else if (type == "geometry") {
228             Geometryval *geo = new Geometryval(m_profile, m_frameSize);
229             geo->setupParam(pa, minFrame, maxFrame);
230             m_vbox->addWidget(geo);
231             m_valueItems[paramName+"geometry"] = geo;
232             connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
233             connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
234         } else if (type == "keyframe" || type == "simplekeyframe") {
235             // keyframe editor widget
236             kDebug() << "min: " << m_in << ", MAX: " << m_out;
237             KeyframeEdit *geo = new KeyframeEdit(pa, m_out - m_in - 1, pa.attribute("min").toInt(), pa.attribute("max").toInt(), m_timecode, paramName);
238             //geo->setupParam(100, pa.attribute("min").toInt(), pa.attribute("max").toInt(), pa.attribute("keyframes"));
239             //connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
240             //geo->setupParam(pa, minFrame, maxFrame);
241             m_vbox->addWidget(geo);
242             m_valueItems[paramName+"keyframe"] = geo;
243             connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
244         } else if (type == "color") {
245             Colorval *cval = new Colorval;
246             cval->setupUi(toFillin);
247             bool ok;
248             if (value.startsWith('#')) value = value.replace('#', "0x");
249             cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
250             //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
251             cval->label->setText(paramName);
252             m_valueItems[paramName] = cval;
253             connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
254             m_uiItems.append(cval);
255         } else if (type == "position") {
256             int pos = value.toInt();
257             if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
258                 pos = pos - m_in;
259             } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
260                 // fadeout position starts from clip end
261                 pos = m_out - (pos - m_in);
262             }
263             PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
264             m_vbox->addWidget(posedit);
265             m_valueItems[paramName+"position"] = posedit;
266             connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
267         } else if (type == "wipe") {
268             Wipeval *wpval = new Wipeval;
269             wpval->setupUi(toFillin);
270             wipeInfo w = getWipeInfo(value);
271             switch (w.start) {
272             case UP:
273                 wpval->start_up->setChecked(true);
274                 break;
275             case DOWN:
276                 wpval->start_down->setChecked(true);
277                 break;
278             case RIGHT:
279                 wpval->start_right->setChecked(true);
280                 break;
281             case LEFT:
282                 wpval->start_left->setChecked(true);
283                 break;
284             default:
285                 wpval->start_center->setChecked(true);
286                 break;
287             }
288             switch (w.end) {
289             case UP:
290                 wpval->end_up->setChecked(true);
291                 break;
292             case DOWN:
293                 wpval->end_down->setChecked(true);
294                 break;
295             case RIGHT:
296                 wpval->end_right->setChecked(true);
297                 break;
298             case LEFT:
299                 wpval->end_left->setChecked(true);
300                 break;
301             default:
302                 wpval->end_center->setChecked(true);
303                 break;
304             }
305             wpval->start_transp->setValue(w.startTransparency);
306             wpval->end_transp->setValue(w.endTransparency);
307             m_valueItems[paramName] = wpval;
308             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
309             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
310             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
311             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
312             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
313             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
314             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
315             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
316             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
317             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
318             connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
319             connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
320             //wpval->title->setTitle(na.toElement().text());
321             m_uiItems.append(wpval);
322         } else {
323             delete toFillin;
324             toFillin = NULL;
325         }
326
327         if (toFillin) {
328             m_vbox->addWidget(toFillin);
329         }
330     }
331     m_vbox->addStretch();
332 }
333
334 void EffectStackEdit::slotSeekToPos(int pos)
335 {
336     emit seekTimeline(m_in + pos);
337 }
338
339 wipeInfo EffectStackEdit::getWipeInfo(QString value)
340 {
341     wipeInfo info;
342     QString start = value.section(';', 0, 0);
343     QString end = value.section(';', 1, 1).section('=', 1, 1);
344     if (start.startsWith("-100%,0")) info.start = LEFT;
345     else if (start.startsWith("100%,0")) info.start = RIGHT;
346     else if (start.startsWith("0%,100%")) info.start = DOWN;
347     else if (start.startsWith("0%,-100%")) info.start = UP;
348     else info.start = CENTER;
349     if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
350     else info.startTransparency = 100;
351
352     if (end.startsWith("-100%,0")) info.end = LEFT;
353     else if (end.startsWith("100%,0")) info.end = RIGHT;
354     else if (end.startsWith("0%,100%")) info.end = DOWN;
355     else if (end.startsWith("0%,-100%")) info.end = UP;
356     else info.end = CENTER;
357     if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
358     else info.endTransparency = 100;
359     return info;
360 }
361
362 QString EffectStackEdit::getWipeString(wipeInfo info)
363 {
364
365     QString start;
366     QString end;
367     switch (info.start) {
368     case LEFT:
369         start = "-100%,0%:100%x100%";
370         break;
371     case RIGHT:
372         start = "100%,0%:100%x100%";
373         break;
374     case DOWN:
375         start = "0%,100%:100%x100%";
376         break;
377     case UP:
378         start = "0%,-100%:100%x100%";
379         break;
380     default:
381         start = "0%,0%:100%x100%";
382         break;
383     }
384     start.append(':' + QString::number(info.startTransparency));
385
386     switch (info.end) {
387     case LEFT:
388         end = "-100%,0%:100%x100%";
389         break;
390     case RIGHT:
391         end = "100%,0%:100%x100%";
392         break;
393     case DOWN:
394         end = "0%,100%:100%x100%";
395         break;
396     case UP:
397         end = "0%,-100%:100%x100%";
398         break;
399     default:
400         end = "0%,0%:100%x100%";
401         break;
402     }
403     end.append(':' + QString::number(info.endTransparency));
404     return QString(start + ";-1=" + end);
405 }
406
407 void EffectStackEdit::collectAllParameters()
408 {
409     if (m_valueItems.isEmpty() || m_params.isNull()) return;
410     const QDomElement oldparam = m_params.cloneNode().toElement();
411     QDomElement newparam = oldparam.cloneNode().toElement();
412     QDomNodeList namenode = newparam.elementsByTagName("parameter");
413
414     for (int i = 0; i < namenode.count() ; i++) {
415         QDomNode pa = namenode.item(i);
416         QDomNode na = pa.firstChildElement("name");
417         QString type = pa.attributes().namedItem("type").nodeValue();
418         QString paramName = i18n(na.toElement().text().toUtf8().data());
419         if (type == "complex") paramName.append("complex");
420         else if (type == "position") paramName.append("position");
421         else if (type == "geometry") paramName.append("geometry");
422         else if (type == "keyframe") paramName.append("keyframe");
423         if (!m_valueItems.contains(paramName)) {
424             // kDebug() << "// Param: " << paramName << " NOT FOUND";
425             continue;
426         }
427
428         QString setValue;
429         if (type == "double" || type == "constant") {
430             QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
431             setValue = QString::number(slider->value());
432         } else if (type == "list") {
433             KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
434             setValue = box->itemData(box->currentIndex()).toString();
435         } else if (type == "bool") {
436             QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
437             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
438         } else if (type == "color") {
439             KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
440             setValue = color->color().name();
441         } else if (type == "complex") {
442             ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
443             namenode.item(i) = complex->getParamDesc();
444         } else if (type == "geometry") {
445             Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
446             namenode.item(i).toElement().setAttribute("value", geom->getValue());
447         } else if (type == "position") {
448             PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
449             int pos = pedit->getPosition();
450             setValue = QString::number(pos);
451             if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
452                 // Make sure duration is not longer than clip
453                 /*if (pos > m_out) {
454                     pos = m_out;
455                     pedit->setPosition(pos);
456                 }*/
457                 EffectsList::setParameter(newparam, "in", QString::number(m_in));
458                 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
459                 setValue.clear();
460             } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
461                 // Make sure duration is not longer than clip
462                 /*if (pos > m_out) {
463                     pos = m_out;
464                     pedit->setPosition(pos);
465                 }*/
466                 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
467                 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
468                 setValue.clear();
469             }
470         } else if (type == "wipe") {
471             Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
472             wipeInfo info;
473             if (wp->start_left->isChecked()) info.start = LEFT;
474             else if (wp->start_right->isChecked()) info.start = RIGHT;
475             else if (wp->start_up->isChecked()) info.start = UP;
476             else if (wp->start_down->isChecked()) info.start = DOWN;
477             else if (wp->start_center->isChecked()) info.start = CENTER;
478             else info.start = LEFT;
479             info.startTransparency = wp->start_transp->value();
480             if (wp->end_left->isChecked()) info.end = LEFT;
481             else if (wp->end_right->isChecked()) info.end = RIGHT;
482             else if (wp->end_up->isChecked()) info.end = UP;
483             else if (wp->end_down->isChecked()) info.end = DOWN;
484             else if (wp->end_center->isChecked()) info.end = CENTER;
485             else info.end = RIGHT;
486             info.endTransparency = wp->end_transp->value();
487             setValue = getWipeString(info);
488         }
489
490         if (!setValue.isNull()) {
491             pa.attributes().namedItem("value").setNodeValue(setValue);
492         }
493     }
494     emit parameterChanged(oldparam, newparam);
495 }
496
497 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
498 {
499     QWidget* toFillin = new QWidget(m_baseWidget);
500     Constval *ctval = new Constval;
501     ctval->setupUi(toFillin);
502     ctval->horizontalSlider->setMinimum(min);
503     ctval->horizontalSlider->setMaximum(max);
504     if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
505     ctval->spinBox->setMinimum(min);
506     ctval->spinBox->setMaximum(max);
507     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
508     ctval->horizontalSlider->setValue(val);
509     ctval->label->setText(name);
510     m_valueItems[name] = ctval;
511     m_uiItems.append(ctval);
512     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
513     m_vbox->addWidget(toFillin);
514 }
515
516 void EffectStackEdit::slotSliderMoved(int)
517 {
518     collectAllParameters();
519 }
520
521 void EffectStackEdit::clearAllItems()
522 {
523     blockSignals(true);
524     m_valueItems.clear();
525     m_uiItems.clear();
526     /*while (!m_items.isEmpty()) {
527         QWidget *die = m_items.takeFirst();
528         die->disconnect();
529         delete die;
530     }*/
531     //qDeleteAll(m_uiItems);
532     QLayoutItem *child;
533     while ((child = m_vbox->takeAt(0)) != 0) {
534         QWidget *wid = child->widget();
535         delete child;
536         if (wid) delete wid;
537     }
538     blockSignals(false);
539 }