00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 #include <QResizeEvent>
00032
00035 QVHistogramPlot::QVHistogramPlot(const QString name, bool time, int step, double maxim, double minim, QWidget *parent):
00036 QVPlot(name, false, false, false, true, time, step, parent), histItem(), max(maxim), min(minim), maxAxisNumbers(10)
00037 {
00038 histItem.setColor(Qt::darkCyan);
00039 enableAxis(0,true);
00040 enableAxis(2,true);
00041 setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
00042 setAxisTitle(QwtPlot::xBottom, "Index");
00043 }
00044
00045 bool QVHistogramPlot::linkProperty(QVWorker &worker, const QString propertyName)
00046 {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 if ( worker.isType< QList<double> >(propertyName) || worker.isType< QVector<double> >(propertyName) )
00059 return QVPlot::linkProperty(worker, propertyName);
00060 else
00061 {
00062 std::cerr << "Warning: a histogramplot can only be linked to a QList<double> or QVector<double> property." << std::endl;
00063 return false;
00064 }
00065 }
00066
00067
00068 QStringList QVHistogramPlot::getPropertyCurvNames(QString propertyName) const
00069 {
00070
00071
00072
00073
00074
00075 QStringList names;
00076
00077 const int auxListSize = isType< QList<double> >(propertyName)? getPropertyValue< QList<double> >(propertyName).size():
00078 isType< QVector<double> >(propertyName)? getPropertyValue< QVector<double> >(propertyName).size(): -1;
00079
00080 for (int i = 0; i < auxListSize; i++)
00081 names << QString::number(i);
00082
00083 return names;
00084 }
00085
00086 QList<double> QVHistogramPlot::getPropertyCurvValues(QString propertyName) const
00087 {
00088 if (isType< QList<double> >(propertyName))
00089 return getPropertyValue< QList<double> >(propertyName);
00090 else if (isType< QVector<double> >(propertyName))
00091 return getPropertyValue< QVector<double> >(propertyName).toList();
00092 else
00093 std::cout << "ERROR 34: Love unknown error codes?" << std::endl;
00094
00095 return QList<double>();
00096 }
00097
00098 QList<int> QVHistogramPlot::getPropertyCurvOrders(QString propertyName) const
00099 {
00100 QList<int> order;
00101
00102
00103 const int auxListSize = isType< QList<double> >(propertyName)? getPropertyValue< QList<double> >(propertyName).size():
00104 isType< QVector<double> >(propertyName)? getPropertyValue< QVector<double> >(propertyName).size(): -1;
00105
00106 for (int i = 1; i <= auxListSize; i++)
00107
00108 order << i;
00109 return order;
00110 }
00111
00112 void QVHistogramPlot::init()
00113 {
00114 if (initied)
00115 {
00116 std::cerr << "Warning: a plot can't be initied more than one time." << std::endl;
00117 return;
00118 }
00119
00120 histItem.attach(this);
00121
00122 readInputProperties();
00123 for(int i = 0; i < linkCont.size(); i++)
00124 for(int j = 0; j < linkCont[i].properties.size(); j++)
00125 {
00126 const QStringList curvNames = getPropertyCurvNames(linkCont[i].properties[j].name);
00127
00128 for(int k = curvNames.size()-1; k >= 0; k--)
00129 {
00130 QwtPlotCurve * qwtpc;
00131 if (byTime) linkCont[i].properties[j].curves.prepend(Curve(curvNames.at(k), qwtpc, 1));
00132 else linkCont[i].properties[j].curves.prepend(Curve(curvNames.at(k), qwtpc, linkCont.size()+1));
00133 haveCurves = TRUE;
00134 }
00135 }
00136
00137 if (byTime) timer = startTimer(nStep * 10);
00138
00139 initied = true;
00140 }
00141
00142 void QVHistogramPlot::insertNewFlags(int cont, int prop)
00143 {
00144 const QStringList curvNames = getPropertyCurvNames(linkCont[cont].properties[prop].name);
00145 if ( (linkCont.size() > cont) && (linkCont[cont].properties.size() > prop) && (curvNames.size() > linkCont[cont].properties[prop].curves.size()) )
00146 {
00147 const QList<int> curvOrders = getPropertyCurvOrders(linkCont[cont].properties[prop].name);
00148 for (int i = 0; i < curvOrders.size(); i++)
00149 if (curvOrders.at(i) > linkCont[cont].properties[prop].curves.size())
00150 {
00151 QwtPlotCurve * qwtpc;
00152 if (byTime) linkCont[cont].properties[prop].curves.insert(i, Curve(curvNames.at(i), qwtpc, 1));
00153 else linkCont[cont].properties[prop].curves.insert(i, Curve(curvNames.at(i), qwtpc, linkCont.size()+1));
00154 haveCurves = TRUE;
00155 }
00156 }
00157 }
00158
00159 void QVHistogramPlot::advancePlot()
00160 {
00161 if (!haveCurves)
00162 {
00163 std::cerr << "QVPlot internal error: early call to advancePlot." << std::endl;
00164 return;
00165 }
00166
00167
00168 int numValues = 0;
00169 for(int i = 0; i < linkCont.size(); i++)
00170 for(int j = 0; j < linkCont[i].properties.size(); j++)
00171 for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00172 {
00173
00174 if (byTime) linkCont[i].properties[j].curves[k].history[0] = linkCont[i].properties[j].curves[k].temp[0];
00175 else linkCont[i].properties[j].curves[k].history[0] = linkCont[i].properties[j].curves[k].temp[(iterationIndex%(linkCont.size()+1))];
00176 numValues++;
00177 }
00178
00179 QwtArray<QwtDoubleInterval> intervals(numValues);
00180 QwtArray<double> values(numValues);
00181
00182
00183 int index = 0;
00184 for(int i = 0; i < linkCont.size(); i++)
00185 for(int j = 0; j < linkCont[i].properties.size(); j++)
00186 for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00187 {
00188 values[index] = linkCont[i].properties[j].curves[k].history[0];
00189 intervals[index] = QwtDoubleInterval(double(index)-0.5, double(index+1)-0.5);
00190 index++;
00191 }
00192
00193
00194 int scaleShow = MAX(index/maxAxisNumbers, 1);
00195 setAxisScaleDiv(QwtPlot::xBottom, axisScaleEngine(QwtPlot::xBottom)->divideScale(-0.5, double(index)-0.5, 1, 1, scaleShow));
00196
00197 histItem.setData(QwtIntervalData(intervals, values));
00198
00199
00200 if (max == 0)
00201 {
00202 for(int i = 0; i < linkCont.size(); i++)
00203 for(int j = 0; j < linkCont[i].properties.size(); j++)
00204 for(int k = 0; k < linkCont[i].properties[j].curves.size(); k++)
00205 if (max < linkCont[i].properties[j].curves[k].history[0]) max = linkCont[i].properties[j].curves[k].history[0];
00206 max = 1.1 *max;
00207 setAxisScale(QwtPlot::yLeft, min, max);
00208 max = 0;
00209 }
00210 else setAxisScale(QwtPlot::yLeft, min, max);
00211
00212
00213 replot();
00214 }
00215
00216
00217
00218 void QVHistogramPlot::resizeEvent(QResizeEvent * e)
00219 {
00220 QVPlot::resizeEvent(e);
00221 maxAxisNumbers = e->size().width() / 50;
00222 }
00223