restsviewmodel.cpp 1.2 KB

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