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):QWidget(parent){
27 kplotwidget=new PlotWrapper(this);
28 QVBoxLayout *vbox=new QVBoxLayout (this);
29 vbox->addWidget(kplotwidget);
30 widget->setLayout(vbox);
32 kplotwidget->setAntialiasing(true);
33 kplotwidget->setLeftPadding(20);
34 kplotwidget->setRightPadding(10);
35 kplotwidget->setTopPadding(10);
36 kplotwidget->setBottomPadding(20);
38 colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
45 buttonLeftRight->setIcon(KIcon("go-next"));//better icons needed
46 buttonLeftRight->setToolTip(i18n("Allow horizontal moves"));
47 buttonUpDown->setIcon(KIcon("go-up"));
48 buttonUpDown->setToolTip(i18n("Allow vertical moves"));
49 buttonShowInTimeline->setIcon(KIcon("kmplayer"));
50 buttonShowInTimeline->setToolTip(i18n("Show keyframes in timeline"));
51 buttonHelp->setIcon(KIcon("help-about"));
52 buttonHelp->setToolTip(i18n("Parameter info"));
53 buttonNewPoints->setIcon(KIcon("xedit"));
54 buttonNewPoints->setToolTip(i18n("Add keyframe"));
56 connect (buttonLeftRight, SIGNAL (clicked()), this , SLOT ( slotSetMoveX() ) );
57 connect (buttonUpDown, SIGNAL (clicked()), this , SLOT ( slotSetMoveY() ) );
58 connect (buttonShowInTimeline, SIGNAL (clicked()), this , SLOT ( slotShowInTimeline() ) );
59 connect (buttonNewPoints, SIGNAL (clicked()), this , SLOT ( slotSetNew() ) );
60 connect (buttonHelp, SIGNAL (clicked()), this , SLOT ( slotSetHelp() ) );
61 connect (parameterList, SIGNAL (currentIndexChanged ( const QString & ) ), this, SLOT( slotParameterChanged(const QString&) ) );
66 <description>Lines from top to bottom</description>
67 <author>Marco Gittler</author>
68 <properties tag="lines" id="lines" />
69 <parameter default="5" type="constant" value="5" min="0" name="num" max="255" >
72 <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
78 void ParameterPlotter::setPointLists(const QDomElement& d,int startframe,int endframe){
80 //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
82 QDomNodeList namenode = d.elementsByTagName("parameter");
85 kplotwidget->removeAllPlotObjects ();
86 parameterNameList.clear();
90 for (int i=0;i< namenode.count() ;i++){
91 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
92 plot->setShowLines(true);
93 //QPair<QString, QMap< int , QVariant > > item=nameit.next();
94 QDomNode pa=namenode.item(i);
95 QDomNode na=pa.firstChildElement("name");
97 parameterNameList << na.toElement().text();
100 max_y=pa.attributes().namedItem("max").nodeValue().toInt();
101 int val=pa.attributes().namedItem("value").nodeValue().toInt();
102 plot->addPoint((i+1)*20,val);
104 while (pointit.hasNext()){
106 plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
107 if (pointit.value().toInt() >maxy)
108 max_y=pointit.value().toInt();
110 plotobjects.append(plot);
114 kplotwidget->setLimits(0,endframe,0,maxy+10);
115 kplotwidget->addPlotObjects(plotobjects);
119 void ParameterPlotter::createParametersNew(){
121 QList<KPlotObject*> plotobjs=kplotwidget->plotObjects();
122 if (plotobjs.size() != parameterNameList.size() ){
123 kDebug() << "ERROR size not equal";
125 QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
126 for (int i=0;i<namenode.count() ;i++){
127 QList<KPlotPoint*> points=plotobjs[i]->points();
128 QDomNode pa=namenode.item(i);
135 QMap<int,QVariant> vals;
136 foreach (KPlotPoint *o,points){
137 //vals[o->x()]=o->y();
138 pa.attributes().namedItem("value").setNodeValue(QString::number(o->y()));
140 QPair<QString,QMap<int,QVariant> > pair("contrast",vals);
144 emit parameterChanged(itemParameter);
149 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
151 if (movepoint!=NULL){
152 QList<KPlotPoint*> list= kplotwidget->pointsUnderPoint (event->pos()-QPoint(kplotwidget->leftPadding(), kplotwidget->topPadding() ) ) ;
154 foreach (KPlotObject *o, kplotwidget->plotObjects() ){
155 QList<KPlotPoint*> points=o->points();
156 for(int p=0;p<points.size();p++){
157 if (points[p]==movepoint){
158 QPoint delta=event->pos()-oldmousepoint;
160 movepoint->setY(movepoint->y()-delta.y()*kplotwidget->dataRect().height()/kplotwidget->pixRect().height() );
161 if (p>0 && p<points.size()-1){
162 double newx=movepoint->x()+delta.x()*kplotwidget->dataRect().width()/kplotwidget->pixRect().width();
163 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
164 movepoint->setX(movepoint->x()+delta.x()*kplotwidget->dataRect().width()/kplotwidget->pixRect().width() );
166 if (m_moveTimeline && (m_moveX|| m_moveY) )
168 kplotwidget->replacePlotObject(i,o);
169 oldmousepoint=event->pos();
174 createParametersNew();
178 void ParameterPlotter::replot(const QString & name){
179 //removeAllPlotObjects();
181 bool drawAll=name.isEmpty() || name=="all";
183 foreach (KPlotObject* p,kplotwidget->plotObjects() ){
184 p->setShowPoints(drawAll || parameterNameList[i]==name);
185 p->setShowLines(drawAll || parameterNameList[i]==name);
186 if ( parameterNameList[i]==name )
188 kplotwidget->replacePlotObject(i++,p);
192 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
193 //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
194 QPoint inPlot=event->pos()-QPoint(kplotwidget->leftPadding(), kplotwidget->topPadding() );
195 QList<KPlotPoint*> list= kplotwidget->pointsUnderPoint (inPlot ) ;
196 if (event->button()==Qt::LeftButton){
197 if (list.size() > 0){
199 oldmousepoint=event->pos();
201 if (m_newPoints && activeIndexPlot>=0){
203 KPlotObject* p=kplotwidget->plotObjects()[activeIndexPlot];
204 QList<KPlotPoint*> points=p->points();
205 QList<QPointF> newpoints;
207 double newx=inPlot.x()*kplotwidget->dataRect().width()/kplotwidget->pixRect().width();
208 double newy=(height()-inPlot.y()-kplotwidget->bottomPadding()-kplotwidget->topPadding() )*kplotwidget->dataRect().height()/kplotwidget->pixRect().height();
210 foreach (KPlotPoint* pt,points){
211 if (pt->x() >newx && !inserted){
212 newpoints.append(QPointF(newx,newy));
215 newpoints.append(QPointF(pt->x(),pt->y()));
218 foreach (QPointF qf, newpoints ){
221 kplotwidget->replacePlotObject(activeIndexPlot,p);
225 }else if (event->button()==Qt::LeftButton){
226 //menu for deleting or exact setup of point
231 void ParameterPlotter::slotSetMoveX(){
233 updateButtonStatus();
236 void ParameterPlotter::slotSetMoveY(){
238 updateButtonStatus();
241 void ParameterPlotter::slotSetNew(){
242 m_newPoints=!m_newPoints;
243 updateButtonStatus();
246 void ParameterPlotter::slotSetHelp(){
247 infoBox->setVisible(!infoBox->isVisible());
248 buttonHelp->setDown(infoBox->isVisible());
251 void ParameterPlotter::slotShowInTimeline(){
253 m_moveTimeline=!m_moveTimeline;
254 updateButtonStatus();
258 void ParameterPlotter::updateButtonStatus(){
259 buttonLeftRight->setDown(m_moveY);
260 buttonUpDown->setDown(m_moveX);
262 buttonShowInTimeline->setEnabled( m_moveX || m_moveY);
263 buttonShowInTimeline->setDown(m_moveTimeline);
265 ///TODO buttonNewPoints->setEnabled(currentText()!="all");
266 buttonNewPoints->setDown(m_newPoints);
269 void ParameterPlotter::slotParameterChanged(const QString& text){
271 //ui.buttonNewPoints->setEnabled(text!="all");
273 updateButtonStatus();
275 ui.parameterList->clear();
276 ui.parameterList->addItem("all");
277 QDomNodeList namenode = effects.at(activeRow).elementsByTagName("parameter");
278 for (int i=0;i<namenode.count();i++){
279 QDomNode pa=namenode.item(i);
280 QDomNode na=pa.firstChildElement("name");
281 ui.parameterList->addItem(na.toElement().text() );