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