1 /***************************************************************************
2 parameterplotter.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 ***************************************************************************/
18 #include "parameterplotter.h"
20 #include <KPlotObject>
21 #include <QMouseEvent>
26 ParameterPlotter::ParameterPlotter (QWidget *parent):KPlotWidget (parent){
27 setAntialiasing(true);
33 colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
43 <description>Lines from top to bottom</description>
44 <author>Marco Gittler</author>
45 <properties tag="lines" id="lines" />
46 <parameter default="5" type="constant" value="5" min="0" name="num" max="255" >
49 <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
55 void ParameterPlotter::setPointLists(const QDomElement& d,int startframe,int endframe){
57 //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
59 QDomNodeList namenode = d.elementsByTagName("parameter");
63 removeAllPlotObjects ();
64 stretchFactors.clear();
65 parameterNameList.clear();
69 QTextStream stre(&dat);
73 if ( namenode.count() ){
76 QDomNode pa=namenode.item(0);
77 QDomNode na=pa.firstChildElement("name");
79 parameterNameList << pa.attributes().namedItem("name").nodeValue().split(";");
80 emit parameterList(parameterNameList);
82 //max_y=pa.attributes().namedItem("max").nodeValue().toInt();
83 //int val=pa.attributes().namedItem("value").nodeValue().toInt();
85 if (pa.attributes().namedItem("start").nodeValue().contains(";"))
86 defaults=pa.attributes().namedItem("start").nodeValue().split(";");
87 else if (pa.attributes().namedItem("value").nodeValue().contains(";"))
88 defaults=pa.attributes().namedItem("value").nodeValue().split(";");
89 else if (pa.attributes().namedItem("default").nodeValue().contains(";"))
90 defaults=pa.attributes().namedItem("default").nodeValue().split(";");
91 QStringList maxv=pa.attributes().namedItem("max").nodeValue().split(";");
92 QStringList minv=pa.attributes().namedItem("max").nodeValue().split(";");
93 for (int i=0;i<maxv.size();i++){
94 if (max_y< maxv[i].toInt()) max_y=maxv[i].toInt();
95 if (min_y> minv[i].toInt()) min_y=minv[i].toInt();
98 for (int i=0;i<parameterNameList.count();i++){
99 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
100 plot->setShowLines(true);
101 if (!stretchFactors.contains(i) ){
102 if ( maxv[i].toInt()!=0)
103 stretchFactors[i]=max_y/maxv[i].toInt();
105 stretchFactors[i]=1.0;
107 if (defaults[i].toDouble()>max_y)
109 plot->addPoint(startframe,defaults[i].toInt()*stretchFactors[i],parameterNameList[i]);
111 plot->addPoint(endframe,defaults[i].toInt()*stretchFactors[i]);
113 plotobjects.append(plot);
117 while (pointit.hasNext()){
119 plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
120 if (pointit.value().toInt() >maxy)
121 max_y=pointit.value().toInt();
127 setLimits(-1,endframe+1,min_y-10,maxy+10);
128 addPlotObjects(plotobjects);
132 void ParameterPlotter::createParametersNew(){
134 QList<KPlotObject*> plotobjs=plotObjects();
135 if (plotobjs.size() != parameterNameList.size() ){
136 kDebug() << "ERROR size not equal";
138 QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
140 QTextStream txtstr(¶mlist);
141 QDomNode pa=namenode.item(0);
142 if (namenode.count()>0){
143 for (int i=0;i<plotobjs.count();i++){
144 QList<KPlotPoint*> points=plotobjs[i]->points();
145 foreach (KPlotPoint *o,points){
146 txtstr << (int)o->y() ;
147 break;//first no keyframes
149 if (i+1!=plotobjs.count())
153 pa.attributes().namedItem("value").setNodeValue(paramlist);
154 pa.attributes().namedItem("start").setNodeValue(paramlist);
155 emit parameterChanged(itemParameter);
160 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
162 if (movepoint!=NULL){
163 QList<KPlotPoint*> list= pointsUnderPoint (event->pos()-QPoint(leftPadding(), topPadding() ) ) ;
165 foreach (KPlotObject *o, plotObjects() ){
166 QList<KPlotPoint*> points=o->points();
167 for(int p=0;p<points.size();p++){
168 if (points[p]==movepoint && (activeIndexPlot==-1 || activeIndexPlot==i ) ){
169 QPoint delta=event->pos()-oldmousepoint;
170 double newy=movepoint->y()-delta.y()*dataRect().height()/pixRect().height();
171 if (m_moveY && newy>min_y && newy<max_y)
172 movepoint->setY(newy);
173 if (p>0 && p<points.size()-1){
174 double newx=movepoint->x()+delta.x()*dataRect().width()/pixRect().width();
175 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
176 movepoint->setX(movepoint->x()+delta.x()*dataRect().width()/pixRect().width() );
178 if (m_moveTimeline && (m_moveX|| m_moveY) )
180 replacePlotObject(i,o);
181 oldmousepoint=event->pos();
186 createParametersNew();
190 void ParameterPlotter::replot(const QString & name){
191 //removeAllPlotObjects();
193 bool drawAll=name.isEmpty() || name=="all";
196 foreach (KPlotObject* p, plotObjects() ){
197 p->setShowPoints(drawAll || parameterNameList[i]==name);
198 p->setShowLines(drawAll || parameterNameList[i]==name);
199 QPen pen=(drawAll || parameterNameList[i]==name ? QPen(Qt::SolidLine) : QPen(Qt::NoPen) );
200 pen.setColor(p->linePen().color());
202 if ( parameterNameList[i]==name )
204 replacePlotObject(i++,p);
208 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
209 //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
210 QPoint inPlot=event->pos()-QPoint(leftPadding(), topPadding() );
211 QList<KPlotPoint*> list= pointsUnderPoint (inPlot ) ;
212 if (event->button()==Qt::LeftButton){
213 if (list.size() > 0){
215 oldmousepoint=event->pos();
217 if (m_newPoints && activeIndexPlot>=0){
219 KPlotObject* p=plotObjects()[activeIndexPlot];
220 QList<KPlotPoint*> points=p->points();
221 QList<QPointF> newpoints;
223 double newx=inPlot.x()*dataRect().width()/pixRect().width();
224 double newy=(height()-inPlot.y()-bottomPadding()-topPadding() )*dataRect().height()/pixRect().height();
226 foreach (KPlotPoint* pt,points){
227 if (pt->x() >newx && !inserted){
228 newpoints.append(QPointF(newx,newy));
231 newpoints.append(QPointF(pt->x(),pt->y()));
234 foreach (QPointF qf, newpoints ){
237 replacePlotObject(activeIndexPlot,p);
241 }else if (event->button()==Qt::LeftButton){
242 //menu for deleting or exact setup of point
246 void ParameterPlotter::setMoveX(bool b){
250 void ParameterPlotter::setMoveY(bool b){
254 void ParameterPlotter::setMoveTimeLine(bool b){
258 void ParameterPlotter::setNewPoints(bool b){
262 bool ParameterPlotter::isMoveX(){
266 bool ParameterPlotter::isMoveY(){
270 bool ParameterPlotter::isMoveTimeline(){
271 return m_moveTimeline;
274 bool ParameterPlotter::isNewPoints(){