]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
some small updates
[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 #include "ui_constval_ui.h"
28 #include "ui_listval_ui.h"
29 #include "ui_boolval_ui.h"
30 #include "ui_colorval_ui.h"
31 #include "complexparameter.h"
32
33 QMap<QString, QImage> EffectStackEdit::iconCache;
34
35 EffectStackEdit::EffectStackEdit(QFrame* frame, QWidget *parent): QObject(parent) {
36     QScrollArea *area;
37     QVBoxLayout *vbox1 = new QVBoxLayout(frame);
38     QVBoxLayout *vbox2 = new QVBoxLayout(frame);
39     vbox = new QVBoxLayout(frame);
40     vbox1->setContentsMargins(0, 0, 0, 0);
41     vbox1->setSpacing(0);
42     vbox2->setContentsMargins(0, 0, 0, 0);
43     vbox2->setSpacing(0);
44     vbox->setContentsMargins(0, 0, 0, 0);
45     vbox->setSpacing(0);
46     frame->setLayout(vbox1);
47     QFont widgetFont = frame->font();
48     widgetFont.setPointSize(widgetFont.pointSize() - 2);
49     frame->setFont(widgetFont);
50
51     area = new QScrollArea(frame);
52     QWidget *wid = new QWidget(area);
53     area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54     area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
55     wid->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
56     //area->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::MinimumExpanding));
57
58     vbox1->addWidget(area);
59     wid->setLayout(vbox2);
60     vbox2->addLayout(vbox);
61     vbox2->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
62     area->setWidget(wid);
63     area->setWidgetResizable(true);
64     wid->show();
65
66 }
67 void EffectStackEdit::transferParamDesc(const QDomElement& d, int , int) {
68     kDebug() << "in";
69     params = d;
70     QDomNodeList namenode = params.elementsByTagName("parameter");
71
72     clearAllItems();
73
74     for (int i = 0;i < namenode.count() ;i++) {
75         kDebug() << "in form";
76         QDomElement pa = namenode.item(i).toElement();
77         QDomNode na = pa.firstChildElement("name");
78         QString type = pa.attribute("type");
79         QString paramName = na.toElement().text();
80         QWidget * toFillin = new QWidget;
81         QString value = pa.attribute("value").isNull() ?
82                         pa.attribute("default") : pa.attribute("value");
83         if (type == "geometry") {
84             pa.setAttribute("namedesc", "X;Y;W;H;M");
85             pa.setAttribute("format", "%d%,%d%:%d%x%d%:%d%");
86             pa.setAttribute("min", "-200;-20;0;0;0");
87             pa.setAttribute("max", "200;200;100;100;100");
88         }
89         if (type == "complex") {
90             //pa.setAttribute("namedesc",pa.attribute("name"));
91
92         }
93         //TODO constant, list, bool, complex , color, geometry, position
94         if (type == "double" || type == "constant") {
95             createSliderItem(paramName, value.toInt(), pa.attribute("min").toInt(), pa.attribute("max").toInt());
96             delete toFillin;
97             toFillin = NULL;
98         } else if (type == "list") {
99
100             Ui::Listval_UI *lsval = new Ui::Listval_UI;
101             lsval->setupUi(toFillin);
102             QStringList listitems = pa.attribute("paramlist").split(",");
103             lsval->list->addItems(listitems);
104             lsval->list->setCurrentIndex(listitems.indexOf(value));
105             for (int i = 0;i < lsval->list->count();i++) {
106                 QString entry = lsval->list->itemText(i);
107                 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
108                     if (!EffectStackEdit::iconCache.contains(entry)) {
109                         QImage pix(entry);
110                         EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
111                     }
112                     lsval->list->setIconSize(QSize(30, 30));
113                     lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
114                 }
115             }
116             connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
117             lsval->title->setTitle(na.toElement().text());
118             valueItems[paramName] = lsval;
119             uiItems.append(lsval);
120         } else if (type == "bool") {
121             Ui::Boolval_UI *bval = new Ui::Boolval_UI;
122             bval->setupUi(toFillin);
123             bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
124
125             connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
126             bval->checkBox->setText(na.toElement().text());
127             valueItems[paramName] = bval;
128             uiItems.append(bval);
129         } else if (type == "complex" || type == "geometry") {
130             /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(";");
131             QStringList max=nodeAtts.namedItem("max").nodeValue().split(";");
132             QStringList min=nodeAtts.namedItem("min").nodeValue().split(";");
133             QStringList val=value.split(";");
134             kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size()  ;
135             if ( (names.size() == max.size() ) &&
136                  (names.size()== min.size()) &&
137                  (names.size()== val.size()) )
138             {
139              for (int i=0;i< names.size();i++){
140               createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
141              };
142             }*/
143             ComplexParameter *pl = new ComplexParameter;
144             connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
145             pl->setupParam(d, pa.attribute("name"), 0, 100);
146             vbox->addWidget(pl);
147             valueItems[paramName+"complex"] = pl;
148             items.append(pl);
149         } else if (type == "color") {
150             Ui::Colorval_UI *cval = new Ui::Colorval_UI;
151             cval->setupUi(toFillin);
152             bool ok;
153             cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
154             kDebug() << value.toUInt(&ok, 16);
155
156             connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
157             cval->label->setText(na.toElement().text());
158             valueItems[paramName] = cval;
159             uiItems.append(cval);
160         } else {
161             delete toFillin;
162             toFillin = NULL;
163         }
164
165         if (toFillin) {
166             items.append(toFillin);
167             vbox->addWidget(toFillin);
168         }
169     }
170 }
171 void EffectStackEdit::collectAllParameters() {
172     QDomElement oldparam = params.cloneNode().toElement();
173     QDomNodeList namenode = params.elementsByTagName("parameter");
174
175     for (int i = 0;i < namenode.count() ;i++) {
176         QDomNode pa = namenode.item(i);
177         QDomNode na = pa.firstChildElement("name");
178         QString type = pa.attributes().namedItem("type").nodeValue();
179         QString setValue;
180         if (type == "double" || type == "constant") {
181             QSlider* slider = ((Ui::Constval_UI*)valueItems[na.toElement().text()])->horizontalSlider;
182             setValue = QString::number(slider->value());
183         } else
184             if (type == "list") {
185                 KComboBox *box = ((Ui::Listval_UI*)valueItems[na.toElement().text()])->list;
186                 setValue = box->currentText();
187             } else
188                 if (type == "bool") {
189                     QCheckBox *box = ((Ui::Boolval_UI*)valueItems[na.toElement().text()])->checkBox;
190                     setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
191                 } else
192                     if (type == "color") {
193                         KColorButton *color = ((Ui::Colorval_UI*)valueItems[na.toElement().text()])->kcolorbutton;
194                         setValue.sprintf("0x%08x", color->color().rgba());
195                     } else
196                         if (type == "complex" || type == "geometry") {
197                             ComplexParameter *complex = ((ComplexParameter*)valueItems[na.toElement().text()+"complex"]);
198                             namenode.item(i) = complex->getParamDesc();
199                         }
200         if (!setValue.isNull()) {
201             pa.attributes().namedItem("value").setNodeValue(setValue);
202         }
203     }
204     emit parameterChanged(oldparam, params);
205 }
206
207 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
208     QWidget* toFillin = new QWidget;
209     Ui::Constval_UI *ctval = new Ui::Constval_UI;
210     ctval->setupUi(toFillin);
211
212     ctval->horizontalSlider->setMinimum(min);
213     ctval->horizontalSlider->setMaximum(max);
214     ctval->spinBox->setMinimum(min);
215     ctval->spinBox->setMaximum(max);
216     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
217     ctval->horizontalSlider->setValue(val);
218     ctval->label->setText(name);
219     valueItems[name] = ctval;
220     uiItems.append(ctval);
221     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
222     items.append(toFillin);
223     vbox->addWidget(toFillin);
224 }
225
226 void EffectStackEdit::slotSliderMoved(int) {
227     collectAllParameters();
228 }
229
230 void EffectStackEdit::clearAllItems() {
231     foreach(QWidget* w, items) {
232         vbox->removeWidget(w);
233         delete w;
234     }
235     foreach(void * p, uiItems) {
236         delete p;
237     }
238     uiItems.clear();
239     items.clear();
240     valueItems.clear();
241 }