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_wipeval_ui.h"
23 #include "ui_urlval_ui.h"
24 #include "complexparameter.h"
25 #include "geometryval.h"
26 #include "positionedit.h"
27 #include "projectlist.h"
28 #include "effectslist.h"
29 #include "kdenlivesettings.h"
30 #include "profilesdialog.h"
31 #include "kis_curve_widget.h"
32 #include "kis_cubic_curve.h"
33 #include "choosecolorwidget.h"
37 #include <KFileDialog>
39 #include <QVBoxLayout>
42 #include <QPushButton>
44 #include <QScrollArea>
47 class Boolval: public QWidget, public Ui::Boolval_UI
51 class Constval: public QWidget, public Ui::Constval_UI
55 class Listval: public QWidget, public Ui::Listval_UI
59 class Wipeval: public QWidget, public Ui::Wipeval_UI
63 class Urlval: public QWidget, public Ui::Urlval_UI
67 QMap<QString, QImage> EffectStackEdit::iconCache;
69 EffectStackEdit::EffectStackEdit(QWidget *parent) :
73 m_frameSize(QPoint()),
74 m_keyframeEditor(NULL)
76 m_baseWidget = new QWidget(this);
77 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
78 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
79 setFrameStyle(QFrame::NoFrame);
80 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
82 setWidget(m_baseWidget);
83 setWidgetResizable(true);
84 m_vbox = new QVBoxLayout(m_baseWidget);
85 m_vbox->setContentsMargins(0, 0, 0, 0);
86 m_vbox->setSpacing(0);
90 EffectStackEdit::~EffectStackEdit()
96 void EffectStackEdit::setFrameSize(QPoint p)
99 QDomNodeList namenode = m_params.elementsByTagName("parameter");
100 for (int i = 0; i < namenode.count() ; i++) {
101 QDomNode pa = namenode.item(i);
102 QDomNode na = pa.firstChildElement("name");
103 QString type = pa.attributes().namedItem("type").nodeValue();
104 QString paramName = i18n(na.toElement().text().toUtf8().data());
106 if (type == "geometry") {
107 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
108 geom->setFrameSize(m_frameSize);
114 void EffectStackEdit::updateTimecodeFormat()
116 if (m_keyframeEditor)
117 m_keyframeEditor->updateTimecodeFormat();
118 QDomNodeList namenode = m_params.elementsByTagName("parameter");
119 for (int i = 0; i < namenode.count() ; i++) {
120 QDomNode pa = namenode.item(i);
121 QDomNode na = pa.firstChildElement("name");
122 QString type = pa.attributes().namedItem("type").nodeValue();
123 QString paramName = i18n(na.toElement().text().toUtf8().data());
125 if (type == "geometry") {
126 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
127 geom->updateTimecodeFormat();
130 if (type == "position") {
131 PositionEdit *posi = ((PositionEdit*)m_valueItems[paramName+"position"]);
132 posi->updateTimecodeFormat();
138 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
144 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
146 m_params.setAttribute(name, value);
149 void EffectStackEdit::transferParamDesc(const QDomElement d, int pos, int in, int out)
152 if (m_keyframeEditor) delete m_keyframeEditor;
153 m_keyframeEditor = NULL;
157 if (m_params.isNull()) {
158 kDebug() << "// EMPTY EFFECT STACK";
163 doc.appendChild(doc.importNode(m_params, true));
164 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
166 QDomNodeList namenode = m_params.elementsByTagName("parameter");
167 QDomElement e = m_params.toElement();
168 const int minFrame = e.attribute("start").toInt();
169 const int maxFrame = e.attribute("end").toInt();
172 for (int i = 0; i < namenode.count() ; i++) {
173 QDomElement pa = namenode.item(i).toElement();
174 QDomNode na = pa.firstChildElement("name");
175 QString type = pa.attribute("type");
176 QString paramName = i18n(na.toElement().text().toUtf8().data());
177 QWidget * toFillin = new QWidget(m_baseWidget);
178 QString value = pa.attribute("value").isNull() ?
179 pa.attribute("default") : pa.attribute("value");
181 /** Currently supported parameter types are:
182 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
183 * list: a combobox containing a list of values to choose
185 * complex: designed for keyframe parameters, but old and not finished, do not use
186 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
187 * keyframe: a list widget with a list of entries (position and value)
188 * color: a color chooser button
189 * position: a slider representing the position of a frame in the current clip
190 * curve: a single curve representing multiple points
191 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
194 if (type == "double" || type == "constant") {
197 if (pa.attribute("min").startsWith('%'))
198 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
200 min = pa.attribute("min").toInt();
201 if (pa.attribute("max").startsWith('%'))
202 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
204 max = pa.attribute("max").toInt();
205 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
208 } else if (type == "list") {
209 Listval *lsval = new Listval;
210 lsval->setupUi(toFillin);
211 QStringList listitems = pa.attribute("paramlist").split(',');
212 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
213 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
214 //lsval->list->addItems(listitems);
215 lsval->list->setIconSize(QSize(30, 30));
216 for (int i = 0; i < listitems.count(); i++) {
217 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
218 QString entry = listitems.at(i);
219 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
220 if (!EffectStackEdit::iconCache.contains(entry)) {
222 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
224 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
227 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
228 lsval->title->setTitle(paramName);
229 m_valueItems[paramName] = lsval;
230 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
231 m_uiItems.append(lsval);
232 } else if (type == "bool") {
233 Boolval *bval = new Boolval;
234 bval->setupUi(toFillin);
235 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
236 bval->checkBox->setText(paramName);
237 m_valueItems[paramName] = bval;
238 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
239 m_uiItems.append(bval);
240 } else if (type == "complex") {
241 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
242 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
243 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
244 QStringList val=value.split(';');
245 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
246 if ( (names.size() == max.size() ) &&
247 (names.size()== min.size()) &&
248 (names.size()== val.size()) )
250 for (int i=0;i< names.size();i++){
251 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
254 ComplexParameter *pl = new ComplexParameter;
255 pl->setupParam(d, pa.attribute("name"), 0, 100);
256 m_vbox->addWidget(pl);
257 m_valueItems[paramName+"complex"] = pl;
258 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
259 } else if (type == "geometry") {
260 Geometryval *geo = new Geometryval(m_profile, m_timecode, m_frameSize, pos);
261 if (minFrame == maxFrame)
262 geo->setupParam(pa, m_in, m_out);
264 geo->setupParam(pa, minFrame, maxFrame);
265 m_vbox->addWidget(geo);
266 m_valueItems[paramName+"geometry"] = geo;
267 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
268 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
269 } else if (type == "keyframe" || type == "simplekeyframe") {
270 //Â keyframe editor widget
271 kDebug() << "min: " << m_in << ", MAX: " << m_out;
272 if (m_keyframeEditor == NULL) {
273 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());
274 m_vbox->addWidget(geo);
275 m_valueItems[paramName+"keyframe"] = geo;
276 m_keyframeEditor = geo;
277 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
278 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
280 // we already have a keyframe editor, so just add another column for the new param
281 m_keyframeEditor->addParameter(pa);
283 } else if (type == "color") {
284 if (value.startsWith('#'))
285 value = value.replace('#', "0x");
287 ChooseColorWidget *choosecolor = new ChooseColorWidget(paramName, QColor(value.toUInt(&ok, 16)), this);
288 m_vbox->addWidget(choosecolor);
289 m_valueItems[paramName] = choosecolor;
290 connect(choosecolor, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
291 connect(choosecolor, SIGNAL(modified()) , this, SLOT(collectAllParameters()));
292 } else if (type == "position") {
293 int pos = value.toInt();
294 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
296 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
297 // fadeout position starts from clip end
298 pos = m_out - (pos - m_in);
300 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
301 m_vbox->addWidget(posedit);
302 m_valueItems[paramName+"position"] = posedit;
303 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
304 } else if (type == "curve") {
305 KisCurveWidget *curve = new KisCurveWidget(this);
306 curve->setMaxPoints(pa.attribute("max").toInt());
307 QList<QPointF> points;
308 int number = EffectsList::parameter(e, pa.attribute("number")).toInt();
309 QString inName = pa.attribute("inpoints");
310 QString outName = pa.attribute("outpoints");
311 int start = pa.attribute("min").toInt();
312 for (int j = start; j <= number; j++) {
314 in.replace("%i", QString::number(j));
315 QString out = outName;
316 out.replace("%i", QString::number(j));
317 points << QPointF(EffectsList::parameter(e, in).toDouble(), EffectsList::parameter(e, out).toDouble());
319 if (!points.isEmpty())
320 curve->setCurve(KisCubicCurve(points));
321 QSpinBox *spinin = new QSpinBox();
322 spinin->setRange(0, 1000);
323 QSpinBox *spinout = new QSpinBox();
324 spinout->setRange(0, 1000);
325 curve->setupInOutControls(spinin, spinout, 0, 1000);
326 m_vbox->addWidget(curve);
327 m_vbox->addWidget(spinin);
328 m_vbox->addWidget(spinout);
329 connect(curve, SIGNAL(modified()), this, SLOT(collectAllParameters()));
330 m_valueItems[paramName] = curve;
331 } else if (type == "wipe") {
332 Wipeval *wpval = new Wipeval;
333 wpval->setupUi(toFillin);
334 wipeInfo w = getWipeInfo(value);
337 wpval->start_up->setChecked(true);
340 wpval->start_down->setChecked(true);
343 wpval->start_right->setChecked(true);
346 wpval->start_left->setChecked(true);
349 wpval->start_center->setChecked(true);
354 wpval->end_up->setChecked(true);
357 wpval->end_down->setChecked(true);
360 wpval->end_right->setChecked(true);
363 wpval->end_left->setChecked(true);
366 wpval->end_center->setChecked(true);
369 wpval->start_transp->setValue(w.startTransparency);
370 wpval->end_transp->setValue(w.endTransparency);
371 m_valueItems[paramName] = wpval;
372 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
373 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
374 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
375 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
376 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
377 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
378 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
379 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
380 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
381 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
382 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
383 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
384 //wpval->title->setTitle(na.toElement().text());
385 m_uiItems.append(wpval);
386 } else if (type == "url") {
387 Urlval *cval = new Urlval;
388 cval->setupUi(toFillin);
389 cval->label->setText(paramName);
390 cval->urlwidget->fileDialog()->setFilter(ProjectList::getExtensions());
391 m_valueItems[paramName] = cval;
392 cval->urlwidget->setUrl(KUrl(value));
393 connect(cval->urlwidget, SIGNAL(returnPressed()) , this, SLOT(collectAllParameters()));
394 connect(cval->urlwidget, SIGNAL(urlSelected(const KUrl&)) , this, SLOT(collectAllParameters()));
395 m_uiItems.append(cval);
402 m_vbox->addWidget(toFillin);
404 m_vbox->addStretch();
407 wipeInfo EffectStackEdit::getWipeInfo(QString value)
410 QString start = value.section(';', 0, 0);
411 QString end = value.section(';', 1, 1).section('=', 1, 1);
413 if (start.startsWith("-100%,0"))
415 else if (start.startsWith("100%,0"))
417 else if (start.startsWith("0%,100%"))
419 else if (start.startsWith("0%,-100%"))
424 if (start.count(':') == 2)
425 info.startTransparency = start.section(':', -1).toInt();
427 info.startTransparency = 100;
429 if (end.startsWith("-100%,0"))
431 else if (end.startsWith("100%,0"))
433 else if (end.startsWith("0%,100%"))
435 else if (end.startsWith("0%,-100%"))
440 if (end.count(':') == 2)
441 info.endTransparency = end.section(':', -1).toInt();
443 info.endTransparency = 100;
448 QString EffectStackEdit::getWipeString(wipeInfo info)
453 switch (info.start) {
455 start = "-100%,0%:100%x100%";
458 start = "100%,0%:100%x100%";
461 start = "0%,100%:100%x100%";
464 start = "0%,-100%:100%x100%";
467 start = "0%,0%:100%x100%";
470 start.append(':' + QString::number(info.startTransparency));
474 end = "-100%,0%:100%x100%";
477 end = "100%,0%:100%x100%";
480 end = "0%,100%:100%x100%";
483 end = "0%,-100%:100%x100%";
486 end = "0%,0%:100%x100%";
489 end.append(':' + QString::number(info.endTransparency));
490 return QString(start + ";-1=" + end);
493 void EffectStackEdit::collectAllParameters()
495 if (m_valueItems.isEmpty() || m_params.isNull()) return;
496 const QDomElement oldparam = m_params.cloneNode().toElement();
497 QDomElement newparam = oldparam.cloneNode().toElement();
498 QDomNodeList namenode = newparam.elementsByTagName("parameter");
500 for (int i = 0; i < namenode.count() ; i++) {
501 QDomNode pa = namenode.item(i);
502 QDomNode na = pa.firstChildElement("name");
503 QString type = pa.attributes().namedItem("type").nodeValue();
504 QString paramName = i18n(na.toElement().text().toUtf8().data());
505 if (type == "complex")
506 paramName.append("complex");
507 else if (type == "position")
508 paramName.append("position");
509 else if (type == "geometry")
510 paramName.append("geometry");
511 else if (type == "keyframe")
512 paramName.append("keyframe");
513 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
514 kDebug() << "// Param: " << paramName << " NOT FOUND";
519 if (type == "double" || type == "constant") {
520 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
521 setValue = QString::number(slider->value());
522 } else if (type == "list") {
523 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
524 setValue = box->itemData(box->currentIndex()).toString();
525 } else if (type == "bool") {
526 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
527 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
528 } else if (type == "color") {
529 ChooseColorWidget *choosecolor = ((ChooseColorWidget*)m_valueItems.value(paramName));
530 setValue = choosecolor->getColor().name();
531 } else if (type == "complex") {
532 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
533 namenode.item(i) = complex->getParamDesc();
534 } else if (type == "geometry") {
535 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
536 namenode.item(i).toElement().setAttribute("value", geom->getValue());
537 } else if (type == "position") {
538 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
539 int pos = pedit->getPosition();
540 setValue = QString::number(pos);
541 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
542 // Make sure duration is not longer than clip
545 pedit->setPosition(pos);
547 EffectsList::setParameter(newparam, "in", QString::number(m_in));
548 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
550 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
551 // Make sure duration is not longer than clip
554 pedit->setPosition(pos);
556 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
557 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
560 } else if (type == "curve") {
561 KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
562 QList<QPointF> points = curve->curve().points();
563 QString number = pa.attributes().namedItem("number").nodeValue();
564 QString inName = pa.attributes().namedItem("inpoints").nodeValue();
565 QString outName = pa.attributes().namedItem("outpoints").nodeValue();
566 int off = pa.attributes().namedItem("min").nodeValue().toInt();
567 int end = pa.attributes().namedItem("max").nodeValue().toInt();
568 EffectsList::setParameter(newparam, number, QString::number(points.count()));
569 for (int j = 0; (j < points.count() && j + off <= end); j++) {
571 in.replace("%i", QString::number(j + off));
572 QString out = outName;
573 out.replace("%i", QString::number(j + off));
574 EffectsList::setParameter(newparam, in, QString::number(points.at(j).x()));
575 EffectsList::setParameter(newparam, out, QString::number(points.at(j).y()));
577 } else if (type == "wipe") {
578 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
580 if (wp->start_left->isChecked())
582 else if (wp->start_right->isChecked())
584 else if (wp->start_up->isChecked())
586 else if (wp->start_down->isChecked())
588 else if (wp->start_center->isChecked())
592 info.startTransparency = wp->start_transp->value();
594 if (wp->end_left->isChecked())
596 else if (wp->end_right->isChecked())
598 else if (wp->end_up->isChecked())
600 else if (wp->end_down->isChecked())
602 else if (wp->end_center->isChecked())
606 info.endTransparency = wp->end_transp->value();
608 setValue = getWipeString(info);
609 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
610 QString realName = i18n(na.toElement().text().toUtf8().data());
611 QString val = m_keyframeEditor->getValue(realName);
612 kDebug() << "SET VALUE: " << val;
613 namenode.item(i).toElement().setAttribute("keyframes", val);
614 } else if (type == "url") {
615 KUrlRequester *req = ((Urlval*)m_valueItems.value(paramName))->urlwidget;
616 setValue = req->url().path();
619 if (!setValue.isNull())
620 pa.attributes().namedItem("value").setNodeValue(setValue);
623 emit parameterChanged(oldparam, newparam);
626 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
628 QWidget* toFillin = new QWidget(m_baseWidget);
629 Constval *ctval = new Constval;
630 ctval->setupUi(toFillin);
631 ctval->horizontalSlider->setMinimum(min);
632 ctval->horizontalSlider->setMaximum(max);
633 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
634 ctval->spinBox->setMinimum(min);
635 ctval->spinBox->setMaximum(max);
636 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
637 ctval->horizontalSlider->setValue(val);
638 ctval->label->setText(name);
639 m_valueItems[name] = ctval;
640 m_uiItems.append(ctval);
641 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
642 m_vbox->addWidget(toFillin);
645 void EffectStackEdit::slotSliderMoved(int)
647 collectAllParameters();
650 void EffectStackEdit::clearAllItems()
653 m_valueItems.clear();
655 /*while (!m_items.isEmpty()) {
656 QWidget *die = m_items.takeFirst();
660 //qDeleteAll(m_uiItems);
662 while ((child = m_vbox->takeAt(0)) != 0) {
663 QWidget *wid = child->widget();
667 m_keyframeEditor = NULL;