00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "statplotgraph.h"
00019
00020 #include <qpixmap.h>
00021 #include <qpainter.h>
00022
00023 #ifdef KDE_APP
00024 #include <kprinter.h>
00025 #else
00026 #include <qprinter.h>
00027 #endif //KDE_APP
00028
00029 #include "plotmodule.h"
00030 #include "statinfo.h"
00031 #include "fungmath.h"
00032
00033 StatPlotGraph::StatPlotGraph(QWidget *parent, const char *name ) : BasicGraph(parent,name)
00034 {
00035 plotIndex = 0;
00036 doTrace = false;
00037 }
00038
00039 StatPlotGraph::~StatPlotGraph()
00040 {}
00041
00042 QColor StatPlotGraph::getColor( unsigned int i ) const
00043 {
00044 return (plots.size() >= i+1) ? plots[i]->color() : QColor();
00045 }
00046
00047 void StatPlotGraph::setShowCurrentPlot(bool show)
00048 {
00049 if (plots.size() != 0)
00050 {
00051 (plots[plotIndex])->setVisible(show);
00052 repaint(false);
00053 }
00054 }
00055
00056 void StatPlotGraph::nextPlot()
00057 {
00058 if (plotIndex + 1 == plots.size())
00059 plotIndex = 0;
00060 else
00061 plotIndex++;
00062 repaint(false);
00063 }
00064
00065 void StatPlotGraph::prevPlot()
00066 {
00067 if (plotIndex == 0)
00068 plotIndex = plots.size() - 1;
00069 else
00070 plotIndex--;
00071 repaint(false);
00072 }
00073
00074 void StatPlotGraph::paintEvent(QPaintEvent *)
00075 {
00076 buffer->fill(backgroundColor());
00077 p->begin(buffer);
00078 paint(p);
00079 drawPlots(p);
00080 p->end();
00081 bitBlt(this,0,0,buffer);
00082 }
00083
00084 void StatPlotGraph::drawPlots(QPainter * painter)
00085 {
00086 QFontMetrics metrics(painter->font());
00087 painter->setFont(QFont("times",12,QFont::Black));
00088
00089 int baw_plots = 0;
00090 unsigned int visible_plots = 0;
00091 for ( unsigned int i=0; i<plots.size(); i++)
00092 {
00093 PlotModule *plot = plots[i];
00094 if (plot->show() == false){continue;}
00095
00096 painter->setPen(plot->color());
00097 plot->drawPlot(painter,&baw_plots,this,xScale,yScale);
00098
00099 painter->setPen(plot->color());
00100 painter->drawText(30,35+(metrics.height()+1)*visible_plots,QString(tr("Plot %1")).arg(i+1));
00101
00102 visible_plots++;
00103
00104 if ( i == plotIndex && isTracing() )
00105 painter->fillRect(toPixelXCoord(getMouseX()) - 6 / 2, toPixelYCoord(getMouseY()) - 6 / 2, 6, 6, QBrush(QColor(255,255,255),Qt::SolidPattern));
00106 }
00107 }
00108
00109 void StatPlotGraph::addPlot(unsigned int index, PlotModule * plot)
00110 {
00111 if ( plots.size() < index + 1 )
00112 plots.resize(plots.size()+1);
00113 plots[index] = plot;
00114 repaint(false);
00115 }
00116
00117 void StatPlotGraph::removePlot(unsigned int index)
00118 {
00119 qDebug(QString::number(index));
00120 if (plots.size() >= index + 1)
00121 plots.erase(plots.begin()+index);
00122
00123 repaint(false);
00124 }
00125
00126 void StatPlotGraph::mouseMoveEvent(QMouseEvent *m)
00127 {
00128 if ( isTracing() && ( plots.size() >= plotIndex + 1))
00129 {
00130 double mousex, mousey;
00131 plots[plotIndex]->trace( toGraphXCoord(m->x()), toGraphYCoord(m->y()), &mousex, &mousey );
00132
00133 setMouseX(mousex,false);
00134 setMouseY(mousey,false);
00135
00136 updateCoords();
00137 repaint(false);
00138 }
00139 else
00140 BasicGraph::mouseMoveEvent(m);
00141
00142 }
00143
00144 void StatPlotGraph::updateCoords()
00145 {
00146 if ( isTracing() )
00147 {
00148 std::string message = plots[plotIndex]->updateCoords( getMouseX(), getMouseY() );
00149 emit infoMessageReady(QString(message.c_str()));
00150
00151 }
00152 else
00153 BasicGraph::updateCoords();
00154 }
00155
00156 void StatPlotGraph::zoomStat()
00157 {
00158 if ( plotIndex >= plots.size() ){qDebug("No plot entered"); return;}
00159
00160 double xmin = xMin;
00161 double xmax = xMax;
00162 double ymin = yMin;
00163 double ymax = yMax;
00164
00165 plots[plotIndex]->zoomStat( &xmin, &xmax, &ymin, &ymax );
00166
00167 setRange(xmin,xmax,ymin,ymax);
00168
00169 updateGraph();
00170 emitDimensionsChanged();
00171 repaint(false);
00172 }
00173
00174
00175 void StatPlotGraph::print(
00176 #ifdef KDE_APP
00177 KPrinter *printer
00178 #else
00179 QPrinter *printer
00180 #endif
00181 )
00182 {
00183 if ( printer->setup(this) )
00184 {
00185 qDebug( "Printing...");
00186 QPainter p;
00187 if ( !p.begin( printer ) )
00188 return;
00189 p.setClipRect(0,0,width(),height());
00190 p.fillRect(0,0,width(),height(),backgroundColor());
00191 paint(&p);
00192 drawPlots(&p);
00193 p.end();
00194 qDebug( "Printing completed");
00195 }
00196 else
00197 qDebug( "Printing aborted" );
00198 }
00199
00200 void StatPlotGraph::exportAsImage(const QString &file, const QString &fileType)
00201 {
00202 QPixmap pm(width(),height());
00203 getPixmap(pm);
00204
00205 pm.save(file,fileType);
00206 }
00207
00208 void StatPlotGraph::getPixmap(QPixmap &pm)
00209 {
00210 pm.fill(backgroundColor());
00211 QPainter painter;
00212 painter.begin(&pm);
00213 paint(&painter);
00214 drawPlots(&painter);
00215 painter.end();
00216 }
00217