Fraxinus  17.12-rc2
An IGT application
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 "cxViewService.h"
56 
57 #include "cxLogMessageFilter.h"
58 #include "cxMessageListener.h"
59 #include "cxVLCRecorder.h"
60 
61 
62 namespace cx
63 {
64 StatusBar::StatusBar(TrackingServicePtr trackingService, ViewServicePtr viewService, VideoServicePtr videoService) :
65  mRenderingFpsLabel(new QLabel(this)),
66  mGrabbingInfoLabel(new QLabel(this)),
67  mRecordFullscreenLabel(new QLabel(this)),
68  mTpsLabel(new QLabel(this)),
69  mTrackingService(trackingService)
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 
77  mActiveTool = ActiveToolProxy::New(trackingService);
78  connect(mActiveTool.get(), &ActiveToolProxy::tps, this, &StatusBar::tpsSlot);
79 
80  connect(trackingService.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(updateToolButtons()));
81 
82  connect(viewService.get(), SIGNAL(fps(int)), this, SLOT(renderingFpsSlot(int)));
83 
84  connect(videoService.get(), SIGNAL(fps(int)), this, SLOT(grabbingFpsSlot(int)));
85  connect(videoService.get(), SIGNAL(connected(bool)), this, SLOT(grabberConnectedSlot(bool)));
86 
87  connect(vlc(), &VLCRecorder::stateChanged, this, &StatusBar::onRecordFullscreenChanged);
88 
89 // this->addPermanentWidget(mMessageLevelLabel);
90  this->addPermanentWidget(mRenderingFpsLabel);
91 }
92 
94 {
95 }
96 
97 void StatusBar::resetToolManagerConnection()
98 {
99  this->disconnectFromToolSignals();
100  if (mTrackingService->getState()>=Tool::tsCONFIGURED)
101  this->connectToToolSignals();
102  this->updateToolButtons();
103 }
104 
105 void StatusBar::connectToToolSignals()
106 {
107  this->disconnectFromToolSignals(); // avoid duplicates
108 
109  this->addPermanentWidget(mTpsLabel);
110  mTpsLabel->show();
111 
112  TrackingService::ToolMap tools = mTrackingService->getTools();
113  for (TrackingService::ToolMap::iterator it = tools.begin(); it != tools.end(); ++it)
114  {
115  ToolPtr tool = it->second;
116  if (tool->hasType(Tool::TOOL_MANUAL))
117  continue;
118  if (tool == mTrackingService->getManualTool())
119  continue;
120  connect(tool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
121 
122  ToolData current;
123  current.mTool = tool;
124  current.mAction.reset(new QAction(tool->getName(), NULL));
125  current.mAction->setToolTip("Press to set active");
126 
127  QtSignalAdapters::connect0<void()>(
128  current.mAction.get(),
129  SIGNAL(triggered()),
130  boost::bind(&StatusBar::activateTool, this, tool->getUid()));
131 
132  current.mButton.reset(new QToolButton);
133  current.mButton->setDefaultAction(current.mAction.get());
134  this->addPermanentWidget(current.mButton.get());
135  mToolData.push_back(current);
136  }
137 
138  this->updateToolButtons();
139 }
140 
141 void StatusBar::disconnectFromToolSignals()
142 {
143  this->removeWidget(mTpsLabel);
144 
145  for (unsigned i = 0; i < mToolData.size(); ++i)
146  {
147  ToolData current = mToolData[i];
148 
149  disconnect(current.mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
150  this->removeWidget(current.mButton.get());
151  }
152  mToolData.clear();
153 }
154 
155 
156 void StatusBar::activateTool(QString uid)
157 {
158  mTrackingService->setActiveTool(uid);
159 }
160 
161 void StatusBar::updateToolButtons()
162 {
163  ToolPtr activeTool = mTrackingService->getActiveTool();
164 
165  for (unsigned i = 0; i < mToolData.size(); ++i)
166  {
167  ToolData current = mToolData[i];
168  ToolPtr tool = current.mTool;
169  QString color = this->getToolStyle(tool->getVisible(), tool->isInitialized(), activeTool == tool);
170  current.mButton->setStyleSheet(QString("QToolButton { %1; }").arg(color));
171 
172  if (!tool->isInitialized())
173  current.mAction->setToolTip("Tool is not Initialized");
174  else if (activeTool == tool)
175  current.mAction->setToolTip("Active Tool");
176  else if (!tool->getVisible())
177  current.mAction->setToolTip("Tool not visible/not tracking");
178  else
179  current.mAction->setToolTip("Tool visible. Press to set as active");
180  }
181 }
182 
183 QString StatusBar::getToolStyle(bool visible, bool initialized, bool active)
184 {
185  if (!initialized)
186  return QString("background-color: silver");
187 
188  if (visible)
189  {
190  if (active)
191  return QString("background-color: lime");
192  else
193  return QString("background-color: green");
194  }
195 
196  return QString("background-color: orangered");
197 }
198 
199 void StatusBar::renderingFpsSlot(int numFps)
200 {
201  QString fpsString = "FPS: " + QString::number(numFps);
202  mRenderingFpsLabel->setText(fpsString);
203 }
204 
205 void StatusBar::tpsSlot(int numTps)
206 {
207  QString tpsString = "TPS: " + QString::number(numTps);
208  mTpsLabel->setText(tpsString);
209 }
210 
211 void StatusBar::grabbingFpsSlot(int numFps)
212 {
213  QString infoString = "VideoConnection-FPS: " + QString::number(numFps);
214  mGrabbingInfoLabel->setText(infoString);
215 }
216 
217 void StatusBar::grabberConnectedSlot(bool connected)
218 {
219  if (connected)
220  {
221  this->addPermanentWidget(mGrabbingInfoLabel);
222  mGrabbingInfoLabel->show();
223  }
224  else
225  this->removeWidget(mGrabbingInfoLabel);
226 }
227 
228 void StatusBar::onRecordFullscreenChanged()
229 {
230  QLabel* label = mRecordFullscreenLabel;
231 
232  if (vlc()->isRecording())
233  {
234  label->setMargin(0);
235  int size = this->height()*0.75; // fit within statusbar
236  QPixmap map;
237  map.load(":/icons/Video-icon_green.png");
238  label->setPixmap(map.scaled(size, size, Qt::KeepAspectRatio));
239 
240  this->addPermanentWidget(mRecordFullscreenLabel);
241  mRecordFullscreenLabel->show();
242  }
243  else
244  {
245  this->removeWidget(mRecordFullscreenLabel);
246  }
247 }
248 
249 void StatusBar::showMessageSlot(Message message)
250 {
251  QString text = QString("[%1] %4")
252  .arg(qstring_cast(message.getMessageLevel()))
253  .arg(message.getText());
254 
255  this->showMessage(text, message.getTimeout());
256 }
257 
258 }//namespace cx
QString qstring_cast(const T &val)
static MessageListenerPtr create(LogPtr log=LogPtr())
boost::shared_ptr< class VideoService > VideoServicePtr
boost::shared_ptr< class TrackingService > TrackingServicePtr
StatusBar(TrackingServicePtr trackingService, ViewServicePtr viewService, VideoServicePtr videoService)
connects signals and slots
Definition: cxStatusBar.cpp:64
QString getText() const
The raw message.
boost::shared_ptr< class ViewService > ViewServicePtr
static MessageFilterStatusBarPtr create()
Definition: cxStatusBar.h:61
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
std::map< QString, ToolPtr > ToolMap
void newMessage(Message message)
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
virtual ~StatusBar()
empty
Definition: cxStatusBar.cpp:93
VLCRecorder * vlc()
Shortcut for accessing the vlc recorder.
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr