Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYIGNGeoProvider.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 
16 #include "TYIGNGeoProvider.h"
17 #include "TYWebSocketTransport.h"
18 #include "TYGeographicData.h"
19 
20 #include <QQmlComponent>
21 #include <QQuickItem>
22 #include <QQmlContext>
23 #include <QQuickWindow>
24 #include <QWidget>
25 
27 
28 TYIGNGeoProvider::TYIGNGeoProvider() : _geographicData(nullptr)
29 {
30  _engine = std::make_shared<QQmlApplicationEngine>();
31  _parser = std::make_shared<TYIGNLevelCurvesParser>();
32  _mocker = std::make_shared<TYIGNGeoPfMocker>();
33  _isInitialised = false;
34  _isSelectionValidated = false;
35 }
36 
38 {
39  if (singleton_ == nullptr)
40  {
42  }
43  return singleton_;
44 }
45 
47 {
48  // If the Window is not hidden, then it has been maximised or minimised and the engine must not be reset
49  QQuickWindow* window = qobject_cast<QQuickWindow*>(sender());
50  if (window && window->visibility() != QWindow::Hidden)
51  {
52  return;
53  }
54  // Disconnect any existing connections
55  // Clean up any resources related to the previous QML content
56  _engine->clearComponentCache();
57  _engine->trimComponentCache(); // Also trim the component cache
58  _engine->collectGarbage(); // Collect any garbage
59 
60  // Remove all objects from the rootObjects list
61  QList<QObject*> rootObjects = _engine->rootObjects();
62  for (QObject* obj : rootObjects)
63  {
64  // Destroy the object
65  obj->deleteLater();
66  }
67 
68  // Clear the rootObjects list
69  rootObjects.clear();
70  // Additionally, you may want to reset other variables or states
71  _isInitialised = false;
72  _isSelectionValidated = false;
73  _geographicData = nullptr; // Reset geographic data object
74 }
75 
77 {
78  Q_INIT_RESOURCE(qml);
79  // Register types to use them in QML
80  qmlRegisterType<TYWebSocketTransport>("codetympan.org", 1, 0, "WebSocketTransport");
81  qmlRegisterType<TYGeographicData>("codetympan.org", 1, 0, "GeographicData");
82  _engine->load(QUrl(QStringLiteral("qrc:/qml/TYZoneSelectPage.qml")));
83  QList<QObject*> rootObjects = _engine->rootObjects();
84  if (!rootObjects.isEmpty())
85  {
86  QObject* mainComponent = rootObjects.at(0);
87 #ifdef QT_DEBUG
88  mainComponent->setProperty("debugMode", true);
89 #else
90  mainComponent->setProperty("debugMode", false);
91 #endif
92  QObject* geographicData = mainComponent->findChild<QObject*>("geographicData");
93  if (geographicData)
94  {
95  _geographicData = geographicData;
97  QObject::connect(geographicData, SIGNAL(cancelRequested()), this, SLOT(handleCancelRequest()));
98  QObject::connect(geographicData, SIGNAL(oKRequested()), this, SLOT(handleOKRequest()));
99  }
100  }
101 
102  _isInitialised = true;
103  _isSelectionValidated = false;
104 }
105 
106 void TYIGNGeoProvider::openModalWindow(QWidget* mainWindow)
107 {
108  // Get the root QML object
109  _isSelectionValidated = false;
110  if (!_isInitialised)
111  {
112  initEngine();
113  }
114  QList<QObject*> rootObjects = _engine->rootObjects();
115  if (!rootObjects.isEmpty())
116  {
117  QObject* mainComponent = rootObjects.at(0);
118  QQuickWindow* window = qobject_cast<QQuickWindow*>(mainComponent);
119  if (window)
120  {
121  // Rattacher dynamiquement la fenêtre QML à la fenêtre principale Qt si elle existe
122  if (mainWindow)
123  {
124  QWindow* mainWinHandle = mainWindow->windowHandle();
125  if (mainWinHandle)
126  {
127  window->setTransientParent(mainWinHandle);
128  window->setScreen(mainWinHandle->screen());
129 
130  // Connecter le signal screenChanged pour suivre les déplacements entre écrans
131  connect(mainWinHandle, &QWindow::screenChanged, window,
132  [window, mainWinHandle]() { window->setScreen(mainWinHandle->screen()); });
133  }
134  }
135  connect(window, &QQuickWindow::visibilityChanged, TYIGNGeoProvider::getInstance(),
137  QRect screenGeometry = window->screen()->availableGeometry();
138  QSize windowSize = window->size();
139 
140  // Définit une taille proportionnelle à l'écran
141  int targetWidth = int(screenGeometry.width() * 0.83);
142  int targetHeight = int(screenGeometry.height() * 0.88);
143  window->resize(targetWidth, targetHeight);
144 
145  // Centre la fenêtre
146  window->setPosition(screenGeometry.center() - QPoint(targetWidth / 2, targetHeight / 2));
147  window->show();
148  }
149  }
150  _parser->reset();
151 }
152 
154 {
155  QList<QObject*> rootObjects = _engine->rootObjects();
156  if (!rootObjects.isEmpty())
157  {
158  QObject* mainComponent = rootObjects.at(0);
159  QQuickWindow* window = qobject_cast<QQuickWindow*>(mainComponent);
160  window->close();
161 
162  qDebug() << "Closing Window object.";
163  }
164 }
165 
167 {
168  QString ret{};
169  if (_geographicData)
170  {
171  ret = _geographicData->property("landtake_coordinates").toString();
172  }
173  return ret;
174 }
175 
176 void TYIGNGeoProvider::setLandtakeCoord(const QString& text)
177 {
178  if (_geographicData)
179  {
180  _geographicData->setProperty("landtake_coordinates", text);
181  }
182 }
183 
185 {
186  QString ret{};
187  if (_geographicData)
188  {
189  ret = _geographicData->property("scale_factor").toString();
190  }
191  return ret;
192 }
193 
194 void TYIGNGeoProvider::setScaleFactor(const QString& text)
195 {
196  if (_geographicData)
197  {
198  _geographicData->setProperty("scale_factor", text);
199  }
200 }
201 
203 {
204  QString ret{};
205  if (_geographicData)
206  {
207  ret = _geographicData->property("image_width").toString();
208  }
209  return ret;
210 }
211 
212 void TYIGNGeoProvider::setImageWidth(const QString& text)
213 {
214  if (_geographicData)
215  {
216  _geographicData->setProperty("image_width", text);
217  }
218 }
219 
220 bool TYIGNGeoProvider::saveImageToFile(const QString& filePath)
221 {
222  bool ret = false;
223  if (_geographicData)
224  {
225  TYGeographicData* geoData = dynamic_cast<TYGeographicData*>(_geographicData);
226  if (geoData != nullptr)
227  {
228  ret = geoData->saveImageToFile(filePath);
229  }
230  }
231  return ret;
232 }
233 
234 void TYIGNGeoProvider::buildLevelCurves(const OCoord3D& SIGCoords, const OBox& selectedZone,
235  double scaleFactor)
236 {
237  _parser->parseXmlData(SIGCoords, selectedZone, scaleFactor);
238 }
239 
240 std::vector<double> TYIGNGeoProvider::coordinatesToDouble(const QString& coord)
241 {
242  QStringList listCoords = coord.split(",");
243  const int len = listCoords.size();
244  std::vector<double> coordsDouble = std::vector<double>(len);
245  for (int i = 0; i < len; i++)
246  {
247  listCoords[i].remove("\"");
248  coordsDouble[i] = listCoords[i].toDouble();
249  }
250  return coordsDouble;
251 }
252 
254 {
255  bool ret;
256  ret = _mocker->mockGeoPf();
257  if (ret)
258  {
260  }
261  return ret;
262 }
263 
265 {
266  _geographicData->setProperty("landtake_coordinates", QString(""));
267  _geographicData->setProperty("background_img", QString(""));
268  _geographicData->setProperty("crs", QString(""));
269  _geographicData->setProperty("level_curves", QString(""));
270  _geographicData->setProperty("scale_factor", QString(""));
271  _geographicData->setProperty("image_width", QString(""));
272 }
273 
275 {
276  // Handle the cancel request here
277  // For example, emit a signal to indicate that the cancel action was triggered
278  qDebug() << "Geo project creation cancelled";
279  _engine->disconnect();
280 }
281 
283 {
285  {
286  _isSelectionValidated = true;
287  qDebug() << "Geo project creation validated";
288  _parser->buildXmlDocument(_geographicData->property("level_curves").toString());
289  _engine->disconnect();
291  }
292 }
The box class.
Definition: 3d.h:1346
The 3D coordinate class.
Definition: 3d.h:226
Q_INVOKABLE bool saveImageToFile(const QString &filePath)
This class holds a singleton instance which is an entry point for accessing IGN Geoplatform in order ...
std::shared_ptr< TYIGNLevelCurvesParser > _parser
std::shared_ptr< TYIGNGeoPfMocker > _mocker
void setImageWidth(const QString &text)
const QString getLandtakeCoord()
const QString getImageWidth()
static TYIGNGeoProvider * getInstance()
void setLandtakeCoord(const QString &text)
QObject * _geographicData
void geoProjectCreationRequested()
bool _isInitialised
True when web engine is intialized.
std::vector< double > coordinatesToDouble(const QString &coord)
void buildLevelCurves(const OCoord3D &SIGCoords, const OBox &selectedZone, double scaleFactor)
void openModalWindow(QWidget *mainWindow)
std::shared_ptr< QQmlApplicationEngine > _engine
bool _isSelectionValidated
Set to false when displaying zone selection page. When selection is validated, it is set to true in o...
bool mockGeoPf()
Mocks IGN Geoplatform web services when they are down.
void setScaleFactor(const QString &text)
bool saveImageToFile(const QString &filePath)
const QString getScaleFactor()
static TYIGNGeoProvider * singleton_