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