File indexing completed on 2024-12-22 04:17:19
0001 /*************************************************************************** 0002 dataststring.cpp - a string from a data source 0003 ------------------- 0004 begin : September, 2008 0005 copyright : (C) 2008 by cbn 0006 email : netterfield@astro.utoronto.ca 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 ***************************************************************************/ 0017 #include "datastring.h" 0018 #include "stringscriptinterface.h" 0019 0020 #include <QDebug> 0021 #include <QTextDocument> 0022 #include <QXmlStreamWriter> 0023 0024 #include "debug.h" 0025 #include "objectstore.h" 0026 0027 namespace Kst { 0028 0029 const QString DataString::staticTypeString = "Data String"; 0030 const QString DataString::staticTypeTag = "datastring"; 0031 0032 /** Create a DataVector: raw data from a file */ 0033 DataString::DataString(ObjectStore *store) 0034 : String(store), DataPrimitive(this) { 0035 0036 setOrphan(true); 0037 } 0038 0039 0040 DataString::~DataString() { 0041 } 0042 0043 ScriptInterface* DataString::createScriptInterface() { 0044 return new StringDataSI(this); 0045 } 0046 0047 int DataString::fileLength() const { 0048 0049 if (dataSource()) { 0050 const DataInfo info = dataSource()->string().dataInfo(_field); 0051 0052 return info.frameCount; 0053 } 0054 0055 return 0; 0056 } 0057 0058 0059 QString DataString::_automaticDescriptiveName() const { 0060 QString name = _field; 0061 0062 // un-escape escaped special characters so they aren't escaped 2x. 0063 name.replace("\\_", "_").replace("\\^","^").replace("\\[", "[").replace("\\]", "]"); 0064 // now escape the special characters. 0065 name.replace('_', "\\_").replace('^', "\\^").replace('[', "\\[").replace(']', "\\]"); 0066 0067 return name; 0068 } 0069 0070 0071 const QString& DataString::typeString() const { 0072 return staticTypeString; 0073 } 0074 0075 0076 /** return true if it has a valid file and field, or false otherwise */ 0077 bool DataString::isValid() const { 0078 if (dataSource()) { 0079 dataSource()->readLock(); 0080 bool rc = dataSource()->string().isValid(_field); 0081 dataSource()->unlock(); 0082 return rc; 0083 } 0084 return false; 0085 } 0086 0087 0088 bool DataString::checkValidity(const DataSourcePtr& ds) const { 0089 if (ds) { 0090 ds->readLock(); 0091 bool rc = ds->string().isValid(_field); 0092 ds->unlock(); 0093 return rc; 0094 } 0095 return false; 0096 } 0097 0098 0099 void DataString::change(DataSourcePtr in_file, const QString &in_field, int in_frame) { 0100 Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED); 0101 0102 _field = in_field; 0103 _frame = in_frame; 0104 setDataSource(in_file); 0105 0106 registerChange(); 0107 } 0108 0109 void DataString::changeFile(DataSourcePtr in_file) { 0110 Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED); 0111 0112 if (!in_file) { 0113 Debug::self()->log(tr("Data file for string %1 was not opened.").arg(Name()), Debug::Warning); 0114 } 0115 setDataSource(in_file); 0116 0117 registerChange(); 0118 } 0119 0120 bool DataString::isStream() 0121 { 0122 return dataSource()->isStringStream(_field); 0123 } 0124 0125 0126 /** Save data string information */ 0127 void DataString::save(QXmlStreamWriter &s) { 0128 if (dataSource()) { 0129 s.writeStartElement("datastring"); 0130 saveFilename(s); 0131 s.writeAttribute("field", _field); 0132 0133 saveNameInfo(s, STRINGNUM); 0134 s.writeEndElement(); 0135 } 0136 } 0137 0138 0139 /** Update a data String */ 0140 void DataString::internalUpdate() { 0141 if (dataSource()) { 0142 int frame; 0143 const DataInfo info = dataSource()->string().dataInfo(_field); 0144 if ((_frame < 0) || (_frame >= info.frameCount)) { 0145 frame = info.frameCount-1; 0146 } else { 0147 frame = _frame; 0148 } 0149 0150 dataSource()->writeLock(); 0151 ReadInfo readInfo(&_value, frame); 0152 dataSource()->string().read(_field, readInfo); 0153 dataSource()->unlock(); 0154 } 0155 } 0156 0157 qint64 DataString::minInputSerial() const { 0158 if (dataSource()) { 0159 return (dataSource()->serial()); 0160 } 0161 return LLONG_MAX; 0162 } 0163 0164 qint64 DataString::maxInputSerialOfLastChange() const { 0165 if (dataSource()) { 0166 return (dataSource()->serialOfLastChange()); 0167 } 0168 return NoInputs; 0169 } 0170 0171 0172 0173 PrimitivePtr DataString::makeDuplicate() const { 0174 Q_ASSERT(store()); 0175 DataStringPtr string = store()->createObject<DataString>(); 0176 0177 string->writeLock(); 0178 string->change(dataSource(), _field, _frame); 0179 if (descriptiveNameIsManual()) { 0180 string->setDescriptiveName(descriptiveName()); 0181 } 0182 0183 string->registerChange(); 0184 string->unlock(); 0185 0186 return kst_cast<Primitive>(string); 0187 } 0188 0189 0190 QString DataString::descriptionTip() const { 0191 QString IDstring; 0192 0193 IDstring = tr( 0194 "Data String: %1 = %4\n" 0195 " %2\n" 0196 " Field: %3" 0197 ).arg(Name()).arg(dataSource()->fileName()).arg(_field).arg(value()); 0198 return IDstring; 0199 } 0200 0201 0202 QString DataString::propertyString() const { 0203 return tr("%1 of %2").arg(_field).arg(dataSource()->fileName()); 0204 } 0205 0206 void DataString::reload() { 0207 Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED); 0208 0209 if (dataSource()) { 0210 dataSource()->writeLock(); 0211 dataSource()->reset(); 0212 dataSource()->unlock(); 0213 reset(); 0214 registerChange(); 0215 } 0216 } 0217 0218 void DataString::reset() { 0219 ReadInfo readInfo(&_value, _frame); 0220 dataSource()->string().read(_field, readInfo); 0221 } 0222 0223 DataString::DataInfo::DataInfo() : 0224 frameCount(-1) 0225 { 0226 } 0227 0228 } 0229 // vim: ts=2 sw=2 et