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);
112 void EffectStackEdit::updateTimecodeFormat()
114 QDomNodeList namenode = m_params.elementsByTagName("parameter");
115 for (int i = 0; i < namenode.count() ; i++) {
116 QDomNode pa = namenode.item(i);
117 QDomNode na = pa.firstChildElement("name");
118 QString type = pa.attributes().namedItem("type").nodeValue();
119 QString paramName = i18n(na.toElement().text().toUtf8().data());
121 if (type == "geometry") {
122 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
123 geom->updateTimecodeFormat();
126 if (type == "position") {
127 PositionEdit *posi = ((PositionEdit*)m_valueItems[paramName+"position"]);
128 posi->updateTimecodeFormat();
134 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
140 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
142 m_params.setAttribute(name, value);
145 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
148 if (m_keyframeEditor) delete m_keyframeEditor;
149 m_keyframeEditor = NULL;
153 if (m_params.isNull()) {
154 kDebug() << "// EMPTY EFFECT STACK";
159 doc.appendChild(doc.importNode(m_params, true));
160 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
162 QDomNodeList namenode = m_params.elementsByTagName("parameter");
163 QDomElement e = m_params.toElement();
164 const int minFrame = e.attribute("start").toInt();
165 const int maxFrame = e.attribute("end").toInt();
168 for (int i = 0; i < namenode.count() ; i++) {
169 QDomElement pa = namenode.item(i).toElement();
170 QDomNode na = pa.firstChildElement("name");
171 QString type = pa.attribute("type");
172 QString paramName = i18n(na.toElement().text().toUtf8().data());
173 QWidget * toFillin = new QWidget(m_baseWidget);
174 QString value = pa.attribute("value").isNull() ?
175 pa.attribute("default") : pa.attribute("value");
177 /** Currently supported parameter types are:
178 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
179 * list: a combobox containing a list of values to choose
181 * complex: designed for keyframe parameters, but old and not finished, do not use
182 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
183 * keyframe: a list widget with a list of entries (position and value)
184 * color: a color chooser button
185 * position: a slider representing the position of a frame in the current clip
186 * curve: a single curve representing multiple points
187 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
190 if (type == "double" || type == "constant") {
193 if (pa.attribute("min").startsWith('%')) {
194 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
195 } else min = pa.attribute("min").toInt();
196 if (pa.attribute("max").startsWith('%')) {
197 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
198 } else max = pa.attribute("max").toInt();
199 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
202 } else if (type == "list") {
203 Listval *lsval = new Listval;
204 lsval->setupUi(toFillin);
205 QStringList listitems = pa.attribute("paramlist").split(',');
206 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
207 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
208 //lsval->list->addItems(listitems);
209 lsval->list->setIconSize(QSize(30, 30));
210 for (int i = 0; i < listitems.count(); i++) {
211 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
212 QString entry = listitems.at(i);
213 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
214 if (!EffectStackEdit::iconCache.contains(entry)) {
216 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
218 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
221 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
222 lsval->title->setTitle(paramName);
223 m_valueItems[paramName] = lsval;
224 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
225 m_uiItems.append(lsval);
226 } else if (type == "bool") {
227 Boolval *bval = new Boolval;
228 bval->setupUi(toFillin);
229 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
230 bval->checkBox->setText(paramName);
231 m_valueItems[paramName] = bval;
232 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
233 m_uiItems.append(bval);
234 } else if (type == "complex") {
235 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
236 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
237 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
238 QStringList val=value.split(';');
239 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
240 if ( (names.size() == max.size() ) &&
241 (names.size()== min.size()) &&
242 (names.size()== val.size()) )
244 for (int i=0;i< names.size();i++){
245 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
248 ComplexParameter *pl = new ComplexParameter;
249 pl->setupParam(d, pa.attribute("name"), 0, 100);
250 m_vbox->addWidget(pl);
251 m_valueItems[paramName+"complex"] = pl;
252 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
253 } else if (type == "geometry") {
254 Geometryval *geo = new Geometryval(m_profile, m_timecode, m_frameSize, m_in);
255 if (minFrame == maxFrame) geo->setupParam(pa, m_in, m_out);
256 else geo->setupParam(pa, minFrame, maxFrame);
257 m_vbox->addWidget(geo);
258 m_valueItems[paramName+"geometry"] = geo;
259 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
260 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
261 } else if (type == "keyframe" || type == "simplekeyframe") {
262 //Â keyframe editor widget
263 kDebug() << "min: " << m_in << ", MAX: " << m_out;
264 if (m_keyframeEditor == NULL) {
265 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());
266 m_vbox->addWidget(geo);
267 m_valueItems[paramName+"keyframe"] = geo;
268 m_keyframeEditor = geo;
269 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
270 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
272 // we already have a keyframe editor, so just add another column for the new param
273 m_keyframeEditor->addParameter(pa);
275 } else if (type == "color") {
276 Colorval *cval = new Colorval;
277 cval->setupUi(toFillin);
279 if (value.startsWith('#')) value = value.replace('#', "0x");
280 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
281 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
282 cval->label->setText(paramName);
283 m_valueItems[paramName] = cval;
284 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
285 m_uiItems.append(cval);
286 } else if (type == "position") {
287 int pos = value.toInt();
288 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
290 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
291 // fadeout position starts from clip end
292 pos = m_out - (pos - m_in);
294 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
295 m_vbox->addWidget(posedit);
296 m_valueItems[paramName+"position"] = posedit;
297 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
298 } else if (type == "curve") {
299 KisCurveWidget *curve = new KisCurveWidget(this);
300 curve->setMaxPoints(pa.attribute("max").toInt());
301 QList<QPointF> points;
302 int number = EffectsList::parameter(e, pa.attribute("number")).toInt();
303 QString inName = pa.attribute("inpoints");
304 QString outName = pa.attribute("outpoints");
305 int start = pa.attribute("min").toInt();
306 for (int j = start; j <= number; j++) {
308 in.replace("%i", QString::number(j));
309 QString out = outName;
310 out.replace("%i", QString::number(j));
311 points << QPointF(EffectsList::parameter(e, in).toDouble(), EffectsList::parameter(e, out).toDouble());
313 if (!points.isEmpty()) curve->setCurve(KisCubicCurve(points));
314 QSpinBox *spinin = new QSpinBox();
315 spinin->setRange(0, 1000);
316 QSpinBox *spinout = new QSpinBox();
317 spinout->setRange(0, 1000);
318 curve->setupInOutControls(spinin, spinout, 0, 1000);
319 m_vbox->addWidget(curve);
320 m_vbox->addWidget(spinin);
321 m_vbox->addWidget(spinout);
322 connect(curve, SIGNAL(modified()), this, SLOT(collectAllParameters()));
323 m_valueItems[paramName] = curve;
324 } else if (type == "wipe") {
325 Wipeval *wpval = new Wipeval;
326 wpval->setupUi(toFillin);
327 wipeInfo w = getWipeInfo(value);
330 wpval->start_up->setChecked(true);
333 wpval->start_down->setChecked(true);
336 wpval->start_right->setChecked(true);
339 wpval->start_left->setChecked(true);
342 wpval->start_center->setChecked(true);
347 wpval->end_up->setChecked(true);
350 wpval->end_down->setChecked(true);
353 wpval->end_right->setChecked(true);
356 wpval->end_left->setChecked(true);
359 wpval->end_center->setChecked(true);
362 wpval->start_transp->setValue(w.startTransparency);
363 wpval->end_transp->setValue(w.endTransparency);
364 m_valueItems[paramName] = wpval;
365 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
366 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
367 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
368 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
369 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
370 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
371 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
372 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
373 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
374 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
375 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
376 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
377 //wpval->title->setTitle(na.toElement().text());
378 m_uiItems.append(wpval);
385 m_vbox->addWidget(toFillin);
388 m_vbox->addStretch();
391 void EffectStackEdit::slotSeekToPos(int pos)
393 emit seekTimeline(pos);
396 wipeInfo EffectStackEdit::getWipeInfo(QString value)
399 QString start = value.section(';', 0, 0);
400 QString end = value.section(';', 1, 1).section('=', 1, 1);
401 if (start.startsWith("-100%,0")) info.start = LEFT;
402 else if (start.startsWith("100%,0")) info.start = RIGHT;
403 else if (start.startsWith("0%,100%")) info.start = DOWN;
404 else if (start.startsWith("0%,-100%")) info.start = UP;
405 else info.start = CENTER;
406 if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
407 else info.startTransparency = 100;
409 if (end.startsWith("-100%,0")) info.end = LEFT;
410 else if (end.startsWith("100%,0")) info.end = RIGHT;
411 else if (end.startsWith("0%,100%")) info.end = DOWN;
412 else if (end.startsWith("0%,-100%")) info.end = UP;
413 else info.end = CENTER;
414 if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
415 else info.endTransparency = 100;
419 QString EffectStackEdit::getWipeString(wipeInfo info)
424 switch (info.start) {
426 start = "-100%,0%:100%x100%";
429 start = "100%,0%:100%x100%";
432 start = "0%,100%:100%x100%";
435 start = "0%,-100%:100%x100%";
438 start = "0%,0%:100%x100%";
441 start.append(':' + QString::number(info.startTransparency));
445 end = "-100%,0%:100%x100%";
448 end = "100%,0%:100%x100%";
451 end = "0%,100%:100%x100%";
454 end = "0%,-100%:100%x100%";
457 end = "0%,0%:100%x100%";
460 end.append(':' + QString::number(info.endTransparency));
461 return QString(start + ";-1=" + end);
464 void EffectStackEdit::collectAllParameters()
466 if (m_valueItems.isEmpty() || m_params.isNull()) return;
467 const QDomElement oldparam = m_params.cloneNode().toElement();
468 QDomElement newparam = oldparam.cloneNode().toElement();
469 QDomNodeList namenode = newparam.elementsByTagName("parameter");
471 for (int i = 0; i < namenode.count() ; i++) {
472 QDomNode pa = namenode.item(i);
473 QDomNode na = pa.firstChildElement("name");
474 QString type = pa.attributes().namedItem("type").nodeValue();
475 QString paramName = i18n(na.toElement().text().toUtf8().data());
476 if (type == "complex") paramName.append("complex");
477 else if (type == "position") paramName.append("position");
478 else if (type == "geometry") paramName.append("geometry");
479 else if (type == "keyframe") paramName.append("keyframe");
480 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
481 kDebug() << "// Param: " << paramName << " NOT FOUND";
486 if (type == "double" || type == "constant") {
487 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
488 setValue = QString::number(slider->value());
489 } else if (type == "list") {
490 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
491 setValue = box->itemData(box->currentIndex()).toString();
492 } else if (type == "bool") {
493 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
494 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
495 } else if (type == "color") {
496 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
497 setValue = color->color().name();
498 } else if (type == "complex") {
499 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
500 namenode.item(i) = complex->getParamDesc();
501 } else if (type == "geometry") {
502 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
503 namenode.item(i).toElement().setAttribute("value", geom->getValue());
504 } else if (type == "position") {
505 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
506 int pos = pedit->getPosition();
507 setValue = QString::number(pos);
508 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
509 // Make sure duration is not longer than clip
512 pedit->setPosition(pos);
514 EffectsList::setParameter(newparam, "in", QString::number(m_in));
515 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
517 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
518 // Make sure duration is not longer than clip
521 pedit->setPosition(pos);
523 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
524 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
527 } else if (type == "curve") {
528 KisCurveWidget *curve = ((KisCurveWidget*)m_valueItems.value(paramName));
529 QList<QPointF> points = curve->curve().points();
530 QString number = pa.attributes().namedItem("number").nodeValue();
531 QString inName = pa.attributes().namedItem("inpoints").nodeValue();
532 QString outName = pa.attributes().namedItem("outpoints").nodeValue();
533 int off = pa.attributes().namedItem("min").nodeValue().toInt();
534 int end = pa.attributes().namedItem("max").nodeValue().toInt();
535 EffectsList::setParameter(newparam, number, QString::number(points.count()));
536 for (int j = 0; (j < points.count() && j + off <= end); j++) {
538 in.replace("%i", QString::number(j + off));
539 QString out = outName;
540 out.replace("%i", QString::number(j + off));
541 EffectsList::setParameter(newparam, in, QString::number(points.at(j).x()));
542 EffectsList::setParameter(newparam, out, QString::number(points.at(j).y()));
544 } else if (type == "wipe") {
545 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
547 if (wp->start_left->isChecked()) info.start = LEFT;
548 else if (wp->start_right->isChecked()) info.start = RIGHT;
549 else if (wp->start_up->isChecked()) info.start = UP;
550 else if (wp->start_down->isChecked()) info.start = DOWN;
551 else if (wp->start_center->isChecked()) info.start = CENTER;
552 else info.start = LEFT;
553 info.startTransparency = wp->start_transp->value();
554 if (wp->end_left->isChecked()) info.end = LEFT;
555 else if (wp->end_right->isChecked()) info.end = RIGHT;
556 else if (wp->end_up->isChecked()) info.end = UP;
557 else if (wp->end_down->isChecked()) info.end = DOWN;
558 else if (wp->end_center->isChecked()) info.end = CENTER;
559 else info.end = RIGHT;
560 info.endTransparency = wp->end_transp->value();
561 setValue = getWipeString(info);
562 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
563 QString realName = i18n(na.toElement().text().toUtf8().data());
564 QString val = m_keyframeEditor->getValue(realName);
565 kDebug() << "SET VALUE: " << val;
566 namenode.item(i).toElement().setAttribute("keyframes", val);
568 if (!setValue.isNull()) {
569 pa.attributes().namedItem("value").setNodeValue(setValue);
572 emit parameterChanged(oldparam, newparam);
575 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
577 QWidget* toFillin = new QWidget(m_baseWidget);
578 Constval *ctval = new Constval;
579 ctval->setupUi(toFillin);
580 ctval->horizontalSlider->setMinimum(min);
581 ctval->horizontalSlider->setMaximum(max);
582 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
583 ctval->spinBox->setMinimum(min);
584 ctval->spinBox->setMaximum(max);
585 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
586 ctval->horizontalSlider->setValue(val);
587 ctval->label->setText(name);
588 m_valueItems[name] = ctval;
589 m_uiItems.append(ctval);
590 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
591 m_vbox->addWidget(toFillin);
594 void EffectStackEdit::slotSliderMoved(int)
596 collectAllParameters();
599 void EffectStackEdit::clearAllItems()
602 m_valueItems.clear();
604 /*while (!m_items.isEmpty()) {
605 QWidget *die = m_items.takeFirst();
609 //qDeleteAll(m_uiItems);
611 while ((child = m_vbox->takeAt(0)) != 0) {
612 QWidget *wid = child->widget();
616 m_keyframeEditor = NULL;