]> git.sesse.net Git - kdenlive/blob - src/parameterplotter.cpp
new points can be simple added
[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 void ParameterPlotter::setPointLists(const QList< QPair<QString, QMap< int , QVariant > > >& params,int startframe, int endframe){
42         
43         QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
44         int max_y=0;
45         parameterNameList.clear();
46         
47         
48         while (nameit.hasNext() ){      
49                 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
50                 plot->setShowLines(true);
51                 QPair<QString, QMap< int , QVariant > > item=nameit.next();
52                 parameterNameList << item.first;
53
54                 QMapIterator <int,QVariant> pointit=item.second;
55                 while (pointit.hasNext()){
56                         pointit.next();
57                         plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
58                         if (pointit.value().toInt() >maxy)
59                                 max_y=pointit.value().toInt();
60                 }
61                 plotobjects.append(plot);
62         }
63         maxx=endframe;
64         maxy=max_y;
65         setLimits(0,endframe,0,maxy+10);
66         addPlotObjects(plotobjects);
67
68 }
69
70 void ParameterPlotter::createParametersNew(){
71         QList< QPair<QString, QMap<int,QVariant> > > ret;
72         QList<KPlotObject*> plotobjs=plotObjects();
73         if (plotobjs.size() != parameterNameList.size() ){
74                 kDebug() << "ERROR size not equal";
75         }
76         
77         for (int i=0;i<parameterNameList.size() ;i++){
78                 QList<KPlotPoint*> points=plotobjs[i]->points();
79                 QMap<int,QVariant> vals;
80                 foreach (KPlotPoint *o,points){
81                         vals[o->x()]=o->y();
82                 }
83                 QPair<QString,QMap<int,QVariant> > pair("contrast",vals);
84                 ret.append(pair);
85         }
86         
87         emit parameterChanged(ret);
88         
89 }
90
91
92 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
93         
94         if (movepoint!=NULL){
95                 QList<KPlotPoint*> list=   pointsUnderPoint (event->pos()-QPoint(leftPadding(), topPadding() )  ) ;
96                 int i=0,j=-1;
97                 foreach (KPlotObject *o, plotObjects() ){
98                         QList<KPlotPoint*> points=o->points();
99                         for(int p=0;p<points.size();p++){
100                                 if (points[p]==movepoint){
101                                         QPoint delta=event->pos()-oldmousepoint;
102                                         if (m_moveY)
103                                         movepoint->setY(movepoint->y()-delta.y()*dataRect().height()/pixRect().height() );
104                                         if (p>0 && p<points.size()-1){
105                                                 double newx=movepoint->x()+delta.x()*dataRect().width()/pixRect().width();
106                                                 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
107                                                         movepoint->setX(movepoint->x()+delta.x()*dataRect().width()/pixRect().width() );
108                                         }
109                                         if (m_moveTimeline && (m_moveX|| m_moveY) )
110                                                 emit updateFrame(0);
111                                         replacePlotObject(i,o);
112                                         oldmousepoint=event->pos();
113                                 }
114                         }
115                         i++;
116                 }
117                 createParametersNew();
118         }
119 }
120
121 void ParameterPlotter::replot(const QString & name){
122         //removeAllPlotObjects();
123         int i=0;
124         bool drawAll=name.isEmpty() || name=="all";
125         activeIndexPlot=-1;
126         foreach (KPlotObject* p,plotObjects() ){
127                 p->setShowPoints(drawAll || parameterNameList[i]==name);
128                 p->setShowLines(drawAll || parameterNameList[i]==name);
129                 if ( parameterNameList[i]==name )
130                         activeIndexPlot = i;
131                 replacePlotObject(i++,p);
132         }
133 }
134
135 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
136         //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
137         QPoint inPlot=event->pos()-QPoint(leftPadding(), topPadding() );
138         QList<KPlotPoint*> list=   pointsUnderPoint (inPlot ) ;
139         if (list.size() > 0){
140                 movepoint=list[0];
141                 oldmousepoint=event->pos();
142         }else{
143                 if (m_newPoints && activeIndexPlot>=0){
144                         //setup new points
145                         KPlotObject* p=plotObjects()[activeIndexPlot];
146                         QList<KPlotPoint*> points=p->points();
147                         QList<QPointF> newpoints;
148                         
149                         double newx=inPlot.x()*dataRect().width()/pixRect().width();
150                         double newy=(height()-inPlot.y()-bottomPadding()-topPadding() )*dataRect().height()/pixRect().height();
151                         bool inserted=false;
152                         foreach (KPlotPoint* pt,points){
153                                 if (pt->x() >newx && !inserted){
154                                         newpoints.append(QPointF(newx,newy));
155                                         inserted=true;
156                                 }
157                                 newpoints.append(QPointF(pt->x(),pt->y()));
158                         }
159                         p->clearPoints();
160                         foreach (QPointF qf, newpoints ){
161                                 p->addPoint(qf);
162                         }
163                         replacePlotObject(activeIndexPlot,p);
164                 }
165                 movepoint=NULL;
166         }
167 }
168
169 void ParameterPlotter::setMoveX(bool b){
170         m_moveX=b;
171 }
172
173 void ParameterPlotter::setMoveY(bool b){
174         m_moveY=b;
175 }
176
177 void ParameterPlotter::setMoveTimeLine(bool b){
178         m_moveTimeline=b;
179 }
180
181 void ParameterPlotter::setNewPoints(bool b){
182         m_newPoints=b;
183 }
184
185 bool ParameterPlotter::isMoveX(){
186         return m_moveX;
187 }
188
189 bool ParameterPlotter::isMoveY(){
190         return m_moveY;
191 }
192
193 bool ParameterPlotter::isMoveTimeline(){
194         return m_moveTimeline;
195 }
196
197 bool ParameterPlotter::isNewPoints(){
198         return m_newPoints;
199 }