File indexing completed on 2024-12-22 04:17:22

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2014 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 "scalarscriptinterface.h"
0014 
0015 #include "objectstore.h"
0016 #include "datasourcepluginmanager.h"
0017 #include "updatemanager.h"
0018 #include "updateserver.h"
0019 
0020 #include <QStringBuilder>
0021 
0022 
0023 namespace Kst {
0024 
0025 /******************************************************/
0026 /* Generated Scalars                                  */
0027 /******************************************************/
0028 ScalarGenSI::ScalarGenSI(ScalarPtr it) : scalar(it) {
0029     //scalar=it;
0030 }
0031 
0032 QString ScalarGenSI::doCommand(QString x) {
0033 
0034   QString v=doObjectCommand(x, scalar);
0035   if (!v.isEmpty()) {
0036     return v;
0037   }
0038 
0039   if (x.startsWith(QLatin1String("setValue("))) {
0040     scalar->writeLock();
0041     scalar->setValue(x.remove("setValue(").remove(')').toDouble());
0042     scalar->unlock();
0043     return "Done";
0044   } else if (x.startsWith(QLatin1String("value()"))) {
0045     return QString::number(scalar->value());
0046   }
0047   return "No such command";
0048 }
0049 
0050 bool ScalarGenSI::isValid() {
0051   return scalar.isPtrValid();
0052 }
0053 
0054 ScriptInterface* ScalarGenSI::newScalar(ObjectStore *store) {
0055   ScalarPtr scalar;
0056   scalar = store->createObject<Scalar>();
0057   scalar->setOrphan(true);
0058   scalar->setEditable(true);
0059   return new ScalarGenSI(scalar);
0060 }
0061 
0062 QByteArray ScalarGenSI::endEditUpdate() {
0063   UpdateManager::self()->doUpdates(true);
0064   UpdateServer::self()->requestUpdateSignal();
0065   return ("Finished editing "+scalar->Name()).toLatin1();
0066 }
0067 
0068 /******************************************************/
0069 /* Data Scalars                                       */
0070 /******************************************************/
0071 ScalarDataSI::ScalarDataSI(DataScalarPtr it) : scalar(it) {
0072     //scalar=it;
0073 }
0074 
0075 QString ScalarDataSI::doCommand(QString x) {
0076 
0077   QString v=doObjectCommand(x, scalar);
0078   if (!v.isEmpty()) {
0079     return v;
0080   }
0081 
0082   if (x.startsWith(QLatin1String("change("))) {
0083     x.remove("change(").remove(')');
0084     QStringList p = x.split(',');
0085     DataSourcePtr ds = DataSourcePluginManager::findOrLoadSource(
0086                          scalar->store(), p.at(0));
0087     scalar->writeLock();
0088     scalar->change(ds,p.at(1));
0089     scalar->unlock();
0090     return "Done";
0091   } else if (x.startsWith(QLatin1String("file()"))) {
0092     return scalar->filename();
0093   } else if (x.startsWith(QLatin1String("field()"))) {
0094     return scalar->field();
0095   } else if (x.startsWith(QLatin1String("value()"))) {
0096     return QString::number(scalar->value());
0097   }
0098   return "No such command";
0099 }
0100 
0101 bool ScalarDataSI::isValid() {
0102   return scalar.isPtrValid();
0103 }
0104 
0105 ScriptInterface* ScalarDataSI::newScalar(ObjectStore *store) {
0106   DataScalarPtr scalar;
0107   scalar = store->createObject<DataScalar>();
0108   return new ScalarDataSI(scalar);
0109 }
0110 
0111 QByteArray ScalarDataSI::endEditUpdate() {
0112   scalar->registerChange();
0113   UpdateManager::self()->doUpdates(true);
0114   UpdateServer::self()->requestUpdateSignal();
0115   return ("Finished editing "+scalar->Name()).toLatin1();
0116 }
0117 
0118 /******************************************************/
0119 /* Vector Scalars                                       */
0120 /******************************************************/
0121 ScalarVectorSI::ScalarVectorSI(VScalarPtr it) : scalar(it) {
0122     //scalar=it;
0123 }
0124 
0125 QString ScalarVectorSI::doCommand(QString x) {
0126 
0127   QString v=doObjectCommand(x, scalar);
0128   if (!v.isEmpty()) {
0129     return v;
0130   }
0131 
0132   if (x.startsWith(QLatin1String("change("))) {
0133     x.remove("change(").remove(')');
0134     QStringList p = x.split(',');
0135     DataSourcePtr ds = DataSourcePluginManager::findOrLoadSource(
0136                          scalar->store(), p.at(0));
0137     scalar->writeLock();
0138     scalar->change(ds,p.at(1), p.at(2).toInt());
0139     scalar->unlock();
0140     return "Done";
0141   } else if (x.startsWith(QLatin1String("value()"))) {
0142     return QString::number(scalar->value());
0143   } else if (x.startsWith(QLatin1String("file()"))) {
0144     return scalar->filename();
0145   } else if (x.startsWith(QLatin1String("field()"))) {
0146     return scalar->field();
0147   } else if (x.startsWith(QLatin1String("frame()"))) {
0148     return QString::number(scalar->F0());
0149   }
0150   return "No such command";
0151 }
0152 
0153 bool ScalarVectorSI::isValid() {
0154   return scalar.isPtrValid();
0155 }
0156 
0157 ScriptInterface* ScalarVectorSI::newScalar(ObjectStore *store) {
0158   VScalarPtr scalar;
0159   scalar = store->createObject<VScalar>();
0160   return new ScalarVectorSI(scalar);
0161 }
0162 
0163 QByteArray ScalarVectorSI::endEditUpdate() {
0164   scalar->registerChange();
0165   UpdateManager::self()->doUpdates(true);
0166   UpdateServer::self()->requestUpdateSignal();
0167   return ("Finished editing "+scalar->Name()).toLatin1();
0168 }
0169 
0170 }