File indexing completed on 2024-12-22 04:17:52
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 "curvefactory.h" 0014 0015 #include "debug.h" 0016 #include "curve.h" 0017 #include "datacollection.h" 0018 #include "objectstore.h" 0019 0020 namespace Kst { 0021 0022 CurveFactory::CurveFactory() 0023 : RelationFactory() { 0024 registerFactory(Curve::staticTypeTag, this); 0025 } 0026 0027 0028 CurveFactory::~CurveFactory() { 0029 } 0030 0031 0032 RelationPtr CurveFactory::generateRelation(ObjectStore *store, QXmlStreamReader& xml) { 0033 0034 Q_ASSERT(store); 0035 0036 int lineStyle=0, lineWidth=0, pointType=0, pointDensity=0, pointSize = 0, headType=0; 0037 QString xVectorName, yVectorName, legend, errorXVectorName, errorYVectorName, errorXMinusVectorName; 0038 QString errorYMinusVectorName; 0039 QString colorName; 0040 QString headColorName; 0041 QString barFillColorName; 0042 QString descriptiveName; 0043 int alpha = 255; 0044 int barFillAlpha = 255; 0045 int headAlpha = 255; 0046 QString alphaStr; 0047 0048 bool hasLines=true, hasPoints=false, hasBars=false, ignoreAutoScale=false, hasHead=false; 0049 0050 while (!xml.atEnd()) { 0051 const QString n = xml.name().toString(); 0052 if (xml.isStartElement()) { 0053 if (n == Curve::staticTypeTag) { 0054 QXmlStreamAttributes attrs = xml.attributes(); 0055 xVectorName = attrs.value("xvector").toString(); 0056 yVectorName = attrs.value("yvector").toString(); 0057 legend = attrs.value("legend").toString(); 0058 colorName = attrs.value("color").toString(); 0059 headColorName = attrs.value("headcolor").toString(); 0060 barFillColorName = attrs.value("barfillcolor").toString(); 0061 0062 alphaStr = attrs.value("alpha").toString(); 0063 if (!alphaStr.isEmpty()) { 0064 alpha = alphaStr.toInt(); 0065 } 0066 alphaStr = attrs.value("headalpha").toString(); 0067 if (!alphaStr.isEmpty()) { 0068 headAlpha = alphaStr.toInt(); 0069 } 0070 alphaStr = attrs.value("barfillalpha").toString(); 0071 if (!alphaStr.isEmpty()) { 0072 barFillAlpha = alphaStr.toInt(); 0073 } 0074 0075 0076 errorXVectorName = attrs.value("errorxvector").toString(); 0077 errorYVectorName = attrs.value("erroryvector").toString(); 0078 errorXMinusVectorName = attrs.value("errorxminusvector").toString(); 0079 errorYMinusVectorName = attrs.value("erroryminusvector").toString(); 0080 0081 hasLines = attrs.value("haslines").toString() == "true" ? true : false; 0082 lineWidth = attrs.value("linewidth").toString().toInt(); 0083 lineStyle = attrs.value("linestyle").toString().toInt(); 0084 0085 hasPoints = attrs.value("haspoints").toString() == "true" ? true : false; 0086 pointType = attrs.value("pointtype").toString().toInt(); 0087 pointDensity = attrs.value("pointdensity").toString().toInt(); 0088 pointSize = attrs.value("pointsize").toString().toInt(); 0089 if (pointSize < 1) { 0090 pointSize = CURVE_DEFAULT_POINT_SIZE; // FIXME: default if we were reading a pre kst2.0.7 kst file 0091 } 0092 0093 hasHead = attrs.value("hashead").toString() == "true" ? true : false; 0094 headType = attrs.value("headtype").toString().toInt(); 0095 0096 hasBars = attrs.value("hasbars").toString() == "true" ? true : false; 0097 0098 ignoreAutoScale = attrs.value("ignoreautoscale").toString() == "true" ? true : false; 0099 0100 if (attrs.value("descriptiveNameIsManual").toString() == "true") { 0101 descriptiveName = attrs.value("descriptiveName").toString(); 0102 } 0103 Object::processShortNameIndexAttributes(attrs); 0104 0105 } else { 0106 return 0; 0107 } 0108 } else if (xml.isEndElement()) { 0109 if (n == Curve::staticTypeTag) { 0110 break; 0111 } else { 0112 Debug::self()->log(QObject::tr("Error creating Curve from Kst file."), Debug::Warning); 0113 return 0; 0114 } 0115 } 0116 xml.readNext(); 0117 } 0118 0119 if (xml.hasError()) { 0120 return 0; 0121 } 0122 0123 VectorPtr xVector = 0; 0124 if (store && !xVectorName.isEmpty()) { 0125 xVector = kst_cast<Vector>(store->retrieveObject(xVectorName)); 0126 } 0127 0128 if (!xVector) { 0129 Debug::self()->log(QObject::tr("Error creating Curve from Kst file. Could not find xVector."), Debug::Warning); 0130 return 0; 0131 } 0132 0133 VectorPtr yVector = 0; 0134 if (store && !yVectorName.isEmpty()) { 0135 yVector = kst_cast<Vector>(store->retrieveObject(yVectorName)); 0136 } 0137 0138 if (!yVector) { 0139 Debug::self()->log(QObject::tr("Error creating Curve from Kst file. Could not find yVector."), Debug::Warning); 0140 return 0; 0141 } 0142 0143 VectorPtr errorXVector = 0; 0144 if (store && !errorXVectorName.isEmpty()) { 0145 errorXVector = kst_cast<Vector>(store->retrieveObject(errorXVectorName)); 0146 } 0147 0148 VectorPtr errorYVector = 0; 0149 if (store && !errorYVectorName.isEmpty()) { 0150 errorYVector = kst_cast<Vector>(store->retrieveObject(errorYVectorName)); 0151 } 0152 0153 VectorPtr errorXMinusVector = 0; 0154 if (store && !errorXMinusVectorName.isEmpty()) { 0155 errorXMinusVector = kst_cast<Vector>(store->retrieveObject(errorXMinusVectorName)); 0156 } 0157 0158 VectorPtr errorYMinusVector = 0; 0159 if (store && !errorYMinusVectorName.isEmpty()) { 0160 errorYMinusVector = kst_cast<Vector>(store->retrieveObject(errorYMinusVectorName)); 0161 } 0162 0163 CurvePtr curve = store->createObject<Curve>(); 0164 0165 curve->setXVector(xVector); 0166 curve->setYVector(yVector); 0167 curve->setXError(errorXVector); 0168 curve->setYError(errorYVector); 0169 curve->setXMinusError(errorXMinusVector); 0170 curve->setYMinusError(errorYMinusVector); 0171 0172 QColor color(colorName); 0173 color.setAlpha(alpha); 0174 curve->setColor(color); 0175 0176 QColor headColor(headColorName); 0177 headColor.setAlpha(headAlpha); 0178 curve->setHeadColor(QColor(headColor)); 0179 0180 if (barFillColorName.isEmpty()) { 0181 curve->setBarFillColor(curve->color()); 0182 } else { 0183 QColor barFillColor(barFillColorName); 0184 barFillColor.setAlpha(barFillAlpha); 0185 curve->setBarFillColor(barFillColor); 0186 } 0187 0188 curve->setHasPoints(hasPoints); 0189 curve->setHasLines(hasLines); 0190 curve->setHasBars(hasBars); 0191 curve->setHasHead(hasHead); 0192 curve->setLineWidth(lineWidth); 0193 curve->setLineStyle(lineStyle); 0194 curve->setPointType(pointType); 0195 curve->setPointSize(pointSize); 0196 curve->setHeadType(headType); 0197 curve->setPointDensity(pointDensity); 0198 curve->setIgnoreAutoScale(ignoreAutoScale); 0199 0200 curve->setDescriptiveName(descriptiveName); 0201 0202 curve->writeLock(); 0203 curve->registerChange(); 0204 curve->unlock(); 0205 0206 return curve; 0207 } 0208 0209 } 0210 0211 // vim: ts=2 sw=2 et