aboutsummaryrefslogtreecommitdiffstats
path: root/tests/tst_qtdotnet/foo.cpp
blob: 8c8c3d3f9d86e260b056fdd1af733422f1698c09 (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
/***************************************************************************************************
 Copyright (C) 2023 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

#include "foo.h"

#include <qdotnetevent.h>

struct FooPrivate final : QDotNetObject::IEventHandler
{
    Foo *q;
    FooPrivate(Foo *q) : q(q) {}

    QDotNetFunction<Foo, IBarTransformation> ctor = nullptr;

    QDotNetFunction<QString> bar;
    QDotNetFunction<void, QString> setBar;

    void handleEvent(const QString &eventName, QDotNetObject &sender, QDotNetObject &args) override
    {
        if (eventName != "PropertyChanged")
            return;

        if (args.type().fullName() != QDotNetPropertyEvent::FullyQualifiedTypeName)
            return;

        const auto propertyChangedEvent = args.cast<QDotNetPropertyEvent>();
        if (propertyChangedEvent.propertyName() == "Bar")
            emit q->barChanged();
    }
};

Q_DOTNET_OBJECT_IMPL(Foo, Q_DOTNET_OBJECT_INIT(d(new FooPrivate(this))));

Foo::Foo() : d(new FooPrivate(this))
{
    const auto ctor = constructor<Foo, Null<IBarTransformation>>();
    *this = ctor(nullptr);
    subscribeEvent("PropertyChanged", d);
}

Foo::Foo(const IBarTransformation &transformation) : d(new FooPrivate(this))
{
    *this = constructor(d->ctor).invoke(*this, transformation);
    subscribeEvent("PropertyChanged", d);
}

Foo::~Foo()
{
    delete d;
}

QString Foo::bar() const
{
    return method("get_Bar", d->bar).invoke(*this);
}

void Foo::setBar(const QString &value)
{
    method("set_Bar", d->setBar).invoke(*this, value);
}

IBarTransformation::IBarTransformation() : QDotNetInterface(FullyQualifiedTypeName)
{
    setCallback<QString, QString>("Transform", { QDotNetParameter::String, UnmanagedType::LPWStr },
        [this](const QString &bar) {
            return transform(bar);
        });
}