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