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