]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
SOme work on transitions, including a new wipe transition (to be improved)
[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
210             connect(wpval->end_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
211             connect(wpval->end_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
212             connect(wpval->end_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
213             connect(wpval->end_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
214             connect(wpval->end_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
215             connect(wpval->start_up, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
216             connect(wpval->start_down, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
217             connect(wpval->start_left, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
218             connect(wpval->start_right, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
219             connect(wpval->start_center, SIGNAL(clicked()), this, SLOT(collectAllParameters()));
220
221             //wpval->title->setTitle(na.toElement().text());
222             valueItems[paramName] = wpval;
223             uiItems.append(wpval);
224         } else {
225             delete toFillin;
226             toFillin = NULL;
227         }
228
229         if (toFillin) {
230             items.append(toFillin);
231             vbox->addWidget(toFillin);
232         }
233     }
234 }
235
236 wipeInfo EffectStackEdit::getWipeInfo(QString value) {
237     wipeInfo info;
238     QString start = value.section(";", 0, 0);
239     QString end = value.section(";", 1, 1).section("=", 1, 1);
240     if (start.startsWith("-100%,0")) info.start = LEFT;
241     else if (start.startsWith("100%,0")) info.start = RIGHT;
242     else if (start.startsWith("0%,100%")) info.start = DOWN;
243     else if (start.startsWith("0%,-100%")) info.start = UP;
244     else if (start.startsWith("0%,0%")) info.start = CENTER;
245
246     if (end.startsWith("-100%,0")) info.end = LEFT;
247     else if (end.startsWith("100%,0")) info.end = RIGHT;
248     else if (end.startsWith("0%,100%")) info.end = DOWN;
249     else if (end.startsWith("0%,-100%")) info.end = UP;
250     else if (end.startsWith("0%,0%")) info.end = CENTER;
251
252     return info;
253 }
254
255 QString EffectStackEdit::getWipeString(wipeInfo info) {
256
257     QString start;
258     QString end;
259     switch (info.start) {
260     case LEFT:
261         start = "-100%,0%:100%x100%";
262         break;
263     case RIGHT:
264         start = "100%,0%:100%x100%";
265         break;
266     case DOWN:
267         start = "0%,100%:100%x100%";
268         break;
269     case UP:
270         start = "0%,-100%:100%x100%";
271         break;
272     default:
273         start = "0%,0%:100%x100%";
274         break;
275     }
276     switch (info.end) {
277     case LEFT:
278         end = "-100%,0%:100%x100%";
279         break;
280     case RIGHT:
281         end = "100%,0%:100%x100%";
282         break;
283     case DOWN:
284         end = "0%,100%:100%x100%";
285         break;
286     case UP:
287         end = "0%,-100%:100%x100%";
288         break;
289     default:
290         end = "0%,0%:100%x100%";
291         break;
292     }
293     return QString(start + ";-1=" + end);
294 }
295
296 void EffectStackEdit::collectAllParameters() {
297     QDomElement oldparam = params.cloneNode().toElement();
298     QDomNodeList namenode = params.elementsByTagName("parameter");
299
300     for (int i = 0;i < namenode.count() ;i++) {
301         QDomNode pa = namenode.item(i);
302         QDomNode na = pa.firstChildElement("name");
303         QString type = pa.attributes().namedItem("type").nodeValue();
304         QString setValue;
305         if (type == "double" || type == "constant") {
306             QSlider* slider = ((Ui::Constval_UI*)valueItems[na.toElement().text()])->horizontalSlider;
307             setValue = QString::number(slider->value());
308         } else if (type == "list") {
309             KComboBox *box = ((Ui::Listval_UI*)valueItems[na.toElement().text()])->list;
310             setValue = box->itemData(box->currentIndex()).toString();
311         } else if (type == "bool") {
312             QCheckBox *box = ((Ui::Boolval_UI*)valueItems[na.toElement().text()])->checkBox;
313             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
314         } else if (type == "color") {
315             KColorButton *color = ((Ui::Colorval_UI*)valueItems[na.toElement().text()])->kcolorbutton;
316             setValue.sprintf("0x%08x", color->color().rgba());
317         } else if (type == "complex" || type == "geometry") {
318             ComplexParameter *complex = ((ComplexParameter*)valueItems[na.toElement().text()+"complex"]);
319             namenode.item(i) = complex->getParamDesc();
320         } else if (type == "wipe") {
321             Ui::Wipeval_UI *wp = (Ui::Wipeval_UI*)valueItems[na.toElement().text()];
322             wipeInfo info;
323             if (wp->start_left->isChecked()) info.start = LEFT;
324             else if (wp->start_right->isChecked()) info.start = RIGHT;
325             else if (wp->start_up->isChecked()) info.start = UP;
326             else if (wp->start_down->isChecked()) info.start = DOWN;
327             else if (wp->start_center->isChecked()) info.start = CENTER;
328
329             if (wp->end_left->isChecked()) info.end = LEFT;
330             else if (wp->end_right->isChecked()) info.end = RIGHT;
331             else if (wp->end_up->isChecked()) info.end = UP;
332             else if (wp->end_down->isChecked()) info.end = DOWN;
333             else if (wp->end_center->isChecked()) info.end = CENTER;
334
335             setValue = getWipeString(info);
336         }
337
338         if (!setValue.isNull()) {
339             pa.attributes().namedItem("value").setNodeValue(setValue);
340         }
341     }
342     emit parameterChanged(oldparam, params);
343 }
344
345 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
346     QWidget* toFillin = new QWidget;
347     Ui::Constval_UI *ctval = new Ui::Constval_UI;
348     ctval->setupUi(toFillin);
349
350     ctval->horizontalSlider->setMinimum(min);
351     ctval->horizontalSlider->setMaximum(max);
352     ctval->spinBox->setMinimum(min);
353     ctval->spinBox->setMaximum(max);
354     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
355     ctval->horizontalSlider->setValue(val);
356     ctval->label->setText(name);
357     valueItems[name] = ctval;
358     uiItems.append(ctval);
359     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
360     items.append(toFillin);
361     vbox->addWidget(toFillin);
362 }
363
364 void EffectStackEdit::slotSliderMoved(int) {
365     collectAllParameters();
366 }
367
368 void EffectStackEdit::clearAllItems() {
369     foreach(QWidget *w, items) {
370         vbox->removeWidget(w);
371         delete w;
372     }
373     foreach(void *p, uiItems) {
374         delete p;
375     }
376     uiItems.clear();
377     items.clear();
378     valueItems.clear();
379 }