]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
Fix remaining issues with audio fades:
[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 <QVBoxLayout>
19 #include <QSlider>
20 #include <QLabel>
21 #include <QPushButton>
22 #include <QCheckBox>
23 #include <QScrollArea>
24
25 #include <KDebug>
26 #include <KLocale>
27
28 #include "ui_constval_ui.h"
29 #include "ui_listval_ui.h"
30 #include "ui_boolval_ui.h"
31 #include "ui_colorval_ui.h"
32 #include "ui_positionval_ui.h"
33 #include "ui_wipeval_ui.h"
34 #include "complexparameter.h"
35 #include "effectstackedit.h"
36 #include "geometryval.h"
37
38 #include "kdenlivesettings.h"
39
40 QMap<QString, QImage> EffectStackEdit::iconCache;
41
42 EffectStackEdit::EffectStackEdit(QWidget *parent): QWidget(parent), m_in(0), m_out(0) {
43     setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
44     QVBoxLayout *vbox1 = new QVBoxLayout(parent);
45     vbox1->setContentsMargins(0, 0, 0, 0);
46     vbox1->setSpacing(0);
47
48     QScrollArea *area = new QScrollArea;
49     QWidget *wid = new QWidget(parent);
50     area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
51     area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
52     wid->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
53     area->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));
54
55     vbox1->addWidget(area);
56     area->setWidget(wid);
57     area->setWidgetResizable(true);
58     vbox = new QVBoxLayout(wid);
59     vbox->setContentsMargins(0, 0, 0, 0);
60     vbox->setSpacing(0);
61     wid->show();
62
63 }
64
65 EffectStackEdit::~EffectStackEdit() {
66     iconCache.clear();
67 }
68
69 void EffectStackEdit::updateProjectFormat(MltVideoProfile profile, Timecode t) {
70     m_profile = profile;
71     m_timecode = t;
72 }
73
74 void EffectStackEdit::transferParamDesc(const QDomElement& d, int in, int out) {
75     kDebug() << "in";
76     params = d;
77     m_in = in;
78     m_out = out;
79     clearAllItems();
80     if (params.isNull()) return;
81
82     QDomDocument doc;
83     doc.appendChild(doc.importNode(params, true));
84     kDebug() << "IMPORTED TRANS: " << doc.toString();
85     QDomNodeList namenode = params.elementsByTagName("parameter");
86     QDomElement e = params.toElement();
87     const int minFrame = e.attribute("start").toInt();
88     const int maxFrame = e.attribute("end").toInt();
89
90
91     for (int i = 0;i < namenode.count() ;i++) {
92         kDebug() << "in form";
93         QDomElement pa = namenode.item(i).toElement();
94         QDomNode na = pa.firstChildElement("name");
95         QString type = pa.attribute("type");
96         QString paramName = i18n(na.toElement().text().toUtf8().data());
97         QWidget * toFillin = new QWidget;
98         QString value = pa.attribute("value").isNull() ?
99                         pa.attribute("default") : pa.attribute("value");
100         if (type == "geometry") {
101             /*pa.setAttribute("namedesc", "X;Y;Width;Height;Transparency");
102             pa.setAttribute("format", "%d%,%d%:%d%x%d%:%d");
103             pa.setAttribute("min", "-500;-500;0;0;0");
104             pa.setAttribute("max", "500;500;200;200;100");*/
105         } else if (type == "complex") {
106             //pa.setAttribute("namedesc",pa.attribute("name"));
107
108         }
109
110
111         //TODO constant, list, bool, complex , color, geometry, position
112         if (type == "double" || type == "constant") {
113             createSliderItem(paramName, value.toInt(), pa.attribute("min").toInt(), pa.attribute("max").toInt());
114             delete toFillin;
115             toFillin = NULL;
116         } else if (type == "list") {
117             Ui::Listval_UI *lsval = new Ui::Listval_UI;
118             lsval->setupUi(toFillin);
119             QStringList listitems = pa.attribute("paramlist").split(",");
120             QStringList listitemsdisplay = pa.attribute("paramlistdisplay").split(",");
121             if (listitemsdisplay.count() != listitems.count()) listitemsdisplay = listitems;
122             //lsval->list->addItems(listitems);
123             for (int i = 0;i < listitems.count();i++) {
124                 lsval->list->addItem(listitemsdisplay.at(i), listitems.at(i));
125             }
126             lsval->list->setCurrentIndex(listitems.indexOf(value));
127             for (int i = 0;i < lsval->list->count();i++) {
128                 QString entry = lsval->list->itemData(i).toString();
129                 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
130                     if (!EffectStackEdit::iconCache.contains(entry)) {
131                         QImage pix(entry);
132                         EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
133                     }
134                     lsval->list->setIconSize(QSize(30, 30));
135                     lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
136                 }
137             }
138             connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
139             lsval->title->setTitle(paramName);
140             valueItems[paramName] = lsval;
141             uiItems.append(lsval);
142         } else if (type == "bool") {
143             Ui::Boolval_UI *bval = new Ui::Boolval_UI;
144             bval->setupUi(toFillin);
145             bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
146
147             connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
148             bval->checkBox->setText(paramName);
149             valueItems[paramName] = bval;
150             uiItems.append(bval);
151         } else if (type == "complex") {
152             /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(";");
153             QStringList max=nodeAtts.namedItem("max").nodeValue().split(";");
154             QStringList min=nodeAtts.namedItem("min").nodeValue().split(";");
155             QStringList val=value.split(";");
156             kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size()  ;
157             if ( (names.size() == max.size() ) &&
158                  (names.size()== min.size()) &&
159                  (names.size()== val.size()) )
160             {
161              for (int i=0;i< names.size();i++){
162               createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
163              };
164             }*/
165             ComplexParameter *pl = new ComplexParameter;
166             connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
167             pl->setupParam(d, pa.attribute("name"), 0, 100);
168             vbox->addWidget(pl);
169             valueItems[paramName+"complex"] = pl;
170             items.append(pl);
171         } else if (type == "geometry") {
172             Geometryval *geo = new Geometryval(m_profile);
173             connect(geo, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
174             connect(geo, SIGNAL(seekToPos(int)), this, SLOT(slotSeekToPos(int)));
175             geo->setupParam(pa, minFrame, maxFrame);
176             vbox->addWidget(geo);
177             valueItems[paramName+"geometry"] = geo;
178             items.append(geo);
179         } else if (type == "color") {
180             Ui::Colorval_UI *cval = new Ui::Colorval_UI;
181             cval->setupUi(toFillin);
182             bool ok;
183             cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
184             kDebug() << value.toUInt(&ok, 16);
185
186             connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
187             cval->label->setText(paramName);
188             valueItems[paramName] = cval;
189             uiItems.append(cval);
190         } else if (type == "position") {
191             Ui::Positionval_UI *pval = new Ui::Positionval_UI;
192             pval->setupUi(toFillin);
193             int pos = value.toInt();
194             if (d.attribute("id") == "fadein") {
195                 pos = pos - m_in;
196             } else if (d.attribute("id") == "fadeout") {
197                 // fadeout position starts from clip end
198                 pos = m_out - (pos - m_in);
199             }
200             pval->krestrictedline->setText(m_timecode.getTimecodeFromFrames(pos));
201             connect(pval->krestrictedline, SIGNAL(editingFinished()), this, SLOT(collectAllParameters()));
202             pval->label->setText(paramName);
203             valueItems[paramName + "position"] = pval;
204             uiItems.append(pval);
205         } else if (type == "wipe") {
206             Ui::Wipeval_UI *wpval = new Ui::Wipeval_UI;
207             wpval->setupUi(toFillin);
208             wipeInfo w = getWipeInfo(value);
209             switch (w.start) {
210             case UP:
211                 wpval->start_up->setChecked(true);
212                 break;
213             case DOWN:
214                 wpval->start_down->setChecked(true);
215                 break;
216             case RIGHT:
217                 wpval->start_right->setChecked(true);
218                 break;
219             case LEFT:
220                 wpval->start_left->setChecked(true);
221                 break;
222             default:
223                 wpval->start_center->setChecked(true);
224                 break;
225             }
226             switch (w.end) {
227             case UP:
228                 wpval->end_up->setChecked(true);
229                 break;
230             case DOWN:
231                 wpval->end_down->setChecked(true);
232                 break;
233             case RIGHT:
234                 wpval->end_right->setChecked(true);
235                 break;
236             case LEFT:
237                 wpval->end_left->setChecked(true);
238                 break;
239             default:
240                 wpval->end_center->setChecked(true);
241                 break;
242             }
243             wpval->start_transp->setValue(w.startTransparency);
244             wpval->end_transp->setValue(w.endTransparency);
245
246             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
247             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
248             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
249             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
250             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
251             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
252             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
253             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
254             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
255             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
256             connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
257             connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
258             //wpval->title->setTitle(na.toElement().text());
259             valueItems[paramName] = wpval;
260             uiItems.append(wpval);
261         } else {
262             delete toFillin;
263             toFillin = NULL;
264         }
265
266         if (toFillin) {
267             items.append(toFillin);
268             vbox->addWidget(toFillin);
269         }
270     }
271     vbox->addStretch();
272 }
273
274 void EffectStackEdit::slotSeekToPos(int pos) {
275     emit seekTimeline(m_in + pos);
276 }
277
278 wipeInfo EffectStackEdit::getWipeInfo(QString value) {
279     wipeInfo info;
280     QString start = value.section(";", 0, 0);
281     QString end = value.section(";", 1, 1).section("=", 1, 1);
282     if (start.startsWith("-100%,0")) info.start = LEFT;
283     else if (start.startsWith("100%,0")) info.start = RIGHT;
284     else if (start.startsWith("0%,100%")) info.start = DOWN;
285     else if (start.startsWith("0%,-100%")) info.start = UP;
286     else if (start.startsWith("0%,0%")) info.start = CENTER;
287     if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
288     else info.startTransparency = 100;
289
290     if (end.startsWith("-100%,0")) info.end = LEFT;
291     else if (end.startsWith("100%,0")) info.end = RIGHT;
292     else if (end.startsWith("0%,100%")) info.end = DOWN;
293     else if (end.startsWith("0%,-100%")) info.end = UP;
294     else if (end.startsWith("0%,0%")) info.end = CENTER;
295     if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
296     else info.endTransparency = 100;
297     return info;
298 }
299
300 QString EffectStackEdit::getWipeString(wipeInfo info) {
301
302     QString start;
303     QString end;
304     switch (info.start) {
305     case LEFT:
306         start = "-100%,0%:100%x100%";
307         break;
308     case RIGHT:
309         start = "100%,0%:100%x100%";
310         break;
311     case DOWN:
312         start = "0%,100%:100%x100%";
313         break;
314     case UP:
315         start = "0%,-100%:100%x100%";
316         break;
317     default:
318         start = "0%,0%:100%x100%";
319         break;
320     }
321     start.append(":" + QString::number(info.startTransparency));
322
323     switch (info.end) {
324     case LEFT:
325         end = "-100%,0%:100%x100%";
326         break;
327     case RIGHT:
328         end = "100%,0%:100%x100%";
329         break;
330     case DOWN:
331         end = "0%,100%:100%x100%";
332         break;
333     case UP:
334         end = "0%,-100%:100%x100%";
335         break;
336     default:
337         end = "0%,0%:100%x100%";
338         break;
339     }
340     end.append(":" + QString::number(info.endTransparency));
341     return QString(start + ";-1=" + end);
342 }
343
344 void EffectStackEdit::collectAllParameters() {
345     QDomElement oldparam = params.cloneNode().toElement();
346     QDomNodeList namenode = params.elementsByTagName("parameter");
347
348     for (int i = 0;i < namenode.count() ;i++) {
349         QDomNode pa = namenode.item(i);
350         QDomNode na = pa.firstChildElement("name");
351         QString type = pa.attributes().namedItem("type").nodeValue();
352         QString paramName = i18n(na.toElement().text().toUtf8().data());
353
354         QString setValue;
355         if (type == "double" || type == "constant") {
356             QSlider* slider = ((Ui::Constval_UI*)valueItems[paramName])->horizontalSlider;
357             setValue = QString::number(slider->value());
358         } else if (type == "list") {
359             KComboBox *box = ((Ui::Listval_UI*)valueItems[paramName])->list;
360             setValue = box->itemData(box->currentIndex()).toString();
361         } else if (type == "bool") {
362             QCheckBox *box = ((Ui::Boolval_UI*)valueItems[paramName])->checkBox;
363             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
364         } else if (type == "color") {
365             KColorButton *color = ((Ui::Colorval_UI*)valueItems[paramName])->kcolorbutton;
366             setValue = color->color().name();
367             setValue.replace('#', "0x");
368         } else if (type == "complex") {
369             ComplexParameter *complex = ((ComplexParameter*)valueItems[paramName+"complex"]);
370             namenode.item(i) = complex->getParamDesc();
371         } else if (type == "geometry") {
372             Geometryval *geom = ((Geometryval*)valueItems[paramName+"geometry"]);
373             namenode.item(i) = geom->getParamDesc();
374         } else if (type == "position") {
375             KRestrictedLine *line = ((Ui::Positionval_UI*)valueItems[paramName+"position"])->krestrictedline;
376             int pos = m_timecode.getFrameCount(line->text(), KdenliveSettings::project_fps());
377             if (params.attribute("id") == "fadein") {
378                 pos += m_in;
379                 if (pos > m_out) {
380                     pos = m_out;
381                     line->setText(m_timecode.getTimecodeFromFrames(pos));
382                 }
383             } else if (params.attribute("id") == "fadeout") {
384                 pos = m_out - (pos - m_in);
385                 if (pos > m_out - m_in) {
386                     pos = m_out - m_in;
387                     line->setText(m_timecode.getTimecodeFromFrames(pos));
388                 }
389             }
390             setValue = QString::number(pos);
391         } else if (type == "wipe") {
392             Ui::Wipeval_UI *wp = (Ui::Wipeval_UI*)valueItems[paramName];
393             wipeInfo info;
394             if (wp->start_left->isChecked()) info.start = LEFT;
395             else if (wp->start_right->isChecked()) info.start = RIGHT;
396             else if (wp->start_up->isChecked()) info.start = UP;
397             else if (wp->start_down->isChecked()) info.start = DOWN;
398             else if (wp->start_center->isChecked()) info.start = CENTER;
399             info.startTransparency = wp->start_transp->value();
400             if (wp->end_left->isChecked()) info.end = LEFT;
401             else if (wp->end_right->isChecked()) info.end = RIGHT;
402             else if (wp->end_up->isChecked()) info.end = UP;
403             else if (wp->end_down->isChecked()) info.end = DOWN;
404             else if (wp->end_center->isChecked()) info.end = CENTER;
405             info.endTransparency = wp->end_transp->value();
406             setValue = getWipeString(info);
407         }
408
409         if (!setValue.isNull()) {
410             pa.attributes().namedItem("value").setNodeValue(setValue);
411         }
412     }
413     emit parameterChanged(oldparam, params);
414 }
415
416 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
417     QWidget* toFillin = new QWidget;
418     Ui::Constval_UI *ctval = new Ui::Constval_UI;
419     ctval->setupUi(toFillin);
420
421     ctval->horizontalSlider->setMinimum(min);
422     ctval->horizontalSlider->setMaximum(max);
423     ctval->spinBox->setMinimum(min);
424     ctval->spinBox->setMaximum(max);
425     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
426     ctval->horizontalSlider->setValue(val);
427     ctval->label->setText(name);
428     valueItems[name] = ctval;
429     uiItems.append(ctval);
430     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
431     items.append(toFillin);
432     vbox->addWidget(toFillin);
433 }
434
435 void EffectStackEdit::slotSliderMoved(int) {
436     collectAllParameters();
437 }
438
439 void EffectStackEdit::clearAllItems() {
440     qDeleteAll(items);
441     foreach(void *p, uiItems) {
442         delete p;
443     }
444     uiItems.clear();
445     items.clear();
446     valueItems.clear();
447     QLayoutItem *item = vbox->itemAt(0);
448     while (item) {
449         vbox->removeItem(item);
450         delete item;
451         item = vbox->itemAt(0);
452     }
453 }