trikRuntime
scriptExecutionControl.h
Go to the documentation of this file.
1 /* Copyright 2015 CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <QtCore/QList>
18 #include <QtCore/QTimer>
19 #include <QtCore/QStringList>
20 
21 namespace trikScriptRunner {
22 
24 class ScriptExecutionControl : public QObject
25 {
26  Q_OBJECT
27 
28 public:
29  ~ScriptExecutionControl() override;
30 
33  bool isInEventDrivenMode() const;
34 
36  Q_INVOKABLE QTimer *timer(int milliseconds);
37 
39  Q_INVOKABLE void wait(const int &milliseconds);
40 
42  Q_INVOKABLE qint64 time() const;
43 
45  Q_INVOKABLE int random(int from, int to) const;
46 
48  Q_INVOKABLE void system(const QString &command, bool synchronously = false);
49 
51  Q_INVOKABLE void writeToFile(const QString &file, const QString &text);
52 
54  Q_INVOKABLE QStringList readAll(const QString &file) const;
55 
57  Q_INVOKABLE void removeFile(const QString &file);
58 
59 public slots:
61  void run();
62 
64  void quit();
65 
67  void reset();
68 
69 signals:
71  void quitSignal();
72 
74  void stopWaiting();
75 
77  void sendMessage(const QString &text);
78 
79 private:
80  QList<QTimer *> mTimers; // Has ownership.
81 
84  bool mInEventDrivenMode = false;
85 };
86 
87 }
void run()
Starts event loop for script.
Definition: scriptExecutionControl.cpp:76
Definition: trikCommunicator.h:23
void stopWaiting()
To be connected to quit() slot of local event loops that are used for waiting.
Q_INVOKABLE QStringList readAll(const QString &file) const
Reads all lines from a text file and returns it as a list of strings.
Definition: scriptExecutionControl.cpp:111
bool isInEventDrivenMode() const
Returns true if a script is in event-driven running mode, so it shall wait for events when script is ...
Definition: scriptExecutionControl.cpp:81
void quitSignal()
Emitted when script requested system to abort execution.
~ScriptExecutionControl() override
Definition: scriptExecutionControl.cpp:27
void sendMessage(const QString &text)
Requests sending a message to a desktop.
Q_INVOKABLE void removeFile(const QString &file)
Removes a file.
Definition: scriptExecutionControl.cpp:129
void quit()
Aborts script execution.
Definition: scriptExecutionControl.cpp:86
Q_INVOKABLE void wait(const int &milliseconds)
Waits given amount of time in milliseconds and returns.
Definition: scriptExecutionControl.cpp:52
Q_INVOKABLE int random(int from, int to) const
Returns random number from an interval [from, to].
Definition: scriptExecutionControl.cpp:67
Script execution controller, provides related functions to scripts.
Definition: scriptExecutionControl.h:24
Q_INVOKABLE QTimer * timer(int milliseconds)
Starts a new timer with given interval and returns reference to it.
Definition: scriptExecutionControl.cpp:44
Q_INVOKABLE qint64 time() const
Returns the number of milliseconds since 1970-01-01T00:00:00 UTC.
Definition: scriptExecutionControl.cpp:62
Q_INVOKABLE void system(const QString &command, bool synchronously=false)
Execute given sh command.
Definition: scriptExecutionControl.cpp:91
void reset()
Resets script execution state, clearing all flags and stopping all timers.
Definition: scriptExecutionControl.cpp:32
Q_INVOKABLE void writeToFile(const QString &file, const QString &text)
Appends given text to the end of a file.
Definition: scriptExecutionControl.cpp:104