]> git.sesse.net Git - kdenlive/blob - src/parameterplotter.cpp
use first simple sliders for parameter setup
[kdenlive] / src / parameterplotter.cpp
1 /***************************************************************************
2                           parameterplotter.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 #if 0
18 #include "parameterplotter.h"
19 #include <QVariant>
20 #include <KPlotObject>
21 #include <QMouseEvent>
22 #include <KDebug>
23 #include <KPlotPoint>
24
25 ParameterPlotter::ParameterPlotter (QWidget *parent):QWidget(parent){
26         setupUi(this);
27         setAntialiasing(true);
28         setLeftPadding(20);
29         setRightPadding(10);
30         setTopPadding(10);
31         setBottomPadding(20);
32         movepoint=NULL;
33         colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
34         maxy=0;
35         m_moveX=false;
36         m_moveY=true;
37         m_moveTimeline=true;
38         m_newPoints=false;
39         activeIndexPlot=-1;
40         ui.buttonLeftRight->setIcon(KIcon("go-next"));//better icons needed
41         ui.buttonLeftRight->setToolTip(i18n("Allow horizontal moves"));
42         ui.buttonUpDown->setIcon(KIcon("go-up"));
43         ui.buttonUpDown->setToolTip(i18n("Allow vertical moves"));
44         ui.buttonShowInTimeline->setIcon(KIcon("kmplayer"));
45         ui.buttonShowInTimeline->setToolTip(i18n("Show keyframes in timeline"));
46         ui.buttonHelp->setIcon(KIcon("help-about"));
47         ui.buttonHelp->setToolTip(i18n("Parameter info"));
48         ui.buttonNewPoints->setIcon(KIcon("xedit"));
49         ui.buttonNewPoints->setToolTip(i18n("Add keyframe"));
50         connect (ui.buttonLeftRight, SIGNAL (clicked()), this , SLOT ( slotSetMoveX() ) );
51         connect (ui.buttonUpDown, SIGNAL (clicked()), this , SLOT ( slotSetMoveY() ) );
52         connect (ui.buttonShowInTimeline, SIGNAL (clicked()), this , SLOT ( slotShowInTimeline() ) );
53         connect (ui.buttonNewPoints, SIGNAL (clicked()), this , SLOT ( slotSetNew() ) );
54         connect (ui.buttonHelp, SIGNAL (clicked()), this , SLOT ( slotSetHelp() ) );
55 connect (ui.parameterList, SIGNAL (currentIndexChanged ( const QString &  ) ), this, SLOT( slotParameterChanged(const QString&) ) );
56 updateButtonStatus();
57 }
58 /*
59     <name>Lines</name>
60     <description>Lines from top to bottom</description>
61     <author>Marco Gittler</author>
62     <properties tag="lines" id="lines" />
63     <parameter default="5" type="constant" value="5" min="0" name="num" max="255" >
64       <name>Num</name>
65     </parameter>
66     <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
67       <name>Width</name>
68     </parameter>
69   </effect>
70
71 */
72 void ParameterPlotter::setPointLists(const QDomElement& d,int startframe,int endframe){
73         
74         //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
75         itemParameter=d;
76         QDomNodeList namenode = d.elementsByTagName("parameter");
77         
78         int max_y=0;
79         removeAllPlotObjects ();
80         parameterNameList.clear();
81         plotobjects.clear();
82         
83
84         for (int i=0;i< namenode.count() ;i++){
85                 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
86                 plot->setShowLines(true);
87                 //QPair<QString, QMap< int , QVariant > > item=nameit.next();
88                 QDomNode pa=namenode.item(i);
89                 QDomNode na=pa.firstChildElement("name");
90                 
91                 parameterNameList << na.toElement().text();
92                 
93                 
94                 max_y=pa.attributes().namedItem("max").nodeValue().toInt();
95                 int val=pa.attributes().namedItem("value").nodeValue().toInt();
96                 plot->addPoint((i+1)*20,val);
97                 /*TODO keyframes
98                 while (pointit.hasNext()){
99                         pointit.next();
100                         plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
101                         if (pointit.value().toInt() >maxy)
102                                 max_y=pointit.value().toInt();
103                 }*/
104                 plotobjects.append(plot);
105         }
106         maxx=endframe;
107         maxy=max_y;
108         setLimits(0,endframe,0,maxy+10);
109         addPlotObjects(plotobjects);
110
111 }
112
113 void ParameterPlotter::createParametersNew(){
114         
115         QList<KPlotObject*> plotobjs=plotObjects();
116         if (plotobjs.size() != parameterNameList.size() ){
117                 kDebug() << "ERROR size not equal";
118         }
119         QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
120         for (int i=0;i<namenode.count() ;i++){
121                 QList<KPlotPoint*> points=plotobjs[i]->points();
122                 QDomNode pa=namenode.item(i);
123                 
124                 
125                 
126                 
127                 
128                 
129                 QMap<int,QVariant> vals;
130                 foreach (KPlotPoint *o,points){
131                         //vals[o->x()]=o->y();
132                         pa.attributes().namedItem("value").setNodeValue(QString::number(o->y()));
133                 }
134                 QPair<QString,QMap<int,QVariant> > pair("contrast",vals);
135                 //ret.append(pair);
136         }
137         
138         emit parameterChanged(itemParameter);
139         
140 }
141
142
143 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
144         
145         if (movepoint!=NULL){
146                 QList<KPlotPoint*> list=   pointsUnderPoint (event->pos()-QPoint(leftPadding(), topPadding() )  ) ;
147                 int i=0,j=-1;
148                 foreach (KPlotObject *o, plotObjects() ){
149                         QList<KPlotPoint*> points=o->points();
150                         for(int p=0;p<points.size();p++){
151                                 if (points[p]==movepoint){
152                                         QPoint delta=event->pos()-oldmousepoint;
153                                         if (m_moveY)
154                                         movepoint->setY(movepoint->y()-delta.y()*dataRect().height()/pixRect().height() );
155                                         if (p>0 && p<points.size()-1){
156                                                 double newx=movepoint->x()+delta.x()*dataRect().width()/pixRect().width();
157                                                 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
158                                                         movepoint->setX(movepoint->x()+delta.x()*dataRect().width()/pixRect().width() );
159                                         }
160                                         if (m_moveTimeline && (m_moveX|| m_moveY) )
161                                                 emit updateFrame(0);
162                                         replacePlotObject(i,o);
163                                         oldmousepoint=event->pos();
164                                 }
165                         }
166                         i++;
167                 }
168                 createParametersNew();
169         }
170 }
171
172 void ParameterPlotter::replot(const QString & name){
173         //removeAllPlotObjects();
174         int i=0;
175         bool drawAll=name.isEmpty() || name=="all";
176         activeIndexPlot=-1;
177         foreach (KPlotObject* p,plotObjects() ){
178                 p->setShowPoints(drawAll || parameterNameList[i]==name);
179                 p->setShowLines(drawAll || parameterNameList[i]==name);
180                 if ( parameterNameList[i]==name )
181                         activeIndexPlot = i;
182                 replacePlotObject(i++,p);
183         }
184 }
185
186 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
187         //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
188         QPoint inPlot=event->pos()-QPoint(leftPadding(), topPadding() );
189         QList<KPlotPoint*> list=   pointsUnderPoint (inPlot ) ;
190         if (event->button()==Qt::LeftButton){
191                 if (list.size() > 0){
192                         movepoint=list[0];
193                         oldmousepoint=event->pos();
194                 }else{
195                         if (m_newPoints && activeIndexPlot>=0){
196                                 //setup new points
197                                 KPlotObject* p=plotObjects()[activeIndexPlot];
198                                 QList<KPlotPoint*> points=p->points();
199                                 QList<QPointF> newpoints;
200                                 
201                                 double newx=inPlot.x()*dataRect().width()/pixRect().width();
202                                 double newy=(height()-inPlot.y()-bottomPadding()-topPadding() )*dataRect().height()/pixRect().height();
203                                 bool inserted=false;
204                                 foreach (KPlotPoint* pt,points){
205                                         if (pt->x() >newx && !inserted){
206                                                 newpoints.append(QPointF(newx,newy));
207                                                 inserted=true;
208                                         }
209                                         newpoints.append(QPointF(pt->x(),pt->y()));
210                                 }
211                                 p->clearPoints();
212                                 foreach (QPointF qf, newpoints ){
213                                         p->addPoint(qf);
214                                 }
215                                 replacePlotObject(activeIndexPlot,p);
216                         }
217                         movepoint=NULL;
218                 }
219         }else if (event->button()==Qt::LeftButton){
220                 //menu for deleting or exact setup of point
221         }
222 }
223
224 void ParameterPlotter::setMoveX(bool b){
225         m_moveX=b;
226 }
227
228 void ParameterPlotter::setMoveY(bool b){
229         m_moveY=b;
230 }
231
232 void ParameterPlotter::setMoveTimeLine(bool b){
233         m_moveTimeline=b;
234 }
235
236 void ParameterPlotter::setNewPoints(bool b){
237         m_newPoints=b;
238 }
239
240 bool ParameterPlotter::isMoveX(){
241         return m_moveX;
242 }
243
244 bool ParameterPlotter::isMoveY(){
245         return m_moveY;
246 }
247
248 bool ParameterPlotter::isMoveTimeline(){
249         return m_moveTimeline;
250 }
251
252 bool ParameterPlotter::isNewPoints(){
253         return m_newPoints;
254 }
255 void EffectStackView::slotSetMoveX(){
256         ui.kplotwidget->setMoveX(!ui.kplotwidget->isMoveX());
257         updateButtonStatus();
258 }
259
260 void EffectStackView::slotSetMoveY(){
261         ui.kplotwidget->setMoveY(!ui.kplotwidget->isMoveY());
262         updateButtonStatus();
263 }
264
265 void EffectStackView::slotSetNew(){
266         ui.kplotwidget->setNewPoints(!ui.kplotwidget->isNewPoints());
267         updateButtonStatus();
268 }
269
270 void EffectStackView::slotSetHelp(){
271         ui.infoBox->setVisible(!ui.infoBox->isVisible());
272         ui.buttonHelp->setDown(ui.infoBox->isVisible());
273 }
274
275 void EffectStackView::slotShowInTimeline(){
276         
277         ui.kplotwidget->setMoveTimeLine(!ui.kplotwidget->isMoveTimeline());
278         updateButtonStatus();
279         
280 }
281
282 void EffectStackView::updateButtonStatus(){
283         ui.buttonLeftRight->setDown(ui.kplotwidget->isMoveX());
284         ui.buttonUpDown->setDown(ui.kplotwidget->isMoveY());
285         
286         ui.buttonShowInTimeline->setEnabled( ui.kplotwidget->isMoveX() || ui.kplotwidget->isMoveY ()  );
287         ui.buttonShowInTimeline->setDown(ui.kplotwidget->isMoveTimeline());
288         
289         ui.buttonNewPoints->setEnabled(ui.parameterList->currentText()!="all");
290         ui.buttonNewPoints->setDown(ui.kplotwidget->isNewPoints());
291 }
292
293 void EffectStackView::slotParameterChanged(const QString& text){
294         
295         //ui.buttonNewPoints->setEnabled(text!="all");
296         ui.kplotwidget->replot(text);
297         updateButtonStatus();
298 /*
299 ui.parameterList->clear();
300                 ui.parameterList->addItem("all");
301                 QDomNodeList namenode = effects.at(activeRow).elementsByTagName("parameter");
302                 for (int i=0;i<namenode.count();i++){
303                         QDomNode pa=namenode.item(i);
304                         QDomNode na=pa.firstChildElement("name");
305                         ui.parameterList->addItem(na.toElement().text() );
306                 }*/
307 }
308 #endif