1 /*
2     Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5     Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10     Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #pragma once
22 
23 #include "executer_global.h"
24 #include "consolewidget.h"
25 #include "version.h"
26 
27 #include <QObject>
28 #include <QTimer>
29 #include <QElapsedTimer>
30 #include <QScriptEngineDebugger>
31 
32 namespace ActionTools
33 {
34 	class Script;
35 	class ActionFactory;
36 	class ActionInstance;
37 }
38 
39 class QStandardItemModel;
40 class QMainWindow;
41 class QScriptEngine;
42 class QProgressDialog;
43 
44 namespace LibExecuter
45 {
46 	class ExecutionWindow;
47 	class ScriptAgent;
48 
49 	class EXECUTERSHARED_EXPORT Executer : public QObject
50 	{
51 		Q_OBJECT
52 
53 	public:
54 		Executer(QObject *parent = nullptr);
55 		~Executer() override ;
56 
57 		void setup(ActionTools::Script *script,
58 				   ActionTools::ActionFactory *actionFactory,
59 				   bool showExecutionWindow,
60 				   int executionWindowPosition,
61 				   int executionWindowScreen,
62 				   bool showConsoleWindow,
63 				   int consoleWindowPosition,
64 				   int consoleWindowScreen,
65 				   int pauseBefore,
66 				   int pauseAfter,
67                    Tools::Version actionaVersion,
68 				   Tools::Version scriptVersion,
69 				   bool isActExec,
70 				   QStandardItemModel *consoleModel);
71 
executionWindow()72 		ExecutionWindow *executionWindow() const			{ return mExecutionWindow; }
consoleWidget()73 		ActionTools::ConsoleWidget *consoleWidget() const	{ return mConsoleWidget; }
scriptAgent()74 		ScriptAgent *scriptAgent() const					{ return mScriptAgent; }
75 
currentActionIndex()76 		int currentActionIndex() const						{ return mCurrentActionIndex; }
script()77 		ActionTools::Script *script() const					{ return mScript; }
78 
isExecuterRunning()79 		static bool isExecuterRunning()						{ return (mExecutionStatus != Stopped); }
80 
81         ActionTools::ActionInstance *currentActionInstance() const;
82 
83 	public slots:
84         bool startExecution(bool onlySelection, const QString &filename);
85 		void stopExecution();
86 		void pauseExecution();
87 		void debugExecution();
88 
89 	signals:
90 		void executionStopped();
91 		void scriptError(int actionIndex, const QString &parameter, const QString &error);
92 		void actionStarted(int actionIndex, int maxActions);
93 		void actionEnded(int actionIndex, int maxActions);
94 
95 	private slots:
96 		void executionException(int exception,
97 								const QString &message);
98 		void actionExecutionEnded();
99 		void disableAction(bool disable);
100 		void startNextAction();
101 		void startActionExecution();
102 		void updateTimerProgress();
103 		void showProgressDialog(const QString &title, int maximum);
104 		void updateProgressDialog(const QString &caption);
105 		void updateProgressDialog(int value);
106 		void hideProgressDialog();
107 		void executionPaused();
108 		void executionResumed();
109 		void consolePrint(const QString &text);
110 		void consolePrintWarning(const QString &text);
111 		void consolePrintError(const QString &text);
112 
113 	private:
114 		enum ExecuteActionResult
115 		{
116 			CanExecute,
117 			IncorrectLine,
118 			InvalidAction,
119 			DisabledAction,
120 			UnselectedAction
121 		};
122 		enum ExecutionStatus
123 		{
124 			Stopped,
125 			PrePause,
126 			Executing,
127 			PostPause
128 		};
129 
130 		void consolePrint(const QString &text, ActionTools::ConsoleWidget::Type type);
131 		void pauseOrDebug(bool debug);
132 		ExecuteActionResult canExecuteAction(const QString &line) const;
133 		ExecuteActionResult canExecuteAction(int index) const;
134 		void executeCurrentAction();
135 
136 		ActionTools::Script *mScript;
137 		ActionTools::ActionFactory *mActionFactory;
138 		bool mShowExecutionWindow;
139 		int mExecutionWindowPosition;
140 		int mExecutionWindowScreen;
141 		bool mShowConsoleWindow;
142 		int mConsoleWindowPosition;
143 		int mConsoleWindowScreen;
144 		ExecutionWindow *mExecutionWindow;
145 		ActionTools::ConsoleWidget *mConsoleWidget;
146 		int mCurrentActionIndex;
147 		bool mExecutionStarted;
148 		bool mExecutionEnded;
149 		QScriptEngine *mScriptEngine{nullptr};
150 		QScriptEngineDebugger mScriptEngineDebugger;
151 		QMainWindow *mDebuggerWindow;
152 		bool mExecuteOnlySelection;
153 		ScriptAgent *mScriptAgent{nullptr};
154 		QList<bool> mActionEnabled;
155 		QTimer mExecutionTimer;
156 		QElapsedTimer mExecutionTime;
157 		QProgressDialog *mProgressDialog;
158 		int mActiveActionsCount;
159 		bool mExecutionPaused;
160 		bool mHasExecuted{false};
161 		static ExecutionStatus mExecutionStatus;
162 		bool mPauseInterrupt{false};
163 		int mPauseBefore;
164 		int mPauseAfter;
165         Tools::Version mActionaVersion;
166 		Tools::Version mScriptVersion;
167 		bool mIsActExec;
168         bool mShowDebuggerOnCodeError{true};
169 
170 		Q_DISABLE_COPY(Executer)
171 	};
172 }
173 
174