]> git.sesse.net Git - kdenlive/blob - src/effectstackedit.cpp
use parameterplotter for "complex" effects
[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 "parameterplotter.h"
31
32 EffectStackEdit::EffectStackEdit(QFrame* frame,QWidget *parent): QObject(parent)
33 {
34         QScrollArea *area;
35         QVBoxLayout *vbox1=new QVBoxLayout;
36         frame->setLayout(vbox1);
37         vbox=new QVBoxLayout(frame);
38         
39         area=new QScrollArea(frame);
40         QWidget *wid=new QWidget(area);
41         area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42         area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
43         wid->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Minimum));
44         //area->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::MinimumExpanding));
45
46         vbox1->addWidget(area);
47         wid->setLayout(vbox);
48         area->setWidget(wid);
49         area->setWidgetResizable(true);
50         wid->show();
51         
52 }
53 void EffectStackEdit::transferParamDesc(const QDomElement& d,int ,int){
54         kDebug() << "in";
55         params=d;
56         QDomNodeList namenode = params.elementsByTagName("parameter");
57         
58         clearAllItems();
59                 QString outstr;
60                 QTextStream str(&outstr);
61                 d.save(str,2);
62                 kDebug() << outstr;
63         for (int i=0;i< namenode.count() ;i++){
64                 kDebug() << "in form";
65                 QDomNode pa=namenode.item(i);
66                 QDomNode na=pa.firstChildElement("name");
67                 QDomNamedNodeMap nodeAtts=pa.attributes();
68                 QString type=nodeAtts.namedItem("type").nodeValue();
69                 QString paramName=na.toElement().text();
70                 QWidget * toFillin=new QWidget;
71                 QString value=nodeAtts.namedItem("value").isNull()?
72                         nodeAtts.namedItem("default").nodeValue():
73                         nodeAtts.namedItem("value").nodeValue();
74                 
75                 //TODO constant, list, bool, complex , color, geometry, position
76                 if (type=="double" || type=="constant"){
77                         createSliderItem(paramName,value.toInt(),nodeAtts.namedItem("min").nodeValue().toInt(),nodeAtts.namedItem("max").nodeValue().toInt() );
78                         delete toFillin;
79                         toFillin=NULL;
80                 }else if (type=="list"){
81                         
82                         Ui::Listval_UI *lsval=new Ui::Listval_UI;
83                         lsval->setupUi(toFillin);
84                         nodeAtts.namedItem("paramlist");
85                         QStringList listitems=nodeAtts.namedItem("paramlist").nodeValue().split(",");
86                         lsval->list->addItems(listitems);
87                         lsval->list->setCurrentIndex(listitems.indexOf(value));;
88                         connect (lsval->list, SIGNAL(currentIndexChanged(int)) , this, SLOT (collectAllParameters()));
89                         lsval->title->setTitle(na.toElement().text() );
90                         valueItems[paramName]=lsval;
91                         uiItems.append(lsval);
92                 }else if (type=="bool"){
93                         Ui::Boolval_UI *bval=new Ui::Boolval_UI;
94                         bval->setupUi(toFillin);
95                         bval->checkBox->setCheckState(value=="0" ? Qt::Unchecked : Qt::Checked);
96                         
97                         connect (bval->checkBox, SIGNAL(stateChanged(int)) , this, SLOT (collectAllParameters()));
98                         bval->title->setTitle(na.toElement().text() );
99                         valueItems[paramName]=bval;
100                         uiItems.append(bval);
101                 }else if(type=="complex"){
102                         QStringList names=nodeAtts.namedItem("name").nodeValue().split(";");
103                         QStringList max=nodeAtts.namedItem("max").nodeValue().split(";");
104                         QStringList min=nodeAtts.namedItem("min").nodeValue().split(";");
105                         QStringList val=value.split(";");
106                         kDebug() << "in complex"<<names.size() << " " << max.size() << " " << min.size() << " " << val.size()  ;
107                         if ( (names.size() == max.size() ) && 
108                              (names.size()== min.size()) && 
109                              (names.size()== val.size()) )
110                         {
111                                 for (int i=0;i< names.size();i++){
112                                         createSliderItem(names[i],val[i].toInt(),min[i].toInt(),max[i].toInt());
113                                 };
114                         }
115                         ParameterPlotter *pl=new ParameterPlotter;
116                         vbox->addWidget(pl);
117                         items.append(pl);
118                 }else{
119                         delete toFillin;
120                         toFillin=NULL;
121                 }
122
123                 if (toFillin){
124                         items.append(toFillin);
125                         vbox->addWidget(toFillin);
126                 }       
127         }
128 }
129 void EffectStackEdit::collectAllParameters(){
130         QDomNodeList namenode = params.elementsByTagName("parameter");
131
132         for (int i=0;i< namenode.count() ;i++){
133                 QDomNode pa=namenode.item(i);
134                 QDomNode na=pa.firstChildElement("name");
135                 QString type=pa.attributes().namedItem("type").nodeValue();
136                 QString setValue;
137                 if (type=="double" || type=="constant"){
138                         QSlider* slider=((Ui::Constval_UI*)valueItems[na.toElement().text()])->horizontalSlider;
139                         setValue=QString::number(slider->value());
140                 }
141                 if (type=="list"){
142                         KComboBox *box=((Ui::Listval_UI*)valueItems[na.toElement().text()])->list;
143                         setValue=box->currentText();
144                 }
145                 if (type=="bool"){
146                         QCheckBox *box=((Ui::Boolval_UI*)valueItems[na.toElement().text()])->checkBox;
147                         setValue=box->checkState() == Qt::Checked ? "1" :"0" ;
148                 }
149                 if (!setValue.isEmpty()){
150                         pa.attributes().namedItem("value").setNodeValue(setValue);
151                 }
152         }
153         emit parameterChanged(params);
154 }
155
156 void EffectStackEdit::createSliderItem(const QString& name, int val ,int min, int max){
157         QWidget* toFillin=new QWidget;
158         Ui::Constval_UI *ctval=new Ui::Constval_UI;
159         ctval->setupUi(toFillin);
160         
161         ctval->horizontalSlider->setMinimum(min);
162         ctval->horizontalSlider->setMaximum(max);
163         ctval->spinBox->setMinimum(min);
164         ctval->spinBox->setMaximum(max);
165         
166         ctval->horizontalSlider->setValue(val);
167         ctval->title->setTitle(name);
168         valueItems[name]=ctval;
169         uiItems.append(ctval);
170         connect (ctval->horizontalSlider, SIGNAL(valueChanged(int)) , this, SLOT (collectAllParameters()));
171         items.append(toFillin);
172         vbox->addWidget(toFillin);
173 }
174
175 void EffectStackEdit::slotSliderMoved(int){
176         collectAllParameters();
177 }
178
179 void EffectStackEdit::clearAllItems(){
180         foreach (QWidget* w,items){
181                 vbox->removeWidget(w);
182                 delete w;
183         }
184         foreach(void * p, uiItems){
185                 delete p;
186         }
187         uiItems.clear();
188         items.clear();
189         valueItems.clear();
190 }