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

0001 /***************************************************************************
0002                           datastprimitive.cpp  -  add datasource handling
0003                           capability to a primitive
0004                              -------------------
0005     begin                : October, 2009
0006     copyright            : (C) 2009 by cbn
0007     email                : netterfield@astro.utoronto.ca
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 #include "dataprimitive.h"
0019 
0020 #include "debug.h"
0021 
0022 #include "datasource.h"
0023 
0024 #include <QXmlStreamWriter>
0025 #include <QDir>
0026 
0027 
0028 namespace Kst {
0029 
0030 
0031 struct DataPrimitive::Private
0032 {
0033   Primitive* _primitive;
0034   DataSourcePtr _file;
0035 
0036   static void saveFilename(const QString& fn, QXmlStreamWriter& s);
0037 };
0038 
0039 
0040 void DataPrimitive::Private::saveFilename(const QString& fn, QXmlStreamWriter& s) {
0041   if (!fn.isEmpty()) {
0042     // QDir::current is set to *.kst's file path in mainwindow.cpp
0043     QDir current = QDir::current();
0044     QString relFn = current.relativeFilePath(fn);
0045     s.writeAttribute("file", DataSource::cleanPath(current.absoluteFilePath(fn)));
0046     if (QDir::isRelativePath(relFn)) { // is absolute if on a differnt disk/network 
0047         s.writeAttribute("fileRelative", relFn);
0048     }
0049   }
0050 }
0051 
0052 
0053 DataPrimitive::DataPrimitive(Primitive* primitive) : d(*new Private)
0054 {
0055   d._file = 0;
0056   d._primitive = primitive;
0057   _field.clear();
0058 }
0059 
0060 
0061 DataPrimitive::~DataPrimitive() {
0062   _field.clear();
0063   d._file = 0;
0064   d._primitive = 0;
0065   delete &d;
0066 }
0067 
0068 
0069 void DataPrimitive::changeFile(DataSourcePtr in_file) {
0070   Q_ASSERT(d._primitive->myLockStatus() == KstRWLock::WRITELOCKED);
0071 
0072   if (!in_file) {
0073     Debug::self()->log(Primitive::tr("Data file for vector %1 was not opened.").arg(d._primitive->Name()), Debug::Warning);
0074   }
0075   d._file = in_file;
0076   if (d._file) {
0077     d._file->writeLock();
0078   }
0079   d._primitive->reset();
0080   if (d._file) {
0081     d._file->unlock();
0082   }
0083   d._primitive->registerChange();
0084 }
0085 
0086 
0087 /** return the name of the file */
0088 QString DataPrimitive::filename() const {
0089   QString rc;
0090   if (d._file) {
0091     d._file->readLock();
0092     rc = d._file->fileName();
0093     d._file->unlock();
0094   }
0095   return rc;
0096 }
0097 
0098 
0099 void DataPrimitive::saveFilename(QXmlStreamWriter& s) {
0100   if (d._file) {
0101     d._file->readLock();
0102     DataPrimitive::Private::saveFilename(d._file->fileName(), s);
0103     d._file->unlock();
0104   }
0105 }
0106 
0107 
0108 void DataPrimitive::saveFilename(const QString& fileName, QXmlStreamWriter& s)
0109 {
0110     DataPrimitive::Private::saveFilename(fileName, s);
0111 }
0112 
0113 
0114 QString DataPrimitive::readFilename(const QXmlStreamAttributes& attrs)
0115 {
0116     // TODO discuss strategy: 
0117     //   - first try relative path
0118     //   - fall back to absolute path    
0119 
0120     // QDir::current is set to *.kst's file path in mainwindow.cpp
0121     QString name;
0122 
0123     QDir current = QDir::current();
0124 
0125     QString fnRel = attrs.value("fileRelative").toString();    
0126 
0127     if (!fnRel.isEmpty() && current.exists(fnRel)) {
0128         // internally only use absolute paths
0129         name = DataSource::cleanPath(current.absoluteFilePath(fnRel));
0130     } else {
0131 
0132       name = DataSource::cleanPath(attrs.value("file").toString());
0133     }
0134 
0135     return(name);
0136 
0137 }
0138 
0139 
0140 /** return the field */
0141 const QString& DataPrimitive::field() const {
0142   return _field;
0143 }
0144 
0145 DataSourcePtr DataPrimitive::dataSource() const {
0146   return d._file;
0147 }
0148 
0149 void DataPrimitive::setDataSource(const DataSourcePtr& file) {
0150   d._file = file;
0151 }
0152 
0153 }