summaryrefslogtreecommitdiffstats
path: root/src/qml/qmlcontrolproxy.h
blob: 3bbf3435cb6ed6676a7dfc6ffce660394e95638d (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
#pragma once

#include <QObject>
#include <QQmlParserStatus>
#include <QtQml>
#include <memory>

#include "control/controlproxy.h"

namespace mixxx {
namespace qml {

class QmlControlProxy : public QObject, public QQmlParserStatus {
    Q_OBJECT
    Q_INTERFACES(QQmlParserStatus)
    // TODO: The REQUIRED flag only exists in Qt 5.14 and later. Once we
    // require that as minimum dependency, add it to the group and key
    // properties.
    Q_PROPERTY(QString group READ getGroup WRITE setGroup NOTIFY groupChanged)
    Q_PROPERTY(QString key READ getKey WRITE setKey NOTIFY keyChanged)
    Q_PROPERTY(QString keyValid READ isKeyValid NOTIFY keyValidChanged)
    Q_PROPERTY(QString initialized READ isInitialized NOTIFY initializedChanged)
    Q_PROPERTY(double value READ getValue WRITE setValue NOTIFY valueChanged)
    Q_PROPERTY(double parameter READ getParameter WRITE setParameter NOTIFY parameterChanged)

  public:
    explicit QmlControlProxy(QObject* parent = nullptr);

    /// Implementing the QQmlParserStatus interface requires overriding this
    /// method, but we don't need it.
    // Invoked after class creation, but before any properties have been set.
    void classBegin() override{};

    /// QML cannot pass arguments to C++ constructors so this class needs to
    /// rely on the QML object setting the group and key properties to
    /// initialize the ControlProxy. We want to deplay the initialization of
    /// the underlying ControlProxy until the object has been fully created and
    /// all properties (group and key in particular) have been set.  Perform
    /// some initialization here now that the object is fully created.
    void componentComplete() override;

    void setGroup(const QString& group);
    const QString& getGroup() const;

    void setKey(const QString& key);
    const QString& getKey() const;

    void setValue(double newValue);
    double getValue() const;

    void setParameter(double newValue);
    double getParameter() const;

    bool isKeyValid() const;
    bool isInitialized() const;

    /// Reset the control to the default value.
    Q_INVOKABLE void reset();

  signals:
    void groupChanged(const QString& group);
    void keyChanged(const QString& key);
    void keyValidChanged(bool valid);
    void initializedChanged(bool initialized);
    void valueChanged(double newValue);
    void parameterChanged(double newParameter);

  private slots:
    /// Emits both the valueChanged and parameterChanged signals
    void slotControlProxyValueChanged(double newValue);

  private:
    /// (Re-)Initializes or resets the pointer to the underlying control proxy.
    /// Called for the first time when component construction has been
    /// completed. From that moment on, it's called whenever the group or key
    /// changes.
    void reinitializeFromKey();

    ConfigKey m_coKey;

    /// Set to true in the componentComplete() method, which is called when the
    /// QML object creation is complete.
    bool m_isComponentComplete;
    std::unique_ptr<ControlProxy> m_pControlProxy;
};

} // namespace qml
} // namespace mixxx