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