1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/TInspector_Preferences.hxx>
18 #include <Standard_WarningsDisable.hxx>
20 #include <QDomDocument>
21 #include <QDomElement>
24 #include <QTextStream>
25 #include <Standard_WarningsRestore.hxx>
27 // =======================================================================
28 // function : GetPreferences
30 // =======================================================================
31 void TInspector_Preferences::GetPreferences (const TCollection_AsciiString& thePluginName,
32 TInspectorAPI_PreferencesDataMap& theItem)
34 if (!myIsLoadedPreferences)
37 myIsLoadedPreferences = Standard_True;
39 myLoadedPreferences.Find (thePluginName, theItem);
42 // =======================================================================
43 // function : StorePreferences
45 // =======================================================================
46 void TInspector_Preferences::StorePreferences()
48 if (myLoadedPreferences.IsEmpty())
51 QString aFileName = QString (GetDirectory().ToCString()) + QDir::separator() + PreferencesFileName();
52 QFile aFile(aFileName);
53 if (!aFile.open (QFile::WriteOnly))
56 QDomDocument aDomDocument (documentKey());
57 QDomComment aComment = aDomDocument.createComment("\nThis file is automatically created by TInspector application.\nChanges made in this file can be lost.\n");
58 aDomDocument.appendChild (aComment);
59 QDomElement aRootElement = aDomDocument.createElement (documentKey());
60 aDomDocument.appendChild (aRootElement);
62 for (NCollection_DataMap<TCollection_AsciiString, TInspectorAPI_PreferencesDataMap>::Iterator aPrefsIt (myLoadedPreferences);
63 aPrefsIt.More(); aPrefsIt.Next())
65 QDomElement aPluginElement = aDomDocument.createElement (pluginKey());
66 aPluginElement.setAttribute (nameKey(), aPrefsIt.Key().ToCString());
67 aRootElement.appendChild (aPluginElement);
69 const TInspectorAPI_PreferencesDataMap& aPluginMap = aPrefsIt.Value();
70 for (TInspectorAPI_IteratorOfPreferencesDataMap aPluginPrefsIt (aPluginMap); aPluginPrefsIt.More(); aPluginPrefsIt.Next())
72 QDomElement aParameterElement = aDomDocument.createElement (parameterKey());
73 aParameterElement.setAttribute (nameKey(), aPluginPrefsIt.Key().ToCString());
74 aParameterElement.setAttribute (valueKey(), aPluginPrefsIt.Value().ToCString());
75 aPluginElement.appendChild (aParameterElement);
79 QTextStream aTextStream (&aFile);
80 QStringList aDocumentStr = aDomDocument.toString().split ("\n");
81 for (QStringList::ConstIterator aContentIt = aDocumentStr.begin(); aContentIt != aDocumentStr.end(); ++aContentIt)
82 aTextStream << *aContentIt << endl;
86 // =======================================================================
87 // function : RemovePreferences
89 // =======================================================================
90 void TInspector_Preferences::RemovePreferences()
92 QString aFileName = QString (GetDirectory().ToCString()) + QDir::separator() + PreferencesFileName();
93 QDir aDir (GetDirectory().ToCString());
94 if (aDir.exists (aFileName))
95 aDir.remove (aFileName);
99 // =======================================================================
100 // function : loadPreferences
102 // =======================================================================
103 void TInspector_Preferences::loadPreferences()
105 QString aFileName = QString (GetDirectory().ToCString()) + QDir::separator() + PreferencesFileName();
106 QFile aFile (aFileName);
107 if (!aFile.open (QFile::ReadOnly))
110 QDomDocument aDomDocument;
111 bool aResult = aDomDocument.setContent (&aFile);
116 QDomElement aRootElement = aDomDocument.documentElement();
117 if (aRootElement.isNull() || aRootElement.tagName() != documentKey())
120 QDomNode aPluginNode = aRootElement.firstChild();
121 while (!aPluginNode.isNull())
123 if (aPluginNode.isElement())
125 QDomElement aPluginElement = aPluginNode.toElement();
126 if (aPluginElement.tagName() == pluginKey() && aPluginElement.hasAttribute (nameKey()))
128 TInspectorAPI_PreferencesDataMap anItem;
129 readPluginItem (aPluginElement, anItem);
130 myLoadedPreferences.Bind (aPluginElement.attribute (nameKey()).toStdString().c_str(), anItem);
133 aPluginNode = aPluginNode.nextSibling();
137 // =======================================================================
138 // function : readPluginItem
140 // =======================================================================
141 void TInspector_Preferences::readPluginItem (const QDomElement thePluginElement, TInspectorAPI_PreferencesDataMap& theItem)
143 QDomNode aParameterNode = thePluginElement.firstChild();
144 while (!aParameterNode.isNull())
146 if (aParameterNode.isElement())
148 QDomElement aParameterElement = aParameterNode.toElement();
149 if (aParameterElement.tagName() == parameterKey() &&
150 aParameterElement.hasAttribute (nameKey()) && aParameterElement.hasAttribute (valueKey()))
152 theItem.Bind (aParameterElement.attribute (nameKey()).toStdString().c_str(),
153 aParameterElement.attribute (valueKey()).toStdString().c_str());
156 aParameterNode = aParameterNode.nextSibling();