1 /***************************************************************************
2 effecstackview.cpp - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
20 #include "effectstackedit.h"
21 #include <QVBoxLayout>
24 #include <QPushButton>
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"
33 static QMap<QString, QIcon> iconCache;
35 EffectStackEdit::EffectStackEdit(QFrame* frame, QWidget *parent): QObject(parent) {
37 QVBoxLayout *vbox1 = new QVBoxLayout(frame);
38 QVBoxLayout *vbox2 = new QVBoxLayout(frame);
39 vbox = new QVBoxLayout(frame);
40 vbox1->setContentsMargins(0, 0, 0, 0);
42 vbox2->setContentsMargins(0, 0, 0, 0);
44 vbox->setContentsMargins(0, 0, 0, 0);
46 frame->setLayout(vbox1);
47 QFont widgetFont = frame->font();
48 widgetFont.setPointSize(widgetFont.pointSize() - 2);
49 frame->setFont(widgetFont);
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));
58 vbox1->addWidget(area);
59 wid->setLayout(vbox2);
60 vbox2->addLayout(vbox);
61 vbox2->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
63 area->setWidgetResizable(true);
67 void EffectStackEdit::transferParamDesc(const QDomElement& d, int , int) {
70 QDomNodeList namenode = params.elementsByTagName("parameter");
74 for (int i = 0;i < namenode.count() ;i++) {
75 kDebug() << "in form";
76 QDomNode pa = namenode.item(i);
77 QDomNode na = pa.firstChildElement("name");
78 QDomNamedNodeMap nodeAtts = pa.attributes();
79 QString type = nodeAtts.namedItem("type").nodeValue();
80 QString paramName = na.toElement().text();
81 QWidget * toFillin = new QWidget;
82 QString value = nodeAtts.namedItem("value").isNull() ?
83 nodeAtts.namedItem("default").nodeValue() :
84 nodeAtts.namedItem("value").nodeValue();
86 //TODO constant, list, bool, complex , color, geometry, position
87 if (type == "double" || type == "constant") {
88 createSliderItem(paramName, value.toInt(), nodeAtts.namedItem("min").nodeValue().toInt(), nodeAtts.namedItem("max").nodeValue().toInt());
91 } else if (type == "list") {
93 Ui::Listval_UI *lsval = new Ui::Listval_UI;
94 lsval->setupUi(toFillin);
95 nodeAtts.namedItem("paramlist");
96 QStringList listitems = nodeAtts.namedItem("paramlist").nodeValue().split(",");
97 lsval->list->addItems(listitems);
98 lsval->list->setCurrentIndex(listitems.indexOf(value));
99 for (int i = 0;i < lsval->list->count();i++) {
100 QString entry = lsval->list->itemText(i);
101 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
102 if (!iconCache.contains(entry)) {
104 iconCache[entry] = pix.scaled(30, 30);
106 lsval->list->setIconSize(QSize(30, 30));
107 lsval->list->setItemIcon(i, iconCache[entry]);
110 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
111 lsval->title->setTitle(na.toElement().text());
112 valueItems[paramName] = lsval;
113 uiItems.append(lsval);
114 } else if (type == "bool") {
115 Ui::Boolval_UI *bval = new Ui::Boolval_UI;
116 bval->setupUi(toFillin);
117 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
119 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
120 bval->checkBox->setText(na.toElement().text());
121 valueItems[paramName] = bval;
122 uiItems.append(bval);
123 } else if (type == "complex") {
124 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(";");
125 QStringList max=nodeAtts.namedItem("max").nodeValue().split(";");
126 QStringList min=nodeAtts.namedItem("min").nodeValue().split(";");
127 QStringList val=value.split(";");
128 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
129 if ( (names.size() == max.size() ) &&
130 (names.size()== min.size()) &&
131 (names.size()== val.size()) )
133 for (int i=0;i< names.size();i++){
134 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
137 ComplexParameter *pl = new ComplexParameter;
138 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
139 pl->setupParam(d, 0, 100);
141 valueItems[paramName+"complex"] = pl;
143 } else if (type == "color") {
144 Ui::Colorval_UI *cval = new Ui::Colorval_UI;
145 cval->setupUi(toFillin);
147 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
148 kDebug() << value.toUInt(&ok, 16);
150 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
151 cval->label->setText(na.toElement().text());
152 valueItems[paramName] = cval;
153 uiItems.append(cval);
160 items.append(toFillin);
161 vbox->addWidget(toFillin);
165 void EffectStackEdit::collectAllParameters() {
166 QDomElement oldparam = params.cloneNode().toElement();
167 QDomNodeList namenode = params.elementsByTagName("parameter");
169 for (int i = 0;i < namenode.count() ;i++) {
170 QDomNode pa = namenode.item(i);
171 QDomNode na = pa.firstChildElement("name");
172 QString type = pa.attributes().namedItem("type").nodeValue();
174 if (type == "double" || type == "constant") {
175 QSlider* slider = ((Ui::Constval_UI*)valueItems[na.toElement().text()])->horizontalSlider;
176 setValue = QString::number(slider->value());
178 if (type == "list") {
179 KComboBox *box = ((Ui::Listval_UI*)valueItems[na.toElement().text()])->list;
180 setValue = box->currentText();
182 if (type == "bool") {
183 QCheckBox *box = ((Ui::Boolval_UI*)valueItems[na.toElement().text()])->checkBox;
184 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
186 if (type == "color") {
187 KColorButton *color = ((Ui::Colorval_UI*)valueItems[na.toElement().text()])->kcolorbutton;
188 setValue.sprintf("0x%08x", color->color().rgba());
190 if (type == "complex") {
191 ComplexParameter *complex = ((ComplexParameter*)valueItems[na.toElement().text()+"complex"]);
192 namenode.item(i) = complex->getParamDesc();
194 if (!setValue.isEmpty()) {
195 pa.attributes().namedItem("value").setNodeValue(setValue);
198 emit parameterChanged(oldparam, params);
201 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
202 QWidget* toFillin = new QWidget;
203 Ui::Constval_UI *ctval = new Ui::Constval_UI;
204 ctval->setupUi(toFillin);
206 ctval->horizontalSlider->setMinimum(min);
207 ctval->horizontalSlider->setMaximum(max);
208 ctval->spinBox->setMinimum(min);
209 ctval->spinBox->setMaximum(max);
210 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
211 ctval->horizontalSlider->setValue(val);
212 ctval->label->setText(name);
213 valueItems[name] = ctval;
214 uiItems.append(ctval);
215 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
216 items.append(toFillin);
217 vbox->addWidget(toFillin);
220 void EffectStackEdit::slotSliderMoved(int) {
221 collectAllParameters();
224 void EffectStackEdit::clearAllItems() {
225 foreach(QWidget* w, items) {
226 vbox->removeWidget(w);
229 foreach(void * p, uiItems) {