]> git.sesse.net Git - kdenlive/blob - src/parameterplotter.cpp
effectstack can setup the parameter now
[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
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):KPlotWidget (parent){
26         setAntialiasing(true);
27         setLeftPadding(20);
28         setRightPadding(10);
29         setTopPadding(10);
30         setBottomPadding(20);
31         movepoint=NULL;
32         colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
33         maxy=0;
34         m_moveX=false;
35         m_moveY=true;
36         m_moveTimeline=true;
37         m_newPoints=false;
38         activeIndexPlot=-1;
39 }
40 /*
41     <name>Lines</name>
42     <description>Lines from top to bottom</description>
43     <author>Marco Gittler</author>
44     <properties tag="lines" id="lines" />
45     <parameter default="5" type="constant" value="5" min="0" name="num" max="255" >
46       <name>Num</name>
47     </parameter>
48     <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
49       <name>Width</name>
50     </parameter>
51   </effect>
52
53 */
54 void ParameterPlotter::setPointLists(const QDomElement& d,int startframe,int endframe){
55         
56         //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
57         itemParameter=d;
58         QDomNodeList namenode = d.elementsByTagName("parameter");
59         
60         int max_y=0;
61         removeAllPlotObjects ();
62         parameterNameList.clear();
63         plotobjects.clear();
64         
65
66         for (int i=0;i< namenode.count() ;i++){
67                 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
68                 plot->setShowLines(true);
69                 //QPair<QString, QMap< int , QVariant > > item=nameit.next();
70                 QDomNode pa=namenode.item(i);
71                 QDomNode na=pa.firstChildElement("name");
72                 
73                 parameterNameList << na.toElement().text();
74                 
75                 
76                 max_y=pa.attributes().namedItem("max").nodeValue().toInt();
77                 int val=pa.attributes().namedItem("value").nodeValue().toInt();
78                 plot->addPoint((i+1)*20,val);
79                 /*TODO keyframes
80                 while (pointit.hasNext()){
81                         pointit.next();
82                         plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
83                         if (pointit.value().toInt() >maxy)
84                                 max_y=pointit.value().toInt();
85                 }*/
86                 plotobjects.append(plot);
87         }
88         maxx=endframe;
89         maxy=max_y;
90         setLimits(0,endframe,0,maxy+10);
91         addPlotObjects(plotobjects);
92
93 }
94
95 void ParameterPlotter::createParametersNew(){
96         
97         QList<KPlotObject*> plotobjs=plotObjects();
98         if (plotobjs.size() != parameterNameList.size() ){
99                 kDebug() << "ERROR size not equal";
100         }
101         QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
102         for (int i=0;i<namenode.count() ;i++){
103                 QList<KPlotPoint*> points=plotobjs[i]->points();
104                 QDomNode pa=namenode.item(i);
105                 
106                 
107                 
108                 
109                 
110                 
111                 QMap<int,QVariant> vals;
112                 foreach (KPlotPoint *o,points){
113                         //vals[o->x()]=o->y();
114                         pa.attributes().namedItem("value").setNodeValue(QString::number(o->y()));
115                 }
116                 QPair<QString,QMap<int,QVariant> > pair("contrast",vals);
117                 //ret.append(pair);
118         }
119         
120         emit parameterChanged(itemParameter);
121         
122 }
123
124
125 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
126         
127         if (movepoint!=NULL){
128                 QList<KPlotPoint*> list=   pointsUnderPoint (event->pos()-QPoint(leftPadding(), topPadding() )  ) ;
129                 int i=0,j=-1;
130                 foreach (KPlotObject *o, plotObjects() ){
131                         QList<KPlotPoint*> points=o->points();
132                         for(int p=0;p<points.size();p++){
133                                 if (points[p]==movepoint){
134                                         QPoint delta=event->pos()-oldmousepoint;
135                                         if (m_moveY)
136                                         movepoint->setY(movepoint->y()-delta.y()*dataRect().height()/pixRect().height() );
137                                         if (p>0 && p<points.size()-1){
138                                                 double newx=movepoint->x()+delta.x()*dataRect().width()/pixRect().width();
139                                                 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
140                                                         movepoint->setX(movepoint->x()+delta.x()*dataRect().width()/pixRect().width() );
141                                         }
142                                         if (m_moveTimeline && (m_moveX|| m_moveY) )
143                                                 emit updateFrame(0);
144                                         replacePlotObject(i,o);
145                                         oldmousepoint=event->pos();
146                                 }
147                         }
148                         i++;
149                 }
150                 createParametersNew();
151         }
152 }
153
154 void ParameterPlotter::replot(const QString & name){
155         //removeAllPlotObjects();
156         int i=0;
157         bool drawAll=name.isEmpty() || name=="all";
158         activeIndexPlot=-1;
159         foreach (KPlotObject* p,plotObjects() ){
160                 p->setShowPoints(drawAll || parameterNameList[i]==name);
161                 p->setShowLines(drawAll || parameterNameList[i]==name);
162                 if ( parameterNameList[i]==name )
163                         activeIndexPlot = i;
164                 replacePlotObject(i++,p);
165         }
166 }
167
168 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
169         //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
170         QPoint inPlot=event->pos()-QPoint(leftPadding(), topPadding() );
171         QList<KPlotPoint*> list=   pointsUnderPoint (inPlot ) ;
172         if (event->button()==Qt::LeftButton){
173                 if (list.size() > 0){
174                         movepoint=list[0];
175                         oldmousepoint=event->pos();
176                 }else{
177                         if (m_newPoints && activeIndexPlot>=0){
178                                 //setup new points
179                                 KPlotObject* p=plotObjects()[activeIndexPlot];
180                                 QList<KPlotPoint*> points=p->points();
181                                 QList<QPointF> newpoints;
182                                 
183                                 double newx=inPlot.x()*dataRect().width()/pixRect().width();
184                                 double newy=(height()-inPlot.y()-bottomPadding()-topPadding() )*dataRect().height()/pixRect().height();
185                                 bool inserted=false;
186                                 foreach (KPlotPoint* pt,points){
187                                         if (pt->x() >newx && !inserted){
188                                                 newpoints.append(QPointF(newx,newy));
189                                                 inserted=true;
190                                         }
191                                         newpoints.append(QPointF(pt->x(),pt->y()));
192                                 }
193                                 p->clearPoints();
194                                 foreach (QPointF qf, newpoints ){
195                                         p->addPoint(qf);
196                                 }
197                                 replacePlotObject(activeIndexPlot,p);
198                         }
199                         movepoint=NULL;
200                 }
201         }else if (event->button()==Qt::LeftButton){
202                 //menu for deleting or exact setup of point
203         }
204 }
205
206 void ParameterPlotter::setMoveX(bool b){
207         m_moveX=b;
208 }
209
210 void ParameterPlotter::setMoveY(bool b){
211         m_moveY=b;
212 }
213
214 void ParameterPlotter::setMoveTimeLine(bool b){
215         m_moveTimeline=b;
216 }
217
218 void ParameterPlotter::setNewPoints(bool b){
219         m_newPoints=b;
220 }
221
222 bool ParameterPlotter::isMoveX(){
223         return m_moveX;
224 }
225
226 bool ParameterPlotter::isMoveY(){
227         return m_moveY;
228 }
229
230 bool ParameterPlotter::isMoveTimeline(){
231         return m_moveTimeline;
232 }
233
234 bool ParameterPlotter::isNewPoints(){
235         return m_newPoints;
236 }