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 setAntialiasing(true);
33 colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
40 ui.buttonLeftRight->setIcon(KIcon("go-next"));//better icons needed
41 ui.buttonLeftRight->setToolTip(i18n("Allow horizontal moves"));
42 ui.buttonUpDown->setIcon(KIcon("go-up"));
43 ui.buttonUpDown->setToolTip(i18n("Allow vertical moves"));
44 ui.buttonShowInTimeline->setIcon(KIcon("kmplayer"));
45 ui.buttonShowInTimeline->setToolTip(i18n("Show keyframes in timeline"));
46 ui.buttonHelp->setIcon(KIcon("help-about"));
47 ui.buttonHelp->setToolTip(i18n("Parameter info"));
48 ui.buttonNewPoints->setIcon(KIcon("xedit"));
49 ui.buttonNewPoints->setToolTip(i18n("Add keyframe"));
50 connect (ui.buttonLeftRight, SIGNAL (clicked()), this , SLOT ( slotSetMoveX() ) );
51 connect (ui.buttonUpDown, SIGNAL (clicked()), this , SLOT ( slotSetMoveY() ) );
52 connect (ui.buttonShowInTimeline, SIGNAL (clicked()), this , SLOT ( slotShowInTimeline() ) );
53 connect (ui.buttonNewPoints, SIGNAL (clicked()), this , SLOT ( slotSetNew() ) );
54 connect (ui.buttonHelp, SIGNAL (clicked()), this , SLOT ( slotSetHelp() ) );
55 connect (ui.parameterList, SIGNAL (currentIndexChanged ( const QString & ) ), this, SLOT( slotParameterChanged(const QString&) ) );
60 <description>Lines from top to bottom</description>
61 <author>Marco Gittler</author>
62 <properties tag="lines" id="lines" />
63 <parameter default="5" type="constant" value="5" min="0" name="num" max="255" >
66 <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
72 void ParameterPlotter::setPointLists(const QDomElement& d,int startframe,int endframe){
74 //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
76 QDomNodeList namenode = d.elementsByTagName("parameter");
79 removeAllPlotObjects ();
80 parameterNameList.clear();
84 for (int i=0;i< namenode.count() ;i++){
85 KPlotObject *plot=new KPlotObject(colors[plotobjects.size()%colors.size()]);
86 plot->setShowLines(true);
87 //QPair<QString, QMap< int , QVariant > > item=nameit.next();
88 QDomNode pa=namenode.item(i);
89 QDomNode na=pa.firstChildElement("name");
91 parameterNameList << na.toElement().text();
94 max_y=pa.attributes().namedItem("max").nodeValue().toInt();
95 int val=pa.attributes().namedItem("value").nodeValue().toInt();
96 plot->addPoint((i+1)*20,val);
98 while (pointit.hasNext()){
100 plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
101 if (pointit.value().toInt() >maxy)
102 max_y=pointit.value().toInt();
104 plotobjects.append(plot);
108 setLimits(0,endframe,0,maxy+10);
109 addPlotObjects(plotobjects);
113 void ParameterPlotter::createParametersNew(){
115 QList<KPlotObject*> plotobjs=plotObjects();
116 if (plotobjs.size() != parameterNameList.size() ){
117 kDebug() << "ERROR size not equal";
119 QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
120 for (int i=0;i<namenode.count() ;i++){
121 QList<KPlotPoint*> points=plotobjs[i]->points();
122 QDomNode pa=namenode.item(i);
129 QMap<int,QVariant> vals;
130 foreach (KPlotPoint *o,points){
131 //vals[o->x()]=o->y();
132 pa.attributes().namedItem("value").setNodeValue(QString::number(o->y()));
134 QPair<QString,QMap<int,QVariant> > pair("contrast",vals);
138 emit parameterChanged(itemParameter);
143 void ParameterPlotter::mouseMoveEvent ( QMouseEvent * event ) {
145 if (movepoint!=NULL){
146 QList<KPlotPoint*> list= pointsUnderPoint (event->pos()-QPoint(leftPadding(), topPadding() ) ) ;
148 foreach (KPlotObject *o, plotObjects() ){
149 QList<KPlotPoint*> points=o->points();
150 for(int p=0;p<points.size();p++){
151 if (points[p]==movepoint){
152 QPoint delta=event->pos()-oldmousepoint;
154 movepoint->setY(movepoint->y()-delta.y()*dataRect().height()/pixRect().height() );
155 if (p>0 && p<points.size()-1){
156 double newx=movepoint->x()+delta.x()*dataRect().width()/pixRect().width();
157 if ( newx>points[p-1]->x() && newx<points[p+1]->x() && m_moveX)
158 movepoint->setX(movepoint->x()+delta.x()*dataRect().width()/pixRect().width() );
160 if (m_moveTimeline && (m_moveX|| m_moveY) )
162 replacePlotObject(i,o);
163 oldmousepoint=event->pos();
168 createParametersNew();
172 void ParameterPlotter::replot(const QString & name){
173 //removeAllPlotObjects();
175 bool drawAll=name.isEmpty() || name=="all";
177 foreach (KPlotObject* p,plotObjects() ){
178 p->setShowPoints(drawAll || parameterNameList[i]==name);
179 p->setShowLines(drawAll || parameterNameList[i]==name);
180 if ( parameterNameList[i]==name )
182 replacePlotObject(i++,p);
186 void ParameterPlotter::mousePressEvent ( QMouseEvent * event ) {
187 //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
188 QPoint inPlot=event->pos()-QPoint(leftPadding(), topPadding() );
189 QList<KPlotPoint*> list= pointsUnderPoint (inPlot ) ;
190 if (event->button()==Qt::LeftButton){
191 if (list.size() > 0){
193 oldmousepoint=event->pos();
195 if (m_newPoints && activeIndexPlot>=0){
197 KPlotObject* p=plotObjects()[activeIndexPlot];
198 QList<KPlotPoint*> points=p->points();
199 QList<QPointF> newpoints;
201 double newx=inPlot.x()*dataRect().width()/pixRect().width();
202 double newy=(height()-inPlot.y()-bottomPadding()-topPadding() )*dataRect().height()/pixRect().height();
204 foreach (KPlotPoint* pt,points){
205 if (pt->x() >newx && !inserted){
206 newpoints.append(QPointF(newx,newy));
209 newpoints.append(QPointF(pt->x(),pt->y()));
212 foreach (QPointF qf, newpoints ){
215 replacePlotObject(activeIndexPlot,p);
219 }else if (event->button()==Qt::LeftButton){
220 //menu for deleting or exact setup of point
224 void ParameterPlotter::setMoveX(bool b){
228 void ParameterPlotter::setMoveY(bool b){
232 void ParameterPlotter::setMoveTimeLine(bool b){
236 void ParameterPlotter::setNewPoints(bool b){
240 bool ParameterPlotter::isMoveX(){
244 bool ParameterPlotter::isMoveY(){
248 bool ParameterPlotter::isMoveTimeline(){
249 return m_moveTimeline;
252 bool ParameterPlotter::isNewPoints(){
255 void EffectStackView::slotSetMoveX(){
256 ui.kplotwidget->setMoveX(!ui.kplotwidget->isMoveX());
257 updateButtonStatus();
260 void EffectStackView::slotSetMoveY(){
261 ui.kplotwidget->setMoveY(!ui.kplotwidget->isMoveY());
262 updateButtonStatus();
265 void EffectStackView::slotSetNew(){
266 ui.kplotwidget->setNewPoints(!ui.kplotwidget->isNewPoints());
267 updateButtonStatus();
270 void EffectStackView::slotSetHelp(){
271 ui.infoBox->setVisible(!ui.infoBox->isVisible());
272 ui.buttonHelp->setDown(ui.infoBox->isVisible());
275 void EffectStackView::slotShowInTimeline(){
277 ui.kplotwidget->setMoveTimeLine(!ui.kplotwidget->isMoveTimeline());
278 updateButtonStatus();
282 void EffectStackView::updateButtonStatus(){
283 ui.buttonLeftRight->setDown(ui.kplotwidget->isMoveX());
284 ui.buttonUpDown->setDown(ui.kplotwidget->isMoveY());
286 ui.buttonShowInTimeline->setEnabled( ui.kplotwidget->isMoveX() || ui.kplotwidget->isMoveY () );
287 ui.buttonShowInTimeline->setDown(ui.kplotwidget->isMoveTimeline());
289 ui.buttonNewPoints->setEnabled(ui.parameterList->currentText()!="all");
290 ui.buttonNewPoints->setDown(ui.kplotwidget->isNewPoints());
293 void EffectStackView::slotParameterChanged(const QString& text){
295 //ui.buttonNewPoints->setEnabled(text!="all");
296 ui.kplotwidget->replot(text);
297 updateButtonStatus();
299 ui.parameterList->clear();
300 ui.parameterList->addItem("all");
301 QDomNodeList namenode = effects.at(activeRow).elementsByTagName("parameter");
302 for (int i=0;i<namenode.count();i++){
303 QDomNode pa=namenode.item(i);
304 QDomNode na=pa.firstChildElement("name");
305 ui.parameterList->addItem(na.toElement().text() );