Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxStatusBar.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxStatusBar.h"
34 
35 #include <QLabel>
36 #include <QString>
37 #include <QHBoxLayout>
38 #include <QAction>
39 #include <QToolButton>
40 #include <QPixmap>
41 #include <QMetaObject>
42 
43 #include "cxTrackingService.h"
44 
45 #include "cxTrackingService.h"
46 #include "cxVideoService.h"
47 #include "boost/bind.hpp"
48 #include <QMetaMethod>
49 #include "libQtSignalAdapters/Qt2Func.h"
50 #include "libQtSignalAdapters/ConnectionFactories.h"
51 #include "cxManualTool.h"
52 #include "cxTypeConversions.h"
53 #include "cxDefinitionStrings.h"
54 #include "cxActiveToolProxy.h"
55 #include "cxLogicManager.h"
56 #include "cxViewService.h"
57 
58 #include "cxLogMessageFilter.h"
59 #include "cxMessageListener.h"
60 #include "cxVLCRecorder.h"
61 
62 
63 namespace cx
64 {
66  mRenderingFpsLabel(new QLabel(this)),
67  mGrabbingInfoLabel(new QLabel(this)),
68  mRecordFullscreenLabel(new QLabel(this)),
69  mTpsLabel(new QLabel(this))
70 {
71  mMessageListener = MessageListener::create();
72  mMessageListener->installFilter(MessageFilterStatusBar::create());
73  connect(mMessageListener.get(), &MessageListener::newMessage, this, &StatusBar::showMessageSlot);
74 
75  connect(trackingService().get(), &TrackingService::stateChanged, this, &StatusBar::resetToolManagerConnection);
76 
78  mActiveTool = ActiveToolProxy::New(ts);
79  connect(mActiveTool.get(), &ActiveToolProxy::tps, this, &StatusBar::tpsSlot);
80 
81  connect(trackingService().get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(updateToolButtons()));
82 
83  connect(viewService().get(), SIGNAL(fps(int)), this, SLOT(renderingFpsSlot(int)));
84 
85  connect(videoService().get(), SIGNAL(fps(int)), this, SLOT(grabbingFpsSlot(int)));
86  connect(videoService().get(), SIGNAL(connected(bool)), this, SLOT(grabberConnectedSlot(bool)));
87 
88  connect(vlc(), &VLCRecorder::stateChanged, this, &StatusBar::onRecordFullscreenChanged);
89 
90 // this->addPermanentWidget(mMessageLevelLabel);
91  this->addPermanentWidget(mRenderingFpsLabel);
92 }
93 
95 {
96 }
97 
98 void StatusBar::resetToolManagerConnection()
99 {
100  this->disconnectFromToolSignals();
101  if (trackingService()->getState()>=Tool::tsCONFIGURED)
102  this->connectToToolSignals();
103  this->updateToolButtons();
104 }
105 
106 void StatusBar::connectToToolSignals()
107 {
108  this->disconnectFromToolSignals(); // avoid duplicates
109 
110  this->addPermanentWidget(mTpsLabel);
111  mTpsLabel->show();
112 
113  TrackingService::ToolMap tools = trackingService()->getTools();
114  for (TrackingService::ToolMap::iterator it = tools.begin(); it != tools.end(); ++it)
115  {
116  ToolPtr tool = it->second;
117  if (tool->hasType(Tool::TOOL_MANUAL))
118  continue;
119  if (tool == trackingService()->getManualTool())
120  continue;
121  connect(tool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
122 
123  ToolData current;
124  current.mTool = tool;
125  current.mAction.reset(new QAction(tool->getName(), NULL));
126  current.mAction->setToolTip("Press to set active");
127 
128  QtSignalAdapters::connect0<void()>(
129  current.mAction.get(),
130  SIGNAL(triggered()),
131  boost::bind(&StatusBar::activateTool, this, tool->getUid()));
132 
133  current.mButton.reset(new QToolButton);
134  current.mButton->setDefaultAction(current.mAction.get());
135  this->addPermanentWidget(current.mButton.get());
136  mToolData.push_back(current);
137  }
138 
139  this->updateToolButtons();
140 }
141 
142 void StatusBar::disconnectFromToolSignals()
143 {
144  this->removeWidget(mTpsLabel);
145 
146  for (unsigned i = 0; i < mToolData.size(); ++i)
147  {
148  ToolData current = mToolData[i];
149 
150  disconnect(current.mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
151  this->removeWidget(current.mButton.get());
152  }
153  mToolData.clear();
154 }
155 
156 
157 void StatusBar::activateTool(QString uid)
158 {
159  trackingService()->setActiveTool(uid);
160 }
161 
162 void StatusBar::updateToolButtons()
163 {
164  ToolPtr activeTool = trackingService()->getActiveTool();
165 
166  for (unsigned i = 0; i < mToolData.size(); ++i)
167  {
168  ToolData current = mToolData[i];
169  ToolPtr tool = current.mTool;
170  QString color = this->getToolStyle(tool->getVisible(), tool->isInitialized(), activeTool == tool);
171  current.mButton->setStyleSheet(QString("QToolButton { %1; }").arg(color));
172 
173  if (!tool->isInitialized())
174  current.mAction->setToolTip("Tool is not Initialized");
175  else if (activeTool == tool)
176  current.mAction->setToolTip("Active Tool");
177  else if (tool->getVisible())
178  current.mAction->setToolTip("Tool not visible/not tracking");
179  else
180  current.mAction->setToolTip("Tool visible. Press to set as active");
181  }
182 }
183 
184 QString StatusBar::getToolStyle(bool visible, bool initialized, bool active)
185 {
186  if (!initialized)
187  return QString("background-color: silver");
188 
189  if (visible)
190  {
191  if (active)
192  return QString("background-color: lime");
193  else
194  return QString("background-color: green");
195  }
196 
197  return QString("background-color: orangered");
198 }
199 
200 void StatusBar::renderingFpsSlot(int numFps)
201 {
202  QString fpsString = "FPS: " + QString::number(numFps);
203  mRenderingFpsLabel->setText(fpsString);
204 }
205 
206 void StatusBar::tpsSlot(int numTps)
207 {
208  QString tpsString = "TPS: " + QString::number(numTps);
209  mTpsLabel->setText(tpsString);
210 }
211 
212 void StatusBar::grabbingFpsSlot(int numFps)
213 {
214  QString infoString = "VideoConnection-FPS: " + QString::number(numFps);
215  mGrabbingInfoLabel->setText(infoString);
216 }
217 
218 void StatusBar::grabberConnectedSlot(bool connected)
219 {
220  if (connected)
221  {
222  this->addPermanentWidget(mGrabbingInfoLabel);
223  mGrabbingInfoLabel->show();
224  }
225  else
226  this->removeWidget(mGrabbingInfoLabel);
227 }
228 
229 void StatusBar::onRecordFullscreenChanged()
230 {
231  QLabel* label = mRecordFullscreenLabel;
232 
233  if (vlc()->isRecording())
234  {
235  label->setMargin(0);
236  int size = this->height()*0.75; // fit within statusbar
237  QPixmap map;
238  map.load(":/icons/Video-icon_green.png");
239  label->setPixmap(map.scaled(size, size, Qt::KeepAspectRatio));
240 
241  this->addPermanentWidget(mRecordFullscreenLabel);
242  mRecordFullscreenLabel->show();
243  }
244  else
245  {
246  this->removeWidget(mRecordFullscreenLabel);
247  }
248 }
249 
250 void StatusBar::showMessageSlot(Message message)
251 {
252  QString text = QString("[%1] %4")
253  .arg(qstring_cast(message.getMessageLevel()))
254  .arg(message.getText());
255 
256  this->showMessage(text, message.getTimeout());
257 }
258 
259 }//namespace cx
QString qstring_cast(const T &val)
static MessageListenerPtr create(LogPtr log=LogPtr())
boost::shared_ptr< class TrackingService > TrackingServicePtr
TrackingServicePtr getTrackingService()
QString getText() const
The raw message.
static MessageFilterStatusBarPtr create()
Definition: cxStatusBar.h:62
int getTimeout() const
Timout tells the statusbar how long it should be displayed, this depends on the message level...
configured with basic info
Definition: cxTool.h:96
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
Representation of a mouse/keyboard-controlled virtual tool.
Definition: cxTool.h:106
LogicManager * logicManager()
std::map< QString, ToolPtr > ToolMap
cxLogicManager_EXPORT ViewServicePtr viewService()
cxLogicManager_EXPORT VideoServicePtr videoService()
cxLogicManager_EXPORT TrackingServicePtr trackingService()
void newMessage(Message message)
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
virtual ~StatusBar()
empty
Definition: cxStatusBar.cpp:94
VLCRecorder * vlc()
Shortcut for accessing the vlc recorder.
StatusBar()
connects signals and slots
Definition: cxStatusBar.cpp:65
boost::shared_ptr< class Tool > ToolPtr