00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mathfunction.h"
00019
00020 #include <qapplication.h>
00021 #include <qpoint.h>
00022 #include <qpainter.h>
00023
00024 #include "expressiongraph.h"
00025 #include "fungparser.h"
00026
00027 MathFunction::MathFunction(BasicGraph *graph, int expressions) : expression_count(expressions), expression(0), expression2(0)
00028 {
00029 mouseClicks = 0;
00030 resultString = QString();
00031 _active = false;
00032
00033 basicgraph = graph;
00034
00035 upperBounds = QPoint();
00036 lowerBounds = QPoint();
00037 }
00038
00039 MathFunction::~MathFunction()
00040 {
00041 }
00042
00043 void MathFunction::setResult( QString & s )
00044 {
00045 resultString = s;
00046 }
00047
00048 int MathFunction::toPixelXCoord(double x_coordinate)
00049 {
00050 return basicgraph->toPixelXCoord(x_coordinate);
00051 }
00052
00053 int MathFunction::toPixelYCoord(double y_coordinate)
00054 {
00055 return basicgraph->toPixelYCoord(y_coordinate);
00056 }
00057
00058 double MathFunction::toGraphXCoord(double x_coordinate)
00059 {
00060 return basicgraph->toGraphXCoord(x_coordinate);
00061 }
00062
00063 double MathFunction::toGraphYCoord(double y_coordinate)
00064 {
00065 return basicgraph->toGraphYCoord(y_coordinate);
00066 }
00067
00068 double MathFunction::animatorValue()
00069 {
00070 return static_cast<ExpressionGraph*>(basicgraph)->animatorValue();
00071 }
00072
00073
00074 void MathFunction::getTwoPoints()
00075 {
00076 qApp->processOneEvent();
00077 if (mouseClicks == CANCEL){return;}
00078 if (mouseClicks == GETLOWERBOUND){getTwoPoints();return;}
00079 if (mouseClicks == GOTLOWERBOUND)
00080 {
00081 lowerBounds.setX( lastClick.x());
00082 lowerBounds.setY( lastClick.y());
00083 arg1 = beta;
00084
00085 arg1x = beta;
00086 arg2y = beta2;
00087
00088 basicgraph->repaint(false);
00089 mouseClicks++;
00090 }
00091 if (mouseClicks == GETUPPERBOUND){getTwoPoints();return;}
00092 if (mouseClicks == GOTUPPERBOUND)
00093 {
00094 upperBounds.setX(lastClick.x());
00095 upperBounds.setY(lastClick.y());
00096 arg2 = beta;
00097
00098 arg2x = beta;
00099 arg2y = beta2;
00100
00101
00102 if (expression){basicgraph->setTrace(false);}
00103 basicgraph->repaint(false); mouseClicks++;
00104 }
00105 if (mouseClicks == CALCULATE){basicgraph->repaint(false); mouseClicks++;}
00106 if (mouseClicks == CLICKTOCONTINUE){getTwoPoints(); return;}
00107
00108 basicgraph->repaint(false);
00109 }
00110
00111 void MathFunction::sendClick(QMouseEvent *, double arg, double arg2)
00112 {
00113 lastClick = basicgraph->getMousePoint();
00114
00115 beta = arg;
00116 beta2 = arg2;
00117 mouseClicks++;
00118 }
00119
00120 void MathFunction::drawFunction(QPainter *painter)
00121 {
00122 painter->setPen(QPen(QColor(255,0,0),2));
00123
00124 if (mouseClicks == 0){painter->drawText(30,basicgraph->height()-20,QObject::tr("Select Lower Bounds"));}
00125 if (mouseClicks == 1 || mouseClicks == 2){drawLeftArrow(painter); painter->drawText(30,basicgraph->height()-20,QObject::tr("Select Upper Bounds"));}
00126 if (mouseClicks == 4)
00127 {
00128 calculate_and_draw(painter);
00129
00130 drawLeftArrow(painter);
00131 drawRightArrow(painter);
00132
00133 painter->drawText(LEFTOFFSET,basicgraph->height()-24,resultString);
00134 painter->drawText(LEFTOFFSET,basicgraph->height()-10,QObject::tr("Click to continue..."));
00135 }
00136 if (mouseClicks == 5)
00137 {
00138 draw_stored(painter);
00139
00140 drawLeftArrow(painter);
00141 drawRightArrow(painter);
00142
00143 painter->drawText(LEFTOFFSET,basicgraph->height()-24,resultString);
00144 painter->drawText(LEFTOFFSET,basicgraph->height()-10,QObject::tr("Click to continue..."));
00145 }
00146 }
00147
00148 void MathFunction::exec(FungParser *fp, FungParser *fp2)
00149 {
00150 bool tmpTrace = false;
00151 if (fp)
00152 {
00153 expression = fp;
00154 tmpTrace = basicgraph->isTracing();
00155 basicgraph->setTrace(true);
00156 }
00157
00158 if (fp2)
00159 {
00160 qDebug("is fp2");
00161 expression2 = fp2;
00162 }
00163
00164 _active = true;
00165 mouseClicks = GETLOWERBOUND;
00166 basicgraph->repaint(false);
00167 getTwoPoints();
00168
00169 if (fp)
00170 basicgraph->setTrace(tmpTrace);
00171
00172 _active = false;
00173
00174 expression = 0;
00175 expression2= 0;
00176 }
00177
00178 void MathFunction::cancel()
00179 {
00180 mouseClicks = CANCEL;
00181 }
00182
00183 void MathFunction::drawSelectedPoint(double x, double y, QPainter *painter)
00184 {
00185 painter->save();
00186 painter->setPen(QPen(QColor(100,200,0),1,Qt::DotLine));
00187 painter->drawLine(toPixelXCoord(x),0,toPixelXCoord(x),basicgraph->height());
00188 painter->drawLine(0,toPixelYCoord(y),basicgraph->width(),toPixelYCoord(y));
00189 painter->fillRect(toPixelXCoord(x)-3,toPixelYCoord(y)-3,6,6,QBrush(QColor(0,100,255),Qt::SolidPattern));
00190 painter->restore();
00191 }
00192
00193 void MathFunction::drawLeftArrow(QPainter *painter)
00194 {
00195 int x = lowerBounds.x();
00196 int y = lowerBounds.y();
00197
00198 QPointArray points(3);
00199 points.setPoint(0,x,y);
00200 points.setPoint(1,x-BOUNDS_TRIANGLE*2,y-BOUNDS_TRIANGLE);
00201 points.setPoint(2,x-BOUNDS_TRIANGLE*2,y+BOUNDS_TRIANGLE);
00202
00203 painter->save();
00204 painter->setPen(QPen(QColor(0,100,255),2));
00205 painter->setBrush(QBrush(QColor(0,100,255),Qt::SolidPattern));
00206 painter->drawPolygon(points);
00207 painter->restore();
00208 }
00209
00210 void MathFunction::drawRightArrow(QPainter *painter)
00211 {
00212 int x = upperBounds.x();
00213 int y = upperBounds.y();
00214
00215 QPointArray points(3);
00216 points.setPoint(0,x,y);
00217 points.setPoint(1,x+BOUNDS_TRIANGLE*2,y-BOUNDS_TRIANGLE);
00218 points.setPoint(2,x+BOUNDS_TRIANGLE*2,y+BOUNDS_TRIANGLE);
00219
00220 painter->save();
00221 painter->setPen(QPen(QColor(0,100,255),2));
00222 painter->setBrush(QBrush(QColor(0,100,255),Qt::SolidPattern));
00223 painter->drawPolygon(points);
00224 painter->restore();
00225 }