]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
Try to avoid effect crash
[kdenlive] / src / effectstackedit.cpp
1 /***************************************************************************
2                           effecstackview.cpp  -  description
3                              -------------------
4     begin                : Feb 15 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
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_keyframeeditor_ui.h"
25 #include "complexparameter.h"
26 #include "geometryval.h"
27 #include "keyframeedit.h"
28 #include "positionedit.h"
29 #include "effectslist.h"
30 #include "kdenlivesettings.h"
31 #include "profilesdialog.h"
32
33 #include <KDebug>
34 #include <KLocale>
35
36 #include <QVBoxLayout>
37 #include <QSlider>
38 #include <QLabel>
39 #include <QPushButton>
40 #include <QCheckBox>
41 #include <QScrollArea>
42
43
44 class Boolval: public EffectStackEdit::UiItem, public Ui::Boolval_UI
45 {
46 };
47
48 class Colorval: public EffectStackEdit::UiItem, public Ui::Colorval_UI
49 {
50 };
51
52 class Constval: public EffectStackEdit::UiItem, public Ui::Constval_UI
53 {
54 };
55
56 class Listval: public EffectStackEdit::UiItem, public Ui::Listval_UI
57 {
58 };
59
60 class Wipeval: public EffectStackEdit::UiItem, public Ui::Wipeval_UI
61 {
62 };
63
64
65 QMap<QString, QImage> EffectStackEdit::iconCache;
66
67 EffectStackEdit::EffectStackEdit(QWidget *parent) :
68         QWidget(parent),
69         m_in(0),
70         m_out(0),
71         m_frameSize(QPoint())
72 {
73     setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
74     QVBoxLayout *vbox1 = new QVBoxLayout(parent);
75     vbox1->setContentsMargins(0, 0, 0, 0);
76     vbox1->setSpacing(0);
77
78     QScrollArea *area = new QScrollArea;
79     QWidget *wid = new QWidget(parent);
80     area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
81     area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
82     area->setFrameStyle(QFrame::NoFrame);
83     wid->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
84     area->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
85
86     vbox1->addWidget(area);
87     area->setWidget(wid);
88     area->setWidgetResizable(true);
89     m_vbox = new QVBoxLayout(wid);
90     m_vbox->setContentsMargins(0, 0, 0, 0);
91     m_vbox->setSpacing(0);
92     wid->show();
93
94 }
95
96 EffectStackEdit::~EffectStackEdit()
97 {
98     iconCache.clear();
99 }
100
101 void EffectStackEdit::setFrameSize(QPoint p)
102 {
103     m_frameSize = p;
104     QDomNodeList namenode = m_params.elementsByTagName("parameter");
105     for (int i = 0; i < namenode.count() ; i++) {
106         QDomNode pa = namenode.item(i);
107         QDomNode na = pa.firstChildElement("name");
108         QString type = pa.attributes().namedItem("type").nodeValue();
109         QString paramName = i18n(na.toElement().text().toUtf8().data());
110
111         if (type == "geometry") {
112             Geometryval *geom = ((Geometryval*)m_valueItems[paramName+"geometry"]);
113             geom->setFrameSize(m_frameSize);
114             break;
115         }
116     }
117
118 }
119
120 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t)
121 {
122     m_profile = profile;
123     m_timecode = t;
124 }
125
126 void EffectStackEdit::updateParameter(const QString &name, const QString &value)
127 {
128     m_params.setAttribute(name, value);
129 }
130
131 void EffectStackEdit::transferParamDesc(const QDomElement d, int in, int out)
132 {
133     kDebug() << " + + + +DELETING EFFECT STACK";
134     clearAllItems();
135     m_params = d;
136     m_in = in;
137     m_out = out;
138     if (m_params.isNull()) {
139         return;
140     }
141
142     /*QDomDocument doc;
143     doc.appendChild(doc.importNode(m_params, true));
144     kDebug() << "IMPORTED TRANS: " << doc.toString();*/
145
146     QDomNodeList namenode = m_params.elementsByTagName("parameter");
147     QDomElement e = m_params.toElement();
148     const int minFrame = e.attribute("start").toInt();
149     const int maxFrame = e.attribute("end").toInt();
150
151
152     for (int i = 0; i < namenode.count() ; i++) {
153         kDebug() << "in form";
154         QDomElement pa = namenode.item(i).toElement();
155         QDomNode na = pa.firstChildElement("name");
156         QString type = pa.attribute("type");
157         QString paramName = i18n(na.toElement().text().toUtf8().data());
158         QWidget * toFillin = new QWidget;
159         QString value = pa.attribute("value").isNull() ?
160                         pa.attribute("default") : pa.attribute("value");
161
162         if (type == "geometry") {
163             /*pa.setAttribute("namedesc", "X;Y;Width;Height;Transparency");
164             pa.setAttribute("format", "%d%,%d%:%d%x%d%:%d");
165             pa.setAttribute("min", "-500;-500;0;0;0");
166             pa.setAttribute("max", "500;500;200;200;100");*/
167         } else if (type == "complex") {
168             //pa.setAttribute("namedesc",pa.attribute("name"));
169
170         }
171
172
173         //TODO constant, list, bool, complex , color, geometry, position
174         if (type == "double" || type == "constant") {
175             int min;
176             int max;
177             if (pa.attribute("min").startsWith('%')) {
178                 min = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("min"));
179             } else min = pa.attribute("min").toInt();
180             if (pa.attribute("max").startsWith('%')) {
181                 max = (int) ProfilesDialog::getStringEval(m_profile, pa.attribute("max"));
182             } else max = pa.attribute("max").toInt();
183             createSliderItem(paramName, (int)(value.toDouble() + 0.5) , min, max, pa.attribute("suffix", QString()));
184             delete toFillin;
185             toFillin = NULL;
186         } else if (type == "list") {
187             Listval *lsval = new Listval;
188             lsval->setupUi(toFillin);
189             QStringList listitems = pa.attribute("paramlist").split(',');
190             QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(',');
191             if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
192             //lsval->list->addItems(listitems);
193             lsval->list->setIconSize(QSize(30, 30));
194             for (int i = 0; i < listitems.count(); i++) {
195                 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
196                 QString entry = listitems.at(i);
197                 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
198                     if (!EffectStackEdit::iconCache.contains(entry)) {
199                         QImage pix(entry);
200                         EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
201                     }
202                     lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
203                 }
204             }
205             if (!value.isEmpty()) lsval->list->setCurrentIndex(listitems.indexOf(value));
206             lsval->title->setTitle(paramName);
207             m_valueItems[paramName] = lsval;
208             connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
209             m_uiItems.append(lsval);
210         } else if (type == "bool") {
211             Boolval *bval = new Boolval;
212             bval->setupUi(toFillin);
213             bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
214             bval->checkBox->setText(paramName);
215             m_valueItems[paramName] = bval;
216             connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
217             m_uiItems.append(bval);
218         } else if (type == "complex") {
219             /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(';');
220             QStringList max=nodeAtts.namedItem("max").nodeValue().split(';');
221             QStringList min=nodeAtts.namedItem("min").nodeValue().split(';');
222             QStringList val=value.split(';');
223             kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size()  ;
224             if ( (names.size() == max.size() ) &&
225                  (names.size()== min.size()) &&
226                  (names.size()== val.size()) )
227             {
228              for (int i=0;i< names.size();i++){
229               createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
230              };
231             }*/
232             ComplexParameter *pl = new ComplexParameter;
233             pl->setupParam(d, pa.attribute("name"), 0, 100);
234             m_vbox->addWidget(pl);
235             m_valueItems[paramName+"complex"] = pl;
236             connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
237             m_items.append(pl);
238         } else if (type == "geometry") {
239             Geometryval *geo = new Geometryval(m_profile, m_frameSize);
240             geo->setupParam(pa, minFrame, maxFrame);
241             m_vbox->addWidget(geo);
242             m_valueItems[paramName+"geometry"] = geo;
243             connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
244             connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
245             m_items.append(geo);
246         } else if (type == "keyframe") {
247             // keyframe editor widget
248             kDebug() << "min: " << m_in << ", MAX: " << m_out;
249             KeyframeEdit *geo = new KeyframeEdit(pa, m_out - m_in, pa.attribute("min").toInt(), pa.attribute("max").toInt(), m_timecode);
250             //geo->setupParam(100, pa.attribute("min").toInt(), pa.attribute("max").toInt(), pa.attribute("keyframes"));
251             //connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
252             //geo->setupParam(pa, minFrame, maxFrame);
253             m_vbox->addWidget(geo);
254             m_valueItems[paramName+"keyframe"] = geo;
255             connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
256             m_items.append(geo);
257         } else if (type == "color") {
258             Colorval *cval = new Colorval;
259             cval->setupUi(toFillin);
260             bool ok;
261             if (value.startsWith('#')) value = value.replace('#', "0x");
262             cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
263             //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
264             cval->label->setText(paramName);
265             m_valueItems[paramName] = cval;
266             connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
267             m_uiItems.append(cval);
268         } else if (type == "position") {
269             int pos = value.toInt();
270             if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
271                 pos = pos - m_in;
272             } else if (d.attribute("id") == "fadeout" || d.attribute("id") == "fade_to_black") {
273                 // fadeout position starts from clip end
274                 pos = m_out - (pos - m_in);
275             }
276             PositionEdit *posedit = new PositionEdit(paramName, pos, 1, m_out, m_timecode);
277             m_vbox->addWidget(posedit);
278             m_valueItems[paramName+"position"] = posedit;
279             connect(posedit, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
280             m_items.append(posedit);
281         } else if (type == "wipe") {
282             Wipeval *wpval = new Wipeval;
283             wpval->setupUi(toFillin);
284             wipeInfo w = getWipeInfo(value);
285             switch (w.start) {
286             case UP:
287                 wpval->start_up->setChecked(true);
288                 break;
289             case DOWN:
290                 wpval->start_down->setChecked(true);
291                 break;
292             case RIGHT:
293                 wpval->start_right->setChecked(true);
294                 break;
295             case LEFT:
296                 wpval->start_left->setChecked(true);
297                 break;
298             default:
299                 wpval->start_center->setChecked(true);
300                 break;
301             }
302             switch (w.end) {
303             case UP:
304                 wpval->end_up->setChecked(true);
305                 break;
306             case DOWN:
307                 wpval->end_down->setChecked(true);
308                 break;
309             case RIGHT:
310                 wpval->end_right->setChecked(true);
311                 break;
312             case LEFT:
313                 wpval->end_left->setChecked(true);
314                 break;
315             default:
316                 wpval->end_center->setChecked(true);
317                 break;
318             }
319             wpval->start_transp->setValue(w.startTransparency);
320             wpval->end_transp->setValue(w.endTransparency);
321             m_valueItems[paramName] = wpval;
322             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
323             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
324             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
325             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
326             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
327             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
328             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
329             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
330             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
331             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
332             connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
333             connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
334             //wpval->title->setTitle(na.toElement().text());
335             m_uiItems.append(wpval);
336         } else {
337             delete toFillin;
338             toFillin = NULL;
339         }
340
341         if (toFillin) {
342             m_items.append(toFillin);
343             m_vbox->addWidget(toFillin);
344         }
345     }
346     m_vbox->addStretch();
347 }
348
349 void EffectStackEdit::slotSeekToPos(int pos)
350 {
351     emit seekTimeline(m_in + pos);
352 }
353
354 wipeInfo EffectStackEdit::getWipeInfo(QString value)
355 {
356     wipeInfo info;
357     QString start = value.section(';', 0, 0);
358     QString end = value.section(';', 1, 1).section('=', 1, 1);
359     if (start.startsWith("-100%,0")) info.start = LEFT;
360     else if (start.startsWith("100%,0")) info.start = RIGHT;
361     else if (start.startsWith("0%,100%")) info.start = DOWN;
362     else if (start.startsWith("0%,-100%")) info.start = UP;
363     else info.start = CENTER;
364     if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
365     else info.startTransparency = 100;
366
367     if (end.startsWith("-100%,0")) info.end = LEFT;
368     else if (end.startsWith("100%,0")) info.end = RIGHT;
369     else if (end.startsWith("0%,100%")) info.end = DOWN;
370     else if (end.startsWith("0%,-100%")) info.end = UP;
371     else info.end = CENTER;
372     if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
373     else info.endTransparency = 100;
374     return info;
375 }
376
377 QString EffectStackEdit::getWipeString(wipeInfo info)
378 {
379
380     QString start;
381     QString end;
382     switch (info.start) {
383     case LEFT:
384         start = "-100%,0%:100%x100%";
385         break;
386     case RIGHT:
387         start = "100%,0%:100%x100%";
388         break;
389     case DOWN:
390         start = "0%,100%:100%x100%";
391         break;
392     case UP:
393         start = "0%,-100%:100%x100%";
394         break;
395     default:
396         start = "0%,0%:100%x100%";
397         break;
398     }
399     start.append(':' + QString::number(info.startTransparency));
400
401     switch (info.end) {
402     case LEFT:
403         end = "-100%,0%:100%x100%";
404         break;
405     case RIGHT:
406         end = "100%,0%:100%x100%";
407         break;
408     case DOWN:
409         end = "0%,100%:100%x100%";
410         break;
411     case UP:
412         end = "0%,-100%:100%x100%";
413         break;
414     default:
415         end = "0%,0%:100%x100%";
416         break;
417     }
418     end.append(':' + QString::number(info.endTransparency));
419     return QString(start + ";-1=" + end);
420 }
421
422 void EffectStackEdit::collectAllParameters()
423 {
424     if (m_valueItems.isEmpty()) return;
425
426     const QDomElement oldparam = m_params.cloneNode().toElement();
427     QDomElement newparam = oldparam.cloneNode().toElement();
428     QDomNodeList namenode = newparam.elementsByTagName("parameter");
429
430     for (int i = 0; i < namenode.count() ; i++) {
431         QDomNode pa = namenode.item(i);
432         QDomNode na = pa.firstChildElement("name");
433         QString type = pa.attributes().namedItem("type").nodeValue();
434         QString paramName = i18n(na.toElement().text().toUtf8().data());
435         if (type == "complex") paramName.append("complex");
436         else if (type == "position") paramName.append("position");
437         else if (type == "geometry") paramName.append("geometry");
438         else if (type == "keyframe") paramName.append("keyframe");
439         if (!m_valueItems.contains(paramName)) {
440             // kDebug() << "// Param: " << paramName << " NOT FOUND";
441             continue;
442         }
443
444         QString setValue;
445         if (type == "double" || type == "constant") {
446             QSlider* slider = ((Constval*)m_valueItems.value(paramName))->horizontalSlider;
447             setValue = QString::number(slider->value());
448         } else if (type == "list") {
449             KComboBox *box = ((Listval*)m_valueItems.value(paramName))->list;
450             setValue = box->itemData(box->currentIndex()).toString();
451         } else if (type == "bool") {
452             QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
453             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
454         } else if (type == "color") {
455             KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
456             setValue = color->color().name();
457         } else if (type == "complex") {
458             ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
459             namenode.item(i) = complex->getParamDesc();
460         } else if (type == "geometry") {
461             Geometryval *geom = ((Geometryval*)m_valueItems.value(paramName));
462             namenode.item(i) = geom->getParamDesc();
463         } else if (type == "position") {
464             PositionEdit *pedit = ((PositionEdit*)m_valueItems.value(paramName));
465             int pos = pedit->getPosition();
466             setValue = QString::number(pos);
467             if (newparam.attribute("id") == "fadein" || newparam.attribute("id") == "fade_from_black") {
468                 // Make sure duration is not longer than clip
469                 /*if (pos > m_out) {
470                     pos = m_out;
471                     pedit->setPosition(pos);
472                 }*/
473                 EffectsList::setParameter(newparam, "in", QString::number(m_in));
474                 EffectsList::setParameter(newparam, "out", QString::number(m_in + pos));
475                 setValue.clear();
476             } else if (newparam.attribute("id") == "fadeout" || newparam.attribute("id") == "fade_to_black") {
477                 // Make sure duration is not longer than clip
478                 /*if (pos > m_out) {
479                     pos = m_out;
480                     pedit->setPosition(pos);
481                 }*/
482                 EffectsList::setParameter(newparam, "in", QString::number(m_out + m_in - pos));
483                 EffectsList::setParameter(newparam, "out", QString::number(m_out + m_in));
484                 setValue.clear();
485             }
486         } else if (type == "wipe") {
487             Wipeval *wp = (Wipeval*)m_valueItems.value(paramName);
488             wipeInfo info;
489             if (wp->start_left->isChecked()) info.start = LEFT;
490             else if (wp->start_right->isChecked()) info.start = RIGHT;
491             else if (wp->start_up->isChecked()) info.start = UP;
492             else if (wp->start_down->isChecked()) info.start = DOWN;
493             else if (wp->start_center->isChecked()) info.start = CENTER;
494             else info.start = LEFT;
495             info.startTransparency = wp->start_transp->value();
496             if (wp->end_left->isChecked()) info.end = LEFT;
497             else if (wp->end_right->isChecked()) info.end = RIGHT;
498             else if (wp->end_up->isChecked()) info.end = UP;
499             else if (wp->end_down->isChecked()) info.end = DOWN;
500             else if (wp->end_center->isChecked()) info.end = CENTER;
501             else info.end = RIGHT;
502             info.endTransparency = wp->end_transp->value();
503             setValue = getWipeString(info);
504         }
505
506         if (!setValue.isNull()) {
507             pa.attributes().namedItem("value").setNodeValue(setValue);
508         }
509     }
510     emit parameterChanged(oldparam, newparam);
511 }
512
513 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max, const QString suffix)
514 {
515     QWidget* toFillin = new QWidget;
516     Constval *ctval = new Constval;
517     ctval->setupUi(toFillin);
518     ctval->horizontalSlider->setMinimum(min);
519     ctval->horizontalSlider->setMaximum(max);
520     if (!suffix.isEmpty()) ctval->spinBox->setSuffix(suffix);
521     ctval->spinBox->setMinimum(min);
522     ctval->spinBox->setMaximum(max);
523     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
524     ctval->horizontalSlider->setValue(val);
525     ctval->label->setText(name);
526     m_valueItems[name] = ctval;
527     m_uiItems.append(ctval);
528     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
529     m_items.append(toFillin);
530     m_vbox->addWidget(toFillin);
531 }
532
533 void EffectStackEdit::slotSliderMoved(int)
534 {
535     collectAllParameters();
536 }
537
538 void EffectStackEdit::clearAllItems()
539 {
540     blockSignals(true);
541     m_valueItems.clear();
542
543     while (!m_items.isEmpty()) {
544         QWidget * die = m_items.takeFirst();
545         die->disconnect();
546         delete die;
547     }
548
549     qDeleteAll(m_uiItems);
550     m_uiItems.clear();
551     m_items.clear();
552     QLayoutItem *item = m_vbox->itemAt(0);
553     while (item) {
554         m_vbox->removeItem(item);
555         delete item;
556         item = m_vbox->itemAt(0);
557     }
558     blockSignals(false);
559 }