ClockWidgetController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * \package ArmarXGui::gui-plugins::ClockWidgetController
19  * \author Clemens Wallrath ( uagzs at student dot kit dot edu )
20  * \date 2015
21  * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "ClockWidgetController.h"
26 
27 #include <string>
31 
32 
33 namespace armarx
34 {
36  {
37  widget.setupUi(getWidget());
38  updateTimer = new QTimer(this);
39  }
40 
41 
43  {
44 
45  }
46 
47 
48  void ClockWidgetController::loadSettings(QSettings* settings)
49  {
50 
51  }
52 
53  void ClockWidgetController::saveSettings(QSettings* settings)
54  {
55 
56  }
57 
58 
60  {
61  if (Application::getInstance()->getProperty<bool>("UseTimeServer").getValue())
62  {
63  usingProxy(GLOBAL_TIMESERVER_NAME);
64  }
65  }
66 
67 
69  {
70  connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateGui()));
71  connect(widget.btnStart, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
72  connect(widget.btnStop, SIGNAL(clicked()), this, SLOT(stopButtonPressed()));
73  connect(widget.btnStep, SIGNAL(clicked()), this, SLOT(stepButtonPressed()));
74  connect(widget.spinBoxSpeed, SIGNAL(valueChanged(double)), this, SLOT(speedChanged(double)));
75 
76  iteration = 0;
77  lastChangeSystemTime = IceUtil::Time::now();
78  lastChangeTime = TimeUtil::GetTime();
79 
80  updateTimer->start(53);
81  timeServerPtr = TimeUtil::GetTimeServer();
82 
83  if (timeServerPtr)
84  {
85  widget.btnStart->setEnabled(true);
86  widget.btnStop->setEnabled(true);
87  widget.btnStep->setEnabled(true);
88  widget.spinBoxSpeed->setEnabled(true);
89  widget.spinBoxStepCount->setEnabled(true);
90 
91  try
92  {
93  // sometimes the Timeserver is not fully connected at this time and this may fail
94  widget.spinBoxSpeed->setValue(timeServerPtr->getSpeed());
95  }
96  catch (IceUtil::NullHandleException&)
97  {
98  widget.spinBoxSpeed->setValue(1);
99  }
100  }
101  }
102 
104  {
105  timeServerPtr = NULL;
106  widget.btnStart->setEnabled(false);
107  widget.btnStop->setEnabled(false);
108  widget.btnStep->setEnabled(false);
109  widget.spinBoxSpeed->setEnabled(false);
110  widget.spinBoxStepCount->setEnabled(false);
111  }
112 
114  {
116  QString timeStr;
117  bool useTimeserver = Application::getInstance()->getProperty<bool>("UseTimeServer").getValue();
118 
119  if (useTimeserver)
120  {
121  // calculate actual speed factor
122  if (iteration % 10 == 0) // ~2x per second
123  {
124  auto timeDiffVirtual = time - lastChangeTime;
125  auto now = IceUtil::Time::now();
126  auto timeDiffSystem = now - lastChangeSystemTime;
127  if (timeDiffSystem.toMilliSeconds() > 0 && timeDiffVirtual.toMilliSeconds() >= 0) // timeDiffVirtual < 0 on timeserver restart
128  {
129  float actualFactor = float(timeDiffVirtual.toMilliSeconds()) / timeDiffSystem.toMilliSeconds();
130  widget.lblActualSpeed->setText(QString("(%1)").arg(actualFactor, 0, 'f', 2));
131  }
132  if (timeDiffVirtual.toMilliSeconds() != 0)
133  {
134  lastChangeSystemTime = now;
135  lastChangeTime = time;
136  }
137  }
138 
139  // update gui elements if values like speed where changed remotely
140  if (timeServerPtr && iteration % 20 == 0) // ~1x per second
141  {
142  try
143  {
144  widget.lblSteps->setText(QString("x %1ms").arg(timeServerPtr->getStepTimeMS()));
145  if (!widget.spinBoxSpeed->hasFocus())
146  {
147  widget.spinBoxSpeed->setValue(timeServerPtr->getSpeed());
148  }
149  }
150  catch (Ice::NotRegisteredException&)
151  {
152  return;
153  }
154  catch (IceUtil::NullHandleException&)
155  {
156  return;
157  }
158  catch (Ice::ConnectionRefusedException&)
159  {
160  return;
161  }
162 
163  }
164  }
165 
166  if (useTimeserver)
167  {
168  timeStr = QString::fromStdString(time.toDuration());
169  }
170  else
171  {
172  timeStr = QString::fromStdString(time.toDateTime());
173  }
174 
175  widget.lblTime->setText(timeStr);
176 
177  iteration++;
178  }
179 
181  {
182  if (timeServerPtr)
183  {
184  timeServerPtr->start();
185  }
186  }
187 
189  {
190  if (timeServerPtr)
191  {
192  for (int i = 0; i < widget.spinBoxStepCount->value() ; ++i)
193  {
194  timeServerPtr->step();
195  }
196  }
197  }
198 
200  {
201  if (timeServerPtr)
202  {
203  timeServerPtr->stop();
204  }
205  }
206 
208  {
209  if (timeServerPtr)
210  {
211  timeServerPtr->setSpeed(newSpeed);
212  }
213  }
214 }
armarx::ClockWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Definition: ClockWidgetController.cpp:48
armarx::ClockWidgetController::updateGui
void updateGui()
Definition: ClockWidgetController.cpp:113
ClockWidgetController.h
armarx::ClockWidgetController::~ClockWidgetController
~ClockWidgetController() override
Controller destructor.
Definition: ClockWidgetController.cpp:42
armarx::ClockWidgetController::onDisconnectComponent
void onDisconnectComponent() override
Definition: ClockWidgetController.cpp:103
armarx::TimeUtil::GetTimeServer
static LocalTimeServerPtr GetTimeServer()
Definition: TimeUtil.cpp:109
armarx::ClockWidgetController::startButtonPressed
void startButtonPressed()
Definition: ClockWidgetController.cpp:180
LocalTimeServer.h
armarx::ClockWidgetController::onConnectComponent
void onConnectComponent() override
Definition: ClockWidgetController.cpp:68
armarx::ClockWidgetController::stopButtonPressed
void stopButtonPressed()
Definition: ClockWidgetController.cpp:199
armarx::ClockWidgetController::onInitComponent
void onInitComponent() override
Definition: ClockWidgetController.cpp:59
armarx::ClockWidgetController::ClockWidgetController
ClockWidgetController()
Controller Constructor.
Definition: ClockWidgetController.cpp:35
armarx::ClockWidgetController::stepButtonPressed
void stepButtonPressed()
Definition: ClockWidgetController.cpp:188
armarx::Application::getInstance
static ApplicationPtr getInstance()
Retrieve shared pointer to the application object.
Definition: Application.cpp:289
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::ClockWidgetController::speedChanged
void speedChanged(double newSpeed)
Definition: ClockWidgetController.cpp:207
TimeUtil.h
float
#define float
Definition: 16_Level.h:22
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
Application.h
armarx::ClockWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: ClockWidgetController.cpp:53