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"
37 #include <KFileDialog>
39 #include <QVBoxLayout>
42 #include <QPushButton>
44 #include <QScrollArea>
47 class Boolval: public QWidget, public Ui::Boolval_UI
51 class Colorval: public QWidget, public Ui::Colorval_UI
55 class Constval: public QWidget, public Ui::Constval_UI
59 class Listval: public QWidget, public Ui::Listval_UI
63 class Wipeval: public QWidget, public Ui::Wipeval_UI
67 class Urlval: public QWidget, public Ui::Urlval_UI
71 QMap<QString, QImage> EffectStackEdit::iconCache;
73 EffectStackEdit::EffectStackEdit(QWidget *parent) :
77 m_frameSize(QPoint()),
78 m_keyframeEditor(NULL)
80 m_baseWidget = new QWidget(this);
81 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
82 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
83 setFrameStyle(QFrame::NoFrame);
84 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
86 setWidget(m_baseWidget);
87 setWidgetResizable(true);
88 m_vbox = new QVBoxLayout(m_baseWidget);
89 m_vbox->setContentsMargins(0, 0, 0, 0);
90 m_vbox->setSpacing(0);
94 EffectStackEdit::~EffectStackEdit()
100 void EffectStackEdit::setFrameSize(QPoint p)
103 QDomNodeList namenode = m_params.elementsByTagName("parameter");
104 for (int i = 0; i < namenode.count() ; i++) {
105 QDomNode pa = namenode.item(i);
106 QDomNode na = pa.firstChildElement("name");
107 QString type = pa.attributes().namedItem("type").nodeValue();
108 QString paramName = i18n(na.toElement().text().toUtf8().data());
110 if (type == "geometry") {
111 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
112 geom->setFrameSize(m_frameSize);
118 void EffectStackEdit::updateTimecodeFormat()
120 if (m_keyframeEditor)
121 m_keyframeEditor->updateTimecodeFormat();
122 QDomNodeList namenode = m_params.elementsByTagName("parameter");
123 for (int i = 0; i < namenode.count() ; i++) {
124 QDomNode pa = namenode.item(i);
125 QDomNode na = pa.firstChildElement("name");
126 QString type = pa.attributes().namedItem("type").nodeValue();
127 QString paramName = i18n(na.toElement().text().toUtf8().data());
129 if (type == "geometry") {
130 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
131 geom->updateTimecodeFormat();
134 if (type == "position") {
135 PositionEdit *posi = ((PositionEdit*)m_valueItems[paramName+"position"]);
136 posi->updateTimecodeFormat();
142 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
148 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
150 m_params.setAttribute(name, value);
153 void EffectStackEdit::transferParamDesc(const QDomElement d, int pos, int in, int out)
156 if (m_keyframeEditor) delete m_keyframeEditor;
157 m_keyframeEditor = NULL;
161 if (m_params.isNull()) {
162 kDebug() << "// EMPTY EFFECT STACK";
167 doc.appendChild(doc.importNode(m_params, true));
168 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
170 QDomNodeList namenode = m_params.elementsByTagName("parameter");
171 QDomElement e = m_params.toElement();
172 const int minFrame = e.attribute("start").toInt();
173 const int maxFrame = e.attribute("end").toInt();
176 for (int i = 0; i < namenode.count() ; i++) {
177 QDomElement pa = namenode.item(i).toElement();
178 QDomNode na = pa.firstChildElement("name");
179 QString type = pa.attribute("type");
180 QString paramName = i18n(na.toElement().text().toUtf8().data());
181 QWidget * toFillin = new QWidget(m_baseWidget);
182 QString value = pa.attribute("value").isNull() ?
183 pa.attribute("default") : pa.attribute("value");
185 /** Currently supported parameter types are:
186 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
187 * list: a combobox containing a list of values to choose
189 * complex: designed for keyframe parameters, but old and not finished, do not use
190 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
191 * keyframe: a list widget with a list of entries (position and value)
192 * color: a color chooser button
193 * position: a slider representing the position of a frame in the current clip
194 * curve: a single curve representing multiple points
195 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
198 if (type == "double" || type == "constant") {
201 if (pa.attribute("min").startsWith('%'))
202 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
204 min = pa.attribute("min").toInt();
205 if (pa.attribute("max").startsWith('%'))
206 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
208 max = pa.attribute("max").toInt();
209 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
212 } else if (type == "list") {
213 Listval *lsval = new Listval;
214 lsval->setupUi(toFillin);
215 QStringList listitems = pa.attribute("paramlist").split(',');
216 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
217 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
218 //lsval->list->addItems(listitems);
219 lsval->list->setIconSize(QSize(30, 30));
220 for (int i = 0; i < listitems.count(); i++) {
221 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
222 QString entry = listitems.at(i);
223 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
224 if (!EffectStackEdit::iconCache.contains(entry)) {
226 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
228 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
231 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
232 lsval->title->setTitle(paramName);
233 m_valueItems[paramName] = lsval;
234 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
235 m_uiItems.append(lsval);
236 } else if (type == "bool") {
237 Boolval *bval = new Boolval;
238 bval->setupUi(toFillin);
239 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
240 bval->checkBox->setText(paramName);
241 m_valueItems[paramName] = bval;
242 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
243 m_uiItems.append(bval);
244 } else if (type == "complex") {
245 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
246 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
247 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
248 QStringList val=value.split(';');
249 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
250 if ( (names.size() == max.size() ) &&
251 (names.size()== min.size()) &&
252 (names.size()== val.size()) )
254 for (int i=0;i< names.size();i++){
255 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
258 ComplexParameter *pl = new ComplexParameter;
259 pl->setupParam(d, pa.attribute("name"), 0, 100);
260 m_vbox->addWidget(pl);
261 m_valueItems[paramName+"complex"] = pl;
262 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
263 } else if (type == "geometry") {
264 Geometryval *geo = new Geometryval(m_profile, m_timecode, m_frameSize, pos);
265 if (minFrame == maxFrame)
266 geo->setupParam(pa, m_in, m_out);
268 geo->setupParam(pa, minFrame, maxFrame);
269 m_vbox->addWidget(geo);
270 m_valueItems[paramName+"geometry"] = geo;
271 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
272 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
273 } else if (type == "keyframe" || type == "simplekeyframe") {
274 //Â keyframe editor widget
275 kDebug() << "min: " << m_in << ", MAX: " << m_out;
276 if (m_keyframeEditor == NULL) {
277 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());
278 m_vbox->addWidget(geo);
279 m_valueItems[paramName+"keyframe"] = geo;
280 m_keyframeEditor = geo;
281 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
282 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
284 // we already have a keyframe editor, so just add another column for the new param
285 m_keyframeEditor->addParameter(pa);
287 } else if (type == "color") {
288 Colorval *cval = new Colorval;
289 cval->setupUi(toFillin);
291 if (value.startsWith('#')) value = value.replace('#', "0x");
292 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
293 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
294 cval->label->setText(paramName);
295 m_valueItems[paramName] = cval;
296 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
297 m_uiItems.append(cval);
298 } else if (type == "position") {
299 int pos = value.toInt();
300 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
302 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
303 // fadeout position starts from clip end
304 pos = m_out - (pos - m_in);
306 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
307 m_vbox->addWidget(posedit);
308 m_valueItems[paramName+"position"] = posedit;
309 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
310 } else if (type == "curve") {
311 KisCurveWidget *curve = new KisCurveWidget(this);
312 curve->setMaxPoints(pa.attribute("max").toInt());
313 QList<QPointF> points;
314 int number = EffectsList::parameter(e, pa.attribute("number")).toInt();
315 QString inName = pa.attribute("inpoints");
316 QString outName = pa.attribute("outpoints");
317 int start = pa.attribute("min").toInt();
318 for (int j = start; j <= number; j++) {
320 in.replace("%i", QString::number(j));
321 QString out = outName;
322 out.replace("%i", QString::number(j));
323 points << QPointF(EffectsList::parameter(e, in).toDouble(), EffectsList::parameter(e, out).toDouble());
325 if (!points.isEmpty())
326 curve->setCurve(KisCubicCurve(points));
327 QSpinBox *spinin = new QSpinBox();
328 spinin->setRange(0, 1000);
329 QSpinBox *spinout = new QSpinBox();
330 spinout->setRange(0, 1000);
331 curve->setupInOutControls(spinin, spinout, 0, 1000);
332 m_vbox->addWidget(curve);
333 m_vbox->addWidget(spinin);
334 m_vbox->addWidget(spinout);
335 connect(curve, SIGNAL(modified()), this, SLOT(collectAllParameters()));
336 m_valueItems[paramName] = curve;
337 } else if (type == "wipe") {
338 Wipeval *wpval = new Wipeval;
339 wpval->setupUi(toFillin);
340 wipeInfo w = getWipeInfo(value);
343 wpval->start_up->setChecked(true);
346 wpval->start_down->setChecked(true);
349 wpval->start_right->setChecked(true);
352 wpval->start_left->setChecked(true);
355 wpval->start_center->setChecked(true);
360 wpval->end_up->setChecked(true);
363 wpval->end_down->setChecked(true);
366 wpval->end_right->setChecked(true);
369 wpval->end_left->setChecked(true);
372 wpval->end_center->setChecked(true);
375 wpval->start_transp->setValue(w.startTransparency);
376 wpval->end_transp->setValue(w.endTransparency);
377 m_valueItems[paramName] = wpval;
378 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
379 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
380 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
381 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
382 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
383 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
384 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
385 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
386 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
387 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
388 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
389 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
390 //wpval->title->setTitle(na.toElement().text());
391 m_uiItems.append(wpval);
392 } else if (type == "url") {
393 Urlval *cval = new Urlval;
394 cval->setupUi(toFillin);
395 cval->label->setText(paramName);
396 cval->urlwidget->fileDialog()->setFilter(ProjectList::getExtensions());
397 m_valueItems[paramName] = cval;
398 cval->urlwidget->setUrl(KUrl(value));
399 connect(cval->urlwidget, SIGNAL(returnPressed()) , this, SLOT(collectAllParameters()));
400 connect(cval->urlwidget, SIGNAL(urlSelected(const KUrl&)) , this, SLOT(collectAllParameters()));
401 m_uiItems.append(cval);
408 m_vbox->addWidget(toFillin);
410 m_vbox->addStretch();
413 wipeInfo EffectStackEdit::getWipeInfo(QString value)
416 QString start = value.section(';', 0, 0);
417 QString end = value.section(';', 1, 1).section('=', 1, 1);
419 if (start.startsWith("-100%,0"))
421 else if (start.startsWith("100%,0"))
423 else if (start.startsWith("0%,100%"))
425 else if (start.startsWith("0%,-100%"))
430 if (start.count(':') == 2)
431 info.startTransparency = start.section(':', -1).toInt();
433 info.startTransparency = 100;
435 if (end.startsWith("-100%,0"))
437 else if (end.startsWith("100%,0"))
439 else if (end.startsWith("0%,100%"))
441 else if (end.startsWith("0%,-100%"))
446 if (end.count(':') == 2)
447 info.endTransparency = end.section(':', -1).toInt();
449 info.endTransparency = 100;
454 QString EffectStackEdit::getWipeString(wipeInfo info)
459 switch (info.start) {
461 start = "-100%,0%:100%x100%";
464 start = "100%,0%:100%x100%";
467 start = "0%,100%:100%x100%";
470 start = "0%,-100%:100%x100%";
473 start = "0%,0%:100%x100%";
476 start.append(':' + QString::number(info.startTransparency));
480 end = "-100%,0%:100%x100%";
483 end = "100%,0%:100%x100%";
486 end = "0%,100%:100%x100%";
489 end = "0%,-100%:100%x100%";
492 end = "0%,0%:100%x100%";
495 end.append(':' + QString::number(info.endTransparency));
496 return QString(start + ";-1=" + end);
499 void EffectStackEdit::collectAllParameters()
501 if (m_valueItems.isEmpty() || m_params.isNull()) return;
502 const QDomElement oldparam = m_params.cloneNode().toElement();
503 QDomElement newparam = oldparam.cloneNode().toElement();
504 QDomNodeList namenode = newparam.elementsByTagName("parameter");
506 for (int i = 0; i < namenode.count() ; i++) {
507 QDomNode pa = namenode.item(i);
508 QDomNode na = pa.firstChildElement("name");
509 QString type = pa.attributes().namedItem("type").nodeValue();
510 QString paramName = i18n(na.toElement().text().toUtf8().data());
511 if (type == "complex")
512 paramName.append("complex");
513 else if (type == "position")
514 paramName.append("position");
515 else if (type == "geometry")
516 paramName.append("geometry");
517 else if (type == "keyframe")
518 paramName.append("keyframe");
519 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
520 kDebug() << "// Param: " << paramName << " NOT FOUND";
525 if (type == "double" || type == "constant") {
526 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
527 setValue = QString::number(slider->value());
528 } else if (type == "list") {
529 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
530 setValue = box->itemData(box->currentIndex()).toString();
531 } else if (type == "bool") {
532 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
533 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
534 } else if (type == "color") {
535 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
536 setValue = color->color().name();
537 } else if (type == "complex") {
538 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
539 namenode.item(i) = complex->getParamDesc();
540 } else if (type == "geometry") {
541 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
542 namenode.item(i).toElement().setAttribute("value", geom->getValue());
543 } else if (type == "position") {
544 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
545 int pos = pedit->getPosition();
546 setValue = QString::number(pos);
547 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
548 // Make sure duration is not longer than clip
551 pedit->setPosition(pos);
553 EffectsList::setParameter(newparam, "in", QString::number(m_in));
554 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
556 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
557 // Make sure duration is not longer than clip
560 pedit->setPosition(pos);
562 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
563 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
566 } else if (type == "curve") {
567 KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
568 QList<QPointF> points = curve->curve().points();
569 QString number = pa.attributes().namedItem("number").nodeValue();
570 QString inName = pa.attributes().namedItem("inpoints").nodeValue();
571 QString outName = pa.attributes().namedItem("outpoints").nodeValue();
572 int off = pa.attributes().namedItem("min").nodeValue().toInt();
573 int end = pa.attributes().namedItem("max").nodeValue().toInt();
574 EffectsList::setParameter(newparam, number, QString::number(points.count()));
575 for (int j = 0; (j < points.count() && j + off <= end); j++) {
577 in.replace("%i", QString::number(j + off));
578 QString out = outName;
579 out.replace("%i", QString::number(j + off));
580 EffectsList::setParameter(newparam, in, QString::number(points.at(j).x()));
581 EffectsList::setParameter(newparam, out, QString::number(points.at(j).y()));
583 } else if (type == "wipe") {
584 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
586 if (wp->start_left->isChecked())
588 else if (wp->start_right->isChecked())
590 else if (wp->start_up->isChecked())
592 else if (wp->start_down->isChecked())
594 else if (wp->start_center->isChecked())
598 info.startTransparency = wp->start_transp->value();
600 if (wp->end_left->isChecked())
602 else if (wp->end_right->isChecked())
604 else if (wp->end_up->isChecked())
606 else if (wp->end_down->isChecked())
608 else if (wp->end_center->isChecked())
612 info.endTransparency = wp->end_transp->value();
614 setValue = getWipeString(info);
615 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
616 QString realName = i18n(na.toElement().text().toUtf8().data());
617 QString val = m_keyframeEditor->getValue(realName);
618 kDebug() << "SET VALUE: " << val;
619 namenode.item(i).toElement().setAttribute("keyframes", val);
620 } else if (type == "url") {
621 KUrlRequester *req = ((Urlval*)m_valueItems.value(paramName))->urlwidget;
622 setValue = req->url().path();
625 if (!setValue.isNull())
626 pa.attributes().namedItem("value").setNodeValue(setValue);
629 emit parameterChanged(oldparam, newparam);
632 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
634 QWidget* toFillin = new QWidget(m_baseWidget);
635 Constval *ctval = new Constval;
636 ctval->setupUi(toFillin);
637 ctval->horizontalSlider->setMinimum(min);
638 ctval->horizontalSlider->setMaximum(max);
639 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
640 ctval->spinBox->setMinimum(min);
641 ctval->spinBox->setMaximum(max);
642 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
643 ctval->horizontalSlider->setValue(val);
644 ctval->label->setText(name);
645 m_valueItems[name] = ctval;
646 m_uiItems.append(ctval);
647 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
648 m_vbox->addWidget(toFillin);
651 void EffectStackEdit::slotSliderMoved(int)
653 collectAllParameters();
656 void EffectStackEdit::clearAllItems()
659 m_valueItems.clear();
661 /*while (!m_items.isEmpty()) {
662 QWidget *die = m_items.takeFirst();
666 //qDeleteAll(m_uiItems);
668 while ((child = m_vbox->takeAt(0)) != 0) {
669 QWidget *wid = child->widget();
673 m_keyframeEditor = NULL;