Engauge Digitizer  2
Ghosts.cpp
Go to the documentation of this file.
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "DataKey.h"
8 #include "Ghosts.h"
9 #include <qdebug.h>
10 #include <QGraphicsItem>
11 #include <QGraphicsPathItem>
12 #include <QGraphicsPolygonItem>
13 #include <QGraphicsScene>
14 
15 const double Z_VALUE = 100.0;
16 
17 Ghosts::Ghosts (unsigned int coordSystemIndexToBeRestored) :
18  m_coordSystemIndexToBeRestored (coordSystemIndexToBeRestored)
19 {
20 }
21 
23 {
24 }
25 
26 void Ghosts::captureGraphicsItems (QGraphicsScene &scene)
27 {
28  QList<QGraphicsItem*> items = scene.items();
29 
30  QList<QGraphicsItem*>::iterator itr;
31  for (itr = items.begin(); itr != items.end(); itr++) {
32 
33  QGraphicsItem *item = *itr;
34 
35  QGraphicsEllipseItem *itemEllipse = dynamic_cast<QGraphicsEllipseItem*> (item);
36  if (itemEllipse != nullptr) {
37 
38  GhostEllipse ghost (itemEllipse->boundingRect(),
39  itemEllipse->pen(),
40  itemEllipse->brush());
41  m_ellipses.push_back (ghost);
42 
43  } else {
44 
45  QGraphicsPathItem *itemPath = dynamic_cast<QGraphicsPathItem*> (item);
46  if (itemPath != nullptr) {
47 
48  GhostPath ghost (itemPath->path (),
49  itemPath->pen(),
50  itemPath->brush());
51  m_paths.push_back (ghost);
52 
53  } else {
54 
55  QGraphicsPolygonItem *itemPolygon = dynamic_cast<QGraphicsPolygonItem*> (item);
56  if (itemPolygon != nullptr) {
57 
58  // Polygon is centered at origin so we have to add offset
59  QPolygonF polygon = itemPolygon->polygon();
60  polygon.translate (itemPolygon->pos ());
61 
62  GhostPolygon ghost (polygon,
63  itemPolygon->pen(),
64  itemPolygon->brush());
65  m_polygons.push_back (ghost);
66 
67  }
68  }
69  }
70  }
71 }
72 
74 {
75  return m_coordSystemIndexToBeRestored;
76 }
77 
78 void Ghosts::createGhosts (QGraphicsScene &scene)
79 {
80  int i;
81 
82  for (i = 0; i < m_ellipses.count(); i++) {
83  GhostEllipse ghost = m_ellipses.at(i);
84 
85  QGraphicsEllipseItem *item = scene.addEllipse (ghost.rect());
86 
87  item->setData (DATA_KEY_GHOST, QVariant (true));
88  item->setPen (ghost.pen());
89  item->setBrush (ghost.brush());
90  item->setZValue (Z_VALUE);
91  item->setVisible (true);
92  }
93 
94  for (i = 0; i < m_paths.count(); i++) {
95  GhostPath ghost = m_paths.at(i);
96 
97  QGraphicsPathItem *item = scene.addPath (ghost.path(),
98  ghost.pen(),
99  ghost.brush());
100 
101  item->setData (DATA_KEY_GHOST, QVariant (true));
102  item->setZValue (Z_VALUE);
103  item->setVisible (true);
104  }
105 
106  for (i = 0; i < m_polygons.count(); i++) {
107  GhostPolygon ghost = m_polygons.at(i);
108 
109  QGraphicsPolygonItem *item = scene.addPolygon (ghost.polygon(),
110  ghost.pen(),
111  ghost.brush());
112 
113  item->setData (DATA_KEY_GHOST, QVariant (true));
114  item->setZValue (Z_VALUE);
115  item->setVisible (true);
116  }
117 }
118 
119 void Ghosts::destroyGhosts (QGraphicsScene &scene)
120 {
121  QList<QGraphicsItem*> items = scene.items();
122  QList<QGraphicsItem*>::iterator itr;
123  for (itr = items.begin(); itr != items.end(); itr++) {
124 
125  QGraphicsItem *item = *itr;
126  QVariant data = item->data (DATA_KEY_GHOST);
127  if (!data.isNull()) {
128  if (data.toBool()) {
129  scene.removeItem (item);
130  }
131  }
132  }
133 }
@ DATA_KEY_GHOST
True if item has changed since last mousePressEvent
Definition: DataKey.h:17
const double Z_VALUE
Definition: Ghosts.cpp:15
Ghost for a QGraphicsEllipseItem.
Definition: GhostEllipse.h:16
QRectF rect() const
Get method for bounding rectangle.
QPen pen() const
Get method for pen.
QBrush brush() const
Get method for brush.
Ghost for a QGraphicsPathItem.
Definition: GhostPath.h:16
QPainterPath path() const
Get method for path.
Definition: GhostPath.cpp:43
QBrush brush() const
Get method for brush.
Definition: GhostPath.cpp:38
QPen pen() const
Get method for pen.
Definition: GhostPath.cpp:48
Ghost for a QGraphicsPolygonItem.
Definition: GhostPolygon.h:16
QPen pen() const
Get method for pen.
QPolygonF polygon() const
Get method for polygon.
QBrush brush() const
Get method for brush.
unsigned int coordSystemIndexToBeRestored() const
Coordinate system index that was active before the ghosts.
Definition: Ghosts.cpp:73
~Ghosts()
Definition: Ghosts.cpp:22
void createGhosts(QGraphicsScene &scene)
Create ghosts from the path/rect/polygon lists.
Definition: Ghosts.cpp:78
void captureGraphicsItems(QGraphicsScene &scene)
Take a snapshot of the graphics items.
Definition: Ghosts.cpp:26
Ghosts(unsigned int coordSystemIndexToBeRestored)
Single constructor.
Definition: Ghosts.cpp:17
void destroyGhosts(QGraphicsScene &scene)
Destory ghosts. Called at end of algorithm.
Definition: Ghosts.cpp:119