diff options
| author | Anton Novoselov <[email protected]> | 2017-08-01 12:53:38 +0300 |
|---|---|---|
| committer | Anton Novoselov <[email protected]> | 2017-08-01 12:53:38 +0300 |
| commit | 236f03c0b9a4982328ed1201978f7f69d192d9b2 (patch) | |
| tree | e486f2fa39dba203563895541e92c60ed3e25759 /tools/ArtistTools/source/CoreLib/Utils | |
| parent | Added screens to welcome page (diff) | |
| download | blast-236f03c0b9a4982328ed1201978f7f69d192d9b2.tar.xz blast-236f03c0b9a4982328ed1201978f7f69d192d9b2.zip | |
Blast 1.1 release (windows / linux)
see docs/release_notes.txt for details
Diffstat (limited to 'tools/ArtistTools/source/CoreLib/Utils')
| -rw-r--r-- | tools/ArtistTools/source/CoreLib/Utils/XMLHelper.cpp | 121 | ||||
| -rw-r--r-- | tools/ArtistTools/source/CoreLib/Utils/XMLHelper.h | 39 |
2 files changed, 160 insertions, 0 deletions
diff --git a/tools/ArtistTools/source/CoreLib/Utils/XMLHelper.cpp b/tools/ArtistTools/source/CoreLib/Utils/XMLHelper.cpp new file mode 100644 index 0000000..c48b0dc --- /dev/null +++ b/tools/ArtistTools/source/CoreLib/Utils/XMLHelper.cpp @@ -0,0 +1,121 @@ +#include "XMLHelper.h" +#include <QtXml\QtXml> +#include "ViewerOutput.h" + +static int gcounter = 0; +static char gbuf[256]; + +XMLFile::XMLFile(const QString& rootName) + : _rootName(rootName) +{ +} + +void XMLFile::load(const QString& filePath) +{ + QFile file(filePath); + + if (!file.open(QIODevice::ReadOnly)) + { + return; + } + + QDomDocument xmlDoc; + if (!xmlDoc.setContent(&file)) + { + file.close(); + return; + } + file.close(); + + if (xmlDoc.isNull() || xmlDoc.documentElement().tagName() != _rootName) + { + sprintf(gbuf, "The file you selected is empty or not a speficied file: %s.", filePath.toStdString().c_str()); + viewer_msg(gbuf); + return; + } + + loadItems(xmlDoc); + + //QDomNodeList elms = xmlDoc.documentElement().elementsByTagName(QObject::tr("StressSolverPreset")); + //for (int i = 0; i < elms.count(); ++i) + //{ + // StressSolverUserPreset preset(""); + // _userPresets.push_back(preset); + // _loadStressSolverPreset(elms.at(i).toElement(), _userPresets[i]); + //} +} + +void XMLFile::save(const QString& filePath) +{ + std::string rr = filePath.toStdString(); + rr; + + QFileInfo fileInfo(filePath); + QDir fileDir = fileInfo.absoluteDir(); + + QString tt = fileDir.absolutePath(); + std::string hh = tt.toStdString(); + hh; + + if (!fileDir.exists()) + { + if (!fileDir.mkdir(fileDir.absolutePath())) + { + sprintf(gbuf, "Failed to crreate the folder: %s.", filePath.toStdString().c_str()); + viewer_msg(gbuf); + return; + } + } + QFile file(filePath); + if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) + { + return; + } + QTextStream out(&file); + + QDomDocument xmlDoc; + QDomElement rootElm = xmlDoc.createElement(_rootName); + xmlDoc.appendChild(rootElm); + + saveItems(xmlDoc); + + // 4 is count of indent + xmlDoc.save(out, 4); +} + +SingleItemKindFile::SingleItemKindFile(const QString& rootName, const QString& itemName) + : XMLFile(rootName) + , _itemName(itemName) +{ +} + +void SingleItemKindFile::loadItems(QDomDocument xmlDoc) +{ + QDomNodeList elms = xmlDoc.documentElement().elementsByTagName(_itemName); + for (int i = 0; i < elms.count(); ++i) + { + QDomElement elm = elms.at(i).toElement(); + _items.push_back(elm.attribute(QObject::tr("Value"))); + } +} + +void SingleItemKindFile::saveItems(QDomDocument xmlDoc) +{ + for (int i = 0; i < _items.count(); ++i) + { + QDomElement elm = xmlDoc.createElement(_itemName); + elm.setAttribute(QObject::tr("Value"), _items.at(i)); + xmlDoc.documentElement().appendChild(elm); + } +} + +bool SingleItemKindFile::isItemExist(const QString& item) +{ + for (int i = 0; i < _items.count(); ++i) + { + if (_items.at(i) == item) + return true; + } + + return false; +} diff --git a/tools/ArtistTools/source/CoreLib/Utils/XMLHelper.h b/tools/ArtistTools/source/CoreLib/Utils/XMLHelper.h new file mode 100644 index 0000000..ca43033 --- /dev/null +++ b/tools/ArtistTools/source/CoreLib/Utils/XMLHelper.h @@ -0,0 +1,39 @@ +#pragma once +#include <QtCore\QString> +#include <QtCore\QList> +#include "corelib_global.h" + +class QDomDocument; + +class CORELIB_EXPORT XMLFile +{ +public: + XMLFile(const QString& rootName); + + void load(const QString& filePath); + void save(const QString& filePath); + +protected: + virtual void loadItems(QDomDocument xmlDoc) = 0; + virtual void saveItems(QDomDocument xmlDoc) = 0; +protected: + QString _rootName; +}; + +class CORELIB_EXPORT SingleItemKindFile : public XMLFile +{ +public: + SingleItemKindFile(const QString& rootName, const QString& itemName); + + virtual void loadItems(QDomDocument xmlDoc); + virtual void saveItems(QDomDocument xmlDoc); + + void addItemBack(const QString& item) { _items.push_back(item); } + void addItemFront(const QString& item) { _items.push_back(item); } + QList<QString>& getItems() { return _items; } + bool isItemExist(const QString& item); + +private: + QString _itemName; + QList<QString> _items; +}; |