aboutsummaryrefslogtreecommitdiffstats
path: root/src/ivicore/qiviabstractfeaturelistmodel.cpp
blob: 056ccdfc78c925050800c573d048b14944d7e1c0 (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
260
261
262
263
264
265
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2019 Luxoft Sweden AB
** 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$
** Commercial License Usage
** Licensees holding valid commercial Qt 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$
**
****************************************************************************/

#include "qiviabstractfeaturelistmodel.h"
#include "qiviabstractfeaturelistmodel_p.h"

QT_BEGIN_NAMESPACE

QIviHelperFeature::QIviHelperFeature(const QString &interfaceName, QIviAbstractFeatureListModel *model)
    : QIviAbstractFeature(interfaceName)
    , m_model(model)
{
}

bool QIviHelperFeature::acceptServiceObject(QIviServiceObject *so)
{
    return m_model->acceptServiceObject(so);
}

bool QIviHelperFeature::acceptServiceObjectDefaultImpl(QIviServiceObject *so)
{
    return QIviAbstractFeature::acceptServiceObject(so);
}

void QIviHelperFeature::connectToServiceObject(QIviServiceObject *so)
{
    m_model->connectToServiceObject(so);
}

void QIviHelperFeature::connectToServiceObjectDefaultImpl(QIviServiceObject *so)
{
    QIviAbstractFeature::connectToServiceObject(so);
}

void QIviHelperFeature::disconnectFromServiceObject(QIviServiceObject *so)
{
    m_model->disconnectFromServiceObject(so);
}

void QIviHelperFeature::disconnectFromServiceObjectDefaultImpl(QIviServiceObject *so)
{
    QIviAbstractFeature::disconnectFromServiceObject(so);
}

void QIviHelperFeature::clearServiceObject()
{
    m_model->clearServiceObject();
}

QIviAbstractFeaturePrivate *QIviHelperFeature::iviPrivate()
{
    Q_D(QIviAbstractFeature);
    return d;
}

const QIviAbstractFeaturePrivate *QIviHelperFeature::iviPrivate() const
{
    Q_D(const QIviAbstractFeature);
    return d;
}

QIviAbstractFeatureListModelPrivate::QIviAbstractFeatureListModelPrivate(const QString &interfaceName, QIviAbstractFeatureListModel *model)
    : QAbstractItemModelPrivate()
    , m_feature(new QIviHelperFeature(interfaceName, model))
    , m_qmlCreation(false)
{

}

QIviAbstractFeatureListModelPrivate::~QIviAbstractFeatureListModelPrivate()
{
    delete m_feature;
}

void QIviAbstractFeatureListModelPrivate::initialize()
{
}

QIviFeatureInterface *QIviAbstractFeatureListModelPrivate::backend() const
{
    return m_feature->iviPrivate()->backend();
}

/*!
    \class QIviAbstractFeatureListModel
    \inmodule QtIviCore
    \brief The QIviAbstractFeatureListModel is the base class for QtIvi Features which should act as a model.

    This base class is necessary to avoid virtual inheritance from QAbstractListModel and
    QIviAbstractFeature.

    For more details on how a Feature works, see QIviAbstractFeature.
    For more information about models in QtIvi, see \l{Models}.

    \section1 Subclassing

    Your QIviAbstractFeatureListModel subclass must provide implementations for all virtual
    functions from QIviAbstractFeature as well as the virtual functions from QAbstractListModel.

    \list
        \li For more details on how to integrate with the \l{Dynamic Backend System}, see
            \l{Write a Subclass}{QIviAbstractFeature}.
        \li For more details on what you need to do to provide the model's required
            functionality, see \l{QAbstractListModel - Subclassing}.
        \li For a class that implements all the necessary QIviAbstractFeatureListModel functions
            to provide pagination functionality, see QIviPagingModel.
    \endlist

*/

/*!
    \qmltype AbstractFeatureListModel
    \instantiates QIviAbstractFeatureListModel
    \inqmlmodule QtIvi

    \brief The QIviAbstractFeatureListModel is the base class for QtIvi Features which should act as a model.

    This base class is necessary to avoid virtual inheritance from QAbstractListModel and
    QIviAbstractFeature.

    This element is not directly accessible from QML. It provides the base QML properties for the
    feature, like autoDiscovery and isValid.

    For more details on how a Feature works, see QIviAbstractFeature.
    For more information about models in QtIvi, see \l{Models}.

    \sa AbstractFeature
*/

/*!
    \fn void QIviAbstractFeatureListModel::clearServiceObject()

    This method is expected to be implemented by any class subclassing QIviAbstractFeatureListModel.

    Called when no service object is available. The implementation is expected to set all
    properties to safe defaults and forget all links to the previous service object.

    \note You must emit the corresponding change signals for these properties, so that the feature
    is informed about the state change. This makes it possible for the implemented class to connect
    to a new service object afterwards.

    There is no need to disconnect from the service object. If it still exists, it is guaranteed
    that \l disconnectFromServiceObject is called first.

    \sa acceptServiceObject(), connectToServiceObject(), disconnectFromServiceObject()
*/

/*!
    Constructs a QIviAbstractFeatureListModel.

    The \a parent argument is passed on to the \l QAbstractListModel base class.

    The \a interfaceName argument is used to locate suitable service objects.
*/
QIviAbstractFeatureListModel::QIviAbstractFeatureListModel(const QString &interfaceName, QObject *parent)
    : QAbstractListModel(*new QIviAbstractFeatureListModelPrivate(interfaceName, this), parent)
{
    Q_D(QIviAbstractFeatureListModel);
    d->initialize();
    connect(d->m_feature, &QIviAbstractFeature::serviceObjectChanged, this, &QIviAbstractFeatureListModel::serviceObjectChanged);
    connect(d->m_feature, &QIviAbstractFeature::discoveryModeChanged, this, &QIviAbstractFeatureListModel::discoveryModeChanged);
    connect(d->m_feature, &QIviAbstractFeature::discoveryResultChanged, this, &QIviAbstractFeatureListModel::discoveryResultChanged);
    connect(d->m_feature, &QIviAbstractFeature::isValidChanged, this, &QIviAbstractFeatureListModel::isValidChanged);
    connect(d->m_feature, &QIviAbstractFeature::isInitializedChanged, this, &QIviAbstractFeatureListModel::isInitializedChanged);
    connect(d->m_feature, &QIviAbstractFeature::errorChanged, this, &QIviAbstractFeatureListModel::errorChanged);
}

/*!
    \qmlproperty ServiceObject AbstractFeatureListModel::serviceObject
    \brief Sets the service object for the feature.

    As features only expose the front API facing the developer, a service object implementing the
    actual function is required. This is usually retrieved through the auto discovery mechanism.

    The setter for this property returns false if the \e {Service Object} is already set to exactly this instance
    or the \e {Service Object} doesn't get accepted by the feature.

    \sa discoveryMode
*/

/*!
    \property QIviAbstractFeatureListModel::serviceObject
    \brief Sets the service object for the feature.

    As features only expose the front API facing the developer, a service object implementing the
    actual function is required. This is usually retrieved through the auto discovery mechanism.

    The setter for this property returns false if the \e {Service Object} is already set to exactly this instance
    or the \e {Service Object} doesn't get accepted by the feature.

    \sa discoveryMode
*/
QIviServiceObject *QIviAbstractFeatureListModel::serviceObject() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->serviceObject();
}

/*!
    \qmlproperty enumeration AbstractFeatureListModel::discoveryMode
    \brief Holds the mode that is used for the autoDiscovery

    Available values are:
    \value NoAutoDiscovery
           No auto discovery is done and the ServiceObject needs to be set manually.
    \value AutoDiscovery
           Tries to find a production backend with a matching interface and falls back to a simulation backend if not found.
    \value LoadOnlyProductionBackends
           Only tries to load a production backend with a matching interface.
    \value LoadOnlySimulationBackends
           Only tries to load a simulation backend with a matching interface.

    If needed the auto discovery will be started once the Feature creation is completed.

    \note If you change this property after the Feature is instantiated you need to call startAutoDiscovery() to search for
    a new Service Object
*/

/*!
    \property QIviAbstractFeatureListModel::discoveryMode
    \brief Holds the mode that is used for the autoDiscovery

    \note If you change this property after the Feature is instantiated you need to call startAutoDiscovery() to search for
    a new Service Object
*/
QIviAbstractFeature::DiscoveryMode QIviAbstractFeatureListModel::discoveryMode() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->discoveryMode();
}

/*!
    \qmlproperty enumeration AbstractFeatureListModel::discoveryResult
    \brief The result of the last autoDiscovery attempt

    Available values are:
    \value NoResult
           Indicates that no auto discovery was started because the feature has already assigned a valid ServiceObject.
    \value ErrorWhileLoading
           An error has happened while searching for a backend with a matching interface.
    \value ProductionBackendLoaded
           As a result of the auto discovery a production backend was loaded.
    \value SimulationBackendLoaded
           As a result of the auto discovery a simulation backend was loaded.
*/

/*!
    \property QIviAbstractFeatureListModel::discoveryResult
    \brief The result of the last autoDiscovery attempt

    \sa startAutoDiscovery()
*/
QIviAbstractFeature::DiscoveryResult QIviAbstractFeatureListModel::discoveryResult() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->discoveryResult();
}

/*!
    \qmlproperty bool AbstractFeatureListModel::isValid
    \brief Indicates whether the feature is ready for use.

    The property is \c true if the feature is ready to be used, otherwise \c false. Not being
    ready usually indicates that no suitable service object could be found, or that automatic
    discovery has not been triggered.

    The backend still might not have sent all properties yet and is not fully initialized.
    Use isInitialized instead to know when the feature holds all correct values.

    \sa QIviServiceObject, discoveryMode, isInitialized
*/
/*!
    \property QIviAbstractFeatureListModel::isValid
    \brief Indicates whether the feature is ready to use.

    The property is \c true if the feature is ready to be used, otherwise \c false. Not being
    ready usually indicates that no suitable service object could be found, or that automatic
    discovery has not been triggered.

    The backend still might not have sent all properties yet and is not fully initialized.
    Use isInitialized instead to know when the feature holds all correct values.

    \sa QIviServiceObject, discoveryMode, isInitialized
*/
bool QIviAbstractFeatureListModel::isValid() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->isValid();
}

/*!
    \qmlproperty bool AbstractFeatureListModel::isInitialized
    \brief Indicates whether the feature has been initialized with all the values from the backend.

    The property is \c true once the backend sends the QIviFeatureInterface::initializationDone signal
    to indicate that all values have now been initialized with values from the backend.

    \sa isValid, QIviFeatureInterface::initializationDone
*/
/*!
    \property QIviAbstractFeatureListModel::isInitialized
    \brief Indicates whether the feature has been initialized with all the values from the backend.

    The property is \c true once the backend sends the QIviFeatureInterface::initializationDone signal
    to indicate that all values have now been initialized with values from the backend.

    \sa isValid, QIviFeatureInterface::initializationDone
*/
bool QIviAbstractFeatureListModel::isInitialized() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->isInitialized();
}

/*!
    Returns the last error code.

    \sa QIviAbstractFeature::Error
*/
QIviAbstractFeature::Error QIviAbstractFeatureListModel::error() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->error();
}

/*!
    \qmlproperty string AbstractFeatureListModel::error

    Last error message of the feature. Empty if no error.
*/
/*!
    \property QIviAbstractFeatureListModel::error

    Last error message of the feature. Empty if no error.
*/
QString QIviAbstractFeatureListModel::errorMessage() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->errorMessage();
}

bool QIviAbstractFeatureListModel::setServiceObject(QIviServiceObject *so)
{
    Q_D(QIviAbstractFeatureListModel);
    return d->m_feature->setServiceObject(so);
}

void QIviAbstractFeatureListModel::setDiscoveryMode(QIviAbstractFeature::DiscoveryMode discoveryMode)
{
    Q_D(QIviAbstractFeatureListModel);
    d->m_feature->setDiscoveryMode(discoveryMode);
}

/*!
    \qmlmethod enumeration AbstractFeatureListModel::startAutoDiscovery()

    Performs an automatic discovery attempt.

    See AbstractFeature::startAutoDiscovery() for more information
*/

/*!
    \brief Performs an automatic discovery attempt.
*/
QIviAbstractFeature::DiscoveryResult QIviAbstractFeatureListModel::startAutoDiscovery()
{
    Q_D(QIviAbstractFeatureListModel);
    return d->m_feature->startAutoDiscovery();
}

/*!
    \internal
*/
QIviAbstractFeatureListModel::QIviAbstractFeatureListModel(QIviAbstractFeatureListModelPrivate &dd, QObject *parent)
    : QAbstractListModel(dd, parent)
{
    Q_D(QIviAbstractFeatureListModel);
    d->initialize();
    connect(d->m_feature, &QIviAbstractFeature::serviceObjectChanged, this, &QIviAbstractFeatureListModel::serviceObjectChanged);
    connect(d->m_feature, &QIviAbstractFeature::discoveryModeChanged, this, &QIviAbstractFeatureListModel::discoveryModeChanged);
    connect(d->m_feature, &QIviAbstractFeature::discoveryResultChanged, this, &QIviAbstractFeatureListModel::discoveryResultChanged);
    connect(d->m_feature, &QIviAbstractFeature::isValidChanged, this, &QIviAbstractFeatureListModel::isValidChanged);
    connect(d->m_feature, &QIviAbstractFeature::isInitializedChanged, this, &QIviAbstractFeatureListModel::isInitializedChanged);
    connect(d->m_feature, &QIviAbstractFeature::errorChanged, this, &QIviAbstractFeatureListModel::errorChanged);
}

/*!
    This method is expected to be implemented by any class subclassing QIviAbstractFeature.

    The method should return \c true if the given \a serviceObject is accepted and
    can be used, otherwise \c false.

    If the object is accepted, \l connectToServiceObject is called to actually connect to the
    service object.

    The default implementation accepts the \a serviceObject if it implements the interface
    returned by interfaceName();

    \sa connectToServiceObject(), disconnectFromServiceObject(), clearServiceObject()
*/
bool QIviAbstractFeatureListModel::acceptServiceObject(QIviServiceObject *serviceObject)
{
    Q_D(QIviAbstractFeatureListModel);
    return d->m_feature->acceptServiceObjectDefaultImpl(serviceObject);
}

/*!
    This method is expected to be implemented by any class subclassing QIviAbstractFeature.

    The implementation should connect to the \a serviceObject, and set up all
    properties to reflect the state of the service object.

    There is no previous service object connected, as this function call is always preceded by a call to
    \l disconnectFromServiceObject or \l clearServiceObject.

    It is safe to assume that the \a serviceObject, has always been accepted through the
    \l acceptServiceObject method prior to being passed to this method.

    The default implementation connects to the signals offered by QIviFeatureInterface and calls
    QIviFeatureInterface::initialize() afterwards.

    When reimplementing please keep in mind to connect all signals before calling this function. e.g.

    /code
    void SimpleFeature::connectToServiceObject(QIviServiceObject *serviceObject)
    {
         SimpleFeatureBackendInterface *backend = backend(serviceObject);
         if (!backend)
             return;

         // connect your signals
         connect(backend, &SimpleFeatureBackendInterface::propertyChanged,
                 this, &SimpleFeature::onPropertyChanged);

         // connects the base signals and call initialize()
         QIviAbstractFeature::connectToServiceObject(serviceObject);

         // Additional initialization functions can be added here
    }
    /endcode

    \sa acceptServiceObject(), disconnectFromServiceObject(), clearServiceObject()
*/
void QIviAbstractFeatureListModel::connectToServiceObject(QIviServiceObject *serviceObject)
{
    Q_D(QIviAbstractFeatureListModel);
    d->m_feature->connectToServiceObjectDefaultImpl(serviceObject);
}

/*!
    This method is expected to be implemented by any class subclassing QIviAbstractFeature.

    The implementation should disconnect all connections to the \a serviceObject.

    There is no need to reset internal variables to safe defaults. A call to this function is
    always followed by a call to \l connectToServiceObject or \l clearServiceObject.

    The default implementation disconnects all signals from the serviceObject to this instance.

    \sa acceptServiceObject(), connectToServiceObject(), clearServiceObject()
*/
void QIviAbstractFeatureListModel::disconnectFromServiceObject(QIviServiceObject *serviceObject)
{
    Q_D(QIviAbstractFeatureListModel);
    d->m_feature->disconnectFromServiceObjectDefaultImpl(serviceObject);
}

/*!
    \internal
*/
void QIviAbstractFeatureListModel::classBegin()
{
    Q_D(QIviAbstractFeatureListModel);
    d->m_qmlCreation = true;
}

/*!
    \internal
*/
void QIviAbstractFeatureListModel::componentComplete()
{
    Q_D(QIviAbstractFeatureListModel);
    d->m_qmlCreation = false;
    startAutoDiscovery();
}

/*!
    Returns the interface name this Feature expect to be available from the Service Object and this Feature is implementing.

    See \l {Extending Qt IVI} for more information.
*/
QString QIviAbstractFeatureListModel::interfaceName() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->interfaceName();
}

/*!
    Returns the current error code converted from QIviAbstractFeature::Error to QString

    \sa error
*/
QString QIviAbstractFeatureListModel::errorText() const
{
    Q_D(const QIviAbstractFeatureListModel);
    return d->m_feature->errorText();
}

/*!
    Sets \a error with the \a message.

    Emits errorChanged() signal.

    \sa QIviAbstractFeature::Error
*/
void QIviAbstractFeatureListModel::setError(QIviAbstractFeature::Error error, const QString &message)
{
    Q_D(QIviAbstractFeatureListModel);
    d->m_feature->setError(error, message);
}

/*!
    Updates \a error and \a message from the backend.

    This slot can be used when implementing a new Feature to report generic errors.
*/
void QIviAbstractFeatureListModel::onErrorChanged(QIviAbstractFeature::Error error, const QString &message)
{
    setError(error, message);
}

QT_END_NAMESPACE