Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYApplication.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 
21 #include <qdir.h>
22 #include <qtimer.h>
23 #include <qmessagebox.h>
24 #include <signal.h>
25 
26 #include "Tympan/core/config.h"
40 #include "TYApplication.h"
41 
42 // using namespace Qt;
43 
44 #define TR(id) OLocalizator::getString("TYApplication", (id))
45 
47 {
48  return (TYApplication*)qApp;
49 }
50 
51 // Chemin d'origine de l'application.
53 
54 // Repertoire des settings utilisateur
55 QString* TYApplication::_settingsDir = NULL;
56 
57 // Nom du repertoire de travail
58 QString* TYApplication::_currentDirName = NULL;
59 
60 // Nom du fichier de travail
61 QString* TYApplication::_currentFileName = NULL;
62 
64 {
65  if (getTYApp())
66  {
67  return getTYApp()->getMainWnd();
68  }
69  else
70  {
71  return NULL;
72  }
73 }
74 
75 void writeOutputMsg(QString msg)
76 {
77  if (getTYMainWnd())
78  {
80  }
81 }
82 
83 void writeDebugMsg(QString msg)
84 {
85 #ifdef _DEBUG
86  writeOutputMsg("Debug : " + msg + "\n");
87 #endif
88 }
89 
91 {
93  {
94  _originalCurrentDirPath = new QString(QDir::currentPath());
96  }
97 
99 }
100 
101 void TYApplication::setOriginalCurrentDirPath(const QString& newValue)
102 {
104  {
105  *_originalCurrentDirPath = newValue;
106  }
107  else
108  {
109  _originalCurrentDirPath = new QString(newValue);
110  }
111 }
112 
114 {
115  if (_currentFileName)
116  {
117  return *_currentFileName;
118  }
119  else
120  {
121  _currentFileName = new QString("");
122  return *_currentFileName;
123  }
124 }
125 
126 void TYApplication::setCurrentFileName(const QString& newValue)
127 {
128  if (_currentFileName)
129  {
130  *_currentFileName = newValue;
131  }
132  else
133  {
134  _currentFileName = new QString(newValue);
135  }
136 }
137 
139 {
140  if (_currentDirName)
141  {
142  return *_currentDirName;
143  }
144  else
145  {
147  return *_currentDirName;
148  }
149 }
150 
151 void TYApplication::setCurrentDirName(const QString& newValue)
152 {
153  if (_currentDirName)
154  {
155  *_currentDirName = newValue;
156  }
157  else
158  {
159  _currentDirName = new QString(newValue);
160  }
161 }
162 
163 TYApplication::TYApplication(int& argc, char** argv) : QApplication(argc, argv), _actionManager(10)
164 {
165  _pSplash = NULL;
166  _pMainWnd = NULL;
167  _pCurProjet = NULL;
168  _pCurSiteNode = NULL;
169  _pCalculManager = NULL;
170  _saved = false;
171  QStringList args = this->arguments();
172  signal(SIGSEGV, signalHandler);
173 }
174 
176 {
177  if (_pSplash)
178  {
179  delete _pSplash;
180  _pSplash = NULL;
181  }
182 }
183 
185 {
186  // Resources path
187  QString ressourcePath(getOriginalCurrentDirPath() + "/resources/");
188  ressourcePath = QDir::toNativeSeparators(ressourcePath);
189 
190  // Splash screen
191 #ifndef _DEBUG
192  QPixmap pix(ressourcePath + "Splash.png");
193 
194  if (pix.isNull())
195  {
196  pix = QPixmap(230, 368);
197  pix.fill();
198  }
199 
200  QString version = TY_PRODUCT_VERSION_;
201  _pSplash = new OSplashScreen(pix, "<font color=\"#09367A\"><i><b>TYMPAN " + version + "</i></b></font>");
202 
203  QColor splashColor(4, 34, 245);
204  _pSplash->setStatus("Initialisation...", Qt::AlignLeft, splashColor);
205 
206  // Resources
207  _pSplash->setStatus("Chargement des ressources...", Qt::AlignLeft, splashColor);
208 #endif
209 
210  // TODO Check consistency of this coherent with the resources distribution.
211  if (!((OLocalizator::setRessourceFile(ressourcePath + "Language.xml") == true) ||
212  (OLocalizator::setRessourceFile(ressourcePath + "Language_FR.xml") == true) ||
213  (OLocalizator::setRessourceFile(ressourcePath + "Language_EN.xml") == true)))
214  {
215 #ifndef _DEBUG
216  _pSplash->hide();
217 #endif
218  QMessageBox::critical(NULL, "Tympan fatal error", "Resources file not found : Language.xml");
219  return false;
220  }
221  OLocalizator::setRessourcePath(ressourcePath);
222 
223  // Creation de la fenetre principale, contenant le workspace
224  bool bStatus = createMainWnd();
225  // Initialisation du Progress Manager
226  bStatus &= createProgressMngr();
227  // Initialisation du Message Manager
228  bStatus &= createMessageMngr();
229  // Chargement des textures
230  bStatus &= loadTextures();
231  // Chargement des plugins
232  bStatus &= loadPlugins();
233  // Initialisation du Calcul Manager
234  bStatus &= createCalculMngr();
235  // Chargement des settings
236  bStatus &= loadSettings();
237  // Definition du chemin pour la sauvegarde du fichier topographique temporaire
238  bStatus &= createCacheDir();
239  // Creation d'un dossier de travail/d'echange
240  bStatus &= createWorkDir();
241  // Fournit une fonction retournant la forme du spectre initiale à afficher pour le projet
243  // Fournit une fonction retournant la pondération du spectre initiale à afficher pour le projet
245 
246 #ifndef _DEBUG
247  _pSplash->setStatus("Chargement termine.", Qt::AlignLeft, splashColor);
248 #endif
249 
250  return bStatus;
251 }
252 
254 {
255 #ifndef _DEBUG
256  QColor splashColor(4, 34, 245);
257  _pSplash->setStatus("Creation de l'espace de travail...", Qt::AlignLeft, splashColor);
258 #endif
259  _pMainWnd = new TYMainWindow();
260 
261  return true;
262 }
263 
265 {
266 #ifndef _DEBUG
267  QColor splashColor(4, 34, 245);
268  _pSplash->setStatus("Initialisation du gestionnaire de progression...", Qt::AlignLeft, splashColor);
269 #endif
270  TYProgressManager::create(_pMainWnd, "progress manager");
271 
272  return true;
273 }
274 
276 {
277 #ifndef _DEBUG
278  QColor splashColor(4, 34, 245);
279  _pSplash->setStatus("Initialisation du gestionnaire de message...", Qt::AlignLeft, splashColor);
280 #endif
283 
284  return true;
285 }
286 
288 {
289 #ifndef _DEBUG
290  QColor splashColor(4, 34, 245);
291  _pSplash->setStatus("Chargement des textures...", Qt::AlignLeft, splashColor);
292 #endif
295  if (!_pImageManager->loadImages())
296  {
297 #ifndef _DEBUG
298  _pSplash->hide();
299 #endif
300  QMessageBox::critical(NULL, "Tympan fatal error", "Image loading failed!");
301  return false;
302  }
303 
304  return true;
305 }
306 
308 {
309 #ifndef _DEBUG
310  QColor splashColor(4, 34, 245);
311  _pSplash->setStatus("Chargement des plugins...", Qt::AlignLeft, splashColor);
312 #endif
315  QString pluginsPath;
316 #ifndef _DEBUG
317  pluginsPath = getOriginalCurrentDirPath() + "/plugins/";
318 #else
319  pluginsPath = getOriginalCurrentDirPath() + "/pluginsd/";
320 #endif
321  if (!_pPluginManager->loadPlugins(pluginsPath))
322  {
323 #ifndef _DEBUG
324  _pSplash->hide();
325 #endif
326  QMessageBox::critical(NULL, "Tympan fatal error", "Plugin loading failed!");
327  return false;
328  }
329 
330  return true;
331 }
332 
334 {
335 #ifndef _DEBUG
336  QColor splashColor(4, 34, 245);
337  _pSplash->setStatus("Initialisation du gestionnaire de calcul...", Qt::AlignLeft, splashColor);
338 #endif
342 
343  return true;
344 }
345 
347 {
348 #ifndef _DEBUG
349  QColor splashColor(4, 34, 245);
350  _pSplash->setStatus("Chargement des preferences utilisateur...", Qt::AlignLeft, splashColor);
351 #endif
352  if (!_pMainWnd->loadSettings(QDir::toNativeSeparators(tympanUserDir() + "/Settings")))
353  {
354 #ifndef _DEBUG
355  _pSplash->hide();
356 #endif
357  QMessageBox::critical(NULL, "Tympan fatal error", "Settings loading failed!");
358  return false;
359  }
360 
361  return true;
362 }
363 
365 {
366 #ifndef _DEBUG
367  QColor splashColor(4, 34, 245);
368  _pSplash->setStatus("Definition du dossier temporaire de travail...", Qt::AlignLeft, splashColor);
369 #endif
370 
371  QString tympanCachePath(tympanUserDir() + "/cache/");
372  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
373  QDir tympanCacheDir(tympanCachePath);
374  if (!tympanCacheDir.exists())
375  {
376  tympanCacheDir.mkdir(tympanCachePath);
377  }
378  TYSiteNode::setTopoFilePath(tympanCachePath.toLatin1().data());
379 
380  return true;
381 }
382 
384 {
385 #ifndef _DEBUG
386  QColor splashColor(4, 34, 245);
387  _pSplash->setStatus("Initialisation du dossier de travail...", Qt::AlignLeft, splashColor);
388 #endif
389  QString tympanExchangePath(tympanUserDir());
390  tympanExchangePath = QDir::toNativeSeparators(tympanExchangePath);
391  setCurrentDirName(tympanExchangePath);
392  QDir tympanExchangeDir(tympanExchangePath);
393  if (!tympanExchangeDir.exists())
394  {
395  tympanExchangeDir.mkdir(tympanExchangePath);
396  }
397  // Le dossier d'echange devient le dossier courant de l'application
398  QDir::setCurrent(tympanExchangePath);
399 
400  return true;
401 }
402 
404 {
405  // Affichage
406  _pMainWnd->show();
407 
408  if (_pSplash)
409  {
411  delete _pSplash;
412  _pSplash = NULL;
413  }
414 
415  // Execution de la boucle principale Qt
416  exec();
417 
418  return true;
419 }
420 
422 {
423  // Suppression des fichiers topographiques temporaires
424 
425  QString tympanCachePath(tympanUserDir() + "/cache/");
426  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
427  QDir tympanCacheDir(tympanCachePath);
428  for (unsigned int i = 0; i < tympanCacheDir.count(); i++)
429  {
430  tympanCacheDir.remove(tympanCacheDir[i]);
431  }
432 
433  if (_pCalculManager != NULL)
434  {
435  delete _pCalculManager;
436  _pCalculManager = NULL;
437  }
438  _pMessageManager = NULL;
439  _pImageManager = NULL;
440  _pPluginManager = NULL;
441  _pCurProjet = NULL;
442  _pCurSiteNode = NULL;
443 
444  return true;
445 }
446 
448 {
449  _pCurProjet = pProjet;
451 }
452 
454 {
455  auto* app = getTYApp();
456  LPTYProjet proj = app ? app->getCurProjet() : LPTYProjet();
457  if (proj && proj->displaySpectrumInOctaveForm())
458  {
459  return SPECTRE_FORM_OCT;
460  }
461  // Valeur par défaut souhaitée : tiers d’octave
462  return SPECTRE_FORM_TIERS;
463 }
464 
466 {
467  auto* app = getTYApp();
468  LPTYProjet proj = app ? app->getCurProjet() : LPTYProjet();
469  if (proj)
470  {
471  return proj->getSpectrePond();
472  }
473  // Valeur par défaut souhaitée : dB(Z)
474  return QString{"dB(Z)"};
475 }
476 
478 {
479  _pCurSiteNode = pSiteNode;
481 }
482 
483 /*static*/ QString TYApplication::tympanUserDir()
484 {
485  QString workingDir(getSettingsDir());
486  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "WorkDirPath"))
487  {
488  workingDir = TYPreferenceManager::getString(TYDIRPREFERENCEMANAGER, "WorkDirPath");
489  }
490  else
491  {
492  TYPreferenceManager::setString(TYDIRPREFERENCEMANAGER, "WorkDirPath", workingDir);
493  }
494 
495  return workingDir;
496 }
497 
499 {
500  if (!_settingsDir)
501  {
502  _settingsDir = new QString();
503  QString dir;
504 #ifdef _WIN32
505  char* path = nullptr;
506  size_t sz = 0;
507  _dupenv_s(&path, &sz, "TYMPANUSERDIR");
508 #else
509  char* path = getenv("TYMPANUSERDIR");
510 #endif
511  dir = QString::fromLatin1(path);
512  // Si TYMPANUSERDIR n'est pas defini, on essaye HOME
513  if (dir.isEmpty() || !QFile::exists(dir))
514  {
515  dir = QDir::home().path() + "/TYMPAN";
516 
517  QDir tympanDir(dir);
518  if (!tympanDir.exists())
519  {
520  tympanDir.mkdir(dir);
521  }
522  }
523  // Si vide, on essaye de prendre la variable TEMP qui est generalement definie
524  if (dir.isEmpty() || !QFile::exists(dir))
525  {
526 #ifdef _WIN32
527  _dupenv_s(&path, &sz, "TEMP");
528 #else
529  path = getenv("TEMP");
530 #endif
531  dir = QString::fromLatin1(path);
532  }
533 
534  // Si TEMP est vide, on essaye TMP
535  if (dir.isEmpty() || !QFile::exists(dir))
536  {
537 #ifdef _WIN32
538  _dupenv_s(&path, &sz, "TEMP");
539 #else
540  path = getenv("TMP");
541 #endif
542  dir = QString::fromLatin1(path);
543  }
544  // En desespoir de cause on se rabat sur le repertoire de l'application
545  if (dir.isEmpty() || !QFile::exists(dir))
546  {
548  }
549 
550  *_settingsDir = dir;
551  }
552 
553  return *_settingsDir;
554 }
555 
556 /*static*/ QString TYApplication::tympanAppDir()
557 {
558  return getOriginalCurrentDirPath();
559 }
560 
562 {
563  QDir dir = getCurrentDirName();
564  QString dirName = dir.absolutePath();
565  QDir::setCurrent(dirName);
566 }
567 
568 void signalHandler(int signum)
569 {
570  signal(signum, SIG_DFL);
571  qDebug() << "Ooops ! Tympan just crashed, sorry ... Please warn support and relaunch application.";
572  QMessageBox::critical(nullptr, TR("id_crash_msg_title"), TR("id_crash_msg_text"));
573 }
Splash screen widget (fichier header)
void writeOutputMsg(QString msg)
Affiche un message dans la fenetre de sortie.
TYApplication * getTYApp()
Retourne le pointeur sur l'application.
void writeDebugMsg(QString msg)
Affiche un message de debug dans la fenetre de sortie.
void signalHandler(int signum)
Handle segmentation fault and warn user before app terminates.
TYMainWindow * getTYMainWnd()
Retourne le pointeur sur la fenetre principale.
#define TR(id)
pour l'application Tympan (fichier header)
SmartPtr< TYProjet > LPTYProjet
Smart pointer sur TYProjet.
Definition: TYDefines.h:280
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:51
Fenetre principale de l'application Tympan (fichier header)
utilitaire pour la gestion des messages dans Tympan (fichier header)
Frame pour les messages de retour (fichier header)
Frame pour la gestion de projet (fichier header)
outil IHM pour un spectre (fichier header)
static void setRessourcePath(const QString &path)
Definition: OLocalizator.h:50
static bool setRessourceFile(const QString &filename)
bool setAsSingleton()
Definition: logging.cpp:99
void setStatus(const QString &message, int alignment=Qt::AlignLeft, const QColor &color=Qt::black)
Affiche un nouveau message indiquant le status de l'application.
void finish(QWidget *pMainWin)
Indique que le chargement de l'application est termine.
Classe principale pour l'application Tympan.
Definition: TYApplication.h:46
void setCurSiteNode(LPTYSiteNode pSiteNode)
Set/Get du site node courant.
static TYSpectreForm currentProjectSpectreForm()
Retourne la forme d’affichage Ã&#160; utiliser pour le projet courant.
bool init()
Initialise l'application et charges toutes les ressources.
static void setCurrentQtDir()
Indique Ã&#160; Qt le chemin du répertoire courant.
static const QString & getOriginalCurrentDirPath()
Get Chemin d'origine de l'application. (singleton)
static const QString & getCurrentDirName()
bool createCalculMngr()
static QString tympanAppDir()
Retourne le chemin du dossier de l'application.
static QString * _currentDirName
Nom du repertoire de travail courant.
bool createMessageMngr()
OSplashScreen * _pSplash
Splash screen.
TYMainWindow * getMainWnd()
Get de la fenetre principale.
Definition: TYApplication.h:81
TYMainWindow * _pMainWnd
La fenetre principale de l'application.
static void setOriginalCurrentDirPath(const QString &)
Set Chemin d'origine de l'application.
static QString * _currentFileName
Nom du fichier de travail courant.
bool close()
Ferme l'application et libere toutes les ressources.
bool _saved
Indicateur de l'etat de sauvegarde du fichier courant.
TYApplication(int &argc, char **argv)
Constructeur par defaut.
static const QString & getCurrentFileName()
Retourne le nom du fichier en cours d'utilisation.
static const QString & getSettingsDir()
Get chemin des preferences utilisateur.
void curSiteNodeChanged(LPTYSiteNode pCurSiteNode)
Signal que le site node courant a change.
LPTYProjet _pCurProjet
Pointeur sur le projet courant.
static QString * _originalCurrentDirPath
Chemin d'origine de l'application.
void curProjetChanged(LPTYProjet pCurProjet)
Signal que le projet courant a change.
LPTYSiteNode _pCurSiteNode
Pointeur sur le site node courant.
static void setCurrentFileName(const QString &fileName)
Enregistre le nom du fichier de travail courant.
TYMessageManager * _pMessageManager
Le gestionnaire de messages d'erreurs.
static QString tympanUserDir()
Retourne le chemin du dossier de sauvegarde de l'utilisateur courant. Celui-ci est donne par la varia...
bool run()
Lance l'application.
TYCalculManager * _pCalculManager
Le gestionnaire de calculs.
virtual ~TYApplication()
Destructeur.
TYPluginManager * _pPluginManager
Le gestionnaire de plugins.
void setCurProjet(LPTYProjet pProjet)
Set/Get du projet courant.
static void setCurrentDirName(const QString &dirName)
Get/Set du repertoire de travail courant.
TYImageManager * _pImageManager
Le gestionnaire d'image.
static QString * _settingsDir
Chemin des parametres utilisateur de l'application.
static QString currentProjectSpectrePond()
Retourne la pondération d’affichage Ã&#160; utiliser pour le projet courant.
bool createProgressMngr()
Gestionnaire des calculs acoustiques. Il fait l'interface entre l'IHM et le gestionnaire de donnees p...
void setCurrent(LPTYCalcul pCalcul)
Set du Calcul et Projet courant.
Main window of the Tympan application.
Definition: TYMainWindow.h:50
TYOutputFrame * getOutputFrame()
Definition: TYMainWindow.h:81
bool loadSettings(const QString &fileName)
TYProjetFrame * getProjetFrame()
Definition: TYMainWindow.h:73
Classe utilitaire pour la gestion des messages dans Tympan.
void appendMsg(QString msg)
bool loadPlugins(const QString &directory)
Load, check, create and start plugins.
static void create(QWidget *pParent, const char *name=0)
void changeCurrentCalcul(LPTYCalcul pCalcul)
QString getSpectrePond() const
Get/Set de la pondération du spectre Ã&#160; afficher pour le projet.
Definition: TYProjet.h:613
const bool displaySpectrumInOctaveForm()
Definition: TYProjet.cpp:1167
static void setTopoFilePath(const QString &path)
Definition: TYSiteNode.cpp:54
static void setSpectrePondProvider(TYSpectrePondProvider p)
static void setSpectreFormProvider(TYSpectreFormProvider p)
TYSpectreForm
Spectrum representation.
Definition: spectre.h:36
@ SPECTRE_FORM_TIERS
Definition: spectre.h:37
@ SPECTRE_FORM_OCT
Definition: spectre.h:38