Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYApplication.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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"
39 #include "TYApplication.h"
40 
41 using namespace Qt;
42 
43 #define TR(id) OLocalizator::getString("TYApplication", (id))
44 
46 {
47  return (TYApplication*)qApp;
48 }
49 
50 // Chemin d'origine de l'application.
52 
53 // Repertoire des settings utilisateur
54 QString* TYApplication::_settingsDir = NULL;
55 
56 // Nom du repertoire de travail
57 QString* TYApplication::_currentDirName = NULL;
58 
59 // Nom du fichier de travail
60 QString* TYApplication::_currentFileName = NULL;
61 
63 {
64  if (getTYApp())
65  {
66  return getTYApp()->getMainWnd();
67  }
68  else
69  {
70  return NULL;
71  }
72 }
73 
74 void writeOutputMsg(QString msg)
75 {
76  if (getTYMainWnd())
77  {
79  }
80 }
81 
82 void writeDebugMsg(QString msg)
83 {
84 #ifdef _DEBUG
85  writeOutputMsg("Debug : " + msg + "\n");
86 #endif
87 }
88 
90 {
91  if (!_originalCurrentDirPath)
92  {
93  _originalCurrentDirPath = new QString(QDir::currentPath());
94  return *_originalCurrentDirPath;
95  }
96 
97  return *_originalCurrentDirPath;
98 }
99 
100 void TYApplication::setOriginalCurrentDirPath(const QString& newValue)
101 {
102  if (_originalCurrentDirPath)
103  {
104  *_originalCurrentDirPath = newValue;
105  }
106  else
107  {
108  _originalCurrentDirPath = new QString(newValue);
109  }
110 }
111 
113 {
114  if (_currentFileName)
115  {
116  return *_currentFileName;
117  }
118  else
119  {
120  _currentFileName = new QString("");
121  return *_currentFileName;
122  }
123 }
124 
125 void TYApplication::setCurrentFileName(const QString& newValue)
126 {
127  if (_currentFileName)
128  {
129  *_currentFileName = newValue;
130  }
131  else
132  {
133  _currentFileName = new QString(newValue);
134  }
135 }
136 
138 {
139  if (_currentDirName)
140  {
141  return *_currentDirName;
142  }
143  else
144  {
145  *_currentDirName = tympanUserDir();
146  return *_currentDirName;
147  }
148 }
149 
150 void TYApplication::setCurrentDirName(const QString& newValue)
151 {
152  if (_currentDirName)
153  {
154  *_currentDirName = newValue;
155  }
156  else
157  {
158  _currentDirName = new QString(newValue);
159  }
160 }
161 
162 TYApplication::TYApplication(int& argc, char** argv) : QApplication(argc, argv), _actionManager(10)
163 {
164  _pSplash = NULL;
165  _pMainWnd = NULL;
166  _pCurProjet = NULL;
167  _pCurSiteNode = NULL;
168  _pCalculManager = NULL;
169  _saved = false;
170  QStringList args = this->arguments();
171  signal(SIGSEGV, signalHandler);
172 }
173 
175 {
176  if (_pSplash)
177  {
178  delete _pSplash;
179  _pSplash = NULL;
180  }
181 }
182 
184 {
185  // Resources path
186  QString ressourcePath(getOriginalCurrentDirPath() + "/resources/");
187  ressourcePath = QDir::toNativeSeparators(ressourcePath);
188 
189  // Splash screen
190 #ifndef _DEBUG
191  QPixmap pix(ressourcePath + "Splash.png");
192 
193  if (pix.isNull())
194  {
195  pix = QPixmap(230, 368);
196  pix.fill();
197  }
198 
199  QString version = TY_PRODUCT_VERSION_;
200  _pSplash = new OSplashScreen(pix, "<font color=\"#09367A\"><i><b>TYMPAN " + version + "</i></b></font>");
201 
202  QColor splashColor(4, 34, 245);
203  _pSplash->setStatus("Initialisation...", AlignLeft, splashColor);
204 
205  // Resources
206  _pSplash->setStatus("Chargement des ressources...", AlignLeft, splashColor);
207 #endif
208 
209  // TODO Check consistency of this coherent with the resources distribution.
210  if (!((OLocalizator::setRessourceFile(ressourcePath + "Language.xml") == true) ||
211  (OLocalizator::setRessourceFile(ressourcePath + "Language_FR.xml") == true) ||
212  (OLocalizator::setRessourceFile(ressourcePath + "Language_EN.xml") == true)))
213  {
214 #ifndef _DEBUG
215  _pSplash->hide();
216 #endif
217  QMessageBox::critical(NULL, "Tympan fatal error", "Resources file not found : Language.xml");
218  return false;
219  }
220  OLocalizator::setRessourcePath(ressourcePath);
221 
222  // Creation de la fenetre principale, contenant le workspace
223  bool bStatus = createMainWnd();
224  // Initialisation du Progress Manager
225  bStatus &= createProgressMngr();
226  // Initialisation du Message Manager
227  bStatus &= createMessageMngr();
228  // Chargement des textures
229  bStatus &= loadTextures();
230  // Chargement des plugins
231  bStatus &= loadPlugins();
232  // Initialisation du Calcul Manager
233  bStatus &= createCalculMngr();
234  // Chargement des settings
235  bStatus &= loadSettings();
236  // Definition du chemin pour la sauvegarde du fichier topographique temporaire
237  bStatus &= createCacheDir();
238  // Creation d'un dossier de travail/d'echange
239  bStatus &= createWorkDir();
240 
241 #ifndef _DEBUG
242  _pSplash->setStatus("Chargement termine.", AlignLeft, splashColor);
243 #endif
244 
245  return bStatus;
246 }
247 
249 {
250 #ifndef _DEBUG
251  QColor splashColor(4, 34, 245);
252  _pSplash->setStatus("Creation de l'espace de travail...", AlignLeft, splashColor);
253 #endif
254  _pMainWnd = new TYMainWindow();
255 
256  return true;
257 }
258 
260 {
261 #ifndef _DEBUG
262  QColor splashColor(4, 34, 245);
263  _pSplash->setStatus("Initialisation du gestionnaire de progression...", AlignLeft, splashColor);
264 #endif
265  TYProgressManager::create(_pMainWnd, "progress manager");
266 
267  return true;
268 }
269 
271 {
272 #ifndef _DEBUG
273  QColor splashColor(4, 34, 245);
274  _pSplash->setStatus("Initialisation du gestionnaire de message...", AlignLeft, splashColor);
275 #endif
278 
279  return true;
280 }
281 
283 {
284 #ifndef _DEBUG
285  QColor splashColor(4, 34, 245);
286  _pSplash->setStatus("Chargement des textures...", AlignLeft, splashColor);
287 #endif
290  if (!_pImageManager->loadImages())
291  {
292 #ifndef _DEBUG
293  _pSplash->hide();
294 #endif
295  QMessageBox::critical(NULL, "Tympan fatal error", "Image loading failed!");
296  return false;
297  }
298 
299  return true;
300 }
301 
303 {
304 #ifndef _DEBUG
305  QColor splashColor(4, 34, 245);
306  _pSplash->setStatus("Chargement des plugins...", AlignLeft, splashColor);
307 #endif
310  QString pluginsPath;
311 #ifndef _DEBUG
312  pluginsPath = getOriginalCurrentDirPath() + "/plugins/";
313 #else
314  pluginsPath = getOriginalCurrentDirPath() + "/pluginsd/";
315 #endif
316  if (!_pPluginManager->loadPlugins(pluginsPath))
317  {
318 #ifndef _DEBUG
319  _pSplash->hide();
320 #endif
321  QMessageBox::critical(NULL, "Tympan fatal error", "Plugin loading failed!");
322  return false;
323  }
324 
325  return true;
326 }
327 
329 {
330 #ifndef _DEBUG
331  QColor splashColor(4, 34, 245);
332  _pSplash->setStatus("Initialisation du gestionnaire de calcul...", AlignLeft, splashColor);
333 #endif
337 
338  return true;
339 }
340 
342 {
343 #ifndef _DEBUG
344  QColor splashColor(4, 34, 245);
345  _pSplash->setStatus("Chargement des preferences utilisateur...", AlignLeft, splashColor);
346 #endif
347  _pMainWnd->loadSettings(QDir::toNativeSeparators(tympanUserDir() + "/Settings" + TY_PRODUCT_VERSION_));
348 
349  return true;
350 }
351 
353 {
354 #ifndef _DEBUG
355  QColor splashColor(4, 34, 245);
356  _pSplash->setStatus("Definition du dossier temporaire de travail...", AlignLeft, splashColor);
357 #endif
358 
359  QString tympanCachePath(tympanUserDir() + "/cache/");
360  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
361  QDir tympanCacheDir(tympanCachePath);
362  if (!tympanCacheDir.exists())
363  {
364  tympanCacheDir.mkdir(tympanCachePath);
365  }
366  TYSiteNode::setTopoFilePath(tympanCachePath.toLatin1().data());
367 
368  return true;
369 }
370 
372 {
373 #ifndef _DEBUG
374  QColor splashColor(4, 34, 245);
375  _pSplash->setStatus("Initialisation du dossier de travail...", AlignLeft, splashColor);
376 #endif
377  QString tympanExchangePath(tympanUserDir());
378  tympanExchangePath = QDir::toNativeSeparators(tympanExchangePath);
379  setCurrentDirName(tympanExchangePath);
380  QDir tympanExchangeDir(tympanExchangePath);
381  if (!tympanExchangeDir.exists())
382  {
383  tympanExchangeDir.mkdir(tympanExchangePath);
384  }
385  // Le dossier d'echange devient le dossier courant de l'application
386  QDir::setCurrent(tympanExchangePath);
387 
388  return true;
389 }
390 
392 {
393  // Affichage
394  _pMainWnd->show();
395 
396  if (_pSplash)
397  {
399  delete _pSplash;
400  _pSplash = NULL;
401  }
402 
403  // Execution de la boucle principale Qt
404  exec();
405 
406  return true;
407 }
408 
410 {
411  // Suppression des fichiers topographiques temporaires
412 
413  QString tympanCachePath(tympanUserDir() + "/cache/");
414  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
415  QDir tympanCacheDir(tympanCachePath);
416  for (unsigned int i = 0; i < tympanCacheDir.count(); i++)
417  {
418  tympanCacheDir.remove(tympanCacheDir[i]);
419  }
420 
421  if (_pCalculManager != NULL)
422  {
423  delete _pCalculManager;
424  _pCalculManager = NULL;
425  }
426  // setMainWidget(NULL);
427  delete _pMainWnd;
428  _pMainWnd = NULL;
429  _pMessageManager = NULL;
430  _pImageManager = NULL;
431  _pPluginManager = NULL;
432  _pCurProjet = NULL;
433  _pCurSiteNode = NULL;
434 
435  return true;
436 }
437 
439 {
440  _pCurProjet = pProjet;
442 }
443 
445 {
446  _pCurSiteNode = pSiteNode;
448 }
449 
450 /*static*/ QString TYApplication::tympanUserDir()
451 {
452  QString workingDir(getSettingsDir());
453  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "WorkDirPath"))
454  {
455  workingDir = TYPreferenceManager::getString(TYDIRPREFERENCEMANAGER, "WorkDirPath");
456  }
457  else
458  {
459  TYPreferenceManager::setString(TYDIRPREFERENCEMANAGER, "WorkDirPath", workingDir);
460  }
461 
462  return workingDir;
463 }
464 
466 {
467  if (!_settingsDir)
468  {
469  _settingsDir = new QString();
470  QString dir;
471 
472  char* path = nullptr;
473  size_t sz = 0;
474  _dupenv_s(&path, &sz, "LD_LIBRARY_PATH");
475  dir = QString::fromLatin1(path);
476  // Si TYMPANUSERDIR n'est pas defini, on essaye HOME
477  if (dir.isEmpty() || !QFile::exists(dir))
478  {
479  dir = QDir::home().path() + "/TYMPAN";
480 
481  QDir tympanDir(dir);
482  if (!tympanDir.exists())
483  {
484  tympanDir.mkdir(dir);
485  }
486  }
487  // Si vide, on essaye de prendre la variable TEMP qui est generalement definie
488  if (dir.isEmpty() || !QFile::exists(dir))
489  {
490  _dupenv_s(&path, &sz, "TEMP");
491  dir = QString::fromLatin1(path);
492  }
493 
494  // Si TEMP est vide, on essaye TMP
495  if (dir.isEmpty() || !QFile::exists(dir))
496  {
497  _dupenv_s(&path, &sz, "TMP");
498  dir = QString::fromLatin1(path);
499  }
500  // En desespoir de cause on se rabat sur le repertoire de l'application
501  if (dir.isEmpty() || !QFile::exists(dir))
502  {
504  }
505 
506  *_settingsDir = dir;
507  }
508 
509  return *_settingsDir;
510 }
511 
512 /*static*/ QString TYApplication::tympanAppDir()
513 {
514  return getOriginalCurrentDirPath();
515 }
516 
517 void signalHandler(int signum)
518 {
519  signal(signum, SIG_DFL);
520  qDebug() << "Ooops ! Tympan just crashed, sorry ... Please warn support and relaunch application.";
521  QMessageBox::critical(nullptr, TR("id_crash_msg_title"), TR("id_crash_msg_text"));
522 }
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)
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:52
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)
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.
bool init()
Initialise l'application et charges toutes les ressources.
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 ficgier 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.
Fenetre principale de l'application Tympan.
Definition: TYMainWindow.h:53
TYOutputFrame * getOutputFrame()
Definition: TYMainWindow.h:84
bool loadSettings(const QString &fileName)
TYProjetFrame * getProjetFrame()
Definition: TYMainWindow.h:76
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)
static void setTopoFilePath(const std::string &path)
Definition: TYSiteNode.cpp:60