src/qvgui/qvhistogramplot.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. 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 
00033 QVHistogramPlot::QVHistogramPlot(const QString name, bool time, int step, double maxim, double minim, QWidget *parent):
00034         QVPlot(name, false, false, false, true, time, step, parent), histItem(), max(maxim), min(minim)
00035         {
00036         histItem.setColor(Qt::darkCyan);
00037         enableAxis(0,true);
00038         enableAxis(2,true);
00039         setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
00040         setAxisTitle(QwtPlot::xBottom, "Index");
00041         }
00042 
00043 void QVHistogramPlot::linkProperty(QVWorker &worker, const QString propertyName)
00044         {
00045         int propType = worker.getPropertyType(propertyName);
00046         QList<double> auxList;
00047         int histType = QVariant::fromValue(auxList).userType();
00048 
00049         if (propType != histType)
00050                 std::cerr << "Warning: a histogramplot only can be linked to a int or double property." << std::endl;
00051         else
00052                 QVPlot::linkProperty(worker, propertyName);
00053         }
00054 
00055 
00056 QStringList QVHistogramPlot::getPropertyCurvNames(QString property) const
00057         {
00058         QStringList names;
00059         QList<double> auxList = getPropertyValue<QList<double> >(property);
00060         for (int i = 0; i < auxList.size(); i++)
00061                 names << QString("%1").arg(i);
00062         return names;
00063         }
00064 
00065 QList<double> QVHistogramPlot::getPropertyCurvValues(QString property) const
00066         {
00067         return getPropertyValue<QList<double> >(property);
00068         }
00069 
00070 QList<int> QVHistogramPlot::getPropertyCurvOrders(QString property) const
00071         {
00072         QList<int> order;
00073         QList<double> auxList = getPropertyValue<QList<double> >(property);
00074         for (int i = 1; i <= auxList.size(); i++)
00075                 order << i;
00076         return order;
00077         }
00078 
00079 void QVHistogramPlot::init()
00080         {
00081         if (initied)
00082                 {
00083                 std::cerr << "Warning: a plot can't be initied more than one time." << std::endl;
00084                 return;
00085                 }
00086 
00087         histItem.attach(this);
00088 
00089         readInputProperties();
00090         for(int i = 0; i < linkCont.size(); i++)
00091                 for(int j = 0; j < linkCont[i].properties.size(); j++)
00092                         {
00093                         const QStringList curvNames = getPropertyCurvNames(linkCont[i].properties[j].name);
00094 
00095                         for(int k = curvNames.size()-1; k >= 0; k--)
00096                                 {
00097                                 QwtPlotCurve * qwtpc;
00098                                 if (byTime) linkCont[i].properties[j].curves.prepend(Curve(curvNames.at(k), qwtpc, 1));
00099                                 else        linkCont[i].properties[j].curves.prepend(Curve(curvNames.at(k), qwtpc, linkCont.size()+1));
00100                                 haveCurves = TRUE;
00101                                 }
00102                         }
00103 
00104         if (byTime) startTimer(nStep * 10); // nStep * 10 ms
00105 
00106         initied = true;
00107         }
00108 
00109 void QVHistogramPlot::insertNewFlags(int cont, int prop)
00110         {
00111         const QStringList curvNames = getPropertyCurvNames(linkCont[cont].properties[prop].name);
00112         if ( (linkCont.size() > cont) && (linkCont[cont].properties.size() > prop) && (curvNames.size() > linkCont[cont].properties[prop].curves.size()) )
00113                 {
00114                 const QList<int> curvOrders = getPropertyCurvOrders(linkCont[cont].properties[prop].name);
00115                 for (int i = 0; i < curvOrders.size(); i++)
00116                         if (curvOrders.at(i) > linkCont[cont].properties[prop].curves.size())
00117                                 {
00118                                 QwtPlotCurve * qwtpc;
00119                                 if (byTime) linkCont[cont].properties[prop].curves.insert(i, Curve(curvNames.at(i), qwtpc, 1));
00120                                 else        linkCont[cont].properties[prop].curves.insert(i, Curve(curvNames.at(i), qwtpc, linkCont.size()+1));
00121                                 haveCurves = TRUE;
00122                                 }
00123                 }
00124         }
00125 
00126 void QVHistogramPlot::advancePlot()
00127         {
00128         if (!haveCurves)
00129                 {
00130                 std::cerr << "QVPlot internal error: early call to advancePlot." << std::endl;
00131                 return;
00132                 }
00133 
00134         // update data
00135         int numValues = 0;
00136         for(int i = 0; i < linkCont.size(); i++)
00137                 for(int j = 0; j < linkCont[i].properties.size(); j++)
00138                         for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00139                                 {
00140                                 updateValue(i, j, k); // and puss the new value
00141                                 numValues++;
00142                                 }
00143 
00144         QwtArray<QwtDoubleInterval> intervals(numValues);
00145         QwtArray<double> values(numValues);
00146 
00147         // curve update
00148         int index = 0;
00149         for(int i = 0; i < linkCont.size(); i++)
00150                 for(int j = 0; j < linkCont[i].properties.size(); j++)
00151                         for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00152                                 {
00153                                 values[index] = linkCont[i].properties[j].curves[k].history[0];
00154                                 intervals[index] = QwtDoubleInterval(double(index)-0.5, double(index+1)-0.5);
00155                                 index++;
00156                                 }
00157 
00158         // scroll bottom axis, scaled like an index
00159         int scaleShow = MAX(index/20, 1);
00160         setAxisScaleDiv(QwtPlot::xBottom, axisScaleEngine(QwtPlot::xBottom)->divideScale(-0.5, double(index)-0.5, 1, 1, scaleShow));
00161 
00162         histItem.setData(QwtIntervalData(intervals, values));
00163 
00164         // adjust left axis (using the visible values's maximum)
00165         if (max == 0)
00166                 {
00167                 for(int i = 0; i < linkCont.size(); i++)
00168                         for(int j = 0; j < linkCont[i].properties.size(); j++)
00169                                 for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00170                                         if (max < linkCont[i].properties[j].curves[k].history[0]) max = linkCont[i].properties[j].curves[k].history[0];
00171                 max = 1.1 *max;
00172                 setAxisScale(QwtPlot::yLeft, min, max);
00173                 max = 0;
00174                 }
00175         else setAxisScale(QwtPlot::yLeft, min, max);
00176 
00177         // and replot
00178         replot();
00179         }
00180 

Generated on Thu Jul 17 17:23:28 2008 for QVision by  doxygen 1.5.3