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