File indexing completed on 2024-12-22 04:18:09
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * copyright : (C) 2005 University of British Columbia * 0006 * dscott@phas.ubc.ca * 0007 * * 0008 * This program is free software; you can redistribute it and/or modify * 0009 * it under the terms of the GNU General Public License as published by * 0010 * the Free Software Foundation; either version 2 of the License, or * 0011 * (at your option) any later version. * 0012 * * 0013 ***************************************************************************/ 0014 0015 0016 #include "cumulativesum.h" 0017 #include "objectstore.h" 0018 #include "ui_cumulativesumconfig.h" 0019 0020 static const QString& VECTOR_IN = "Vector In"; 0021 static const QString& SCALAR_IN = "Scale Scalar"; 0022 static const QString& VECTOR_OUT = "sum(Y)dX"; 0023 0024 class ConfigCumulativeSumPlugin : public Kst::DataObjectConfigWidget, public Ui_CumulativeSumConfig { 0025 public: 0026 ConfigCumulativeSumPlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_CumulativeSumConfig() { 0027 _store = 0; 0028 setupUi(this); 0029 } 0030 0031 ~ConfigCumulativeSumPlugin() {} 0032 0033 void setObjectStore(Kst::ObjectStore* store) { 0034 _store = store; 0035 _vector->setObjectStore(store); 0036 _scalarStep->setObjectStore(store); 0037 } 0038 0039 void setupSlots(QWidget* dialog) { 0040 if (dialog) { 0041 connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0042 connect(_scalarStep, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0043 } 0044 } 0045 0046 void setVectorX(Kst::VectorPtr vector) { 0047 setSelectedVector(vector); 0048 } 0049 0050 void setVectorY(Kst::VectorPtr vector) { 0051 setSelectedVector(vector); 0052 } 0053 0054 void setVectorsLocked(bool locked = true) { 0055 _vector->setEnabled(!locked); 0056 } 0057 0058 Kst::VectorPtr selectedVector() { return _vector->selectedVector(); }; 0059 void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); }; 0060 0061 Kst::ScalarPtr selectedScalar() { return _scalarStep->selectedScalar(); }; 0062 void setSelectedScalar(Kst::ScalarPtr scalar) { return _scalarStep->setSelectedScalar(scalar); }; 0063 0064 virtual void setupFromObject(Kst::Object* dataObject) { 0065 if (CumulativeSumSource* source = static_cast<CumulativeSumSource*>(dataObject)) { 0066 setSelectedVector(source->vector()); 0067 setSelectedScalar(source->scalarStep()); 0068 } 0069 } 0070 0071 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0072 Q_UNUSED(store); 0073 Q_UNUSED(attrs); 0074 0075 bool validTag = true; 0076 0077 // QStringRef av; 0078 // av = attrs.value("value"); 0079 // if (!av.isNull()) { 0080 // _configValue = QVariant(av.toString()).toBool(); 0081 // } 0082 0083 return validTag; 0084 } 0085 0086 public slots: 0087 virtual void save() { 0088 if (_cfg) { 0089 _cfg->beginGroup("Cumulative Sum DataObject Plugin"); 0090 _cfg->setValue("Input Vector", _vector->selectedVector()->Name()); 0091 _cfg->setValue("Input Scalar", _scalarStep->selectedScalar()->Name()); 0092 _cfg->endGroup(); 0093 } 0094 } 0095 0096 virtual void load() { 0097 if (_cfg && _store) { 0098 _cfg->beginGroup("Cumulative Sum DataObject Plugin"); 0099 QString vectorName = _cfg->value("Input Vector").toString(); 0100 Kst::Object* object = _store->retrieveObject(vectorName); 0101 Kst::Vector* vector = static_cast<Kst::Vector*>(object); 0102 if (vector) { 0103 setSelectedVector(vector); 0104 } 0105 QString scalarName = _cfg->value("Input Scalar").toString(); 0106 _scalarStep->setSelectedScalar(scalarName); 0107 0108 _cfg->endGroup(); 0109 } 0110 } 0111 0112 private: 0113 Kst::ObjectStore *_store; 0114 0115 }; 0116 0117 0118 CumulativeSumSource::CumulativeSumSource(Kst::ObjectStore *store) 0119 : Kst::BasicPlugin(store) { 0120 } 0121 0122 0123 CumulativeSumSource::~CumulativeSumSource() { 0124 } 0125 0126 0127 QString CumulativeSumSource::_automaticDescriptiveName() const { 0128 if (vector()) { 0129 return tr("%1 Integral").arg(vector()->descriptiveName()); 0130 } else { 0131 return tr("Integral"); 0132 } 0133 } 0134 0135 QString CumulativeSumSource::descriptionTip() const { 0136 QString tip; 0137 0138 tip = tr("Integral: %1\n dX: %2\n").arg(Name()).arg(scalarStep()->value()); 0139 0140 tip += tr("\nInput: %1").arg(vector()->descriptionTip()); 0141 return tip; 0142 } 0143 0144 0145 void CumulativeSumSource::change(Kst::DataObjectConfigWidget *configWidget) { 0146 if (ConfigCumulativeSumPlugin* config = static_cast<ConfigCumulativeSumPlugin*>(configWidget)) { 0147 setInputVector(VECTOR_IN, config->selectedVector()); 0148 setInputScalar(SCALAR_IN, config->selectedScalar()); 0149 } 0150 } 0151 0152 0153 void CumulativeSumSource::setupOutputs() { 0154 setOutputVector(VECTOR_OUT, ""); 0155 } 0156 0157 0158 bool CumulativeSumSource::algorithm() { 0159 Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN]; 0160 Kst::ScalarPtr inputScalar = _inputScalars[SCALAR_IN]; 0161 Kst::VectorPtr outputVector; 0162 // maintain kst file compatibility if the output vector name is changed. 0163 if (_outputVectors.contains(VECTOR_OUT)) { 0164 outputVector = _outputVectors[VECTOR_OUT]; 0165 } else { 0166 outputVector = _outputVectors.values().at(0); 0167 } 0168 0169 0170 /* Memory allocation */ 0171 outputVector->resize(inputVector->length(), true); 0172 0173 double const *v_in = inputVector->noNanValue(); 0174 double *v_out = outputVector->raw_V_ptr(); 0175 double s_in = inputScalar->value(); 0176 int len = inputVector->length(); 0177 0178 v_out[0] = v_in[0]; 0179 0180 for (int i = 1; i < len; i++) { 0181 v_out[i] = v_in[i]*s_in + v_out[i-1]; 0182 } 0183 0184 return true; 0185 } 0186 0187 0188 Kst::VectorPtr CumulativeSumSource::vector() const { 0189 return _inputVectors[VECTOR_IN]; 0190 } 0191 0192 0193 Kst::ScalarPtr CumulativeSumSource::scalarStep() const { 0194 return _inputScalars[SCALAR_IN]; 0195 } 0196 0197 0198 QStringList CumulativeSumSource::inputVectorList() const { 0199 return QStringList( VECTOR_IN ); 0200 } 0201 0202 0203 QStringList CumulativeSumSource::inputScalarList() const { 0204 return QStringList( SCALAR_IN ); 0205 } 0206 0207 0208 QStringList CumulativeSumSource::inputStringList() const { 0209 return QStringList( /*STRING_IN*/ ); 0210 } 0211 0212 0213 QStringList CumulativeSumSource::outputVectorList() const { 0214 return QStringList( VECTOR_OUT ); 0215 } 0216 0217 0218 QStringList CumulativeSumSource::outputScalarList() const { 0219 return QStringList( /*SCALAR_OUT*/ ); 0220 } 0221 0222 0223 QStringList CumulativeSumSource::outputStringList() const { 0224 return QStringList( /*STRING_OUT*/ ); 0225 } 0226 0227 0228 void CumulativeSumSource::saveProperties(QXmlStreamWriter &s) { 0229 Q_UNUSED(s); 0230 // s.writeAttribute("value", _configValue); 0231 } 0232 0233 0234 QString CumulativeSumPlugin::pluginName() const { return tr("Cumulative Sum"); } 0235 QString CumulativeSumPlugin::pluginDescription() const { return tr("Computes the cumulative sum (integral) of the input vector."); } 0236 0237 0238 Kst::DataObject *CumulativeSumPlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0239 0240 if (ConfigCumulativeSumPlugin* config = static_cast<ConfigCumulativeSumPlugin*>(configWidget)) { 0241 0242 CumulativeSumSource* object = store->createObject<CumulativeSumSource>(); 0243 0244 if (setupInputsOutputs) { 0245 object->setInputScalar(SCALAR_IN, config->selectedScalar()); 0246 object->setupOutputs(); 0247 object->setInputVector(VECTOR_IN, config->selectedVector()); 0248 } 0249 0250 object->setPluginName(pluginName()); 0251 0252 object->writeLock(); 0253 object->registerChange(); 0254 object->unlock(); 0255 0256 return object; 0257 } 0258 return 0; 0259 } 0260 0261 0262 Kst::DataObjectConfigWidget *CumulativeSumPlugin::configWidget(QSettings *settingsObject) const { 0263 ConfigCumulativeSumPlugin *widget = new ConfigCumulativeSumPlugin(settingsObject); 0264 return widget; 0265 } 0266 0267 #ifndef QT5 0268 Q_EXPORT_PLUGIN2(kstplugin_BinPlugin, CumulativeSumPlugin) 0269 #endif 0270 0271 // vim: ts=2 sw=2 et