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