File indexing completed on 2024-12-22 04:17:51

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "basicpluginfactory.h"
0014 
0015 #include "debug.h"
0016 #include "basicplugin.h"
0017 #include "datacollection.h"
0018 #include "objectstore.h"
0019 
0020 namespace Kst {
0021 
0022 BasicPluginFactory::BasicPluginFactory()
0023 : ObjectFactory() {
0024   registerFactory(BasicPlugin::staticTypeTag, this);
0025 }
0026 
0027 
0028 BasicPluginFactory::~BasicPluginFactory() {
0029 }
0030 
0031 
0032 DataObjectPtr BasicPluginFactory::generateObject(ObjectStore *store, QXmlStreamReader& xml) {
0033   Q_ASSERT(store);
0034 
0035   DataObjectConfigWidget* configWidget;
0036   QString pluginName;
0037   QString descriptiveName;
0038   BasicPluginPtr dataObject;
0039   //bool validTag;
0040   while (!xml.atEnd()) {
0041     const QString n = xml.name().toString();
0042     if (xml.isStartElement()) {
0043       if (n == BasicPlugin::staticTypeTag) {
0044         QXmlStreamAttributes attrs = xml.attributes();
0045         pluginName = attrs.value("type").toString();
0046         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0047           descriptiveName = attrs.value("descriptiveName").toString();
0048         }
0049         Object::processShortNameIndexAttributes(attrs);
0050 
0051         configWidget = DataObject::pluginWidget(pluginName);
0052         if (configWidget) {
0053           if (!configWidget->configurePropertiesFromXml(store, attrs) ) {
0054             Debug::self()->log(QObject::tr("Error: unable to create data object from plugin"), Debug::Warning);
0055             return 0;
0056           } else {
0057             if (xml.isEndElement() && n == BasicPlugin::staticTypeTag) {
0058               break;
0059             }
0060           }
0061         } else {
0062           Debug::self()->log(QObject::tr("Error: unable to find plugin for data object"), Debug::Warning);
0063           return 0;
0064         }
0065 
0066         dataObject = kst_cast<BasicPlugin>(DataObject::createPlugin(pluginName, store, configWidget, false));
0067         QString expectedEnd;
0068         while (!(xml.isEndElement() && (xml.name().toString() == BasicPlugin::staticTypeTag))) {
0069           if (xml.isStartElement() && xml.name().toString() == "inputvector") {
0070             expectedEnd = xml.name().toString();
0071             attrs = xml.attributes();
0072             QString type = attrs.value("type").toString();
0073             QString tagName = attrs.value("tag").toString();
0074             VectorPtr vector = kst_cast<Vector>(store->retrieveObject(tagName));
0075             if (vector) {
0076               dataObject->setInputVector(type, vector);
0077             }
0078           } else if (xml.isStartElement() && xml.name().toString() == "inputscalar") {
0079             expectedEnd = xml.name().toString();
0080             attrs = xml.attributes();
0081             QString type = attrs.value("type").toString();
0082             QString tagName = attrs.value("tag").toString();
0083             ScalarPtr scalar = kst_cast<Scalar>(store->retrieveObject(tagName));
0084             if (scalar) {
0085               dataObject->setInputScalar(type, scalar);
0086             }
0087           } else if (xml.isStartElement() && xml.name().toString() == "inputstring") {
0088             expectedEnd = xml.name().toString();
0089             attrs = xml.attributes();
0090             QString type = attrs.value("type").toString();
0091             QString tagName = attrs.value("tag").toString();
0092             StringPtr string = kst_cast<String>(store->retrieveObject(tagName));
0093             if (string) {
0094               dataObject->setInputString(type, string);
0095             }
0096           } else if (xml.isStartElement() && xml.name().toString() == "outputvector") {
0097             expectedEnd = xml.name().toString();
0098             attrs = xml.attributes();
0099             QString type = attrs.value("type").toString();
0100             QString tagName = attrs.value("tag").toString();
0101             dataObject->setOutputVector(type, tagName);
0102           } else if (xml.isStartElement() && xml.name().toString() == "outputscalar") {
0103             expectedEnd = xml.name().toString();
0104             attrs = xml.attributes();
0105             QString type = attrs.value("type").toString();
0106             QString tagName = attrs.value("tag").toString();
0107             dataObject->setOutputScalar(type, tagName);
0108           } else if (xml.isStartElement() && xml.name().toString() == "outputstring") {
0109             expectedEnd = xml.name().toString();
0110             attrs = xml.attributes();
0111             QString type = attrs.value("type").toString();
0112             QString tagName = attrs.value("tag").toString();
0113             dataObject->setOutputString(type, tagName);
0114           } else if (xml.isEndElement()) {
0115             if (xml.name().toString() != expectedEnd) {
0116               //validTag = false;
0117               break;
0118             }
0119           }
0120           xml.readNext();
0121         }
0122       } else {
0123         return 0;
0124       }
0125     }
0126     if (xml.isEndElement()) {
0127       if (n == BasicPlugin::staticTypeTag) {
0128         break;
0129       } else {
0130         Debug::self()->log(QObject::tr("Error creating Plugin Object from Kst file."), Debug::Warning);
0131         return 0;
0132       }
0133     }
0134     xml.readNext();
0135   }
0136 
0137   if (xml.hasError()) {
0138     return 0;
0139   }
0140 
0141   dataObject->setDescriptiveName(descriptiveName);
0142 
0143   dataObject->writeLock();
0144   dataObject->registerChange();
0145   dataObject->unlock();
0146 
0147   return dataObject;
0148 }
0149 
0150 }
0151 
0152 // vim: ts=2 sw=2 et