File indexing completed on 2024-12-22 04:18:16
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 "sampleplugin.h" 0017 #include "objectstore.h" 0018 #include "ui_samplepluginconfig.h" 0019 0020 static const QString& VECTOR_IN = "Vector In"; 0021 static const QString& SCALAR_IN = "Scalar In"; 0022 static const QString& STRING_IN = "String In"; 0023 static const QString& VECTOR_OUT = "Vector Out"; 0024 static const QString& SCALAR_OUT = "Scalar Out"; 0025 static const QString& STRING_OUT = "String Out"; 0026 0027 /********************** 0028 ConfigWidgetSamplePlugin - This class defines the config widget that will be added to the 0029 BasicPluginDialog container for configuring the plugin. By default the BasicPluginDialog has 0030 no controls and thus all desired inputs/outputs/options should included in the .ui. 0031 0032 DataObjectConfigWidget is defined in dataobject.h 0033 ***********************/ 0034 class ConfigWidgetSamplePlugin : public Kst::DataObjectConfigWidget, public Ui_SamplePluginConfig { 0035 public: 0036 ConfigWidgetSamplePlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_SamplePluginConfig() { 0037 setupUi(this); 0038 } 0039 0040 ~ConfigWidgetSamplePlugin() {} 0041 0042 void setObjectStore(Kst::ObjectStore* store) { 0043 // _store must be set here. Any additional controls requiring the objectstore should also 0044 // have it set at this time. 0045 _store = store; 0046 _vector->setObjectStore(store); 0047 } 0048 0049 void setupSlots(QWidget* dialog) { 0050 if (dialog) { 0051 // In order to for Apply button logic to function. All controls change state must be linked to 0052 // the dialog's modified signal. 0053 connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0054 } 0055 } 0056 0057 // All information in the config widget must be publically accessible so that the plugin can pull 0058 // the required information and use it construct the object. 0059 Kst::VectorPtr selectedVector() { return _vector->selectedVector(); }; 0060 void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); }; 0061 0062 // This function is required and is used when editing of the object is triggered. It must be able 0063 // to configure the widget with the settings currently in use by the object. 0064 virtual void setupFromObject(Kst::Object* dataObject) { 0065 if (SamplePluginSource* source = static_cast<SamplePluginSource*>(dataObject)) { 0066 setSelectedVector(source->vector()); 0067 } 0068 } 0069 0070 // This function is used when loading a .kst file from disk. The plugins are expected to store any 0071 // required parameters as attributes of the plugin tag. The name and type are handled by BasicPluginFactory 0072 // as are all inputs and outputs. This should be used for any non-generic attributes associated with the plugin. 0073 // If false is returned, the creation of the object will not occur. 0074 // NOTE: Saving of the object is done in the objects saveProperties function. Values must match between them. 0075 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0076 Q_UNUSED(store); 0077 Q_UNUSED(attrs); 0078 0079 bool validTag = true; 0080 0081 // QStringRef av; 0082 // av = attrs.value("value"); 0083 // if (!av.isNull()) { 0084 // _configValue = QVariant(av.toString()).toBool(); 0085 // } 0086 0087 return validTag; 0088 } 0089 0090 public slots: 0091 // Dialog Defaults control - Save 0092 // Uses the provided QSettings Object to save the plugin default details as a new group. 0093 // Called every time a new dialog request is made. Does not affect editing. 0094 virtual void save() { 0095 if (_cfg) { 0096 _cfg->beginGroup("Sample DataObject Plugin"); 0097 _cfg->setValue("Input Vector", _vector->selectedVector()->Name()); 0098 _cfg->endGroup(); 0099 } 0100 } 0101 0102 // Dialog Defaults control - Load 0103 // Uses the provided QSettings Object to load the plugin default details. 0104 // Called every time a new dialog request is completed. Does not get called on edit. 0105 virtual void load() { 0106 if (_cfg && _store) { 0107 _cfg->beginGroup("Sample DataObject Plugin"); 0108 QString vectorName = _cfg->value("Input Vector").toString(); 0109 Kst::Object* object = _store->retrieveObject(vectorName); 0110 Kst::Vector* vector = static_cast<Kst::Vector*>(object); 0111 if (vector) { 0112 setSelectedVector(vector); 0113 } 0114 _cfg->endGroup(); 0115 } 0116 } 0117 0118 private: 0119 Kst::ObjectStore *_store; 0120 0121 }; 0122 0123 0124 /********************** 0125 SamplePluginSource - This class defines the main DataObject which derives from BasicPlugin (a DataObject). 0126 The key functions that this class must provide is the ability to create/modify the object, setup outputs, and 0127 be able to process the data (algorithm function). 0128 0129 ***********************/ 0130 SamplePluginSource::SamplePluginSource(Kst::ObjectStore *store) 0131 : Kst::BasicPlugin(store) { 0132 } 0133 0134 0135 SamplePluginSource::~SamplePluginSource() { 0136 } 0137 0138 0139 QString SamplePluginSource::_automaticDescriptiveName() const { 0140 return QString("Test Plugin Object"); 0141 } 0142 0143 0144 // Change is used to configure the DataObject and will be run immediately after initial creation as well as 0145 // when modifications have occurred. 0146 void SamplePluginSource::change(Kst::DataObjectConfigWidget *configWidget) { 0147 if (ConfigWidgetSamplePlugin* config = static_cast<ConfigWidgetSamplePlugin*>(configWidget)) { 0148 setInputVector(VECTOR_IN, config->selectedVector()); 0149 } 0150 } 0151 0152 0153 void SamplePluginSource::setupOutputs() { 0154 setOutputVector(VECTOR_OUT, ""); 0155 } 0156 0157 0158 // The algorithm function must handle all the data calculations for the plugin. This function is run during 0159 // each update of the input objects which includes after creation of the object. Only output objects that have 0160 // have setOutputVector/setOutputScalar/setOutputString should be updated as others will not appear outside of 0161 // the plugin. 0162 bool SamplePluginSource::algorithm() { 0163 Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN]; 0164 Kst::VectorPtr outputVector = _outputVectors[VECTOR_OUT]; 0165 0166 outputVector->resize(inputVector->length(), false); 0167 0168 // Sample only. Copy all values from input vector to output vector. 0169 for (int i = 0; i < inputVector->length(); i++) { 0170 outputVector->value()[i] = inputVector->value(i); 0171 } 0172 0173 // Return true to continue update. Returning false indicates that the update did not result in the output 0174 // objects changing and will not trigger updates of objects using them. 0175 return true; 0176 } 0177 0178 0179 Kst::VectorPtr SamplePluginSource::vector() const { 0180 return _inputVectors[VECTOR_IN]; 0181 } 0182 0183 0184 QStringList SamplePluginSource::inputVectorList() const { 0185 return QStringList( VECTOR_IN ); 0186 } 0187 0188 0189 QStringList SamplePluginSource::inputScalarList() const { 0190 return QStringList( /*SCALAR_IN*/ ); 0191 } 0192 0193 0194 QStringList SamplePluginSource::inputStringList() const { 0195 return QStringList( /*STRING_IN*/ ); 0196 } 0197 0198 0199 QStringList SamplePluginSource::outputVectorList() const { 0200 return QStringList( VECTOR_OUT ); 0201 } 0202 0203 0204 QStringList SamplePluginSource::outputScalarList() const { 0205 return QStringList( /*SCALAR_OUT*/ ); 0206 } 0207 0208 0209 QStringList SamplePluginSource::outputStringList() const { 0210 return QStringList( /*STRING_OUT*/ ); 0211 } 0212 0213 0214 // This function allows for support of saving / loading this object from a .kst file. The function should only 0215 // write attributes as any other advancement of the StreamWriter will result in a malformed Xml document. All 0216 // values required to recreate the should be saved here. All Input/Output Objects are saved by BasicPlugin and do 0217 // not need to be stored here. This should be used for any non-generic attributes associated with the plugin. 0218 // NOTE: Loading of the object is done in the configWidgets configurePropertiesFromXml function. Values must match between them. 0219 void SamplePluginSource::saveProperties(QXmlStreamWriter &s) { 0220 Q_UNUSED(s); 0221 // s.writeAttribute("value", _configValue); 0222 } 0223 0224 0225 // Name used to identify the plugin. Used when loading the plugin. 0226 QString SamplePlugin::pluginName() const { return "Sample DataObject Plugin"; } 0227 QString SamplePlugin::pluginDescription() const { return "Sample DataObject Plugin Description"; } 0228 0229 0230 /********************** 0231 SamplePlugin - This class defines the plugin interface to the DataObject defined by the plugin. 0232 The primary requirements of this class are to provide the necessary connections to create the object 0233 which includes providing access to the configuration widget. 0234 0235 ***********************/ 0236 0237 // The create function is responsible for creating the DataObject. It is provided the ObjectStore as well as a 0238 // fully configured configWidget to setup the object from. 0239 // setupInputOutputs is a flag to mark whether the object should automatically setup the inputs and outputs. New 0240 // creations will always configure them. When loading from a .kst file they will be done manually. 0241 Kst::DataObject *SamplePlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0242 0243 if (ConfigWidgetSamplePlugin* config = static_cast<ConfigWidgetSamplePlugin*>(configWidget)) { 0244 0245 // Create a generic object. 0246 SamplePluginSource* object = store->createObject<SamplePluginSource>(); 0247 0248 // Setup the object values based on the config widget. 0249 // object->setValue(config->value()); 0250 0251 // If the inputs/outputs should be configured, setup them up from the configWidget. 0252 if (setupInputsOutputs) { 0253 object->setInputVector(VECTOR_IN, config->selectedVector()); 0254 object->setupOutputs(); 0255 } 0256 0257 // Set the pluginName. 0258 object->setPluginName(pluginName()); 0259 0260 // Trigger an update of the object. This is required to trigger the algorithm call and creation of the output. 0261 // If inputs/outputs was not triggered, this will also be run a second time after all inputs/outputs have been 0262 // configured. 0263 object->writeLock(); 0264 object->registerChange(); 0265 object->unlock(); 0266 0267 return object; 0268 } 0269 return 0; 0270 } 0271 0272 0273 // Request for this plugins configuration widget. 0274 Kst::DataObjectConfigWidget *SamplePlugin::configWidget(QSettings *settingsObject) const { 0275 ConfigWidgetSamplePlugin *widget = new ConfigWidgetSamplePlugin(settingsObject); 0276 return widget; 0277 } 0278 0279 Q_EXPORT_PLUGIN2(kstplugin_sampleplugin, SamplePlugin) 0280 0281 // vim: ts=2 sw=2 et