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