Code_TYMPAN  4.2.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 
22 #include <qdir.h>
23 #include <qtimer.h>
24 #include <qmessagebox.h>
25 
26 #include "Tympan/core/config.h"
39 #include "TYApplication.h"
40 
41 
42 using namespace Qt;
43 
44 #define TR(id) OLocalizator::getString("TYApplication", (id))
45 
46 
47 TYApplication* getTYApp() { return (TYApplication*) qApp; }
48 
49 //Chemin d'origine de l'application.
51 
52 // Repertoire des settings utilisateur
53 QString* TYApplication::_settingsDir = NULL;
54 
55 // Nom du repertoire de travail
56 QString* TYApplication::_currentDirName = NULL;
57 
58 // Nom du fichier de travail
59 QString* TYApplication::_currentFileName = NULL;
60 
62 {
63  if (getTYApp())
64  {
65  return getTYApp()->getMainWnd();
66  }
67  else { return NULL; }
68 }
69 
70 void writeOutputMsg(QString msg)
71 {
72  if (getTYMainWnd())
73  {
75  }
76 }
77 
78 void writeDebugMsg(QString msg)
79 {
80 #ifdef _DEBUG
81  writeOutputMsg("Debug : " + msg + "\n");
82 #endif
83 }
84 
86 {
87  if (!_originalCurrentDirPath)
88  {
89  _originalCurrentDirPath = new QString(QDir::currentPath());
90  return *_originalCurrentDirPath;
91  }
92 
93  return *_originalCurrentDirPath;
94 }
95 
96 void TYApplication::setOriginalCurrentDirPath(const QString& newValue)
97 {
98  if (_originalCurrentDirPath)
99  {
100  *_originalCurrentDirPath = newValue;
101  }
102  else
103  {
104  _originalCurrentDirPath = new QString(newValue);
105  }
106 }
107 
109 {
110  if (_currentFileName)
111  {
112  return *_currentFileName;
113  }
114  else
115  {
116  _currentFileName = new QString("");
117  return *_currentFileName;
118  }
119 }
120 
121 void TYApplication::setCurrentFileName(const QString& newValue)
122 {
123  if (_currentFileName)
124  {
125  *_currentFileName = newValue;
126  }
127  else
128  {
129  _currentFileName = new QString(newValue);
130  }
131 }
132 
134 {
135  if (_currentDirName)
136  {
137  return *_currentDirName;
138  }
139  else
140  {
141  *_currentDirName = tympanUserDir();
142  return *_currentDirName;
143  }
144 }
145 
146 
147 void TYApplication::setCurrentDirName(const QString& newValue)
148 {
149  if (_currentDirName)
150  {
151  *_currentDirName = newValue;
152  }
153  else
154  {
155  _currentDirName = new QString(newValue);
156  }
157 }
158 
159 
160 TYApplication::TYApplication(int& argc, char** argv)
161  : QApplication(argc, argv), _actionManager(10)
162 {
163  _pSplash = NULL;
164  _pMainWnd = NULL;
165  _pCurProjet = NULL;
166  _pCurSiteNode = NULL;
167  _pCalculManager = NULL;
168  _saved = false;
169  QStringList args = this->arguments();
170 }
171 
173 {
174  if (_pSplash)
175  {
176  delete _pSplash;
177  _pSplash = NULL;
178  }
179 }
180 
182 {
183  // Resources path
184  QString ressourcePath(getOriginalCurrentDirPath() + "/resources/");
185  ressourcePath = QDir::toNativeSeparators(ressourcePath);
186 
187  // Splash screen
188 #ifndef _DEBUG
189  QPixmap pix(ressourcePath + "Splash.png");
190 
191  if (pix.isNull())
192  {
193  pix = QPixmap(230, 368);
194  pix.fill();
195  }
196 
197  QString version = TY_PRODUCT_VERSION_;
198  _pSplash = new OSplashScreen(pix, "<font color=\"#09367A\"><i><b>TYMPAN " + version + "</i></b></font>");
199 
200  QColor splashColor(4, 34, 245);
201  _pSplash->setStatus("Initialisation...", AlignLeft, splashColor);
202 
203  // Resources
204  _pSplash->setStatus("Chargement des ressources...", AlignLeft, splashColor);
205 #endif
206 
207  // TODO Check consistency of this coherent with the resources distribution.
208  if (!((OLocalizator::setRessourceFile(ressourcePath + "Language.xml") == true)
209  || (OLocalizator::setRessourceFile(ressourcePath + "Language_FR.xml") == true)
210  || (OLocalizator::setRessourceFile(ressourcePath + "Language_EN.xml") == true)))
211  {
212 #ifndef _DEBUG
213  _pSplash->hide();
214 #endif
215  QMessageBox::critical(NULL, "Tympan fatal error", "Resources file not found : Language.xml");
216  return false;
217  }
218  OLocalizator::setRessourcePath(ressourcePath);
219 
220  // Creation de la fenetre principale, contenant le workspace
221  bool bStatus = createMainWnd();
222  // Initialisation du Progress Manager
223  bStatus &= createProgressMngr();
224  // Initialisation du Message Manager
225  bStatus &= createMessageMngr();
226  // Chargement des textures
227  bStatus &= loadTextures();
228  // Chargement des plugins
229  bStatus &= loadPlugins();
230  // Initialisation du Calcul Manager
231  bStatus &= createCalculMngr();
232  // Chargement des settings
233  bStatus &= loadSettings();
234  // Definition du chemin pour la sauvegarde du fichier topographique temporaire
235  bStatus &= createCacheDir();
236  // Creation d'un dossier de travail/d'echange
237  bStatus &= createWorkDir();
238 
239 #ifndef _DEBUG
240  _pSplash->setStatus("Chargement termine.", AlignLeft, splashColor);
241 #endif
242 
243  return bStatus;
244 
245 }
246 
248 {
249 #ifndef _DEBUG
250  QColor splashColor(4, 34, 245);
251  _pSplash->setStatus("Creation de l'espace de travail...", AlignLeft, splashColor);
252 #endif
253  _pMainWnd = new TYMainWindow();
254 
255  return true;
256 }
257 
259 {
260 #ifndef _DEBUG
261  QColor splashColor(4, 34, 245);
262  _pSplash->setStatus("Initialisation du gestionnaire de progression...", AlignLeft, splashColor);
263 #endif
264  TYProgressManager::create(_pMainWnd, "progress manager");
265 
266  return true;
267 }
268 
270 {
271 #ifndef _DEBUG
272  QColor splashColor(4, 34, 245);
273  _pSplash->setStatus("Initialisation du gestionnaire de message...", AlignLeft, splashColor);
274 #endif
277 
278  return true;
279 }
280 
282 {
283 #ifndef _DEBUG
284  QColor splashColor(4, 34, 245);
285  _pSplash->setStatus("Chargement des textures...", AlignLeft, splashColor);
286 #endif
289  if (!_pImageManager->loadImages())
290  {
291 #ifndef _DEBUG
292  _pSplash->hide();
293 #endif
294  QMessageBox::critical(NULL, "Tympan fatal error", "Image loading failed!");
295  return false;
296  }
297 
298  return true;
299 }
300 
302 {
303 #ifndef _DEBUG
304  QColor splashColor(4, 34, 245);
305  _pSplash->setStatus("Chargement des plugins...", AlignLeft, splashColor);
306 #endif
309  QString pluginsPath;
310 #ifndef _DEBUG
311  pluginsPath = getOriginalCurrentDirPath() + "/plugins/";
312 #else
313  pluginsPath = getOriginalCurrentDirPath() + "/pluginsd/";
314 #endif
315  if (!_pPluginManager->loadPlugins(pluginsPath))
316  {
317 #ifndef _DEBUG
318  _pSplash->hide();
319 #endif
320  QMessageBox::critical(NULL, "Tympan fatal error", "Plugin loading failed!");
321  return false;
322  }
323 
324  return true;
325 }
326 
328 {
329 #ifndef _DEBUG
330  QColor splashColor(4, 34, 245);
331  _pSplash->setStatus("Initialisation du gestionnaire de calcul...", AlignLeft, splashColor);
332 #endif
335 
336  return true;
337 }
338 
340 {
341 #ifndef _DEBUG
342  QColor splashColor(4, 34, 245);
343  _pSplash->setStatus("Chargement des preferences utilisateur...", AlignLeft, splashColor);
344 #endif
345  _pMainWnd->loadSettings(QDir::toNativeSeparators(tympanUserDir() + "/Settings" + TY_PRODUCT_VERSION_));
346 
347  return true;
348 }
349 
351 {
352 #ifndef _DEBUG
353  QColor splashColor(4, 34, 245);
354  _pSplash->setStatus("Definition du dossier temporaire de travail...", AlignLeft, splashColor);
355 #endif
356  QString tympanCachePath(tympanUserDir() + "/cache/");
357  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
358  QDir tympanCacheDir(tympanCachePath);
359  if (!tympanCacheDir.exists())
360  {
361  tympanCacheDir.mkdir(tympanCachePath);
362  }
363  TYSiteNode::setTopoFilePath(tympanCachePath.toLatin1().data());
364 
365  return true;
366 }
367 
369 {
370 #ifndef _DEBUG
371  QColor splashColor(4, 34, 245);
372  _pSplash->setStatus("Initialisation du dossier de travail...", AlignLeft, splashColor);
373 #endif
374  QString tympanExchangePath(tympanUserDir());
375  tympanExchangePath = QDir::toNativeSeparators(tympanExchangePath);
376  setCurrentDirName(tympanExchangePath);
377  QDir tympanExchangeDir(tympanExchangePath);
378  if (!tympanExchangeDir.exists())
379  {
380  tympanExchangeDir.mkdir(tympanExchangePath);
381  }
382  // Le dossier d'echange devient le dossier courant de l'application
383  QDir::setCurrent(tympanExchangePath);
384 
385  return true;
386 }
387 
388 
390 {
391  // Affichage
392  _pMainWnd->show();
393 
394  if (_pSplash)
395  {
397  delete _pSplash;
398  _pSplash = NULL;
399  }
400 
401  // Execution de la boucle principale Qt
402  exec();
403 
404  return true;
405 }
406 
408 {
409  // Suppression des fichiers topographiques temporaires
410  QString tympanCachePath(tympanUserDir() + "/cache/");
411  tympanCachePath = QDir::toNativeSeparators(tympanCachePath);
412  QDir tympanCacheDir(tympanCachePath);
413  for (unsigned int i = 0; i < tympanCacheDir.count(); i++)
414  {
415  tympanCacheDir.remove(tympanCacheDir[i]);
416  }
417 
418  if (_pCalculManager != NULL)
419  {
420  delete _pCalculManager;
421  _pCalculManager = NULL;
422  }
423  // setMainWidget(NULL);
424  delete _pMainWnd;
425  _pMainWnd = NULL;
426  _pMessageManager = NULL;
427  _pImageManager = NULL;
428  _pPluginManager = NULL;
429  _pCurProjet = NULL;
430  _pCurSiteNode = NULL;
431 
432  return true;
433 }
434 
436 {
437  _pCurProjet = pProjet;
439 }
440 
442 {
443  _pCurSiteNode = pSiteNode;
445 }
446 
447 /*static*/ QString TYApplication::tympanUserDir()
448 {
449  QString workingDir(getSettingsDir());
450  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "WorkDirPath"))
451  {
452  workingDir = TYPreferenceManager::getString(TYDIRPREFERENCEMANAGER, "WorkDirPath");
453  }
454  else
455  {
456  TYPreferenceManager::setString(TYDIRPREFERENCEMANAGER, "WorkDirPath", workingDir);
457  }
458 
459  return workingDir;
460 }
461 
463 {
464  if (!_settingsDir)
465  {
466  _settingsDir = new QString();
467  QString dir;
468 
469  dir = QString::fromLatin1(getenv("TYMPANUSERDIR"));
470 
471  // Si TYMPANUSERDIR n'est pas defini, on essaye HOME
472  if (dir.isEmpty() || !QFile::exists(dir))
473  {
474  dir = QDir::home().path() + "/TYMPAN";
475 
476  QDir tympanDir(dir);
477  if (!tympanDir.exists())
478  {
479  tympanDir.mkdir(dir);
480  }
481  }
482  // Si vide, on essaye de prendre la variable TEMP qui est generalement definie
483  if (dir.isEmpty() || !QFile::exists(dir))
484  {
485  dir = QString::fromLatin1(getenv("TEMP"));
486  }
487 
488  // Si TEMP est vide, on essaye TMP
489  if (dir.isEmpty() || !QFile::exists(dir))
490  {
491  dir = QString::fromLatin1(getenv("TMP"));
492  }
493  // En desespoir de cause on se rabat sur le repertoire de l'application
494  if (dir.isEmpty() || !QFile::exists(dir))
495  {
497  }
498 
499  *_settingsDir = dir;
500  }
501 
502  return *_settingsDir;
503 }
504 
505 /*static*/ QString TYApplication::tympanAppDir()
506 {
507  return getOriginalCurrentDirPath();
508 }
509 
510 
511 
static const QString & getCurrentDirName()
void changeCurrentCalcul(LPTYCalcul pCalcul)
Gestionnaire des calculs acoustiques. Il fait l&#39;interface entre l&#39;IHM et le gestionnaire de donnees p...
bool _saved
Indicateur de l&#39;etat de sauvegarde du fichier courant.
TYMainWindow * _pMainWnd
La fenetre principale de l&#39;application.
void setCurrent(LPTYCalcul pCalcul)
Set du Calcul et Projet courant.
static QString * _currentDirName
Nom du repertoire de travail courant.
Splash screen widget (fichier header)
static QString tympanUserDir()
Retourne le chemin du dossier de sauvegarde de l&#39;utilisateur courant. Celui-ci est donne par la varia...
bool init()
Initialise l&#39;application et charges toutes les ressources.
static void setOriginalCurrentDirPath(const QString &)
Set Chemin d&#39;origine de l&#39;application.
void curSiteNodeChanged(LPTYSiteNode pCurSiteNode)
Signal que le site node courant a change.
void writeOutputMsg(QString msg)
Affiche un message dans la fenetre de sortie.
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:54
static const QString & getSettingsDir()
Get chemin des preferences utilisateur.
void appendMsg(QString msg)
bool close()
Ferme l&#39;application et libere toutes les ressources.
Fenetre principale de l&#39;application Tympan (fichier header)
TYCalculManager * _pCalculManager
Le gestionnaire de calculs.
static const QString & getOriginalCurrentDirPath()
Get Chemin d&#39;origine de l&#39;application. (singleton)
static void setCurrentFileName(const QString &fileName)
Enregistre le nom du ficgier de travail courant.
void setCurProjet(LPTYProjet pProjet)
Set/Get du projet courant.
pour l&#39;application Tympan (fichier header)
static QString * _originalCurrentDirPath
Chemin d&#39;origine de l&#39;application.
TYOutputFrame * getOutputFrame()
Definition: TYMainWindow.h:72
bool loadSettings(const QString &fileName)
bool run()
Lance l&#39;application.
Fenetre principale de l&#39;application Tympan.
Definition: TYMainWindow.h:52
static bool setRessourceFile(const QString &filename)
TYApplication(int &argc, char **argv)
Constructeur par defaut.
static QString tympanAppDir()
Retourne le chemin du dossier de l&#39;application.
TYProjetFrame * getProjetFrame()
Definition: TYMainWindow.h:70
static void setRessourcePath(const QString &path)
Definition: OLocalizator.h:51
virtual ~TYApplication()
Destructeur.
void writeDebugMsg(QString msg)
Affiche un message de debug dans la fenetre de sortie.
void curProjetChanged(LPTYProjet pCurProjet)
Signal que le projet courant a change.
Frame pour les messages de retour (fichier header)
void setCurSiteNode(LPTYSiteNode pSiteNode)
Set/Get du site node courant.
TYImageManager * _pImageManager
Le gestionnaire d&#39;image.
utilitaire pour la gestion des messages dans Tympan (fichier header)
LPTYProjet _pCurProjet
Pointeur sur le projet courant.
static void setCurrentDirName(const QString &dirName)
Get/Set du repertoire de travail courant.
void setStatus(const QString &message, int alignment=Qt::AlignLeft, const QColor &color=Qt::black)
Affiche un nouveau message indiquant le status de l&#39;application.
static QString * _settingsDir
Chemin des parametres utilisateur de l&#39;application.
static QString * _currentFileName
Nom du fichier de travail courant.
LPTYSiteNode _pCurSiteNode
Pointeur sur le site node courant.
bool setAsSingleton()
Definition: logging.cpp:101
OSplashScreen * _pSplash
Splash screen.
void finish(QWidget *pMainWin)
Indique que le chargement de l&#39;application est termine.
Frame pour la gestion de projet (fichier header)
bool createMessageMngr()
TYMainWindow * getMainWnd()
Get de la fenetre principale.
Definition: TYApplication.h:81
static void create(QWidget *pParent, const char *name=0)
TYApplication * getTYApp()
Retourne le pointeur sur l&#39;application.
TYPluginManager * _pPluginManager
Le gestionnaire de plugins.
static void setTopoFilePath(const std::string &path)
Definition: TYSiteNode.cpp:61
static const QString & getCurrentFileName()
Retourne le nom du fichier en cours d&#39;utilisation.
TYMessageManager * _pMessageManager
Le gestionnaire de messages d&#39;erreurs.
Classe principale pour l&#39;application Tympan.
Definition: TYApplication.h:45
Classe utilitaire pour la gestion des messages dans Tympan.
bool loadPlugins(const QString &directory)
Load, check, create and start plugins.
bool createCalculMngr()
bool createProgressMngr()
TYMainWindow * getTYMainWnd()
Retourne le pointeur sur la fenetre principale.