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