Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPolyLineEditor.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 <qcursor.h>
22 #include <QList>
23 
33 #include "TYPolyLineEditor.h"
34 
35 #define TR(id) OLocalizator::getString("TYPolyLineEditor", (id))
36 
38 {
39  _pCamEditor = new TYCameraEditor(pModeler);
40 
46 
47  _shiftOn = false;
48  _active = false;
49  _dispDist = false;
50 
51  // Taille des points differente selon le type de modeler
52  if (QString(pModeler->metaObject()->className()).compare("TYSiteModelerFrame") == 0)
53  {
54  _pointSize = 1.0f;
55  }
56  else if (QString(pModeler->metaObject()->className()).compare("TYBatimentModelerFrame") == 0)
57  {
58  _pointSize = 0.2f;
59  }
60  else if (QString(pModeler->metaObject()->className()).compare("TYMachineModelerFrame") == 0)
61  {
62  _pointSize = 0.1f;
63  }
64  else
65  {
66  _pointSize = 0.5f;
67  }
68 
69  _pOGLLineElement = NULL;
70 }
71 
73 {
74  close();
75 
76  delete _pCamEditor;
77 
78  if (_pOGLLineElement)
79  {
81  delete _pOGLLineElement;
82  _pOGLLineElement = NULL;
83  }
84 
85  if (_OGLSphereElementList.count() > 0)
86  {
87  for (QList<OGLSphereElement*>::iterator ite = _OGLSphereElementList.begin();
88  ite != _OGLSphereElementList.end(); ite++)
89  {
91  delete *ite;
92  }
93  _OGLSphereElementList.clear();
94  }
95 
96  if (_OGLLineElementList.count() > 0)
97  {
98  for (QList<OGLLineElement*>::iterator ite = _OGLLineElementList.begin();
99  ite != _OGLLineElementList.end(); ite++)
100  {
102  delete *ite;
103  }
104  _OGLLineElementList.clear();
105  }
106 }
107 
109 {
111 }
112 
114 {
117 }
118 
120 {
121  _savedPoints.clear();
122  if (_OGLSphereElementList.count() > 0)
123  {
124  for (QList<OGLSphereElement*>::iterator ite = _OGLSphereElementList.begin();
125  ite != _OGLSphereElementList.end(); ite++)
126  {
128  delete *ite;
129  }
130  _OGLSphereElementList.clear();
131  }
132 
133  if (_OGLLineElementList.count() > 0)
134  {
135  for (QList<OGLLineElement*>::iterator ite = _OGLLineElementList.begin();
136  ite != _OGLLineElementList.end(); ite++)
137  {
139  delete *ite;
140  }
141  _OGLLineElementList.clear();
142  }
143 
144  updateMovingLine(0, 0);
145  showText(false);
146 
148 }
149 
151 {
152  init();
153 }
154 
156 {
157  _savedPoints.clear();
158 }
159 
161 {
162  switch (key)
163  {
164  case Qt::Key_Return:
165  case Qt::Key_Enter:
166  if (_active)
167  {
168  // Commented until we could understand what it does
169  // int nbpoints = static_cast<int>(_savedPoints.size());
170  //
171  // //if (nbpoints >= 3)
172  //{
173  // OVector3D normale;
174  // OPoint3D* pts = new OPoint3D[nbpoints];
175 
176  // for (unsigned int i = 0; i < nbpoints; i++)
177  // {
178  // pts[i] = _savedPoints[i];
179  // }
180 
181  // OGeometrie::computeNormal(pts, nbpoints, normale);
182 
183  // OVector3D viewnormale;
184  // OGLCamera* camera = _pInteractor->getRenderer()->getActiveCamera();
185  // if (!camera->fromTo.sameDirection(NxVec3(normale._x, normale._y, normale._z)))
186  // {
187  // _savedPoints.clear();
188 
189  // for (long i = (nbpoints - 1); i >= 0; i--)
190  // {
191  // _savedPoints.push_back(pts[i]);
192  // }
193  // }
194 
195  // delete [] pts;
196 
197  //}
198 
199  // Set all z values to 0
200  for (size_t i = 0; i < _savedPoints.size(); i++)
201  {
202  _savedPoints[i]._z = 0.0;
203  }
204 
205  emit(endedSavingPoints());
206 
207  init();
208 
210  }
211  break;
212  case Qt::Key_Shift:
214  {
215  _pCamEditor->connect();
216  }
217  _shiftOn = true;
218  break;
219  case Qt::Key_Escape:
220  if (_active)
221  {
222  cancel();
223  }
224  break;
225  case Qt::Key_Delete:
226  if (_active)
227  {
228  removeLastPoint();
229  }
230  break;
231  }
232 }
233 
235 {
236  if (key == Qt::Key_Shift)
237  {
239  {
241  }
242  _shiftOn = false;
243  }
244 }
245 
247 {
248  if (view == TYModelerFrame::TopView)
249  {
251  _active = true;
252  }
253  else
254  {
255  _pCamEditor->connect();
256  _active = false;
257 
258  showText(false);
260  }
261 
262  // On relaie le signal au CameraEditor
264 }
265 
266 void TYPolyLineEditor::slotMouseMoved(int x, int y, Qt::MouseButtons button, Qt::KeyboardModifiers state)
267 {
268  updateMovingLine(x, y);
269 }
270 
271 void TYPolyLineEditor::slotMouseReleased(int x, int y, Qt::MouseButton button, Qt::KeyboardModifiers state)
272 {
273  if (_active && !_shiftOn)
274  {
275  if (button == Qt::LeftButton)
276  {
277  addPoint(x, y);
278  }
279  else if ((button == Qt::RightButton) && (_savedPoints.size() > 0))
280  {
281  removeLastPoint();
282  }
283  }
284 }
285 
286 void TYPolyLineEditor::addPoint(int x, int y)
287 {
288  NxVec3 pt = OGLCamera::displayToWorld(NxVec3(x, _pInteractor->height() - y, 0.1));
289  TYPoint pt2(pt.x, -pt.z, pt.y);
290  validatePoint(pt2);
291 
292  // Changement de repere Disp->World
293  if (_savedPoints.size() >= 1)
294  {
297  OColor oLineColor;
298  oLineColor.r = 1.0;
299  oLineColor.g = 0.0;
300  oLineColor.b = 0.0;
301  _pOGLLineElement->setColor(oLineColor);
302  _pOGLLineElement->setIs3D(true);
303  _pOGLLineElement->setPoint1(_OGLSphereElementList.last()->getCenter());
304  _pOGLLineElement->setPoint2(OPoint3D(pt2._x, pt2._z, -pt2._y));
307  }
308 
309  // Ajout du nouveau point a la collection
310  _savedPoints.push_back(pt2);
311 
312  OGLSphereElement* _pOGLSphereElement = new OGLSphereElement();
313  _pOGLSphereElement->setCenter(OPoint3D(pt2._x, pt2._z, -pt2._y));
314  _pOGLSphereElement->setRadius(_pointSize);
315  _pOGLSphereElement->setIs3D(true);
316  _pOGLSphereElement->setColor(OColor(1.0, 0.0, 0.0));
317  _pOGLSphereElement->setVisibility(true);
318  _pInteractor->getRenderer()->addOGLElement(_pOGLSphereElement);
319  _OGLSphereElementList.append(_pOGLSphereElement);
320 
322 }
323 
325 {
326  bool res = true;
327 
328  // Suppression du point precedent
329  _savedPoints.pop_back();
330 
331  if (!_savedPoints.size())
332  {
333  res = false;
334  }
335 
336  if (_OGLSphereElementList.count() == 0)
337  {
338  return false;
339  }
340  else
341  {
343  delete _OGLSphereElementList.last();
344  _OGLSphereElementList.removeLast();
345 
346  if (_OGLLineElementList.count() == 0)
347  {
348  return false;
349  }
350  else
351  {
353  delete _OGLLineElementList.last();
354  _OGLLineElementList.removeLast();
355  if (!updateMovingLine(0, 0))
356  {
358  }
359  }
360  }
361 
362  return res;
363 }
364 
366 {
367  if (!_active)
368  {
369  return false;
370  }
371  if (_OGLSphereElementList.count() == 0)
372  {
373  if (_pOGLLineElement)
374  {
376  }
377 
378  showText(false);
379 
380  return false;
381  }
382 
383  QPoint pos = _pInteractor->mapFromGlobal(QCursor::pos());
384 
385  // Changement de repere Disp->World
386  // On recupere le pt precedent
387  TYPoint pt1 = _savedPoints.back();
388 
389  NxVec3 pt;
390  if ((x == 0) && (y == 0))
391  {
392  pt = OGLCamera::displayToWorld(NxVec3(pos.x(), _pInteractor->height() - pos.y(), 0.1));
393  }
394  else
395  {
396  pt = OGLCamera::displayToWorld(NxVec3(x, _pInteractor->height() - y, 0.1));
397  }
398  TYPoint pt2(pt.x, -pt.z, pt.y);
399  validatePoint(pt2);
400 
401  // Init des objets graphiques
402  if (!_pOGLLineElement)
403  {
406  _pOGLLineElement->setIs3D(true);
407  OColor oLineColor;
408  oLineColor.r = 1.0;
409  oLineColor.g = 0.0;
410  oLineColor.b = 0.0;
411  _pOGLLineElement->setColor(oLineColor);
412 
414  }
415 
416  // On trace une ligne avec les 2 pts
418  _pOGLLineElement->setPoint1(OPoint3D(pt1._x, pt1._z, -pt1._y));
419  _pOGLLineElement->setPoint2(OPoint3D(pt2._x, pt2._z, -pt2._y));
420 
421  if (_dispDist)
422  {
423  // Affichage de la distance entre les 2 points
424  double dist = OVector3D(pt1, pt2).norme();
425  QString msg = QString(TR("id_output_dist")).arg(dist, 0, 'f', 2);
426 
427  double textPos[4];
429  (_pOGLLineElement->getPoint2()[0] - _pOGLLineElement->getPoint1()[0]) / 2.0),
430  (_pOGLLineElement->getPoint2()[1] +
431  (_pOGLLineElement->getPoint1()[1] - _pOGLLineElement->getPoint2()[1]) / 2.0),
432  (_pOGLLineElement->getPoint2()[2] +
433  (_pOGLLineElement->getPoint1()[2] - _pOGLLineElement->getPoint2()[2]) / 2.0),
434  textPos);
435  updateText(msg, (int)textPos[0], (int)textPos[1]);
436  }
437 
438  // Redraw
440 
441  return true;
442 }
443 
445 {
446  // Si la grille magnetique est activee
448  {
449  snapToGrid(pt._x, pt._y, pt._z, _gridMagnStep);
450  }
451 }
All base classes related to 3D manipulation.
gestion de l'edition de la camera (fichier header)
Classe generique pour une fenetre de modeleur (fichier header)
#define TR(id)
gestion de l'edition d'une polyligne (fichier header)
Le role de cette classe est limite a emettre des signaux pouvant etre utilise pour interagir sur le r...
Definition: NxVec3.h:23
NxReal z
Definition: NxVec3.h:80
NxReal y
Definition: NxVec3.h:80
NxReal x
Definition: NxVec3.h:80
Definition: color.h:31
float b
Definition: color.h:33
float r
Definition: color.h:33
float g
Definition: color.h:33
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
static NxVec3 displayToWorld(NxVec3 display)
Definition: OGLCamera.cpp:699
void setVisibility(bool bVisible)
Definition: OGLElement.h:51
void setIs3D(bool bIs3D)
Definition: OGLElement.h:59
double * getPoint2()
void setPoint1(const OPoint3D &point1)
void setColor(const OColor &oColor)
double * getPoint1()
void setPoint2(const OPoint3D &point2)
void setColor(const OColor &oColor)
void setRadius(float radius)
void setCenter(const OPoint3D &center)
The 3D point class.
Definition: 3d.h:487
The 3D vector class.
Definition: 3d.h:298
double norme() const
Computes the length of this vector.
Definition: 3d.cpp:215
Classe abstraite pour la gestion de l'interaction entre la vue graphique (2D ou 3D) et le clavier et ...
double _gridMagnStep
Pas de la grille magnétique de positionnement.
void worldToDisplay(double x, double y, double z, double *displayPoint)
Methode utilitaire pour convertir un point en coordonnees globale en coordonnees ecran.
static void snapToGrid(float &x, float &y, float &z, float &gridMagnStep)
Methode utilitaire qui adapte les coordonnees d'un point pour que celui-ci soit aligne avec la grille...
virtual void disconnect()
Deconnecte cet editor a l'interactor associe.
void updateText(QString msg="", int posX=0, int posY=0, bool show=true)
Mets a jour le texte informatif sur la vue 3D.
TYModelerFrame * _pModeler
Le modeler associe a cet editor.
virtual void connect()
Connecte cet editor a l'interactor associe.
void showText(bool show=true)
Affiche ou pas le texte informatif sur la vue 3D.
TYRenderWindowInteractor * _pInteractor
La vue graphique associee a cet editor.
Gestion de l'edition en mode camera.
void setLeftButtonFunction2D(void(TYCameraEditor::*function)())
void setMiddleButtonFunction2D(void(TYCameraEditor::*function)())
void setRightButtonFunction2D(void(TYCameraEditor::*function)())
void setShiftRightButtonFunction2D(void(TYCameraEditor::*function)())
void setShiftLeftButtonFunction2D(void(TYCameraEditor::*function)())
virtual void slotViewTypeChanged(int view)
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:915
Classe generique pour une fenetre de modeleur.
bool getSnapGridActive()
void addOGLElement(OGLElement *pOGLElement)
void removeOGLElement(OGLElement *pOGLElement)
virtual void connect()
void endedSavingPoints()
virtual void cancel()
QList< OGLLineElement * > _OGLLineElementList
Lignes dans la vue 3D.
TYCameraEditor * _pCamEditor
Un CameraEditor associe a cet editor.
void addPoint(int x, int y)
QList< OGLSphereElement * > _OGLSphereElementList
Table de points openGL.
bool updateMovingLine(int x, int y)
bool _dispDist
Indique si l'information de distance doit etre affichee ou non.
TYTabPoint _savedPoints
Tableau pour la sauvegarde des points du mode Save points.
void validatePoint(TYPoint &pt)
TYPolyLineEditor(TYModelerFrame *pModeler)
virtual void init()
virtual void disconnect()
virtual void slotMouseMoved(int x, int y, Qt::MouseButtons button, Qt::KeyboardModifiers state)
virtual void slotKeyPressed(int key)
float _pointSize
Taille pour la representation des points.
virtual void slotViewTypeChanged(int view)
bool _shiftOn
Indique si le bouton Shift est enfonce.
bool _active
Indique si cet editor est actif.
virtual void close()
virtual void slotKeyReleased(int key)
OGLLineElement * _pOGLLineElement
La ligne courante.
virtual void slotMouseReleased(int x, int y, Qt::MouseButton button, Qt::KeyboardModifiers state)
virtual void updateGL()
TYOpenGLRenderer * getRenderer()