File indexing completed on 2024-12-22 04:17:58
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 "imagefactory.h" 0014 0015 #include "debug.h" 0016 #include "image.h" 0017 #include "datacollection.h" 0018 #include "objectstore.h" 0019 0020 namespace Kst { 0021 0022 ImageFactory::ImageFactory() 0023 : RelationFactory() { 0024 registerFactory(Image::staticTypeTag, this); 0025 } 0026 0027 0028 ImageFactory::~ImageFactory() { 0029 } 0030 0031 0032 RelationPtr ImageFactory::generateRelation(ObjectStore *store, QXmlStreamReader& xml) { 0033 Q_ASSERT(store); 0034 0035 double lowerThreshold=0, upperThreshold=1; 0036 int numberOfContourLines=4, contourWeight=0; 0037 QString matrixName, paletteName, legend, contourColor, descriptiveName; 0038 bool hasColorMap=true, hasContourMap=false, autoThreshold=true; 0039 0040 while (!xml.atEnd()) { 0041 const QString n = xml.name().toString(); 0042 if (xml.isStartElement()) { 0043 if (n == Image::staticTypeTag) { 0044 QXmlStreamAttributes attrs = xml.attributes(); 0045 0046 matrixName = attrs.value("matrix").toString(); 0047 legend = attrs.value("legend").toString(); 0048 0049 paletteName = attrs.value("palettename").toString(); 0050 0051 hasColorMap = attrs.value("hascolormap").toString() == "true" ? true : false; 0052 lowerThreshold = attrs.value("lowerthreshold").toString().toDouble(); 0053 upperThreshold = attrs.value("upperthreshold").toString().toDouble(); 0054 0055 hasContourMap = attrs.value("hascontourmap").toString() == "true" ? true : false; 0056 numberOfContourLines = attrs.value("numcontourlines").toString().toInt(); 0057 contourWeight = attrs.value("contourweight").toString().toInt(); 0058 contourColor = attrs.value("contourcolor").toString(); 0059 0060 autoThreshold = attrs.value("autothreshold").toString() == "true" ? true : false; 0061 if (attrs.value("descriptiveNameIsManual").toString() == "true") { 0062 descriptiveName = attrs.value("descriptiveName").toString(); 0063 } 0064 Object::processShortNameIndexAttributes(attrs); 0065 0066 } else { 0067 return 0; 0068 } 0069 } else if (xml.isEndElement()) { 0070 if (n == Image::staticTypeTag) { 0071 break; 0072 } else { 0073 Debug::self()->log(QObject::tr("Error creating Image from Kst file."), Debug::Warning); 0074 return 0; 0075 } 0076 } 0077 xml.readNext(); 0078 } 0079 0080 if (xml.hasError()) { 0081 return 0; 0082 } 0083 0084 MatrixPtr matrix = 0; 0085 if (store && !matrixName.isEmpty()) { 0086 matrix = kst_cast<Matrix>(store->retrieveObject(matrixName)); 0087 } 0088 0089 if (!matrix) { 0090 Debug::self()->log(QObject::tr("Error creating Image from Kst file. Could not find matrix."), Debug::Warning); 0091 return 0; 0092 } 0093 0094 ImagePtr image = store->createObject<Image>(); 0095 0096 if (hasColorMap && hasContourMap) { 0097 image->changeToColorAndContour(matrix, 0098 lowerThreshold, 0099 upperThreshold, 0100 autoThreshold, 0101 paletteName, 0102 numberOfContourLines, 0103 QColor(contourColor), 0104 contourWeight); 0105 } else if (hasContourMap) { 0106 image->changeToContourOnly(matrix, 0107 numberOfContourLines, 0108 QColor(contourColor), 0109 contourWeight); 0110 } else { 0111 image->changeToColorOnly(matrix, 0112 lowerThreshold, 0113 upperThreshold, 0114 autoThreshold, 0115 paletteName); 0116 } 0117 0118 image->setDescriptiveName(descriptiveName); 0119 0120 image->writeLock(); 0121 image->registerChange(); 0122 image->unlock(); 0123 0124 return image; 0125 } 0126 0127 } 0128 0129 // vim: ts=2 sw=2 et