267 changes: 267 additions & 0 deletions src/libkstmath/relationscriptinterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/***************************************************************************
* *
* copyright : (C) 2014 Barth Netterfield *
* netterfield@astro.utoronto.ca *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "relationscriptinterface.h"

#include "objectstore.h"
#include "datasourcepluginmanager.h"
#include "updatemanager.h"
#include "updateserver.h"
#include "vector.h"
#include "colorsequence.h"

#include <QStringBuilder>


namespace Kst {

QString doRelationScriptCommand(QString command,Relation *relation) {

QString v=ScriptInterface::doNamedObjectCommand(command, relation);
if (!v.isEmpty()) {
return v;
}

if (command.startsWith("maxX(")) {
return QString::number(relation->maxX());
} // FIXME the rest...

return QString();
}


/******************************************************/
/* Curves */
/******************************************************/
CurveSI::CurveSI(CurvePtr it) {
curve=it;

_fnMap.insert("setXVector",&CurveSI::setXVector);
_fnMap.insert("setYVector",&CurveSI::setYVector);
_fnMap.insert("setXError",&CurveSI::setXError);
_fnMap.insert("setYError",&CurveSI::setYError);
_fnMap.insert("setXMinusError",&CurveSI::setXMinusError);
_fnMap.insert("setYMinusError",&CurveSI::setYMinusError);

_fnMap.insert("setColor",&CurveSI::setColor);
_fnMap.insert("setHeadColor",&CurveSI::setHeadColor);
_fnMap.insert("setBarFillColor",&CurveSI::setBarFillColor);
_fnMap.insert("setHasPoints",&CurveSI::setHasPoints);
_fnMap.insert("setHasLines",&CurveSI::setHasLines);
_fnMap.insert("setHasBars",&CurveSI::setHasBars);
_fnMap.insert("setHasHead",&CurveSI::setHasHead);

_fnMap.insert("setLineWidth",&CurveSI::setLineWidth);
_fnMap.insert("setPointSize",&CurveSI::setPointSize);

}

QString CurveSI::doCommand(QString command_in) {
QString command = command_in.left(command_in.indexOf('('));

CurveInterfaceMemberFn fn=_fnMap.value(command,&CurveSI::noSuchFn);

if(fn!=&CurveSI::noSuchFn) {
return CALL_MEMBER_FN(*this,fn)(command_in);
}


QString v=doRelationScriptCommand(command_in, curve);
if (!v.isEmpty()) {
return v;
}

return "No such command";
}

bool CurveSI::isValid() {
return curve.isPtrValid();
}

ScriptInterface* CurveSI::newCurve(ObjectStore *store) {
CurvePtr curve;
curve = store->createObject<Curve>();
curve->setColor(ColorSequence::self().next());

return new CurveSI(curve);
}

QByteArray CurveSI::endEditUpdate() {
curve->registerChange();
UpdateManager::self()->doUpdates(true);
UpdateServer::self()->requestUpdateSignal();
return ("Finished editing "+curve->Name()).toLatin1();
}

/***************************/
/* commands */
/***************************/
QString CurveSI::setXVector(QString& command) {
QString parameter = getArg(command);

VectorPtr v = kst_cast<Vector>(curve->store()->retrieveObject(parameter));
if (v) {
curve->setXVector(v);
return "Done";
} else {
return QString("Vector %1 not found").arg(parameter);
}

}

QString CurveSI::setYVector(QString& command) {
QString parameter = getArg(command);

VectorPtr v = kst_cast<Vector>(curve->store()->retrieveObject(parameter));
if (v) {
curve->setYVector(v);
return "Done";
} else {
return QString("Vector %1 not found").arg(parameter);
}
}

QString CurveSI::setXError(QString& command) {
QString parameter = getArg(command);

VectorPtr v = kst_cast<Vector>(curve->store()->retrieveObject(parameter));
if (v) {
curve->setXError(v);
return "Done";
} else {
return QString("Vector %1 not found").arg(parameter);
}
}

QString CurveSI::setYError(QString& command) {
QString parameter = getArg(command);

VectorPtr v = kst_cast<Vector>(curve->store()->retrieveObject(parameter));
if (v) {
curve->setYError(v);
return "Done";
} else {
return QString("Vector %1 not found").arg(parameter);
}
}

QString CurveSI::setXMinusError(QString& command) {
QString parameter = getArg(command);

VectorPtr v = kst_cast<Vector>(curve->store()->retrieveObject(parameter));
if (v) {
curve->setXMinusError(v);
return "Done";
} else {
return QString("Vector %1 not found").arg(parameter);
}

}

QString CurveSI::setYMinusError(QString& command) {
QString parameter = getArg(command);

VectorPtr v = kst_cast<Vector>(curve->store()->retrieveObject(parameter));
if (v) {
curve->setYMinusError(v);
return "Done";
} else {
return QString("Vector %1 not found").arg(parameter);
}
}

QString CurveSI::setColor(QString& command) {
QString parameter = getArg(command);
curve->setColor(QColor(parameter));
return "Done";
}

QString CurveSI::setHeadColor(QString& command) {
QString parameter = getArg(command);
curve->setHeadColor(QColor(parameter));
return "Done";
}

QString CurveSI::setBarFillColor(QString& command) {
QString parameter = getArg(command);
curve->setBarFillColor(QColor(parameter));
return "Done";
}

QString CurveSI::setHasPoints(QString& command) {
QString parameter = getArg(command);

if (parameter.toLower() == "true") {
curve->setHasPoints(true);
} else {
curve->setHasPoints(false);
}
return "Done";
}

QString CurveSI::setHasLines(QString& command) {
QString parameter = getArg(command);

if (parameter.toLower() == "true") {
curve->setHasLines(true);
} else {
curve->setHasLines(false);
}
return "Done";
}

QString CurveSI::setHasBars(QString& command) {
QString parameter = getArg(command);

if (parameter.toLower() == "true") {
curve->setHasBars(true);
} else {
curve->setHasBars(false);
}
return "Done";
}

QString CurveSI::setHasHead(QString& command) {
QString parameter = getArg(command);

if (parameter.toLower() == "true") {
curve->setHasHead(true);
} else {
curve->setHasHead(false);
}
return "Done";
}

QString CurveSI::setLineWidth(QString& command) {
QString parameter = getArg(command);
int x = parameter.toInt();

if (x<0) x = 0;
if (x>100) x = 100;

curve->setLineWidth(x);
return "Done";
}

QString CurveSI::setPointSize(QString& command) {
QString parameter = getArg(command);
int x = parameter.toInt();

if (x<0) x = 0;
if (x>100) x = 100;

curve->setPointSize(x);
return "Done";
}


}
67 changes: 67 additions & 0 deletions src/libkstmath/relationscriptinterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***************************************************************************
* *
* copyright : (C) 2014 Barth Netterfield *
* netterfield@astro.utoronto.ca *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef RELATIONSCRIPTINTERFACE_H
#define RELATIONSCRIPTINTERFACE_H

#include <QString>

#include "scriptinterface.h"
#include "curve.h"

namespace Kst {

class CurveSI;
typedef QString (CurveSI::*CurveInterfaceMemberFn)(QString& command);

class KSTMATH_EXPORT CurveSI : public ScriptInterface
{
Q_OBJECT
public:
explicit CurveSI(CurvePtr it);
QString doCommand(QString);
bool isValid();
QByteArray endEditUpdate();

static ScriptInterface* newCurve(ObjectStore *store);

protected:
QString noSuchFn(QString&) {return ""; }

private:
CurvePtr curve;

QMap<QString,CurveInterfaceMemberFn> _fnMap;

QString setXVector(QString& command);
QString setYVector(QString& command);
QString setXError(QString& command);
QString setYError(QString& command);
QString setXMinusError(QString& command);
QString setYMinusError(QString& command);

QString setColor(QString& command);
QString setHeadColor(QString& command);
QString setBarFillColor(QString& command);
QString setHasPoints(QString& command);
QString setHasLines(QString& command);
QString setHasBars(QString& command);
QString setHasHead(QString& command);

QString setLineWidth(QString& command);
QString setPointSize(QString& command);

};

}

#endif // RELATIONSCRIPTINTERFACE_H