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