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