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