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