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>
25 ParameterPlotter::ParameterPlotter (QWidget *parent):KPlotWidget (parent){
26 setAntialiasing(true);
32 colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
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" >
48 <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
54 void ParameterPlotter::setPointLists(const QDomElement& d,int startframe,int endframe){
56 //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
58 QDomNodeList namenode = d.elementsByTagName("parameter");
61 removeAllPlotObjects ();
62 parameterNameList.clear();
66 QTextStream stre(&dat);
70 if ( namenode.count() ){
73 QDomNode pa=namenode.item(0);
74 QDomNode na=pa.firstChildElement("name");
76 parameterNameList << pa.attributes().namedItem("name").nodeValue().split(";");
77 emit parameterList(parameterNameList);
79 //max_y=pa.attributes().namedItem("max").nodeValue().toInt();
80 //int val=pa.attributes().namedItem("value").nodeValue().toInt();
81 QStringList defauls=pa.attributes().namedItem("default").nodeValue().split(";");
82 QStringList maxv=pa.attributes().namedItem("max").nodeValue().split(";");
83 QStringList minv=pa.attributes().namedItem("max").nodeValue().split(";");
84 for (int i=0;i<maxv.size();i++){
85 if (max_y< maxv[i].toInt()) max_y=maxv[i].toInt();
86 if (min_y< minv[i].toInt()) min_y=minv[i].toInt();
89 for (int i=0;i<parameterNameList.count();i++){
90 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
91 plot->setShowLines(true);
93 plot->addPoint(startframe,defauls[i].toInt());
95 plot->addPoint(endframe,defauls[i].toInt());
97 plotobjects.append(plot);
101 while (pointit.hasNext()){
103 plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
104 if (pointit.value().toInt() >maxy)
105 max_y=pointit.value().toInt();
111 setLimits(-1,endframe+1,-1,maxy+10);
112 addPlotObjects(plotobjects);
116 void ParameterPlotter::createParametersNew(){
118 QList<KPlotObject*> plotobjs=plotObjects();
119 if (plotobjs.size() != parameterNameList.size() ){
120 kDebug() << "ERROR size not equal";
122 QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
123 for (int i=0;i<namenode.count() ;i++){
124 QList<KPlotPoint*> points=plotobjs[i]->points();
125 QDomNode pa=namenode.item(i);
132 QMap<int,QVariant> vals;
133 foreach (KPlotPoint *o,points){
134 //vals[o->x()]=o->y();
135 pa.attributes().namedItem("value").setNodeValue(QString::number(o->y()));
137 QPair<QString,QMap<int,QVariant> > pair("contrast",vals);
141 emit parameterChanged(itemParameter);
146 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
148 if (movepoint!=NULL){
149 QList<KPlotPoint*> list= pointsUnderPoint (event->pos()-QPoint(leftPadding(), topPadding() ) ) ;
151 foreach (KPlotObject *o, plotObjects() ){
152 QList<KPlotPoint*> points=o->points();
153 for(int p=0;p<points.size();p++){
154 if (points[p]==movepoint){
155 QPoint delta=event->pos()-oldmousepoint;
157 movepoint->setY(movepoint->y()-delta.y()*dataRect().height()/pixRect().height() );
158 if (p>0 && p<points.size()-1){
159 double newx=movepoint->x()+delta.x()*dataRect().width()/pixRect().width();
160 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
161 movepoint->setX(movepoint->x()+delta.x()*dataRect().width()/pixRect().width() );
163 if (m_moveTimeline && (m_moveX|| m_moveY) )
165 replacePlotObject(i,o);
166 oldmousepoint=event->pos();
171 createParametersNew();
175 void ParameterPlotter::replot(const QString & name){
176 //removeAllPlotObjects();
178 bool drawAll=name.isEmpty() || name=="all";
180 foreach (KPlotObject* p,plotObjects() ){
181 p->setShowPoints(drawAll || parameterNameList[i]==name);
182 p->setShowLines(drawAll || parameterNameList[i]==name);
183 if ( parameterNameList[i]==name )
185 replacePlotObject(i++,p);
189 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
190 //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
191 QPoint inPlot=event->pos()-QPoint(leftPadding(), topPadding() );
192 QList<KPlotPoint*> list= pointsUnderPoint (inPlot ) ;
193 if (event->button()==Qt::LeftButton){
194 if (list.size() > 0){
196 oldmousepoint=event->pos();
198 if (m_newPoints && activeIndexPlot>=0){
200 KPlotObject* p=plotObjects()[activeIndexPlot];
201 QList<KPlotPoint*> points=p->points();
202 QList<QPointF> newpoints;
204 double newx=inPlot.x()*dataRect().width()/pixRect().width();
205 double newy=(height()-inPlot.y()-bottomPadding()-topPadding() )*dataRect().height()/pixRect().height();
207 foreach (KPlotPoint* pt,points){
208 if (pt->x() >newx && !inserted){
209 newpoints.append(QPointF(newx,newy));
212 newpoints.append(QPointF(pt->x(),pt->y()));
215 foreach (QPointF qf, newpoints ){
218 replacePlotObject(activeIndexPlot,p);
222 }else if (event->button()==Qt::LeftButton){
223 //menu for deleting or exact setup of point
227 void ParameterPlotter::setMoveX(bool b){
231 void ParameterPlotter::setMoveY(bool b){
235 void ParameterPlotter::setMoveTimeLine(bool b){
239 void ParameterPlotter::setNewPoints(bool b){
243 bool ParameterPlotter::isMoveX(){
247 bool ParameterPlotter::isMoveY(){
251 bool ParameterPlotter::isMoveTimeline(){
252 return m_moveTimeline;
255 bool ParameterPlotter::isNewPoints(){