Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include <usGetModuleContext.h>
15 
16 
18  : QWidget(parent), m_Controls(nullptr), m_StatusLabels(nullptr), m_NavigationDatas(nullptr), m_NavDatasNewFlag(false), m_previewToolStorage(nullptr)
19 {
20  m_ShowPositions = false;
21  m_ShowQuaternions = false;
22  m_Alignment = Qt::AlignHCenter;
24 
25  CreateQtPartControl( this );
26 }
27 
29  {
30  //set new style
31  m_Style = newStyle;
32 
33  //update current labels to show them in the new style
34  Refresh();
35  }
36 
38 {
39  m_ShowPositions = enable;
40 
41  //update current labels
42  Refresh();
43 }
44 
46 {
47  m_ShowQuaternions = enable;
48 
49  //update current labels
50  Refresh();
51 }
52 
53 void QmitkToolTrackingStatusWidget::SetTextAlignment(Qt::AlignmentFlag alignment)
54 {
55  m_Alignment = alignment;
56 
57  //update current labels
58  Refresh();
59 }
60 
62 {
63  m_Context->RemoveServiceListener(this, &QmitkToolTrackingStatusWidget::OnServiceEvent);
64  //m_Controls = nullptr;
65  if (m_StatusLabels!=nullptr) {delete m_StatusLabels;}
66  if (m_NavigationDatas != nullptr)
67  {
68  m_NavigationDatas->clear();
69  if (m_NavDatasNewFlag)
70  {
71  delete m_NavigationDatas;
72  }
73  }
74 }
75 
77 {
78  if (!m_Controls)
79  {
80  // create GUI widgets
81  m_Controls = new Ui::QmitkToolTrackingStatusWidgetControls;
82  m_Controls->setupUi(parent);
83 
84  this->CreateConnections();
85 
86  //add empty label
87  AddEmptyLabel();
88  }
89  m_Context = us::GetModuleContext();
90 
91  std::string m_Filter = "(" + us::ServiceConstants::OBJECTCLASS() + "=" + "org.mitk.services.NavigationToolStorage" + ")";
92 
93  m_Context->AddServiceListener(this, &QmitkToolTrackingStatusWidget::OnServiceEvent, m_Filter);
94 }
95 
97 {
98  if ((event.GetType() == us::ServiceEvent::MODIFIED) && (m_previewToolStorage.IsNotNull())) {this->PreShowTools(m_previewToolStorage);}
99 }
100 
102 {
103 }
104 
105 void QmitkToolTrackingStatusWidget::SetNavigationDatas(std::vector<mitk::NavigationData::Pointer>* navDatas)
106 {
107  m_NavigationDatas = navDatas;
108  m_previewToolStorage = nullptr;
109 }
110 
111 void QmitkToolTrackingStatusWidget::AddNavigationData(mitk::NavigationData::Pointer nd)
112 {
113  if(m_NavigationDatas == nullptr)
114  {
115  m_NavigationDatas = new std::vector<mitk::NavigationData::Pointer>();
116  m_NavDatasNewFlag = true;
117  m_previewToolStorage = nullptr;
118  }
119 
120  m_NavigationDatas->push_back(nd);
121 }
122 
123 void QmitkToolTrackingStatusWidget::Refresh(int posPrecision, int quatPrecision)
124 {
125  if(m_NavigationDatas == nullptr || m_NavigationDatas->size() <= 0)
126  {
127  RemoveGuiLabels();
128  AddEmptyLabel();
129  return;
130  }
131 
132  mitk::NavigationData* navData;
133 
134  for(unsigned int i = 0; i < m_NavigationDatas->size(); i++)
135  {
136  navData = m_NavigationDatas->at(i).GetPointer();
137  QString name(navData->GetName());
138  QString pos = "";
139  QString quat = "";
140  if (m_ShowPositions)
141  {
142  mitk::Point3D position = navData->GetPosition();
143  pos = " [" + QString::number(position[0],'f',posPrecision) + ";" + QString::number(position[1],'f',posPrecision) + ";" + QString::number(position[2],'f',posPrecision) + "]";
144  }
145  if (m_ShowQuaternions)
146  {
147  mitk::Quaternion quaternion = navData->GetOrientation();
148  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()) + "]";
149  }
150 
151  if(!(m_StatusLabels->at(i)->text() == name+pos+quat))
152  m_StatusLabels->at(i)->setText(name+pos+quat);
153 
154  if(navData->IsDataValid())
155  m_StatusLabels->at(i)->setStyleSheet("QLabel{background-color: #1F641C }");
156  else
157  m_StatusLabels->at(i)->setStyleSheet("QLabel{background-color: #ff7878 }");
158  }
159 }
160 
162 {
163  RemoveGuiLabels();
164 
165  if(m_NavigationDatas == nullptr || m_NavigationDatas->size() <= 0)
166  {
167  RemoveGuiLabels();
168  AddEmptyLabel();
169  return;
170  }
171 
172  m_StatusLabels = new QVector<QLabel*>();
173  mitk::NavigationData* navData;
174  QLabel* label;
175 
176  for(unsigned int i = 0; i < m_NavigationDatas->size(); i++)
177  {
178  navData = m_NavigationDatas->at(i).GetPointer();
179 
180  QString name(navData->GetName());
181 
182  label = new QLabel(name, this);
183  label->setObjectName(name);
184  label->setAlignment(m_Alignment | Qt::AlignVCenter);
185  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
186 
187  m_StatusLabels->append(label);
189  m_Controls->m_VerticalLayout->addWidget(m_StatusLabels->at(i));
190  else m_Controls->m_GridLayout->addWidget(m_StatusLabels->at(i),i,0);
191  }
192 }
193 
194 void QmitkToolTrackingStatusWidget::PreShowTools(mitk::NavigationToolStorage::Pointer toolStorage)
195 {
196  RemoveGuiLabels();
197  QLabel* label;
198 
199  for(unsigned int i = 0; i < toolStorage->GetToolCount(); i++)
200  {
201  QString name(toolStorage->GetTool(i)->GetToolName().c_str());
202 
203  label = new QLabel(name, this);
204  label->setObjectName(name);
205  label->setAlignment(m_Alignment | Qt::AlignVCenter);
206  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
207  label->setStyleSheet("QLabel{background-color: #dddddd; color: #000000 }");
208  if (m_Style == QmitkToolTrackingStatusWidget::VerticalUpperStyle) m_Controls->m_VerticalLayout->addWidget(label);
209  else m_Controls->m_GridLayout->addWidget(label);
210  }
211  m_previewToolStorage = toolStorage;
212 }
213 
215 {
216  //remove GUI elements
217  RemoveGuiLabels();
218 
219  //clear members
220  if(m_StatusLabels != nullptr && m_StatusLabels->size() > 0)
221  {
222  delete m_StatusLabels;
223  m_StatusLabels = new QVector< QLabel* >();
224  }
225 
226  if(m_NavigationDatas != nullptr && m_NavigationDatas->size() > 0)
227  {
228  if (m_NavDatasNewFlag)
229  {
230  delete m_NavigationDatas;
231  m_NavDatasNewFlag = false;
232  }
233  m_NavigationDatas = new std::vector<mitk::NavigationData::Pointer>();
234  m_NavDatasNewFlag = true;
235  }
236 
237  //add empty label
238  AddEmptyLabel();
239 }
240 
241 void QmitkToolTrackingStatusWidget::RemoveGuiLabels()
242 {
243  while(m_Controls->m_GridLayout->count() > 0 || m_Controls->m_VerticalLayout->count() > 0)
244  {
245  if (m_Controls->m_GridLayout->count() > 0)
246  {
247  QWidget* actWidget = m_Controls->m_GridLayout->itemAt(0)->widget();
248  m_Controls->m_GridLayout->removeWidget(actWidget);
249  delete actWidget;
250  }
251  else if (m_Controls->m_VerticalLayout->count() > 0)
252  {
253  QWidget* actWidget = m_Controls->m_VerticalLayout->itemAt(0)->widget();
254  m_Controls->m_VerticalLayout->removeWidget(actWidget);
255  delete actWidget;
256  }
257  }
258 }
259 
260 void QmitkToolTrackingStatusWidget::AddEmptyLabel()
261 {
262  //add a label which tells that no tools are loaded yet
263  QLabel* label = new QLabel("No tools loaded yet.", this);
264  label->setObjectName("No tools loaded yet.");
265  label->setAlignment(m_Alignment | Qt::AlignVCenter);
266  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
267  label->setStyleSheet("QLabel{background-color: #dddddd; color: #000000 }");
268  if (m_Style == QmitkToolTrackingStatusWidget::VerticalUpperStyle) m_Controls->m_VerticalLayout->addWidget(label);
269  else m_Controls->m_GridLayout->addWidget(label);
270 }
Ui::QmitkToolTrackingStatusWidgetControls * m_Controls
gui widgets
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 ...
Navigation Data.
virtual const char * GetName() const
returns the name of the NavigationData object
virtual PositionType GetPosition() const
returns position of the NavigationData object
void SetShowPositions(bool enable)
Enables / disables if the tool positions are shown. Default is off.
QmitkToolTrackingStatusWidget(QWidget *parent)
default constructor
~QmitkToolTrackingStatusWidget() override
default destructor
void ShowStatusLabels()
Sets up the labels in this widget&#39;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...
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()
void AddNavigationData(mitk::NavigationData::Pointer nd)
Adds the NavigationData to the existing ones.
Type GetType() const
virtual OrientationType GetOrientation() const
returns the orientation of the NavigationData object
virtual bool IsDataValid() const
returns true if the object contains valid data
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.