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