QPoint ActionInstance::evaluatePoint(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QPoint(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); const SubParameter &unitSubParameter = retreiveSubParameter(parameterName, "unit"); QString result; if(subParameter.isCode()) { QScriptValue evaluationResult = evaluateCode(ok, subParameter); if(Code::Point *codePoint = qobject_cast<Code::Point*>(evaluationResult.toQObject())) { QPointF point = QPointF(codePoint->point().x(), codePoint->point().y()); computePercentPosition(point, unitSubParameter); return QPoint(point.x(), point.y()); } result = evaluationResult.toString(); } else result = evaluateText(ok, subParameter); if(!ok) return QPoint(); if(result.isEmpty() || result == ":") return QPoint(); QStringList positionStringList = result.split(":"); if(positionStringList.count() != 2) { ok = false; emit executionException(ActionException::InvalidParameterException, tr("\"%1\" is not a valid position.").arg(result)); return QPoint(); } QPointF point(positionStringList.at(0).toFloat(&ok), positionStringList.at(1).toFloat(&ok)); if(!ok) { emit executionException(ActionException::InvalidParameterException, tr("\"%1\" is not a valid position.").arg(result)); return QPoint(); } computePercentPosition(point, unitSubParameter); return QPoint(point.x(), point.y()); }
QDateTime ActionInstance::evaluateDateTime(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QDateTime(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString result; if(subParameter.isCode()) { QScriptValue evaluationResult = evaluateCode(ok, subParameter); if(evaluationResult.isDate()) return evaluationResult.toDateTime(); result = evaluationResult.toString(); } else result = evaluateText(ok, subParameter); if(!ok) return QDateTime(); QDateTime dateTime = QDateTime::fromString(result, QStringLiteral("dd/MM/yyyy hh:mm:ss")); if(!dateTime.isValid()) { ok = false; return QDateTime(); } return dateTime; }
QPolygon ActionInstance::evaluatePolygon(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QPolygon(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString result; if(subParameter.isCode()) result = evaluateCode(ok, subParameter).toString(); else result = evaluateText(ok, subParameter); if(!ok) return QPolygon(); if(result.isEmpty() || result == QLatin1String(";")) return QPolygon(); QStringList pointStrings = result.split(QLatin1Char(';'), QString::SkipEmptyParts); QPolygon polygon; for(const QString &pointString: pointStrings) { QStringList pointComponents = pointString.split(QLatin1Char(':'), QString::SkipEmptyParts); if(pointComponents.size() != 2) continue; polygon << QPoint(pointComponents.at(0).toInt(), pointComponents.at(1).toInt()); } return polygon; }
QColor ActionInstance::evaluateColor(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return {}; const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString result; if(subParameter.isCode()) { QScriptValue evaluationResult = evaluateCode(ok, subParameter); if(auto codeColor = qobject_cast<Code::Color*>(evaluationResult.toQObject())) return codeColor->color(); result = evaluationResult.toString(); } else result = evaluateText(ok, subParameter); if(!ok) return QColor(); if(result.isEmpty() || result == QLatin1String("::")) return QColor(); QStringList colorStringList = result.split(QStringLiteral(":")); if(colorStringList.count() != 3) { ok = false; emit executionException(ActionException::InvalidParameterException, tr("\"%1\" is not a valid color.").arg(result)); return QColor(); } QColor color = QColor(colorStringList.at(0).toInt(&ok), colorStringList.at(1).toInt(&ok), colorStringList.at(2).toInt(&ok)); if(!ok) { emit executionException(ActionException::InvalidParameterException, tr("\"%1\" is not a valid color.").arg(result)); return QColor(); } return color; }
QStringList ActionInstance::evaluateItemList(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QStringList(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString result; if(subParameter.isCode()) result = evaluateCode(ok, subParameter).toString(); else result = evaluateText(ok, subParameter); if(!ok) return QStringList(); return result.split(QLatin1Char('\n'), QString::SkipEmptyParts); }
QImage ActionInstance::evaluateImage(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QImage(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString filename; if(subParameter.isCode()) { QScriptValue evaluationResult = evaluateCode(ok, subParameter); if(auto codeImage = qobject_cast<Code::Image*>(evaluationResult.toQObject())) return codeImage->image(); if(!evaluationResult.isString()) { ok = false; emit executionException(ActionException::InvalidParameterException, tr("Invalid image.")); return QImage(); } filename = evaluationResult.toString(); } else filename = evaluateText(ok, subParameter); if(!ok || filename.isEmpty()) return QImage(); QImage image(filename); if(!image.isNull()) return image; ok = false; emit executionException(ActionException::InvalidParameterException, tr("Unable to load image: %1").arg(filename)); return QImage(); }
QString ActionInstance::evaluateString(bool &ok, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QString(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString result; if(subParameter.isCode()) result = evaluateCode(ok, subParameter).toString(); else result = evaluateText(ok, subParameter); if(!ok) return QString(); return result; }
QString ActionInstance::evaluateEditableListElement(bool &ok, const Tools::StringListPair &listElements, const QString ¶meterName, const QString &subParameterName) { if(!ok) return QString(); const SubParameter &subParameter = retreiveSubParameter(parameterName, subParameterName); QString result; if(subParameter.isCode()) result = evaluateCode(ok, subParameter).toString(); else result = evaluateText(ok, subParameter); if(!ok) return QString(); //Search in the translated items for(int i=0;i<listElements.second.size();++i) { if(listElements.second.at(i) == result) return listElements.first.at(i); } if(result.isEmpty()) { ok = false; setCurrentParameter(parameterName, subParameterName); emit executionException(ActionException::InvalidParameterException, tr("Please choose a value for this field.")); return QString(); } return result; }