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