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>
28 
32 
33 namespace armarx
34 {
36  {
37  widget.setupUi(getWidget());
38  updateTimer = new QTimer(this);
39  }
40 
42  {
43  }
44 
45  void
47  {
48  }
49 
50  void
52  {
53  }
54 
55  void
57  {
58  if (Application::getInstance()->getProperty<bool>("UseTimeServer").getValue())
59  {
60  usingProxy(GLOBAL_TIMESERVER_NAME);
61  }
62  }
63 
64  void
66  {
67  connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateGui()));
68  connect(widget.btnStart, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
69  connect(widget.btnStop, SIGNAL(clicked()), this, SLOT(stopButtonPressed()));
70  connect(widget.btnStep, SIGNAL(clicked()), this, SLOT(stepButtonPressed()));
71  connect(
72  widget.spinBoxSpeed, SIGNAL(valueChanged(double)), this, SLOT(speedChanged(double)));
73 
74  iteration = 0;
75  lastChangeSystemTime = IceUtil::Time::now();
76  lastChangeTime = TimeUtil::GetTime();
77 
78  updateTimer->start(53);
79  timeServerPtr = TimeUtil::GetTimeServer();
80 
81  if (timeServerPtr)
82  {
83  widget.btnStart->setEnabled(true);
84  widget.btnStop->setEnabled(true);
85  widget.btnStep->setEnabled(true);
86  widget.spinBoxSpeed->setEnabled(true);
87  widget.spinBoxStepCount->setEnabled(true);
88 
89  try
90  {
91  // sometimes the Timeserver is not fully connected at this time and this may fail
92  widget.spinBoxSpeed->setValue(timeServerPtr->getSpeed());
93  }
94  catch (IceUtil::NullHandleException&)
95  {
96  widget.spinBoxSpeed->setValue(1);
97  }
98  }
99  }
100 
101  void
103  {
104  timeServerPtr = NULL;
105  widget.btnStart->setEnabled(false);
106  widget.btnStop->setEnabled(false);
107  widget.btnStep->setEnabled(false);
108  widget.spinBoxSpeed->setEnabled(false);
109  widget.spinBoxStepCount->setEnabled(false);
110  }
111 
112  void
114  {
116  QString timeStr;
117  bool useTimeserver =
118  Application::getInstance()->getProperty<bool>("UseTimeServer").getValue();
119 
120  if (useTimeserver)
121  {
122  // calculate actual speed factor
123  if (iteration % 10 == 0) // ~2x per second
124  {
125  auto timeDiffVirtual = time - lastChangeTime;
126  auto now = IceUtil::Time::now();
127  auto timeDiffSystem = now - lastChangeSystemTime;
128  if (timeDiffSystem.toMilliSeconds() > 0 &&
129  timeDiffVirtual.toMilliSeconds() >=
130  0) // timeDiffVirtual < 0 on timeserver restart
131  {
132  float actualFactor =
133  float(timeDiffVirtual.toMilliSeconds()) / timeDiffSystem.toMilliSeconds();
134  widget.lblActualSpeed->setText(QString("(%1)").arg(actualFactor, 0, 'f', 2));
135  }
136  if (timeDiffVirtual.toMilliSeconds() != 0)
137  {
138  lastChangeSystemTime = now;
139  lastChangeTime = time;
140  }
141  }
142 
143  // update gui elements if values like speed where changed remotely
144  if (timeServerPtr && iteration % 20 == 0) // ~1x per second
145  {
146  try
147  {
148  widget.lblSteps->setText(QString("x %1ms").arg(timeServerPtr->getStepTimeMS()));
149  if (!widget.spinBoxSpeed->hasFocus())
150  {
151  widget.spinBoxSpeed->setValue(timeServerPtr->getSpeed());
152  }
153  }
154  catch (Ice::NotRegisteredException&)
155  {
156  return;
157  }
158  catch (IceUtil::NullHandleException&)
159  {
160  return;
161  }
162  catch (Ice::ConnectionRefusedException&)
163  {
164  return;
165  }
166  }
167  }
168 
169  if (useTimeserver)
170  {
171  timeStr = QString::fromStdString(time.toDuration());
172  }
173  else
174  {
175  timeStr = QString::fromStdString(time.toDateTime());
176  }
177 
178  widget.lblTime->setText(timeStr);
179 
180  iteration++;
181  }
182 
183  void
185  {
186  if (timeServerPtr)
187  {
188  timeServerPtr->start();
189  }
190  }
191 
192  void
194  {
195  if (timeServerPtr)
196  {
197  for (int i = 0; i < widget.spinBoxStepCount->value(); ++i)
198  {
199  timeServerPtr->step();
200  }
201  }
202  }
203 
204  void
206  {
207  if (timeServerPtr)
208  {
209  timeServerPtr->stop();
210  }
211  }
212 
213  void
215  {
216  if (timeServerPtr)
217  {
218  timeServerPtr->setSpeed(newSpeed);
219  }
220  }
221 } // namespace armarx
armarx::ClockWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Definition: ClockWidgetController.cpp:46
armarx::ClockWidgetController::updateGui
void updateGui()
Definition: ClockWidgetController.cpp:113
ClockWidgetController.h
armarx::ClockWidgetController::~ClockWidgetController
~ClockWidgetController() override
Controller destructor.
Definition: ClockWidgetController.cpp:41
armarx::ClockWidgetController::onDisconnectComponent
void onDisconnectComponent() override
Definition: ClockWidgetController.cpp:102
armarx::TimeUtil::GetTimeServer
static LocalTimeServerPtr GetTimeServer()
Definition: TimeUtil.cpp:118
armarx::ClockWidgetController::startButtonPressed
void startButtonPressed()
Definition: ClockWidgetController.cpp:184
LocalTimeServer.h
armarx::ClockWidgetController::onConnectComponent
void onConnectComponent() override
Definition: ClockWidgetController.cpp:65
armarx::ClockWidgetController::stopButtonPressed
void stopButtonPressed()
Definition: ClockWidgetController.cpp:205
armarx::ClockWidgetController::onInitComponent
void onInitComponent() override
Definition: ClockWidgetController.cpp:56
armarx::ClockWidgetController::ClockWidgetController
ClockWidgetController()
Controller Constructor.
Definition: ClockWidgetController.cpp:35
armarx::ClockWidgetController::stepButtonPressed
void stepButtonPressed()
Definition: ClockWidgetController.cpp:193
armarx::Application::getInstance
static ApplicationPtr getInstance()
Retrieve shared pointer to the application object.
Definition: Application.cpp:315
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:214
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:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
Application.h
armarx::ClockWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: ClockWidgetController.cpp:51