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 QDomNodeList namenode = m_params.elementsByTagName("parameter");
121 for (int i = 0; i < namenode.count() ; i++) {
122 QDomNode pa = namenode.item(i);
123 QDomNode na = pa.firstChildElement("name");
124 QString type = pa.attributes().namedItem("type").nodeValue();
125 QString paramName = i18n(na.toElement().text().toUtf8().data());
127 if (type == "geometry") {
128 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
129 geom->updateTimecodeFormat();
132 if (type == "position") {
133 PositionEdit *posi = ((PositionEdit*)m_valueItems[paramName+"position"]);
134 posi->updateTimecodeFormat();
140 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
146 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
148 m_params.setAttribute(name, value);
151 void EffectStackEdit::transferParamDesc(const QDomElement d, int pos, int in, int out)
154 if (m_keyframeEditor) delete m_keyframeEditor;
155 m_keyframeEditor = NULL;
159 if (m_params.isNull()) {
160 kDebug() << "// EMPTY EFFECT STACK";
165 doc.appendChild(doc.importNode(m_params, true));
166 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
168 QDomNodeList namenode = m_params.elementsByTagName("parameter");
169 QDomElement e = m_params.toElement();
170 const int minFrame = e.attribute("start").toInt();
171 const int maxFrame = e.attribute("end").toInt();
174 for (int i = 0; i < namenode.count() ; i++) {
175 QDomElement pa = namenode.item(i).toElement();
176 QDomNode na = pa.firstChildElement("name");
177 QString type = pa.attribute("type");
178 QString paramName = i18n(na.toElement().text().toUtf8().data());
179 QWidget * toFillin = new QWidget(m_baseWidget);
180 QString value = pa.attribute("value").isNull() ?
181 pa.attribute("default") : pa.attribute("value");
183 /** Currently supported parameter types are:
184 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
185 * list: a combobox containing a list of values to choose
187 * complex: designed for keyframe parameters, but old and not finished, do not use
188 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
189 * keyframe: a list widget with a list of entries (position and value)
190 * color: a color chooser button
191 * position: a slider representing the position of a frame in the current clip
192 * curve: a single curve representing multiple points
193 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
196 if (type == "double" || type == "constant") {
199 if (pa.attribute("min").startsWith('%')) {
200 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
201 } else min = pa.attribute("min").toInt();
202 if (pa.attribute("max").startsWith('%')) {
203 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
204 } else 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) geo->setupParam(pa, m_in, m_out);
262 else geo->setupParam(pa, minFrame, maxFrame);
263 m_vbox->addWidget(geo);
264 m_valueItems[paramName+"geometry"] = geo;
265 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
266 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
267 } else if (type == "keyframe" || type == "simplekeyframe") {
268 //Â keyframe editor widget
269 kDebug() << "min: " << m_in << ", MAX: " << m_out;
270 if (m_keyframeEditor == NULL) {
271 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());
272 m_vbox->addWidget(geo);
273 m_valueItems[paramName+"keyframe"] = geo;
274 m_keyframeEditor = geo;
275 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
276 connect(geo, SIGNAL(seekToPos(int)), this, SIGNAL(seekTimeline(int)));
278 // we already have a keyframe editor, so just add another column for the new param
279 m_keyframeEditor->addParameter(pa);
281 } else if (type == "color") {
282 Colorval *cval = new Colorval;
283 cval->setupUi(toFillin);
285 if (value.startsWith('#')) value = value.replace('#', "0x");
286 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
287 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
288 cval->label->setText(paramName);
289 m_valueItems[paramName] = cval;
290 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
291 m_uiItems.append(cval);
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()) curve->setCurve(KisCubicCurve(points));
320 QSpinBox *spinin = new QSpinBox();
321 spinin->setRange(0, 1000);
322 QSpinBox *spinout = new QSpinBox();
323 spinout->setRange(0, 1000);
324 curve->setupInOutControls(spinin, spinout, 0, 1000);
325 m_vbox->addWidget(curve);
326 m_vbox->addWidget(spinin);
327 m_vbox->addWidget(spinout);
328 connect(curve, SIGNAL(modified()), this, SLOT(collectAllParameters()));
329 m_valueItems[paramName] = curve;
330 } else if (type == "wipe") {
331 Wipeval *wpval = new Wipeval;
332 wpval->setupUi(toFillin);
333 wipeInfo w = getWipeInfo(value);
336 wpval->start_up->setChecked(true);
339 wpval->start_down->setChecked(true);
342 wpval->start_right->setChecked(true);
345 wpval->start_left->setChecked(true);
348 wpval->start_center->setChecked(true);
353 wpval->end_up->setChecked(true);
356 wpval->end_down->setChecked(true);
359 wpval->end_right->setChecked(true);
362 wpval->end_left->setChecked(true);
365 wpval->end_center->setChecked(true);
368 wpval->start_transp->setValue(w.startTransparency);
369 wpval->end_transp->setValue(w.endTransparency);
370 m_valueItems[paramName] = wpval;
371 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
372 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
373 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
374 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
375 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
376 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
377 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
378 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
379 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
380 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
381 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
382 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
383 //wpval->title->setTitle(na.toElement().text());
384 m_uiItems.append(wpval);
385 } else if (type == "url") {
386 Urlval *cval = new Urlval;
387 cval->setupUi(toFillin);
388 cval->label->setText(paramName);
389 cval->urlwidget->fileDialog()->setFilter(ProjectList::getExtensions());
390 m_valueItems[paramName] = cval;
391 cval->urlwidget->setUrl(KUrl(value));
392 connect(cval->urlwidget, SIGNAL(returnPressed()) , this, SLOT(collectAllParameters()));
393 connect(cval->urlwidget, SIGNAL(urlSelected(const KUrl&)) , this, SLOT(collectAllParameters()));
394 m_uiItems.append(cval);
401 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);
412 if (start.startsWith("-100%,0")) info.start = LEFT;
413 else if (start.startsWith("100%,0")) info.start = RIGHT;
414 else if (start.startsWith("0%,100%")) info.start = DOWN;
415 else if (start.startsWith("0%,-100%")) info.start = UP;
416 else info.start = CENTER;
417 if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
418 else info.startTransparency = 100;
420 if (end.startsWith("-100%,0")) info.end = LEFT;
421 else if (end.startsWith("100%,0")) info.end = RIGHT;
422 else if (end.startsWith("0%,100%")) info.end = DOWN;
423 else if (end.startsWith("0%,-100%")) info.end = UP;
424 else info.end = CENTER;
425 if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
426 else info.endTransparency = 100;
430 QString EffectStackEdit::getWipeString(wipeInfo info)
435 switch (info.start) {
437 start = "-100%,0%:100%x100%";
440 start = "100%,0%:100%x100%";
443 start = "0%,100%:100%x100%";
446 start = "0%,-100%:100%x100%";
449 start = "0%,0%:100%x100%";
452 start.append(':' + QString::number(info.startTransparency));
456 end = "-100%,0%:100%x100%";
459 end = "100%,0%:100%x100%";
462 end = "0%,100%:100%x100%";
465 end = "0%,-100%:100%x100%";
468 end = "0%,0%:100%x100%";
471 end.append(':' + QString::number(info.endTransparency));
472 return QString(start + ";-1=" + end);
475 void EffectStackEdit::collectAllParameters()
477 if (m_valueItems.isEmpty() || m_params.isNull()) return;
478 const QDomElement oldparam = m_params.cloneNode().toElement();
479 QDomElement newparam = oldparam.cloneNode().toElement();
480 QDomNodeList namenode = newparam.elementsByTagName("parameter");
482 for (int i = 0; i < namenode.count() ; i++) {
483 QDomNode pa = namenode.item(i);
484 QDomNode na = pa.firstChildElement("name");
485 QString type = pa.attributes().namedItem("type").nodeValue();
486 QString paramName = i18n(na.toElement().text().toUtf8().data());
487 if (type == "complex") paramName.append("complex");
488 else if (type == "position") paramName.append("position");
489 else if (type == "geometry") paramName.append("geometry");
490 else if (type == "keyframe") paramName.append("keyframe");
491 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
492 kDebug() << "// Param: " << paramName << " NOT FOUND";
497 if (type == "double" || type == "constant") {
498 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
499 setValue = QString::number(slider->value());
500 } else if (type == "list") {
501 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
502 setValue = box->itemData(box->currentIndex()).toString();
503 } else if (type == "bool") {
504 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
505 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
506 } else if (type == "color") {
507 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
508 setValue = color->color().name();
509 } else if (type == "complex") {
510 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
511 namenode.item(i) = complex->getParamDesc();
512 } else if (type == "geometry") {
513 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
514 namenode.item(i).toElement().setAttribute("value", geom->getValue());
515 } else if (type == "position") {
516 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
517 int pos = pedit->getPosition();
518 setValue = QString::number(pos);
519 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
520 // Make sure duration is not longer than clip
523 pedit->setPosition(pos);
525 EffectsList::setParameter(newparam, "in", QString::number(m_in));
526 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
528 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
529 // Make sure duration is not longer than clip
532 pedit->setPosition(pos);
534 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
535 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
538 } else if (type == "curve") {
539 KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
540 QList<QPointF> points = curve->curve().points();
541 QString number = pa.attributes().namedItem("number").nodeValue();
542 QString inName = pa.attributes().namedItem("inpoints").nodeValue();
543 QString outName = pa.attributes().namedItem("outpoints").nodeValue();
544 int off = pa.attributes().namedItem("min").nodeValue().toInt();
545 int end = pa.attributes().namedItem("max").nodeValue().toInt();
546 EffectsList::setParameter(newparam, number, QString::number(points.count()));
547 for (int j = 0; (j < points.count() && j + off <= end); j++) {
549 in.replace("%i", QString::number(j + off));
550 QString out = outName;
551 out.replace("%i", QString::number(j + off));
552 EffectsList::setParameter(newparam, in, QString::number(points.at(j).x()));
553 EffectsList::setParameter(newparam, out, QString::number(points.at(j).y()));
555 } else if (type == "wipe") {
556 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
558 if (wp->start_left->isChecked()) info.start = LEFT;
559 else if (wp->start_right->isChecked()) info.start = RIGHT;
560 else if (wp->start_up->isChecked()) info.start = UP;
561 else if (wp->start_down->isChecked()) info.start = DOWN;
562 else if (wp->start_center->isChecked()) info.start = CENTER;
563 else info.start = LEFT;
564 info.startTransparency = wp->start_transp->value();
565 if (wp->end_left->isChecked()) info.end = LEFT;
566 else if (wp->end_right->isChecked()) info.end = RIGHT;
567 else if (wp->end_up->isChecked()) info.end = UP;
568 else if (wp->end_down->isChecked()) info.end = DOWN;
569 else if (wp->end_center->isChecked()) info.end = CENTER;
570 else info.end = RIGHT;
571 info.endTransparency = wp->end_transp->value();
572 setValue = getWipeString(info);
573 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
574 QString realName = i18n(na.toElement().text().toUtf8().data());
575 QString val = m_keyframeEditor->getValue(realName);
576 kDebug() << "SET VALUE: " << val;
577 namenode.item(i).toElement().setAttribute("keyframes", val);
578 } else if (type == "url") {
579 KUrlRequester *req = ((Urlval*)m_valueItems.value(paramName))->urlwidget;
580 setValue = req->url().path();
582 if (!setValue.isNull()) {
583 pa.attributes().namedItem("value").setNodeValue(setValue);
586 emit parameterChanged(oldparam, newparam);
589 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
591 QWidget* toFillin = new QWidget(m_baseWidget);
592 Constval *ctval = new Constval;
593 ctval->setupUi(toFillin);
594 ctval->horizontalSlider->setMinimum(min);
595 ctval->horizontalSlider->setMaximum(max);
596 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
597 ctval->spinBox->setMinimum(min);
598 ctval->spinBox->setMaximum(max);
599 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
600 ctval->horizontalSlider->setValue(val);
601 ctval->label->setText(name);
602 m_valueItems[name] = ctval;
603 m_uiItems.append(ctval);
604 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
605 m_vbox->addWidget(toFillin);
608 void EffectStackEdit::slotSliderMoved(int)
610 collectAllParameters();
613 void EffectStackEdit::clearAllItems()
616 m_valueItems.clear();
618 /*while (!m_items.isEmpty()) {
619 QWidget *die = m_items.takeFirst();
623 //qDeleteAll(m_uiItems);
625 while ((child = m_vbox->takeAt(0)) != 0) {
626 QWidget *wid = child->widget();
630 m_keyframeEditor = NULL;