Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPreferenceManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 /*
17  *
18  */
19 
20 #if TY_USE_IHM
21 
22  #include <qwidget.h>
23  #include <qrect.h>
24  #include <qstring.h>
25  #include <qfile.h>
26 
29 
30  #include "TYPreferenceManager.h"
31  #include "OLocalizator.h"
32 
33 OPreferenceManager* TYPreferenceManager::_prefMngr = nullptr;
34 QString* TYPreferenceManager::_fileName = nullptr;
35 
36 OPreferenceManager& TYPreferenceManager::getInstance()
37 {
38  if (_prefMngr == nullptr)
39  {
40  _prefMngr = new OPreferenceManager("Tympan");
41  }
42  return *_prefMngr;
43 }
44 
45 void TYPreferenceManager::completePreferences()
46 {
47  QString DefaultSettingsFilePath = OLocalizator::getResourcePath() + "DefaultSettings.xml";
48 
49  _prefMngr->completePreferences(DefaultSettingsFilePath);
50 }
51 
52 QString& TYPreferenceManager::getFileName()
53 {
54  if (_fileName == nullptr)
55  {
56  _fileName = new QString("");
57  }
58  return *_fileName;
59 }
60 
61 bool TYPreferenceManager::init(const QString& regularSettingsFileName, bool& isCopiedFromDefaultSettings,
62  const std::unique_ptr<QFile> settingsFile)
63 {
64  bool ret = true;
65  QString effectiveFileName;
66  // If we haven't found a settings file or it does not exist
67  if (settingsFile == nullptr || !settingsFile->exists())
68  {
69  // Copy the default settings in the regular setting file
70  QString DefaultSettingsFilePath = OLocalizator::getResourcePath() + "DefaultSettings.xml";
71  ret = copyDefaultSettings(DefaultSettingsFilePath, regularSettingsFileName);
72  isCopiedFromDefaultSettings = true;
73  effectiveFileName = regularSettingsFileName;
74  }
75  else
76  // Else the settings file name is the file name of the file found
77  {
78  effectiveFileName = settingsFile->fileName();
79  }
80  setFileName(effectiveFileName);
81  return ret;
82 }
83 
84 void TYPreferenceManager::reset()
85 {
86  getInstance().reset();
87 }
88 
89 void TYPreferenceManager::setFileName(const QString& fileName)
90 {
91  getFileName() = fileName;
92 }
93 
94 bool TYPreferenceManager::read()
95 {
96  if (QFile::exists(getFileName()))
97  {
98  return getInstance().readXML(getFileName());
99  }
100  else
101  {
102  return false;
103  }
104 }
105 
106 bool TYPreferenceManager::write()
107 {
108  return getInstance().writeXML(getFileName());
109 }
110 
111 bool TYPreferenceManager::exists(const QString& pref)
112 {
113  return getInstance().exists(pref);
114 }
115 
116 void TYPreferenceManager::saveGeometryToPreferences(const QString& pref, const QWidget* pWidget)
117 {
118  Q_ASSERT(pWidget);
119 
120  getInstance().setBool(pref + "Maximized", pWidget->isMaximized());
121 
122  QPoint pos = pWidget->pos();
123  QSize size = pWidget->size();
124 
125  if (pos.x() < 0)
126  {
127  pos.setX(0);
128  }
129  if (pos.y() < 0)
130  {
131  pos.setY(0);
132  }
133  if (size.width() <= 0)
134  {
135  size.setWidth(100);
136  }
137  if (size.height() <= 0)
138  {
139  size.setHeight(100);
140  }
141 
142  getInstance().setFrame(pref, pos.x(), pos.y(), size.width(), size.height());
143 }
144 
145 void TYPreferenceManager::loadGeometryFromPreferences(const QString& pref, QWidget* pWidget)
146 {
147  Q_ASSERT(pWidget);
148 
149  if (getInstance().getBool(pref + "Maximized"))
150  {
151  pWidget->showMaximized();
152  }
153  else
154  {
155  int posX = 0, posY = 0, sizeX = 0, sizeY = 0;
156 
157  getInstance().getFrame(pref, posX, posY, sizeX, sizeY);
158 
159  if (posX < 0)
160  {
161  posX = 0;
162  }
163  if (posY < 0)
164  {
165  posY = 0;
166  }
167  // pWidget->move(posX, posY);
168  if (sizeX <= 0)
169  {
170  sizeX = 100;
171  }
172  if (sizeY <= 0)
173  {
174  sizeY = 100;
175  }
176  pWidget->resize(sizeX, sizeY);
177  }
178 }
179 
180 void TYPreferenceManager::setSpectre(const QString& pref, const TYSpectre* pSpectre)
181 {
182  setSpectre(getInstance().getCurrentDirectory(), pref, pSpectre);
183 }
184 
185 void TYPreferenceManager::setSpectre(const QString& dir, const QString& pref, const TYSpectre* pSpectre)
186 {
187  for (unsigned int i = 0; i < pSpectre->getNbValues(); i++)
188  {
189  setFloat(pref + OPreferenceManager::intToString(i), pSpectre->getTabValReel()[i]);
190  }
191 }
192 
193 TYSpectre* TYPreferenceManager::getSpectre(const QString& pref)
194 {
195  return getSpectre(getInstance().getCurrentDirectory(), pref);
196 }
197 
198 TYSpectre* TYPreferenceManager::getSpectre(const QString& dir, const QString& pref)
199 {
200  int nbFreq = 0;
201 
202  // Search array size
203  while (exists(dir, pref + OPreferenceManager::intToString(nbFreq)))
204  {
205  ++nbFreq;
206  }
207 
208  if (nbFreq <= 0)
209  {
210  return nullptr;
211  }
212 
213  TYSpectre* pSpectre = new TYSpectre();
214  for (int i = 0; i < nbFreq; i++)
215  {
216  double value = getFloat(dir, pref + OPreferenceManager::intToString(i));
217  pSpectre->getTabValReel()[i] = value;
218  }
219 
220  return pSpectre;
221 }
222 
223 bool TYPreferenceManager::copyDefaultSettings(const QString& sourcePath, const QString& destinationPath)
224 {
225  QFile sourceFile(sourcePath);
226  QFile destinationFile(destinationPath);
227 
228  // Check if the source file exists
229  if (!sourceFile.exists())
230  {
231  qDebug() << "Source file does not exist:" << sourcePath;
232  return false;
233  }
234 
235  // Attempt to open the source file for reading
236  if (!sourceFile.open(QIODevice::ReadOnly))
237  {
238  qDebug() << "Error opening source file for reading:" << sourcePath;
239  return false;
240  }
241 
242  // Attempt to open the destination file for writing
243  if (!destinationFile.open(QIODevice::WriteOnly))
244  {
245  qDebug() << "Error opening destination file for writing:" << destinationPath;
246  sourceFile.close();
247  return false;
248  }
249 
250  // Copy the file content
251  QByteArray fileContent = sourceFile.readAll();
252  if (destinationFile.write(fileContent) == -1)
253  {
254  qDebug() << "Error writing to destination file:" << destinationPath;
255  sourceFile.close();
256  destinationFile.close();
257  return false;
258  }
259 
260  qDebug() << "File copied successfully from" << sourcePath << "to" << destinationPath;
261 
262  // Close the file handles
263  sourceFile.close();
264  destinationFile.close();
265 
266  return true;
267 }
268 
269 #endif // TY_USE_IHM
pour l'application Tympan (fichier header)
static QString getResourcePath()
Definition: OLocalizator.h:77
Systeme de gestion des preferences.
static QString intToString(const int &val)
unsigned int getNbValues() const
Number of values in the spectrum.
Definition: spectre.cpp:187
double * getTabValReel() override
Definition: spectre.h:357