Commit 37acdd63 authored by Jan Möbius's avatar Jan Möbius
Browse files

Merge branch 'simplify-loadsave' into 'master'

Simplify prop load/save; truncate file before writing.

See merge request !3
parents d96528c7 5ccaa333
...@@ -117,7 +117,7 @@ private: ...@@ -117,7 +117,7 @@ private:
virtual void saveProperty(); virtual void saveProperty();
virtual bool parseHeader(QString header, PropertyVisualizer*& propVis, unsigned int& n); 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. /// Disables picking.
void resetPicking(); void resetPicking();
......
...@@ -305,7 +305,7 @@ bool OMPropertyModel<MeshT>::parseHeader(QString header, PropertyVisualizer*& pr ...@@ -305,7 +305,7 @@ bool OMPropertyModel<MeshT>::parseHeader(QString header, PropertyVisualizer*& pr
template<typename MeshT> 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 #ifdef ENABLE_SKELETON_SUPPORT
if (propVis->getPropertyInfo().typeinfo() == proptype_SkinWeights) if (propVis->getPropertyInfo().typeinfo() == proptype_SkinWeights)
...@@ -314,7 +314,7 @@ void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream*& file_stream_, uns ...@@ -314,7 +314,7 @@ void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream*& file_stream_, uns
{ {
QString propertyText = ""; QString propertyText = "";
QString tmp; QString tmp;
while ((tmp = readLine(file_stream_)) != "") while ((tmp = file_stream.readLine()) != "")
propertyText = propertyText + tmp; propertyText = propertyText + tmp;
propVis->setPropertyFromText(i, propertyText); propVis->setPropertyFromText(i, propertyText);
} }
...@@ -322,7 +322,7 @@ void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream*& file_stream_, uns ...@@ -322,7 +322,7 @@ void OMPropertyModel<MeshT>::setPropertyFromFile(QTextStream*& file_stream_, uns
else else
#endif #endif
{ {
SingleObjectPropertyModel::setPropertyFromFile(file_stream_, n, propVis); SingleObjectPropertyModel::setPropertyFromFile(file_stream, n, propVis);
} }
} }
......
...@@ -255,47 +255,6 @@ QString SingleObjectPropertyModel::getDefaultSaveFilename(unsigned int propId) ...@@ -255,47 +255,6 @@ QString SingleObjectPropertyModel::getDefaultSaveFilename(unsigned int propId)
return name; 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) void SingleObjectPropertyModel::saveProperty(unsigned int propId)
{ {
PropertyVisualizer* propVis = propertyVisualizers[propId]; PropertyVisualizer* propVis = propertyVisualizers[propId];
...@@ -303,23 +262,21 @@ void SingleObjectPropertyModel::saveProperty(unsigned int propId) ...@@ -303,23 +262,21 @@ void SingleObjectPropertyModel::saveProperty(unsigned int propId)
QString filename = getSaveFilename(propId); QString filename = getSaveFilename(propId);
if (filename == "") return; if (filename == "") return;
QFile file_; QFile file(filename);
QTextStream* file_stream_ = 0; if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
std::cerr << "PropertyVis saveProperty(): cannot open file for writing" << std::endl;
openFile(filename, file_, file_stream_); return;
}
QTextStream file_stream(&file);
QString header = propVis->getHeader(); file_stream << propVis->getHeader() << '\n';
writeLine(header, file_stream_);
int n = propVis->getEntityCount(); int n = propVis->getEntityCount();
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
{ {
QString propertyText = propVis->getPropertyText(i); QString propertyText = propVis->getPropertyText(i);
writeLine(propertyText, file_stream_); file_stream << propertyText << '\n';
} }
closeFile(file_, file_stream_);
} }
void SingleObjectPropertyModel::loadProperty() void SingleObjectPropertyModel::loadProperty()
...@@ -328,34 +285,34 @@ void SingleObjectPropertyModel::loadProperty() ...@@ -328,34 +285,34 @@ void SingleObjectPropertyModel::loadProperty()
QString filename = getLoadFilename(); QString filename = getLoadFilename();
if (filename == "") return; if (filename == "") return;
QFile file_; QFile file(filename);
QTextStream* file_stream_ = 0; if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cerr << "PropertyVis loadProperty(): cannot open file for reading" << std::endl;
openFile(filename, file_, file_stream_); return;
}
QString header = readLine(file_stream_); QTextStream file_stream(&file);
QString header = file_stream.readLine();
PropertyVisualizer* propVis; PropertyVisualizer* propVis;
unsigned int n; unsigned int n;
if (parseHeader(header, propVis, n)) if (parseHeader(header, propVis, n))
{ {
setPropertyFromFile(file_stream_, n, propVis); setPropertyFromFile(file_stream, n, propVis);
} }
else else
{ {
emit log("Property could not be loaded."); 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) for (unsigned int i = 0; i < n; ++i)
{ {
QString propertyText = readLine(file_stream_); QString propertyText = file_stream.readLine();
propVis->setPropertyFromText(i, propertyText); propVis->setPropertyFromText(i, propertyText);
} }
} }
......
...@@ -163,19 +163,6 @@ protected: ...@@ -163,19 +163,6 @@ protected:
/// Returns the default file name /// Returns the default file name
virtual QString getDefaultSaveFilename(unsigned int propId); 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. /// Saves property.
void saveProperty(unsigned int propId); void saveProperty(unsigned int propId);
...@@ -183,7 +170,7 @@ protected: ...@@ -183,7 +170,7 @@ protected:
void loadProperty(); void loadProperty();
/// Sets the property values from a given file. /// 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. * @brief Parses the property file header.
......
Supports Markdown
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