00001
00002
00003
00004
00005
00006
00007 #include "Checker.h"
00008 #include "DocumentModelCoords.h"
00009 #include "EngaugeAssert.h"
00010 #include "EnumsToQt.h"
00011 #include "GridLineFactory.h"
00012 #include "Logger.h"
00013 #include "mmsubs.h"
00014 #include <QDebug>
00015 #include <QGraphicsItem>
00016 #include <QGraphicsScene>
00017 #include <qmath.h>
00018 #include <QPen>
00019 #include <QTextStream>
00020 #include "QtToString.h"
00021 #include "Transformation.h"
00022
00023 const int NUM_AXES_POINTS_2 = 2;
00024 const int NUM_AXES_POINTS_3 = 3;
00025 const int NUM_AXES_POINTS_4 = 4;
00026
00027 extern const QString DUMMY_CURVE_NAME;
00028
00029
00030
00031
00032 const int CHECKER_POINTS_WIDTH = 5;
00033
00034 Checker::Checker(QGraphicsScene &scene) :
00035 m_scene (scene)
00036 {
00037 }
00038
00039 void Checker::adjustPolarAngleRanges (const DocumentModelCoords &modelCoords,
00040 const Transformation &transformation,
00041 const QList<Point> &points,
00042 double &xMin,
00043 double &xMax,
00044 double &yMin) const
00045 {
00046 LOG4CPP_INFO_S ((*mainCat)) << "Checker::adjustPolarAngleRanges transformation=" << transformation;
00047
00048 const double UNIT_LENGTH = 1.0;
00049
00050 QString path;
00051 if (modelCoords.coordsType() == COORDS_TYPE_POLAR) {
00052
00053
00054 yMin = modelCoords.originRadius();
00055
00056 path = QString ("yMin=%1 ").arg (yMin);
00057
00058
00059
00060 double angle0 = points.at(0).posGraph().x();
00061 double angle1 = points.at(1).posGraph().x();
00062 double angle2 = points.at(2).posGraph().x();
00063 QPointF pos0 = transformation.cartesianFromCartesianOrPolar(modelCoords,
00064 QPointF (angle0, UNIT_LENGTH));
00065 QPointF pos1 = transformation.cartesianFromCartesianOrPolar(modelCoords,
00066 QPointF (angle1, UNIT_LENGTH));
00067 QPointF pos2 = transformation.cartesianFromCartesianOrPolar(modelCoords,
00068 QPointF (angle2, UNIT_LENGTH));
00069
00070
00071
00072 double sumAngle0 = angleBetweenVectors(pos0, pos1) + angleBetweenVectors(pos0, pos2);
00073 double sumAngle1 = angleBetweenVectors(pos1, pos0) + angleBetweenVectors(pos1, pos2);
00074 double sumAngle2 = angleBetweenVectors(pos2, pos0) + angleBetweenVectors(pos2, pos1);
00075 if ((sumAngle0 <= sumAngle1) && (sumAngle0 <= sumAngle2)) {
00076
00077
00078 if ((angleFromVectorToVector (pos0, pos1) < 0) ||
00079 (angleFromVectorToVector (pos0, pos2) > 0)) {
00080 path += QString ("from 1=%1 through 0 to 2=%2").arg (angle1).arg (angle2);
00081 xMin = angle1;
00082 xMax = angle2;
00083 } else {
00084 path += QString ("from 2=%1 through 0 to 1=%2").arg (angle2).arg (angle1);
00085 xMin = angle2;
00086 xMax = angle1;
00087 }
00088 } else if ((sumAngle1 <= sumAngle0) && (sumAngle1 <= sumAngle2)) {
00089
00090
00091 if ((angleFromVectorToVector (pos1, pos0) < 0) ||
00092 (angleFromVectorToVector (pos1, pos2) > 0)) {
00093 path += QString ("from 0=%1 through 1 to 2=%2").arg (angle0).arg (angle2);
00094 xMin = angle0;
00095 xMax = angle2;
00096 } else {
00097 path += QString ("from 2=%1 through 1 to 0=%2").arg (angle2).arg (angle0);
00098 xMin = angle2;
00099 xMax = angle0;
00100 }
00101 } else {
00102
00103
00104 if ((angleFromVectorToVector (pos2, pos0) < 0) ||
00105 (angleFromVectorToVector (pos2, pos1) > 0)) {
00106 path += QString ("from 0=%1 through 2 to 1=%2").arg (angle0).arg (angle1);
00107 xMin = angle0;
00108 xMax = angle1;
00109 } else {
00110 path += QString ("from 1=%1 through 2 to 0=%2").arg (angle1).arg (angle0);
00111 xMin = angle1;
00112 xMax = angle0;
00113 }
00114 }
00115
00116
00117 while (xMax < xMin) {
00118
00119 double thetaPeriod = modelCoords.thetaPeriod();
00120
00121 path += QString (" xMax+=%1").arg (thetaPeriod);
00122 xMax += thetaPeriod;
00123
00124 }
00125 }
00126
00127 LOG4CPP_INFO_S ((*mainCat)) << "Checker::adjustPolarAngleRanges path=(" << path.toLatin1().data() << ")";
00128 }
00129
00130 void Checker::prepareForDisplay (const QPolygonF &polygon,
00131 int pointRadius,
00132 const DocumentModelAxesChecker &modelAxesChecker,
00133 const DocumentModelCoords &modelCoords,
00134 DocumentAxesPointsRequired documentAxesPointsRequired)
00135 {
00136 LOG4CPP_INFO_S ((*mainCat)) << "Checker::prepareForDisplay";
00137
00138 ENGAUGE_ASSERT ((polygon.count () == NUM_AXES_POINTS_2) ||
00139 (polygon.count () == NUM_AXES_POINTS_3) ||
00140 (polygon.count () == NUM_AXES_POINTS_4));
00141
00142
00143
00144 QList<Point> points;
00145 QPolygonF::const_iterator itr;
00146 for (itr = polygon.begin (); itr != polygon.end (); itr++) {
00147
00148 const QPointF &pF = *itr;
00149
00150 Point p (DUMMY_CURVE_NAME,
00151 pF,
00152 pF,
00153 false);
00154 points.push_back (p);
00155 }
00156
00157
00158 Transformation transformIdentity;
00159 transformIdentity.identity();
00160 prepareForDisplay (points,
00161 pointRadius,
00162 modelAxesChecker,
00163 modelCoords,
00164 transformIdentity,
00165 documentAxesPointsRequired);
00166 }
00167
00168 void Checker::prepareForDisplay (const QList<Point> &points,
00169 int pointRadius,
00170 const DocumentModelAxesChecker &modelAxesChecker,
00171 const DocumentModelCoords &modelCoords,
00172 const Transformation &transformation,
00173 DocumentAxesPointsRequired documentAxesPointsRequired)
00174 {
00175 LOG4CPP_INFO_S ((*mainCat)) << "Checker::prepareForDisplay "
00176 << " transformation=" << transformation;
00177
00178 ENGAUGE_ASSERT ((points.count () == NUM_AXES_POINTS_2) ||
00179 (points.count () == NUM_AXES_POINTS_3) ||
00180 (points.count () == NUM_AXES_POINTS_4));
00181
00182
00183 m_gridLines.clear ();
00184
00185 bool fourPoints = (documentAxesPointsRequired == DOCUMENT_AXES_POINTS_REQUIRED_4);
00186
00187
00188 double xFrom = 0, xTo = 0, yFrom = 0, yTo = 0;
00189 int i;
00190 bool firstX = true;
00191 bool firstY = true;
00192 for (i = 0; i < points.count(); i++) {
00193 if (!fourPoints || (points.at(i).isXOnly() && fourPoints)) {
00194
00195
00196 if (firstX) {
00197 xFrom = points.at(i).posGraph().x();
00198 xTo = points.at(i).posGraph().x();
00199 firstX = false;
00200 } else {
00201 xFrom = qMin (xFrom, points.at(i).posGraph().x());
00202 xTo = qMax (xTo , points.at(i).posGraph().x());
00203 }
00204 }
00205
00206 if (!fourPoints || (!points.at(i).isXOnly() && fourPoints)) {
00207
00208
00209 if (firstY) {
00210 yFrom = points.at(i).posGraph().y();
00211 yTo = points.at(i).posGraph().y();
00212 firstY = false;
00213 } else {
00214 yFrom = qMin (yFrom, points.at(i).posGraph().y());
00215 yTo = qMax (yTo , points.at(i).posGraph().y());
00216 }
00217 }
00218 }
00219
00220
00221
00222 adjustPolarAngleRanges (modelCoords,
00223 transformation,
00224 points,
00225 xFrom,
00226 xTo,
00227 yFrom);
00228
00229
00230 GridLineFactory factory (m_scene,
00231 pointRadius,
00232 points,
00233 modelCoords);
00234 m_gridLines.add (factory.createGridLine (xFrom, yFrom, xFrom, yTo , transformation));
00235 m_gridLines.add (factory.createGridLine (xFrom, yTo , xTo , yTo , transformation));
00236 m_gridLines.add (factory.createGridLine (xTo , yTo , xTo , yFrom, transformation));
00237 m_gridLines.add (factory.createGridLine (xTo , yFrom, xFrom, yFrom, transformation));
00238
00239 updateModelAxesChecker (modelAxesChecker);
00240 }
00241
00242 void Checker::setVisible (bool visible)
00243 {
00244 m_gridLines.setVisible (visible);
00245 }
00246
00247 void Checker::updateModelAxesChecker (const DocumentModelAxesChecker &modelAxesChecker)
00248 {
00249 QColor color = ColorPaletteToQColor (modelAxesChecker.lineColor());
00250 QPen pen (QBrush (color), CHECKER_POINTS_WIDTH);
00251
00252 m_gridLines.setPen (pen);
00253 }