Fraxinus  2023.01.05-dev+develop.0da12
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 #include "cxSettings.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  fixFlickeringBar();
69 // this->addPermanentWidget(mMessageLevelLabel);
70  this->addPermanentWidget(mRenderingFpsLabel);
71 }
72 
74 {
75 }
76 
77 void StatusBar::fixFlickeringBar()
78 {
79  //Add an empty label with larger font size to make room for tool buttons.
80  //Otherwise tool buttons cause status bar to flicker
81 
82  QLabel* emptyLabel = new QLabel(this);
83  QFont font = emptyLabel->font();
84  font.setPointSize(font.pointSize()+5);
85  emptyLabel->setFont(font);
86  this->addWidget(emptyLabel);
87 }
88 
89 void StatusBar::resetToolManagerConnection()
90 {
91  this->disconnectFromToolSignals();
92  if (mTrackingService->getState()>=Tool::tsCONFIGURED)
93  this->connectToToolSignals();
94  this->updateToolButtons();
95 }
96 
97 void StatusBar::connectToToolSignals()
98 {
99  this->disconnectFromToolSignals(); // avoid duplicates
100 
101  this->addPermanentWidget(mTpsLabel);
102  mTpsLabel->show();
103 
104  TrackingService::ToolMap tools = mTrackingService->getTools();
105  for (TrackingService::ToolMap::iterator it = tools.begin(); it != tools.end(); ++it)
106  {
107  ToolPtr tool = it->second;
108  if (tool->hasType(Tool::TOOL_MANUAL))
109  continue;
110  if (tool == mTrackingService->getManualTool())
111  continue;
112  connect(tool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
113 
114  ToolData current;
115  current.mTool = tool;
116  current.mAction.reset(new QAction(tool->getName(), NULL));
117  current.mAction->setToolTip("Press to set active");
118 
119  QtSignalAdapters::connect0<void()>(
120  current.mAction.get(),
121  SIGNAL(triggered()),
122  boost::bind(&StatusBar::activateTool, this, tool->getUid()));
123 
124  current.mButton.reset(new QToolButton);
125  current.mButton->setDefaultAction(current.mAction.get());
126  this->addPermanentWidget(current.mButton.get());
127  mToolData.push_back(current);
128  }
129 
130  this->updateToolButtons();
131 }
132 
133 void StatusBar::disconnectFromToolSignals()
134 {
135  this->removeWidget(mTpsLabel);
136 
137  for (unsigned i = 0; i < mToolData.size(); ++i)
138  {
139  ToolData current = mToolData[i];
140 
141  disconnect(current.mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
142  this->removeWidget(current.mButton.get());
143  }
144  mToolData.clear();
145 }
146 
147 
148 void StatusBar::activateTool(QString uid)
149 {
150  ToolPtr activeTool = mTrackingService->getActiveTool();
151 
152  if (!activeTool || activeTool->getUid() != uid)
153  mTrackingService->setActiveTool(uid);
154  else
155  mTrackingService->clearActiveTool();
156 }
157 
158 void StatusBar::updateToolButtons()
159 {
160  ToolPtr activeTool = mTrackingService->getActiveTool();
161 
162  bool autoSelectActiveTool = settings()->value("Automation/autoSelectActiveTool").toBool();
163 
164  for (unsigned i = 0; i < mToolData.size(); ++i)
165  {
166  ToolData current = mToolData[i];
167  ToolPtr tool = current.mTool;
168  QString color = this->getToolStyle(tool->getVisible(), tool->isInitialized(), activeTool == tool);
169  current.mButton->setStyleSheet(QString("QToolButton { %1; }").arg(color));
170 
171  if (!tool->isInitialized())
172  current.mAction->setToolTip("Tool is not Initialized");
173  else if (activeTool == tool)
174  current.mAction->setToolTip("Active Tool");
175  else if (!tool->getVisible())
176  current.mAction->setToolTip("Tool not visible/not tracking");
177  else
178  {
179  if(autoSelectActiveTool)
180  current.mAction->setToolTip("Tool visible - Other tool is auto active");
181  else
182  current.mAction->setToolTip("Tool visible - Press to set as active");
183  }
184  }
185 }
186 
187 QString StatusBar::getToolStyle(bool visible, bool initialized, bool active)
188 {
189  if (!initialized)
190  return QString("background-color: silver");
191 
192  if (visible)
193  {
194  if (active)
195  return QString("background-color: lime");
196  else
197  return QString("background-color: green");
198  }
199 
200  return QString("background-color: orangered");
201 }
202 
203 void StatusBar::renderingFpsSlot(int numFps)
204 {
205  QString fpsString = "FPS: " + QString::number(numFps);
206  mRenderingFpsLabel->setText(fpsString);
207 }
208 
209 void StatusBar::tpsSlot(int numTps)
210 {
211  QString tpsString = "TPS: " + QString::number(numTps);
212  mTpsLabel->setText(tpsString);
213 }
214 
215 void StatusBar::grabbingFpsSlot(int numFps)
216 {
217  QString infoString = "VideoConnection-FPS: " + QString::number(numFps);
218  mGrabbingInfoLabel->setText(infoString);
219 }
220 
221 void StatusBar::grabberConnectedSlot(bool connected)
222 {
223  if (connected)
224  {
225  this->addPermanentWidget(mGrabbingInfoLabel);
226  mGrabbingInfoLabel->show();
227  }
228  else
229  this->removeWidget(mGrabbingInfoLabel);
230 }
231 
232 void StatusBar::onRecordFullscreenChanged()
233 {
234  QLabel* label = mRecordFullscreenLabel;
235 
236  if (vlc()->isRecording())
237  {
238  label->setMargin(0);
239  int size = this->height()*0.75; // fit within statusbar
240  QPixmap map;
241  map.load(":/icons/Video-icon_green.png");
242  label->setPixmap(map.scaled(size, size, Qt::KeepAspectRatio));
243 
244  this->addPermanentWidget(mRecordFullscreenLabel);
245  mRecordFullscreenLabel->show();
246  }
247  else
248  {
249  this->removeWidget(mRecordFullscreenLabel);
250  }
251 }
252 
253 void StatusBar::showMessageSlot(Message message)
254 {
255  QString text = QString("[%1] %4")
256  .arg(qstring_cast(message.getMessageLevel()))
257  .arg(message.getText());
258 
259  this->showMessage(text, message.getTimeout());
260 }
261 
262 }//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
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
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
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
std::map< QString, ToolPtr > ToolMap
void newMessage(Message message)
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
virtual ~StatusBar()
empty
Definition: cxStatusBar.cpp:73
VLCRecorder * vlc()
Shortcut for accessing the vlc recorder.
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr