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 "actioninstance.h" 24 #include "script.h" 25 #include "code/color.h" 26 #include "code/point.h" 27 28 #include <QPoint> 29 30 namespace Actions 31 { 32 class VariableInstance : public ActionTools::ActionInstance 33 { 34 Q_OBJECT 35 Q_ENUMS(Type) 36 37 public: 38 enum Type 39 { 40 String, 41 Integer, 42 Float, 43 Color, 44 Position 45 }; 46 enum Exceptions 47 { 48 ConversionFailedException = ActionTools::ActionException::UserException 49 }; 50 51 VariableInstance(const ActionTools::ActionDefinition *definition, QObject *parent = nullptr) ActionInstance(definition,parent)52 : ActionTools::ActionInstance(definition, parent) {} 53 54 static Tools::StringListPair types; 55 startExecution()56 void startExecution() override 57 { 58 bool ok = true; 59 60 QString variable = evaluateString(ok, QStringLiteral("variable")); 61 QString value = evaluateString(ok, QStringLiteral("value")); 62 QColor colorValue = evaluateColor(ok, QStringLiteral("colorValue")); 63 QPoint positionValue = evaluatePoint(ok, QStringLiteral("positionValue")); 64 Type type = evaluateListElement<Type>(ok, types, QStringLiteral("type")); 65 66 if(!ok) 67 return; 68 69 switch(type) 70 { 71 case String: 72 { 73 setVariable(variable, value); 74 } 75 break; 76 case Integer: 77 { 78 bool ok; 79 setVariable(variable, value.toInt(&ok)); 80 if(!ok) 81 { 82 emit executionException(ConversionFailedException, tr("Cannot evaluate the value as an integer")); 83 return; 84 } 85 } 86 break; 87 case Float: 88 { 89 bool ok; 90 setVariable(variable, value.toFloat(&ok)); 91 if(!ok) 92 { 93 emit executionException(ConversionFailedException, tr("Cannot evaluate the value as a floating number")); 94 return; 95 } 96 } 97 break; 98 case Color: 99 { 100 setVariable(variable, Code::Color::constructor(colorValue, scriptEngine())); 101 } 102 break; 103 case Position: 104 { 105 setVariable(variable, Code::Point::constructor(positionValue, scriptEngine())); 106 } 107 break; 108 } 109 110 executionEnded(); 111 } 112 113 private: 114 Q_DISABLE_COPY(VariableInstance) 115 }; 116 } 117 118