renderworldwrapperwidget.cpp
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2014 *
4  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
5  * Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it> *
6  * Gianluca Massera <emmegian@yahoo.it> *
7  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the Free Software *
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
22  ********************************************************************************/
23 
24 #include "renderworldwrapperwidget.h"
25 #include "renderworld.h"
26 #include "logger.h"
27 #include <QVBoxLayout>
28 
29 using namespace qglviewer;
30 
31 // All the suff below is to avoid warnings on Windows about the use of unsafe
32 // functions. This should be only a temporary workaround, the solution is stop
33 // using C string and file functions...
34 #if defined(_MSC_VER)
35  #pragma warning(push)
36  #pragma warning(disable:4996)
37 #endif
38 
39 namespace farsa {
40 
41 namespace {
42  // A custom QEvent to force update of RenderWorldWrapperWidget
43  class ForceRenderWorldUpdateEvent : public QEvent
44  {
45  public:
46  ForceRenderWorldUpdateEvent() :
47  QEvent(QEvent::User)
48  {
49  }
50 
51  virtual ~ForceRenderWorldUpdateEvent()
52  {
53  }
54  };
55 }
56 
57 RenderWorldWrapperWidget::RenderWorldWrapperWidget(QWidget* parent, Qt::WindowFlags flags) :
58  QWidget(parent, flags),
60  m_renderWorld(new RenderWorld(this, "world")),
61  m_layout(new QVBoxLayout(this)),
62  m_renderWorldStateRestored(false),
63  m_setCameraToLookAtRobot(false),
64  m_robotTm(wMatrix::identity())
65 {
66  // Setting some window property and creating the layout that will contain renderworld
67  setWindowTitle("3D World");
68  m_layout->setContentsMargins(0, 0, 0, 0);
69  m_layout->addWidget(m_renderWorld);
70  const QString stateFileName = ".evolver.xml";
71  m_renderWorld->setStateFileName(stateFileName);
72  if (QFile::exists(stateFileName)) {
75  }
76 
77  // Declaring which resources we will need to use
78  usableResources(QStringList() << "robot");
79 
80  // timer used if selfUpdate is on
81  m_selfUpdateTimer = new QTimer(this);
82  // 40 ms correspond to 25 frame per seconds
83  m_selfUpdateTimer->setInterval( 40 );
84  m_selfUpdateTimer->setSingleShot( false );
85  connect( m_selfUpdateTimer, SIGNAL(timeout()), this, SLOT(updateRenderWorld()) );
86 }
87 
89 {
90  m_selfUpdateTimer->stop();
91 }
92 
94 {
95  // First of all calling parent function
97 
98  // Now sharing with RenderWorld
100 }
101 
103 {
105  lookAtRobot();
106 
107  m_setCameraToLookAtRobot = false;
108  // Setting this to true so that the next time the robot changes we don't move camera
110  }
111 
112  m_renderWorld->update();
113 }
114 
116  if ( on ) {
117  m_selfUpdateTimer->start();
118  } else {
119  m_selfUpdateTimer->stop();
120  }
121 }
122 
124  return m_selfUpdateTimer->isActive();
125 }
126 
127 void RenderWorldWrapperWidget::resourceChanged(QString resourceName, ResourceChangeType changeType)
128 {
129  if (resourceName == "robot") {
130  if ((changeType != Deleted) && (!m_renderWorldStateRestored)) {
131  try {
132  m_robotTm = getResource<WObject>()->matrix();
133  } catch (const ResourceTypeMismatchException&) {
134  }
135 
137  }
138  } else {
139  Logger::info("Unknown resource " + resourceName + " for RenderWorldWrapperWidget widget");
140  }
141 
142  // Forcing renderworld update
143  QCoreApplication::postEvent(this, new ForceRenderWorldUpdateEvent());
144 }
145 
147 {
148  if (event->type() == QEvent::User) {
149  // Forcing RenderWorld update
151  }
152 }
153 
155 {
156  wVector cameraDefaultPosition = m_robotTm.transformVector(wVector(-0.8f, 0.0f, +0.6f));
157  m_renderWorld->camera()->setPosition(Vec(cameraDefaultPosition[0], cameraDefaultPosition[1], cameraDefaultPosition[2]));
158  m_renderWorld->camera()->setUpVector(Vec(0.0f, 0.0f, 1.0f));
160 }
161 
162 } //end namespace farsa
163 
164 // All the suff below is to restore the warning state on Windows
165 #if defined(_MSC_VER)
166  #pragma warning(pop)
167 #endif