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