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) :
29 setAntialiasing(true);
35 m_colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
38 m_moveTimeline = true;
40 m_activeIndexPlot = -1;
46 <description>Lines from top to bottom</description>
47 <author>Marco Gittler</author>
48 <properties tag="lines" id="lines" />
49 <parameter default="5" type="constant" value="5" min="0" name="num" max="255" >
52 <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
58 void ParameterPlotter::setPointLists(const QDomElement& d, const QString& paramName, int startframe, int endframe)
61 //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
62 m_paramName = paramName;
64 QDomNodeList namenode = d.elementsByTagName("parameter");
68 removeAllPlotObjects();
69 m_stretchFactors.clear();
70 m_parameterNameList.clear();
71 m_plotobjects.clear();
74 QTextStream stre(&dat);
78 while (!namenode.item(i).isNull() && namenode.item(i).toElement().attribute("name") != m_paramName)
80 if (namenode.count()) {
83 QDomElement pa = namenode.item(i).toElement();
84 QDomNode na = pa.firstChildElement("name");
86 m_parameterNameList << pa.attribute("namedesc").split(';');
87 emit parameterList(m_parameterNameList);
89 //max_y=pa.attributes().namedItem("max").nodeValue().toInt();
90 //int val=pa.attributes().namedItem("value").nodeValue().toInt();
92 if (pa.attribute("start").contains(';'))
93 defaults = pa.attribute("start").split(';');
94 else if (pa.attribute("value").contains(';'))
95 defaults = pa.attribute("value").split(';');
96 else if (pa.attribute("default").contains(';'))
97 defaults = pa.attribute("default").split(';');
98 QStringList maxv = pa.attribute("max").split(';');
99 QStringList minv = pa.attribute("min").split(';');
100 for (int i = 0; i < maxv.size() && i < minv.size(); i++) {
101 if (m_max_y < maxv[i].toInt()) m_max_y = maxv[i].toInt();
102 if (m_min_y > minv[i].toInt()) m_min_y = minv[i].toInt();
104 for (int i = 0; i < m_parameterNameList.count(); i++) {
105 KPlotObject *plot = new KPlotObject(m_colors[m_plotobjects.size()%m_colors.size()]);
106 plot->setShowLines(true);
107 if (!m_stretchFactors.contains(i) && i < maxv.size()) {
108 if (maxv[i].toInt() != 0)
109 m_stretchFactors[i] = m_max_y / maxv[i].toInt();
111 m_stretchFactors[i] = 1.0;
113 if (i < defaults.size() && defaults[i].toDouble() > m_max_y)
114 defaults[i] = m_max_y;
116 if (i < defaults.size())
117 def = (int)(defaults[i].toInt() * m_stretchFactors[i]);
119 if (i < m_parameterNameList.size())
120 name = m_parameterNameList[i];
121 plot->addPoint(startframe, def, name);
123 plot->addPoint(endframe, def);
125 m_plotobjects.append(plot);
129 while (pointit.hasNext()){
131 plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
132 if (pointit.value().toInt() >maxy)
133 max_y=pointit.value().toInt();
137 setLimits(-1, endframe + 1, m_min_y - 10, m_max_y + 10);
139 addPlotObjects(m_plotobjects);
143 void ParameterPlotter::createParametersNew()
145 QList<KPlotObject*> plotobjs = plotObjects();
146 if (plotobjs.size() != m_parameterNameList.size()) {
147 kDebug() << "ERROR size not equal";
149 QDomNodeList namenode = m_itemParameter.elementsByTagName("parameter");
151 QTextStream txtstr(¶mlist);
152 QDomNode pa = namenode.item(0);
153 if (namenode.count() > 0) {
154 for (int i = 0; i < plotobjs.count(); i++) {
155 QList<KPlotPoint*> points = plotobjs[i]->points();
156 foreach(const KPlotPoint *o, points) {
157 txtstr << (int)o->y() ;
158 break;//first no keyframes
160 if (i + 1 != plotobjs.count())
164 pa.attributes().namedItem("value").setNodeValue(paramlist);
165 pa.attributes().namedItem("start").setNodeValue(paramlist);
166 emit parameterChanged(m_itemParameter);
171 void ParameterPlotter::mouseMoveEvent(QMouseEvent * event)
174 if (m_movepoint != NULL) {
175 QList<KPlotPoint*> list = pointsUnderPoint(event->pos() - QPoint(leftPadding(), topPadding())) ;
177 foreach(KPlotObject *o, plotObjects()) {
178 QList<KPlotPoint*> points = o->points();
179 for (int p = 0; p < points.size(); p++) {
180 if (points[p] == m_movepoint && (m_activeIndexPlot == -1 || m_activeIndexPlot == i)) {
181 QPoint delta = event->pos() - m_oldmousepoint;
182 double newy = m_movepoint->y() - delta.y() * dataRect().height() / pixRect().height();
183 if (m_moveY && newy > m_min_y && newy < m_max_y)
184 m_movepoint->setY(newy);
185 if (p > 0 && p < points.size() - 1) {
186 double newx = m_movepoint->x() + delta.x() * dataRect().width() / pixRect().width();
187 if (newx > points[p-1]->x() && newx < points[p+1]->x() && m_moveX)
188 m_movepoint->setX(m_movepoint->x() + delta.x()*dataRect().width() / pixRect().width());
190 if (m_moveTimeline && (m_moveX || m_moveY))
192 replacePlotObject(i, o);
193 m_oldmousepoint = event->pos();
198 createParametersNew();
202 void ParameterPlotter::replot(const QString & name)
205 //removeAllPlotObjects();
207 bool drawAll = name.isEmpty() || name == "all";
208 m_activeIndexPlot = -1;
211 foreach(KPlotObject* p, plotObjects()) {
212 QString selectedName = "none";
213 if (i < m_parameterNameList.size())
214 selectedName = m_parameterNameList[i];
215 p->setShowPoints(drawAll || selectedName == name);
216 p->setShowLines(drawAll || selectedName == name);
217 QPen pen = (drawAll || selectedName == name ? QPen(Qt::SolidLine) : QPen(Qt::NoPen));
218 pen.setColor(p->linePen().color());
220 if (selectedName == name)
221 m_activeIndexPlot = i;
222 replacePlotObject(i++, p);
226 void ParameterPlotter::mousePressEvent(QMouseEvent * event)
228 //topPadding and other padding can be wrong and this (i hope) will be correctet in newer kde versions
229 QPoint inPlot = event->pos() - QPoint(leftPadding(), topPadding());
230 QList<KPlotPoint*> list = pointsUnderPoint(inPlot) ;
231 if (event->button() == Qt::LeftButton) {
232 if (list.size() > 0) {
233 m_movepoint = list[0];
234 m_oldmousepoint = event->pos();
236 if (m_newPoints && m_activeIndexPlot >= 0) {
238 KPlotObject* p = plotObjects()[m_activeIndexPlot];
239 QList<KPlotPoint*> points = p->points();
240 QList<QPointF> newpoints;
242 double newx = inPlot.x() * dataRect().width() / pixRect().width();
243 double newy = (height() - inPlot.y() - bottomPadding() - topPadding()) * dataRect().height() / pixRect().height();
244 bool inserted = false;
245 foreach(const KPlotPoint* pt, points) {
246 if (pt->x() > newx && !inserted) {
247 newpoints.append(QPointF(newx, newy));
250 newpoints.append(QPointF(pt->x(), pt->y()));
253 foreach(const QPointF &qf, newpoints) {
256 replacePlotObject(m_activeIndexPlot, p);
260 } else if (event->button() == Qt::LeftButton) {
261 //menu for deleting or exact setup of point
265 void ParameterPlotter::setMoveX(bool b)
270 void ParameterPlotter::setMoveY(bool b)
275 void ParameterPlotter::setMoveTimeLine(bool b)
280 void ParameterPlotter::setNewPoints(bool b)
285 bool ParameterPlotter::isMoveX()
290 bool ParameterPlotter::isMoveY()
295 bool ParameterPlotter::isMoveTimeline()
297 return m_moveTimeline;
300 bool ParameterPlotter::isNewPoints()