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