Commit 6b117262 authored by Michal Policht's avatar Michal Policht

Introduce CuteHMI.Services.2 extension.

parent c1b10092
......@@ -18,3 +18,6 @@ tools/*/locale/* -crlf
extra/logo/*.svg -crlf
extra/logo/*.png -crlf
extra/logo/*.ico -crlf
# SCXML files.
*.scxml -crlf
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
//(c)MP: Copyright © %YEAR%, Michal Policht <michpolicht@gmail.com>. All rights reserved.
//(c)MP: This file is a part of CuteHMI.
//(c)MP: 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.
//(c)MP: 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.
//(c)MP: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
#(c)MP: Copyright © %YEAR%, Michal Policht <michpolicht@gmail.com>. All rights reserved.
#(c)MP: This file is a part of CuteHMI.
#(c)MP: 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.
#(c)MP: 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.
#(c)MP: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
# CuteHMI - Services
Extension that helps creating services. Many extensions may need to behave like services or daemons, doing their tasks in the
background, independently from UI. This extension helps to organize and coordinate them.
import qbs
import cutehmi
Project {
name: "CuteHMI.Services.2"
references: [
"tests/tests.qbs"
]
cutehmi.Extension {
name: parent.name
minor: 0
micro: 0
vendor: "CuteHMI"
domain: "cutehmi.kde.org"
friendlyName: "Services"
description: "Extension that helps creating services."
author: "Michal Policht"
copyright: "Michal Policht"
license: "GNU Lesser General Public License, v. 3.0"
files: [
"README.md",
"LICENSE",
"dev/StandardStates.scxml",
"include/cutehmi/services/Initializer.hpp",
"include/cutehmi/services/PollingTimer.hpp",
"include/cutehmi/services/Service.hpp",
"include/cutehmi/services/ServiceListModel.hpp",
"include/cutehmi/services/ServiceManager.hpp",
"include/cutehmi/services/Serviceable.hpp",
"include/cutehmi/services/internal/QMLPlugin.cpp",
"include/cutehmi/services/internal/QMLPlugin.hpp",
"include/cutehmi/services/internal/StateInterface.hpp",
"include/cutehmi/services/internal/platform.hpp",
"include/cutehmi/services/internal/common.hpp",
"include/cutehmi/services/logging.hpp",
"include/cutehmi/services/metadata.hpp",
"qmldir",
"src/cutehmi/services/Initializer.cpp",
"src/cutehmi/services/PollingTimer.cpp",
"src/cutehmi/services/Service.cpp",
"src/cutehmi/services/ServiceListModel.cpp",
"src/cutehmi/services/ServiceManager.cpp",
"src/cutehmi/services/internal/StateInterface.cpp",
"src/cutehmi/services/logging.cpp",
]
Depends { name: "CuteHMI.2" }
Export {
Depends { name: "CuteHMI.2" }
}
}
}
//(c)MP: Copyright © 2018, Michal Policht. All rights reserved.
//(c)MP: This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" name="StandardStates" qt:editorversion="4.7.2">
<state id="stopped">
<qt:editorinfo scenegeometry="24.50;133.97;-25.70;47.04;120;108.50" geometry="24.50;133.97;-50.20;-86.93;120;108.50"/>
<transition type="external" event="Service::started()" target="starting">
<qt:editorinfo movePoint="1.02;7.14"/>
</transition>
<initial>
<qt:editorinfo scenegeometry="34.30;113.55;14.30;93.55;40;40" geometry="9.80;-20.42;-20;-20;40;40"/>
</initial>
</state>
<state id="starting">
<qt:editorinfo scenegeometry="973.51;101.29;871.01;51.29;120;100" geometry="973.51;101.29;-102.50;-50;120;100"/>
<transition type="external" target="started" event="Serviceable::transitionToStarted()">
<qt:editorinfo movePoint="-13.46;15.72"/>
</transition>
<transition type="external" event="Serviceable::transitionToBroken()" target="broken">
<qt:editorinfo movePoint="140.81;-10.20"/>
</transition>
</state>
<state id="started" initial="yielding">
<qt:editorinfo scenegeometry="869.22;380.12;213.83;328.28;594.72;354.57" geometry="869.22;380.12;-655.39;-51.84;594.72;354.57"/>
<transition type="external" target="broken" event="Serviceable::transitionToBroken()">
<qt:editorinfo movePoint="3.33;25.02"/>
</transition>
<transition type="external" event="Service::stopped()" target="stopping">
<qt:editorinfo movePoint="14.29;-20.41"/>
</transition>
<state id="yielding">
<qt:editorinfo scenegeometry="726.82;444.88;666.82;394.88;120;100" geometry="-142.40;64.76;-60;-50;120;100"/>
<transition type="external" event="Service::activated()" target="active">
<qt:editorinfo movePoint="59.91;12.84"/>
</transition>
</state>
<onentry/>
<state id="idling">
<qt:editorinfo scenegeometry="295.10;452.39;235.10;402.39;120;100" geometry="-574.12;72.27;-60;-50;120;100"/>
<transition type="external" event="Serviceable::transitionToYielding()" target="yielding">
<qt:editorinfo movePoint="-2.14;7.49" endTargetFactors="73.60;58.29"/>
</transition>
</state>
<state id="active">
<qt:editorinfo scenegeometry="551.19;608.58;451.19;558.58;120;100" geometry="-318.03;228.46;-100;-50;120;100"/>
<transition type="external" event="Serviceable::transitionToIdling()" target="idling">
<qt:editorinfo movePoint="-65.25;16.05"/>
</transition>
</state>
</state>
<state id="broken">
<qt:editorinfo scenegeometry="931.01;879.38;871.01;829.38;120;100" geometry="931.01;879.38;-60;-50;120;100"/>
<transition type="external" event="Service::started()" target="repairing">
<qt:editorinfo startTargetFactors="8.78;39.48" movePoint="10.66;-22.50" endTargetFactors="87.05;39.46"/>
</transition>
<transition type="external" event="Service::stopped()" target="stopped">
<qt:editorinfo movePoint="17.35;-22.45" endTargetFactors="25.55;58.27" localGeometry="-0.02;76.32;-987.16;76.32;-987.16;-769.12"/>
</transition>
</state>
<state id="repairing">
<qt:editorinfo scenegeometry="34.30;879.38;-25.70;829.38;120;100" geometry="34.30;879.38;-60;-50;120;100"/>
<transition type="external" event="Serviceable::transitionToBroken()" target="broken">
<qt:editorinfo startTargetFactors="85.60;71.97" movePoint="3.36;10.30" endTargetFactors="48.50;73.76"/>
</transition>
<transition type="external" event="Serviceable::transitionToStarted()" target="started">
<qt:editorinfo movePoint="14.39;39.85"/>
</transition>
</state>
<state id="stopping">
<qt:editorinfo scenegeometry="39.65;506.70;-20.35;456.70;120;100" geometry="39.65;506.70;-60;-50;120;100"/>
<transition type="external" event="Serviceable::transitionToStopped()" target="stopped">
<qt:editorinfo movePoint="131.65;-31.13"/>
</transition>
<transition type="external" event="timeout()" target="interrupted">
<qt:editorinfo movePoint="31.02;0"/>
</transition>
</state>
<state id="interrupted">
<qt:editorinfo scenegeometry="39.65;679.31;-20.35;629.31;120;100" geometry="39.65;679.31;-60;-50;120;100"/>
</state>
</scxml>
import qbs
import cutehmi
cutehmi.Example {
name: "CustomService"
vendor: "CuteHMI"
friendlyName: "Custom Service"
description: "Custom service example."
author: "Michal Policht"
copyright: "Michal Policht"
license: "GNU Lesser General Public License, v. 3.0"
files: [
"_Main.qml"
]
Depends { name: "cutehmi_view" }
Depends { name: "CuteHMI.Services.2" }
}
//(c)MP: Copyright © 2019, Michal Policht. All rights reserved.
//(c)MP: The MIT License
//(c)MP: 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:
//(c)MP: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//(c)MP: 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.
import QtQml 2.2
import QtQuick 2.11
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
import CuteHMI.Services 2.0
import Examples.CustomService 0.0
Item {
anchors.fill: parent
ColumnLayout {
anchors.centerIn: parent
GridLayout {
columns: 2
Text {
Layout.alignment: Qt.AlignCenter
text: "operation"
}
Text {
Layout.alignment: Qt.AlignCenter
text: "status"
}
Row {
Layout.alignment: Qt.AlignCenter
Button {
text: "start"
onClicked: service.start()
}
Button {
text: "stop"
onClicked: service.stop()
}
}
Text {
Layout.alignment: Qt.AlignCenter
text: service.status
}
}
RowLayout
{
Button {
text: "unlock steering"
onClicked: batmobile.unlockSteering()
}
Button {
text: "damage"
onClicked: batmobile.damage()
}
}
}
Service {
id: service
Car {
id: batmobile
}
onStatusChanged: console.log("Batmobile status changed to: " + status)
}
}
import qbs
Project {
references: [
"CustomService"
]
}
//(c)MP: Copyright © 2019, Michal Policht <michpolicht@gmail.com>. All rights reserved.
//(c)MP: This file is a part of CuteHMI.
//(c)MP: 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.
//(c)MP: 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.
//(c)MP: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
#ifndef INCLUDECUTEHMISERVICESINITIALIZER_HPP
#define INCLUDECUTEHMISERVICESINITIALIZER_HPP
#include "internal/common.hpp"
#include <cutehmi/ExtensionInitializer.hpp>
namespace cutehmi {
namespace services {
/**
* Extension initializer. This class performs initialization of CuteHMI.Services extension.
*
* Normally it is not advisable to create custom instances of this class as extension provides an instance on its own. This can be
* invalidated particulary by static builds in which case a global variable may not get into resulting binary.
*
* Types registered as meta types:
* - Serviceable *
* .
*
* Classes registered as meta types can be used in string-based, queued signal-slot connections and various functions that rely on
* QMetaType features.
*/
class CUTEHMI_SERVICES_API Initializer final:
public ExtensionInitializer<Initializer>
{
public:
/**
* Default constructor.
*/
Initializer();
};
}
}
#endif // INCLUDECUTEHMIMODBUSINITIALIZER_HPP
#ifndef INCLUDECUTEHMISERVICESPOLLINGCONTROLLER_HPP
#define INCLUDECUTEHMISERVICESPOLLINGCONTROLLER_HPP
#include "internal/common.hpp"
#include <QObject>
namespace cutehmi {
namespace services {
/**
* Polling timer. Polling timer is a simple, single-shot timer, useful when dealing with polling. It allows for dynamic
* creation of subtimers, which may be used to control execution of individual tasks into which polling process can be decomposed.
*/
class CUTEHMI_SERVICES_API PollingTimer:
public QObject
{
Q_OBJECT
public:
static constexpr int INITIAL_INTERVAL = 250;
static constexpr int INITIAL_SUBTIMER_INTERVAL = 10;
Q_PROPERTY(int interval READ interval WRITE setlInterval NOTIFY intervalChanged)
Q_PROPERTY(PollingTimer * subtimer READ subtimer CONSTANT)
explicit PollingTimer(int interval = INITIAL_INTERVAL, QObject * parent = nullptr);
int interval() const;
void setlInterval(int interval);
/**
* Get subtimer. Subtimers are created dynamically, whenever some piece of code wants to access it.
* @return subtimer.
*/
PollingTimer * subtimer();
public slots:
/**
* Start timer.
*/
void start();
signals:
void intervalChanged();
void triggered();
private slots:
void shoot();
private:
struct Members {
int interval;
PollingTimer * subtimer;
};
MPtr<Members> m;
};
}
}
#endif // INCLUDECUTEHMISERVICESPOLLINGCONTROLLER_HPP
#ifndef INCLUDECUTEHMISERVICESSERVICE_HPP
#define INCLUDECUTEHMISERVICESSERVICE_HPP
#include "internal/common.hpp"
#include "internal/StateInterface.hpp"
#include "Serviceable.hpp"
#include <QString>
#include <QVariant>
#include <QObject>
#include <QStateMachine>
#include <QTimer>
namespace cutehmi {
namespace services {
/**
* Service.
*/
class CUTEHMI_SERVICES_API Service:
public QObject
{
Q_OBJECT
friend class ServiceManager;
public:
static constexpr int INITIAL_STOP_TIMEOUT = 30000;
static constexpr const char * INITIAL_NAME = "Unnamed Service";
Q_PROPERTY(int stopTimeout READ stopTimeout WRITE setStopTimeout NOTIFY stopTimeoutChanged)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString status READ status NOTIFY statusChanged)
Q_PROPERTY(QVariant serviceable READ serviceable WRITE setServiceable NOTIFY serviceableChanged)
Q_CLASSINFO("DefaultProperty", "serviceable")
Service(QObject * parent = nullptr);
~Service() override;
int stopTimeout() const;
/**
* Set timeout.
* @param timeout amount of time [milliseconds] after which stopping sequence should be interrupted. Setting a negative
* value will disable timeout functionality.
*/
void setStopTimeout(int stopTimeout);
QString name() const;
void setName(const QString & name);
QString status() const;
/**
* Set serviced object. Object must implement Serviceable interface. Whole communication between service and
* @a serviceable is accomplished through state interface. State interface can not be accessed directly. Instead,
* @a serviceable is asked to configure states of the service via Serviceable interface.
* @param serviceable serviceable object to be maintained by this service. Object must implement Serviceable interface.
*/
void setServiceable(QVariant serviceable);
/**
* Get serviced object.
* @return serviced object. QVariant wraps Serviceable * pointer.
*/
QVariant serviceable() const;
public slots:
void start();
void stop();
signals:
void stopTimeoutChanged();
void nameChanged();
void statusChanged();
void serviceableChanged();
void started();
void stopped();
void activated();
protected:
internal::StateInterface * stateInterface();
const internal::StateInterface * stateInterface() const;
protected slots:
void activate();
void setStatus(const QString & status);
private:
static QString & DefaultStatus();
void destroyStateMachine();
void initializeStateMachine(Serviceable & serviceable);
void addTransition(QState * source, QState * target, std::unique_ptr<QAbstractTransition>);
void addStatuses(std::unique_ptr<Serviceable::ServiceStatuses> statuses);
struct Members {
int stopTimeout = INITIAL_STOP_TIMEOUT;
QString name = INITIAL_NAME;
QString status;
Serviceable * serviceable = nullptr;
QStateMachine * stateMachine = nullptr;
internal::StateInterface * stateInterface = nullptr;
QTimer timeoutTimer;
};
MPtr<Members> m;
};
}
}
#endif // INCLUDECUTEHMISERVICESSERVICE_HPP
#ifndef H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_SERVICELISTMODEL_HPP
#define H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_SERVICELISTMODEL_HPP
#include "internal/common.hpp"
#include "Service.hpp"
#include <QAbstractListModel>
namespace cutehmi {
namespace services {