00001
00002
00003
00004
00005
00006
00007 #include "DataKey.h"
00008 #include "Ghosts.h"
00009 #include <qdebug.h>
00010 #include <QGraphicsItem>
00011 #include <QGraphicsPathItem>
00012 #include <QGraphicsPolygonItem>
00013 #include <QGraphicsScene>
00014
00015 const double Z_VALUE = 100.0;
00016
00017 Ghosts::Ghosts (unsigned int coordSystemIndexToBeRestored) :
00018 m_coordSystemIndexToBeRestored (coordSystemIndexToBeRestored)
00019 {
00020 }
00021
00022 Ghosts::~Ghosts ()
00023 {
00024 }
00025
00026 void Ghosts::captureGraphicsItems (QGraphicsScene &scene)
00027 {
00028 QList<QGraphicsItem*> items = scene.items();
00029
00030 QList<QGraphicsItem*>::iterator itr;
00031 for (itr = items.begin(); itr != items.end(); itr++) {
00032
00033 QGraphicsItem *item = *itr;
00034
00035 QGraphicsEllipseItem *itemEllipse = dynamic_cast<QGraphicsEllipseItem*> (item);
00036 if (itemEllipse != 0) {
00037
00038 GhostEllipse ghost (itemEllipse->boundingRect(),
00039 itemEllipse->pen(),
00040 itemEllipse->brush());
00041 m_ellipses.push_back (ghost);
00042
00043 } else {
00044
00045 QGraphicsPathItem *itemPath = dynamic_cast<QGraphicsPathItem*> (item);
00046 if (itemPath != 0) {
00047
00048 GhostPath ghost (itemPath->path (),
00049 itemPath->pen(),
00050 itemPath->brush());
00051 m_paths.push_back (ghost);
00052
00053 } else {
00054
00055 QGraphicsPolygonItem *itemPolygon = dynamic_cast<QGraphicsPolygonItem*> (item);
00056 if (itemPolygon != 0) {
00057
00058
00059 QPolygonF polygon = itemPolygon->polygon();
00060 polygon.translate (itemPolygon->pos ());
00061
00062 GhostPolygon ghost (polygon,
00063 itemPolygon->pen(),
00064 itemPolygon->brush());
00065 m_polygons.push_back (ghost);
00066
00067 }
00068 }
00069 }
00070 }
00071 }
00072
00073 unsigned int Ghosts::coordSystemIndexToBeRestored () const
00074 {
00075 return m_coordSystemIndexToBeRestored;
00076 }
00077
00078 void Ghosts::createGhosts (QGraphicsScene &scene)
00079 {
00080 int i;
00081
00082 for (i = 0; i < m_ellipses.count(); i++) {
00083 GhostEllipse ghost = m_ellipses.at(i);
00084
00085 QGraphicsEllipseItem *item = scene.addEllipse (ghost.rect());
00086
00087 item->setData (DATA_KEY_GHOST, QVariant (true));
00088 item->setPen (ghost.pen());
00089 item->setBrush (ghost.brush());
00090 item->setZValue (Z_VALUE);
00091 item->setVisible (true);
00092 }
00093
00094 for (i = 0; i < m_paths.count(); i++) {
00095 GhostPath ghost = m_paths.at(i);
00096
00097 QGraphicsPathItem *item = scene.addPath (ghost.path(),
00098 ghost.pen(),
00099 ghost.brush());
00100
00101 item->setData (DATA_KEY_GHOST, QVariant (true));
00102 item->setZValue (Z_VALUE);
00103 item->setVisible (true);
00104 }
00105
00106 for (i = 0; i < m_polygons.count(); i++) {
00107 GhostPolygon ghost = m_polygons.at(i);
00108
00109 QGraphicsPolygonItem *item = scene.addPolygon (ghost.polygon(),
00110 ghost.pen(),
00111 ghost.brush());
00112
00113 item->setData (DATA_KEY_GHOST, QVariant (true));
00114 item->setZValue (Z_VALUE);
00115 item->setVisible (true);
00116 }
00117 }
00118
00119 void Ghosts::destroyGhosts (QGraphicsScene &scene)
00120 {
00121 QList<QGraphicsItem*> items = scene.items();
00122 QList<QGraphicsItem*>::iterator itr;
00123 for (itr = items.begin(); itr != items.end(); itr++) {
00124
00125 QGraphicsItem *item = *itr;
00126 QVariant data = item->data (DATA_KEY_GHOST);
00127 if (!data.isNull()) {
00128 if (data.toBool()) {
00129 scene.removeItem (item);
00130 }
00131 }
00132 }
00133 }