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"
34 #include <QVBoxLayout>
37 #include <QPushButton>
39 #include <QScrollArea>
42 class Boolval: public QWidget, public Ui::Boolval_UI
46 class Colorval: public QWidget, public Ui::Colorval_UI
50 class Constval: public QWidget, public Ui::Constval_UI
54 class Listval: public QWidget, public Ui::Listval_UI
58 class Wipeval: public QWidget, public Ui::Wipeval_UI
63 QMap<QString, QImage> EffectStackEdit::iconCache;
65 EffectStackEdit::EffectStackEdit(QWidget *parent) :
69 m_frameSize(QPoint()),
70 m_keyframeEditor(NULL)
72 m_baseWidget = new QWidget(this);
73 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
74 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
75 setFrameStyle(QFrame::NoFrame);
76 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
78 setWidget(m_baseWidget);
79 setWidgetResizable(true);
80 m_vbox = new QVBoxLayout(m_baseWidget);
81 m_vbox->setContentsMargins(0, 0, 0, 0);
82 m_vbox->setSpacing(0);
86 EffectStackEdit::~EffectStackEdit()
92 void EffectStackEdit::setFrameSize(QPoint p)
95 QDomNodeList namenode = m_params.elementsByTagName("parameter");
96 for (int i = 0; i < namenode.count() ; i++) {
97 QDomNode pa = namenode.item(i);
98 QDomNode na = pa.firstChildElement("name");
99 QString type = pa.attributes().namedItem("type").nodeValue();
100 QString paramName = i18n(na.toElement().text().toUtf8().data());
102 if (type == "geometry") {
103 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
104 geom->setFrameSize(m_frameSize);
111 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
117 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
119 m_params.setAttribute(name, value);
122 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
125 if (m_keyframeEditor) delete m_keyframeEditor;
126 m_keyframeEditor = NULL;
130 if (m_params.isNull()) {
131 kDebug() << "// EMPTY EFFECT STACK";
136 doc.appendChild(doc.importNode(m_params, true));
137 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
139 QDomNodeList namenode = m_params.elementsByTagName("parameter");
140 QDomElement e = m_params.toElement();
141 const int minFrame = e.attribute("start").toInt();
142 const int maxFrame = e.attribute("end").toInt();
145 for (int i = 0; i < namenode.count() ; i++) {
146 QDomElement pa = namenode.item(i).toElement();
147 QDomNode na = pa.firstChildElement("name");
148 QString type = pa.attribute("type");
149 QString paramName = i18n(na.toElement().text().toUtf8().data());
150 QWidget * toFillin = new QWidget(m_baseWidget);
151 QString value = pa.attribute("value").isNull() ?
152 pa.attribute("default") : pa.attribute("value");
154 /** Currently supported parameter types are:
155 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
156 * list: a combobox containing a list of values to choose
158 * complex: designed for keyframe parameters, but old and not finished, do not use
159 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
160 * keyframe: a list widget with a list of entries (position and value)
161 * color: a color chooser button
162 * position: a slider representing the position of a frame in the current clip
163 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
166 if (type == "double" || type == "constant") {
169 if (pa.attribute("min").startsWith('%')) {
170 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
171 } else min = pa.attribute("min").toInt();
172 if (pa.attribute("max").startsWith('%')) {
173 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
174 } else max = pa.attribute("max").toInt();
175 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
178 } else if (type == "list") {
179 Listval *lsval = new Listval;
180 lsval->setupUi(toFillin);
181 QStringList listitems = pa.attribute("paramlist").split(',');
182 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
183 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
184 //lsval->list->addItems(listitems);
185 lsval->list->setIconSize(QSize(30, 30));
186 for (int i = 0; i < listitems.count(); i++) {
187 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
188 QString entry = listitems.at(i);
189 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
190 if (!EffectStackEdit::iconCache.contains(entry)) {
192 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
194 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
197 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
198 lsval->title->setTitle(paramName);
199 m_valueItems[paramName] = lsval;
200 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
201 m_uiItems.append(lsval);
202 } else if (type == "bool") {
203 Boolval *bval = new Boolval;
204 bval->setupUi(toFillin);
205 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
206 bval->checkBox->setText(paramName);
207 m_valueItems[paramName] = bval;
208 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
209 m_uiItems.append(bval);
210 } else if (type == "complex") {
211 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
212 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
213 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
214 QStringList val=value.split(';');
215 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
216 if ( (names.size() == max.size() ) &&
217 (names.size()== min.size()) &&
218 (names.size()== val.size()) )
220 for (int i=0;i< names.size();i++){
221 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
224 ComplexParameter *pl = new ComplexParameter;
225 pl->setupParam(d, pa.attribute("name"), 0, 100);
226 m_vbox->addWidget(pl);
227 m_valueItems[paramName+"complex"] = pl;
228 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
229 } else if (type == "geometry") {
230 Geometryval *geo = new Geometryval(m_profile, m_frameSize);
231 geo->setupParam(pa, minFrame, maxFrame);
232 m_vbox->addWidget(geo);
233 m_valueItems[paramName+"geometry"] = geo;
234 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
235 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
236 } else if (type == "keyframe" || type == "simplekeyframe") {
237 //Â keyframe editor widget
238 kDebug() << "min: " << m_in << ", MAX: " << m_out;
239 if (m_keyframeEditor == NULL) {
240 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());
241 m_vbox->addWidget(geo);
242 m_valueItems[paramName+"keyframe"] = geo;
243 m_keyframeEditor = geo;
244 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
245 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
247 // we already have a keyframe editor, so just add another column for the new param
248 m_keyframeEditor->addParameter(pa);
250 } else if (type == "color") {
251 Colorval *cval = new Colorval;
252 cval->setupUi(toFillin);
254 if (value.startsWith('#')) value = value.replace('#', "0x");
255 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
256 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
257 cval->label->setText(paramName);
258 m_valueItems[paramName] = cval;
259 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
260 m_uiItems.append(cval);
261 } else if (type == "position") {
262 int pos = value.toInt();
263 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
265 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
266 // fadeout position starts from clip end
267 pos = m_out - (pos - m_in);
269 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
270 m_vbox->addWidget(posedit);
271 m_valueItems[paramName+"position"] = posedit;
272 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
273 } else if (type == "wipe") {
274 Wipeval *wpval = new Wipeval;
275 wpval->setupUi(toFillin);
276 wipeInfo w = getWipeInfo(value);
279 wpval->start_up->setChecked(true);
282 wpval->start_down->setChecked(true);
285 wpval->start_right->setChecked(true);
288 wpval->start_left->setChecked(true);
291 wpval->start_center->setChecked(true);
296 wpval->end_up->setChecked(true);
299 wpval->end_down->setChecked(true);
302 wpval->end_right->setChecked(true);
305 wpval->end_left->setChecked(true);
308 wpval->end_center->setChecked(true);
311 wpval->start_transp->setValue(w.startTransparency);
312 wpval->end_transp->setValue(w.endTransparency);
313 m_valueItems[paramName] = wpval;
314 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
315 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
316 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
317 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
318 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
319 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
320 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
321 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
322 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
323 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
324 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
325 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
326 //wpval->title->setTitle(na.toElement().text());
327 m_uiItems.append(wpval);
334 m_vbox->addWidget(toFillin);
337 m_vbox->addStretch();
340 void EffectStackEdit::slotSeekToPos(int pos)
342 emit seekTimeline(m_in + pos);
345 wipeInfo EffectStackEdit::getWipeInfo(QString value)
348 QString start = value.section(';', 0, 0);
349 QString end = value.section(';', 1, 1).section('=', 1, 1);
350 if (start.startsWith("-100%,0")) info.start = LEFT;
351 else if (start.startsWith("100%,0")) info.start = RIGHT;
352 else if (start.startsWith("0%,100%")) info.start = DOWN;
353 else if (start.startsWith("0%,-100%")) info.start = UP;
354 else info.start = CENTER;
355 if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
356 else info.startTransparency = 100;
358 if (end.startsWith("-100%,0")) info.end = LEFT;
359 else if (end.startsWith("100%,0")) info.end = RIGHT;
360 else if (end.startsWith("0%,100%")) info.end = DOWN;
361 else if (end.startsWith("0%,-100%")) info.end = UP;
362 else info.end = CENTER;
363 if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
364 else info.endTransparency = 100;
368 QString EffectStackEdit::getWipeString(wipeInfo info)
373 switch (info.start) {
375 start = "-100%,0%:100%x100%";
378 start = "100%,0%:100%x100%";
381 start = "0%,100%:100%x100%";
384 start = "0%,-100%:100%x100%";
387 start = "0%,0%:100%x100%";
390 start.append(':' + QString::number(info.startTransparency));
394 end = "-100%,0%:100%x100%";
397 end = "100%,0%:100%x100%";
400 end = "0%,100%:100%x100%";
403 end = "0%,-100%:100%x100%";
406 end = "0%,0%:100%x100%";
409 end.append(':' + QString::number(info.endTransparency));
410 return QString(start + ";-1=" + end);
413 void EffectStackEdit::collectAllParameters()
415 if (m_valueItems.isEmpty() || m_params.isNull()) return;
416 const QDomElement oldparam = m_params.cloneNode().toElement();
417 QDomElement newparam = oldparam.cloneNode().toElement();
418 QDomNodeList namenode = newparam.elementsByTagName("parameter");
420 for (int i = 0; i < namenode.count() ; i++) {
421 QDomNode pa = namenode.item(i);
422 QDomNode na = pa.firstChildElement("name");
423 QString type = pa.attributes().namedItem("type").nodeValue();
424 QString paramName = i18n(na.toElement().text().toUtf8().data());
425 if (type == "complex") paramName.append("complex");
426 else if (type == "position") paramName.append("position");
427 else if (type == "geometry") paramName.append("geometry");
428 else if (type == "keyframe") paramName.append("keyframe");
429 if (type != "simplekeyframe" && !m_valueItems.contains(paramName)) {
430 kDebug() << "// Param: " << paramName << " NOT FOUND";
435 if (type == "double" || type == "constant") {
436 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
437 setValue = QString::number(slider->value());
438 } else if (type == "list") {
439 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
440 setValue = box->itemData(box->currentIndex()).toString();
441 } else if (type == "bool") {
442 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
443 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
444 } else if (type == "color") {
445 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
446 setValue = color->color().name();
447 } else if (type == "complex") {
448 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
449 namenode.item(i) = complex->getParamDesc();
450 } else if (type == "geometry") {
451 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
452 namenode.item(i).toElement().setAttribute("value", geom->getValue());
453 } else if (type == "position") {
454 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
455 int pos = pedit->getPosition();
456 setValue = QString::number(pos);
457 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
458 // Make sure duration is not longer than clip
461 pedit->setPosition(pos);
463 EffectsList::setParameter(newparam, "in", QString::number(m_in));
464 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
466 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
467 // Make sure duration is not longer than clip
470 pedit->setPosition(pos);
472 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
473 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
476 } else if (type == "wipe") {
477 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
479 if (wp->start_left->isChecked()) info.start = LEFT;
480 else if (wp->start_right->isChecked()) info.start = RIGHT;
481 else if (wp->start_up->isChecked()) info.start = UP;
482 else if (wp->start_down->isChecked()) info.start = DOWN;
483 else if (wp->start_center->isChecked()) info.start = CENTER;
484 else info.start = LEFT;
485 info.startTransparency = wp->start_transp->value();
486 if (wp->end_left->isChecked()) info.end = LEFT;
487 else if (wp->end_right->isChecked()) info.end = RIGHT;
488 else if (wp->end_up->isChecked()) info.end = UP;
489 else if (wp->end_down->isChecked()) info.end = DOWN;
490 else if (wp->end_center->isChecked()) info.end = CENTER;
491 else info.end = RIGHT;
492 info.endTransparency = wp->end_transp->value();
493 setValue = getWipeString(info);
494 } else if ((type == "simplekeyframe" || type == "keyframe") && m_keyframeEditor) {
495 QString realName = i18n(na.toElement().text().toUtf8().data());
496 QString val = m_keyframeEditor->getValue(realName);
497 kDebug() << "SET VALUE: " << val;
498 namenode.item(i).toElement().setAttribute("keyframes", val);
500 if (!setValue.isNull()) {
501 pa.attributes().namedItem("value").setNodeValue(setValue);
504 emit parameterChanged(oldparam, newparam);
507 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
509 QWidget* toFillin = new QWidget(m_baseWidget);
510 Constval *ctval = new Constval;
511 ctval->setupUi(toFillin);
512 ctval->horizontalSlider->setMinimum(min);
513 ctval->horizontalSlider->setMaximum(max);
514 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
515 ctval->spinBox->setMinimum(min);
516 ctval->spinBox->setMaximum(max);
517 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
518 ctval->horizontalSlider->setValue(val);
519 ctval->label->setText(name);
520 m_valueItems[name] = ctval;
521 m_uiItems.append(ctval);
522 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
523 m_vbox->addWidget(toFillin);
526 void EffectStackEdit::slotSliderMoved(int)
528 collectAllParameters();
531 void EffectStackEdit::clearAllItems()
534 m_valueItems.clear();
536 /*while (!m_items.isEmpty()) {
537 QWidget *die = m_items.takeFirst();
541 //qDeleteAll(m_uiItems);
543 while ((child = m_vbox->takeAt(0)) != 0) {
544 QWidget *wid = child->widget();
548 m_keyframeEditor = NULL;