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