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