]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
Add const to foreach, some indent fixes
[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
68 EffectStackEdit::~EffectStackEdit() {
69     iconCache.clear();
70 }
71
72 void EffectStackEdit::transferParamDesc(const QDomElement& d, int , int) {
73     kDebug() << "in";
74     params = d;
75     QDomNodeList namenode = params.elementsByTagName("parameter");
76
77     clearAllItems();
78
79     for (int i = 0;i < namenode.count() ;i++) {
80         kDebug() << "in form";
81         QDomElement pa = namenode.item(i).toElement();
82         QDomNode na = pa.firstChildElement("name");
83         QString type = pa.attribute("type");
84         QString paramName = na.toElement().text();
85         QWidget * toFillin = new QWidget;
86         QString value = pa.attribute("value").isNull() ?
87                         pa.attribute("default") : pa.attribute("value");
88         if (type == "geometry") {
89             pa.setAttribute("namedesc", "X;Y;W;H;M");
90             pa.setAttribute("format", "%d%,%d%:%d%x%d%:%d%");
91             pa.setAttribute("min", "-200;-20;0;0;0");
92             pa.setAttribute("max", "200;200;100;100;100");
93         } else if (type == "complex") {
94             //pa.setAttribute("namedesc",pa.attribute("name"));
95
96         }
97
98
99         //TODO constant, list, bool, complex , color, geometry, position
100         if (type == "double" || type == "constant") {
101             createSliderItem(paramName, value.toInt(), pa.attribute("min").toInt(), pa.attribute("max").toInt());
102             delete toFillin;
103             toFillin = NULL;
104         } else if (type == "list") {
105
106             Ui::Listval_UI *lsval = new Ui::Listval_UI;
107             lsval->setupUi(toFillin);
108             QStringList listitems = pa.attribute("paramlist").split(",");
109             lsval->list->addItems(listitems);
110             lsval->list->setCurrentIndex(listitems.indexOf(value));
111             for (int i = 0;i < lsval->list->count();i++) {
112                 QString entry = lsval->list->itemText(i);
113                 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
114                     if (!EffectStackEdit::iconCache.contains(entry)) {
115                         QImage pix(entry);
116                         EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
117                     }
118                     lsval->list->setIconSize(QSize(30, 30));
119                     lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
120                 }
121             }
122             connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
123             lsval->title->setTitle(na.toElement().text());
124             valueItems[paramName] = lsval;
125             uiItems.append(lsval);
126         } else if (type == "bool") {
127             Ui::Boolval_UI *bval = new Ui::Boolval_UI;
128             bval->setupUi(toFillin);
129             bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
130
131             connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
132             bval->checkBox->setText(na.toElement().text());
133             valueItems[paramName] = bval;
134             uiItems.append(bval);
135         } else if (type == "complex" || type == "geometry") {
136             /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(";");
137             QStringList max=nodeAtts.namedItem("max").nodeValue().split(";");
138             QStringList min=nodeAtts.namedItem("min").nodeValue().split(";");
139             QStringList val=value.split(";");
140             kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size()  ;
141             if ( (names.size() == max.size() ) &&
142                  (names.size()== min.size()) &&
143                  (names.size()== val.size()) )
144             {
145              for (int i=0;i< names.size();i++){
146               createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
147              };
148             }*/
149             ComplexParameter *pl = new ComplexParameter;
150             connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
151             pl->setupParam(d, pa.attribute("name"), 0, 100);
152             vbox->addWidget(pl);
153             valueItems[paramName+"complex"] = pl;
154             items.append(pl);
155         } else if (type == "color") {
156             Ui::Colorval_UI *cval = new Ui::Colorval_UI;
157             cval->setupUi(toFillin);
158             bool ok;
159             cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
160             kDebug() << value.toUInt(&ok, 16);
161
162             connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
163             cval->label->setText(na.toElement().text());
164             valueItems[paramName] = cval;
165             uiItems.append(cval);
166         } else {
167             delete toFillin;
168             toFillin = NULL;
169         }
170
171         if (toFillin) {
172             items.append(toFillin);
173             vbox->addWidget(toFillin);
174         }
175     }
176 }
177 void EffectStackEdit::collectAllParameters() {
178     QDomElement oldparam = params.cloneNode().toElement();
179     QDomNodeList namenode = params.elementsByTagName("parameter");
180
181     for (int i = 0;i < namenode.count() ;i++) {
182         QDomNode pa = namenode.item(i);
183         QDomNode na = pa.firstChildElement("name");
184         QString type = pa.attributes().namedItem("type").nodeValue();
185         QString setValue;
186         if (type == "double" || type == "constant") {
187             QSlider* slider = ((Ui::Constval_UI*)valueItems[na.toElement().text()])->horizontalSlider;
188             setValue = QString::number(slider->value());
189         } else
190             if (type == "list") {
191                 KComboBox *box = ((Ui::Listval_UI*)valueItems[na.toElement().text()])->list;
192                 setValue = box->currentText();
193             } else
194                 if (type == "bool") {
195                     QCheckBox *box = ((Ui::Boolval_UI*)valueItems[na.toElement().text()])->checkBox;
196                     setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
197                 } else
198                     if (type == "color") {
199                         KColorButton *color = ((Ui::Colorval_UI*)valueItems[na.toElement().text()])->kcolorbutton;
200                         setValue.sprintf("0x%08x", color->color().rgba());
201                     } else
202                         if (type == "complex" || type == "geometry") {
203                             ComplexParameter *complex = ((ComplexParameter*)valueItems[na.toElement().text()+"complex"]);
204                             namenode.item(i) = complex->getParamDesc();
205                         }
206         if (!setValue.isNull()) {
207             pa.attributes().namedItem("value").setNodeValue(setValue);
208         }
209     }
210     emit parameterChanged(oldparam, params);
211 }
212
213 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
214     QWidget* toFillin = new QWidget;
215     Ui::Constval_UI *ctval = new Ui::Constval_UI;
216     ctval->setupUi(toFillin);
217
218     ctval->horizontalSlider->setMinimum(min);
219     ctval->horizontalSlider->setMaximum(max);
220     ctval->spinBox->setMinimum(min);
221     ctval->spinBox->setMaximum(max);
222     ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
223     ctval->horizontalSlider->setValue(val);
224     ctval->label->setText(name);
225     valueItems[name] = ctval;
226     uiItems.append(ctval);
227     connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
228     items.append(toFillin);
229     vbox->addWidget(toFillin);
230 }
231
232 void EffectStackEdit::slotSliderMoved(int) {
233     collectAllParameters();
234 }
235
236 void EffectStackEdit::clearAllItems() {
237     foreach(QWidget *w, items) {
238         vbox->removeWidget(w);
239         delete w;
240     }
241     foreach(void *p, uiItems) {
242         delete p;
243     }
244     uiItems.clear();
245     items.clear();
246     valueItems.clear();
247 }