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