]> git.sesse.net Git - kdenlive/blob - src/effectstack/parametercontainer.cpp
Cleanup effect stack (part 1)
[kdenlive] / src / effectstack / parametercontainer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2012 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "ui_listval_ui.h"
21 #include "ui_boolval_ui.h"
22 #include "ui_wipeval_ui.h"
23 #include "ui_urlval_ui.h"
24 #include "ui_keywordval_ui.h"
25 #include "ui_fontval_ui.h"
26 #include "complexparameter.h"
27 #include "geometryval.h"
28 #include "positionedit.h"
29 #include "kis_curve_widget.h"
30 #include "kis_cubic_curve.h"
31 #include "choosecolorwidget.h"
32 #include "geometrywidget.h"
33 #include "colortools.h"
34 #include "doubleparameterwidget.h"
35 #include "cornerswidget.h"
36 #include "dragvalue.h"
37 #include "beziercurve/beziersplinewidget.h"
38 #ifdef USE_QJSON
39 #include "rotoscoping/rotowidget.h"
40 #endif
41 #include "kdenlivesettings.h"
42 #include "profilesdialog.h"
43 #include "projectlist.h"
44 #include "mainwindow.h"
45 #include "parametercontainer.h"
46
47 #include <KUrlRequester>
48 #include <KFileDialog>
49
50 #include <QMap>
51 #include <QString>
52 #include <QImage>
53
54 MySpinBox::MySpinBox(QWidget * parent):
55     QSpinBox(parent)
56 {
57     setFocusPolicy(Qt::StrongFocus);
58 }
59
60 void MySpinBox::focusInEvent(QFocusEvent *e)
61 {
62      setFocusPolicy(Qt::WheelFocus);
63      e->accept();
64 }
65
66 void MySpinBox::focusOutEvent(QFocusEvent *e)
67 {
68      setFocusPolicy(Qt::StrongFocus);
69      e->accept();
70 }
71
72 class Boolval: public QWidget, public Ui::Boolval_UI
73 {
74 };
75
76 class Listval: public QWidget, public Ui::Listval_UI
77 {
78 };
79
80 class Wipeval: public QWidget, public Ui::Wipeval_UI
81 {
82 };
83
84 class Urlval: public QWidget, public Ui::Urlval_UI
85 {
86 };
87
88 class Keywordval: public QWidget, public Ui::Keywordval_UI
89 {
90 };
91
92 class Fontval: public QWidget, public Ui::Fontval_UI
93 {
94 };
95
96
97 ParameterContainer::ParameterContainer(QDomElement effect, ItemInfo info, EffectMetaInfo *metaInfo, QWidget * parent) :
98         m_keyframeEditor(NULL),
99         m_geometryWidget(NULL),
100         m_metaInfo(metaInfo),
101         m_effect(effect)
102 {
103     m_in = info.cropStart.frames(KdenliveSettings::project_fps());
104     m_out = (info.cropStart + info.cropDuration).frames(KdenliveSettings::project_fps()) - 1;
105
106     QDomNodeList namenode = effect.childNodes(); //elementsByTagName("parameter");
107     
108     QDomElement e = effect.toElement();
109     int minFrame = e.attribute("start").toInt();
110     int maxFrame = e.attribute("end").toInt();
111     // In transitions, maxFrame is in fact one frame after the end of transition
112     if (maxFrame > 0) maxFrame --;
113
114     bool disable = effect.attribute("disable") == "1" && KdenliveSettings::disable_effect_parameters();
115     parent->setEnabled(!disable);
116
117     bool stretch = true;
118     m_vbox = new QVBoxLayout(parent);
119     m_vbox->setContentsMargins(4, 0, 4, 0);
120     m_vbox->setSpacing(2);
121
122     for (int i = 0; i < namenode.count() ; i++) {
123         QDomElement pa = namenode.item(i).toElement();
124         if (pa.tagName() != "parameter") continue;
125         QDomElement na = pa.firstChildElement("name");
126         QDomElement commentElem = pa.firstChildElement("comment");
127         QString type = pa.attribute("type");
128         QString paramName = na.isNull() ? pa.attribute("name") : i18n(na.text().toUtf8().data());
129         QString comment;
130         if (!commentElem.isNull())
131             comment = i18n(commentElem.text().toUtf8().data());
132         QWidget * toFillin = new QWidget(parent);
133         QString value = pa.attribute("value").isNull() ?
134                         pa.attribute("default") : pa.attribute("value");
135
136
137         /** See effects/README for info on the different types */
138
139         if (type == "double" || type == "constant") {
140             double min;
141             double max;
142             if (pa.attribute("min").contains('%'))
143                 min = ProfilesDialog::getStringEval(m_metaInfo->profile, pa.attribute("min"), m_metaInfo->frameSize);
144             else
145                 min = pa.attribute("min").toDouble();
146             if (pa.attribute("max").contains('%'))
147                 max = ProfilesDialog::getStringEval(m_metaInfo->profile, pa.attribute("max"), m_metaInfo->frameSize);
148             else
149                 max = pa.attribute("max").toDouble();
150
151             DoubleParameterWidget *doubleparam = new DoubleParameterWidget(paramName, value.toDouble(), min, max,
152                     pa.attribute("default").toDouble(), comment, -1, pa.attribute("suffix"), pa.attribute("decimals").toInt(), parent);
153             doubleparam->setFocusPolicy(Qt::StrongFocus);
154             m_vbox->addWidget(doubleparam);
155             m_valueItems[paramName] = doubleparam;
156             connect(doubleparam, SIGNAL(valueChanged(double)), this, SLOT(slotCollectAllParameters()));
157             connect(this, SIGNAL(showComments(bool)), doubleparam, SLOT(slotShowComment(bool)));
158         } else if (type == "list") {
159             Listval *lsval = new Listval;
160             lsval->setupUi(toFillin);
161             lsval->list->setFocusPolicy(Qt::StrongFocus);
162             QStringList listitems = pa.attribute("paramlist").split(';');
163             if (listitems.count() == 1) {
164                 // probably custom effect created before change to ';' as separator
165                 listitems = pa.attribute("paramlist").split(",");
166             }
167             QDomElement list = pa.firstChildElement("paramlistdisplay");
168             QStringList listitemsdisplay;
169             if (!list.isNull()) {
170                 listitemsdisplay = i18n(list.text().toUtf8().data()).split(',');
171             } else {
172                 listitemsdisplay = i18n(pa.attribute("paramlistdisplay").toUtf8().data()).split(',');
173             }
174             if (listitemsdisplay.count() != listitems.count())
175                 listitemsdisplay = listitems;
176             lsval->list->setIconSize(QSize(30, 30));
177             for (int i = 0; i < listitems.count(); i++) {
178                 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
179                 QString entry = listitems.at(i);
180                 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
181                     if (!MainWindow::m_lumacache.contains(entry)) {
182                         QImage pix(entry);
183                         MainWindow::m_lumacache.insert(entry, pix.scaled(30, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation));
184                     }
185                     lsval->list->setItemIcon(i, QPixmap::fromImage(MainWindow::m_lumacache.value(entry)));
186                 }
187             }
188             if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
189             lsval->name->setText(paramName);
190             lsval->labelComment->setText(comment);
191             lsval->widgetComment->setHidden(true);
192             m_valueItems[paramName] = lsval;
193             connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(slotCollectAllParameters()));
194             if (!comment.isEmpty())
195                 connect(this, SIGNAL(showComments(bool)), lsval->widgetComment, SLOT(setVisible(bool)));
196             m_uiItems.append(lsval);
197         } else if (type == "bool") {
198             Boolval *bval = new Boolval;
199             bval->setupUi(toFillin);
200             bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
201             bval->name->setText(paramName);
202             bval->labelComment->setText(comment);
203             bval->widgetComment->setHidden(true);
204             m_valueItems[paramName] = bval;
205             connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(slotCollectAllParameters()));
206             if (!comment.isEmpty())
207                 connect(this, SIGNAL(showComments(bool)), bval->widgetComment, SLOT(setVisible(bool)));
208             m_uiItems.append(bval);
209         } else if (type == "complex") {
210             ComplexParameter *pl = new ComplexParameter;
211             pl->setupParam(effect, pa.attribute("name"), 0, 100);
212             m_vbox->addWidget(pl);
213             m_valueItems[paramName+"complex"] = pl;
214             connect(pl, SIGNAL(parameterChanged()), this, SLOT(slotCollectAllParameters()));
215         } else if (type == "geometry") {
216             if (KdenliveSettings::on_monitor_effects()) {
217                 m_geometryWidget = new GeometryWidget(m_metaInfo->monitor, m_metaInfo->timecode, 0, true, effect.hasAttribute("showrotation"), parent);
218                 m_geometryWidget->setFrameSize(m_metaInfo->frameSize);
219                 m_geometryWidget->slotShowScene(!disable);
220                 // connect this before setupParam to make sure the monitor scene shows up at startup
221                 connect(m_geometryWidget, SIGNAL(checkMonitorPosition(int)), this, SIGNAL(checkMonitorPosition(int)));
222                 connect(m_geometryWidget, SIGNAL(parameterChanged()), this, SLOT(slotCollectAllParameters()));
223                 if (minFrame == maxFrame)
224                     m_geometryWidget->setupParam(pa, m_in, m_out);
225                 else
226                     m_geometryWidget->setupParam(pa, minFrame, maxFrame);
227                 m_vbox->addWidget(m_geometryWidget);
228                 m_valueItems[paramName+"geometry"] = m_geometryWidget;
229                 connect(m_geometryWidget, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
230                 connect(this, SIGNAL(syncEffectsPos(int)), m_geometryWidget, SLOT(slotSyncPosition(int)));
231                 connect(this, SIGNAL(effectStateChanged(bool)), m_geometryWidget, SLOT(slotShowScene(bool)));
232             } else {
233                 Geometryval *geo = new Geometryval(m_metaInfo->profile, m_metaInfo->timecode, m_metaInfo->frameSize, 0);
234                 if (minFrame == maxFrame)
235                     geo->setupParam(pa, m_in, m_out);
236                 else
237                     geo->setupParam(pa, minFrame, maxFrame);
238                 m_vbox->addWidget(geo);
239                 m_valueItems[paramName+"geometry"] = geo;
240                 connect(geo, SIGNAL(parameterChanged()), this, SLOT(slotCollectAllParameters()));
241                 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
242                 connect(this, SIGNAL(syncEffectsPos(int)), geo, SLOT(slotSyncPosition(int)));
243             }
244         } else if (type == "addedgeometry") {
245             // this is a parameter that should be linked to the geometry widget, for example rotation, shear, ...
246             if (m_geometryWidget) m_geometryWidget->addParameter(pa);
247         } else if (type == "keyframe" || type == "simplekeyframe") {
248             // keyframe editor widget
249             if (m_keyframeEditor == NULL) {
250                 KeyframeEdit *geo;
251                 if (pa.attribute("widget") == "corners") {
252                     // we want a corners-keyframe-widget
253                     CornersWidget *corners = new CornersWidget(m_metaInfo->monitor, pa, m_in, m_out, m_metaInfo->timecode, e.attribute("active_keyframe", "-1").toInt(), parent);
254                     corners->slotShowScene(!disable);
255                     connect(corners, SIGNAL(checkMonitorPosition(int)), this, SIGNAL(checkMonitorPosition(int)));
256                     connect(this, SIGNAL(effectStateChanged(bool)), corners, SLOT(slotShowScene(bool)));
257                     connect(this, SIGNAL(syncEffectsPos(int)), corners, SLOT(slotSyncPosition(int)));
258                     geo = static_cast<KeyframeEdit *>(corners);
259                 } else {
260                     geo = new KeyframeEdit(pa, m_in, m_out, m_metaInfo->timecode, e.attribute("active_keyframe", "-1").toInt());
261                 }
262                 m_vbox->addWidget(geo);
263                 m_valueItems[paramName+"keyframe"] = geo;
264                 m_keyframeEditor = geo;
265                 connect(geo, SIGNAL(parameterChanged()), this, SLOT(slotCollectAllParameters()));
266                 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
267                 connect(this, SIGNAL(showComments(bool)), geo, SIGNAL(showComments(bool)));
268             } else {
269                 // we already have a keyframe editor, so just add another column for the new param
270                 m_keyframeEditor->addParameter(pa);
271             }
272         } else if (type == "color") {
273             if (pa.hasAttribute("paramprefix")) value.remove(0, pa.attribute("paramprefix").size());
274             if (value.startsWith('#'))
275                 value = value.replace('#', "0x");
276             ChooseColorWidget *choosecolor = new ChooseColorWidget(paramName, value, parent);
277             choosecolor->setAlphaChannelEnabled(true);
278             m_vbox->addWidget(choosecolor);
279             m_valueItems[paramName] = choosecolor;
280             connect(choosecolor, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
281             connect(choosecolor, SIGNAL(modified()) , this, SLOT(slotCollectAllParameters()));
282         } else if (type == "position") {
283             int pos = value.toInt();
284             if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
285                 pos = pos - m_in;
286             } else if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
287                 // fadeout position starts from clip end
288                 pos = m_out - pos;
289             }
290             PositionEdit *posedit = new PositionEdit(paramName, pos, 0, m_out - m_in, m_metaInfo->timecode);
291             m_vbox->addWidget(posedit);
292             m_valueItems[paramName+"position"] = posedit;
293             connect(posedit, SIGNAL(parameterChanged()), this, SLOT(slotCollectAllParameters()));
294         } else if (type == "curve") {
295             KisCurveWidget *curve = new KisCurveWidget(parent);
296             curve->setMaxPoints(pa.attribute("max").toInt());
297             QList<QPointF> points;
298             int number;
299             if (e.attribute("version").toDouble() > 0.2) {
300                 // Rounding gives really weird results. (int) (10 * 0.3) gives 2! So for now, add 0.5 to get correct result
301                 number = EffectsList::parameter(e, pa.attribute("number")).toDouble() * 10 + 0.5;
302             } else {
303                 number = EffectsList::parameter(e, pa.attribute("number")).toInt();
304             }
305             QString inName = pa.attribute("inpoints");
306             QString outName = pa.attribute("outpoints");
307             int start = pa.attribute("min").toInt();
308             for (int j = start; j <= number; j++) {
309                 QString in = inName;
310                 in.replace("%i", QString::number(j));
311                 QString out = outName;
312                 out.replace("%i", QString::number(j));
313                 points << QPointF(EffectsList::parameter(e, in).toDouble(), EffectsList::parameter(e, out).toDouble());
314             }
315             if (!points.isEmpty())
316                 curve->setCurve(KisCubicCurve(points));
317             MySpinBox *spinin = new MySpinBox();
318             spinin->setRange(0, 1000);
319             MySpinBox *spinout = new MySpinBox();
320             spinout->setRange(0, 1000);
321             curve->setupInOutControls(spinin, spinout, 0, 1000);
322             m_vbox->addWidget(curve);
323             m_vbox->addWidget(spinin);
324             m_vbox->addWidget(spinout);
325
326             connect(curve, SIGNAL(modified()), this, SLOT(slotCollectAllParameters()));
327             m_valueItems[paramName] = curve;
328
329             QString depends = pa.attribute("depends");
330             if (!depends.isEmpty())
331                 meetDependency(paramName, type, EffectsList::parameter(e, depends));
332         } else if (type == "bezier_spline") {
333             BezierSplineWidget *widget = new BezierSplineWidget(value, parent);
334             stretch = false;
335             m_vbox->addWidget(widget);
336             m_valueItems[paramName] = widget;
337             connect(widget, SIGNAL(modified()), this, SLOT(slotCollectAllParameters()));
338             QString depends = pa.attribute("depends");
339             if (!depends.isEmpty())
340                 meetDependency(paramName, type, EffectsList::parameter(e, depends));
341 #ifdef USE_QJSON
342         } else if (type == "roto-spline") {
343             RotoWidget *roto = new RotoWidget(value, m_metaInfo->monitor, info, m_metaInfo->timecode, parent);
344             roto->slotShowScene(!disable);
345             connect(roto, SIGNAL(valueChanged()), this, SLOT(slotCollectAllParameters()));
346             connect(roto, SIGNAL(checkMonitorPosition(int)), this, SIGNAL(checkMonitorPosition(int)));
347             connect(roto, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
348             connect(this, SIGNAL(syncEffectsPos(int)), roto, SLOT(slotSyncPosition(int)));
349             connect(this, SIGNAL(effectStateChanged(bool)), roto, SLOT(slotShowScene(bool)));
350             m_vbox->addWidget(roto);
351             m_valueItems[paramName] = roto;
352 #endif
353         } else if (type == "wipe") {
354             Wipeval *wpval = new Wipeval;
355             wpval->setupUi(toFillin);
356             wipeInfo w = getWipeInfo(value);
357             switch (w.start) {
358             case UP:
359                 wpval->start_up->setChecked(true);
360                 break;
361             case DOWN:
362                 wpval->start_down->setChecked(true);
363                 break;
364             case RIGHT:
365                 wpval->start_right->setChecked(true);
366                 break;
367             case LEFT:
368                 wpval->start_left->setChecked(true);
369                 break;
370             default:
371                 wpval->start_center->setChecked(true);
372                 break;
373             }
374             switch (w.end) {
375             case UP:
376                 wpval->end_up->setChecked(true);
377                 break;
378             case DOWN:
379                 wpval->end_down->setChecked(true);
380                 break;
381             case RIGHT:
382                 wpval->end_right->setChecked(true);
383                 break;
384             case LEFT:
385                 wpval->end_left->setChecked(true);
386                 break;
387             default:
388                 wpval->end_center->setChecked(true);
389                 break;
390             }
391             wpval->start_transp->setValue(w.startTransparency);
392             wpval->end_transp->setValue(w.endTransparency);
393             m_valueItems[paramName] = wpval;
394             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
395             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
396             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
397             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
398             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
399             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
400             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
401             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
402             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
403             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(slotCollectAllParameters()));
404             connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(slotCollectAllParameters()));
405             connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(slotCollectAllParameters()));
406             //wpval->title->setTitle(na.toElement().text());
407             m_uiItems.append(wpval);
408         } else if (type == "url") {
409             Urlval *cval = new Urlval;
410             cval->setupUi(toFillin);
411             cval->label->setText(paramName);
412             cval->urlwidget->fileDialog()->setFilter(ProjectList::getExtensions());
413             m_valueItems[paramName] = cval;
414             cval->urlwidget->setUrl(KUrl(value));
415             connect(cval->urlwidget, SIGNAL(returnPressed()) , this, SLOT(slotCollectAllParameters()));
416             connect(cval->urlwidget, SIGNAL(urlSelected(const KUrl&)) , this, SLOT(slotCollectAllParameters()));
417             m_uiItems.append(cval);
418         } else if (type == "keywords") {
419             Keywordval* kval = new Keywordval;
420             kval->setupUi(toFillin);
421             kval->label->setText(paramName);
422             kval->lineeditwidget->setText(value);
423             QDomElement klistelem = pa.firstChildElement("keywords");
424             QDomElement kdisplaylistelem = pa.firstChildElement("keywordsdisplay");
425             QStringList keywordlist;
426             QStringList keyworddisplaylist;
427             if (!klistelem.isNull()) {
428                 keywordlist = klistelem.text().split(';');
429                 keyworddisplaylist = i18n(kdisplaylistelem.text().toUtf8().data()).split(';');
430             }
431             if (keyworddisplaylist.count() != keywordlist.count()) {
432                 keyworddisplaylist = keywordlist;
433             }
434             for (int i = 0; i < keywordlist.count(); i++) {
435                 kval->comboboxwidget->addItem(keyworddisplaylist.at(i), keywordlist.at(i));
436             }
437             // Add disabled user prompt at index 0
438             kval->comboboxwidget->insertItem(0, i18n("<select a keyword>"), "");
439             kval->comboboxwidget->model()->setData( kval->comboboxwidget->model()->index(0,0), QVariant(Qt::NoItemFlags), Qt::UserRole -1);
440             kval->comboboxwidget->setCurrentIndex(0);
441             m_valueItems[paramName] = kval;
442             connect(kval->lineeditwidget, SIGNAL(editingFinished()) , this, SLOT(collectAllParameters()));
443             connect(kval->comboboxwidget, SIGNAL(activated (const QString&)), this, SLOT(collectAllParameters()));
444             m_uiItems.append(kval);
445         } else if (type == "fontfamily") {
446             Fontval* fval = new Fontval;
447             fval->setupUi(toFillin);
448             fval->name->setText(paramName);
449             fval->fontfamilywidget->setCurrentFont(QFont(value));
450             m_valueItems[paramName] = fval;
451             connect(fval->fontfamilywidget, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(collectAllParameters())) ;
452             m_uiItems.append(fval);
453         } else if (type == "filterjob") {
454             QVBoxLayout *l= new QVBoxLayout(toFillin);
455             QPushButton *button = new QPushButton(paramName, toFillin);
456             l->addWidget(button);
457             m_valueItems[paramName] = button;
458             connect(button, SIGNAL(pressed()), this, SLOT(slotStartFilterJobAction()));   
459         } else {
460             delete toFillin;
461             toFillin = NULL;
462         }
463
464         if (toFillin)
465             m_vbox->addWidget(toFillin);
466     }
467
468     if (stretch)
469         m_vbox->addStretch();
470
471     if (m_keyframeEditor)
472         m_keyframeEditor->checkVisibleParam();
473
474     // Make sure all doubleparam spinboxes have the same width, looks much better
475     QList<DoubleParameterWidget *> allWidgets = findChildren<DoubleParameterWidget *>();
476     int minSize = 0;
477     for (int i = 0; i < allWidgets.count(); i++) {
478         if (minSize < allWidgets.at(i)->spinSize()) minSize = allWidgets.at(i)->spinSize();
479     }
480     for (int i = 0; i < allWidgets.count(); i++) {
481         allWidgets.at(i)->setSpinSize(minSize);
482     }
483 }
484
485 ParameterContainer::~ParameterContainer()
486 {
487     clearLayout(m_vbox);
488     delete m_vbox;
489 }
490
491 void ParameterContainer::meetDependency(const QString& name, QString type, QString value)
492 {
493     if (type == "curve") {
494         KisCurveWidget *curve = (KisCurveWidget*)m_valueItems[name];
495         if (curve) {
496             int color = value.toInt();
497             curve->setPixmap(QPixmap::fromImage(ColorTools::rgbCurvePlane(curve->size(), (ColorTools::ColorsRGB)(color == 3 ? 4 : color), 0.8)));
498         }
499     } else if (type == "bezier_spline") {
500         BezierSplineWidget *widget = (BezierSplineWidget*)m_valueItems[name];
501         if (widget) {
502             widget->setMode((BezierSplineWidget::CurveModes)((int)(value.toDouble() * 10 + 0.5)));
503         }
504     }
505 }
506
507 wipeInfo ParameterContainer::getWipeInfo(QString value)
508 {
509     wipeInfo info;
510     // Convert old geometry values that used a comma as separator
511     if (value.contains(',')) value.replace(',','/');
512     QString start = value.section(';', 0, 0);
513     QString end = value.section(';', 1, 1).section('=', 1, 1);
514     if (start.startsWith("-100%/0"))
515         info.start = LEFT;
516     else if (start.startsWith("100%/0"))
517         info.start = RIGHT;
518     else if (start.startsWith("0%/100%"))
519         info.start = DOWN;
520     else if (start.startsWith("0%/-100%"))
521         info.start = UP;
522     else
523         info.start = CENTER;
524
525     if (start.count(':') == 2)
526         info.startTransparency = start.section(':', -1).toInt();
527     else
528         info.startTransparency = 100;
529
530     if (end.startsWith("-100%/0"))
531         info.end = LEFT;
532     else if (end.startsWith("100%/0"))
533         info.end = RIGHT;
534     else if (end.startsWith("0%/100%"))
535         info.end = DOWN;
536     else if (end.startsWith("0%/-100%"))
537         info.end = UP;
538     else
539         info.end = CENTER;
540
541     if (end.count(':') == 2)
542         info.endTransparency = end.section(':', -1).toInt();
543     else
544         info.endTransparency = 100;
545
546     return info;
547 }
548
549 void ParameterContainer::updateTimecodeFormat()
550 {
551     if (m_keyframeEditor)
552         m_keyframeEditor->updateTimecodeFormat();
553
554     QDomNodeList namenode = m_effect.elementsByTagName("parameter");
555     for (int i = 0; i < namenode.count() ; i++) {
556         QDomNode pa = namenode.item(i);
557         QDomElement na = pa.firstChildElement("name");
558         QString type = pa.attributes().namedItem("type").nodeValue();
559         QString paramName = na.isNull() ? pa.attributes().namedItem("name").nodeValue() : i18n(na.text().toUtf8().data());
560
561         if (type == "geometry") {
562             if (KdenliveSettings::on_monitor_effects()) {
563                 if (m_geometryWidget) m_geometryWidget->updateTimecodeFormat();
564             } else {
565                 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
566                 geom->updateTimecodeFormat();
567             }
568             break;
569         } else if (type == "position") {
570             PositionEdit *posi = ((PositionEdit*)m_valueItems[paramName+"position"]);
571             posi->updateTimecodeFormat();
572             break;
573 #ifdef USE_QJSON
574         } else if (type == "roto-spline") {
575             RotoWidget *widget = static_cast<RotoWidget *>(m_valueItems[paramName]);
576             widget->updateTimecodeFormat();
577 #endif
578         }
579     }
580 }
581
582 void ParameterContainer::slotCollectAllParameters()
583 {
584     if (m_valueItems.isEmpty() || m_effect.isNull()) return;
585     QLocale locale;
586     locale.setNumberOptions(QLocale::OmitGroupSeparator);
587     const QDomElement oldparam = m_effect.cloneNode().toElement();
588     //QDomElement newparam = oldparam.cloneNode().toElement();
589     QDomNodeList namenode = m_effect.elementsByTagName("parameter");
590
591     for (int i = 0; i < namenode.count() ; i++) {
592         QDomElement pa = namenode.item(i).toElement();
593         QDomElement na = pa.firstChildElement("name");
594         QString type = pa.attribute("type");
595         QString paramName = na.isNull() ? pa.attribute("name") : i18n(na.text().toUtf8().data());
596         if (type == "complex")
597             paramName.append("complex");
598         else if (type == "position")
599             paramName.append("position");
600         else if (type == "geometry")
601             paramName.append("geometry");
602         else if (type == "keyframe")
603             paramName.append("keyframe");
604         if (type != "simplekeyframe" && type != "fixed" && type != "addedgeometry" && !m_valueItems.contains(paramName)) {
605             kDebug() << "// Param: " << paramName << " NOT FOUND";
606             continue;
607         }
608
609         QString setValue;
610         if (type == "double" || type == "constant") {
611             DoubleParameterWidget *doubleparam = (DoubleParameterWidget*)m_valueItems.value(paramName);
612             setValue = locale.toString(doubleparam->getValue());
613         } else if (type == "list") {
614             KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
615             setValue = box->itemData(box->currentIndex()).toString();
616         } else if (type == "bool") {
617             QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
618             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
619         } else if (type == "color") {
620             ChooseColorWidget *choosecolor = ((ChooseColorWidget*)m_valueItems.value(paramName));
621             setValue = choosecolor->getColor();
622             if (pa.hasAttribute("paramprefix")) setValue.prepend(pa.attribute("paramprefix"));
623         } else if (type == "complex") {
624             ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
625             namenode.item(i) = complex->getParamDesc();
626         } else if (type == "geometry") {
627             if (KdenliveSettings::on_monitor_effects()) {
628                 if (m_geometryWidget) namenode.item(i).toElement().setAttribute("value", m_geometryWidget->getValue());
629             } else {
630                 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
631                 namenode.item(i).toElement().setAttribute("value", geom->getValue());
632             }
633         } else if (type == "addedgeometry") {
634             namenode.item(i).toElement().setAttribute("value", m_geometryWidget->getExtraValue(namenode.item(i).toElement().attribute("name")));
635         } else if (type == "position") {
636             PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
637             int pos = pedit->getPosition();
638             setValue = QString::number(pos);
639             if (m_effect.attribute("id") == "fadein" || m_effect.attribute("id") == "fade_from_black") {
640                 // Make sure duration is not longer than clip
641                 /*if (pos > m_out) {
642                     pos = m_out;
643                     pedit->setPosition(pos);
644                 }*/
645                 EffectsList::setParameter(m_effect, "in", QString::number(m_in));
646                 EffectsList::setParameter(m_effect, "out", QString::number(m_in + pos));
647                 setValue.clear();
648             } else if (m_effect.attribute("id") == "fadeout" || m_effect.attribute("id") == "fade_to_black") {
649                 // Make sure duration is not longer than clip
650                 /*if (pos > m_out) {
651                     pos = m_out;
652                     pedit->setPosition(pos);
653                 }*/
654                 EffectsList::setParameter(m_effect, "in", QString::number(m_out - pos));
655                 EffectsList::setParameter(m_effect, "out", QString::number(m_out));
656                 setValue.clear();
657             }
658         } else if (type == "curve") {
659             KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
660             QList<QPointF> points = curve->curve().points();
661             QString number = pa.attribute("number");
662             QString inName = pa.attribute("inpoints");
663             QString outName = pa.attribute("outpoints");
664             int off = pa.attribute("min").toInt();
665             int end = pa.attribute("max").toInt();
666             if (oldparam.attribute("version").toDouble() > 0.2) {
667                 EffectsList::setParameter(m_effect, number, locale.toString(points.count() / 10.));
668             } else {
669                 EffectsList::setParameter(m_effect, number, QString::number(points.count()));
670             }
671             for (int j = 0; (j < points.count() && j + off <= end); j++) {
672                 QString in = inName;
673                 in.replace("%i", QString::number(j + off));
674                 QString out = outName;
675                 out.replace("%i", QString::number(j + off));
676                 EffectsList::setParameter(m_effect, in, locale.toString(points.at(j).x()));
677                 EffectsList::setParameter(m_effect, out, locale.toString(points.at(j).y()));
678             }
679             QString depends = pa.attribute("depends");
680             if (!depends.isEmpty())
681                 meetDependency(paramName, type, EffectsList::parameter(m_effect, depends));
682         } else if (type == "bezier_spline") {
683             BezierSplineWidget *widget = (BezierSplineWidget*)m_valueItems.value(paramName);
684             setValue = widget->spline();
685             QString depends = pa.attribute("depends");
686             if (!depends.isEmpty())
687                 meetDependency(paramName, type, EffectsList::parameter(m_effect, depends));
688 #ifdef USE_QJSON
689         } else if (type == "roto-spline") {
690             RotoWidget *widget = static_cast<RotoWidget *>(m_valueItems.value(paramName));
691             setValue = widget->getSpline();
692 #endif
693         } else if (type == "wipe") {
694             Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
695             wipeInfo info;
696             if (wp->start_left->isChecked())
697                 info.start = LEFT;
698             else if (wp->start_right->isChecked())
699                 info.start = RIGHT;
700             else if (wp->start_up->isChecked())
701                 info.start = UP;
702             else if (wp->start_down->isChecked())
703                 info.start = DOWN;
704             else if (wp->start_center->isChecked())
705                 info.start = CENTER;
706             else
707                 info.start = LEFT;
708             info.startTransparency = wp->start_transp->value();
709
710             if (wp->end_left->isChecked())
711                 info.end = LEFT;
712             else if (wp->end_right->isChecked())
713                 info.end = RIGHT;
714             else if (wp->end_up->isChecked())
715                 info.end = UP;
716             else if (wp->end_down->isChecked())
717                 info.end = DOWN;
718             else if (wp->end_center->isChecked())
719                 info.end = CENTER;
720             else
721                 info.end = RIGHT;
722             info.endTransparency = wp->end_transp->value();
723
724             setValue = getWipeString(info);
725         } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
726             QString realName = i18n(na.toElement().text().toUtf8().data());
727             QString val = m_keyframeEditor->getValue(realName);
728             pa.setAttribute("keyframes", val);
729
730             if (m_keyframeEditor->isVisibleParam(realName)) {
731                 pa.setAttribute("intimeline", "1");
732             }
733             else if (pa.hasAttribute("intimeline"))
734                 pa.removeAttribute("intimeline");
735         } else if (type == "url") {
736             KUrlRequester *req = ((Urlval*)m_valueItems.value(paramName))->urlwidget;
737             setValue = req->url().path();
738         } else if (type == "keywords"){
739             QLineEdit *line = ((Keywordval*)m_valueItems.value(paramName))->lineeditwidget;
740             QComboBox *combo = ((Keywordval*)m_valueItems.value(paramName))->comboboxwidget;
741             if(combo->currentIndex())
742             {
743                 QString comboval = combo->itemData(combo->currentIndex()).toString();
744                 line->insert(comboval);
745                 combo->setCurrentIndex(0);
746             }
747             setValue = line->text();
748         } else if (type == "fontfamily") {
749             QFontComboBox* fontfamily = ((Fontval*)m_valueItems.value(paramName))->fontfamilywidget;
750             setValue = fontfamily->currentFont().family();
751         }
752         if (!setValue.isNull())
753             pa.setAttribute("value", setValue);
754
755     }
756     emit parameterChanged(oldparam, m_effect, m_effect.attribute("kdenlive_ix").toInt());
757 }
758
759 QString ParameterContainer::getWipeString(wipeInfo info)
760 {
761
762     QString start;
763     QString end;
764     switch (info.start) {
765     case LEFT:
766         start = "-100%/0%:100%x100%";
767         break;
768     case RIGHT:
769         start = "100%/0%:100%x100%";
770         break;
771     case DOWN:
772         start = "0%/100%:100%x100%";
773         break;
774     case UP:
775         start = "0%/-100%:100%x100%";
776         break;
777     default:
778         start = "0%/0%:100%x100%";
779         break;
780     }
781     start.append(':' + QString::number(info.startTransparency));
782
783     switch (info.end) {
784     case LEFT:
785         end = "-100%/0%:100%x100%";
786         break;
787     case RIGHT:
788         end = "100%/0%:100%x100%";
789         break;
790     case DOWN:
791         end = "0%/100%:100%x100%";
792         break;
793     case UP:
794         end = "0%/-100%:100%x100%";
795         break;
796     default:
797         end = "0%/0%:100%x100%";
798         break;
799     }
800     end.append(':' + QString::number(info.endTransparency));
801     return QString(start + ";-1=" + end);
802 }
803
804 void ParameterContainer::updateParameter(const QString &key, const QString &value)
805 {
806     m_effect.setAttribute(key, value);
807 }
808
809 void ParameterContainer::slotStartFilterJobAction()
810 {
811     QDomNodeList namenode = m_effect.elementsByTagName("parameter");
812     for (int i = 0; i < namenode.count() ; i++) {
813         QDomElement pa = namenode.item(i).toElement();
814         QString type = pa.attribute("type");
815         if (type == "filterjob") {
816             emit startFilterJob(pa.attribute("filtertag"), pa.attribute("filterparams"), pa.attribute("finalfilter"), pa.attribute("consumer"), pa.attribute("consumerparams"), pa.attribute("wantedproperties"));
817             kDebug()<<" - - -PROPS:\n"<<pa.attribute("filtertag")<<"-"<< pa.attribute("filterparams")<<"-"<< pa.attribute("consumer")<<"-"<< pa.attribute("consumerparams")<<"-"<< pa.attribute("wantedproperties");
818             break;
819         }
820     }
821 }
822
823
824 void ParameterContainer::clearLayout(QLayout *layout)
825 {
826     QLayoutItem *item;
827     while((item = layout->takeAt(0))) {
828         if (item->layout()) {
829             clearLayout(item->layout());
830             delete item->layout();
831         }
832         if (item->widget()) {
833             delete item->widget();
834         }
835         delete item;
836     }
837 }
838
839 bool ParameterContainer::needsMonitorEffectScene()
840 {
841     return true;
842 }