diff options
Diffstat (limited to 'examples/corelib/bindableproperties/bindablesubscription')
7 files changed, 264 insertions, 0 deletions
diff --git a/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt b/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt new file mode 100644 index 0000000000..2734f44a17 --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +qt_add_executable(bindablesubscription + main.cpp + bindablesubscription.cpp + bindablesubscription.h + bindableuser.cpp + bindableuser.h +) + +target_link_libraries(bindablesubscription PRIVATE + bindableproperties_shared +) + diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp new file mode 100644 index 0000000000..32f4194635 --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp @@ -0,0 +1,51 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "bindablesubscription.h" +#include "bindableuser.h" + +//! [binding-expressions] + +BindableSubscription::BindableSubscription(BindableUser *user) : m_user(user) +{ + Q_ASSERT(user); + + m_price.setBinding( + [this] { return qRound(calculateDiscount() * int(m_duration) * basePrice()); }); + + m_isValid.setBinding([this] { + return m_user->country() != BindableUser::Country::AnyCountry && m_user->age() > 12; + }); +} + +//! [binding-expressions] + +//! [set-duration] + +void BindableSubscription::setDuration(Duration newDuration) +{ + m_duration = newDuration; +} + +//! [set-duration] + +double BindableSubscription::calculateDiscount() const +{ + switch (m_duration) { + case Monthly: + return 1; + case Quarterly: + return 0.9; + case Yearly: + return 0.6; + } + Q_UNREACHABLE_RETURN(-1); +} + +int BindableSubscription::basePrice() const +{ + if (m_user->country() == BindableUser::Country::AnyCountry) + return 0; + + return (m_user->country() == BindableUser::Country::Norway) ? 100 : 80; +} diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h new file mode 100644 index 0000000000..03870d0617 --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h @@ -0,0 +1,44 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BINDABLESUBSCRIPTION_H +#define BINDABLESUBSCRIPTION_H + +#include <QBindable> +#include <QProperty> + +class BindableUser; + +//! [bindable-subscription-class] + +class BindableSubscription +{ +public: + enum Duration { Monthly = 1, Quarterly = 3, Yearly = 12 }; + + BindableSubscription(BindableUser *user); + BindableSubscription(const BindableSubscription &) = delete; + + int price() const { return m_price; } + QBindable<int> bindablePrice() { return &m_price; } + + Duration duration() const { return m_duration; } + void setDuration(Duration newDuration); + QBindable<Duration> bindableDuration() { return &m_duration; } + + bool isValid() const { return m_isValid; } + QBindable<bool> bindableIsValid() { return &m_isValid; } + +private: + double calculateDiscount() const; + int basePrice() const; + + BindableUser *m_user; + QProperty<Duration> m_duration { Monthly }; + QProperty<int> m_price { 0 }; + QProperty<bool> m_isValid { false }; +}; + +//! [bindable-subscription-class] + +#endif // BNDABLESUBSCRIPTION_H diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.pro b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.pro new file mode 100644 index 0000000000..321a1226c4 --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.pro @@ -0,0 +1,22 @@ +QT += widgets +TARGET = bindablesubscription + +SOURCES += main.cpp \ + bindablesubscription.cpp \ + bindableuser.cpp \ + ../shared/subscriptionwindow.cpp + +target.path = $$[QT_INSTALL_EXAMPLES]/corelib/bindableproperties/bindablesubscription +INSTALLS += target + +FORMS += \ + ../shared/subscriptionwindow.ui + +HEADERS += \ + bindablesubscription.h \ + bindableuser.h \ + ../shared/subscriptionwindow.h + +RESOURCES += \ + ../shared/countries.qrc + diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.cpp b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.cpp new file mode 100644 index 0000000000..9cc3b7a4a6 --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.cpp @@ -0,0 +1,18 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "bindableuser.h" + +//! [bindable-user-setters] + +void BindableUser::setCountry(Country country) +{ + m_country = country; +} + +void BindableUser::setAge(int age) +{ + m_age = age; +} + +//! [bindable-user-setters] diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h new file mode 100644 index 0000000000..6bb9bcdcb5 --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h @@ -0,0 +1,37 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BINDABLEUSER_H +#define BINDABLEUSER_H + +#include <QBindable> +#include <QLocale> +#include <QProperty> + +//! [bindable-user-class] + +class BindableUser +{ +public: + using Country = QLocale::Territory; + +public: + BindableUser() = default; + BindableUser(const BindableUser &) = delete; + + Country country() const { return m_country; } + void setCountry(Country country); + QBindable<Country> bindableCountry() { return &m_country; } + + int age() const { return m_age; } + void setAge(int age); + QBindable<int> bindableAge() { return &m_age; } + +private: + QProperty<Country> m_country { QLocale::AnyTerritory }; + QProperty<int> m_age { 0 }; +}; + +//! [bindable-user-class] + +#endif // BINDABLEUSER_H diff --git a/examples/corelib/bindableproperties/bindablesubscription/main.cpp b/examples/corelib/bindableproperties/bindablesubscription/main.cpp new file mode 100644 index 0000000000..466f487b8e --- /dev/null +++ b/examples/corelib/bindableproperties/bindablesubscription/main.cpp @@ -0,0 +1,77 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "../shared/subscriptionwindow.h" +#include "bindablesubscription.h" +#include "bindableuser.h" + +#include <QApplication> +#include <QBindable> +#include <QLabel> +#include <QLocale> +#include <QPushButton> +#include <QRadioButton> +#include <QSpinBox> +#include <QString> + +using namespace Qt::StringLiterals; + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + BindableUser user; + BindableSubscription subscription(&user); + + SubscriptionWindow w; + // clazy:excludeall=lambda-in-connect + // when subscription is out of scope so is window + + // Initialize subscription data + QRadioButton *monthly = w.findChild<QRadioButton *>(u"btnMonthly"_s); + QObject::connect(monthly, &QRadioButton::clicked, monthly, [&] { + subscription.setDuration(BindableSubscription::Monthly); + }); + QRadioButton *quarterly = w.findChild<QRadioButton *>(u"btnQuarterly"_s); + QObject::connect(quarterly, &QRadioButton::clicked, quarterly, [&] { + subscription.setDuration(BindableSubscription::Quarterly); + }); + QRadioButton *yearly = w.findChild<QRadioButton *>(u"btnYearly"_s); + QObject::connect(yearly, &QRadioButton::clicked, yearly, [&] { + subscription.setDuration(BindableSubscription::Yearly); + }); + + // Initialize user data + QPushButton *germany = w.findChild<QPushButton *>(u"btnGermany"_s); + QObject::connect(germany, &QPushButton::clicked, germany, [&] { + user.setCountry(BindableUser::Country::Germany); + }); + QPushButton *finland = w.findChild<QPushButton *>(u"btnFinland"_s); + QObject::connect(finland, &QPushButton::clicked, finland, [&] { + user.setCountry(BindableUser::Country::Finland); + }); + QPushButton *norway = w.findChild<QPushButton *>(u"btnNorway"_s); + QObject::connect(norway, &QPushButton::clicked, norway, [&] { + user.setCountry(BindableUser::Country::Norway); + }); + + QSpinBox *ageSpinBox = w.findChild<QSpinBox *>(u"ageSpinBox"_s); + QBindable<int> ageBindable(ageSpinBox, "value"); + user.bindableAge().setBinding([ageBindable](){ return ageBindable.value();}); + + QLabel *priceDisplay = w.findChild<QLabel *>(u"priceDisplay"_s); + + // Track price changes +//! [update-ui] + auto priceChangeHandler = subscription.bindablePrice().subscribe([&] { + QLocale lc{QLocale::AnyLanguage, user.country()}; + priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration())); + }); + + auto priceValidHandler = subscription.bindableIsValid().subscribe([&] { + priceDisplay->setEnabled(subscription.isValid()); + }); +//! [update-ui] + + w.show(); + return a.exec(); +} |