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