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()
91 void EffectStackEdit::setFrameSize(QPoint p)
94 QDomNodeList namenode = m_params.elementsByTagName("parameter");
95 for (int i = 0; i < namenode.count() ; i++) {
96 QDomNode pa = namenode.item(i);
97 QDomNode na = pa.firstChildElement("name");
98 QString type = pa.attributes().namedItem("type").nodeValue();
99 QString paramName = i18n(na.toElement().text().toUtf8().data());
101 if (type == "geometry") {
102 Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
103 geom->setFrameSize(m_frameSize);
110 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
116 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
118 m_params.setAttribute(name, value);
121 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
124 m_keyframeEditor = NULL;
128 if (m_params.isNull()) {
129 kDebug() << "// EMPTY EFFECT STACK";
134 doc.appendChild(doc.importNode(m_params, true));
135 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
137 QDomNodeList namenode = m_params.elementsByTagName("parameter");
138 QDomElement e = m_params.toElement();
139 const int minFrame = e.attribute("start").toInt();
140 const int maxFrame = e.attribute("end").toInt();
143 for (int i = 0; i < namenode.count() ; i++) {
144 QDomElement pa = namenode.item(i).toElement();
145 QDomNode na = pa.firstChildElement("name");
146 QString type = pa.attribute("type");
147 QString paramName = i18n(na.toElement().text().toUtf8().data());
148 QWidget * toFillin = new QWidget(m_baseWidget);
149 QString value = pa.attribute("value").isNull() ?
150 pa.attribute("default") : pa.attribute("value");
152 /** Currently supported parameter types are:
153 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
154 * list: a combobox containing a list of values to choose
156 * complex: designed for keyframe parameters, but old and not finished, do not use
157 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
158 * keyframe: a list widget with a list of entries (position and value)
159 * color: a color chooser button
160 * position: a slider representing the position of a frame in the current clip
161 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
164 if (type == "double" || type == "constant") {
167 if (pa.attribute("min").startsWith('%')) {
168 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
169 } else min = pa.attribute("min").toInt();
170 if (pa.attribute("max").startsWith('%')) {
171 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
172 } else max = pa.attribute("max").toInt();
173 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
176 } else if (type == "list") {
177 Listval *lsval = new Listval;
178 lsval->setupUi(toFillin);
179 QStringList listitems = pa.attribute("paramlist").split(',');
180 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
181 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
182 //lsval->list->addItems(listitems);
183 lsval->list->setIconSize(QSize(30, 30));
184 for (int i = 0; i < listitems.count(); i++) {
185 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
186 QString entry = listitems.at(i);
187 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
188 if (!EffectStackEdit::iconCache.contains(entry)) {
190 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
192 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
195 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
196 lsval->title->setTitle(paramName);
197 m_valueItems[paramName] = lsval;
198 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
199 m_uiItems.append(lsval);
200 } else if (type == "bool") {
201 Boolval *bval = new Boolval;
202 bval->setupUi(toFillin);
203 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
204 bval->checkBox->setText(paramName);
205 m_valueItems[paramName] = bval;
206 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
207 m_uiItems.append(bval);
208 } else if (type == "complex") {
209 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
210 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
211 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
212 QStringList val=value.split(';');
213 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
214 if ( (names.size() == max.size() ) &&
215 (names.size()== min.size()) &&
216 (names.size()== val.size()) )
218 for (int i=0;i< names.size();i++){
219 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
222 ComplexParameter *pl = new ComplexParameter;
223 pl->setupParam(d, pa.attribute("name"), 0, 100);
224 m_vbox->addWidget(pl);
225 m_valueItems[paramName+"complex"] = pl;
226 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
227 } else if (type == "geometry") {
228 Geometryval *geo = new Geometryval(m_profile, m_frameSize);
229 geo->setupParam(pa, minFrame, maxFrame);
230 m_vbox->addWidget(geo);
231 m_valueItems[paramName+"geometry"] = geo;
232 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
233 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
234 } else if (type == "keyframe" || type == "simplekeyframe") {
235 // keyframe editor widget
236 kDebug() << "min: " << m_in << ", MAX: " << m_out;
237 if (m_keyframeEditor == NULL) {
238 KeyframeEdit *geo = new KeyframeEdit(pa, m_in, m_in + m_out, pa.attribute("min").toInt(), pa.attribute("max").toInt(), m_timecode, paramName);
239 m_vbox->addWidget(geo);
240 m_valueItems[paramName+"keyframe"] = geo;
241 m_keyframeEditor = geo;
242 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
245 // we already have a keyframe editor, so just add another column for the new param
246 m_keyframeEditor->addParameter(pa);
248 } else if (type == "color") {
249 Colorval *cval = new Colorval;
250 cval->setupUi(toFillin);
252 if (value.startsWith('#')) value = value.replace('#', "0x");
253 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
254 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
255 cval->label->setText(paramName);
256 m_valueItems[paramName] = cval;
257 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
258 m_uiItems.append(cval);
259 } else if (type == "position") {
260 int pos = value.toInt();
261 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
263 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
264 // fadeout position starts from clip end
265 pos = m_out - (pos - m_in);
267 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
268 m_vbox->addWidget(posedit);
269 m_valueItems[paramName+"position"] = posedit;
270 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
271 } else if (type == "wipe") {
272 Wipeval *wpval = new Wipeval;
273 wpval->setupUi(toFillin);
274 wipeInfo w = getWipeInfo(value);
277 wpval->start_up->setChecked(true);
280 wpval->start_down->setChecked(true);
283 wpval->start_right->setChecked(true);
286 wpval->start_left->setChecked(true);
289 wpval->start_center->setChecked(true);
294 wpval->end_up->setChecked(true);
297 wpval->end_down->setChecked(true);
300 wpval->end_right->setChecked(true);
303 wpval->end_left->setChecked(true);
306 wpval->end_center->setChecked(true);
309 wpval->start_transp->setValue(w.startTransparency);
310 wpval->end_transp->setValue(w.endTransparency);
311 m_valueItems[paramName] = wpval;
312 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
313 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
314 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
315 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
316 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
317 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
318 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
319 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
320 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
321 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
322 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
323 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
324 //wpval->title->setTitle(na.toElement().text());
325 m_uiItems.append(wpval);
332 m_vbox->addWidget(toFillin);
335 m_vbox->addStretch();
338 void EffectStackEdit::slotSeekToPos(int pos)
340 emit seekTimeline(m_in + pos);
343 wipeInfo EffectStackEdit::getWipeInfo(QString value)
346 QString start = value.section(';', 0, 0);
347 QString end = value.section(';', 1, 1).section('=', 1, 1);
348 if (start.startsWith("-100%,0")) info.start = LEFT;
349 else if (start.startsWith("100%,0")) info.start = RIGHT;
350 else if (start.startsWith("0%,100%")) info.start = DOWN;
351 else if (start.startsWith("0%,-100%")) info.start = UP;
352 else info.start = CENTER;
353 if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
354 else info.startTransparency = 100;
356 if (end.startsWith("-100%,0")) info.end = LEFT;
357 else if (end.startsWith("100%,0")) info.end = RIGHT;
358 else if (end.startsWith("0%,100%")) info.end = DOWN;
359 else if (end.startsWith("0%,-100%")) info.end = UP;
360 else info.end = CENTER;
361 if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
362 else info.endTransparency = 100;
366 QString EffectStackEdit::getWipeString(wipeInfo info)
371 switch (info.start) {
373 start = "-100%,0%:100%x100%";
376 start = "100%,0%:100%x100%";
379 start = "0%,100%:100%x100%";
382 start = "0%,-100%:100%x100%";
385 start = "0%,0%:100%x100%";
388 start.append(':' + QString::number(info.startTransparency));
392 end = "-100%,0%:100%x100%";
395 end = "100%,0%:100%x100%";
398 end = "0%,100%:100%x100%";
401 end = "0%,-100%:100%x100%";
404 end = "0%,0%:100%x100%";
407 end.append(':' + QString::number(info.endTransparency));
408 return QString(start + ";-1=" + end);
411 void EffectStackEdit::collectAllParameters()
413 if (m_valueItems.isEmpty() || m_params.isNull()) return;
414 const QDomElement oldparam = m_params.cloneNode().toElement();
415 QDomElement newparam = oldparam.cloneNode().toElement();
416 QDomNodeList namenode = newparam.elementsByTagName("parameter");
418 for (int i = 0; i < namenode.count() ; i++) {
419 QDomNode pa = namenode.item(i);
420 QDomNode na = pa.firstChildElement("name");
421 QString type = pa.attributes().namedItem("type").nodeValue();
422 QString paramName = i18n(na.toElement().text().toUtf8().data());
423 if (type == "complex") paramName.append("complex");
424 else if (type == "position") paramName.append("position");
425 else if (type == "geometry") paramName.append("geometry");
426 else if (type == "keyframe") paramName.append("keyframe");
427 if (!m_valueItems.contains(paramName)) {
428 // kDebug() << "// Param: " << paramName << " NOT FOUND";
433 if (type == "double" || type == "constant") {
434 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
435 setValue = QString::number(slider->value());
436 } else if (type == "list") {
437 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
438 setValue = box->itemData(box->currentIndex()).toString();
439 } else if (type == "bool") {
440 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
441 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
442 } else if (type == "color") {
443 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
444 setValue = color->color().name();
445 } else if (type == "complex") {
446 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
447 namenode.item(i) = complex->getParamDesc();
448 } else if (type == "geometry") {
449 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
450 namenode.item(i).toElement().setAttribute("value", geom->getValue());
451 } else if (type == "position") {
452 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
453 int pos = pedit->getPosition();
454 setValue = QString::number(pos);
455 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
456 // Make sure duration is not longer than clip
459 pedit->setPosition(pos);
461 EffectsList::setParameter(newparam, "in", QString::number(m_in));
462 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
464 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
465 // Make sure duration is not longer than clip
468 pedit->setPosition(pos);
470 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
471 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
474 } else if (type == "wipe") {
475 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
477 if (wp->start_left->isChecked()) info.start = LEFT;
478 else if (wp->start_right->isChecked()) info.start = RIGHT;
479 else if (wp->start_up->isChecked()) info.start = UP;
480 else if (wp->start_down->isChecked()) info.start = DOWN;
481 else if (wp->start_center->isChecked()) info.start = CENTER;
482 else info.start = LEFT;
483 info.startTransparency = wp->start_transp->value();
484 if (wp->end_left->isChecked()) info.end = LEFT;
485 else if (wp->end_right->isChecked()) info.end = RIGHT;
486 else if (wp->end_up->isChecked()) info.end = UP;
487 else if (wp->end_down->isChecked()) info.end = DOWN;
488 else if (wp->end_center->isChecked()) info.end = CENTER;
489 else info.end = RIGHT;
490 info.endTransparency = wp->end_transp->value();
491 setValue = getWipeString(info);
494 if (!setValue.isNull()) {
495 pa.attributes().namedItem("value").setNodeValue(setValue);
498 emit parameterChanged(oldparam, newparam);
501 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
503 QWidget* toFillin = new QWidget(m_baseWidget);
504 Constval *ctval = new Constval;
505 ctval->setupUi(toFillin);
506 ctval->horizontalSlider->setMinimum(min);
507 ctval->horizontalSlider->setMaximum(max);
508 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
509 ctval->spinBox->setMinimum(min);
510 ctval->spinBox->setMaximum(max);
511 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
512 ctval->horizontalSlider->setValue(val);
513 ctval->label->setText(name);
514 m_valueItems[name] = ctval;
515 m_uiItems.append(ctval);
516 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
517 m_vbox->addWidget(toFillin);
520 void EffectStackEdit::slotSliderMoved(int)
522 collectAllParameters();
525 void EffectStackEdit::clearAllItems()
528 m_valueItems.clear();
530 /*while (!m_items.isEmpty()) {
531 QWidget *die = m_items.takeFirst();
535 //qDeleteAll(m_uiItems);
537 while ((child = m_vbox->takeAt(0)) != 0) {
538 QWidget *wid = child->widget();