File indexing completed on 2024-12-22 04:18:10

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 "filterunwind.h"
0017 #include "objectstore.h"
0018 #include "ui_filterunwindconfig.h"
0019 
0020 static const QString& VECTOR_IN = "Y Vector";
0021 static const QString& SCALAR_MAXIMUM_IN = "Maximum Scalar";
0022 static const QString& SCALAR_MINIMUM_IN = "Minimum Scalar";
0023 static const QString& SCALAR_STEP_IN = "Step Scalar";
0024 static const QString& VECTOR_OUT = "Y";
0025 
0026 class ConfigWidgetFilterUnwindPlugin : public Kst::DataObjectConfigWidget, public Ui_FilterUnwindConfig {
0027   public:
0028     ConfigWidgetFilterUnwindPlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_FilterUnwindConfig() {
0029       _store = 0;
0030       setupUi(this);
0031     }
0032 
0033     ~ConfigWidgetFilterUnwindPlugin() {}
0034 
0035     void setObjectStore(Kst::ObjectStore* store) { 
0036       _store = store; 
0037       _vector->setObjectStore(store);
0038       _scalarMinumum->setObjectStore(store);
0039       _scalarMaximum->setObjectStore(store);
0040       _scalarStep->setObjectStore(store);
0041       _scalarMinumum->setDefaultValue(0.0);
0042       _scalarMaximum->setDefaultValue(360.0);
0043       _scalarStep->setDefaultValue(50.0);
0044     }
0045 
0046     void setupSlots(QWidget* dialog) {
0047       if (dialog) {
0048         connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified()));
0049         connect(_scalarMinumum, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified()));
0050         connect(_scalarMaximum, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified()));
0051         connect(_scalarStep, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified()));
0052       }
0053     }
0054 
0055     void setVectorX(Kst::VectorPtr vector) {
0056       setSelectedVector(vector);
0057     }
0058 
0059     void setVectorY(Kst::VectorPtr vector) {
0060       setSelectedVector(vector);
0061     }
0062 
0063     void setVectorsLocked(bool locked = true) {
0064       _vector->setEnabled(!locked);
0065     }
0066 
0067     Kst::VectorPtr selectedVector() { return _vector->selectedVector(); };
0068     void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); };
0069 
0070     Kst::ScalarPtr selectedMinimumScalar() { return _scalarMinumum->selectedScalar(); };
0071     void setSelectedMinimumScalar(Kst::ScalarPtr scalar) { return _scalarMinumum->setSelectedScalar(scalar); };
0072 
0073     Kst::ScalarPtr selectedMaximumScalar() { return _scalarMaximum->selectedScalar(); };
0074     void setSelectedMaximumScalar(Kst::ScalarPtr scalar) { return _scalarMaximum->setSelectedScalar(scalar); };
0075 
0076     Kst::ScalarPtr selectedStepScalar() { return _scalarStep->selectedScalar(); };
0077     void setSelectedStepScalar(Kst::ScalarPtr scalar) { return _scalarStep->setSelectedScalar(scalar); };
0078 
0079     virtual void setupFromObject(Kst::Object* dataObject) {
0080       if (FilterUnwindSource* source = static_cast<FilterUnwindSource*>(dataObject)) {
0081         setSelectedVector(source->vector());
0082         setSelectedMaximumScalar(source->maximumScalar());
0083         setSelectedMinimumScalar(source->minimumScalar());
0084         setSelectedStepScalar(source->stepScalar());
0085       }
0086     }
0087 
0088     virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) {
0089       Q_UNUSED(store);
0090       Q_UNUSED(attrs);
0091 
0092       bool validTag = true;
0093 
0094 //       QStringRef av;
0095 //       av = attrs.value("value");
0096 //       if (!av.isNull()) {
0097 //         _configValue = QVariant(av.toString()).toBool();
0098 //       }
0099 
0100       return validTag;
0101     }
0102 
0103   public slots:
0104     virtual void save() {
0105       if (_cfg) {
0106         _cfg->beginGroup("Filter Unwind Plugin");
0107         _cfg->setValue("Input Vector", _vector->selectedVector()->Name());
0108         _cfg->setValue("Minimum Scalar", _scalarMinumum->selectedScalar()->Name());
0109         _cfg->setValue("Maximum Scalar", _scalarMaximum->selectedScalar()->Name());
0110         _cfg->setValue("Step Scalar", _scalarStep->selectedScalar()->Name());
0111         _cfg->endGroup();
0112       }
0113     }
0114 
0115     virtual void load() {
0116       if (_cfg && _store) {
0117         _cfg->beginGroup("Filter Unwind Plugin");
0118         QString vectorName = _cfg->value("Input Vector").toString();
0119         Kst::Object* object = _store->retrieveObject(vectorName);
0120         Kst::Vector* vector = static_cast<Kst::Vector*>(object);
0121         if (vector) {
0122           setSelectedVector(vector);
0123         }
0124         QString scalarName = _cfg->value("Maximum Scalar").toString();
0125         _scalarMaximum->setSelectedScalar(scalarName);
0126 
0127         scalarName = _cfg->value("Minimum Scalar").toString();
0128         _scalarMinumum->setSelectedScalar(scalarName);
0129 
0130         scalarName = _cfg->value("Step Scalar").toString();
0131         _scalarStep->setSelectedScalar(scalarName);
0132 
0133         _cfg->endGroup();
0134       }
0135     }
0136 
0137   private:
0138     Kst::ObjectStore *_store;
0139 
0140 };
0141 
0142 
0143 FilterUnwindSource::FilterUnwindSource(Kst::ObjectStore *store)
0144 : Kst::BasicPlugin(store) {
0145 }
0146 
0147 
0148 FilterUnwindSource::~FilterUnwindSource() {
0149 }
0150 
0151 
0152 QString FilterUnwindSource::_automaticDescriptiveName() const {
0153   if (vector()) {
0154     return tr("%1 Unwind").arg(vector()->descriptiveName());
0155   } else {
0156     return tr("Unwind");
0157   }
0158 }
0159 
0160 
0161 QString FilterUnwindSource::descriptionTip() const {
0162   QString tip;
0163 
0164   tip = tr("Unwind Filter: %1\n  %2 - %3\n  Step: %4 %", "%2 - %3 specifies a numerical range").arg(Name()).arg(minimumScalar()->value()).arg(maximumScalar()->value()).arg(stepScalar()->value());
0165 
0166   tip += tr("\nInput: %1").arg(vector()->descriptionTip());
0167   return tip;
0168 }
0169 
0170 void FilterUnwindSource::change(Kst::DataObjectConfigWidget *configWidget) {
0171   if (ConfigWidgetFilterUnwindPlugin* config = static_cast<ConfigWidgetFilterUnwindPlugin*>(configWidget)) {
0172     setInputVector(VECTOR_IN, config->selectedVector());
0173     setInputScalar(SCALAR_MAXIMUM_IN, config->selectedMaximumScalar());
0174     setInputScalar(SCALAR_MINIMUM_IN, config->selectedMinimumScalar());
0175     setInputScalar(SCALAR_STEP_IN, config->selectedStepScalar());
0176   }
0177 }
0178 
0179 
0180 void FilterUnwindSource::setupOutputs() {
0181   setOutputVector(VECTOR_OUT, "");
0182 }
0183 
0184 
0185 bool FilterUnwindSource::algorithm() {
0186   Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN];
0187   Kst::ScalarPtr minimumScalar = _inputScalars[SCALAR_MINIMUM_IN];
0188   Kst::ScalarPtr maximumScalar = _inputScalars[SCALAR_MAXIMUM_IN];
0189   Kst::ScalarPtr stepScalar = _inputScalars[SCALAR_STEP_IN];
0190   Kst::VectorPtr outputVector;
0191   // maintain kst file compatibility if the output vector name is changed.
0192   if (_outputVectors.contains(VECTOR_OUT)) {
0193     outputVector = _outputVectors[VECTOR_OUT];
0194   } else {
0195     outputVector = _outputVectors.values().at(0);
0196   }
0197 
0198   int N = inputVector->length();
0199   double max = maximumScalar->value();
0200   double min = minimumScalar->value();
0201   double step = stepScalar->value();
0202   double last_x_in;
0203   double range;
0204   double x;
0205   double wind = 0;
0206   int i;
0207 
0208   if (max<min) {
0209     qSwap(max, min);
0210   }
0211 
0212   range = max - min;
0213   if ( (N < 1) || (max==min) || (step <=0) || (step>=100) ) {
0214     return false;
0215   }
0216 
0217   outputVector->resize(N, false);
0218   step *= (max-min)/100.0;
0219 
0220 
0221   last_x_in = inputVector->noNanValue(0);
0222   outputVector->raw_V_ptr()[0] = last_x_in;
0223 
0224   for (i=1; i<N; ++i) {
0225     x = inputVector->noNanValue(i);
0226     if ((x>max) || (x<min)) { // invalid/spike... ignore.
0227       x = last_x_in;
0228     }
0229     if (x-last_x_in > step) {
0230       wind -= range;
0231     } else if (last_x_in - x > step) {
0232       wind += range;
0233     }
0234     outputVector->raw_V_ptr()[i] = x + wind;
0235     last_x_in = x;
0236   }
0237 
0238   Kst::LabelInfo label_info = inputVector->labelInfo();
0239   label_info.name = tr("Unwind %1").arg(label_info.name);
0240   outputVector->setLabelInfo(label_info);
0241 
0242   return true;
0243 }
0244 
0245 
0246 Kst::VectorPtr FilterUnwindSource::vector() const {
0247   return _inputVectors[VECTOR_IN];
0248 }
0249 
0250 
0251 Kst::ScalarPtr FilterUnwindSource::maximumScalar() const {
0252   return _inputScalars[SCALAR_MAXIMUM_IN];
0253 }
0254 
0255 
0256 Kst::ScalarPtr FilterUnwindSource::minimumScalar() const {
0257   return _inputScalars[SCALAR_MINIMUM_IN];
0258 }
0259 
0260 
0261 Kst::ScalarPtr FilterUnwindSource::stepScalar() const {
0262   return _inputScalars[SCALAR_STEP_IN];
0263 }
0264 
0265 
0266 QStringList FilterUnwindSource::inputVectorList() const {
0267   return QStringList( VECTOR_IN );
0268 }
0269 
0270 
0271 QStringList FilterUnwindSource::inputScalarList() const {
0272   QStringList inputScalars( SCALAR_MAXIMUM_IN );
0273   inputScalars += SCALAR_MINIMUM_IN;
0274   inputScalars += SCALAR_STEP_IN;
0275   return inputScalars;
0276 }
0277 
0278 
0279 QStringList FilterUnwindSource::inputStringList() const {
0280   return QStringList( /*STRING_IN*/ );
0281 }
0282 
0283 
0284 QStringList FilterUnwindSource::outputVectorList() const {
0285   return QStringList( VECTOR_OUT );
0286 }
0287 
0288 
0289 QStringList FilterUnwindSource::outputScalarList() const {
0290   return QStringList( /*SCALAR_OUT*/ );
0291 }
0292 
0293 
0294 QStringList FilterUnwindSource::outputStringList() const {
0295   return QStringList( /*STRING_OUT*/ );
0296 }
0297 
0298 
0299 void FilterUnwindSource::saveProperties(QXmlStreamWriter &s) {
0300   Q_UNUSED(s);
0301 //   s.writeAttribute("value", _configValue);
0302 }
0303 
0304 
0305 // Name used to identify the plugin.  Used when loading the plugin.
0306 QString FilterUnwindPlugin::pluginName() const { return tr("Unwind Filter"); }
0307 QString FilterUnwindPlugin::pluginDescription() const { return tr("Unwinds data that have wrapped."); }
0308 
0309 
0310 Kst::DataObject *FilterUnwindPlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const {
0311 
0312   if (ConfigWidgetFilterUnwindPlugin* config = static_cast<ConfigWidgetFilterUnwindPlugin*>(configWidget)) {
0313 
0314     FilterUnwindSource* object = store->createObject<FilterUnwindSource>();
0315 
0316     if (setupInputsOutputs) {
0317       object->setInputScalar(SCALAR_MAXIMUM_IN, config->selectedMaximumScalar());
0318       object->setInputScalar(SCALAR_MINIMUM_IN, config->selectedMinimumScalar());
0319       object->setInputScalar(SCALAR_STEP_IN, config->selectedStepScalar());
0320       object->setupOutputs();
0321       object->setInputVector(VECTOR_IN, config->selectedVector());
0322     }
0323 
0324     object->setPluginName(pluginName());
0325 
0326     object->writeLock();
0327     object->registerChange();
0328     object->unlock();
0329 
0330     return object;
0331   }
0332   return 0;
0333 }
0334 
0335 
0336 Kst::DataObjectConfigWidget *FilterUnwindPlugin::configWidget(QSettings *settingsObject) const {
0337   ConfigWidgetFilterUnwindPlugin *widget = new ConfigWidgetFilterUnwindPlugin(settingsObject);
0338   return widget;
0339 }
0340 
0341 #ifndef QT5
0342 Q_EXPORT_PLUGIN2(kstplugin_FilterUnwindPlugin, FilterUnwindPlugin)
0343 #endif
0344 
0345 // vim: ts=2 sw=2 et