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 "ui_urlval_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"
38 #include <KFileDialog>
40 #include <QVBoxLayout>
43 #include <QPushButton>
45 #include <QScrollArea>
48 class Boolval: public QWidget, public Ui::Boolval_UI
52 class Colorval: public QWidget, public Ui::Colorval_UI
56 class Constval: public QWidget, public Ui::Constval_UI
60 class Listval: public QWidget, public Ui::Listval_UI
64 class Wipeval: public QWidget, public Ui::Wipeval_UI
68 class Urlval: public QWidget, public Ui::Urlval_UI
72 QMap<QString, QImage> EffectStackEdit::iconCache;
74 EffectStackEdit::EffectStackEdit(QWidget *parent) :
78 m_frameSize(QPoint()),
79 m_keyframeEditor(NULL)
81 m_baseWidget = new QWidget(this);
82 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
83 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
84 setFrameStyle(QFrame::NoFrame);
85 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
87 setWidget(m_baseWidget);
88 setWidgetResizable(true);
89 m_vbox = new QVBoxLayout(m_baseWidget);
90 m_vbox->setContentsMargins(0, 0, 0, 0);
91 m_vbox->setSpacing(0);
95 EffectStackEdit::~EffectStackEdit()
101 void EffectStackEdit::setFrameSize(QPoint p)
104 QDomNodeList namenode = m_params.elementsByTagName("parameter");
105 for (int i = 0; i < namenode.count() ; i++) {
106 QDomNode pa = namenode.item(i);
107 QDomNode na = pa.firstChildElement("name");
108 QString type = pa.attributes().namedItem("type").nodeValue();
109 QString paramName = i18n(na.toElement().text().toUtf8().data());
111 if (type == "geometry") {
112 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
113 geom->setFrameSize(m_frameSize);
119 void EffectStackEdit::updateTimecodeFormat()
121 if (m_keyframeEditor)
122 m_keyframeEditor->updateTimecodeFormat();
123 QDomNodeList namenode = m_params.elementsByTagName("parameter");
124 for (int i = 0; i < namenode.count() ; i++) {
125 QDomNode pa = namenode.item(i);
126 QDomNode na = pa.firstChildElement("name");
127 QString type = pa.attributes().namedItem("type").nodeValue();
128 QString paramName = i18n(na.toElement().text().toUtf8().data());
130 if (type == "geometry") {
131 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
132 geom->updateTimecodeFormat();
135 if (type == "position") {
136 PositionEdit *posi = ((PositionEdit*)m_valueItems[paramName+"position"]);
137 posi->updateTimecodeFormat();
143 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
149 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
151 m_params.setAttribute(name, value);
154 void EffectStackEdit::transferParamDesc(const QDomElement d, int pos, int in, int out)
157 if (m_keyframeEditor) delete m_keyframeEditor;
158 m_keyframeEditor = NULL;
162 if (m_params.isNull()) {
163 kDebug() << "// EMPTY EFFECT STACK";
168 doc.appendChild(doc.importNode(m_params, true));
169 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
171 QDomNodeList namenode = m_params.elementsByTagName("parameter");
172 QDomElement e = m_params.toElement();
173 const int minFrame = e.attribute("start").toInt();
174 const int maxFrame = e.attribute("end").toInt();
177 for (int i = 0; i < namenode.count() ; i++) {
178 QDomElement pa = namenode.item(i).toElement();
179 QDomNode na = pa.firstChildElement("name");
180 QString type = pa.attribute("type");
181 QString paramName = i18n(na.toElement().text().toUtf8().data());
182 QWidget * toFillin = new QWidget(m_baseWidget);
183 QString value = pa.attribute("value").isNull() ?
184 pa.attribute("default") : pa.attribute("value");
186 /** Currently supported parameter types are:
187 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
188 * list: a combobox containing a list of values to choose
190 * complex: designed for keyframe parameters, but old and not finished, do not use
191 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
192 * keyframe: a list widget with a list of entries (position and value)
193 * color: a color chooser button
194 * position: a slider representing the position of a frame in the current clip
195 * curve: a single curve representing multiple points
196 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
199 if (type == "double" || type == "constant") {
202 if (pa.attribute("min").startsWith('%'))
203 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
205 min = pa.attribute("min").toInt();
206 if (pa.attribute("max").startsWith('%'))
207 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
209 max = pa.attribute("max").toInt();
210 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
213 } else if (type == "list") {
214 Listval *lsval = new Listval;
215 lsval->setupUi(toFillin);
216 QStringList listitems = pa.attribute("paramlist").split(',');
217 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
218 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
219 //lsval->list->addItems(listitems);
220 lsval->list->setIconSize(QSize(30, 30));
221 for (int i = 0; i < listitems.count(); i++) {
222 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
223 QString entry = listitems.at(i);
224 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
225 if (!EffectStackEdit::iconCache.contains(entry)) {
227 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
229 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
232 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
233 lsval->title->setTitle(paramName);
234 m_valueItems[paramName] = lsval;
235 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
236 m_uiItems.append(lsval);
237 } else if (type == "bool") {
238 Boolval *bval = new Boolval;
239 bval->setupUi(toFillin);
240 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
241 bval->checkBox->setText(paramName);
242 m_valueItems[paramName] = bval;
243 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
244 m_uiItems.append(bval);
245 } else if (type == "complex") {
246 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
247 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
248 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
249 QStringList val=value.split(';');
250 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
251 if ( (names.size() == max.size() ) &&
252 (names.size()== min.size()) &&
253 (names.size()== val.size()) )
255 for (int i=0;i< names.size();i++){
256 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
259 ComplexParameter *pl = new ComplexParameter;
260 pl->setupParam(d, pa.attribute("name"), 0, 100);
261 m_vbox->addWidget(pl);
262 m_valueItems[paramName+"complex"] = pl;
263 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
264 } else if (type == "geometry") {
265 Geometryval *geo = new Geometryval(m_profile, m_timecode, m_frameSize, pos);
266 if (minFrame == maxFrame)
267 geo->setupParam(pa, m_in, m_out);
269 geo->setupParam(pa, minFrame, maxFrame);
270 m_vbox->addWidget(geo);
271 m_valueItems[paramName+"geometry"] = geo;
272 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
273 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
274 } else if (type == "keyframe" || type == "simplekeyframe") {
275 //Â keyframe editor widget
276 kDebug() << "min: " << m_in << ", MAX: " << m_out;
277 if (m_keyframeEditor == NULL) {
278 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());
279 m_vbox->addWidget(geo);
280 m_valueItems[paramName+"keyframe"] = geo;
281 m_keyframeEditor = geo;
282 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
283 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
285 // we already have a keyframe editor, so just add another column for the new param
286 m_keyframeEditor->addParameter(pa);
288 } else if (type == "color") {
289 if (value.startsWith('#'))
290 value = value.replace('#', "0x");
292 ChooseColorWidget *choosecolor = new ChooseColorWidget(paramName, QColor(value.toUInt(&ok, 16)), this);
293 m_vbox->addWidget(choosecolor);
294 m_valueItems[paramName] = choosecolor;
295 connect(choosecolor, SIGNAL(modified()) , this, SLOT(collectAllParameters()));
296 } else if (type == "position") {
297 int pos = value.toInt();
298 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
300 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
301 // fadeout position starts from clip end
302 pos = m_out - (pos - m_in);
304 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
305 m_vbox->addWidget(posedit);
306 m_valueItems[paramName+"position"] = posedit;
307 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
308 } else if (type == "curve") {
309 KisCurveWidget *curve = new KisCurveWidget(this);
310 curve->setMaxPoints(pa.attribute("max").toInt());
311 QList<QPointF> points;
312 int number = EffectsList::parameter(e, pa.attribute("number")).toInt();
313 QString inName = pa.attribute("inpoints");
314 QString outName = pa.attribute("outpoints");
315 int start = pa.attribute("min").toInt();
316 for (int j = start; j <= number; j++) {
318 in.replace("%i", QString::number(j));
319 QString out = outName;
320 out.replace("%i", QString::number(j));
321 points << QPointF(EffectsList::parameter(e, in).toDouble(), EffectsList::parameter(e, out).toDouble());
323 if (!points.isEmpty())
324 curve->setCurve(KisCubicCurve(points));
325 QSpinBox *spinin = new QSpinBox();
326 spinin->setRange(0, 1000);
327 QSpinBox *spinout = new QSpinBox();
328 spinout->setRange(0, 1000);
329 curve->setupInOutControls(spinin, spinout, 0, 1000);
330 m_vbox->addWidget(curve);
331 m_vbox->addWidget(spinin);
332 m_vbox->addWidget(spinout);
333 connect(curve, SIGNAL(modified()), this, SLOT(collectAllParameters()));
334 m_valueItems[paramName] = curve;
335 } else if (type == "wipe") {
336 Wipeval *wpval = new Wipeval;
337 wpval->setupUi(toFillin);
338 wipeInfo w = getWipeInfo(value);
341 wpval->start_up->setChecked(true);
344 wpval->start_down->setChecked(true);
347 wpval->start_right->setChecked(true);
350 wpval->start_left->setChecked(true);
353 wpval->start_center->setChecked(true);
358 wpval->end_up->setChecked(true);
361 wpval->end_down->setChecked(true);
364 wpval->end_right->setChecked(true);
367 wpval->end_left->setChecked(true);
370 wpval->end_center->setChecked(true);
373 wpval->start_transp->setValue(w.startTransparency);
374 wpval->end_transp->setValue(w.endTransparency);
375 m_valueItems[paramName] = wpval;
376 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
377 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
378 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
379 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
380 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
381 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
382 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
383 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
384 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
385 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
386 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
387 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
388 //wpval->title->setTitle(na.toElement().text());
389 m_uiItems.append(wpval);
390 } else if (type == "url") {
391 Urlval *cval = new Urlval;
392 cval->setupUi(toFillin);
393 cval->label->setText(paramName);
394 cval->urlwidget->fileDialog()->setFilter(ProjectList::getExtensions());
395 m_valueItems[paramName] = cval;
396 cval->urlwidget->setUrl(KUrl(value));
397 connect(cval->urlwidget, SIGNAL(returnPressed()) , this, SLOT(collectAllParameters()));
398 connect(cval->urlwidget, SIGNAL(urlSelected(const KUrl&)) , this, SLOT(collectAllParameters()));
399 m_uiItems.append(cval);
406 m_vbox->addWidget(toFillin);
408 m_vbox->addStretch();
411 wipeInfo EffectStackEdit::getWipeInfo(QString value)
414 QString start = value.section(';', 0, 0);
415 QString end = value.section(';', 1, 1).section('=', 1, 1);
417 if (start.startsWith("-100%,0"))
419 else if (start.startsWith("100%,0"))
421 else if (start.startsWith("0%,100%"))
423 else if (start.startsWith("0%,-100%"))
428 if (start.count(':') == 2)
429 info.startTransparency = start.section(':', -1).toInt();
431 info.startTransparency = 100;
433 if (end.startsWith("-100%,0"))
435 else if (end.startsWith("100%,0"))
437 else if (end.startsWith("0%,100%"))
439 else if (end.startsWith("0%,-100%"))
444 if (end.count(':') == 2)
445 info.endTransparency = end.section(':', -1).toInt();
447 info.endTransparency = 100;
452 QString EffectStackEdit::getWipeString(wipeInfo info)
457 switch (info.start) {
459 start = "-100%,0%:100%x100%";
462 start = "100%,0%:100%x100%";
465 start = "0%,100%:100%x100%";
468 start = "0%,-100%:100%x100%";
471 start = "0%,0%:100%x100%";
474 start.append(':' + QString::number(info.startTransparency));
478 end = "-100%,0%:100%x100%";
481 end = "100%,0%:100%x100%";
484 end = "0%,100%:100%x100%";
487 end = "0%,-100%:100%x100%";
490 end = "0%,0%:100%x100%";
493 end.append(':' + QString::number(info.endTransparency));
494 return QString(start + ";-1=" + end);
497 void EffectStackEdit::collectAllParameters()
499 if (m_valueItems.isEmpty() || m_params.isNull()) return;
500 const QDomElement oldparam = m_params.cloneNode().toElement();
501 QDomElement newparam = oldparam.cloneNode().toElement();
502 QDomNodeList namenode = newparam.elementsByTagName("parameter");
504 for (int i = 0; i < namenode.count() ; i++) {
505 QDomNode pa = namenode.item(i);
506 QDomNode na = pa.firstChildElement("name");
507 QString type = pa.attributes().namedItem("type").nodeValue();
508 QString paramName = i18n(na.toElement().text().toUtf8().data());
509 if (type == "complex")
510 paramName.append("complex");
511 else if (type == "position")
512 paramName.append("position");
513 else if (type == "geometry")
514 paramName.append("geometry");
515 else if (type == "keyframe")
516 paramName.append("keyframe");
517 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
518 kDebug() << "// Param: " << paramName << " NOT FOUND";
523 if (type == "double" || type == "constant") {
524 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
525 setValue = QString::number(slider->value());
526 } else if (type == "list") {
527 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
528 setValue = box->itemData(box->currentIndex()).toString();
529 } else if (type == "bool") {
530 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
531 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
532 } else if (type == "color") {
533 ChooseColorWidget *choosecolor = ((ChooseColorWidget*)m_valueItems.value(paramName));
534 setValue = choosecolor->getColor().name();
535 } else if (type == "complex") {
536 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
537 namenode.item(i) = complex->getParamDesc();
538 } else if (type == "geometry") {
539 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
540 namenode.item(i).toElement().setAttribute("value", geom->getValue());
541 } else if (type == "position") {
542 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
543 int pos = pedit->getPosition();
544 setValue = QString::number(pos);
545 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
546 // Make sure duration is not longer than clip
549 pedit->setPosition(pos);
551 EffectsList::setParameter(newparam, "in", QString::number(m_in));
552 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
554 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
555 // Make sure duration is not longer than clip
558 pedit->setPosition(pos);
560 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
561 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
564 } else if (type == "curve") {
565 KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
566 QList<QPointF> points = curve->curve().points();
567 QString number = pa.attributes().namedItem("number").nodeValue();
568 QString inName = pa.attributes().namedItem("inpoints").nodeValue();
569 QString outName = pa.attributes().namedItem("outpoints").nodeValue();
570 int off = pa.attributes().namedItem("min").nodeValue().toInt();
571 int end = pa.attributes().namedItem("max").nodeValue().toInt();
572 EffectsList::setParameter(newparam, number, QString::number(points.count()));
573 for (int j = 0; (j < points.count() && j + off <= end); j++) {
575 in.replace("%i", QString::number(j + off));
576 QString out = outName;
577 out.replace("%i", QString::number(j + off));
578 EffectsList::setParameter(newparam, in, QString::number(points.at(j).x()));
579 EffectsList::setParameter(newparam, out, QString::number(points.at(j).y()));
581 } else if (type == "wipe") {
582 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
584 if (wp->start_left->isChecked())
586 else if (wp->start_right->isChecked())
588 else if (wp->start_up->isChecked())
590 else if (wp->start_down->isChecked())
592 else if (wp->start_center->isChecked())
596 info.startTransparency = wp->start_transp->value();
598 if (wp->end_left->isChecked())
600 else if (wp->end_right->isChecked())
602 else if (wp->end_up->isChecked())
604 else if (wp->end_down->isChecked())
606 else if (wp->end_center->isChecked())
610 info.endTransparency = wp->end_transp->value();
612 setValue = getWipeString(info);
613 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
614 QString realName = i18n(na.toElement().text().toUtf8().data());
615 QString val = m_keyframeEditor->getValue(realName);
616 kDebug() << "SET VALUE: " << val;
617 namenode.item(i).toElement().setAttribute("keyframes", val);
618 } else if (type == "url") {
619 KUrlRequester *req = ((Urlval*)m_valueItems.value(paramName))->urlwidget;
620 setValue = req->url().path();
623 if (!setValue.isNull())
624 pa.attributes().namedItem("value").setNodeValue(setValue);
627 emit parameterChanged(oldparam, newparam);
630 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
632 QWidget* toFillin = new QWidget(m_baseWidget);
633 Constval *ctval = new Constval;
634 ctval->setupUi(toFillin);
635 ctval->horizontalSlider->setMinimum(min);
636 ctval->horizontalSlider->setMaximum(max);
637 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
638 ctval->spinBox->setMinimum(min);
639 ctval->spinBox->setMaximum(max);
640 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
641 ctval->horizontalSlider->setValue(val);
642 ctval->label->setText(name);
643 m_valueItems[name] = ctval;
644 m_uiItems.append(ctval);
645 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
646 m_vbox->addWidget(toFillin);
649 void EffectStackEdit::slotSliderMoved(int)
651 collectAllParameters();
654 void EffectStackEdit::clearAllItems()
657 m_valueItems.clear();
659 /*while (!m_items.isEmpty()) {
660 QWidget *die = m_items.takeFirst();
664 //qDeleteAll(m_uiItems);
666 while ((child = m_vbox->takeAt(0)) != 0) {
667 QWidget *wid = child->widget();
671 m_keyframeEditor = NULL;