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