PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
QPlusToolStateDisplayWidget.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2  Program: Plus
3  Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4  See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 // Local includes
9 
10 // PlusLib includes
11 //#include <igsioTrackedFrame.h>
12 #include <vtkPlusChannel.h>
13 //#include <vtkIGSIOTrackedFrameList.h>
14 
15 // Qt includes
16 #include <QGridLayout>
17 #include <QLabel>
18 #include <QTextEdit>
19 
20 //-----------------------------------------------------------------------------
21 QPlusToolStateDisplayWidget::QPlusToolStateDisplayWidget(QWidget* aParent, Qt::WindowFlags aFlags)
22  : QWidget(aParent, aFlags)
23  , m_SelectedChannel(NULL)
24  , m_Initialized(false)
25 {
26  m_ToolNameLabels.clear();
27  m_ToolStateLabels.clear();
28 
29  // Create default appearance
30  QGridLayout* grid = new QGridLayout(this);
31  grid->setMargin(0);
32  grid->setSpacing(0);
33  QLabel* uninitializedLabel = new QLabel(tr("Tool state display is unavailable until connected to a device set."), this);
34  uninitializedLabel->setWordWrap(true);
35  grid->addWidget(uninitializedLabel);
36  m_ToolNameLabels.push_back(uninitializedLabel);
37  this->setLayout(grid);
38 }
39 
40 //-----------------------------------------------------------------------------
42 {
43  m_ToolNameLabels.clear();
44  m_ToolStateLabels.clear();
45 
46  m_SelectedChannel = NULL;
47 }
48 
49 //-----------------------------------------------------------------------------
51 {
52  LOG_TRACE("QPlusToolStateDisplayWidget::InitializeTools");
53 
54  // Clear former content
55  if (this->layout())
56  {
57  delete this->layout();
58  }
59  for (std::vector<QLabel*>::iterator it = m_ToolNameLabels.begin(); it != m_ToolNameLabels.end(); ++it)
60  {
61  delete (*it);
62  }
63  m_ToolNameLabels.clear();
64  for (std::vector<QTextEdit*>::iterator it = m_ToolStateLabels.begin(); it != m_ToolStateLabels.end(); ++it)
65  {
66  delete (*it);
67  }
68  m_ToolStateLabels.clear();
69 
70  // If connection was unsuccessful, create default appearance
71  if (!aConnectionSuccessful)
72  {
73  QGridLayout* grid = new QGridLayout(this);
74  grid->setMargin(0);
75  grid->setSpacing(0);
76  QLabel* uninitializedLabel = new QLabel(tr("Tool state display is unavailable until connected to a device set."), this);
77  uninitializedLabel->setWordWrap(true);
78  grid->addWidget(uninitializedLabel);
79  m_ToolNameLabels.push_back(uninitializedLabel);
80  this->setLayout(grid);
81 
82  m_Initialized = false;
83 
84  return PLUS_SUCCESS;
85  }
86 
87  m_SelectedChannel = aChannel;
88 
89  // Fail if data collector or tracker is not initialized (once the content was deleted)
90  if (m_SelectedChannel == NULL)
91  {
92  LOG_ERROR("Data source is missing!");
93  return PLUS_FAIL;
94  }
95 
96  // Get transforms
97  std::vector<igsioTransformName> transformNames;
98  igsioTrackedFrame trackedFrame;
99  m_SelectedChannel->GetTrackedFrame(trackedFrame);
100  trackedFrame.GetFrameTransformNameList(transformNames);
101 
102  // Set up layout
103  QGridLayout* grid = new QGridLayout(this);
104  grid->setColumnStretch(transformNames.size(), 1);
105  grid->setSpacing(2);
106  grid->setVerticalSpacing(4);
107  grid->setContentsMargins(4, 4, 4, 4);
108 
109  m_ToolStateLabels.resize(transformNames.size(), NULL);
110 
111  int i;
112  std::vector<igsioTransformName>::iterator it;
113  for (it = transformNames.begin(), i = 0; it != transformNames.end(); ++it, ++i)
114  {
115  // Assemble tool name and add label to layout and label list
116  QString toolNameString = QString("%1: %2").arg(i).arg(it->GetTransformName().c_str());
117 
118  QLabel* toolNameLabel = new QLabel(this);
119  toolNameLabel->setText(toolNameString);
120  toolNameLabel->setToolTip(toolNameString);
121  QSizePolicy sizePolicyNameLabel(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
122  sizePolicyNameLabel.setHorizontalStretch(2);
123  toolNameLabel->setSizePolicy(sizePolicyNameLabel);
124  toolNameLabel->setMinimumHeight(24);
125  grid->addWidget(toolNameLabel, i, 0, Qt::AlignLeft);
126  m_ToolNameLabels.push_back(toolNameLabel);
127 
128  // Create tool status label and add it to layout and label list
129  QTextEdit* toolStateLabel = new QTextEdit("N/A", this);
130  toolStateLabel->setTextColor(QColor::fromRgb(96, 96, 96));
131  toolStateLabel->setMaximumHeight(18);
132  toolStateLabel->setLineWrapMode(QTextEdit::NoWrap);
133  toolStateLabel->setReadOnly(true);
134  toolStateLabel->setFrameShape(QFrame::NoFrame);
135  toolStateLabel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
136  toolStateLabel->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
137  toolStateLabel->setAlignment(Qt::AlignRight);
138  toolStateLabel->setMinimumHeight(24);
139  QSizePolicy sizePolicyStateLabel(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
140  sizePolicyStateLabel.setHorizontalStretch(1);
141  toolStateLabel->setSizePolicy(sizePolicyStateLabel);
142  grid->addWidget(toolStateLabel, i, 1, Qt::AlignRight);
143  m_ToolStateLabels[i] = toolStateLabel;
144  }
145 
146  this->setLayout(grid);
147 
148  m_Initialized = true;
149 
150  return PLUS_SUCCESS;
151 }
152 
153 //-----------------------------------------------------------------------------
155 {
156  return m_Initialized;
157 }
158 
159 //-----------------------------------------------------------------------------
161 {
162  if (! m_Initialized)
163  {
164  LOG_ERROR("Widget is not inialized!");
165  return PLUS_FAIL;
166  }
167 
168  // Re-initialize widget if enabled statuses have changed
169  int numberOfDisplayedTools = 0;
170 
171  // Get transforms
172  std::vector<igsioTransformName> transformNames;
173  igsioTrackedFrame trackedFrame;
174  m_SelectedChannel->GetTrackedFrame(trackedFrame);
175  trackedFrame.GetFrameTransformNameList(transformNames);
176 
177  if (transformNames.size() != m_ToolStateLabels.size())
178  {
179  LOG_WARNING("Tool number inconsistency!");
180 
182  {
183  LOG_ERROR("Re-initializing tool state widget failed");
184  return PLUS_FAIL;
185  }
186  }
187 
188  std::vector<igsioTransformName>::iterator transformIt;
189  std::vector<QTextEdit*>::iterator labelIt;
190  for (transformIt = transformNames.begin(), labelIt = m_ToolStateLabels.begin(); transformIt != transformNames.end() && labelIt != m_ToolStateLabels.end(); ++transformIt, ++labelIt)
191  {
192  QTextEdit* label = (*labelIt);
193 
194  if (label == NULL)
195  {
196  LOG_WARNING("Invalid tool state label");
197  continue;
198  }
199 
200  ToolStatus status(TOOL_INVALID);
201  if (trackedFrame.GetFrameTransformStatus(*transformIt, status) != PLUS_SUCCESS)
202  {
203  std::string transformNameStr;
204  transformIt->GetTransformName(transformNameStr);
205  LOG_WARNING("Unable to get transform status for transform" << transformNameStr);
206  label->setText("STATUS ERROR");
207  label->setTextColor(QColor::fromRgb(223, 0, 0));
208  }
209  else
210  {
211  label->setText(igsioCommon::ConvertToolStatusToString(status).c_str());
212  switch (status)
213  {
214  case (TOOL_OK):
215  label->setTextColor(Qt::green);
216  break;
217  default:
218  label->setTextColor(QColor::fromRgb(223, 0, 0));
219  break;
220  }
221  }
222  }
223 
224  return PLUS_SUCCESS;
225 }
std::vector< QTextEdit * > m_ToolStateLabels
virtual PlusStatus GetTrackedFrame(double timestamp, igsioTrackedFrame &trackedFrame, bool enableImageData=true)
igsioStatus PlusStatus
Definition: PlusCommon.h:40
for i
#define PLUS_FAIL
Definition: PlusCommon.h:43
PlusStatus InitializeTools(vtkPlusChannel *aChannel, bool aConnectionSuccessful)
QPlusToolStateDisplayWidget(QWidget *aParent=0, Qt::WindowFlags aFlags=0)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
std::vector< QLabel * > m_ToolNameLabels
Contains an optional timestamped circular buffer containing the video images and a number of timestam...