arenascene.cpp
Go to the documentation of this file.
1 #include "arenascene.h"
2 #include "arenasceneelement.h"
3 
4 #include "../model/arena.h"
5 #include "../model/arenaelement.h"
6 #include "../model/arenaelementtype.h"
7 
8 #include <QDebug>
9 #include <QGraphicsPixmapItem>
10 #include <QPixmap>
11 #include <QPen>
12 #include <QBrush>
13 #include <QMimeData>
14 
15 #include <cmath>
16 
18  : m_arena(arena)
19 {
20  connect(arena, SIGNAL(elementAdded(ArenaElement*)),
22  connect(arena, SIGNAL(elementRemoved(ArenaElement*)),
24  connect(arena, SIGNAL(modified()),
25  SLOT(updateViewMargin()));
26 
27  foreach (ArenaElement *element, arena->elements())
28  slotElementAdded(element);
29 
31 }
32 
33 QPoint ArenaScene::sceneToGrid(QPointF scenePos)
34 {
35  QPointF gridPos = sceneToGridF(scenePos);
36  return QPoint(qRound(gridPos.x()), qRound(gridPos.y()));
37 }
38 
39 QPointF ArenaScene::sceneToGridF(QPointF scenePos)
40 {
41 #if 0
42  qreal x = scenePos.x() + SPACING;
43  // Screen y coordinates are reversed ((0,0) is bottom left, not top left)
44  qreal y = -(scenePos.y() + SPACING);
45  return QPointF(x / (qreal)CELL_SIZE - 0.5, y / (qreal)CELL_SIZE + 0.5);
46 #endif
47  return QPointF(scenePos.x() / (qreal)CELL_SIZE, -scenePos.y() / (qreal)CELL_SIZE);
48 }
49 
50 QPointF ArenaScene::gridToScene(QPointF gridPos)
51 {
52  // Screen y coordinates are reversed
53  return QPointF(gridPos.x() * CELL_SIZE, -gridPos.y() * CELL_SIZE);
54 }
55 
56 QPointF ArenaScene::nearestGridPoint(QPointF scenePos)
57 {
58  return gridToScene(sceneToGrid(scenePos));
59 }
60 
61 QList<ArenaSceneElement*> ArenaScene::selectedElements()
62 {
63  QList<ArenaSceneElement*> list;
64  foreach(QGraphicsItem* item, selectedItems())
65  {
66  ArenaSceneElement* element = dynamic_cast<ArenaSceneElement*>(item);
67  if (element)
68  list.append(element);
69  }
70 
71  return list;
72 }
73 
75 {
76  return m_elements[element];
77 }
78 
80 {
81  // The minimum arena is 3x3 (-1 <= x,y <= 1)
82  int left = -1;
83  int right = 1;
84  int top = 1;
85  int bottom = -1;
86  foreach(ArenaSceneElement* element, m_elements)
87  {
88  QPoint pos = element->element()->pos();
89  left = qMin(left, pos.x());
90  right = qMax(right, pos.x());
91  top = qMax(top, pos.y());
92  bottom = qMin(bottom, pos.y());
93  }
94  QPointF topLeft = gridToScene(QPoint(left, top));
95  QPointF bottomRight = gridToScene(QPoint(right, bottom));
96  setSceneRect(QRectF(topLeft, bottomRight));
97 }
98 
100 {
101  ArenaSceneElement *sceneElement = new ArenaSceneElement(element);
102 
103  m_elements[element] = sceneElement;
104  addItem(sceneElement);
105 
107 }
108 
110 {
111  ArenaSceneElement *sceneElement = m_elements[element];
112  Q_ASSERT(sceneElement);
113 
114  removeItem(sceneElement);
115  m_elements.remove(element);
116  delete sceneElement;
117 
119 }
QMap< ArenaElement *, ArenaSceneElement * > m_elements
Definition: arenascene.h:43
QList< ArenaSceneElement * > selectedElements()
Definition: arenascene.cpp:61
void slotElementAdded(ArenaElement *element)
Definition: arenascene.cpp:99
ArenaScene(Arena *arena)
Definition: arenascene.cpp:17
static QPoint sceneToGrid(QPointF scenePos)
Definition: arenascene.cpp:33
void slotElementRemoved(ArenaElement *element)
Definition: arenascene.cpp:109
ArenaElement * element() const
const int SPACING
Definition: arenascene.h:14
void updateViewMargin()
Definition: arenascene.cpp:79
Definition: arena.h:10
const int CELL_SIZE
Definition: arenascene.h:15
QPoint pos() const
Returns the position in arena coordinates.
Definition: arenaelement.h:22
static QPointF sceneToGridF(QPointF scenePos)
Definition: arenascene.cpp:39
static QPointF nearestGridPoint(QPointF scenePos)
Definition: arenascene.cpp:56
static QPointF gridToScene(QPointF gridPos)
Definition: arenascene.cpp:50
QList< ArenaElement * > elements()
Definition: arena.h:37
ArenaSceneElement * sceneElementFor(ArenaElement *element)
Definition: arenascene.cpp:74


hector_nist_arena_designer
Author(s): Stefan Kohlbrecher , Johannes Simon
autogenerated on Fri Aug 21 2020 10:45:27