File: vibesscene2d.cpp

package info (click to toggle)
vibes 0.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,684 kB
  • sloc: cpp: 6,120; python: 412; makefile: 214; sh: 13
file content (201 lines) | stat: -rw-r--r-- 5,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "vibesscene2d.h"

#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>

#include <QtCore>

#include "vibesgraphicsitem.h"

/// Default Constructor

VibesScene2D::VibesScene2D(QObject *parent) :
    QGraphicsScene(parent),
    _dimX(0), _dimY(1), _nbDim(2)
{
}

/// Destructor

VibesScene2D::~VibesScene2D()
{
    // All objects are removed from the scene in VibesScene2D destructor.
    // This is necessary in ordrer to avoid BAD_ACCESS, since named objects call
    // setItemName of the scene to unregister their name when they die. Otherwise,
    // since VibesScene2D is destructed before QGraphicsScene, the hash _namedItems
    // would not exist anymore when QGraphicsScene destructor would destroy the items

    foreach (QGraphicsItem* item, this->items())
        delete item;
}



/// Add a graphics item to the scene from its JSON "shape" object description
/// \param[in] shape The JSON object containing properties
/// \returns The item created and initialized from JSON objet. Null pointer if creation or initialization failed.

VibesGraphicsItem * VibesScene2D::addJsonShapeItem(const QJsonObject &shape)
{
    // The graphics item that will be created (will be null if creation fails)
    VibesGraphicsItem * item = 0;
    // The group to which the item will belong (will be null if no group specified, or if group does not exist)
    VibesGraphicsGroup * group = 0;

    // Contruct a new object from given type string
    if (shape.contains("type"))
    {
        QString type = shape["type"].toString();
        item = VibesGraphicsItem::newWithType(type);
    }
    
    // Check if an VibesGraphicsItem has been created
    if (!item)
    {
        return 0;
    }

    // Find the object parent group if specified
    if (shape.contains("group"))
    {
        QString groupName = shape["group"].toString();
        group = vibesgraphicsitem_cast<VibesGraphicsGroup*>( itemByName(groupName) );
    }

    // Try to initialize item with JSON
    if (!item->setJson(shape, dimX(), dimY()))
    {
        // Cannot set item wth the provided Json, delete item
        delete item;
        item = 0;
    }
/*    else
    {
        // Item successufully initialized, update dimension
        _nbDim = qMax(_nbDim, vibesItem->dimension());
    }
*/
    // If the item has successfully been created and initialized, put it on the scene
    if (item)
    {
        this->addVibesItem(item);
        // If the item belongs to a group, add it to the group
        if (group)
        {
            group->addToGroup(item);
        }
    }

    return item;
}

void VibesScene2D::addVibesItem(VibesGraphicsItem *item)
{
    if (!item) return;
    // Update scene dimension
    _nbDim = qMax(_nbDim, item->dimension());
    // Add item to the scene
    this->addItem(vibesgraphicsitem_cast<QGraphicsItem*>(item));
    // Update list of named objects
    this->setItemName(item, item->name());
}

void VibesScene2D::setItemName(VibesGraphicsItem *item, QString name)
{
    // If needed, unname existing item with the same name
    if (VibesGraphicsItem *old_item = itemByName(name))
    {
        // Nothing to do if the name already points to the good item
        if (old_item != item)
        {
            // Unname previous item
            old_item->setName(QString());
        }
    }
    
    // If needed, remove existing entry for the item
    _namedItems.remove(_namedItems.key(item));
    
    // Update named items list
    if (!name.isEmpty())
    {
        _namedItems[name] = item;
    }
    // Update new item if needed
    if (item && item->name() != name)
    {
        item->setName(name);
    }
}

bool VibesScene2D::setDimX(int dimX)
{
    if (dimX>=0 && dimX<nbDim() && dimX!=this->dimX() && dimX!=dimY())
    {
        _dimX = dimX;
        emit changedDimX(dimX);
        emit dimensionsChanged();
        updateDims();
        return true;
    } else {
        // Notify that invalid change has been refused
        if (dimX != this->dimX()) { emit changedDimX(this->dimX()); emit dimensionsChanged(); }
        return false;
    }
}

bool VibesScene2D::setDimY(int dimY)
{
    if (dimY>=0 && dimY<nbDim() && dimY!=this->dimY() && dimY!=dimX())
    {
        _dimY = dimY;
        emit changedDimY(dimY);
        emit dimensionsChanged();
        updateDims();
        return true;
    } else {
        // Notify that invalid change has been refused
        if (dimY != this->dimY()) { emit changedDimY(this->dimY()); emit dimensionsChanged(); }
        return false;
    }
}

bool VibesScene2D::setDims(int dimX, int dimY)
{
    if (dimX == this->dimX())
        return setDimY(dimY);
    else if (dimY == this->dimY())
        return setDimX(dimX);
    else if (dimY>=0 && dimY<nbDim() && dimX>=0 && dimX<nbDim() && dimX!=dimY)
    {
        _dimX = dimX;
        _dimY = dimY;
        emit changedDimX(dimX);
        emit changedDimY(dimY);
        emit dimensionsChanged();
        updateDims();
        return true;
    } else {
        // Notify that invalid change has been refused
        if (dimX != this->dimX()) { emit changedDimX(this->dimX()); emit dimensionsChanged(); }
        if (dimY != this->dimY()) { emit changedDimY(this->dimY()); emit dimensionsChanged(); }
        return false;
    }
}

void VibesScene2D::updateDims()
{
    QList<QGraphicsItem*> children = this->items();
    foreach(QGraphicsItem *item, children)
    {
        if (VibesGraphicsItem * vibesItem = qgraphicsitem_cast<VibesGraphicsItem*>(item))
        {
            if ( vibesItem->setProj(dimX(),dimY()) )
                item->setVisible(true);
            else
                item->setVisible(false);
        }
    }
    setSceneRect(itemsBoundingRect());
}