File indexing completed on 2025-07-06 04:09:55

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2012 Barth Netterfield                                *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "dataobjectscriptinterface.h"
0014 
0015 #include "objectstore.h"
0016 
0017 #include <QStringBuilder>
0018 
0019 namespace Kst {
0020 
0021 
0022 /***************************/
0023 /* dataobject commands     */
0024 /***************************/
0025 QString DataObjectSI::setInputVector(QString& command) {
0026   QStringList vars = getArgs(command);
0027 
0028   QString key = vars.at(0);
0029   QString vec_name = vars.at(1);
0030 
0031   VectorPtr v = kst_cast<Vector>(_dataObject->store()->retrieveObject(vec_name));
0032   if (v) {
0033     _dataObject->setInputVector(key, v);
0034     return "Done";
0035   } else {
0036     return QString("Vector %1 not found").arg(vec_name);
0037   }
0038 }
0039 
0040 QString DataObjectSI::setInputScalar(QString& command) {
0041   QStringList vars = getArgs(command);
0042 
0043   QString key = vars.at(0);
0044   QString x_name = vars.at(1);
0045 
0046   ScalarPtr x = kst_cast<Scalar>(_dataObject->store()->retrieveObject(x_name));
0047   if (x) {
0048     _dataObject->setInputScalar(key, x);
0049     return "Done";
0050   } else {
0051     return QString("Scalar %1 not found").arg(x_name);
0052   }
0053 }
0054 
0055 
0056 QString DataObjectSI::outputVector(QString& command) {
0057   QString key = getArg(command);
0058 
0059   VectorPtr vout = _dataObject->outputVector(key);
0060   if (vout) {
0061     return vout->shortName();
0062   } else {
0063     return "Invalid";
0064   }
0065 
0066 }
0067 
0068 
0069 QString DataObjectSI::outputScalar(QString& command) {
0070   QString key = getArg(command);
0071 
0072   ScalarPtr xout = _dataObject->outputScalar(key);
0073   if (xout) {
0074     return xout->shortName();
0075   } else {
0076     return "Invalid";
0077   }
0078 
0079 }
0080 
0081 
0082 QString EquationSI::interpolateVectors(QString& command) {
0083   QString key = getArg(command);
0084 
0085   _equation->setDoInterp(key.toLower() == "true");
0086 
0087   return("Done");
0088 }
0089 
0090 
0091 /***************************/
0092 /* PluginSI                */
0093 /***************************/
0094 PluginSI::PluginSI(BasicPluginPtr plugin) {
0095   if (plugin) {
0096     _plugin = plugin;
0097     _dataObject = plugin;
0098   } else {
0099     _plugin = 0;
0100     _dataObject = 0;
0101   }
0102 
0103   _fnMap.insert("setInputVector",&PluginSI::setInputVector);
0104   _fnMap.insert("setInputScalar",&PluginSI::setInputScalar);
0105   _fnMap.insert("outputVector",&PluginSI::outputVector);
0106   _fnMap.insert("outputScalar",&PluginSI::outputScalar);
0107 
0108   _fnMap.insert("setProperty",&PluginSI::setProperty);
0109 
0110 }
0111 
0112 
0113 QString PluginSI::setProperty(QString& command) {
0114   QStringList vars = getArgs(command);
0115 
0116   QString key = vars.at(0);
0117   QString val = vars.at(1);
0118 
0119   if (_plugin) {
0120     _plugin->setProperty(key, val);
0121   }
0122 
0123   return "Done";
0124 }
0125 
0126 
0127 bool PluginSI::isValid() {
0128   return _plugin;
0129 }
0130 
0131 QByteArray PluginSI::endEditUpdate() {
0132   if (_plugin) {
0133     _plugin->registerChange();
0134     UpdateManager::self()->doUpdates(true);
0135     UpdateServer::self()->requestUpdateSignal();
0136 
0137     return ("Finished editing "%_plugin->Name()).toLatin1();
0138   } else {
0139     return ("Finished editing invalid data object");
0140   }
0141 }
0142 
0143 QString PluginSI::doCommand(QString command_in) {
0144 
0145   if (isValid()) {
0146 
0147     QString command = command_in.left(command_in.indexOf('('));
0148 
0149     PluginInterfaceMemberFn fn=_fnMap.value(command,&PluginSI::noSuchFn);
0150 
0151     if(fn!=&PluginSI::noSuchFn) {
0152       return CALL_MEMBER_FN(*this,fn)(command_in);
0153     }
0154 
0155 
0156     QString v=doObjectCommand(command_in, _plugin);
0157     if (!v.isEmpty()) {
0158       return v;
0159     }
0160 
0161     return "No such command";
0162   } else {
0163     return "Invalid";
0164   }
0165 
0166 }
0167 
0168 ScriptInterface* PluginSI::newPlugin(ObjectStore *store, QByteArray pluginName) {
0169   DataObjectConfigWidget* configWidget = DataObject::pluginWidget(pluginName);
0170 
0171   if (configWidget) {
0172       BasicPluginPtr plugin = kst_cast<BasicPlugin>(DataObject::createPlugin(pluginName, store, configWidget));
0173       return new PluginSI(kst_cast<BasicPlugin>(plugin));
0174   }
0175 
0176   return 0L;
0177 }
0178 
0179 /***************************/
0180 /* EquationSI              */
0181 /***************************/
0182 EquationSI::EquationSI(EquationPtr equation) {
0183   if (equation) {
0184     _equation = equation;
0185     _dataObject = equation;
0186   } else {
0187     _equation = 0;
0188     _dataObject = 0;
0189   }
0190 
0191   _fnMap.insert("setEquation",&EquationSI::setEquation);
0192   _fnMap.insert("equation",&EquationSI::equation);
0193 
0194   _fnMap.insert("setInputVector",&EquationSI::setInputVector);
0195   _fnMap.insert("setInputScalar",&EquationSI::setInputScalar);
0196   _fnMap.insert("outputVector",&EquationSI::outputVector);
0197   _fnMap.insert("outputScalar",&EquationSI::outputScalar);
0198   _fnMap.insert("interpolateVectors",&EquationSI::interpolateVectors);
0199 
0200 }
0201 
0202 bool EquationSI::isValid() {
0203   return _equation;
0204 }
0205 
0206 QByteArray EquationSI::endEditUpdate() {
0207   if (_equation) {
0208     _equation->registerChange();
0209     UpdateManager::self()->doUpdates(true);
0210     UpdateServer::self()->requestUpdateSignal();
0211 
0212     return ("Finished editing "%_equation->Name()).toLatin1();
0213   } else {
0214     return ("Finished editing invalid equation");
0215   }
0216 }
0217 
0218 QString EquationSI::doCommand(QString command_in) {
0219   if (isValid()) {
0220 
0221     QString command = command_in.left(command_in.indexOf('('));
0222 
0223     EquationInterfaceMemberFn fn=_fnMap.value(command,&EquationSI::noSuchFn);
0224 
0225     if(fn!=&EquationSI::noSuchFn) {
0226       return CALL_MEMBER_FN(*this,fn)(command_in);
0227     }
0228 
0229 
0230     QString v=doObjectCommand(command_in, _equation);
0231     if (!v.isEmpty()) {
0232       return v;
0233     }
0234 
0235     return "No such command";
0236   } else {
0237     return "Invalid";
0238   }
0239 
0240 }
0241 
0242 ScriptInterface* EquationSI::newEquation(ObjectStore *store) {
0243   EquationPtr equation = store->createObject<Equation>();
0244 
0245   return new EquationSI(equation);
0246 }
0247 
0248 
0249 QString EquationSI::equation(QString&) {
0250   if (_equation) {
0251     return _equation->equation();
0252   } else {
0253     return "Invalid";
0254   }
0255 }
0256 
0257 
0258 QString EquationSI::setEquation(QString& command) {
0259   if (_equation) {
0260     QString eq = getArg(command);
0261 
0262     _equation->setEquation(eq);
0263     return "done";
0264   } else {
0265     return "Invalid";
0266   }
0267 }
0268 
0269 
0270 /***************************/
0271 /* SpectrumSI              */
0272 /***************************/
0273 SpectrumSI::SpectrumSI(PSDPtr psd) {
0274   if (psd) {
0275     _psd = psd;
0276     _dataObject = psd;
0277   } else {
0278     _psd = 0;
0279     _dataObject = 0;
0280   }
0281 
0282   _fnMap.insert("change",&SpectrumSI::change);
0283   _fnMap.insert("sampleRate",&SpectrumSI::sampleRate);
0284 
0285   _fnMap.insert("interleavedAverage",&SpectrumSI::interleavedAverage);
0286   _fnMap.insert("fftLength",&SpectrumSI::fftLength);
0287   _fnMap.insert("apodize",&SpectrumSI::apodize);
0288   _fnMap.insert("removeMean",&SpectrumSI::removeMean);
0289   _fnMap.insert("vectorUnits",&SpectrumSI::vectorUnints);
0290   _fnMap.insert("rateUnits",&SpectrumSI::rateUnits);
0291   _fnMap.insert("apodizeFunctionIndex",&SpectrumSI::apodizeFunctionIndex);
0292   _fnMap.insert("gaussianSigma",&SpectrumSI::gaussianSigma);
0293   _fnMap.insert("outputTypeIndex",&SpectrumSI::outputTypeIndex);
0294 
0295   _fnMap.insert("setInputVector",&SpectrumSI::setInputVector);
0296   _fnMap.insert("outputVector",&SpectrumSI::outputVector);
0297 
0298 }
0299 
0300 bool SpectrumSI::isValid() {
0301   return _psd;
0302 }
0303 
0304 QByteArray SpectrumSI::endEditUpdate() {
0305   if (_psd) {
0306     _psd->registerChange();
0307     UpdateManager::self()->doUpdates(true);
0308     UpdateServer::self()->requestUpdateSignal();
0309 
0310     return ("Finished editing "%_psd->Name()).toLatin1();
0311   } else {
0312     return ("Finished editing invalid spectrum");
0313   }
0314 }
0315 
0316 QString SpectrumSI::doCommand(QString command_in) {
0317   if (isValid()) {
0318 
0319     QString command = command_in.left(command_in.indexOf('('));
0320 
0321     SpectrumInterfaceMemberFn fn=_fnMap.value(command,&SpectrumSI::noSuchFn);
0322 
0323     if(fn!=&SpectrumSI::noSuchFn) {
0324       return CALL_MEMBER_FN(*this,fn)(command_in);
0325     }
0326 
0327 
0328     QString v=doObjectCommand(command_in, _psd);
0329     if (!v.isEmpty()) {
0330       return v;
0331     }
0332 
0333     return "No such command";
0334   } else {
0335     return "Invalid";
0336   }
0337 }
0338 
0339 ScriptInterface* SpectrumSI::newSpectrum(ObjectStore *store) {
0340   PSDPtr psd = store->createObject<PSD>();
0341 
0342   return new SpectrumSI(psd);
0343 }
0344 
0345 
0346 QString SpectrumSI::change(QString& command) {
0347   if (_psd) {
0348     QStringList vars = getArgs(command);
0349 
0350     QString vec_name = vars.at(0);
0351     VectorPtr vector = kst_cast<Vector>(_dataObject->store()->retrieveObject(vec_name));
0352 
0353 
0354     _psd->change(vector,
0355                  vars.at(1).toDouble(),            // sample_rate
0356                  (vars.at(2).toLower() == "true"), // interleaved_average,
0357                  vars.at(3).toInt(),               // fft_length,
0358                  (vars.at(4).toLower() == "true"), // apodize,
0359                  (vars.at(5).toLower() == "true"), // remove_mean,
0360                  vars.at(6),                       // vector unints
0361                  vars.at(7),                       // rate units
0362                  ApodizeFunction(vars.at(8).toInt()), // apodizeFunction,
0363                  vars.at(9).toDouble(),            // sigma,
0364                  PSDType(vars.at(10).toInt())     // output type
0365                  );
0366     
0367     
0368     return "done";
0369   } else {
0370     return "Invalid";
0371   }
0372 }
0373 
0374 QString SpectrumSI::sampleRate(QString&) {
0375   if (_psd) {
0376     return QString::number(_psd->frequency());
0377   } else {
0378     return "Invalid";
0379   }
0380 }
0381 
0382 QString SpectrumSI::interleavedAverage(QString &) {
0383   if (_psd) {
0384     if (_psd->average()) {
0385       return "True";
0386     } else {
0387       return "False";
0388     }
0389   } else {
0390     return "Invalid";
0391   }
0392 }
0393 
0394 
0395 QString SpectrumSI::fftLength(QString &) {
0396   if (_psd) {
0397     return QString::number(_psd->frequency());
0398   } else {
0399     return "Invalid";
0400   }
0401 }
0402 
0403 QString SpectrumSI::apodize(QString &) {
0404   if (_psd) {
0405     if (_psd->apodize()) {
0406       return "True";
0407     } else {
0408       return "False";
0409     }
0410   } else {
0411     return "Invalid";
0412   }
0413 }
0414 
0415 QString SpectrumSI::removeMean(QString &) {
0416   if (_psd) {
0417     if (_psd->removeMean()) {
0418       return "True";
0419     } else {
0420       return "False";
0421     }
0422   } else {
0423     return "Invalid";
0424   }
0425 }
0426 
0427 QString SpectrumSI::vectorUnints(QString &) {
0428   if (_psd) {
0429     return _psd->vectorUnits();
0430   } else {
0431     return "Invalid";
0432   }
0433 }
0434 
0435 QString SpectrumSI::rateUnits(QString &) {
0436   if (_psd) {
0437     return _psd->rateUnits();
0438   } else {
0439     return "Invalid";
0440   }
0441 }
0442 
0443 QString SpectrumSI::apodizeFunctionIndex(QString &) {
0444   if (_psd) {
0445     return QString::number(int(_psd->apodizeFxn()));
0446   } else {
0447     return "Invalid";
0448   }
0449 }
0450 
0451 QString SpectrumSI::gaussianSigma(QString &) {
0452   if (_psd) {
0453     return QString::number(_psd->gaussianSigma());
0454   } else {
0455     return "Invalid";
0456   }
0457 }
0458 
0459 QString SpectrumSI::outputTypeIndex(QString &) {
0460  if (_psd) {
0461    return QString::number(int(_psd->output()));
0462  } else {
0463    return "Invalid";
0464  }
0465 }
0466 
0467 /***************************/
0468 /* HistogramSI             */
0469 /***************************/
0470 HistogramSI::HistogramSI(HistogramPtr histogram) {
0471   if (histogram) {
0472     _histogram = histogram;
0473     _dataObject = histogram;
0474   } else {
0475     _histogram = 0;
0476     _dataObject = 0;
0477   }
0478 
0479   _fnMap.insert("change",&HistogramSI::change);
0480 
0481   _fnMap.insert("xMin",&HistogramSI::xMin);
0482   _fnMap.insert("xMax",&HistogramSI::xMax);
0483   _fnMap.insert("nBins",&HistogramSI::nBins);
0484   _fnMap.insert("normalizationType",&HistogramSI::normalizationType);
0485   _fnMap.insert("autoBin",&HistogramSI::autoBin);
0486 
0487   _fnMap.insert("setInputVector",&HistogramSI::setInputVector);
0488   _fnMap.insert("outputVector",&HistogramSI::outputVector);
0489 }
0490 
0491 bool HistogramSI::isValid() {
0492   return _histogram;
0493 }
0494 
0495 QByteArray HistogramSI::endEditUpdate() {
0496   if (_histogram) {
0497     _histogram->registerChange();
0498     UpdateManager::self()->doUpdates(true);
0499     UpdateServer::self()->requestUpdateSignal();
0500 
0501     return ("Finished editing "%_histogram->Name()).toLatin1();
0502   } else {
0503     return ("Finished editing invalid histogram");
0504   }
0505 }
0506 
0507 QString HistogramSI::doCommand(QString command_in) {
0508   if (isValid()) {
0509 
0510     QString command = command_in.left(command_in.indexOf('('));
0511 
0512     HistogramInterfaceMemberFn fn=_fnMap.value(command,&HistogramSI::noSuchFn);
0513 
0514     if(fn!=&HistogramSI::noSuchFn) {
0515       return CALL_MEMBER_FN(*this,fn)(command_in);
0516     }
0517 
0518     QString v=doObjectCommand(command_in, _histogram);
0519     if (!v.isEmpty()) {
0520       return v;
0521     }
0522 
0523     return "No such command";
0524   } else {
0525     return "Invalid";
0526   }
0527 }
0528 
0529 ScriptInterface* HistogramSI::newHistogram(ObjectStore *store) {
0530   HistogramPtr histogram = store->createObject<Histogram>();
0531 
0532   return new HistogramSI(histogram);
0533 }
0534 
0535 QString HistogramSI::change(QString& command) {
0536   if (_histogram) {
0537     QStringList vars = getArgs(command);
0538 
0539     QString vec_name = vars.at(0);
0540     VectorPtr vector = kst_cast<Vector>(_dataObject->store()->retrieveObject(vec_name));
0541 
0542 
0543     _histogram->change(vector,
0544                        vars.at(1).toDouble(),            // xmin
0545                        vars.at(2).toDouble(),            // xmax
0546                        vars.at(3).toInt(),               // nbins
0547                        Histogram::NormalizationType(vars.at(4).toInt()), // normalization type
0548                        (vars.at(5).toLower() == "true") // real time autobin
0549                        );
0550 
0551     return "done";
0552   } else {
0553     return "Invalid";
0554   }
0555 }
0556 
0557 QString HistogramSI::xMin(QString &) {
0558   if (_histogram) {
0559     return QString::number(double(_histogram->xMin()));
0560   } else {
0561     return "Invalid";
0562   }
0563 }
0564 
0565 QString HistogramSI::xMax(QString &) {
0566   if (_histogram) {
0567     return QString::number(double(_histogram->xMax()));
0568   } else {
0569     return "Invalid";
0570   }
0571 }
0572 
0573 QString HistogramSI::nBins(QString &) {
0574   if (_histogram) {
0575     return QString::number(int(_histogram->numberOfBins()));
0576   } else {
0577     return "Invalid";
0578   }
0579 }
0580 
0581 QString HistogramSI::normalizationType(QString &) {
0582   if (_histogram) {
0583     return QString::number(int(_histogram->normalizationType()));
0584   } else {
0585     return "Invalid";
0586   }
0587 }
0588 
0589 QString HistogramSI::autoBin(QString &) {
0590   if (_histogram) {
0591     if (_histogram->realTimeAutoBin()) {
0592       return "True";
0593     } else {
0594       return "False";
0595     }
0596   } else {
0597     return "Invalid";
0598   }
0599 }
0600 
0601 }