src/qvgui/qvhistogramplot.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <iostream>
00026 #include <qvgui/qvhistogramplot.h>
00027 #include <qwt_interval_data.h>
00028 #include <qwt_scale_draw.h>
00029 #include <qwt_scale_div.h>
00030 #include <qwt_scale_engine.h>
00031 
00032 QVHistogramPlot::QVHistogramPlot(const QString name, bool time, int step, double maxim, double minim, QWidget *parent):
00033         QVPlot(name, false, false, false, true, time, step, parent), histItem(), max(maxim), min(minim)
00034         {
00035         histItem.setColor(Qt::darkCyan);
00036         enableAxis(0,true);
00037         enableAxis(2,true);
00038         setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
00039         setAxisTitle(QwtPlot::xBottom, "Index");
00040         }
00041 
00042 bool QVHistogramPlot::linkProperty(QVWorker &worker, const QString propertyName)
00043         {
00044         int propType = worker.getPropertyType(propertyName);
00045         QList<double> auxList;
00046         int histType = QVariant::fromValue(auxList).userType();
00047 
00048         if (propType != histType) {
00049                 std::cerr << "Warning: a histogramplot only can be linked to a int or double property." << std::endl;
00050                 return false;
00051         }
00052         else
00053                 return QVPlot::linkProperty(worker, propertyName);
00054         }
00055 
00056 
00057 QStringList QVHistogramPlot::getPropertyCurvNames(QString property) const
00058         {
00059         QStringList names;
00060         QList<double> auxList = getPropertyValue<QList<double> >(property);
00061         for (int i = 0; i < auxList.size(); i++)
00062                 names << QString("%1").arg(i);
00063         return names;
00064         }
00065 
00066 QList<double> QVHistogramPlot::getPropertyCurvValues(QString property) const
00067         {
00068         return getPropertyValue<QList<double> >(property);
00069         }
00070 
00071 QList<int> QVHistogramPlot::getPropertyCurvOrders(QString property) const
00072         {
00073         QList<int> order;
00074         QList<double> auxList = getPropertyValue<QList<double> >(property);
00075         for (int i = 1; i <= auxList.size(); i++)
00076                 order << i;
00077         return order;
00078         }
00079 
00080 void QVHistogramPlot::init()
00081         {
00082         if (initied)
00083                 {
00084                 std::cerr << "Warning: a plot can't be initied more than one time." << std::endl;
00085                 return;
00086                 }
00087 
00088         histItem.attach(this);
00089 
00090         readInputProperties();
00091         for(int i = 0; i < linkCont.size(); i++)
00092                 for(int j = 0; j < linkCont[i].properties.size(); j++)
00093                         {
00094                         const QStringList curvNames = getPropertyCurvNames(linkCont[i].properties[j].name);
00095 
00096                         for(int k = curvNames.size()-1; k >= 0; k--)
00097                                 {
00098                                 QwtPlotCurve * qwtpc;
00099                                 if (byTime) linkCont[i].properties[j].curves.prepend(Curve(curvNames.at(k), qwtpc, 1));
00100                                 else        linkCont[i].properties[j].curves.prepend(Curve(curvNames.at(k), qwtpc, linkCont.size()+1));
00101                                 haveCurves = TRUE;
00102                                 }
00103                         }
00104 
00105         if (byTime) startTimer(nStep * 10); // nStep * 10 ms
00106 
00107         initied = true;
00108         }
00109 
00110 void QVHistogramPlot::insertNewFlags(int cont, int prop)
00111         {
00112         const QStringList curvNames = getPropertyCurvNames(linkCont[cont].properties[prop].name);
00113         if ( (linkCont.size() > cont) && (linkCont[cont].properties.size() > prop) && (curvNames.size() > linkCont[cont].properties[prop].curves.size()) )
00114                 {
00115                 const QList<int> curvOrders = getPropertyCurvOrders(linkCont[cont].properties[prop].name);
00116                 for (int i = 0; i < curvOrders.size(); i++)
00117                         if (curvOrders.at(i) > linkCont[cont].properties[prop].curves.size())
00118                                 {
00119                                 QwtPlotCurve * qwtpc;
00120                                 if (byTime) linkCont[cont].properties[prop].curves.insert(i, Curve(curvNames.at(i), qwtpc, 1));
00121                                 else        linkCont[cont].properties[prop].curves.insert(i, Curve(curvNames.at(i), qwtpc, linkCont.size()+1));
00122                                 haveCurves = TRUE;
00123                                 }
00124                 }
00125         }
00126 
00127 void QVHistogramPlot::advancePlot()
00128         {
00129         if (!haveCurves)
00130                 {
00131                 std::cerr << "QVPlot internal error: early call to advancePlot." << std::endl;
00132                 return;
00133                 }
00134 
00135         // update data
00136         int numValues = 0;
00137         for(int i = 0; i < linkCont.size(); i++)
00138                 for(int j = 0; j < linkCont[i].properties.size(); j++)
00139                         for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00140                                 {
00141                                 updateValue(i, j, k); // and puss the new value
00142                                 numValues++;
00143                                 }
00144 
00145         QwtArray<QwtDoubleInterval> intervals(numValues);
00146         QwtArray<double> values(numValues);
00147 
00148         // curve update
00149         int index = 0;
00150         for(int i = 0; i < linkCont.size(); i++)
00151                 for(int j = 0; j < linkCont[i].properties.size(); j++)
00152                         for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00153                                 {
00154                                 values[index] = linkCont[i].properties[j].curves[k].history[0];
00155                                 intervals[index] = QwtDoubleInterval(double(index)-0.5, double(index+1)-0.5);
00156                                 index++;
00157                                 }
00158 
00159         // scroll bottom axis, scaled like an index
00160         int scaleShow = MAX(index/20, 1);
00161         setAxisScaleDiv(QwtPlot::xBottom, axisScaleEngine(QwtPlot::xBottom)->divideScale(-0.5, double(index)-0.5, 1, 1, scaleShow));
00162 
00163         histItem.setData(QwtIntervalData(intervals, values));
00164 
00165         // adjust left axis (using the visible values's maximum)
00166         if (max == 0)
00167                 {
00168                 for(int i = 0; i < linkCont.size(); i++)
00169                         for(int j = 0; j < linkCont[i].properties.size(); j++)
00170                                 for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00171                                         if (max < linkCont[i].properties[j].curves[k].history[0]) max = linkCont[i].properties[j].curves[k].history[0];
00172                 max = 1.1 *max;
00173                 setAxisScale(QwtPlot::yLeft, min, max);
00174                 max = 0;
00175                 }
00176         else setAxisScale(QwtPlot::yLeft, min, max);
00177 
00178         // and replot
00179         replot();
00180         }
00181