C
Qt Quick Ultralite Watch Demo
/****************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/#include "HealthModel.h" #include <platform/platform.h> #include <algorithm> namespace { const int MIN_HEART_RATE = 50; const int MAX_HEART_RATE = 160; const int MIN_HEART_RATE_CHANGE = -2; const int MAX_HEART_RATE_CHANGE = 2; const int DEFAULT_HEART_RATE = 60; int GetRandomNumber(int from, int to) { return static_cast<int>(round(from + Qul::Platform::getPlatformInstance()->rand() * (to - from))); } } // namespace HealthModel::HealthModel() : HeartRate(DEFAULT_HEART_RATE) , MinHeartRate(DEFAULT_HEART_RATE) , MaxHeartRate(DEFAULT_HEART_RATE) { update(); } void HealthModel::update() { const int delta = GetRandomNumber(MIN_HEART_RATE_CHANGE, MAX_HEART_RATE_CHANGE); int heartRate = HeartRate.value(); heartRate += delta; heartRate = std::max(heartRate, MIN_HEART_RATE); heartRate = std::min(heartRate, MAX_HEART_RATE); HeartRate.setValue(heartRate); if (heartRate > MaxHeartRate.value()) { MaxHeartRate.setValue(heartRate); } if (heartRate < MinHeartRate.value()) { MinHeartRate.setValue(heartRate); } }