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