Commit 8c30c68e authored by Martin Heistermann's avatar Martin Heistermann

Simplify prop load/save; truncate file before writing.

parent 7a5f003d
......@@ -117,7 +117,7 @@ private:
virtual void saveProperty();
virtual bool parseHeader(QString header, PropertyVisualizer*& propVis, unsigned int& n);
virtual void setPropertyFromFile(QTextStream*& file_stream_, unsigned int n, PropertyVisualizer *propVis);
virtual void setPropertyFromFile(QTextStream* file_stream_, unsigned int n, PropertyVisualizer *propVis);
/// Disables picking.
void resetPicking();
......
......@@ -305,7 +305,7 @@ bool OMPropertyModel<MeshT>::parseHeader(QString header, PropertyVisualizer*& pr
template<typename MeshT>
void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream*& file_stream_, unsigned int n, PropertyVisualizer* propVis)
void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream* file_stream_, unsigned int n, PropertyVisualizer* propVis)
{
#ifdef ENABLE_SKELETON_SUPPORT
if (propVis->getPropertyInfo().typeinfo() == proptype_SkinWeights)
......@@ -314,7 +314,7 @@ void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream*& file_stream_, uns
{
QString propertyText = "";
QString tmp;
while ((tmp = readLine(file_stream_)) != "")
while ((tmp = file_stream_->readLine()) != "")
propertyText = propertyText + tmp;
propVis->setPropertyFromText(i, propertyText);
}
......
......@@ -255,47 +255,6 @@ QString SingleObjectPropertyModel::getDefaultSaveFilename(unsigned int propId)
return name;
}
void SingleObjectPropertyModel::openFile(QString _filename, QFile& file_, QTextStream*& file_stream_)
{
closeFile(file_, file_stream_);
file_.setFileName(_filename);
if(file_.open(QIODevice::ReadWrite | QIODevice::Text))
file_stream_ = new QTextStream(&file_);
}
void SingleObjectPropertyModel::closeFile(QFile& file_, QTextStream*& file_stream_)
{
if( file_stream_)
{
delete file_stream_;
file_stream_ = 0;
}
if(file_.exists())
{
file_.close();
}
}
QString SingleObjectPropertyModel::readLine(QTextStream* file_stream_)
{
if(file_stream_)
{
QString s;
s = file_stream_->readLine();
return s;
}
else return QString("");
}
void SingleObjectPropertyModel::writeLine(QString _s, QTextStream*& file_stream_)
{
if(file_stream_)
{
(*file_stream_) << _s << '\n';
}
else std::cerr << "Warning: filestream not available...\n";
}
void SingleObjectPropertyModel::saveProperty(unsigned int propId)
{
PropertyVisualizer* propVis = propertyVisualizers[propId];
......@@ -303,23 +262,21 @@ void SingleObjectPropertyModel::saveProperty(unsigned int propId)
QString filename = getSaveFilename(propId);
if (filename == "") return;
QFile file_;
QTextStream* file_stream_ = 0;
openFile(filename, file_, file_stream_);
QFile file_(filename);
if (!file_.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
std::cerr << "PropertyVis saveProperty(): cannot open file for writing" << std::endl;
return;
}
QTextStream file_stream_(&file_);
QString header = propVis->getHeader();
writeLine(header, file_stream_);
file_stream_ << propVis->getHeader() << '\n';
int n = propVis->getEntityCount();
for (int i = 0; i < n; ++i)
{
QString propertyText = propVis->getPropertyText(i);
writeLine(propertyText, file_stream_);
file_stream_ << propertyText << '\n';
}
closeFile(file_, file_stream_);
}
void SingleObjectPropertyModel::loadProperty()
......@@ -328,34 +285,34 @@ void SingleObjectPropertyModel::loadProperty()
QString filename = getLoadFilename();
if (filename == "") return;
QFile file_;
QTextStream* file_stream_ = 0;
openFile(filename, file_, file_stream_);
QFile file_(filename);
if (!file_.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cerr << "PropertyVis loadProperty(): cannot open file for reading" << std::endl;
return;
}
QString header = readLine(file_stream_);
QTextStream file_stream_(&file_);
QString header = file_stream_.readLine();
PropertyVisualizer* propVis;
unsigned int n;
if (parseHeader(header, propVis, n))
{
setPropertyFromFile(file_stream_, n, propVis);
setPropertyFromFile(&file_stream_, n, propVis);
}
else
{
emit log("Property could not be loaded.");
}
closeFile(file_, file_stream_);
}
void SingleObjectPropertyModel::setPropertyFromFile(QTextStream*& file_stream_, unsigned int n, PropertyVisualizer* propVis)
void SingleObjectPropertyModel::setPropertyFromFile(QTextStream* file_stream_, unsigned int n, PropertyVisualizer* propVis)
{
for (unsigned int i = 0; i < n; ++i)
{
QString propertyText = readLine(file_stream_);
QString propertyText = file_stream_->readLine();
propVis->setPropertyFromText(i, propertyText);
}
}
......
......@@ -163,19 +163,6 @@ protected:
/// Returns the default file name
virtual QString getDefaultSaveFilename(unsigned int propId);
/// Opens a file.
void openFile(QString _filename, QFile &file_, QTextStream *&file_stream_);
/// Closes a file.
void closeFile(QFile& file_, QTextStream*& file_stream_);
/// Read line from a file.
QString readLine(QTextStream *file_stream_);
/// Writes a line to a file.
void writeLine(QString _s, QTextStream *&file_stream_);
/// Saves property.
void saveProperty(unsigned int propId);
......@@ -183,7 +170,7 @@ protected:
void loadProperty();
/// Sets the property values from a given file.
virtual void setPropertyFromFile(QTextStream*& file_stream_, unsigned int n, PropertyVisualizer *propVis);
virtual void setPropertyFromFile(QTextStream *file_stream_, unsigned int n, PropertyVisualizer *propVis);
/**
* @brief Parses the property file header.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment