propertyvisitors.cpp 9.94 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
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

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)
{
    QGroupBox* box = new QGroupBox(QString::fromStdString(prop.name()), current_widget());
    box->setLayout(new QVBoxLayout);
    add_widget("", box);
    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());
    add_widget(str.name(), line);

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

void UIBuilderVisitor::visit(IntegerProperty &prop)
{
    QSpinBox* spin = new QSpinBox(current_widget());
    add_widget(prop.name(), spin);

    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));
    add_widget(prop.name(), frame);

117
    spin_x->setValue(prop.c.x); spin_y->setValue(prop.c.y);
118
    QObject::connect(spin_x, QOverload<int>::of(&QSpinBox::valueChanged),
119
                     [&prop](int i) { prop.c.x = i; });
120
    QObject::connect(spin_y, QOverload<int>::of(&QSpinBox::valueChanged),
121
                     [&prop](int i) { prop.c.y = i; });
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
}

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());

        frame->layout()->addWidget(add_button);
        frame->layout()->addWidget(del_button);
        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)
                                    add_button->setEnabled(true);
                             }
                         });
    }
}

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

186
QJsonObject PropertySaverVisitor::save()
187
{
188
    return current().toJsonObject();
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
}

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();
        obj[QString::fromStdString(prop.name())] = val;
        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;
248
    arr.append(prop.c.x); arr.append(prop.c.y);
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    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);
}

263
PropertyLoaderVisitor::PropertyLoaderVisitor(const QJsonObject& root)
264
{
265
    m_current_hierarchy.push(root);
266
267
268
269
270
271
272
273
274
275
276
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
309
310
311
312
313
314
315
316
317
}

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
        return current()[prop.name().c_str()];
}

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)
        {
318
            throw propertyvisitorsException("Invalid property value");
319
320
321
322
323
324
325
326
327
        }
    }
}

void PropertyLoaderVisitor::visit(CoordinateProperty &prop)
{
    QJsonValue val = fetch_value(prop);
    if (val.isArray() && val.toArray().size() == 2)
    {
328
329
        prop.c.x = val.toArray()[0].toInt();
        prop.c.y = val.toArray()[1].toInt();
330
331
332
333
334
335
336
337
338
339
340
341
    }
}


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())
342
            throw propertyvisitorsException("Invalid array size");
343
344
345
346
347
348
349
350
351
352
353

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

        exit_value();
    }
}