1 /***************************************************************************
2 effecstackview.cpp - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
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 "complexparameter.h"
25 #include "geometryval.h"
26 #include "positionedit.h"
27 #include "effectslist.h"
28 #include "kdenlivesettings.h"
29 #include "profilesdialog.h"
30 #include "kis_curve_widget.h"
31 #include "kis_cubic_curve.h"
36 #include <QVBoxLayout>
39 #include <QPushButton>
41 #include <QScrollArea>
44 class Boolval: public QWidget, public Ui::Boolval_UI
48 class Colorval: public QWidget, public Ui::Colorval_UI
52 class Constval: public QWidget, public Ui::Constval_UI
56 class Listval: public QWidget, public Ui::Listval_UI
60 class Wipeval: public QWidget, public Ui::Wipeval_UI
65 QMap<QString, QImage> EffectStackEdit::iconCache;
67 EffectStackEdit::EffectStackEdit(QWidget *parent) :
71 m_frameSize(QPoint()),
72 m_keyframeEditor(NULL)
74 m_baseWidget = new QWidget(this);
75 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
76 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
77 setFrameStyle(QFrame::NoFrame);
78 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
80 setWidget(m_baseWidget);
81 setWidgetResizable(true);
82 m_vbox = new QVBoxLayout(m_baseWidget);
83 m_vbox->setContentsMargins(0, 0, 0, 0);
84 m_vbox->setSpacing(0);
88 EffectStackEdit::~EffectStackEdit()
94 void EffectStackEdit::setFrameSize(QPoint p)
97 QDomNodeList namenode = m_params.elementsByTagName("parameter");
98 for (int i = 0; i < namenode.count() ; i++) {
99 QDomNode pa = namenode.item(i);
100 QDomNode na = pa.firstChildElement("name");
101 QString type = pa.attributes().namedItem("type").nodeValue();
102 QString paramName = i18n(na.toElement().text().toUtf8().data());
104 if (type == "geometry") {
105 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
106 geom->setFrameSize(m_frameSize);
113 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
119 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
121 m_params.setAttribute(name, value);
124 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
127 if (m_keyframeEditor) delete m_keyframeEditor;
128 m_keyframeEditor = NULL;
132 if (m_params.isNull()) {
133 kDebug() << "// EMPTY EFFECT STACK";
138 doc.appendChild(doc.importNode(m_params, true));
139 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
141 QDomNodeList namenode = m_params.elementsByTagName("parameter");
142 QDomElement e = m_params.toElement();
143 const int minFrame = e.attribute("start").toInt();
144 const int maxFrame = e.attribute("end").toInt();
147 for (int i = 0; i < namenode.count() ; i++) {
148 QDomElement pa = namenode.item(i).toElement();
149 QDomNode na = pa.firstChildElement("name");
150 QString type = pa.attribute("type");
151 QString paramName = i18n(na.toElement().text().toUtf8().data());
152 QWidget * toFillin = new QWidget(m_baseWidget);
153 QString value = pa.attribute("value").isNull() ?
154 pa.attribute("default") : pa.attribute("value");
156 /** Currently supported parameter types are:
157 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
158 * list: a combobox containing a list of values to choose
160 * complex: designed for keyframe parameters, but old and not finished, do not use
161 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
162 * keyframe: a list widget with a list of entries (position and value)
163 * color: a color chooser button
164 * position: a slider representing the position of a frame in the current clip
165 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
168 if (type == "double" || type == "constant") {
171 if (pa.attribute("min").startsWith('%')) {
172 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
173 } else min = pa.attribute("min").toInt();
174 if (pa.attribute("max").startsWith('%')) {
175 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
176 } else max = pa.attribute("max").toInt();
177 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
180 } else if (type == "list") {
181 Listval *lsval = new Listval;
182 lsval->setupUi(toFillin);
183 QStringList listitems = pa.attribute("paramlist").split(',');
184 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
185 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
186 //lsval->list->addItems(listitems);
187 lsval->list->setIconSize(QSize(30, 30));
188 for (int i = 0; i < listitems.count(); i++) {
189 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
190 QString entry = listitems.at(i);
191 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
192 if (!EffectStackEdit::iconCache.contains(entry)) {
194 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
196 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
199 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
200 lsval->title->setTitle(paramName);
201 m_valueItems[paramName] = lsval;
202 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
203 m_uiItems.append(lsval);
204 } else if (type == "bool") {
205 Boolval *bval = new Boolval;
206 bval->setupUi(toFillin);
207 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
208 bval->checkBox->setText(paramName);
209 m_valueItems[paramName] = bval;
210 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
211 m_uiItems.append(bval);
212 } else if (type == "complex") {
213 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
214 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
215 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
216 QStringList val=value.split(';');
217 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
218 if ( (names.size() == max.size() ) &&
219 (names.size()== min.size()) &&
220 (names.size()== val.size()) )
222 for (int i=0;i< names.size();i++){
223 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
226 ComplexParameter *pl = new ComplexParameter;
227 pl->setupParam(d, pa.attribute("name"), 0, 100);
228 m_vbox->addWidget(pl);
229 m_valueItems[paramName+"complex"] = pl;
230 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
231 } else if (type == "geometry") {
232 Geometryval *geo = new Geometryval(m_profile, m_frameSize, m_in);
233 geo->setupParam(pa, minFrame, maxFrame);
234 m_vbox->addWidget(geo);
235 m_valueItems[paramName+"geometry"] = geo;
236 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
237 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
238 } else if (type == "keyframe" || type == "simplekeyframe") {
239 // keyframe editor widget
240 kDebug() << "min: " << m_in << ", MAX: " << m_out;
241 if (m_keyframeEditor == NULL) {
242 KeyframeEdit *geo = new KeyframeEdit(pa, m_in, m_in + m_out, pa.attribute("min").toInt(), pa.attribute("max").toInt(), m_timecode, e.attribute("active_keyframe", "-1").toInt());
243 m_vbox->addWidget(geo);
244 m_valueItems[paramName+"keyframe"] = geo;
245 m_keyframeEditor = geo;
246 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
247 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
249 // we already have a keyframe editor, so just add another column for the new param
250 m_keyframeEditor->addParameter(pa);
252 } else if (type == "color") {
253 Colorval *cval = new Colorval;
254 cval->setupUi(toFillin);
256 if (value.startsWith('#')) value = value.replace('#', "0x");
257 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
258 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
259 cval->label->setText(paramName);
260 m_valueItems[paramName] = cval;
261 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
262 m_uiItems.append(cval);
263 } else if (type == "position") {
264 int pos = value.toInt();
265 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
267 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
268 // fadeout position starts from clip end
269 pos = m_out - (pos - m_in);
271 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
272 m_vbox->addWidget(posedit);
273 m_valueItems[paramName+"position"] = posedit;
274 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
275 } else if (type == "curve") {
276 KisCurveWidget *curve = new KisCurveWidget(this);
277 QList<QPointF> points;
278 int number = EffectsList::parameter(e, pa.attribute("number")).toInt();
279 QString inName = pa.attribute("inpoints");
280 QString outName = pa.attribute("outpoints");
281 int start = pa.attribute("min").toInt();
282 for (int j = start; j <= number; j++) {
284 in.replace("%i", QString::number(j));
285 QString out = outName;
286 out.replace("%i", QString::number(j));
287 points << QPointF(EffectsList::parameter(e, in).toDouble(), EffectsList::parameter(e, out).toDouble());
289 if (!points.isEmpty()) curve->setCurve(KisCubicCurve(points));
290 m_vbox->addWidget(curve);
291 connect(curve, SIGNAL(modified()), this, SLOT(collectAllParameters()));
292 m_valueItems[paramName] = curve;
293 } else if (type == "wipe") {
294 Wipeval *wpval = new Wipeval;
295 wpval->setupUi(toFillin);
296 wipeInfo w = getWipeInfo(value);
299 wpval->start_up->setChecked(true);
302 wpval->start_down->setChecked(true);
305 wpval->start_right->setChecked(true);
308 wpval->start_left->setChecked(true);
311 wpval->start_center->setChecked(true);
316 wpval->end_up->setChecked(true);
319 wpval->end_down->setChecked(true);
322 wpval->end_right->setChecked(true);
325 wpval->end_left->setChecked(true);
328 wpval->end_center->setChecked(true);
331 wpval->start_transp->setValue(w.startTransparency);
332 wpval->end_transp->setValue(w.endTransparency);
333 m_valueItems[paramName] = wpval;
334 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
335 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
336 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
337 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
338 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
339 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
340 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
341 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
342 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
343 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
344 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
345 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
346 //wpval->title->setTitle(na.toElement().text());
347 m_uiItems.append(wpval);
354 m_vbox->addWidget(toFillin);
357 m_vbox->addStretch();
360 void EffectStackEdit::slotSeekToPos(int pos)
362 emit seekTimeline(pos);
365 wipeInfo EffectStackEdit::getWipeInfo(QString value)
368 QString start = value.section(';', 0, 0);
369 QString end = value.section(';', 1, 1).section('=', 1, 1);
370 if (start.startsWith("-100%,0")) info.start = LEFT;
371 else if (start.startsWith("100%,0")) info.start = RIGHT;
372 else if (start.startsWith("0%,100%")) info.start = DOWN;
373 else if (start.startsWith("0%,-100%")) info.start = UP;
374 else info.start = CENTER;
375 if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
376 else info.startTransparency = 100;
378 if (end.startsWith("-100%,0")) info.end = LEFT;
379 else if (end.startsWith("100%,0")) info.end = RIGHT;
380 else if (end.startsWith("0%,100%")) info.end = DOWN;
381 else if (end.startsWith("0%,-100%")) info.end = UP;
382 else info.end = CENTER;
383 if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
384 else info.endTransparency = 100;
388 QString EffectStackEdit::getWipeString(wipeInfo info)
393 switch (info.start) {
395 start = "-100%,0%:100%x100%";
398 start = "100%,0%:100%x100%";
401 start = "0%,100%:100%x100%";
404 start = "0%,-100%:100%x100%";
407 start = "0%,0%:100%x100%";
410 start.append(':' + QString::number(info.startTransparency));
414 end = "-100%,0%:100%x100%";
417 end = "100%,0%:100%x100%";
420 end = "0%,100%:100%x100%";
423 end = "0%,-100%:100%x100%";
426 end = "0%,0%:100%x100%";
429 end.append(':' + QString::number(info.endTransparency));
430 return QString(start + ";-1=" + end);
433 void EffectStackEdit::collectAllParameters()
435 if (m_valueItems.isEmpty() || m_params.isNull()) return;
436 const QDomElement oldparam = m_params.cloneNode().toElement();
437 QDomElement newparam = oldparam.cloneNode().toElement();
438 QDomNodeList namenode = newparam.elementsByTagName("parameter");
440 for (int i = 0; i < namenode.count() ; i++) {
441 QDomNode pa = namenode.item(i);
442 QDomNode na = pa.firstChildElement("name");
443 QString type = pa.attributes().namedItem("type").nodeValue();
444 QString paramName = i18n(na.toElement().text().toUtf8().data());
445 if (type == "complex") paramName.append("complex");
446 else if (type == "position") paramName.append("position");
447 else if (type == "geometry") paramName.append("geometry");
448 else if (type == "keyframe") paramName.append("keyframe");
449 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
450 kDebug() << "// Param: " << paramName << " NOT FOUND";
455 if (type == "double" || type == "constant") {
456 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
457 setValue = QString::number(slider->value());
458 } else if (type == "list") {
459 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
460 setValue = box->itemData(box->currentIndex()).toString();
461 } else if (type == "bool") {
462 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
463 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
464 } else if (type == "color") {
465 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
466 setValue = color->color().name();
467 } else if (type == "complex") {
468 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
469 namenode.item(i) = complex->getParamDesc();
470 } else if (type == "geometry") {
471 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
472 namenode.item(i).toElement().setAttribute("value", geom->getValue());
473 } else if (type == "position") {
474 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
475 int pos = pedit->getPosition();
476 setValue = QString::number(pos);
477 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
478 // Make sure duration is not longer than clip
481 pedit->setPosition(pos);
483 EffectsList::setParameter(newparam, "in", QString::number(m_in));
484 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
486 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
487 // Make sure duration is not longer than clip
490 pedit->setPosition(pos);
492 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
493 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
496 } else if (type == "curve") {
497 KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
498 QList<QPointF> points = curve->curve().points();
499 QString number = pa.attributes().namedItem("number").nodeValue();
500 QString inName = pa.attributes().namedItem("inpoints").nodeValue();
501 QString outName = pa.attributes().namedItem("outpoints").nodeValue();
502 int off = pa.attributes().namedItem("min").nodeValue().toInt();
503 int end = pa.attributes().namedItem("max").nodeValue().toInt();
504 EffectsList::setParameter(newparam, number, QString::number(points.count()));
505 for (int j = 0; (j < points.count() && j + off <= end); j++) {
507 in.replace("%i", QString::number(j+off));
508 QString out = outName;
509 out.replace("%i", QString::number(j+off));
510 EffectsList::setParameter(newparam, in, QString::number(points.at(j).x()));
511 EffectsList::setParameter(newparam, out, QString::number(points.at(j).y()));
513 } else if (type == "wipe") {
514 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
516 if (wp->start_left->isChecked()) info.start = LEFT;
517 else if (wp->start_right->isChecked()) info.start = RIGHT;
518 else if (wp->start_up->isChecked()) info.start = UP;
519 else if (wp->start_down->isChecked()) info.start = DOWN;
520 else if (wp->start_center->isChecked()) info.start = CENTER;
521 else info.start = LEFT;
522 info.startTransparency = wp->start_transp->value();
523 if (wp->end_left->isChecked()) info.end = LEFT;
524 else if (wp->end_right->isChecked()) info.end = RIGHT;
525 else if (wp->end_up->isChecked()) info.end = UP;
526 else if (wp->end_down->isChecked()) info.end = DOWN;
527 else if (wp->end_center->isChecked()) info.end = CENTER;
528 else info.end = RIGHT;
529 info.endTransparency = wp->end_transp->value();
530 setValue = getWipeString(info);
531 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
532 QString realName = i18n(na.toElement().text().toUtf8().data());
533 QString val = m_keyframeEditor->getValue(realName);
534 kDebug() << "SET VALUE: " << val;
535 namenode.item(i).toElement().setAttribute("keyframes", val);
537 if (!setValue.isNull()) {
538 pa.attributes().namedItem("value").setNodeValue(setValue);
541 emit parameterChanged(oldparam, newparam);
544 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
546 QWidget* toFillin = new QWidget(m_baseWidget);
547 Constval *ctval = new Constval;
548 ctval->setupUi(toFillin);
549 ctval->horizontalSlider->setMinimum(min);
550 ctval->horizontalSlider->setMaximum(max);
551 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
552 ctval->spinBox->setMinimum(min);
553 ctval->spinBox->setMaximum(max);
554 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
555 ctval->horizontalSlider->setValue(val);
556 ctval->label->setText(name);
557 m_valueItems[name] = ctval;
558 m_uiItems.append(ctval);
559 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
560 m_vbox->addWidget(toFillin);
563 void EffectStackEdit::slotSliderMoved(int)
565 collectAllParameters();
568 void EffectStackEdit::clearAllItems()
571 m_valueItems.clear();
573 /*while (!m_items.isEmpty()) {
574 QWidget *die = m_items.takeFirst();
578 //qDeleteAll(m_uiItems);
580 while ((child = m_vbox->takeAt(0)) != 0) {
581 QWidget *wid = child->widget();
585 m_keyframeEditor = NULL;