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 "cumulativeaverage.h" 0017 #include "objectstore.h" 0018 #include "ui_cumulativeaverageconfig.h" 0019 0020 static const QString& VECTOR_IN = "Vector In"; 0021 static const QString& VECTOR_OUT = "Avg(Y)"; 0022 0023 class ConfigCumulativeAveragePlugin : public Kst::DataObjectConfigWidget, public Ui_CumulativeAverageConfig { 0024 public: 0025 ConfigCumulativeAveragePlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_CumulativeAverageConfig() { 0026 _store = 0; 0027 setupUi(this); 0028 } 0029 0030 ~ConfigCumulativeAveragePlugin() {} 0031 0032 void setObjectStore(Kst::ObjectStore* store) { 0033 _store = store; 0034 _vector->setObjectStore(store); 0035 } 0036 0037 void setupSlots(QWidget* dialog) { 0038 if (dialog) { 0039 connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0040 } 0041 } 0042 0043 void setVectorX(Kst::VectorPtr vector) { 0044 setSelectedVector(vector); 0045 } 0046 0047 void setVectorY(Kst::VectorPtr vector) { 0048 setSelectedVector(vector); 0049 } 0050 0051 void setVectorsLocked(bool locked = true) { 0052 _vector->setEnabled(!locked); 0053 } 0054 0055 Kst::VectorPtr selectedVector() { return _vector->selectedVector(); }; 0056 void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); }; 0057 0058 virtual void setupFromObject(Kst::Object* dataObject) { 0059 if (CumulativeAverageSource* source = static_cast<CumulativeAverageSource*>(dataObject)) { 0060 setSelectedVector(source->vector()); 0061 } 0062 } 0063 0064 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0065 Q_UNUSED(store); 0066 Q_UNUSED(attrs); 0067 0068 bool validTag = true; 0069 0070 // QStringRef av; 0071 // av = attrs.value("value"); 0072 // if (!av.isNull()) { 0073 // _configValue = QVariant(av.toString()).toBool(); 0074 // } 0075 0076 return validTag; 0077 } 0078 0079 public slots: 0080 virtual void save() { 0081 if (_cfg) { 0082 _cfg->beginGroup("Cumulative Average DataObject Plugin"); 0083 _cfg->setValue("Input Vector", _vector->selectedVector()->Name()); 0084 _cfg->endGroup(); 0085 } 0086 } 0087 0088 virtual void load() { 0089 if (_cfg && _store) { 0090 _cfg->beginGroup("Cumulative Average DataObject Plugin"); 0091 QString vectorName = _cfg->value("Input Vector").toString(); 0092 Kst::Object* object = _store->retrieveObject(vectorName); 0093 Kst::Vector* vector = static_cast<Kst::Vector*>(object); 0094 if (vector) { 0095 setSelectedVector(vector); 0096 } 0097 _cfg->endGroup(); 0098 } 0099 } 0100 0101 private: 0102 Kst::ObjectStore *_store; 0103 0104 }; 0105 0106 0107 CumulativeAverageSource::CumulativeAverageSource(Kst::ObjectStore *store) 0108 : Kst::BasicPlugin(store) { 0109 } 0110 0111 0112 CumulativeAverageSource::~CumulativeAverageSource() { 0113 } 0114 0115 0116 QString CumulativeAverageSource::_automaticDescriptiveName() const { 0117 if (vector()) { 0118 return tr("%1 Cumulative Average").arg(vector()->descriptiveName()); 0119 } else { 0120 return tr("Cumulative Average"); 0121 } 0122 } 0123 0124 QString CumulativeAverageSource::descriptionTip() const { 0125 QString tip; 0126 0127 tip = tr("Cumulative Average: %1\n").arg(Name()); 0128 0129 tip += tr("\nInput: %1").arg(vector()->descriptionTip()); 0130 return tip; 0131 } 0132 0133 0134 void CumulativeAverageSource::change(Kst::DataObjectConfigWidget *configWidget) { 0135 if (ConfigCumulativeAveragePlugin* config = static_cast<ConfigCumulativeAveragePlugin*>(configWidget)) { 0136 setInputVector(VECTOR_IN, config->selectedVector()); 0137 } 0138 } 0139 0140 0141 void CumulativeAverageSource::setupOutputs() { 0142 setOutputVector(VECTOR_OUT, ""); 0143 } 0144 0145 0146 // TODO 0147 bool CumulativeAverageSource::algorithm() { 0148 Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN]; 0149 Kst::VectorPtr outputVector; 0150 // maintain kst file compatibility if the output vector name is changed. 0151 if (_outputVectors.contains(VECTOR_OUT)) { 0152 outputVector = _outputVectors[VECTOR_OUT]; 0153 } else { 0154 outputVector = _outputVectors.values().at(0); 0155 } 0156 0157 0158 /* Memory allocation */ 0159 outputVector->resize(inputVector->length(), true); 0160 0161 double const *v_in = inputVector->noNanValue(); 0162 double *v_out = outputVector->raw_V_ptr(); 0163 v_out[0] = v_in[0]; // i = 1 0164 0165 for (int i = 1; i < inputVector->length(); ++i) { 0166 v_out[i] = (v_in[i] + (i * v_out[i-1])) / (i+1); 0167 } 0168 0169 return true; 0170 } 0171 0172 0173 Kst::VectorPtr CumulativeAverageSource::vector() const { 0174 return _inputVectors[VECTOR_IN]; 0175 } 0176 0177 0178 QStringList CumulativeAverageSource::inputVectorList() const { 0179 return QStringList( VECTOR_IN ); 0180 } 0181 0182 0183 QStringList CumulativeAverageSource::inputScalarList() const { 0184 return QStringList( /*SCALAR_IN*/ ); 0185 } 0186 0187 0188 QStringList CumulativeAverageSource::inputStringList() const { 0189 return QStringList( /*STRING_IN*/ ); 0190 } 0191 0192 0193 QStringList CumulativeAverageSource::outputVectorList() const { 0194 return QStringList( VECTOR_OUT ); 0195 } 0196 0197 0198 QStringList CumulativeAverageSource::outputScalarList() const { 0199 return QStringList( /*SCALAR_OUT*/ ); 0200 } 0201 0202 0203 QStringList CumulativeAverageSource::outputStringList() const { 0204 return QStringList( /*STRING_OUT*/ ); 0205 } 0206 0207 0208 void CumulativeAverageSource::saveProperties(QXmlStreamWriter &s) { 0209 Q_UNUSED(s); 0210 // s.writeAttribute("value", _configValue); 0211 } 0212 0213 0214 QString CumulativeAveragePlugin::pluginName() const { return tr("Cumulative Average"); } 0215 QString CumulativeAveragePlugin::pluginDescription() const { return tr("Computes the cumulative average of the input vector."); } 0216 0217 0218 Kst::DataObject *CumulativeAveragePlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0219 0220 if (ConfigCumulativeAveragePlugin* config = static_cast<ConfigCumulativeAveragePlugin*>(configWidget)) { 0221 0222 CumulativeAverageSource* object = store->createObject<CumulativeAverageSource>(); 0223 0224 if (setupInputsOutputs) { 0225 object->setupOutputs(); 0226 object->setInputVector(VECTOR_IN, config->selectedVector()); 0227 } 0228 0229 object->setPluginName(pluginName()); 0230 0231 object->writeLock(); 0232 object->registerChange(); 0233 object->unlock(); 0234 0235 return object; 0236 } 0237 return 0; 0238 } 0239 0240 0241 Kst::DataObjectConfigWidget *CumulativeAveragePlugin::configWidget(QSettings *settingsObject) const { 0242 ConfigCumulativeAveragePlugin *widget = new ConfigCumulativeAveragePlugin(settingsObject); 0243 return widget; 0244 } 0245 0246 #ifndef QT5 0247 Q_EXPORT_PLUGIN2(kstplugin_BinPlugin, CumulativeAveragePlugin) 0248 #endif 0249 0250 // vim: ts=2 sw=2 et