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 m_keyframeEditor = NULL;
129 if (m_params.isNull()) {
130 kDebug() << "// EMPTY EFFECT STACK";
135 doc.appendChild(doc.importNode(m_params, true));
136 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
138 QDomNodeList namenode = m_params.elementsByTagName("parameter");
139 QDomElement e = m_params.toElement();
140 const int minFrame = e.attribute("start").toInt();
141 const int maxFrame = e.attribute("end").toInt();
144 for (int i = 0; i < namenode.count() ; i++) {
145 QDomElement pa = namenode.item(i).toElement();
146 QDomNode na = pa.firstChildElement("name");
147 QString type = pa.attribute("type");
148 QString paramName = i18n(na.toElement().text().toUtf8().data());
149 QWidget * toFillin = new QWidget(m_baseWidget);
150 QString value = pa.attribute("value").isNull() ?
151 pa.attribute("default") : pa.attribute("value");
153 /** Currently supported parameter types are:
154 * constant (=double): a slider with an integer value (use the "factor" attribute to divide the value so that you can get a double
155 * list: a combobox containing a list of values to choose
157 * complex: designed for keyframe parameters, but old and not finished, do not use
158 * geometry: a rectangle that can be moved & resized, with possible keyframes, used in composite transition
159 * keyframe: a list widget with a list of entries (position and value)
160 * color: a color chooser button
161 * position: a slider representing the position of a frame in the current clip
162 * wipe: a widget designed for the wipe transition, allowing to choose a position (left, right, top,...)
165 if (type == "double" || type == "constant") {
168 if (pa.attribute("min").startsWith('%')) {
169 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
170 } else min = pa.attribute("min").toInt();
171 if (pa.attribute("max").startsWith('%')) {
172 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
173 } else max = pa.attribute("max").toInt();
174 createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
177 } else if (type == "list") {
178 Listval *lsval = new Listval;
179 lsval->setupUi(toFillin);
180 QStringList listitems = pa.attribute("paramlist").split(',');
181 QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
182 if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
183 //lsval->list->addItems(listitems);
184 lsval->list->setIconSize(QSize(30, 30));
185 for (int i = 0; i < listitems.count(); i++) {
186 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
187 QString entry = listitems.at(i);
188 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
189 if (!EffectStackEdit::iconCache.contains(entry)) {
191 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
193 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
196 if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
197 lsval->title->setTitle(paramName);
198 m_valueItems[paramName] = lsval;
199 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
200 m_uiItems.append(lsval);
201 } else if (type == "bool") {
202 Boolval *bval = new Boolval;
203 bval->setupUi(toFillin);
204 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
205 bval->checkBox->setText(paramName);
206 m_valueItems[paramName] = bval;
207 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
208 m_uiItems.append(bval);
209 } else if (type == "complex") {
210 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
211 QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
212 QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
213 QStringList val=value.split(';');
214 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
215 if ( (names.size() == max.size() ) &&
216 (names.size()== min.size()) &&
217 (names.size()== val.size()) )
219 for (int i=0;i< names.size();i++){
220 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
223 ComplexParameter *pl = new ComplexParameter;
224 pl->setupParam(d, pa.attribute("name"), 0, 100);
225 m_vbox->addWidget(pl);
226 m_valueItems[paramName+"complex"] = pl;
227 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
228 } else if (type == "geometry") {
229 Geometryval *geo = new Geometryval(m_profile, m_frameSize);
230 geo->setupParam(pa, minFrame, maxFrame);
231 m_vbox->addWidget(geo);
232 m_valueItems[paramName+"geometry"] = geo;
233 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
234 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
235 } else if (type == "keyframe" || type == "simplekeyframe") {
236 // keyframe editor widget
237 kDebug() << "min: " << m_in << ", MAX: " << m_out;
238 if (m_keyframeEditor == NULL) {
239 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());
240 m_vbox->addWidget(geo);
241 m_valueItems[paramName+"keyframe"] = geo;
242 m_keyframeEditor = geo;
243 connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
244 connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
246 // we already have a keyframe editor, so just add another column for the new param
247 m_keyframeEditor->addParameter(pa);
249 } else if (type == "color") {
250 Colorval *cval = new Colorval;
251 cval->setupUi(toFillin);
253 if (value.startsWith('#')) value = value.replace('#', "0x");
254 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
255 //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
256 cval->label->setText(paramName);
257 m_valueItems[paramName] = cval;
258 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
259 m_uiItems.append(cval);
260 } else if (type == "position") {
261 int pos = value.toInt();
262 if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
264 } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
265 // fadeout position starts from clip end
266 pos = m_out - (pos - m_in);
268 PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
269 m_vbox->addWidget(posedit);
270 m_valueItems[paramName+"position"] = posedit;
271 connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
272 } else if (type == "wipe") {
273 Wipeval *wpval = new Wipeval;
274 wpval->setupUi(toFillin);
275 wipeInfo w = getWipeInfo(value);
278 wpval->start_up->setChecked(true);
281 wpval->start_down->setChecked(true);
284 wpval->start_right->setChecked(true);
287 wpval->start_left->setChecked(true);
290 wpval->start_center->setChecked(true);
295 wpval->end_up->setChecked(true);
298 wpval->end_down->setChecked(true);
301 wpval->end_right->setChecked(true);
304 wpval->end_left->setChecked(true);
307 wpval->end_center->setChecked(true);
310 wpval->start_transp->setValue(w.startTransparency);
311 wpval->end_transp->setValue(w.endTransparency);
312 m_valueItems[paramName] = wpval;
313 connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
314 connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
315 connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
316 connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
317 connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
318 connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
319 connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
320 connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
321 connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
322 connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
323 connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
324 connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
325 //wpval->title->setTitle(na.toElement().text());
326 m_uiItems.append(wpval);
333 m_vbox->addWidget(toFillin);
336 m_vbox->addStretch();
339 void EffectStackEdit::slotSeekToPos(int pos)
341 emit seekTimeline(m_in + pos);
344 wipeInfo EffectStackEdit::getWipeInfo(QString value)
347 QString start = value.section(';', 0, 0);
348 QString end = value.section(';', 1, 1).section('=', 1, 1);
349 if (start.startsWith("-100%,0")) info.start = LEFT;
350 else if (start.startsWith("100%,0")) info.start = RIGHT;
351 else if (start.startsWith("0%,100%")) info.start = DOWN;
352 else if (start.startsWith("0%,-100%")) info.start = UP;
353 else info.start = CENTER;
354 if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
355 else info.startTransparency = 100;
357 if (end.startsWith("-100%,0")) info.end = LEFT;
358 else if (end.startsWith("100%,0")) info.end = RIGHT;
359 else if (end.startsWith("0%,100%")) info.end = DOWN;
360 else if (end.startsWith("0%,-100%")) info.end = UP;
361 else info.end = CENTER;
362 if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
363 else info.endTransparency = 100;
367 QString EffectStackEdit::getWipeString(wipeInfo info)
372 switch (info.start) {
374 start = "-100%,0%:100%x100%";
377 start = "100%,0%:100%x100%";
380 start = "0%,100%:100%x100%";
383 start = "0%,-100%:100%x100%";
386 start = "0%,0%:100%x100%";
389 start.append(':' + QString::number(info.startTransparency));
393 end = "-100%,0%:100%x100%";
396 end = "100%,0%:100%x100%";
399 end = "0%,100%:100%x100%";
402 end = "0%,-100%:100%x100%";
405 end = "0%,0%:100%x100%";
408 end.append(':' + QString::number(info.endTransparency));
409 return QString(start + ";-1=" + end);
412 void EffectStackEdit::collectAllParameters()
414 if (m_valueItems.isEmpty() || m_params.isNull()) return;
415 const QDomElement oldparam = m_params.cloneNode().toElement();
416 QDomElement newparam = oldparam.cloneNode().toElement();
417 QDomNodeList namenode = newparam.elementsByTagName("parameter");
419 for (int i = 0; i < namenode.count() ; i++) {
420 QDomNode pa = namenode.item(i);
421 QDomNode na = pa.firstChildElement("name");
422 QString type = pa.attributes().namedItem("type").nodeValue();
423 QString paramName = i18n(na.toElement().text().toUtf8().data());
424 if (type == "complex") paramName.append("complex");
425 else if (type == "position") paramName.append("position");
426 else if (type == "geometry") paramName.append("geometry");
427 else if (type == "keyframe") paramName.append("keyframe");
428 if (!m_valueItems.contains(paramName)) {
429 // kDebug() << "// Param: " << paramName << " NOT FOUND";
434 if (type == "double" || type == "constant") {
435 QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
436 setValue = QString::number(slider->value());
437 } else if (type == "list") {
438 KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
439 setValue = box->itemData(box->currentIndex()).toString();
440 } else if (type == "bool") {
441 QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
442 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
443 } else if (type == "color") {
444 KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
445 setValue = color->color().name();
446 } else if (type == "complex") {
447 ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
448 namenode.item(i) = complex->getParamDesc();
449 } else if (type == "geometry") {
450 Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
451 namenode.item(i).toElement().setAttribute("value", geom->getValue());
452 } else if (type == "position") {
453 PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
454 int pos = pedit->getPosition();
455 setValue = QString::number(pos);
456 if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
457 // Make sure duration is not longer than clip
460 pedit->setPosition(pos);
462 EffectsList::setParameter(newparam, "in", QString::number(m_in));
463 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
465 } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
466 // Make sure duration is not longer than clip
469 pedit->setPosition(pos);
471 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
472 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
475 } else if (type == "wipe") {
476 Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
478 if (wp->start_left->isChecked()) info.start = LEFT;
479 else if (wp->start_right->isChecked()) info.start = RIGHT;
480 else if (wp->start_up->isChecked()) info.start = UP;
481 else if (wp->start_down->isChecked()) info.start = DOWN;
482 else if (wp->start_center->isChecked()) info.start = CENTER;
483 else info.start = LEFT;
484 info.startTransparency = wp->start_transp->value();
485 if (wp->end_left->isChecked()) info.end = LEFT;
486 else if (wp->end_right->isChecked()) info.end = RIGHT;
487 else if (wp->end_up->isChecked()) info.end = UP;
488 else if (wp->end_down->isChecked()) info.end = DOWN;
489 else if (wp->end_center->isChecked()) info.end = CENTER;
490 else info.end = RIGHT;
491 info.endTransparency = wp->end_transp->value();
492 setValue = getWipeString(info);
495 if (!setValue.isNull()) {
496 pa.attributes().namedItem("value").setNodeValue(setValue);
499 emit parameterChanged(oldparam, newparam);
502 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
504 QWidget* toFillin = new QWidget(m_baseWidget);
505 Constval *ctval = new Constval;
506 ctval->setupUi(toFillin);
507 ctval->horizontalSlider->setMinimum(min);
508 ctval->horizontalSlider->setMaximum(max);
509 if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
510 ctval->spinBox->setMinimum(min);
511 ctval->spinBox->setMaximum(max);
512 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
513 ctval->horizontalSlider->setValue(val);
514 ctval->label->setText(name);
515 m_valueItems[name] = ctval;
516 m_uiItems.append(ctval);
517 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
518 m_vbox->addWidget(toFillin);
521 void EffectStackEdit::slotSliderMoved(int)
523 collectAllParameters();
526 void EffectStackEdit::clearAllItems()
529 m_valueItems.clear();
531 /*while (!m_items.isEmpty()) {
532 QWidget *die = m_items.takeFirst();
536 //qDeleteAll(m_uiItems);
538 while ((child = m_vbox->takeAt(0)) != 0) {
539 QWidget *wid = child->widget();