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