CustusX  20.03-rc1
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxStatusBar.h"
13 
14 #include <QLabel>
15 #include <QString>
16 #include <QHBoxLayout>
17 #include <QAction>
18 #include <QToolButton>
19 #include <QPixmap>
20 #include <QMetaObject>
21 
22 #include "cxTrackingService.h"
23 
24 #include "cxTrackingService.h"
25 #include "cxVideoService.h"
26 #include "boost/bind.hpp"
27 #include <QMetaMethod>
28 #include "libQtSignalAdapters/Qt2Func.h"
29 #include "libQtSignalAdapters/ConnectionFactories.h"
30 #include "cxManualTool.h"
31 #include "cxTypeConversions.h"
32 #include "cxActiveToolProxy.h"
33 #include "cxViewService.h"
34 
35 #include "cxLogMessageFilter.h"
36 #include "cxMessageListener.h"
37 #include "cxVLCRecorder.h"
38 
39 
40 namespace cx
41 {
42 StatusBar::StatusBar(TrackingServicePtr trackingService, ViewServicePtr viewService, VideoServicePtr videoService) :
43  mRenderingFpsLabel(new QLabel(this)),
44  mGrabbingInfoLabel(new QLabel(this)),
45  mRecordFullscreenLabel(new QLabel(this)),
46  mTpsLabel(new QLabel(this)),
47  mTrackingService(trackingService)
48 {
49  mMessageListener = MessageListener::create();
50  mMessageListener->installFilter(MessageFilterStatusBar::create());
51  connect(mMessageListener.get(), &MessageListener::newMessage, this, &StatusBar::showMessageSlot);
52 
53  connect(trackingService.get(), &TrackingService::stateChanged, this, &StatusBar::resetToolManagerConnection);
54 
55  mActiveTool = ActiveToolProxy::New(trackingService);
56  connect(mActiveTool.get(), &ActiveToolProxy::tps, this, &StatusBar::tpsSlot);
57 
58  connect(trackingService.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(updateToolButtons()));
59 
60  connect(viewService.get(), SIGNAL(fps(int)), this, SLOT(renderingFpsSlot(int)));
61 
62  connect(videoService.get(), SIGNAL(fps(int)), this, SLOT(grabbingFpsSlot(int)));
63  connect(videoService.get(), SIGNAL(connected(bool)), this, SLOT(grabberConnectedSlot(bool)));
64 
65  connect(vlc(), &VLCRecorder::stateChanged, this, &StatusBar::onRecordFullscreenChanged);
66 
67 // this->addPermanentWidget(mMessageLevelLabel);
68  this->addPermanentWidget(mRenderingFpsLabel);
69 }
70 
72 {
73 }
74 
75 void StatusBar::resetToolManagerConnection()
76 {
77  this->disconnectFromToolSignals();
78  if (mTrackingService->getState()>=Tool::tsCONFIGURED)
79  this->connectToToolSignals();
80  this->updateToolButtons();
81 }
82 
83 void StatusBar::connectToToolSignals()
84 {
85  this->disconnectFromToolSignals(); // avoid duplicates
86 
87  this->addPermanentWidget(mTpsLabel);
88  mTpsLabel->show();
89 
90  TrackingService::ToolMap tools = mTrackingService->getTools();
91  for (TrackingService::ToolMap::iterator it = tools.begin(); it != tools.end(); ++it)
92  {
93  ToolPtr tool = it->second;
94  if (tool->hasType(Tool::TOOL_MANUAL))
95  continue;
96  if (tool == mTrackingService->getManualTool())
97  continue;
98  connect(tool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
99 
100  ToolData current;
101  current.mTool = tool;
102  current.mAction.reset(new QAction(tool->getName(), NULL));
103  current.mAction->setToolTip("Press to set active");
104 
105  QtSignalAdapters::connect0<void()>(
106  current.mAction.get(),
107  SIGNAL(triggered()),
108  boost::bind(&StatusBar::activateTool, this, tool->getUid()));
109 
110  current.mButton.reset(new QToolButton);
111  current.mButton->setDefaultAction(current.mAction.get());
112  this->addPermanentWidget(current.mButton.get());
113  mToolData.push_back(current);
114  }
115 
116  this->updateToolButtons();
117 }
118 
119 void StatusBar::disconnectFromToolSignals()
120 {
121  this->removeWidget(mTpsLabel);
122 
123  for (unsigned i = 0; i < mToolData.size(); ++i)
124  {
125  ToolData current = mToolData[i];
126 
127  disconnect(current.mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
128  this->removeWidget(current.mButton.get());
129  }
130  mToolData.clear();
131 }
132 
133 
134 void StatusBar::activateTool(QString uid)
135 {
136  mTrackingService->setActiveTool(uid);
137 }
138 
139 void StatusBar::updateToolButtons()
140 {
141  ToolPtr activeTool = mTrackingService->getActiveTool();
142 
143  for (unsigned i = 0; i < mToolData.size(); ++i)
144  {
145  ToolData current = mToolData[i];
146  ToolPtr tool = current.mTool;
147  QString color = this->getToolStyle(tool->getVisible(), tool->isInitialized(), activeTool == tool);
148  current.mButton->setStyleSheet(QString("QToolButton { %1; }").arg(color));
149 
150  if (!tool->isInitialized())
151  current.mAction->setToolTip("Tool is not Initialized");
152  else if (activeTool == tool)
153  current.mAction->setToolTip("Active Tool");
154  else if (!tool->getVisible())
155  current.mAction->setToolTip("Tool not visible/not tracking");
156  else
157  current.mAction->setToolTip("Tool visible. Press to set as active");
158  }
159 }
160 
161 QString StatusBar::getToolStyle(bool visible, bool initialized, bool active)
162 {
163  if (!initialized)
164  return QString("background-color: silver");
165 
166  if (visible)
167  {
168  if (active)
169  return QString("background-color: lime");
170  else
171  return QString("background-color: green");
172  }
173 
174  return QString("background-color: orangered");
175 }
176 
177 void StatusBar::renderingFpsSlot(int numFps)
178 {
179  QString fpsString = "FPS: " + QString::number(numFps);
180  mRenderingFpsLabel->setText(fpsString);
181 }
182 
183 void StatusBar::tpsSlot(int numTps)
184 {
185  QString tpsString = "TPS: " + QString::number(numTps);
186  mTpsLabel->setText(tpsString);
187 }
188 
189 void StatusBar::grabbingFpsSlot(int numFps)
190 {
191  QString infoString = "VideoConnection-FPS: " + QString::number(numFps);
192  mGrabbingInfoLabel->setText(infoString);
193 }
194 
195 void StatusBar::grabberConnectedSlot(bool connected)
196 {
197  if (connected)
198  {
199  this->addPermanentWidget(mGrabbingInfoLabel);
200  mGrabbingInfoLabel->show();
201  }
202  else
203  this->removeWidget(mGrabbingInfoLabel);
204 }
205 
206 void StatusBar::onRecordFullscreenChanged()
207 {
208  QLabel* label = mRecordFullscreenLabel;
209 
210  if (vlc()->isRecording())
211  {
212  label->setMargin(0);
213  int size = this->height()*0.75; // fit within statusbar
214  QPixmap map;
215  map.load(":/icons/Video-icon_green.png");
216  label->setPixmap(map.scaled(size, size, Qt::KeepAspectRatio));
217 
218  this->addPermanentWidget(mRecordFullscreenLabel);
219  mRecordFullscreenLabel->show();
220  }
221  else
222  {
223  this->removeWidget(mRecordFullscreenLabel);
224  }
225 }
226 
227 void StatusBar::showMessageSlot(Message message)
228 {
229  QString text = QString("[%1] %4")
230  .arg(qstring_cast(message.getMessageLevel()))
231  .arg(message.getText());
232 
233  this->showMessage(text, message.getTimeout());
234 }
235 
236 }//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:42
QString getText() const
The raw message.
boost::shared_ptr< class ViewService > ViewServicePtr
static MessageFilterStatusBarPtr create()
Definition: cxStatusBar.h:40
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:75
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
Representation of a mouse/keyboard-controlled virtual tool.
Definition: cxTool.h:85
std::map< QString, ToolPtr > ToolMap
void newMessage(Message message)
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
virtual ~StatusBar()
empty
Definition: cxStatusBar.cpp:71
VLCRecorder * vlc()
Shortcut for accessing the vlc recorder.
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr