arenascene.cpp
Go to the documentation of this file.
00001 #include "arenascene.h"
00002 #include "arenasceneelement.h"
00003 
00004 #include "../model/arena.h"
00005 #include "../model/arenaelement.h"
00006 #include "../model/arenaelementtype.h"
00007 
00008 #include <QDebug>
00009 #include <QGraphicsPixmapItem>
00010 #include <QPixmap>
00011 #include <QPen>
00012 #include <QBrush>
00013 #include <QMimeData>
00014 
00015 #include <cmath>
00016 
00017 ArenaScene::ArenaScene(Arena *arena)
00018     : m_arena(arena)
00019 {
00020     connect(arena, SIGNAL(elementAdded(ArenaElement*)),
00021                    SLOT(slotElementAdded(ArenaElement*)));
00022     connect(arena, SIGNAL(elementRemoved(ArenaElement*)),
00023                    SLOT(slotElementRemoved(ArenaElement*)));
00024     connect(arena, SIGNAL(modified()),
00025                    SLOT(updateViewMargin()));
00026 
00027     foreach (ArenaElement *element, arena->elements())
00028         slotElementAdded(element);
00029 
00030     updateViewMargin();
00031 }
00032 
00033 QPoint ArenaScene::sceneToGrid(QPointF scenePos)
00034 {
00035     QPointF gridPos = sceneToGridF(scenePos);
00036     return QPoint(qRound(gridPos.x()), qRound(gridPos.y()));
00037 }
00038 
00039 QPointF ArenaScene::sceneToGridF(QPointF scenePos)
00040 {
00041 #if 0
00042     qreal x = scenePos.x() + SPACING;
00043     // Screen y coordinates are reversed ((0,0) is bottom left, not top left)
00044     qreal y = -(scenePos.y() + SPACING);
00045     return QPointF(x / (qreal)CELL_SIZE - 0.5, y / (qreal)CELL_SIZE + 0.5);
00046 #endif
00047     return QPointF(scenePos.x() / (qreal)CELL_SIZE, -scenePos.y() / (qreal)CELL_SIZE);
00048 }
00049 
00050 QPointF ArenaScene::gridToScene(QPointF gridPos)
00051 {
00052     // Screen y coordinates are reversed
00053     return QPointF(gridPos.x() * CELL_SIZE, -gridPos.y() * CELL_SIZE);
00054 }
00055 
00056 QPointF ArenaScene::nearestGridPoint(QPointF scenePos)
00057 {
00058     return gridToScene(sceneToGrid(scenePos));
00059 }
00060 
00061 QList<ArenaSceneElement*> ArenaScene::selectedElements()
00062 {
00063     QList<ArenaSceneElement*> list;
00064     foreach(QGraphicsItem* item, selectedItems())
00065     {
00066         ArenaSceneElement* element = dynamic_cast<ArenaSceneElement*>(item);
00067         if (element)
00068             list.append(element);
00069     }
00070 
00071     return list;
00072 }
00073 
00074 ArenaSceneElement *ArenaScene::sceneElementFor(ArenaElement *element)
00075 {
00076     return m_elements[element];
00077 }
00078 
00079 void ArenaScene::updateViewMargin()
00080 {
00081     // The minimum arena is 3x3 (-1 <= x,y <= 1)
00082     int left = -1;
00083     int right = 1;
00084     int top = 1;
00085     int bottom = -1;
00086     foreach(ArenaSceneElement* element, m_elements)
00087     {
00088         QPoint pos = element->element()->pos();
00089         left   = qMin(left, pos.x());
00090         right  = qMax(right, pos.x());
00091         top    = qMax(top, pos.y());
00092         bottom = qMin(bottom, pos.y());
00093     }
00094     QPointF topLeft = gridToScene(QPoint(left, top));
00095     QPointF bottomRight = gridToScene(QPoint(right, bottom));
00096     setSceneRect(QRectF(topLeft, bottomRight));
00097 }
00098 
00099 void ArenaScene::slotElementAdded(ArenaElement *element)
00100 {
00101     ArenaSceneElement *sceneElement = new ArenaSceneElement(element);
00102 
00103     m_elements[element] = sceneElement;
00104     addItem(sceneElement);
00105 
00106     updateViewMargin();
00107 }
00108 
00109 void ArenaScene::slotElementRemoved(ArenaElement *element)
00110 {
00111     ArenaSceneElement *sceneElement = m_elements[element];
00112     Q_ASSERT(sceneElement);
00113 
00114     removeItem(sceneElement);
00115     m_elements.remove(element);
00116     delete sceneElement;
00117 
00118     updateViewMargin();
00119 }


hector_nist_arena_designer
Author(s): Stefan Kohlbrecher , Johannes Simon
autogenerated on Wed Oct 4 2017 03:29:29