Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkToolTrackingStatusWidget.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include <usGetModuleContext.h>
19 
20 
22  : QWidget(parent), m_Controls(NULL), m_StatusLabels(NULL), m_NavigationDatas(NULL), m_NavDatasNewFlag(false)
23 {
24  m_ShowPositions = false;
25  m_ShowQuaternions = false;
26  m_Alignment = Qt::AlignHCenter;
28 
29  CreateQtPartControl( this );
30 }
31 
33  {
34  //set new style
35  m_Style = newStyle;
36 
37  //update current labels to show them in the new style
38  Refresh();
39  }
40 
42 {
43  m_ShowPositions = enable;
44 
45  //update current labels
46  Refresh();
47 }
48 
50 {
51  m_ShowQuaternions = enable;
52 
53  //update current labels
54  Refresh();
55 }
56 
57 void QmitkToolTrackingStatusWidget::SetTextAlignment(Qt::AlignmentFlag alignment)
58 {
59  m_Alignment = alignment;
60 
61  //update current labels
62  Refresh();
63 }
64 
66 {
67  //m_Controls = NULL;
68  if (m_StatusLabels!=NULL) {delete m_StatusLabels;}
69  if (m_NavigationDatas != NULL)
70  {
71  m_NavigationDatas->clear();
72  if (m_NavDatasNewFlag)
73  {
74  delete m_NavigationDatas;
75 }
76  }
77 }
78 
80 {
81  if (!m_Controls)
82  {
83  // create GUI widgets
84  m_Controls = new Ui::QmitkToolTrackingStatusWidgetControls;
85  m_Controls->setupUi(parent);
86 
87  this->CreateConnections();
88 
89  //add empty label
90  AddEmptyLabel();
91  }
92  m_Context = us::GetModuleContext();
93 
94  std::string m_Filter = "(" + us::ServiceConstants::OBJECTCLASS() + "=" + "org.mitk.services.NavigationToolStorage" + ")";
95 
96  m_Context->AddServiceListener(this, &QmitkToolTrackingStatusWidget::OnServiceEvent, m_Filter);
97 }
98 
100 {
101  if ((event.GetType() == us::ServiceEvent::MODIFIED) && (m_previewToolStorage.IsNotNull())) {this->PreShowTools(m_previewToolStorage);}
102 }
103 
105 {
106 }
107 
108 void QmitkToolTrackingStatusWidget::SetNavigationDatas(std::vector<mitk::NavigationData::Pointer>* navDatas)
109 {
110  m_NavigationDatas = navDatas;
111  m_previewToolStorage = NULL;
112 }
113 
115 {
116  if(m_NavigationDatas == NULL)
117  {
118  m_NavigationDatas = new std::vector<mitk::NavigationData::Pointer>();
119  m_NavDatasNewFlag = true;
120  m_previewToolStorage = NULL;
121  }
122 
123  m_NavigationDatas->push_back(nd);
124 }
125 
126 void QmitkToolTrackingStatusWidget::Refresh(int posPrecision, int quatPrecision)
127 {
128  if(m_NavigationDatas == NULL || m_NavigationDatas->size() <= 0)
129  {
130  RemoveGuiLabels();
131  AddEmptyLabel();
132  return;
133  }
134 
135  mitk::NavigationData* navData;
136 
137  for(unsigned int i = 0; i < m_NavigationDatas->size(); i++)
138  {
139  navData = m_NavigationDatas->at(i).GetPointer();
140  QString name(navData->GetName());
141  QString pos = "";
142  QString quat = "";
143  if (m_ShowPositions)
144  {
145  mitk::Point3D position = navData->GetPosition();
146  pos = " [" + QString::number(position[0],'f',posPrecision) + ";" + QString::number(position[1],'f',posPrecision) + ";" + QString::number(position[2],'f',posPrecision) + "]";
147  }
148  if (m_ShowQuaternions)
149  {
150  mitk::Quaternion quaternion = navData->GetOrientation();
151  quat = " / [qx:" + QString::number(quaternion.x(),'f',quatPrecision) + ";qy:" + QString::number(quaternion.y(),'f',quatPrecision) + ";qz:" + QString::number(quaternion.z(),'f',quatPrecision) + ";qr:" + QString::number(quaternion.r()) + "]";
152  }
153 
154  if(!(m_StatusLabels->at(i)->text() == name+pos+quat))
155  m_StatusLabels->at(i)->setText(name+pos+quat);
156 
157  if(navData->IsDataValid())
158  m_StatusLabels->at(i)->setStyleSheet("QLabel{background-color: #8bff8b }");
159  else
160  m_StatusLabels->at(i)->setStyleSheet("QLabel{background-color: #ff7878 }");
161  }
162 }
163 
165 {
166  RemoveGuiLabels();
167 
168  if(m_NavigationDatas == NULL || m_NavigationDatas->size() <= 0)
169  {
170  RemoveGuiLabels();
171  AddEmptyLabel();
172  return;
173  }
174 
175  m_StatusLabels = new QVector<QLabel*>();
176  mitk::NavigationData* navData;
177  QLabel* label;
178 
179  for(unsigned int i = 0; i < m_NavigationDatas->size(); i++)
180  {
181  navData = m_NavigationDatas->at(i).GetPointer();
182 
183  QString name(navData->GetName());
184 
185  label = new QLabel(name, this);
186  label->setObjectName(name);
187  label->setAlignment(m_Alignment | Qt::AlignVCenter);
188  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
189 
190  m_StatusLabels->append(label);
191  if (m_Style == QmitkToolTrackingStatusWidget::VerticalUpperStyle) m_Controls->m_VerticalLayout->addWidget(m_StatusLabels->at(i));
192  else m_Controls->m_GridLayout->addWidget(m_StatusLabels->at(i),0,i);
193  }
194 }
195 
197 {
198  RemoveGuiLabels();
199  QLabel* label;
200 
201  for(unsigned int i = 0; i < toolStorage->GetToolCount(); i++)
202  {
203  QString name(toolStorage->GetTool(i)->GetToolName().c_str());
204 
205  label = new QLabel(name, this);
206  label->setObjectName(name);
207  label->setAlignment(m_Alignment | Qt::AlignVCenter);
208  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
209  label->setStyleSheet("QLabel{background-color: #dddddd }");
210  if (m_Style == QmitkToolTrackingStatusWidget::VerticalUpperStyle) m_Controls->m_VerticalLayout->addWidget(label);
211  else m_Controls->m_GridLayout->addWidget(label);
212  }
213  m_previewToolStorage = toolStorage;
214 }
215 
217 {
218  //remove GUI elements
219  RemoveGuiLabels();
220 
221  //clear members
222  if(m_StatusLabels != NULL && m_StatusLabels->size() > 0)
223  {
224  delete m_StatusLabels;
225  m_StatusLabels = new QVector< QLabel* >();
226  }
227 
228  if(m_NavigationDatas != NULL && m_NavigationDatas->size() > 0)
229  {
230  if (m_NavDatasNewFlag)
231  {
232  delete m_NavigationDatas;
233  m_NavDatasNewFlag = false;
234  }
235  m_NavigationDatas = new std::vector<mitk::NavigationData::Pointer>();
236  m_NavDatasNewFlag = true;
237  }
238 
239  //add empty label
240  AddEmptyLabel();
241 }
242 
243 void QmitkToolTrackingStatusWidget::RemoveGuiLabels()
244 {
245  while(m_Controls->m_GridLayout->count() > 0 || m_Controls->m_VerticalLayout->count() > 0)
246  {
247  if (m_Controls->m_GridLayout->count() > 0)
248  {
249  QWidget* actWidget = m_Controls->m_GridLayout->itemAt(0)->widget();
250  m_Controls->m_GridLayout->removeWidget(actWidget);
251  delete actWidget;
252  }
253  else if (m_Controls->m_VerticalLayout->count() > 0)
254  {
255  QWidget* actWidget = m_Controls->m_VerticalLayout->itemAt(0)->widget();
256  m_Controls->m_VerticalLayout->removeWidget(actWidget);
257  delete actWidget;
258  }
259  }
260 }
261 
262 void QmitkToolTrackingStatusWidget::AddEmptyLabel()
263 {
264  //add a label which tells that no tools are loaded yet
265  QLabel* label = new QLabel("No tools loaded yet.", this);
266  label->setObjectName("No tools loaded yet.");
267  label->setAlignment(m_Alignment | Qt::AlignVCenter);
268  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
269  label->setStyleSheet("QLabel{background-color: #dddddd }");
270  if (m_Style == QmitkToolTrackingStatusWidget::VerticalUpperStyle) m_Controls->m_VerticalLayout->addWidget(label);
271  else m_Controls->m_GridLayout->addWidget(label);
272 }
Ui::QmitkToolTrackingStatusWidgetControls * m_Controls
gui widgets
itk::SmartPointer< Self > Pointer
void RemoveStatusLabels()
Removes all status labels.
void SetStyle(QmitkToolTrackingStatusWidget::Style newStyle)
Sets the alignment style of this widget: GridLowerStyle: Tool labels are at the lower side of the wid...
void PreShowTools(mitk::NavigationToolStorage::Pointer toolStorage)
Shows tool labels for the tools in the tool storage. This method can be called BEFORE connecting the ...
virtual const char * GetName() const
returns the name of the NavigationData object
Navigation Data.
virtual OrientationType GetOrientation() const
returns the orientation of the NavigationData object
void SetShowPositions(bool enable)
Enables / disables if the tool positions are shown. Default is off.
QmitkToolTrackingStatusWidget(QWidget *parent)
default constructor
void ShowStatusLabels()
Sets up the labels in this widget's QGridLayout for showing the track status of the tracking tools...
void SetTextAlignment(Qt::AlignmentFlag alignment)
Sets the text alignment of the tool labels. Default is center. Example: Use Qt::AlignLeft for left al...
Type GetType() const
vnl_quaternion< ScalarType > Quaternion
void SetShowQuaternions(bool enable)
Enables / disables if the tool quaternions are shown. Default is off.
US_Core_EXPORT const std::string & OBJECTCLASS()
virtual bool IsDataValid() const
returns true if the object contains valid data
virtual ~QmitkToolTrackingStatusWidget()
default destructor
virtual PositionType GetPosition() const
returns position of the NavigationData object
void AddNavigationData(mitk::NavigationData::Pointer nd)
Adds the NavigationData to the existing ones.
void OnServiceEvent(const us::ServiceEvent event)
void Refresh(int posPrecision=2, int quatPrecision=2)
Changes background color of status labels (green or red) to show if actual navigation data of each to...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void SetNavigationDatas(std::vector< mitk::NavigationData::Pointer > *navDatas)
Sets the ND for this widget.