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

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 "stringfactory.h"
0014 
0015 #include "debug.h"
0016 #include "string_kst.h"
0017 #include "datastring.h"
0018 #include "objectstore.h"
0019 #include "datasourcepluginmanager.h"
0020 
0021 
0022 namespace Kst {
0023 
0024 StringFactory::StringFactory()
0025 : PrimitiveFactory() {
0026   registerFactory(String::staticTypeTag, this);
0027 }
0028 
0029 
0030 StringFactory::~StringFactory() {
0031 }
0032 
0033 
0034 PrimitivePtr StringFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0035   QByteArray data;
0036 
0037   Q_ASSERT(store);
0038 
0039   bool orphan = true;
0040   bool editable = true;
0041   QString value, descriptiveName;
0042 
0043   while (!xml.atEnd()) {
0044       const QString n = xml.name().toString();
0045     if (xml.isStartElement()) {
0046       if (n == "string") {
0047         QXmlStreamAttributes attrs = xml.attributes();
0048         value = attrs.value("value").toString();
0049         orphan = attrs.value("orphan").toString() == "true" ? true : false;
0050         editable = attrs.value("editable").toString() == "true" ? true : false;
0051         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0052           descriptiveName = attrs.value("descriptiveName").toString();
0053         }
0054         Object::processShortNameIndexAttributes(attrs);
0055       } else {
0056         return 0;
0057       }
0058     } else if (xml.isEndElement()) {
0059       if (n == "string") {
0060         break;
0061       } else {
0062         Debug::self()->log(QObject::tr("Error creating string from Kst file."), Debug::Warning);
0063         return 0;
0064       }
0065     }
0066     xml.readNext();
0067   }
0068 
0069   if (xml.hasError()) {
0070     return 0;
0071   }
0072 
0073   StringPtr string = store->createObject<String>();
0074   string->setValue(value);
0075   string->setOrphan(orphan);
0076   string->setEditable(editable);
0077   string->setDescriptiveName(descriptiveName);
0078 
0079   string->registerChange();
0080 
0081   return string;
0082 }
0083 
0084 DataStringFactory::DataStringFactory()
0085 : PrimitiveFactory() {
0086   registerFactory(DataString::staticTypeTag, this);
0087 }
0088 
0089 
0090 DataStringFactory::~DataStringFactory() {
0091 }
0092 
0093 
0094 PrimitivePtr DataStringFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0095   QString descriptiveName;
0096   Q_ASSERT(store);
0097 
0098   QString file, field;
0099   int frame=0;
0100 
0101   while (!xml.atEnd()) {
0102     const QString n = xml.name().toString();
0103     if (xml.isStartElement()) {
0104       if (n == DataString::staticTypeTag) {
0105         QXmlStreamAttributes attrs = xml.attributes();
0106 
0107         file = DataPrimitive::readFilename(attrs);
0108         field = attrs.value("field").toString();
0109         frame = attrs.value("frame").toString().toInt();
0110 
0111         if (!store->override.fileName.isEmpty()) {
0112           file = store->override.fileName;
0113         }
0114 
0115         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0116           descriptiveName = attrs.value("descriptiveName").toString();
0117         }
0118         Object::processShortNameIndexAttributes(attrs);
0119       } else {
0120         return 0;
0121       }
0122     } else if (xml.isEndElement()) {
0123       if (n == DataString::staticTypeTag) {
0124         break;
0125       } else {
0126         Debug::self()->log(QObject::tr("Error creating DataString from Kst file."), Debug::Warning);
0127         return 0;
0128       }
0129     }
0130     xml.readNext();
0131   }
0132 
0133   if (xml.hasError()) {
0134     return 0;
0135   }
0136 
0137   DataSourcePtr dataSource = DataSourcePluginManager::findOrLoadSource(store, file);
0138 
0139   if (!dataSource) {
0140     return 0; //Couldn't find a suitable datasource
0141   }
0142 
0143   DataStringPtr dataString = store->createObject<DataString>();
0144 
0145   dataString->writeLock();
0146   dataString->change(dataSource, field, frame);
0147 
0148   dataString->setDescriptiveName(descriptiveName);
0149   dataString->registerChange();
0150   dataString->unlock();
0151 
0152 
0153   return dataString;
0154 }
0155 
0156 
0157 }
0158 
0159 // vim: ts=2 sw=2 et