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 
244 #ifndef _DEBUG
245  _pSplash->setStatus("Chargement termine.", Qt::AlignLeft, splashColor);
246 #endif
247 
248  return bStatus;
249 }
250 
252 {
253 #ifndef _DEBUG
254  QColor splashColor(4, 34, 245);
255  _pSplash->setStatus("Creation de l'espace de travail...", Qt::AlignLeft, splashColor);
256 #endif
257  _pMainWnd = new TYMainWindow();
258 
259  return true;
260 }
261 
263 {
264 #ifndef _DEBUG
265  QColor splashColor(4, 34, 245);
266  _pSplash->setStatus("Initialisation du gestionnaire de progression...", Qt::AlignLeft, splashColor);
267 #endif
268  TYProgressManager::create(_pMainWnd, "progress manager");
269 
270  return true;
271 }
272 
274 {
275 #ifndef _DEBUG
276  QColor splashColor(4, 34, 245);
277  _pSplash->setStatus("Initialisation du gestionnaire de message...", Qt::AlignLeft, splashColor);
278 #endif
281 
282  return true;
283 }
284 
286 {
287 #ifndef _DEBUG
288  QColor splashColor(4, 34, 245);
289  _pSplash->setStatus("Chargement des textures...", Qt::AlignLeft, splashColor);
290 #endif
293  if (!_pImageManager->loadImages())
294  {
295 #ifndef _DEBUG
296  _pSplash->hide();
297 #endif
298  QMessageBox::critical(NULL, "Tympan fatal error", "Image loading failed!");
299  return false;
300  }
301 
302  return true;
303 }
304 
306 {
307 #ifndef _DEBUG
308  QColor splashColor(4, 34, 245);
309  _pSplash->setStatus("Chargement des plugins...", Qt::AlignLeft, splashColor);
310 #endif
313  QString pluginsPath;
314 #ifndef _DEBUG
315  pluginsPath = getOriginalCurrentDirPath() + "/plugins/";
316 #else
317  pluginsPath = getOriginalCurrentDirPath() + "/pluginsd/";
318 #endif
319  if (!_pPluginManager->loadPlugins(pluginsPath))
320  {
321 #ifndef _DEBUG
322  _pSplash->hide();
323 #endif
324  QMessageBox::critical(NULL, "Tympan fatal error", "Plugin loading failed!");
325  return false;
326  }
327 
328  return true;
329 }
330 
332 {
333 #ifndef _DEBUG
334  QColor splashColor(4, 34, 245);
335  _pSplash->setStatus("Initialisation du gestionnaire de calcul...", Qt::AlignLeft, splashColor);
336 #endif
340 
341  return true;
342 }
343 
345 {
346 #ifndef _DEBUG
347  QColor splashColor(4, 34, 245);
348  _pSplash->setStatus("Chargement des preferences utilisateur...", Qt::AlignLeft, splashColor);
349 #endif
350  if (!_pMainWnd->loadSettings(QDir::toNativeSeparators(tympanUserDir() + "/Settings")))
351  {
352 #ifndef _DEBUG
353  _pSplash->hide();
354 #endif
355  QMessageBox::critical(NULL, "Tympan fatal error", "Settings loading failed!");
356  return false;
357  }
358 
359  return true;
360 }
361 
363 {
364 #ifndef _DEBUG
365  QColor splashColor(4, 34, 245);
366  _pSplash->setStatus("Definition du dossier temporaire de travail...", Qt::AlignLeft, splashColor);
367 #endif
368 
369  QString tympanCachePath(tympanUserDir() + "/cache/");
370  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
371  QDir tympanCacheDir(tympanCachePath);
372  if (!tympanCacheDir.exists())
373  {
374  tympanCacheDir.mkdir(tympanCachePath);
375  }
376  TYSiteNode::setTopoFilePath(tympanCachePath.toLatin1().data());
377 
378  return true;
379 }
380 
382 {
383 #ifndef _DEBUG
384  QColor splashColor(4, 34, 245);
385  _pSplash->setStatus("Initialisation du dossier de travail...", Qt::AlignLeft, splashColor);
386 #endif
387  QString tympanExchangePath(tympanUserDir());
388  tympanExchangePath = QDir::toNativeSeparators(tympanExchangePath);
389  setCurrentDirName(tympanExchangePath);
390  QDir tympanExchangeDir(tympanExchangePath);
391  if (!tympanExchangeDir.exists())
392  {
393  tympanExchangeDir.mkdir(tympanExchangePath);
394  }
395  // Le dossier d'echange devient le dossier courant de l'application
396  QDir::setCurrent(tympanExchangePath);
397 
398  return true;
399 }
400 
402 {
403  // Affichage
404  _pMainWnd->show();
405 
406  if (_pSplash)
407  {
409  delete _pSplash;
410  _pSplash = NULL;
411  }
412 
413  // Execution de la boucle principale Qt
414  exec();
415 
416  return true;
417 }
418 
420 {
421  // Suppression des fichiers topographiques temporaires
422 
423  QString tympanCachePath(tympanUserDir() + "/cache/");
424  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
425  QDir tympanCacheDir(tympanCachePath);
426  for (unsigned int i = 0; i < tympanCacheDir.count(); i++)
427  {
428  tympanCacheDir.remove(tympanCacheDir[i]);
429  }
430 
431  if (_pCalculManager != NULL)
432  {
433  delete _pCalculManager;
434  _pCalculManager = NULL;
435  }
436  _pMessageManager = NULL;
437  _pImageManager = NULL;
438  _pPluginManager = NULL;
439  _pCurProjet = NULL;
440  _pCurSiteNode = NULL;
441 
442  return true;
443 }
444 
446 {
447  _pCurProjet = pProjet;
449 }
450 
452 {
453  auto* app = getTYApp();
454  LPTYProjet proj = app ? app->getCurProjet() : LPTYProjet();
455  if (proj && proj->displaySpectrumInOctaveForm())
456  {
457  return SPECTRE_FORM_OCT;
458  }
459  // Valeur par défaut souhaitée : tiers d’octave
460  return SPECTRE_FORM_TIERS;
461 }
462 
464 {
465  _pCurSiteNode = pSiteNode;
467 }
468 
469 /*static*/ QString TYApplication::tympanUserDir()
470 {
471  QString workingDir(getSettingsDir());
472  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "WorkDirPath"))
473  {
474  workingDir = TYPreferenceManager::getString(TYDIRPREFERENCEMANAGER, "WorkDirPath");
475  }
476  else
477  {
478  TYPreferenceManager::setString(TYDIRPREFERENCEMANAGER, "WorkDirPath", workingDir);
479  }
480 
481  return workingDir;
482 }
483 
485 {
486  if (!_settingsDir)
487  {
488  _settingsDir = new QString();
489  QString dir;
490 #ifdef _WIN32
491  char* path = nullptr;
492  size_t sz = 0;
493  _dupenv_s(&path, &sz, "TYMPANUSERDIR");
494 #else
495  char* path = getenv("TYMPANUSERDIR");
496 #endif
497  dir = QString::fromLatin1(path);
498  // Si TYMPANUSERDIR n'est pas defini, on essaye HOME
499  if (dir.isEmpty() || !QFile::exists(dir))
500  {
501  dir = QDir::home().path() + "/TYMPAN";
502 
503  QDir tympanDir(dir);
504  if (!tympanDir.exists())
505  {
506  tympanDir.mkdir(dir);
507  }
508  }
509  // Si vide, on essaye de prendre la variable TEMP qui est generalement definie
510  if (dir.isEmpty() || !QFile::exists(dir))
511  {
512 #ifdef _WIN32
513  _dupenv_s(&path, &sz, "TEMP");
514 #else
515  path = getenv("TEMP");
516 #endif
517  dir = QString::fromLatin1(path);
518  }
519 
520  // Si TEMP est vide, on essaye TMP
521  if (dir.isEmpty() || !QFile::exists(dir))
522  {
523 #ifdef _WIN32
524  _dupenv_s(&path, &sz, "TEMP");
525 #else
526  path = getenv("TMP");
527 #endif
528  dir = QString::fromLatin1(path);
529  }
530  // En desespoir de cause on se rabat sur le repertoire de l'application
531  if (dir.isEmpty() || !QFile::exists(dir))
532  {
534  }
535 
536  *_settingsDir = dir;
537  }
538 
539  return *_settingsDir;
540 }
541 
542 /*static*/ QString TYApplication::tympanAppDir()
543 {
544  return getOriginalCurrentDirPath();
545 }
546 
548 {
549  QDir dir = getCurrentDirName();
550  QString dirName = dir.absolutePath();
551  QDir::setCurrent(dirName);
552 }
553 
554 void signalHandler(int signum)
555 {
556  signal(signum, SIG_DFL);
557  qDebug() << "Ooops ! Tympan just crashed, sorry ... Please warn support and relaunch application.";
558  QMessageBox::critical(nullptr, TR("id_crash_msg_title"), TR("id_crash_msg_text"));
559 }
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.
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)
const bool displaySpectrumInOctaveForm()
Definition: TYProjet.cpp:1148
static void setTopoFilePath(const QString &path)
Definition: TYSiteNode.cpp:54
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