]> git.sesse.net Git - kdenlive/blob - src/parameterplotter.cpp
Reindent all source files
[kdenlive] / src / parameterplotter.cpp
1 /***************************************************************************
2                           parameterplotter.cpp  -  description
3                              -------------------
4     begin                : Feb 15 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "parameterplotter.h"
19 #include <QVariant>
20 #include <KPlotObject>
21 #include <QMouseEvent>
22 #include <QPen>
23 #include <KDebug>
24 #include <KPlotPoint>
25
26 ParameterPlotter::ParameterPlotter(QWidget *parent): KPlotWidget(parent) {
27     setAntialiasing(true);
28     setLeftPadding(20);
29     setRightPadding(10);
30     setTopPadding(10);
31     setBottomPadding(20);
32     movepoint = NULL;
33     colors << Qt::white << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::gray << Qt::cyan;
34     maxy = 0;
35     m_moveX = false;
36     m_moveY = true;
37     m_moveTimeline = true;
38     m_newPoints = false;
39     activeIndexPlot = -1;
40 }
41 /*
42     <name>Lines</name>
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" >
47       <name>Num</name>
48     </parameter>
49     <parameter default="4" type="constant" value="4" min="0" name="width" max="255" >
50       <name>Width</name>
51     </parameter>
52   </effect>
53
54 */
55 void ParameterPlotter::setPointLists(const QDomElement& d, int startframe, int endframe) {
56
57     //QListIterator <QPair <QString, QMap< int , QVariant > > > nameit(params);
58     itemParameter = d;
59     QDomNodeList namenode = d.elementsByTagName("parameter");
60
61     max_y = 0;
62     min_y = 0;
63     removeAllPlotObjects();
64     stretchFactors.clear();
65     parameterNameList.clear();
66     plotobjects.clear();
67
68     QString dat;
69     QTextStream stre(&dat);
70     d.save(stre, 2);
71     kDebug() << dat;
72
73     if (namenode.count()) {
74
75
76         QDomNode pa = namenode.item(0);
77         QDomNode na = pa.firstChildElement("name");
78
79         parameterNameList << pa.attributes().namedItem("name").nodeValue().split(";");
80         emit parameterList(parameterNameList);
81
82         //max_y=pa.attributes().namedItem("max").nodeValue().toInt();
83         //int val=pa.attributes().namedItem("value").nodeValue().toInt();
84         QStringList defaults;
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();
96         }
97
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();
104                 else
105                     stretchFactors[i] = 1.0;
106             }
107             if (defaults[i].toDouble() > max_y)
108                 defaults[i] = max_y;
109             plot->addPoint(startframe, defaults[i].toInt()*stretchFactors[i], parameterNameList[i]);
110             //add keyframes here
111             plot->addPoint(endframe, defaults[i].toInt()*stretchFactors[i]);
112
113             plotobjects.append(plot);
114         }
115
116         /*TODO keyframes
117         while (pointit.hasNext()){
118          pointit.next();
119          plot->addPoint(QPointF(pointit.key(),pointit.value().toDouble()),item.first,1);
120          if (pointit.value().toInt() >maxy)
121           max_y=pointit.value().toInt();
122         }*/
123
124     }
125     maxx = endframe;
126     maxy = max_y;
127     setLimits(-1, endframe + 1, min_y - 10, maxy + 10);
128     addPlotObjects(plotobjects);
129
130 }
131
132 void ParameterPlotter::createParametersNew() {
133
134     QList<KPlotObject*> plotobjs = plotObjects();
135     if (plotobjs.size() != parameterNameList.size()) {
136         kDebug() << "ERROR size not equal";
137     }
138     QDomNodeList namenode = itemParameter.elementsByTagName("parameter");
139     QString paramlist;
140     QTextStream txtstr(&paramlist);
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
148             }
149             if (i + 1 != plotobjs.count())
150                 txtstr << ";";
151         }
152     }
153     pa.attributes().namedItem("value").setNodeValue(paramlist);
154     pa.attributes().namedItem("start").setNodeValue(paramlist);
155     emit parameterChanged(itemParameter);
156
157 }
158
159
160 void ParameterPlotter::mouseMoveEvent(QMouseEvent * event) {
161
162     if (movepoint != NULL) {
163         QList<KPlotPoint*> list =   pointsUnderPoint(event->pos() - QPoint(leftPadding(), topPadding())) ;
164         int i = 0, j = -1;
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());
177                     }
178                     if (m_moveTimeline && (m_moveX || m_moveY))
179                         emit updateFrame(0);
180                     replacePlotObject(i, o);
181                     oldmousepoint = event->pos();
182                 }
183             }
184             i++;
185         }
186         createParametersNew();
187     }
188 }
189
190 void ParameterPlotter::replot(const QString & name) {
191     //removeAllPlotObjects();
192     int i = 0;
193     bool drawAll = name.isEmpty() || name == "all";
194     activeIndexPlot = -1;
195
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());
201         p->setLabelPen(pen);
202         if (parameterNameList[i] == name)
203             activeIndexPlot = i;
204         replacePlotObject(i++, p);
205     }
206 }
207
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) {
214             movepoint = list[0];
215             oldmousepoint = event->pos();
216         } else {
217             if (m_newPoints && activeIndexPlot >= 0) {
218                 //setup new points
219                 KPlotObject* p = plotObjects()[activeIndexPlot];
220                 QList<KPlotPoint*> points = p->points();
221                 QList<QPointF> newpoints;
222
223                 double newx = inPlot.x() * dataRect().width() / pixRect().width();
224                 double newy = (height() - inPlot.y() - bottomPadding() - topPadding()) * dataRect().height() / pixRect().height();
225                 bool inserted = false;
226                 foreach(KPlotPoint* pt, points) {
227                     if (pt->x() > newx && !inserted) {
228                         newpoints.append(QPointF(newx, newy));
229                         inserted = true;
230                     }
231                     newpoints.append(QPointF(pt->x(), pt->y()));
232                 }
233                 p->clearPoints();
234                 foreach(QPointF qf, newpoints) {
235                     p->addPoint(qf);
236                 }
237                 replacePlotObject(activeIndexPlot, p);
238             }
239             movepoint = NULL;
240         }
241     } else if (event->button() == Qt::LeftButton) {
242         //menu for deleting or exact setup of point
243     }
244 }
245
246 void ParameterPlotter::setMoveX(bool b) {
247     m_moveX = b;
248 }
249
250 void ParameterPlotter::setMoveY(bool b) {
251     m_moveY = b;
252 }
253
254 void ParameterPlotter::setMoveTimeLine(bool b) {
255     m_moveTimeline = b;
256 }
257
258 void ParameterPlotter::setNewPoints(bool b) {
259     m_newPoints = b;
260 }
261
262 bool ParameterPlotter::isMoveX() {
263     return m_moveX;
264 }
265
266 bool ParameterPlotter::isMoveY() {
267     return m_moveY;
268 }
269
270 bool ParameterPlotter::isMoveTimeline() {
271     return m_moveTimeline;
272 }
273
274 bool ParameterPlotter::isNewPoints() {
275     return m_newPoints;
276 }