]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
27fccfa022884801b1cbb2b1e86f7410c42cb837
[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 }
90
91 void EffectStackEdit::setFrameSize(QPoint p)
92 {
93     m_frameSize = p;
94     QDomNodeList namenode = m_params.elementsByTagName("parameter");
95     for (int i = 0; i < namenode.count() ; i++) {
96         QDomNode pa = namenode.item(i);
97         QDomNode na = pa.firstChildElement("name");
98         QString type = pa.attributes().namedItem("type").nodeValue();
99         QString paramName = i18n(na.toElement().text().toUtf8().data());
100
101         if (type == "geometry") {
102             Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
103             geom->setFrameSize(m_frameSize);
104             break;
105         }
106     }
107
108 }
109
110 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
111 {
112     m_profile = profile;
113     m_timecode = t;
114 }
115
116 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
117 {
118     m_params.setAttribute(name, value);
119 }
120
121 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
122 {
123     clearAllItems();
124     m_keyframeEditor = NULL;
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             if (m_keyframeEditor == NULL) {
238                 KeyframeEdit *geo = new KeyframeEdit(pa, m_in, m_in + m_out, pa.attribute("min").toInt(), pa.attribute("max").toInt(), m_timecode, paramName);
239                 m_vbox->addWidget(geo);
240                 m_valueItems[paramName+"keyframe"] = geo;
241                 m_keyframeEditor = geo;
242                 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
243             }
244             else {
245                 // we already have a keyframe editor, so just add another column for the new param
246                 m_keyframeEditor->addParameter(pa);
247             }
248         } else if (type == "color") {
249             Colorval *cval = new Colorval;
250             cval->setupUi(toFillin);
251             bool ok;
252             if (value.startsWith('#')) value = value.replace('#', "0x");
253             cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
254             //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
255             cval->label->setText(paramName);
256             m_valueItems[paramName] = cval;
257             connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
258             m_uiItems.append(cval);
259         } else if (type == "position") {
260             int pos = value.toInt();
261             if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
262                 pos = pos - m_in;
263             } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
264                 // fadeout position starts from clip end
265                 pos = m_out - (pos - m_in);
266             }
267             PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
268             m_vbox->addWidget(posedit);
269             m_valueItems[paramName+"position"] = posedit;
270             connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
271         } else if (type == "wipe") {
272             Wipeval *wpval = new Wipeval;
273             wpval->setupUi(toFillin);
274             wipeInfo w = getWipeInfo(value);
275             switch (w.start) {
276             case UP:
277                 wpval->start_up->setChecked(true);
278                 break;
279             case DOWN:
280                 wpval->start_down->setChecked(true);
281                 break;
282             case RIGHT:
283                 wpval->start_right->setChecked(true);
284                 break;
285             case LEFT:
286                 wpval->start_left->setChecked(true);
287                 break;
288             default:
289                 wpval->start_center->setChecked(true);
290                 break;
291             }
292             switch (w.end) {
293             case UP:
294                 wpval->end_up->setChecked(true);
295                 break;
296             case DOWN:
297                 wpval->end_down->setChecked(true);
298                 break;
299             case RIGHT:
300                 wpval->end_right->setChecked(true);
301                 break;
302             case LEFT:
303                 wpval->end_left->setChecked(true);
304                 break;
305             default:
306                 wpval->end_center->setChecked(true);
307                 break;
308             }
309             wpval->start_transp->setValue(w.startTransparency);
310             wpval->end_transp->setValue(w.endTransparency);
311             m_valueItems[paramName] = wpval;
312             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
313             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
314             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
315             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
316             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
317             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
318             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
319             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
320             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
321             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
322             connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
323             connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
324             //wpval->title->setTitle(na.toElement().text());
325             m_uiItems.append(wpval);
326         } else {
327             delete toFillin;
328             toFillin = NULL;
329         }
330
331         if (toFillin) {
332             m_vbox->addWidget(toFillin);
333         }
334     }
335     m_vbox->addStretch();
336 }
337
338 void EffectStackEdit::slotSeekToPos(int pos)
339 {
340     emit seekTimeline(m_in + pos);
341 }
342
343 wipeInfo EffectStackEdit::getWipeInfo(QString value)
344 {
345     wipeInfo info;
346     QString start = value.section(';', 0, 0);
347     QString end = value.section(';', 1, 1).section('=', 1, 1);
348     if (start.startsWith("-100%,0")) info.start = LEFT;
349     else if (start.startsWith("100%,0")) info.start = RIGHT;
350     else if (start.startsWith("0%,100%")) info.start = DOWN;
351     else if (start.startsWith("0%,-100%")) info.start = UP;
352     else info.start = CENTER;
353     if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
354     else info.startTransparency = 100;
355
356     if (end.startsWith("-100%,0")) info.end = LEFT;
357     else if (end.startsWith("100%,0")) info.end = RIGHT;
358     else if (end.startsWith("0%,100%")) info.end = DOWN;
359     else if (end.startsWith("0%,-100%")) info.end = UP;
360     else info.end = CENTER;
361     if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
362     else info.endTransparency = 100;
363     return info;
364 }
365
366 QString EffectStackEdit::getWipeString(wipeInfo info)
367 {
368
369     QString start;
370     QString end;
371     switch (info.start) {
372     case LEFT:
373         start = "-100%,0%:100%x100%";
374         break;
375     case RIGHT:
376         start = "100%,0%:100%x100%";
377         break;
378     case DOWN:
379         start = "0%,100%:100%x100%";
380         break;
381     case UP:
382         start = "0%,-100%:100%x100%";
383         break;
384     default:
385         start = "0%,0%:100%x100%";
386         break;
387     }
388     start.append(':' + QString::number(info.startTransparency));
389
390     switch (info.end) {
391     case LEFT:
392         end = "-100%,0%:100%x100%";
393         break;
394     case RIGHT:
395         end = "100%,0%:100%x100%";
396         break;
397     case DOWN:
398         end = "0%,100%:100%x100%";
399         break;
400     case UP:
401         end = "0%,-100%:100%x100%";
402         break;
403     default:
404         end = "0%,0%:100%x100%";
405         break;
406     }
407     end.append(':' + QString::number(info.endTransparency));
408     return QString(start + ";-1=" + end);
409 }
410
411 void EffectStackEdit::collectAllParameters()
412 {
413     if (m_valueItems.isEmpty() || m_params.isNull()) return;
414     const QDomElement oldparam = m_params.cloneNode().toElement();
415     QDomElement newparam = oldparam.cloneNode().toElement();
416     QDomNodeList namenode = newparam.elementsByTagName("parameter");
417
418     for (int i = 0; i < namenode.count() ; i++) {
419         QDomNode pa = namenode.item(i);
420         QDomNode na = pa.firstChildElement("name");
421         QString type = pa.attributes().namedItem("type").nodeValue();
422         QString paramName = i18n(na.toElement().text().toUtf8().data());
423         if (type == "complex") paramName.append("complex");
424         else if (type == "position") paramName.append("position");
425         else if (type == "geometry") paramName.append("geometry");
426         else if (type == "keyframe") paramName.append("keyframe");
427         if (!m_valueItems.contains(paramName)) {
428             // kDebug() << "// Param: " << paramName << " NOT FOUND";
429             continue;
430         }
431
432         QString setValue;
433         if (type == "double" || type == "constant") {
434             QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
435             setValue = QString::number(slider->value());
436         } else if (type == "list") {
437             KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
438             setValue = box->itemData(box->currentIndex()).toString();
439         } else if (type == "bool") {
440             QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
441             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
442         } else if (type == "color") {
443             KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
444             setValue = color->color().name();
445         } else if (type == "complex") {
446             ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
447             namenode.item(i) = complex->getParamDesc();
448         } else if (type == "geometry") {
449             Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
450             namenode.item(i).toElement().setAttribute("value", geom->getValue());
451         } else if (type == "position") {
452             PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
453             int pos = pedit->getPosition();
454             setValue = QString::number(pos);
455             if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
456                 // Make sure duration is not longer than clip
457                 /*if (pos > m_out) {
458                     pos = m_out;
459                     pedit->setPosition(pos);
460                 }*/
461                 EffectsList::setParameter(newparam, "in", QString::number(m_in));
462                 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
463                 setValue.clear();
464             } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
465                 // Make sure duration is not longer than clip
466                 /*if (pos > m_out) {
467                     pos = m_out;
468                     pedit->setPosition(pos);
469                 }*/
470                 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
471                 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
472                 setValue.clear();
473             }
474         } else if (type == "wipe") {
475             Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
476             wipeInfo info;
477             if (wp->start_left->isChecked()) info.start = LEFT;
478             else if (wp->start_right->isChecked()) info.start = RIGHT;
479             else if (wp->start_up->isChecked()) info.start = UP;
480             else if (wp->start_down->isChecked()) info.start = DOWN;
481             else if (wp->start_center->isChecked()) info.start = CENTER;
482             else info.start = LEFT;
483             info.startTransparency = wp->start_transp->value();
484             if (wp->end_left->isChecked()) info.end = LEFT;
485             else if (wp->end_right->isChecked()) info.end = RIGHT;
486             else if (wp->end_up->isChecked()) info.end = UP;
487             else if (wp->end_down->isChecked()) info.end = DOWN;
488             else if (wp->end_center->isChecked()) info.end = CENTER;
489             else info.end = RIGHT;
490             info.endTransparency = wp->end_transp->value();
491             setValue = getWipeString(info);
492         }
493
494         if (!setValue.isNull()) {
495             pa.attributes().namedItem("value").setNodeValue(setValue);
496         }
497     }
498     emit parameterChanged(oldparam, newparam);
499 }
500
501 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
502 {
503     QWidget* toFillin = new QWidget(m_baseWidget);
504     Constval *ctval = new Constval;
505     ctval->setupUi(toFillin);
506     ctval->horizontalSlider->setMinimum(min);
507     ctval->horizontalSlider->setMaximum(max);
508     if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
509     ctval->spinBox->setMinimum(min);
510     ctval->spinBox->setMaximum(max);
511     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
512     ctval->horizontalSlider->setValue(val);
513     ctval->label->setText(name);
514     m_valueItems[name] = ctval;
515     m_uiItems.append(ctval);
516     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
517     m_vbox->addWidget(toFillin);
518 }
519
520 void EffectStackEdit::slotSliderMoved(int)
521 {
522     collectAllParameters();
523 }
524
525 void EffectStackEdit::clearAllItems()
526 {
527     blockSignals(true);
528     m_valueItems.clear();
529     m_uiItems.clear();
530     /*while (!m_items.isEmpty()) {
531         QWidget *die = m_items.takeFirst();
532         die->disconnect();
533         delete die;
534     }*/
535     //qDeleteAll(m_uiItems);
536     QLayoutItem *child;
537     while ((child = m_vbox->takeAt(0)) != 0) {
538         QWidget *wid = child->widget();
539         delete child;
540         if (wid) delete wid;
541     }
542     blockSignals(false);
543 }