diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com> | 2014-03-20 10:05:16 +0100 |
---|---|---|
committer | Andy Nichols <andy.nichols@digia.com> | 2014-03-20 13:45:11 +0200 |
commit | a2f33195fa02233df0ae8a382969ea21276156a5 (patch) | |
tree | 799a0dc165e55245369c7a80a078b988b2e62657 | |
parent | ebc0d7669e5805864918b418d8e3aeb38f7d2bdd (diff) |
Make it possible to create products outside store
The current API did not allow for the use case of creating
products from a model in a convenient way, since there was
no way of setting the store pointer except to set the
parent of the objects, which is hacky.
This adds explicitly setting the store as an option.
Change-Id: Id98a9d646afa8818122b73e8bf4aac61e90a4cab
Reviewed-by: Andy Nichols <andy.nichols@digia.com>
-rw-r--r-- | src/imports/purchasing/qinappproductqmltype.cpp | 83 | ||||
-rw-r--r-- | src/imports/purchasing/qinappproductqmltype_p.h | 9 | ||||
-rw-r--r-- | src/imports/purchasing/qinappstoreqmltype.cpp | 2 |
3 files changed, 75 insertions, 19 deletions
diff --git a/src/imports/purchasing/qinappproductqmltype.cpp b/src/imports/purchasing/qinappproductqmltype.cpp index 06e1a00..38a5245 100644 --- a/src/imports/purchasing/qinappproductqmltype.cpp +++ b/src/imports/purchasing/qinappproductqmltype.cpp @@ -51,27 +51,80 @@ QInAppProductQmlType::QInAppProductQmlType(QInAppProduct::ProductType requiredTy { } -void QInAppProductQmlType::setStore(QInAppStore *store) +/*! + \qmlproperty QtPurchasing::Product::store + + This property holds the store containing the product. When the product is created as + a child of the store, this is set automatically to the parent, as in the following + example: + + \qml + Store { + ConsumableProduct { + // No need to set the store explicitly here, as it will automatically be + // bound to the parent + identifier: "product1" + } + ConsumableProduct { + // No need to set the store explicitly here, as it will automatically be + // bound to the parent + identifier: "product2" + } + } + \endqml + + However, in some advanced use cases, for example when products are created based on + a model, it's also possible to create the product anywhere in the QML document + and set the store explicitly, like in the following example: + + \code + ListModel { + id: productModel + ListElement { + productIdentifier: "product1" + } + ListElement { + productIdentifier: "product2" + } + } + + Store { + id: myStore + } + + Instantiator { + model: productModel + delegate: ConsumableProduct { + identifier: productIdentifier + store: myStore + } + } + \endcode + */ +void QInAppProductQmlType::setStore(QInAppStoreQmlType *store) { - if (m_store == store && m_store != 0) + if (m_store == store) return; if (m_store != 0) - m_store->disconnect(this); + m_store->store()->disconnect(this); m_store = store; - if (m_store == 0) { - qWarning("Parent of products should be a Store instance."); - } else { - connect(m_store, SIGNAL(productRegistered(QInAppProduct*)), - this, SLOT(handleProductRegistered(QInAppProduct *))); - connect(m_store, SIGNAL(productUnknown(QInAppProduct::ProductType,QString)), - this, SLOT(handleProductUnknown(QInAppProduct::ProductType,QString))); - connect(m_store, SIGNAL(transactionReady(QInAppTransaction*)), - this, SLOT(handleTransaction(QInAppTransaction*))); - } + connect(m_store->store(), SIGNAL(productRegistered(QInAppProduct*)), + this, SLOT(handleProductRegistered(QInAppProduct *))); + connect(m_store->store(), SIGNAL(productUnknown(QInAppProduct::ProductType,QString)), + this, SLOT(handleProductUnknown(QInAppProduct::ProductType,QString))); + connect(m_store->store(), SIGNAL(transactionReady(QInAppTransaction*)), + this, SLOT(handleTransaction(QInAppTransaction*))); updateProduct(); + + emit storeChanged(); +} + +QInAppStoreQmlType *QInAppProductQmlType::store() const +{ + return m_store; } void QInAppProductQmlType::componentComplete() @@ -122,13 +175,13 @@ void QInAppProductQmlType::updateProduct() if (m_identifier.isEmpty()) { m_status = Unknown; } else { - product = m_store->registeredProduct(m_identifier); + product = m_store->store()->registeredProduct(m_identifier); if (product != 0 && product == m_product) return; if (product == 0) { m_status = PendingRegistration; - m_store->registerProduct(m_requiredType, m_identifier); + m_store->store()->registerProduct(m_requiredType, m_identifier); } else if (product->productType() != m_requiredType) { product = 0; m_status = Unknown; diff --git a/src/imports/purchasing/qinappproductqmltype_p.h b/src/imports/purchasing/qinappproductqmltype_p.h index 4cc0e02..d055ec2 100644 --- a/src/imports/purchasing/qinappproductqmltype_p.h +++ b/src/imports/purchasing/qinappproductqmltype_p.h @@ -27,8 +27,8 @@ QT_BEGIN_NAMESPACE -class QInAppStore; class QInAppTransaction; +class QInAppStoreQmlType; class QInAppProductQmlType : public QObject, public QQmlParserStatus { Q_OBJECT @@ -38,6 +38,7 @@ class QInAppProductQmlType : public QObject, public QQmlParserStatus Q_PROPERTY(QString title READ title NOTIFY titleChanged) Q_PROPERTY(QString description READ description NOTIFY descriptionChanged) Q_PROPERTY(Status status READ status NOTIFY statusChanged) + Q_PROPERTY(QInAppStoreQmlType *store READ store WRITE setStore NOTIFY storeChanged) public: enum Status { Uninitialized, @@ -59,7 +60,8 @@ public: QString title() const; QString description() const; - void setStore(QInAppStore *store); + void setStore(QInAppStoreQmlType *store); + QInAppStoreQmlType *store() const; Q_SIGNALS: void purchaseSucceeded(QInAppTransaction *transaction); @@ -70,6 +72,7 @@ Q_SIGNALS: void priceChanged(); void titleChanged(); void descriptionChanged(); + void storeChanged(); protected: void componentComplete(); @@ -89,7 +92,7 @@ private: QInAppProduct::ProductType m_requiredType; bool m_componentComplete; - QInAppStore *m_store; + QInAppStoreQmlType *m_store; QInAppProduct *m_product; }; diff --git a/src/imports/purchasing/qinappstoreqmltype.cpp b/src/imports/purchasing/qinappstoreqmltype.cpp index d6d4a49..f9c4613 100644 --- a/src/imports/purchasing/qinappstoreqmltype.cpp +++ b/src/imports/purchasing/qinappstoreqmltype.cpp @@ -66,7 +66,7 @@ static void addProduct(QQmlListProperty<QInAppProductQmlType> *property, QInAppP { QInAppStoreQmlType *store = qobject_cast<QInAppStoreQmlType *>(property->object); Q_ASSERT(store != 0); - product->setStore(store->store()); + product->setStore(store); QList<QInAppProductQmlType *> *products = reinterpret_cast<QList<QInAppProductQmlType *> *>(property->data); Q_ASSERT(products != 0); |