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