restsviewmodel.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "restsviewmodel.h"
  2. #include "services/settingsservice.h"
  3. RestsViewModel::RestsViewModel(QObject *parent)
  4. : QObject{parent}
  5. {
  6. connect(m_productsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged);
  7. connect(settings(), &SettingsService::selectedStoreIdChanged, this, &RestsViewModel::productsChanged);
  8. }
  9. QVariantList RestsViewModel::products() const
  10. {
  11. QVariantList res;
  12. const QString storeId = settings()->selectedStoreId();
  13. const QVariantList products = m_productsModel->items();
  14. for (const QVariant &product : products) {
  15. QString productId = product.toMap().value("id").toString();
  16. res << QVariantMap {
  17. { "productId", productId },
  18. { "title", m_productsModel->itemProperty(productId, "name").toString() },
  19. { "rest", m_restsModel->productRest(storeId, productId) }
  20. };
  21. }
  22. return res;
  23. }
  24. void RestsViewModel::setProductRest(const QString &productId, float rest)
  25. {
  26. m_restsModel->submitRest(settings()->selectedStoreId(), productId, rest);
  27. }
  28. SettingsService *RestsViewModel::settings() const
  29. {
  30. return SettingsService::instance();
  31. }