Code_TYMPAN  4.4.0
Industrial site acoustic simulation
main.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 <qmessagebox.h>
22 #include <QDir>
23 #include <QtWebView>
24 #include <QQuickWindow>
25 
26 #include "Tympan/core/logging.h"
28 #include "TYApplication.h"
29 
30 static QtMessageHandler old_handler;
31 
32 static void MyQTMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message)
33 {
34  if (old_handler != NULL)
35  {
36  old_handler(type, context, message);
37  }
38 
39  switch (type)
40  {
41  case QtInfoMsg:
42  break;
43  case QtDebugMsg:
44  break;
45  case QtWarningMsg:
46  break;
47  case QtCriticalMsg:
48  case QtFatalMsg:
49  int selected = 0;
50 
51  selected = QMessageBox::critical(NULL, "Attention !", message, "Debug", "Continue", "Quit");
52 
53  if (selected == 0)
54  {
55  // Debug
56 #if defined(WIN32)
57  _CrtDbgBreak();
58 #endif
59  }
60  else if (selected == 1)
61  {
62  // Continue
63  }
64  else if (selected == 2)
65  {
66  // Quit
67  exit(1);
68  }
69  break;
70  }
71 }
72 
73 static int tyMain(int argc, char** argv)
74 {
75  bool success = false;
76 
77  old_handler = qInstallMessageHandler(MyQTMessageHandler);
78  QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
79  QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGLRhi);
80  TYApplication tyApp(argc, argv);
81  QtWebView::initialize();
82  if (tyApp.init())
83  {
84  // Lance la boucle principal
85  success = tyApp.run();
86 
87  // Termine correctement l'appli
88  tyApp.close();
89  }
90  OMessageManager::get()->debug("Counters : TYElements created %u, deleted %u and ID generated %u",
93  qInstallMessageHandler(old_handler);
94 
95  // return code
96  int ret = 0;
97  success ? ret = 0 : ret = 1;
98 
99  return ret;
100 }
101 
102 bool setenv(const char* pVarEnvName, QString& pVarEnvValue, bool pForceUpdate)
103 {
104  bool ret = true;
105  QString currentVarEnvValue = QString::fromLocal8Bit(qgetenv(pVarEnvName));
106  if (currentVarEnvValue != "" && !pForceUpdate)
107  {
108  OMessageManager::get()->info("Environment variable %s already set to : '%s'.\n", pVarEnvName,
109  currentVarEnvValue.toUtf8().data());
110  pVarEnvValue = currentVarEnvValue;
111  }
112  else
113  {
114  OMessageManager::get()->info("Trying to set environment variable %s to : '%s'.\n", pVarEnvName,
115  pVarEnvValue.toUtf8().data());
116  ret = qputenv(pVarEnvName, pVarEnvValue.toUtf8());
117  }
118  return ret;
119 }
120 
121 bool setenv()
122 {
123  bool ret = true;
124  const QChar SEP = QDir::listSeparator();
125 #ifdef _DEBUG
126  const char* PLUGINS = "/pluginsd";
127  const char* CYTHON = "/cython_d";
128 #else
129  const char* PLUGINS = "/plugins";
130  const char* CYTHON = "/cython";
131 #endif
132 
133 #if defined(WIN32)
134  const char* PYTHON_DIR_NAME = "/Python312";
135  const char* PYTHON_EXECUTABLE = "/python.exe";
136  const char* PYTHON_LIBS = "/Lib";
137  const char* PYTHON_SCRIPTS = "/Scripts";
138 #else
139  const char* PYTHON_DIR_NAME = "Python3";
140  const char* PYTHON_EXECUTABLE = "/bin/python";
141  const char* PYTHON_LIBS = "/lib";
142  const char* PYTHON_SCRIPTS = "/bin";
143 #endif
144 
145  const QString TYMPAN_INSTALL_DIR = QDir::toNativeSeparators(QDir::currentPath());
146  OMessageManager::get()->info("TYMPAN_INSTALL_DIR is '%s'.\n", TYMPAN_INSTALL_DIR.toUtf8().data());
147 
148  ret |= qputenv("PYTHONIOENCODING", "UTF8");
149 
150  QString TYMPAN_INSTALL_PATH = QString(TYMPAN_INSTALL_DIR);
151  ret |= setenv("TYMPAN_INSTALL_PATH", TYMPAN_INSTALL_PATH, false);
152 
153  QString PYTHONTYMPAN = QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + QString(PYTHON_DIR_NAME));
154  ret |= setenv("PYTHONTYMPAN", PYTHONTYMPAN, false);
155  OMessageManager::get()->info("PYTHONTYMPAN is '%s'.\n", PYTHONTYMPAN.toUtf8().data());
156 
157  QString TYMPAN_SOLVERDIR = QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + QString(PLUGINS));
158  ret |= setenv("TYMPAN_SOLVERDIR", TYMPAN_SOLVERDIR, false);
159 
160  QString CGAL_BINDINGS_PATH = QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + CYTHON + QString("/CGAL"));
161  ret |= setenv("CGAL_BINDINGS_PATH", CGAL_BINDINGS_PATH, false);
162 
163  QString TYMPAN_PYTHON_INTERP = QDir::toNativeSeparators(PYTHONTYMPAN + QString(PYTHON_EXECUTABLE));
164  ret |= setenv("TYMPAN_PYTHON_INTERP", TYMPAN_PYTHON_INTERP, false);
165 
166  QString PYTHONPATH = QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + CYTHON) + SEP + PYTHONTYMPAN + SEP +
167  QDir::toNativeSeparators(PYTHONTYMPAN + "/DLLs") + SEP +
168  QDir::toNativeSeparators(PYTHONTYMPAN + PYTHON_LIBS) + SEP +
169  QDir::toNativeSeparators(PYTHONTYMPAN + PYTHON_LIBS + "/site-packages") + SEP +
170  TYMPAN_INSTALL_DIR;
171 
172  ret |= setenv("PYTHONPATH", PYTHONPATH, false);
173 
174  QString PATH = TYMPAN_INSTALL_DIR + SEP + PYTHONTYMPAN + SEP +
175  QDir::toNativeSeparators(PYTHONTYMPAN + "/DLLs") + SEP +
176  QDir::toNativeSeparators(PYTHONTYMPAN + PYTHON_LIBS) + SEP +
177  QDir::toNativeSeparators(PYTHONTYMPAN + PYTHON_SCRIPTS);
178  ret |= setenv("PATH", PATH, true);
179 
180  QString LD_LIBRARY_PATH = QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + PYTHON_LIBS) + SEP +
181  QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + CYTHON) + SEP +
182  QDir::toNativeSeparators(TYMPAN_INSTALL_DIR + CYTHON + QString("/CGAL"));
183  ret |= setenv("LD_LIBRARY_PATH", LD_LIBRARY_PATH, false);
184 
185  QString PYTHONLEGACYWINDOWSSTDIO = QString("1");
186  ret |= setenv("PYTHONLEGACYWINDOWSSTDIO", PYTHONLEGACYWINDOWSSTDIO, false);
187 
188  return ret;
189 }
190 
191 int main(int argc, char** argv)
192 {
193  QApplication::setStyle("Fusion");
194  int ret = 0;
195  bool ret_setenv = true;
196  ret_setenv = setenv();
197  if (!ret_setenv)
198  {
199  OMessageManager::get()->info("Unable to set environment variables, closing application.");
200  return ret;
201  }
202  // Register TY* classes before starting the application
204  // Appel le main de Tympan
205  ret = tyMain(argc, argv);
206  return ret;
207 }
pour l'application Tympan (fichier header)
const char * SEP
virtual void debug(const char *message,...)
Definition: logging.cpp:151
static OMessageManager * get()
Definition: logging.cpp:108
virtual void info(const char *message,...)
Definition: logging.cpp:143
Classe principale pour l'application Tympan.
Definition: TYApplication.h:46
static uint64 getConstructorCount()
Definition: TYElement.cpp:632
static uint64 getIdGenerationCount()
Definition: TYElement.cpp:641
static uint64 getDestructorCount()
Definition: TYElement.cpp:636
int main(int argc, char **argv)
Definition: main.cpp:191
bool setenv(const char *pVarEnvName, QString &pVarEnvValue, bool pForceUpdate)
Definition: main.cpp:102
void init_registry()