File indexing completed on 2024-06-23 05:36:16

0001 #include "AbstractListModel.hpp"
0002 
0003 namespace cutehmi {
0004 namespace dataacquisition {
0005 
0006 constexpr int AbstractListModel::INITIAL_INTERVAL;
0007 
0008 AbstractListModel::AbstractListModel(QObject * parent):
0009     QAbstractListModel(parent),
0010     m(new Members)
0011 {
0012     m->updateTimer.setSingleShot(true);
0013 }
0014 
0015 int AbstractListModel::interval() const
0016 {
0017     return m->interval;
0018 }
0019 
0020 void AbstractListModel::setInterval(int interval)
0021 {
0022     CUTEHMI_ASSERT(interval >= 0, "Value of 'interval' property should be non-negative.");
0023 
0024     if (m->interval != interval) {
0025         m->interval = interval;
0026         emit intervalChanged();
0027     }
0028 }
0029 
0030 Schema * AbstractListModel::schema() const
0031 {
0032     return m->schema;
0033 }
0034 
0035 void AbstractListModel::setSchema(Schema * schema)
0036 {
0037     if (m->schema != schema) {
0038         if (m->schema)
0039             m->schema->disconnect(this);
0040 
0041         m->schema = schema;
0042         emit schemaChanged();
0043 
0044         if (m->schema) {
0045             connect(m->schema, & Schema::validated, this, & AbstractListModel::onSchemaValidated);
0046             connect(m->schema, & Schema::errored, this, & AbstractListModel::broke);
0047         }
0048     }
0049 }
0050 
0051 void AbstractListModel::configureStarting(QState * starting, AssignStatusFunction assignStatus)
0052 {
0053     configureStartingOrRepairing(starting, assignStatus);
0054 }
0055 
0056 void AbstractListModel::configureStarted(QState * active, const QState * idling, const QState * yielding, AssignStatusFunction assignStatus)
0057 {
0058     Q_UNUSED(yielding)
0059 
0060     connect(idling, & QState::entered, this, & AbstractListModel::startUpdateTimer);
0061 
0062     QState * updating = new QState(active);
0063     assignStatus(*updating, tr("Updating model"));
0064     active->setInitialState(updating);
0065     connect(updating, & QState::entered, this, & AbstractListModel::requestUpdate);
0066 }
0067 
0068 void AbstractListModel::configureStopping(QState * stopping, AssignStatusFunction assignStatus)
0069 {
0070     QState * waitingForWorkers = new QState(stopping);
0071     assignStatus(*waitingForWorkers, tr("Waiting for finishing an update"));
0072     connect(waitingForWorkers, & QState::entered, this, & AbstractListModel::confirmUpdateFinished);
0073 
0074     QState * stoppingTimer = new QState(stopping);
0075     stopping->setInitialState(stoppingTimer);
0076     assignStatus(*stoppingTimer, tr("Stopping update timer"));
0077     connect(stoppingTimer, & QState::entered, this, & AbstractListModel::stopUpdateTimer);
0078     stoppingTimer->addTransition(this, & AbstractListModel::updateTimerStopped, waitingForWorkers);
0079 }
0080 
0081 void AbstractListModel::configureBroken(QState * broken, AssignStatusFunction assignStatus)
0082 {
0083     Q_UNUSED(assignStatus)
0084 
0085     connect(broken, & QState::entered, this, & AbstractListModel::stopUpdateTimer);
0086 }
0087 
0088 void AbstractListModel::configureRepairing(QState * repairing, AssignStatusFunction assignStatus)
0089 {
0090     configureStartingOrRepairing(repairing, assignStatus);
0091 }
0092 
0093 void AbstractListModel::configureEvacuating(QState * evacuating, AssignStatusFunction assignStatus)
0094 {
0095     Q_UNUSED(assignStatus)
0096 
0097     connect(evacuating, & QState::entered, this, & AbstractListModel::stopped);
0098 }
0099 
0100 std::unique_ptr<QAbstractTransition> AbstractListModel::transitionToStarted() const
0101 {
0102     connect(this, & AbstractListModel::updateTimerStarted, this, & AbstractListModel::started);
0103 
0104     return std::make_unique<QSignalTransition>(this, & AbstractListModel::started);
0105 }
0106 
0107 std::unique_ptr<QAbstractTransition> AbstractListModel::transitionToStopped() const
0108 {
0109     connect(this, & AbstractListModel::updateFinished, this, & AbstractListModel::stopped);
0110 
0111     return std::make_unique<QSignalTransition>(this, & AbstractListModel::stopped);
0112 }
0113 
0114 std::unique_ptr<QAbstractTransition> AbstractListModel::transitionToBroken() const
0115 {
0116     return std::make_unique<QSignalTransition>(this, & AbstractListModel::broke);
0117 }
0118 
0119 std::unique_ptr<QAbstractTransition> AbstractListModel::transitionToYielding() const
0120 {
0121     return std::make_unique<QSignalTransition>(& m->updateTimer, & QTimer::timeout);
0122 }
0123 
0124 std::unique_ptr<QAbstractTransition> AbstractListModel::transitionToIdling() const
0125 {
0126     return std::make_unique<QSignalTransition>(this, & AbstractListModel::updateFinished);
0127 }
0128 
0129 void AbstractListModel::onSchemaValidated(bool result)
0130 {
0131     if (result)
0132         emit schemaValidated();
0133     else
0134         emit broke();
0135 }
0136 
0137 void AbstractListModel::startUpdateTimer()
0138 {
0139     m->updateTimer.start(interval());
0140     emit updateTimerStarted();
0141 }
0142 
0143 void AbstractListModel::stopUpdateTimer()
0144 {
0145     m->updateTimer.stop();
0146     emit updateTimerStopped();
0147 }
0148 
0149 void AbstractListModel::configureStartingOrRepairing(QState * parent, AssignStatusFunction assignStatus)
0150 {
0151     QState * startingTimer = new QState(parent);
0152     assignStatus(*startingTimer, tr("Starting store timer"));
0153     connect(startingTimer, & QState::entered, this, & AbstractListModel::startUpdateTimer);
0154 
0155     QState * validatingSchema = createValidatingSchemaSate(parent, assignStatus, startingTimer);
0156 
0157     QState * waitingForDatabase = createWaitingForDatabaseConnectedSate(parent, assignStatus, validatingSchema);
0158     parent->setInitialState(waitingForDatabase);
0159 }
0160 
0161 }
0162 }
0163 
0164 //(c)C: Copyright © 2022-2023, Michał Policht <michal@policht.pl>. All rights reserved.
0165 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
0166 //(c)C: This file is a part of CuteHMI.
0167 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
0168 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
0169 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI.  If not, see <https://www.gnu.org/licenses/>.
0170 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
0171 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
0172 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
0173 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.