File indexing completed on 2024-12-22 04:18:01
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 /* 'boxcar decimation filter': not very useful. */ 0016 0017 #include "bin.h" 0018 #include "objectstore.h" 0019 #include "ui_binconfig.h" 0020 0021 static const QString& VECTOR_IN = "Vector In"; 0022 static const QString& SCALAR_IN = "Scalar In"; 0023 static const QString& VECTOR_OUT = "Bins"; 0024 0025 class ConfigWidgetBinPlugin : public Kst::DataObjectConfigWidget, public Ui_BinConfig { 0026 public: 0027 ConfigWidgetBinPlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_BinConfig() { 0028 _store = 0; 0029 setupUi(this); 0030 } 0031 0032 ~ConfigWidgetBinPlugin() {} 0033 0034 void setObjectStore(Kst::ObjectStore* store) { 0035 _store = store; 0036 _vector->setObjectStore(store); 0037 _scalarBin->setObjectStore(store); 0038 _scalarBin->setDefaultValue(10); 0039 } 0040 0041 void setupSlots(QWidget* dialog) { 0042 if (dialog) { 0043 connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0044 connect(_scalarBin, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0045 } 0046 } 0047 0048 Kst::VectorPtr selectedVector() { return _vector->selectedVector(); }; 0049 void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); }; 0050 0051 Kst::ScalarPtr selectedScalar() { return _scalarBin->selectedScalar(); }; 0052 void setSelectedScalar(Kst::ScalarPtr scalar) { return _scalarBin->setSelectedScalar(scalar); }; 0053 0054 virtual void setupFromObject(Kst::Object* dataObject) { 0055 if (BinSource* source = static_cast<BinSource*>(dataObject)) { 0056 setSelectedVector(source->vector()); 0057 setSelectedScalar(source->binScalar()); 0058 } 0059 } 0060 0061 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0062 Q_UNUSED(store); 0063 Q_UNUSED(attrs); 0064 0065 bool validTag = true; 0066 0067 // QStringRef av; 0068 // av = attrs.value("value"); 0069 // if (!av.isNull()) { 0070 // _configValue = QVariant(av.toString()).toBool(); 0071 // } 0072 0073 return validTag; 0074 } 0075 0076 public slots: 0077 virtual void save() { 0078 if (_cfg) { 0079 _cfg->beginGroup("Bin DataObject Plugin"); 0080 _cfg->setValue("Input Vector", _vector->selectedVector()->Name()); 0081 _cfg->setValue("Input Bin Scalar", _scalarBin->selectedScalar()->Name()); 0082 _cfg->endGroup(); 0083 } 0084 } 0085 0086 virtual void load() { 0087 if (_cfg && _store) { 0088 _cfg->beginGroup("Bin DataObject Plugin"); 0089 QString vectorName = _cfg->value("Input Vector").toString(); 0090 Kst::Object* object = _store->retrieveObject(vectorName); 0091 Kst::Vector* vector = static_cast<Kst::Vector*>(object); 0092 if (vector) { 0093 setSelectedVector(vector); 0094 } 0095 QString scalarName = _cfg->value("Input Bin Scalar").toString(); 0096 object = _store->retrieveObject(scalarName); 0097 Kst::Scalar* binScalar = static_cast<Kst::Scalar*>(object); 0098 if (binScalar) { 0099 setSelectedScalar(binScalar); 0100 } 0101 _cfg->endGroup(); 0102 } 0103 } 0104 0105 private: 0106 Kst::ObjectStore *_store; 0107 0108 }; 0109 0110 0111 BinSource::BinSource(Kst::ObjectStore *store) 0112 : Kst::BasicPlugin(store) { 0113 } 0114 0115 0116 BinSource::~BinSource() { 0117 } 0118 0119 0120 QString BinSource::_automaticDescriptiveName() const { 0121 return tr("Bin Plugin Object"); 0122 } 0123 0124 0125 void BinSource::change(Kst::DataObjectConfigWidget *configWidget) { 0126 if (ConfigWidgetBinPlugin* config = static_cast<ConfigWidgetBinPlugin*>(configWidget)) { 0127 setInputVector(VECTOR_IN, config->selectedVector()); 0128 setInputScalar(SCALAR_IN, config->selectedScalar()); 0129 } 0130 } 0131 0132 0133 void BinSource::setupOutputs() { 0134 setOutputVector(VECTOR_OUT, ""); 0135 } 0136 0137 0138 bool BinSource::algorithm() { 0139 Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN]; 0140 Kst::ScalarPtr inputScalar = _inputScalars[SCALAR_IN]; 0141 Kst::VectorPtr outputVector = _outputVectors[VECTOR_OUT]; 0142 0143 //Make sure there is at least 1 element in the input vector 0144 if (inputVector->length() < 1) { 0145 _errorString = tr("Error: Input Vector invalid size"); 0146 return false; 0147 } 0148 //Make sure the bin size is at least 1 0149 if (inputScalar->value() < 1) { 0150 _errorString = tr("Error: No Bins"); 0151 return false; 0152 } 0153 0154 // allocate the lengths 0155 outputVector->resize(int(inputVector->length() / inputScalar->value()), false); 0156 0157 //now bin the data 0158 for (int i = 0; i < outputVector->length(); i++) 0159 { 0160 outputVector->raw_V_ptr()[i] = 0; 0161 //add up the elements for this bin 0162 for (int j = 0; j < inputScalar->value(); j++) 0163 { 0164 outputVector->raw_V_ptr()[i] += inputVector->raw_V_ptr()[int(i*inputScalar->value()+j)]; 0165 } 0166 //find the mean 0167 outputVector->raw_V_ptr()[i] /= inputScalar->value(); 0168 } 0169 return true; 0170 } 0171 0172 0173 Kst::VectorPtr BinSource::vector() const { 0174 return _inputVectors[VECTOR_IN]; 0175 } 0176 0177 0178 Kst::ScalarPtr BinSource::binScalar() const { 0179 return _inputScalars[SCALAR_IN]; 0180 } 0181 0182 0183 QStringList BinSource::inputVectorList() const { 0184 return QStringList( VECTOR_IN ); 0185 } 0186 0187 0188 QStringList BinSource::inputScalarList() const { 0189 return QStringList( SCALAR_IN ); 0190 } 0191 0192 0193 QStringList BinSource::inputStringList() const { 0194 return QStringList( /*STRING_IN*/ ); 0195 } 0196 0197 0198 QStringList BinSource::outputVectorList() const { 0199 return QStringList( VECTOR_OUT ); 0200 } 0201 0202 0203 QStringList BinSource::outputScalarList() const { 0204 return QStringList( /*SCALAR_OUT*/ ); 0205 } 0206 0207 0208 QStringList BinSource::outputStringList() const { 0209 return QStringList( /*STRING_OUT*/ ); 0210 } 0211 0212 0213 void BinSource::saveProperties(QXmlStreamWriter &s) { 0214 Q_UNUSED(s); 0215 // s.writeAttribute("value", _configValue); 0216 } 0217 0218 0219 QString BinPlugin::pluginName() const { return tr("Bin"); } 0220 QString BinPlugin::pluginDescription() const { return tr("Bins data into the given size bins. Each bin contains the mean of the elements belonging to the bin."); } 0221 0222 0223 Kst::DataObject *BinPlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0224 0225 if (ConfigWidgetBinPlugin* config = static_cast<ConfigWidgetBinPlugin*>(configWidget)) { 0226 0227 BinSource* object = store->createObject<BinSource>(); 0228 0229 if (setupInputsOutputs) { 0230 object->setInputScalar(SCALAR_IN, config->selectedScalar()); 0231 object->setupOutputs(); 0232 object->setInputVector(VECTOR_IN, config->selectedVector()); 0233 } 0234 0235 object->setPluginName(pluginName()); 0236 0237 object->writeLock(); 0238 object->registerChange(); 0239 object->unlock(); 0240 0241 return object; 0242 } 0243 return 0; 0244 } 0245 0246 0247 Kst::DataObjectConfigWidget *BinPlugin::configWidget(QSettings *settingsObject) const { 0248 ConfigWidgetBinPlugin *widget = new ConfigWidgetBinPlugin(settingsObject); 0249 return widget; 0250 } 0251 0252 #ifndef QT5 0253 Q_EXPORT_PLUGIN2(kstplugin_BinPlugin, BinPlugin) 0254 #endif 0255 0256 // vim: ts=2 sw=2 et