summaryrefslogtreecommitdiffstats
path: root/examples/corelib/bindableproperties/subscription
diff options
context:
space:
mode:
Diffstat (limited to 'examples/corelib/bindableproperties/subscription')
-rw-r--r--examples/corelib/bindableproperties/subscription/CMakeLists.txt12
-rw-r--r--examples/corelib/bindableproperties/subscription/main.cpp95
-rw-r--r--examples/corelib/bindableproperties/subscription/subscription.cpp79
-rw-r--r--examples/corelib/bindableproperties/subscription/subscription.h48
-rw-r--r--examples/corelib/bindableproperties/subscription/subscription.pro22
-rw-r--r--examples/corelib/bindableproperties/subscription/user.cpp24
-rw-r--r--examples/corelib/bindableproperties/subscription/user.h36
7 files changed, 316 insertions, 0 deletions
diff --git a/examples/corelib/bindableproperties/subscription/CMakeLists.txt b/examples/corelib/bindableproperties/subscription/CMakeLists.txt
new file mode 100644
index 0000000000..91b9340fbf
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/CMakeLists.txt
@@ -0,0 +1,12 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+qt_add_executable(subscription
+ main.cpp
+ subscription.cpp subscription.h
+ user.cpp user.h
+)
+
+target_link_libraries(subscription PRIVATE
+ bindableproperties_shared
+)
diff --git a/examples/corelib/bindableproperties/subscription/main.cpp b/examples/corelib/bindableproperties/subscription/main.cpp
new file mode 100644
index 0000000000..3f98da7467
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/main.cpp
@@ -0,0 +1,95 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include "../shared/subscriptionwindow.h"
+#include "subscription.h"
+#include "user.h"
+
+#include <QApplication>
+#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);
+
+//! [init]
+ User user;
+ Subscription subscription(&user);
+//! [init]
+
+ SubscriptionWindow w;
+
+ // Initialize subscription data
+ QRadioButton *monthly = w.findChild<QRadioButton *>(u"btnMonthly"_s);
+ QObject::connect(monthly, &QRadioButton::clicked, &subscription, [&] {
+ subscription.setDuration(Subscription::Monthly);
+ });
+ QRadioButton *quarterly = w.findChild<QRadioButton *>(u"btnQuarterly"_s);
+ QObject::connect(quarterly, &QRadioButton::clicked, &subscription, [&] {
+ subscription.setDuration(Subscription::Quarterly);
+ });
+ QRadioButton *yearly = w.findChild<QRadioButton *>(u"btnYearly"_s);
+ QObject::connect(yearly, &QRadioButton::clicked, &subscription, [&] {
+ subscription.setDuration(Subscription::Yearly);
+ });
+
+ // Initialize user data
+ QPushButton *germany = w.findChild<QPushButton *>(u"btnGermany"_s);
+ QObject::connect(germany, &QPushButton::clicked, &user, [&] {
+ user.setCountry(User::Country::Germany);
+ });
+ QPushButton *finland = w.findChild<QPushButton *>(u"btnFinland"_s);
+ QObject::connect(finland, &QPushButton::clicked, &user, [&] {
+ user.setCountry(User::Country::Finland);
+ });
+ QPushButton *norway = w.findChild<QPushButton *>(u"btnNorway"_s);
+ QObject::connect(norway, &QPushButton::clicked, &user, [&] {
+ user.setCountry(User::Country::Norway);
+ });
+
+ QSpinBox *ageSpinBox = w.findChild<QSpinBox *>(u"ageSpinBox"_s);
+ QObject::connect(ageSpinBox, &QSpinBox::valueChanged, &user, [&](int value) {
+ user.setAge(value);
+ });
+
+ // Initialize price data
+ QLabel *priceDisplay = w.findChild<QLabel *>(u"priceDisplay"_s);
+ priceDisplay->setText(QString::number(subscription.price()));
+ priceDisplay->setEnabled(subscription.isValid());
+
+ // Track the price changes
+
+//! [connect-price-changed]
+ QObject::connect(&subscription, &Subscription::priceChanged, priceDisplay, [&] {
+ QLocale lc{QLocale::AnyLanguage, user.country()};
+ priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration()));
+ });
+//! [connect-price-changed]
+
+//! [connect-validity-changed]
+ QObject::connect(&subscription, &Subscription::isValidChanged, priceDisplay, [&] {
+ priceDisplay->setEnabled(subscription.isValid());
+ });
+//! [connect-validity-changed]
+
+//! [connect-user]
+ QObject::connect(&user, &User::countryChanged, &subscription, [&] {
+ subscription.calculatePrice();
+ subscription.updateValidity();
+ });
+
+ QObject::connect(&user, &User::ageChanged, &subscription, [&] {
+ subscription.updateValidity();
+ });
+//! [connect-user]
+
+ w.show();
+ return a.exec();
+}
diff --git a/examples/corelib/bindableproperties/subscription/subscription.cpp b/examples/corelib/bindableproperties/subscription/subscription.cpp
new file mode 100644
index 0000000000..85cc5798cc
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/subscription.cpp
@@ -0,0 +1,79 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include "subscription.h"
+#include "user.h"
+
+Subscription::Subscription(User *user) : m_user(user)
+{
+ Q_ASSERT(user);
+}
+
+//! [calculate-price]
+
+void Subscription::calculatePrice()
+{
+ const auto oldPrice = m_price;
+
+ m_price = qRound(calculateDiscount() * int(m_duration) * basePrice());
+ if (m_price != oldPrice)
+ emit priceChanged();
+}
+
+//! [calculate-price]
+
+//! [set-duration]
+
+void Subscription::setDuration(Duration newDuration)
+{
+ if (newDuration != m_duration) {
+ m_duration = newDuration;
+ calculatePrice();
+ emit durationChanged();
+ }
+}
+
+//! [set-duration]
+
+//! [calculate-discount]
+
+double Subscription::calculateDiscount() const
+{
+ switch (m_duration) {
+ case Monthly:
+ return 1;
+ case Quarterly:
+ return 0.9;
+ case Yearly:
+ return 0.6;
+ }
+ Q_ASSERT(false);
+ return -1;
+}
+
+//! [calculate-discount]
+
+//! [calculate-base-price]
+
+int Subscription::basePrice() const
+{
+ if (m_user->country() == User::Country::AnyTerritory)
+ return 0;
+
+ return (m_user->country() == User::Country::Norway) ? 100 : 80;
+}
+
+//! [calculate-base-price]
+
+//! [update-validity]
+
+void Subscription::updateValidity()
+{
+ bool isValid = m_isValid;
+ m_isValid = m_user->country() != User::Country::AnyTerritory && m_user->age() > 12;
+
+ if (m_isValid != isValid)
+ emit isValidChanged();
+}
+
+//! [update-validity]
diff --git a/examples/corelib/bindableproperties/subscription/subscription.h b/examples/corelib/bindableproperties/subscription/subscription.h
new file mode 100644
index 0000000000..8f0d34e948
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/subscription.h
@@ -0,0 +1,48 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef SUBSCRIPTION_H
+#define SUBSCRIPTION_H
+
+#include <QObject>
+#include <QPointer>
+
+class User;
+
+//! [subscription-class]
+
+class Subscription : public QObject
+{
+ Q_OBJECT
+public:
+ enum Duration { Monthly = 1, Quarterly = 3, Yearly = 12 };
+
+ Subscription(User *user);
+
+ void calculatePrice();
+ int price() const { return m_price; }
+
+ Duration duration() const { return m_duration; }
+ void setDuration(Duration newDuration);
+
+ bool isValid() const { return m_isValid; }
+ void updateValidity();
+
+signals:
+ void priceChanged();
+ void durationChanged();
+ void isValidChanged();
+
+private:
+ double calculateDiscount() const;
+ int basePrice() const;
+
+ QPointer<User> m_user;
+ Duration m_duration = Monthly;
+ int m_price = 0;
+ bool m_isValid = false;
+};
+
+//! [subscription-class]
+
+#endif // SUBSCRIPTION_H
diff --git a/examples/corelib/bindableproperties/subscription/subscription.pro b/examples/corelib/bindableproperties/subscription/subscription.pro
new file mode 100644
index 0000000000..68910904bb
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/subscription.pro
@@ -0,0 +1,22 @@
+QT += widgets
+TARGET = subscription
+
+SOURCES += main.cpp \
+ subscription.cpp \
+ user.cpp \
+ ../shared/subscriptionwindow.cpp
+
+target.path = $$[QT_INSTALL_EXAMPLES]/corelib/bindableproperties/subscription
+INSTALLS += target
+
+FORMS += \
+ ../shared/subscriptionwindow.ui
+
+HEADERS += \
+ subscription.h \
+ user.h \
+ ../shared/subscriptionwindow.h
+
+RESOURCES += \
+ ../shared/countries.qrc
+
diff --git a/examples/corelib/bindableproperties/subscription/user.cpp b/examples/corelib/bindableproperties/subscription/user.cpp
new file mode 100644
index 0000000000..575bcb13ee
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/user.cpp
@@ -0,0 +1,24 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include "user.h"
+
+//! [user-setters]
+
+void User::setCountry(Country country)
+{
+ if (m_country != country) {
+ m_country = country;
+ emit countryChanged();
+ }
+}
+
+void User::setAge(int age)
+{
+ if (m_age != age) {
+ m_age = age;
+ emit ageChanged();
+ }
+}
+
+//! [user-setters]
diff --git a/examples/corelib/bindableproperties/subscription/user.h b/examples/corelib/bindableproperties/subscription/user.h
new file mode 100644
index 0000000000..dd32a4fe23
--- /dev/null
+++ b/examples/corelib/bindableproperties/subscription/user.h
@@ -0,0 +1,36 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef USER_H
+#define USER_H
+
+#include <QLocale>
+#include <QObject>
+
+//! [user-class]
+
+class User : public QObject
+{
+ Q_OBJECT
+
+public:
+ using Country = QLocale::Territory;
+
+public:
+ Country country() const { return m_country; }
+ void setCountry(Country country);
+
+ int age() const { return m_age; }
+ void setAge(int age);
+
+signals:
+ void countryChanged();
+ void ageChanged();
+
+private:
+ Country m_country { QLocale::AnyTerritory };
+ int m_age { 0 };
+};
+
+//! [user-class]
+#endif // USER_H