propertyvisitors.cpp 10.7 KB
Newer Older
1
2
3
4
5
#include "propertyvisitors.hpp"

#include <QJsonArray>
#include <QGroupBox>
#include <QPushButton>
Merwane Bouri's avatar
Merwane Bouri committed
6
#include <QDebug>
7
#include <QScrollArea>
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

UIBuilderVisitor::UIBuilderVisitor(QWidget *base_widget, bool destructive)
{
    assert(base_widget != nullptr);

    if (destructive)
    {
        // use a form layout on the parent widget
        if (base_widget->layout())
        {
            clear_layout(base_widget->layout());
            delete base_widget->layout();
        }
        base_widget->setLayout(new QFormLayout);
    }
    else
        assert(base_widget->layout() != nullptr);

    m_widget_hierarchy.push(base_widget);
}

void UIBuilderVisitor::add_widget(const std::string &prop_name, QWidget *ptr)
{
    // handle whether the current widget is for JSON objects or JSON arrays
    QFormLayout* form_layout = qobject_cast<QFormLayout*>(current_widget()->layout());
    QVBoxLayout* vbox_layout = qobject_cast<QVBoxLayout*>(current_widget()->layout());
    if (form_layout)
    {
        if (prop_name.empty())
            form_layout->addRow(ptr);
        else
            form_layout->addRow(QString::fromStdString(prop_name) + " : ", ptr);
    }
    else if (vbox_layout)
    {
        vbox_layout->addWidget(ptr);
    }
}

void UIBuilderVisitor::clear_layout(QLayout *layout) {
    QLayoutItem *item;
    while(layout->count() && (item = layout->takeAt(0))) {
        if (item->layout()) {
            clear_layout(item->layout());
            item->layout()->deleteLater();
        }
        if (item->widget()) {
            item->widget()->deleteLater();
        }
        delete item;
    }
}

QWidget *UIBuilderVisitor::current_widget()
{
    assert(!m_widget_hierarchy.empty());
    return m_widget_hierarchy.top();
}

void UIBuilderVisitor::push_array_widget(const Property &prop)
{
69
70
71
72
73
    QScrollArea* scroll = new QScrollArea(current_widget());
    QGroupBox* box = new QGroupBox(QString::fromStdString(prop.display_name()));
    scroll->setWidget(box);
    scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scroll->setWidgetResizable(true);
74
    box->setLayout(new QVBoxLayout);
75
    add_widget("", scroll);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    m_widget_hierarchy.push(box);
}

QWidget* UIBuilderVisitor::pop_widget()
{
    assert(!m_widget_hierarchy.empty());
    QWidget* ptr = m_widget_hierarchy.top();
    m_widget_hierarchy.pop();
    return ptr;
}

void UIBuilderVisitor::visit(StringProperty &str)
{
    QLineEdit* line = new QLineEdit(QString::fromStdString(str.str), current_widget());
90
    add_widget(str.display_name(), line);
91
92
93
94
95
96
97
98

    QObject::connect(line, &QLineEdit::textEdited,
                     [&str](const QString& qstr) { str.str = qstr.toStdString(); });
}

void UIBuilderVisitor::visit(IntegerProperty &prop)
{
    QSpinBox* spin = new QSpinBox(current_widget());
99
    add_widget(prop.display_name(), spin);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    spin->setValue(prop.val);
    spin->setMinimum(prop.prop_min);
    spin->setMaximum(prop.prop_max);
    QObject::connect(spin, QOverload<int>::of(&QSpinBox::valueChanged),
                     [&prop](int i) { prop.val = i; });
}

void UIBuilderVisitor::visit(CoordinateProperty &prop)
{
    QWidget* frame = new QWidget(current_widget());
    frame->setLayout(new QHBoxLayout);
    frame->layout()->setContentsMargins(0,0,0,0);
    QSpinBox* spin_x = new QSpinBox(frame);
    QSpinBox* spin_y = new QSpinBox(frame);
    frame->layout()->addWidget(new QLabel("x :", frame));
    frame->layout()->addWidget(spin_x);
    frame->layout()->addWidget(new QLabel("y :", frame));
    frame->layout()->addWidget(spin_y);
    frame->layout()->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
120
    add_widget(prop.display_name(), frame);
121

122
123
    spin_x->setMinimum(std::numeric_limits<int>::min()); spin_y->setMinimum(std::numeric_limits<int>::min());
    spin_x->setMaximum(std::numeric_limits<int>::max()); spin_y->setMaximum(std::numeric_limits<int>::max());
124
    spin_x->setValue(prop.c.x); spin_y->setValue(prop.c.y);
125
    QObject::connect(spin_x, QOverload<int>::of(&QSpinBox::valueChanged),
126
                     [&prop](int i) { prop.c.x = i; });
127
    QObject::connect(spin_y, QOverload<int>::of(&QSpinBox::valueChanged),
128
                     [&prop](int i) { prop.c.y = i; });
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

void UIBuilderVisitor::visit(PropertyList &list)
{
    push_array_widget(list);

    for (const auto& ptr : list.contents)
    {
        ptr->accept(*this);
    }

    QWidget* list_widget = pop_widget();

    if (list.dynamic)
    {
        QWidget* frame = new QWidget(current_widget());
        frame->setLayout(new QHBoxLayout);
        frame->layout()->setContentsMargins(0,0,0,0);

        QPushButton* add_button = new QPushButton("Ajouter...", current_widget());
        QPushButton* del_button = new QPushButton("Supprimer...", current_widget());
150
        QPushButton* widgetGridNeighborhoodButton = new QPushButton("Grille...", current_widget());
151
152
153

        frame->layout()->addWidget(add_button);
        frame->layout()->addWidget(del_button);
154
        frame->layout()->addWidget(widgetGridNeighborhoodButton);
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
        frame->layout()->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

        add_widget("", frame);

        QObject::connect(add_button, &QPushButton::pressed,
                         [&list, add_button, del_button, list_widget]() {
                             list.push_back();
                             UIBuilderVisitor visit(list_widget, false); // keep the layout of the previous
                             list.back().accept(visit);

                             if (list_widget->layout()->count() >= (int)list.max_size)
                                 add_button->setEnabled(false);
                             if (list_widget->layout()->count() > (int)list.min_size)
                                 del_button->setEnabled(true);

                         });
        QObject::connect(del_button, &QPushButton::pressed,
                         [&list, add_button, list_widget, del_button]() {
                             if (!list.empty())
                             {
                                 list.pop_back();
                                 assert(list_widget->layout()->count() > 0);
                                 QLayoutItem* child = list_widget->layout()->takeAt(list_widget->layout()->count()-1);
                                 delete child->widget();
                                 delete child;
                                 if (list_widget->layout()->count() <= (int)list.min_size) // no more items to delete, grey the button out
                                     del_button->setEnabled(false);
                                 if (list_widget->layout()->count() < (int)list.max_size)
183
                                     add_button->setEnabled(true);
184
185
                             }
                         });
186
187
        // J'arrive pas à l'afficher
        // QObject::connect(widgetGridNeighborhoodButton, &QPushButton::pressed);
188
189
190
191
192
193
194
195
196
    }
}

PropertySaverVisitor::PropertySaverVisitor()
{
    // create the top level json object
    push_object();
}

197
QJsonObject PropertySaverVisitor::save()
198
{
199
    return current().toJsonObject();
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
}

QVariant &PropertySaverVisitor::current()
{
    assert(!m_current_hierarchy.empty());
    return m_current_hierarchy.top();
}

void PropertySaverVisitor::save_value(Property &prop, const QJsonValue &val)
{
    if (current().canConvert<QJsonArray>())
    {
        QJsonArray arr = current().toJsonArray();
        arr.append(val);
        current() = arr;
    }
    else
    {
        QJsonObject obj = current().toJsonObject();
219
        obj[QString::fromStdString(prop.identifier())] = val;
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
        current() = obj;
    }
}

void PropertySaverVisitor::push_object()
{
    m_current_hierarchy.push(QJsonObject{});
}

void PropertySaverVisitor::push_array()
{
    m_current_hierarchy.push(QJsonArray{});
}

QJsonValue PropertySaverVisitor::pop_value()
{
    QJsonValue val;
    if (current().canConvert<QJsonArray>())
        val = QJsonValue(current().toJsonArray());
    else
        val = QJsonValue(current().toJsonObject());

    m_current_hierarchy.pop();
    return val;
}

void PropertySaverVisitor::visit(StringProperty &prop)
{
    save_value(prop, QJsonValue(QString::fromStdString(prop.str)));
}

void PropertySaverVisitor::visit(IntegerProperty &prop)
{
    save_value(prop, QJsonValue(prop.val));
}

void PropertySaverVisitor::visit(CoordinateProperty &prop)
{
    QJsonArray arr;
259
    arr.append(prop.c.x); arr.append(prop.c.y);
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    save_value(prop, QJsonValue(arr));
}

void PropertySaverVisitor::visit(PropertyList &list)
{
    push_array();

    for (const auto& ptr : list.contents)
        ptr->accept(*this);

    QJsonValue array = pop_value();
    save_value(list, array);
}

274
PropertyLoaderVisitor::PropertyLoaderVisitor(const QJsonObject& root)
275
{
276
    m_current_hierarchy.push(root);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
}

QJsonValue &PropertyLoaderVisitor::current()
{
    assert(!m_current_hierarchy.empty());
    return m_current_hierarchy.top();
}

void PropertyLoaderVisitor::enter_value(const QJsonValue &value)
{
    m_current_hierarchy.push(value);
}

QJsonValue PropertyLoaderVisitor::exit_value()
{
    QJsonValue val = m_current_hierarchy.top();
    m_current_hierarchy.pop();
    return val;
}

QJsonValue PropertyLoaderVisitor::fetch_value(Property &prop)
{
    // in the case of an array, fetch the front value
    if (current().isArray())
    {
        QJsonArray arr = current().toArray();
        QJsonValue val = arr.first();
        arr.pop_front();
        current() = arr;
        return val;
    }
    else
309
        return current()[prop.identifier().c_str()];
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
}

void PropertyLoaderVisitor::visit(StringProperty &prop)
{
    QJsonValue val = fetch_value(prop);
    if (val.isString())
    {
        prop.str = val.toString().toStdString();
    }
}

void PropertyLoaderVisitor::visit(IntegerProperty &prop)
{
    QJsonValue val = fetch_value(prop);
    if (val.isDouble())
    {
        prop.val = val.toInt();
        if (prop.val > prop.prop_max || prop.val < prop.prop_min)
        {
329
            throw PropertyVisitorException("Invalid property value");
330
331
332
333
334
335
336
337
338
        }
    }
}

void PropertyLoaderVisitor::visit(CoordinateProperty &prop)
{
    QJsonValue val = fetch_value(prop);
    if (val.isArray() && val.toArray().size() == 2)
    {
339
340
        prop.c.x = val.toArray()[0].toInt();
        prop.c.y = val.toArray()[1].toInt();
341
342
343
344
345
346
347
348
349
350
351
352
    }
}


void PropertyLoaderVisitor::visit(PropertyList &prop)
{
    QJsonValue val = fetch_value(prop);
    if (val.isArray())
    {
        enter_value(val);

        if (!prop.dynamic && val.toArray().size() != (int)prop.contents.size())
353
            throw PropertyVisitorException("Invalid array size");
354
355
356
357
358
359
360
361
362
363
364

        prop.clear();
        for (int i = 0; i < val.toArray().size(); ++i)
        {
            Property& back = prop.push_back();
            back.accept(*this);
        }

        exit_value();
    }
}