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 QMap<QString, QImage> EffectStackEdit::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);
68 EffectStackEdit::~EffectStackEdit() {
72 void EffectStackEdit::transferParamDesc(const QDomElement& d, int , int) {
75 QDomNodeList namenode = params.elementsByTagName("parameter");
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");
94 else if (type == "complex") {
95 //pa.setAttribute("namedesc",pa.attribute("name"));
100 //TODO constant, list, bool, complex , color, geometry, position
101 if (type == "double" || type == "constant") {
102 createSliderItem(paramName, value.toInt(), pa.attribute("min").toInt(), pa.attribute("max").toInt());
105 } else if (type == "list") {
107 Ui::Listval_UI *lsval = new Ui::Listval_UI;
108 lsval->setupUi(toFillin);
109 QStringList listitems = pa.attribute("paramlist").split(",");
110 lsval->list->addItems(listitems);
111 lsval->list->setCurrentIndex(listitems.indexOf(value));
112 for (int i = 0;i < lsval->list->count();i++) {
113 QString entry = lsval->list->itemText(i);
114 if (!entry.isEmpty() && (entry.endsWith(".png") || entry.endsWith(".pgm"))) {
115 if (!EffectStackEdit::iconCache.contains(entry)) {
117 EffectStackEdit::iconCache[entry] = pix.scaled(30, 30);
119 lsval->list->setIconSize(QSize(30, 30));
120 lsval->list->setItemIcon(i, QPixmap::fromImage(iconCache[entry]));
123 connect(lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT(collectAllParameters()));
124 lsval->title->setTitle(na.toElement().text());
125 valueItems[paramName] = lsval;
126 uiItems.append(lsval);
127 } else if (type == "bool") {
128 Ui::Boolval_UI *bval = new Ui::Boolval_UI;
129 bval->setupUi(toFillin);
130 bval->checkBox->setCheckState(value == "0" ? Qt::Unchecked : Qt::Checked);
132 connect(bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(collectAllParameters()));
133 bval->checkBox->setText(na.toElement().text());
134 valueItems[paramName] = bval;
135 uiItems.append(bval);
136 } else if (type == "complex" || type == "geometry") {
137 /*QStringList names=nodeAtts.namedItem("name").nodeValue().split(";");
138 QStringList max=nodeAtts.namedItem("max").nodeValue().split(";");
139 QStringList min=nodeAtts.namedItem("min").nodeValue().split(";");
140 QStringList val=value.split(";");
141 kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size() ;
142 if ( (names.size() == max.size() ) &&
143 (names.size()== min.size()) &&
144 (names.size()== val.size()) )
146 for (int i=0;i< names.size();i++){
147 createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
150 ComplexParameter *pl = new ComplexParameter;
151 connect(pl, SIGNAL(parameterChanged()), this, SLOT(collectAllParameters()));
152 pl->setupParam(d, pa.attribute("name"), 0, 100);
154 valueItems[paramName+"complex"] = pl;
156 } else if (type == "color") {
157 Ui::Colorval_UI *cval = new Ui::Colorval_UI;
158 cval->setupUi(toFillin);
160 cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
161 kDebug() << value.toUInt(&ok, 16);
163 connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
164 cval->label->setText(na.toElement().text());
165 valueItems[paramName] = cval;
166 uiItems.append(cval);
173 items.append(toFillin);
174 vbox->addWidget(toFillin);
178 void EffectStackEdit::collectAllParameters() {
179 QDomElement oldparam = params.cloneNode().toElement();
180 QDomNodeList namenode = params.elementsByTagName("parameter");
182 for (int i = 0;i < namenode.count() ;i++) {
183 QDomNode pa = namenode.item(i);
184 QDomNode na = pa.firstChildElement("name");
185 QString type = pa.attributes().namedItem("type").nodeValue();
187 if (type == "double" || type == "constant") {
188 QSlider* slider = ((Ui::Constval_UI*)valueItems[na.toElement().text()])->horizontalSlider;
189 setValue = QString::number(slider->value());
191 if (type == "list") {
192 KComboBox *box = ((Ui::Listval_UI*)valueItems[na.toElement().text()])->list;
193 setValue = box->currentText();
195 if (type == "bool") {
196 QCheckBox *box = ((Ui::Boolval_UI*)valueItems[na.toElement().text()])->checkBox;
197 setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
199 if (type == "color") {
200 KColorButton *color = ((Ui::Colorval_UI*)valueItems[na.toElement().text()])->kcolorbutton;
201 setValue.sprintf("0x%08x", color->color().rgba());
203 if (type == "complex" || type == "geometry") {
204 ComplexParameter *complex = ((ComplexParameter*)valueItems[na.toElement().text()+"complex"]);
205 namenode.item(i) = complex->getParamDesc();
207 if (!setValue.isNull()) {
208 pa.attributes().namedItem("value").setNodeValue(setValue);
211 emit parameterChanged(oldparam, params);
214 void EffectStackEdit::createSliderItem(const QString& name, int val , int min, int max) {
215 QWidget* toFillin = new QWidget;
216 Ui::Constval_UI *ctval = new Ui::Constval_UI;
217 ctval->setupUi(toFillin);
219 ctval->horizontalSlider->setMinimum(min);
220 ctval->horizontalSlider->setMaximum(max);
221 ctval->spinBox->setMinimum(min);
222 ctval->spinBox->setMaximum(max);
223 ctval->horizontalSlider->setPageStep((int)(max - min) / 10);
224 ctval->horizontalSlider->setValue(val);
225 ctval->label->setText(name);
226 valueItems[name] = ctval;
227 uiItems.append(ctval);
228 connect(ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT(collectAllParameters()));
229 items.append(toFillin);
230 vbox->addWidget(toFillin);
233 void EffectStackEdit::slotSliderMoved(int) {
234 collectAllParameters();
237 void EffectStackEdit::clearAllItems() {
238 foreach(QWidget* w, items) {
239 vbox->removeWidget(w);
242 foreach(void * p, uiItems) {