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