]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
* Fix undo of effect editing
[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             pval->krestrictedline->setText(m_timecode.getTimecodeFromFrames(value.toInt()));
194             connect(pval->krestrictedline, SIGNAL(editingFinished()), this, SLOT(collectAllParameters()));
195             pval->label->setText(paramName);
196             valueItems[paramName + "position"] = pval;
197             uiItems.append(pval);
198         } else if (type == "wipe") {
199             Ui::Wipeval_UI *wpval = new Ui::Wipeval_UI;
200             wpval->setupUi(toFillin);
201             wipeInfo w = getWipeInfo(value);
202             switch (w.start) {
203             case UP:
204                 wpval->start_up->setChecked(true);
205                 break;
206             case DOWN:
207                 wpval->start_down->setChecked(true);
208                 break;
209             case RIGHT:
210                 wpval->start_right->setChecked(true);
211                 break;
212             case LEFT:
213                 wpval->start_left->setChecked(true);
214                 break;
215             default:
216                 wpval->start_center->setChecked(true);
217                 break;
218             }
219             switch (w.end) {
220             case UP:
221                 wpval->end_up->setChecked(true);
222                 break;
223             case DOWN:
224                 wpval->end_down->setChecked(true);
225                 break;
226             case RIGHT:
227                 wpval->end_right->setChecked(true);
228                 break;
229             case LEFT:
230                 wpval->end_left->setChecked(true);
231                 break;
232             default:
233                 wpval->end_center->setChecked(true);
234                 break;
235             }
236             wpval->start_transp->setValue(w.startTransparency);
237             wpval->end_transp->setValue(w.endTransparency);
238
239             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
240             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
241             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
242             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
243             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
244             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
245             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
246             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
247             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
248             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
249             connect(wpval->start_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
250             connect(wpval->end_transp, SIGNAL(valueChanged(int)), this, SLOT(collectAllParameters()));
251             //wpval->title->setTitle(na.toElement().text());
252             valueItems[paramName] = wpval;
253             uiItems.append(wpval);
254         } else {
255             delete toFillin;
256             toFillin = NULL;
257         }
258
259         if (toFillin) {
260             items.append(toFillin);
261             vbox->addWidget(toFillin);
262         }
263     }
264     vbox->addStretch();
265 }
266
267 void EffectStackEdit::slotSeekToPos(int pos) {
268     emit seekTimeline(m_in + pos);
269 }
270
271 wipeInfo EffectStackEdit::getWipeInfo(QString value) {
272     wipeInfo info;
273     QString start = value.section(";", 0, 0);
274     QString end = value.section(";", 1, 1).section("=", 1, 1);
275     if (start.startsWith("-100%,0")) info.start = LEFT;
276     else if (start.startsWith("100%,0")) info.start = RIGHT;
277     else if (start.startsWith("0%,100%")) info.start = DOWN;
278     else if (start.startsWith("0%,-100%")) info.start = UP;
279     else if (start.startsWith("0%,0%")) info.start = CENTER;
280     if (start.count(':') == 2) info.startTransparency = start.section(':', -1).toInt();
281     else info.startTransparency = 100;
282
283     if (end.startsWith("-100%,0")) info.end = LEFT;
284     else if (end.startsWith("100%,0")) info.end = RIGHT;
285     else if (end.startsWith("0%,100%")) info.end = DOWN;
286     else if (end.startsWith("0%,-100%")) info.end = UP;
287     else if (end.startsWith("0%,0%")) info.end = CENTER;
288     if (end.count(':') == 2) info.endTransparency = end.section(':', -1).toInt();
289     else info.endTransparency = 100;
290     return info;
291 }
292
293 QString EffectStackEdit::getWipeString(wipeInfo info) {
294
295     QString start;
296     QString end;
297     switch (info.start) {
298     case LEFT:
299         start = "-100%,0%:100%x100%";
300         break;
301     case RIGHT:
302         start = "100%,0%:100%x100%";
303         break;
304     case DOWN:
305         start = "0%,100%:100%x100%";
306         break;
307     case UP:
308         start = "0%,-100%:100%x100%";
309         break;
310     default:
311         start = "0%,0%:100%x100%";
312         break;
313     }
314     start.append(":" + QString::number(info.startTransparency));
315
316     switch (info.end) {
317     case LEFT:
318         end = "-100%,0%:100%x100%";
319         break;
320     case RIGHT:
321         end = "100%,0%:100%x100%";
322         break;
323     case DOWN:
324         end = "0%,100%:100%x100%";
325         break;
326     case UP:
327         end = "0%,-100%:100%x100%";
328         break;
329     default:
330         end = "0%,0%:100%x100%";
331         break;
332     }
333     end.append(":" + QString::number(info.endTransparency));
334     return QString(start + ";-1=" + end);
335 }
336
337 void EffectStackEdit::collectAllParameters() {
338     QDomElement oldparam = params.cloneNode().toElement();
339     QDomNodeList namenode = params.elementsByTagName("parameter");
340
341     for (int i = 0;i < namenode.count() ;i++) {
342         QDomNode pa = namenode.item(i);
343         QDomNode na = pa.firstChildElement("name");
344         QString type = pa.attributes().namedItem("type").nodeValue();
345         QString paramName = i18n(na.toElement().text().toUtf8().data());
346
347         QString setValue;
348         if (type == "double" || type == "constant") {
349             QSlider* slider = ((Ui::Constval_UI*)valueItems[paramName])->horizontalSlider;
350             setValue = QString::number(slider->value());
351         } else if (type == "list") {
352             KComboBox *box = ((Ui::Listval_UI*)valueItems[paramName])->list;
353             setValue = box->itemData(box->currentIndex()).toString();
354         } else if (type == "bool") {
355             QCheckBox *box = ((Ui::Boolval_UI*)valueItems[paramName])->checkBox;
356             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
357         } else if (type == "color") {
358             KColorButton *color = ((Ui::Colorval_UI*)valueItems[paramName])->kcolorbutton;
359             setValue = color->color().name();
360             setValue.replace('#', "0x");
361         } else if (type == "complex") {
362             ComplexParameter *complex = ((ComplexParameter*)valueItems[paramName+"complex"]);
363             namenode.item(i) = complex->getParamDesc();
364         } else if (type == "geometry") {
365             Geometryval *geom = ((Geometryval*)valueItems[paramName+"geometry"]);
366             namenode.item(i) = geom->getParamDesc();
367         } else if (type == "position") {
368             KRestrictedLine *line = ((Ui::Positionval_UI*)valueItems[paramName+"position"])->krestrictedline;
369             setValue = QString::number(m_timecode.getFrameCount(line->text(), KdenliveSettings::project_fps()));
370             kDebug() << "// NEW POSITION: " << setValue << "(" << line->text() << " PARAM: " << paramName + "position";
371         } else if (type == "wipe") {
372             Ui::Wipeval_UI *wp = (Ui::Wipeval_UI*)valueItems[paramName];
373             wipeInfo info;
374             if (wp->start_left->isChecked()) info.start = LEFT;
375             else if (wp->start_right->isChecked()) info.start = RIGHT;
376             else if (wp->start_up->isChecked()) info.start = UP;
377             else if (wp->start_down->isChecked()) info.start = DOWN;
378             else if (wp->start_center->isChecked()) info.start = CENTER;
379             info.startTransparency = wp->start_transp->value();
380             if (wp->end_left->isChecked()) info.end = LEFT;
381             else if (wp->end_right->isChecked()) info.end = RIGHT;
382             else if (wp->end_up->isChecked()) info.end = UP;
383             else if (wp->end_down->isChecked()) info.end = DOWN;
384             else if (wp->end_center->isChecked()) info.end = CENTER;
385             info.endTransparency = wp->end_transp->value();
386             setValue = getWipeString(info);
387         }
388
389         if (!setValue.isNull()) {
390             pa.attributes().namedItem("value").setNodeValue(setValue);
391         }
392     }
393     emit parameterChanged(oldparam, params);
394 }
395
396 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
397     QWidget* toFillin = new QWidget;
398     Ui::Constval_UI *ctval = new Ui::Constval_UI;
399     ctval->setupUi(toFillin);
400
401     ctval->horizontalSlider->setMinimum(min);
402     ctval->horizontalSlider->setMaximum(max);
403     ctval->spinBox->setMinimum(min);
404     ctval->spinBox->setMaximum(max);
405     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
406     ctval->horizontalSlider->setValue(val);
407     ctval->label->setText(name);
408     valueItems[name] = ctval;
409     uiItems.append(ctval);
410     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
411     items.append(toFillin);
412     vbox->addWidget(toFillin);
413 }
414
415 void EffectStackEdit::slotSliderMoved(int) {
416     collectAllParameters();
417 }
418
419 void EffectStackEdit::clearAllItems() {
420     qDeleteAll(items);
421     foreach(void *p, uiItems) {
422         delete p;
423     }
424     uiItems.clear();
425     items.clear();
426     valueItems.clear();
427     QLayoutItem *item = vbox->itemAt(0);
428     while (item) {
429         vbox->removeItem(item);
430         delete item;
431         item = vbox->itemAt(0);
432     }
433 }