00001
00002
00003
00004
00005
00006
00007 #include "CurveStyle.h"
00008 #include "DataKey.h"
00009 #include "EnumsToQt.h"
00010 #include "GraphicsItemType.h"
00011 #include "GraphicsPoint.h"
00012 #include "GraphicsPointEllipse.h"
00013 #include "GraphicsPointPolygon.h"
00014 #include "Logger.h"
00015 #include "PointStyle.h"
00016 #include <QGraphicsEllipseItem>
00017 #include <QGraphicsPolygonItem>
00018 #include <QGraphicsScene>
00019 #include <QGraphicsSceneContextMenuEvent>
00020 #include <QPen>
00021 #include <QTextStream>
00022 #include "QtToString.h"
00023
00024 const double ZERO_WIDTH = 0.0;
00025 const double Z_VALUE = 100.0;
00026
00027 GraphicsPoint::GraphicsPoint(QGraphicsScene &scene,
00028 const QString &identifier,
00029 const QPointF &posScreen,
00030 const QColor &color,
00031 unsigned int radius,
00032 double lineWidth) :
00033 GraphicsPointAbstractBase (),
00034 m_scene (scene),
00035 m_graphicsItemEllipse (0),
00036 m_shadowZeroWidthEllipse (0),
00037 m_graphicsItemPolygon (0),
00038 m_shadowZeroWidthPolygon (0),
00039 m_identifier (identifier),
00040 m_posScreen (posScreen),
00041 m_color (color),
00042 m_lineWidth (lineWidth),
00043 m_wanted (true)
00044 {
00045 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsPoint::GraphicsPoint"
00046 << " identifier=" << identifier.toLatin1 ().data ();
00047
00048 createPointEllipse (radius);
00049 }
00050
00051 GraphicsPoint::GraphicsPoint(QGraphicsScene &scene,
00052 const QString &identifier,
00053 const QPointF &posScreen,
00054 const QColor &color,
00055 const QPolygonF &polygon,
00056 double lineWidth) :
00057 GraphicsPointAbstractBase (),
00058 m_scene (scene),
00059 m_graphicsItemEllipse (0),
00060 m_shadowZeroWidthEllipse (0),
00061 m_graphicsItemPolygon (0),
00062 m_shadowZeroWidthPolygon (0),
00063 m_identifier (identifier),
00064 m_posScreen (posScreen),
00065 m_color (color),
00066 m_lineWidth (lineWidth),
00067 m_wanted (true)
00068 {
00069 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsPoint::GraphicsPoint "
00070 << " identifier=" << identifier.toLatin1 ().data ();
00071
00072 createPointPolygon (polygon);
00073 }
00074
00075 GraphicsPoint::~GraphicsPoint()
00076 {
00077 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsPoint::~GraphicsPoint";
00078
00079 if (m_graphicsItemEllipse == 0) {
00080
00081 QGraphicsScene *scene = m_graphicsItemPolygon->scene();
00082
00083
00084 scene->removeItem (m_graphicsItemPolygon);
00085 delete m_graphicsItemPolygon;
00086 m_graphicsItemPolygon = 0;
00087 m_shadowZeroWidthPolygon = 0;
00088
00089
00090 } else {
00091
00092 QGraphicsScene *scene = m_graphicsItemEllipse->scene();
00093
00094
00095 scene->removeItem (m_graphicsItemEllipse);
00096 delete m_graphicsItemEllipse;
00097 m_graphicsItemEllipse = 0;
00098 m_shadowZeroWidthEllipse = 0;
00099
00100 }
00101 }
00102
00103 void GraphicsPoint::createPointEllipse (unsigned int radius)
00104 {
00105 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsPoint::createPointEllipse";
00106
00107 const int radiusSigned = radius;
00108 m_graphicsItemEllipse = new GraphicsPointEllipse (*this,
00109 QRect (- radiusSigned,
00110 - radiusSigned,
00111 2 * radiusSigned + 1,
00112 2 * radiusSigned + 1));
00113 m_scene.addItem (m_graphicsItemEllipse);
00114
00115 m_graphicsItemEllipse->setZValue (Z_VALUE);
00116 m_graphicsItemEllipse->setData (DATA_KEY_IDENTIFIER, m_identifier);
00117 m_graphicsItemEllipse->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
00118 m_graphicsItemEllipse->setPos (m_posScreen.x (),
00119 m_posScreen.y ());
00120 m_graphicsItemEllipse->setPen (QPen (QBrush (m_color), m_lineWidth));
00121 m_graphicsItemEllipse->setEnabled (true);
00122 m_graphicsItemEllipse->setFlags (QGraphicsItem::ItemIsSelectable |
00123 QGraphicsItem::ItemIsMovable |
00124 QGraphicsItem::ItemSendsGeometryChanges);
00125
00126 m_graphicsItemEllipse->setToolTip (m_identifier);
00127 m_graphicsItemEllipse->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
00128
00129
00130
00131 m_shadowZeroWidthEllipse = new GraphicsPointEllipse (*this,
00132 QRect (- radiusSigned,
00133 - radiusSigned,
00134 2 * radiusSigned + 1,
00135 2 * radiusSigned + 1));
00136 m_shadowZeroWidthEllipse->setParentItem(m_graphicsItemPolygon);
00137
00138 m_shadowZeroWidthEllipse->setPen (QPen (QBrush (m_color), ZERO_WIDTH));
00139 m_shadowZeroWidthEllipse->setEnabled (true);
00140 }
00141
00142 void GraphicsPoint::createPointPolygon (const QPolygonF &polygon)
00143 {
00144 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsPoint::createPointPolygon";
00145
00146 m_graphicsItemPolygon = new GraphicsPointPolygon (*this,
00147 polygon);
00148 m_scene.addItem (m_graphicsItemPolygon);
00149
00150 m_graphicsItemPolygon->setZValue (Z_VALUE);
00151 m_graphicsItemPolygon->setData (DATA_KEY_IDENTIFIER, m_identifier);
00152 m_graphicsItemPolygon->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
00153 m_graphicsItemPolygon->setPos (m_posScreen.x (),
00154 m_posScreen.y ());
00155 m_graphicsItemPolygon->setPen (QPen (QBrush (m_color), m_lineWidth));
00156 m_graphicsItemPolygon->setEnabled (true);
00157 m_graphicsItemPolygon->setFlags (QGraphicsItem::ItemIsSelectable |
00158 QGraphicsItem::ItemIsMovable |
00159 QGraphicsItem::ItemSendsGeometryChanges);
00160
00161 m_graphicsItemPolygon->setToolTip (m_identifier);
00162 m_graphicsItemPolygon->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
00163
00164
00165
00166 m_shadowZeroWidthPolygon = new GraphicsPointPolygon (*this,
00167 polygon);
00168 m_shadowZeroWidthPolygon->setParentItem(m_graphicsItemPolygon);
00169
00170 m_shadowZeroWidthPolygon->setPen (QPen (QBrush (m_color), ZERO_WIDTH));
00171 m_shadowZeroWidthPolygon->setEnabled (true);
00172 }
00173
00174 QVariant GraphicsPoint::data (int key) const
00175 {
00176 if (m_graphicsItemEllipse == 0) {
00177 return m_graphicsItemPolygon->data (key);
00178 } else {
00179 return m_graphicsItemEllipse->data (key);
00180 }
00181 }
00182
00183 QPointF GraphicsPoint::pos () const
00184 {
00185 if (m_graphicsItemEllipse == 0) {
00186 return m_graphicsItemPolygon->pos ();
00187 } else {
00188 return m_graphicsItemEllipse->pos ();
00189 }
00190 }
00191
00192 void GraphicsPoint::printStream (QString indentation,
00193 QTextStream &str,
00194 double ordinalKey) const
00195 {
00196 str << indentation << "GraphicsPoint\n";
00197
00198 indentation += INDENTATION_DELTA;
00199
00200 QString identifier;
00201 QString pointType;
00202 QPointF pos;
00203 if (m_graphicsItemEllipse == 0) {
00204 identifier = m_graphicsItemPolygon->data (DATA_KEY_IDENTIFIER).toString ();
00205 pointType = "polygon";
00206 pos = m_graphicsItemPolygon->pos();
00207 } else {
00208 identifier = m_graphicsItemEllipse->data (DATA_KEY_IDENTIFIER).toString ();
00209 pointType = "ellipse";
00210 pos = m_graphicsItemEllipse->pos();
00211 }
00212
00213 DataKey type = (DataKey) data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt();
00214
00215 str << indentation << identifier
00216 << " ordinalKey=" << ordinalKey
00217 << " dataIdentifier=" << data (DATA_KEY_IDENTIFIER).toString().toLatin1().data()
00218 << " dataType=" << dataKeyToString (type).toLatin1().data()
00219 << " " << pointType << "Pos=" << QPointFToString (pos) << "\n";
00220 }
00221
00222 void GraphicsPoint::reset ()
00223 {
00224 m_wanted = false;
00225 }
00226
00227 void GraphicsPoint::setData (int key, const QVariant &data)
00228 {
00229 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsPoint::setData"
00230 << " key=" << dataKeyToString ((DataKey) key).toLatin1().data()
00231 << " data=" << data.toString().toLatin1().data();
00232
00233 if (m_graphicsItemEllipse == 0) {
00234 m_graphicsItemPolygon->setData (key, data);
00235 } else {
00236 m_graphicsItemEllipse->setData (key, data);
00237 }
00238 }
00239
00240 void GraphicsPoint::setPointStyle(const PointStyle &pointStyle)
00241 {
00242
00243
00244 if (m_graphicsItemEllipse == 0) {
00245 if (pointStyle.shape() == POINT_SHAPE_CIRCLE) {
00246
00247
00248 delete m_graphicsItemPolygon;
00249 m_graphicsItemPolygon = 0;
00250 m_shadowZeroWidthPolygon = 0;
00251
00252 createPointEllipse (pointStyle.radius());
00253
00254 } else {
00255
00256
00257 m_graphicsItemPolygon->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
00258 pointStyle.lineWidth()));
00259 m_shadowZeroWidthPolygon->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
00260 pointStyle.lineWidth()));
00261 m_graphicsItemPolygon->setPolygon (pointStyle.polygon());
00262 m_shadowZeroWidthPolygon->setPolygon (pointStyle.polygon());
00263
00264 }
00265 } else {
00266 if (pointStyle.shape() != POINT_SHAPE_CIRCLE) {
00267
00268
00269 delete m_graphicsItemEllipse;
00270 m_graphicsItemEllipse = 0;
00271 m_shadowZeroWidthEllipse = 0;
00272
00273 createPointPolygon (pointStyle.polygon());
00274
00275 } else {
00276
00277
00278 m_graphicsItemEllipse->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
00279 pointStyle.lineWidth()));
00280 m_shadowZeroWidthEllipse->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
00281 pointStyle.lineWidth()));
00282 m_graphicsItemEllipse->setRadius (pointStyle.radius());
00283 m_shadowZeroWidthEllipse->setRadius (pointStyle.radius());
00284 }
00285 }
00286 }
00287
00288 void GraphicsPoint::setPos (const QPointF pos)
00289 {
00290 if (m_graphicsItemEllipse == 0) {
00291 return m_graphicsItemPolygon->setPos (pos);
00292 } else {
00293 return m_graphicsItemEllipse->setPos (pos);
00294 }
00295 }
00296
00297 void GraphicsPoint::setToolTip (const QString &toolTip)
00298 {
00299 if (m_graphicsItemEllipse == 0) {
00300 m_graphicsItemPolygon->setToolTip (toolTip);
00301 } else {
00302 m_graphicsItemEllipse->setToolTip (toolTip);
00303 }
00304 }
00305
00306 void GraphicsPoint::setWanted ()
00307 {
00308 m_wanted = true;
00309 }
00310
00311 void GraphicsPoint::updateCurveStyle (const CurveStyle &curveStyle)
00312 {
00313 setPointStyle (curveStyle.pointStyle());
00314 }
00315
00316 bool GraphicsPoint::wanted () const
00317 {
00318 return m_wanted;
00319 }