aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/ivigenerator/templates_control_panel/interface.cpp.tpl
blob: afb5f5d7834f67ecbd7931e74c89258cf3a93df3 (plain)
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
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
248
249
250
251
252
253
254
255
256
257
258
259
{#
# Copyright (C) 2018 Pelagicore AG.
# Contact: https://www.qt.io/licensing/
#
# This file is part of the QtIvi module of the Qt Toolkit.
#
# $QT_BEGIN_LICENSE:LGPL-QTAS$
# Commercial License Usage
# Licensees holding valid commercial Qt Automotive Suite licenses may use
# this file in accordance with the commercial license agreement provided
# with the Software or, alternatively, in accordance with the terms
# contained in a written agreement between you and The Qt Company.  For
# licensing terms and conditions see https://www.qt.io/terms-conditions.
# For further information use the contact form at https://www.qt.io/contact-us.
#
# GNU Lesser General Public License Usage
# Alternatively, this file may be used under the terms of the GNU Lesser
# General Public License version 3 as published by the Free Software
# Foundation and appearing in the file LICENSE.LGPL3 included in the
# packaging of this file. Please review the following information to
# ensure the GNU Lesser General Public License version 3 requirements
# will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 2.0 or (at your option) the GNU General
# Public license version 3 or any later version approved by the KDE Free
# Qt Foundation. The licenses are as published by the Free Software
# Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-2.0.html and
# https://www.gnu.org/licenses/gpl-3.0.html.
#
# $QT_END_LICENSE$
#
# SPDX-License-Identifier: LGPL-3.0
#}
{% import 'qtivi_macros.j2' as ivi %}
{% set class = '{0}'.format(interface) %}
{% set interface_zoned = interface.tags.config and interface.tags.config.zoned %}
{% include 'generated_comment.cpp.tpl' %}

#include "{{class|lower}}.h"

#include <QQmlEngine>
#include <QDebug>
#include <QtSimulator>

namespace {
const QString INITIAL_MAIN_ZONE = "MainZone";
}

QT_BEGIN_NAMESPACE

{% if interface.tags.config.zoned %}
{{class}}::{{class}}(const QString &zone, QObject *parent)
    : QObject(parent)
    {% for property in interface.properties %}
    , m_{{ property }}({{property|default_value}})
    {% endfor %}
    , m_currentZone(zone)
{
    if (!zone.isEmpty()) {
        return;
    }

    //Add ourself to the available zones to make it available to the UI and still keep the code clean
    m_zoneHash.insert(INITIAL_MAIN_ZONE, this);
    m_zoneMap.insert(INITIAL_MAIN_ZONE, QVariant::fromValue(this));

    {% set zones = interface.tags.config_simulator.zones if interface.tags.config_simulator else {} %}
    {% for zone_name, zone_id in zones.items() %}
    addZone(QLatin1String("{{zone_id}}"));
    {%   for property in interface.properties %}
    {%     if property.tags.config_simulator and property.tags.config_simulator.zoned %}
    m_zoneHash[QLatin1String("{{zone_id}}")]->m_{{property}} = {{property|default_value(zone_name)}};
    {%     endif %}
    {%   endfor %}
    {% endfor %}
{% else %}
{{class}}::{{class}}(QObject *parent)
    : QObject(parent)
{
{% endif %}
    QVariantMap peerInfo{{ '{{' }}"control_panel",""{{ '}}' }};
    QSimulatorServer *server = {{module.module_name}}Module::simulationServer();
    server->registerServer("{{interface}}", QVersionNumber(1, 0, 0), peerInfo, [=](QSimulatorConnectionWorker *client) {
        m_worker = client;
        connect(m_worker, &QSimulatorConnectionWorker::disconnected, this, [=]() {
            qDebug() << "DISCONNECTED";
            //Deleting it on the mainZone is enough as QPointer does the rest of the work for us.
            m_worker = 0;
        });
        m_worker->addReceiver(this);
        qDebug() << "connected: " << client->peerInfo();
{% if interface.tags.config.zoned %}
        auto i = m_zoneHash.constBegin();
        while (i != m_zoneHash.constEnd()) {
            i.value()->m_worker = client;
{%   for property in interface.properties %}
{%     set function_name = property|setter_name %}
{%     if property.readonly or property.const %}
{%       set function_name = property.name + 'Changed' %}
{%     endif %}
            m_worker->call("{{function_name}}", i.value()->m_{{property}}, i.value()->m_currentZone);
{%   endfor %}
            ++i;
        }
{% else %}
{%   for property in interface.properties %}
{%     set function_name = property|setter_name %}
{%     if property.readonly or property.const %}
{%       set function_name = property.name + 'Changed' %}
{%     endif %}
        m_worker->call("{{function_name}}", m_{{property}});
{%   endfor %}
{% endif %}
    });
}

{{class}}::~{{class}}()
{
}

void {{class}}::registerQmlTypes(const QString& uri, int majorVersion, int minorVersion, const QString& qmlName)
{
{% if 'singleton' in interface.tags %}
    qmlRegisterSingletonType<{{class}}>(uri.toLatin1(), majorVersion, minorVersion, "{{interface}}", {{class|lower}}_singletontype_provider);
{% else %}
    qmlRegisterType<{{class}}>(uri.toLatin1(), majorVersion, minorVersion, qmlName.toLatin1());
{% endif %}
}

{% if interface.tags.config.zoned %}
void {{class}}::addZone(const QString &newZone)
{
    if (!m_currentZone.isEmpty()) {
        qWarning("Adding a new zone is only possible from the root zone.");
        return;
    }

    if (m_zoneHash.contains(newZone))
        return;

    {{class}} *zoneObject = new {{class}}(newZone, this);
    zoneObject->m_worker = m_worker;
    m_zoneHash.insert(newZone, zoneObject);
    m_zoneMap.insert(newZone, QVariant::fromValue(zoneObject));

    emit zonesChanged();
}

QStringList {{class}}::zones() const
{
     return m_zoneMap.keys();
}


QString {{class}}::currentZone() const
{
    return m_currentZone;
}

QVariantMap {{class}}::zoneAt() const
{
    return m_zoneMap;
}
{% endif %}

{% for property in interface.properties %}
{{ivi.prop_getter(property, class)}}
{
   return m_{{property}};
}

{{ivi.prop_setter(property, class)}}
{
   if (m_{{property}} == {{property}})
       return;
    m_{{property}} = {{property}};
    emit {{property}}Changed({{property}});
    auto w = worker();
    if (w)
{% set function_name = property|setter_name %}
{% if property.readonly or property.const %}
{%   set function_name = property.name + 'Changed' %}
{% endif %}
{% if interface.tags.config.zoned %}
        w->call("{{function_name}}", {{property}}, m_currentZone);
{% else %}
        w->call("{{function_name}}", {{property}});
{% endif %}
}

{% if interface.tags.config.zoned %}
{{ivi.prop_setter(property, class, true)}}
{
    QString z = zone;
    if (z.isEmpty())
        z = INITIAL_MAIN_ZONE;

    if (!m_zoneMap.contains(z)) {
        return;
    }

    // the setter cannot be used as it would update the simulation connection
    // and by this create a async loop
    if (m_zoneHash[z]->m_{{property}} == {{property}})
        return;
     m_zoneHash[z]->m_{{property}} = {{property}};
     emit m_zoneHash[z]->{{property}}Changed({{property}});
}
{% endif %}

{% endfor %}

{% for signal in interface.signals %}
{{ivi.signal(signal, class)}}
{
    auto w = worker();
    if (w)
        w->call("{{signal}}", {{signal.parameters|join(', ')}}{% if interface.tags.config.zoned %}, m_currentZone{% endif %});
}

{% endfor %}

{% if interface_zoned %}
{%   for operation in interface.operations %}
void {{class}}::{{operation}}({{ivi.join_params(operation, zoned=true)}})
{
    QString z = zone;
    if (z.isEmpty())
        z = INITIAL_MAIN_ZONE;

    if (!m_zoneMap.contains(z)) {
        return;
    }

    emit m_zoneHash[z]->{{operation}}({{operation.parameters|join(', ')}});
}
{%   endfor %}
{% endif %}

QSimulatorConnectionWorker *{{class}}::worker()
{
{% if interface.tags.config.zoned %}
    if (m_currentZone.isEmpty())
        return m_worker;
    {{class}}* globalZone = qobject_cast<{{class}}*>(parent());
    if (globalZone)
        return globalZone->m_worker;
    return nullptr;
{% else %}
    return m_worker;
{% endif %}
}

QT_END_NAMESPACE