CustusX  15.3.4-beta
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 
61 
62 namespace cx
63 {
65  mRenderingFpsLabel(new QLabel(this)),
66  mGrabbingInfoLabel(new QLabel(this)),
67  mTpsLabel(new QLabel(this))
68 {
69  mMessageListener = MessageListener::create();
70  mMessageListener->installFilter(MessageFilterStatusBar::create());
71  connect(mMessageListener.get(), &MessageListener::newMessage, this, &StatusBar::showMessageSlot);
72 
73  connect(trackingService().get(), &TrackingService::stateChanged, this, &StatusBar::resetToolManagerConnection);
74 
76  mActiveTool = ActiveToolProxy::New(ts);
77  connect(mActiveTool.get(), &ActiveToolProxy::tps, this, &StatusBar::tpsSlot);
78 
79  connect(trackingService().get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(updateToolButtons()));
80 
81  connect(viewService().get(), SIGNAL(fps(int)), this, SLOT(renderingFpsSlot(int)));
82 
83  connect(videoService().get(), SIGNAL(fps(int)), this, SLOT(grabbingFpsSlot(int)));
84  connect(videoService().get(), SIGNAL(connected(bool)), this, SLOT(grabberConnectedSlot(bool)));
85 
86 // this->addPermanentWidget(mMessageLevelLabel);
87  this->addPermanentWidget(mRenderingFpsLabel);
88 }
89 
91 {
92 }
93 
94 void StatusBar::resetToolManagerConnection()
95 {
96  this->disconnectFromToolSignals();
97  if (trackingService()->getState()>=Tool::tsCONFIGURED)
98  this->connectToToolSignals();
99  this->updateToolButtons();
100 }
101 
102 void StatusBar::connectToToolSignals()
103 {
104  this->disconnectFromToolSignals(); // avoid duplicates
105 
106  this->addPermanentWidget(mTpsLabel);
107 
108  TrackingService::ToolMap tools = trackingService()->getTools();
109  for (TrackingService::ToolMap::iterator it = tools.begin(); it != tools.end(); ++it)
110  {
111  ToolPtr tool = it->second;
112  if (tool->hasType(Tool::TOOL_MANUAL))
113  continue;
114  if (tool == trackingService()->getManualTool())
115  continue;
116  connect(tool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
117 
118  ToolData current;
119  current.mTool = tool;
120  current.mAction.reset(new QAction(tool->getName(), NULL));
121  current.mAction->setToolTip("Press to set active");
122 
123  QtSignalAdapters::connect0<void()>(
124  current.mAction.get(),
125  SIGNAL(triggered()),
126  boost::bind(&StatusBar::activateTool, this, tool->getUid()));
127 
128  current.mButton.reset(new QToolButton);
129  current.mButton->setDefaultAction(current.mAction.get());
130  this->addPermanentWidget(current.mButton.get());
131  mToolData.push_back(current);
132  }
133 
134  this->updateToolButtons();
135 }
136 
137 void StatusBar::disconnectFromToolSignals()
138 {
139  this->removeWidget(mTpsLabel);
140 
141  for (unsigned i = 0; i < mToolData.size(); ++i)
142  {
143  ToolData current = mToolData[i];
144 
145  disconnect(current.mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolButtons()));
146  this->removeWidget(current.mButton.get());
147  }
148  mToolData.clear();
149 }
150 
151 
152 void StatusBar::activateTool(QString uid)
153 {
154  trackingService()->setActiveTool(uid);
155 }
156 
157 void StatusBar::updateToolButtons()
158 {
159  ToolPtr activeTool = trackingService()->getActiveTool();
160 
161  for (unsigned i = 0; i < mToolData.size(); ++i)
162  {
163  ToolData current = mToolData[i];
164  ToolPtr tool = current.mTool;
165  QString color = this->getToolStyle(tool->getVisible(), tool->isInitialized(), activeTool == tool);
166  current.mButton->setStyleSheet(QString("QToolButton { %1; }").arg(color));
167 
168  if (!tool->isInitialized())
169  current.mAction->setToolTip("Tool is not Initialized");
170  else if (activeTool == tool)
171  current.mAction->setToolTip("Active Tool");
172  else if (tool->getVisible())
173  current.mAction->setToolTip("Tool not visible/not tracking");
174  else
175  current.mAction->setToolTip("Tool visible. Press to set as active");
176  }
177 }
178 
179 QString StatusBar::getToolStyle(bool visible, bool initialized, bool active)
180 {
181  if (!initialized)
182  return QString("background-color: silver");
183 
184  if (visible)
185  {
186  if (active)
187  return QString("background-color: lime");
188  else
189  return QString("background-color: green");
190  }
191 
192  return QString("background-color: orangered");
193 }
194 
195 void StatusBar::renderingFpsSlot(int numFps)
196 {
197  QString fpsString = "FPS: " + QString::number(numFps);
198  mRenderingFpsLabel->setText(fpsString);
199 }
200 
201 void StatusBar::tpsSlot(int numTps)
202 {
203  QString tpsString = "TPS: " + QString::number(numTps);
204  mTpsLabel->setText(tpsString);
205 }
206 
207 void StatusBar::grabbingFpsSlot(int numFps)
208 {
209  QString infoString = "VideoConnection-FPS: " + QString::number(numFps);
210  mGrabbingInfoLabel->setText(infoString);
211 }
212 
213 void StatusBar::grabberConnectedSlot(bool connected)
214 {
215  if (connected)
216  {
217  this->addPermanentWidget(mGrabbingInfoLabel);
218  mGrabbingInfoLabel->show();
219  }
220  else
221  this->removeWidget(mGrabbingInfoLabel);
222 }
223 
224 void StatusBar::showMessageSlot(Message message)
225 {
226  QString text = QString("[%1] %4")
227  .arg(qstring_cast(message.getMessageLevel()))
228  .arg(message.getText());
229 
230 // this->showMessage(message.getPrintableMessage(), message.getTimeout());
231 // mMessageLevelLabel->setPixmap(QPixmap(":/images/go-home.png"));
232 // mMessageLevelLabel->setPixmap(QPixmap(":/icons/screenshot-screen.png"));
233 // mMessageLevelLabel->setIcon(QIcon(":/icons/screenshot-screen.png"));
234 // mMessageLevelLabel->show();
235  this->showMessage(text, message.getTimeout());
236 }
237 
238 }//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:81
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
Representation of a mouse/keyboard-controlled virtual tool.
Definition: cxTool.h:91
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:90
StatusBar()
connects signals and slots
Definition: cxStatusBar.cpp:64
boost::shared_ptr< class Tool > ToolPtr