QueryWidget.cpp
Go to the documentation of this file.
1 #include "QueryWidget.h"
2 
3 #include <limits>
4 #include <mutex>
5 
6 #include <QCheckBox>
7 #include <QGroupBox>
8 #include <QHBoxLayout>
9 #include <QLabel>
10 #include <QLineEdit>
11 #include <QPushButton>
12 #include <QScrollArea>
13 #include <QSpinBox>
14 #include <QTabWidget>
15 #include <QVBoxLayout>
16 #include <QWidget>
17 
19 
20 namespace armarx::armem::gui
21 {
22 
24  {
25  auto* vlayout = new QVBoxLayout();
26  auto* hlayout = new QHBoxLayout();
27 
28  {
29  _availableMemoriesGroupBox = new QGroupBox("Available memories");
30  auto vboxlayout = new QVBoxLayout();
31  vboxlayout->setMargin(1);
32  vboxlayout->setSizeConstraint(QLayout::SizeConstraint::SetMinAndMaxSize);
33  vboxlayout->setAlignment(Qt::AlignTop);
34  _de_selectMemoryServers = new QPushButton(selectText);
35  allMemoryServersSelected = false;
36  vboxlayout->addWidget(_de_selectMemoryServers);
37  _availableMemoriesGroupBox->setLayout(vboxlayout);
38  hlayout->addWidget(_availableMemoriesGroupBox);
39  }
40 
41  QFrame* vFrame = new QFrame;
42  vFrame->setFrameShape(QFrame::VLine);
43  hlayout->addWidget(vFrame);
44 
45  {
46  _additionalSettingsGroupBox = new QGroupBox("Additional query settings");
47  auto vboxlayout = new QVBoxLayout();
48  vboxlayout->setMargin(1);
49  vboxlayout->setSizeConstraint(QLayout::SizeConstraint::SetMinAndMaxSize);
50  vboxlayout->setAlignment(Qt::AlignTop);
51  _additionalSettingsGroupBox->setLayout(vboxlayout);
52 
53  _dataCheckBox = new QCheckBox("Get Data");
54  _dataCheckBox->setChecked(true);
55  vboxlayout->addWidget(_dataCheckBox);
56 
57  _dropRemovedCheckBox = new QCheckBox("Drop disconnected memories");
58  _dropRemovedCheckBox->setChecked(true);
59  vboxlayout->addWidget(_dropRemovedCheckBox);
60 
61  _dropDisabledCheckBox = new QCheckBox("Drop disabled memories");
62  _dropDisabledCheckBox->setChecked(false);
63  vboxlayout->addWidget(_dropDisabledCheckBox);
64 
65  auto* recDepthHLayout = new QHBoxLayout();
66  _recursionDepthLabel = new QLabel("Link resolution depth:");
67  recDepthHLayout->addWidget(_recursionDepthLabel);
68  _recursionDepthLabel->setFixedWidth(200);
69 
70  _recursionDepthSpinner = new QSpinBox(); // NOLINT
71  _recursionDepthSpinner->setRange(-1, std::numeric_limits<int>::max());
72  _recursionDepthSpinner->setValue(0);
73  _recursionDepthSpinner->setEnabled(_dataCheckBox->isChecked());
74  _recursionDepthSpinner->setSpecialValueText(QString("Unlimited"));
75  recDepthHLayout->addWidget(_recursionDepthSpinner);
76  recDepthHLayout->addStretch();
77  vboxlayout->addLayout(recDepthHLayout);
78 
79  /*
80  auto* exportPathHLayout = new QHBoxLayout();
81  _exportPathLabel = new QLabel("Export path:");
82  exportPathHLayout->addWidget(_exportPathLabel);
83  _exportPathLabel->setFixedWidth(200);
84 
85  _exportPathLineEdit = new QLineEdit();
86  exportPathHLayout->addWidget(_exportPathLineEdit);
87  exportPathHLayout->setStretchFactor(_exportPathLineEdit, 4);
88 
89  _exportPathConfirmButton = new QPushButton("Confirm Path");
90  exportPathHLayout->addWidget(_exportPathConfirmButton);
91  _exportPathConfirmButton->setFixedWidth(100);
92  vboxlayout->addLayout(exportPathHLayout);
93 
94  auto* exportNameHLayout = new QHBoxLayout();
95  _exportNameLabel = new QLabel("Export name:");
96  exportNameHLayout->addWidget(_exportNameLabel);
97  _exportNameLabel->setFixedWidth(200);
98 
99  _exportNameLineEdit = new QLineEdit();
100  exportNameHLayout->addWidget(_exportNameLineEdit);
101  exportNameHLayout->setStretchFactor(_exportNameLineEdit, 4);
102 
103  _exportNameConfirmButton = new QPushButton("Confirm Name");
104  exportNameHLayout->addWidget(_exportNameConfirmButton);
105  _exportNameConfirmButton->setFixedWidth(100);
106  vboxlayout->addLayout(exportNameHLayout);
107 */
108 
109  auto ltmButtonsLayout = new QVBoxLayout();
110  _storeInLTMButton = new QPushButton("Query and store in LTM");
111  ltmButtonsLayout->addWidget(_storeInLTMButton);
112 
113  auto* ltmRecHLayout = new QHBoxLayout();
114  _startLTMRecordingButton = new QPushButton("Start LTM Recording");
115  _stopLTMRecordingButton = new QPushButton("Stop LTM Recording");
116 
117  ltmRecHLayout->addWidget(_startLTMRecordingButton);
118  ltmRecHLayout->addWidget(_stopLTMRecordingButton);
119  ltmButtonsLayout->addLayout(ltmRecHLayout);
120 
121  vboxlayout->addLayout(ltmButtonsLayout);
122 
123  hlayout->addWidget(_additionalSettingsGroupBox);
124  }
125  vlayout->addLayout(hlayout);
126 
127  // Public connections.
128  /*
129  connect(_exportPathConfirmButton, &QPushButton::pressed, this, [=]() {
130  confirmExportPath(_exportPathLineEdit->text().toStdString());
131  });
132  connect(_exportNameConfirmButton, &QPushButton::pressed, this, [=]() {
133  confirmExportPath(_exportNameLineEdit->text().toStdString());
134  });
135 */
136  connect(_storeInLTMButton, &QPushButton::pressed, this, &This::storeInLTM);
137  connect(_startLTMRecordingButton, &QPushButton::pressed, this, &This::startRecording);
138  connect(_stopLTMRecordingButton, &QPushButton::pressed, this, &This::stopRecording);
139 
140  // Private connections.
141  connect(
142  _dataCheckBox, &QCheckBox::stateChanged, this, &This::setRecursionDepthSpinnerEnabled);
143 
144  connect(_de_selectMemoryServers, &QPushButton::pressed, this, &This::deSelectMemoryServers);
145 
146  setLayout(vlayout);
147  }
148 
149  void
150  QueryWidget::update(const std::vector<std::string>& activeMemoryNames)
151  {
152  std::scoped_lock l(enabledMemoriesMutex);
153  std::vector<std::string> alreadyPresentMemoryCheckboxes;
154 
155  // get information about memories already present and activate inactive ones if necessary
156  int maxIndex = _availableMemoriesGroupBox->layout()->count(); //as the (de)select button is counted as well
157  for (int i = 1; i < maxIndex; ++i)
158  {
159  auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
160  QCheckBox* box = static_cast<QCheckBox*>(w);
161  std::string memoryName = box->text().toStdString();
162  if (box->isEnabled() &&
163  std::find(activeMemoryNames.begin(), activeMemoryNames.end(), memoryName) ==
164  activeMemoryNames.end())
165  {
166  // checkbox is enabled but memory is not available anymore
167  box->setVisible(false);
168  box->setChecked(false);
169  box->setEnabled(false);
170  }
171  if (not(box->isEnabled()) &&
172  std::find(activeMemoryNames.begin(), activeMemoryNames.end(), memoryName) !=
173  activeMemoryNames.end())
174  {
175  // checkbox is disabled but memory is available again
176  box->setVisible(true);
177  box->setChecked(false);
178  box->setEnabled(true);
179  }
180  alreadyPresentMemoryCheckboxes.push_back(memoryName);
181  }
182 
183  // Add checkboxes for new memories
184  for (const auto& memoryName : activeMemoryNames)
185  {
186  if (std::find(alreadyPresentMemoryCheckboxes.begin(),
187  alreadyPresentMemoryCheckboxes.end(),
188  memoryName) == alreadyPresentMemoryCheckboxes.end())
189  {
190  // new memory available
191  auto box = new QCheckBox(QString::fromStdString(memoryName));
192  box->setChecked(false); // we do not want all memories to be enabled on startup, to reduce communication overhead
193  box->setVisible(true);
194  box->setEnabled(true);
195  _availableMemoriesGroupBox->layout()->addWidget(box);
196  }
197  }
198  }
199 
202  {
203  return _dataCheckBox->isChecked() ? armem::query::DataMode::WithData
205  }
206 
207  bool
209  {
210  return _dropRemovedCheckBox->isChecked();
211  }
212 
213  bool
215  {
216  return _dropDisabledCheckBox->isChecked();
217  }
218 
219  std::map<std::string, QueryWidget::ActiveMemoryState>
221  {
222  std::scoped_lock l(enabledMemoriesMutex);
223 
224  std::map<std::string, QueryWidget::ActiveMemoryState> states;
225  int maxIndex = _availableMemoriesGroupBox->layout()->count();
226  for (int i = 1; i < maxIndex; ++i)
227  {
228  auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
229  QCheckBox* box = static_cast<QCheckBox*>(w);
230  std::string memoryName = box->text().toStdString();
231  if (box->isEnabled() && box->isChecked())
232  {
234  }
235  else if (box->isEnabled() && !box->isChecked())
236  {
238  }
239  else
240  {
242  }
243  }
244  return states;
245  }
246 
247  std::vector<std::string>
249  {
250  std::scoped_lock l(enabledMemoriesMutex);
251 
252  std::vector<std::string> enabledMemoryCheckboxes;
253  int maxIndex = _availableMemoriesGroupBox->layout()->count();
254  for (int i = 1; i < maxIndex; ++i)
255  {
256  auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
257  QCheckBox* box = static_cast<QCheckBox*>(w);
258  std::string memoryName = box->text().toStdString();
259  if (box->isEnabled() && box->isChecked())
260  {
261  // Invisible ones are always unchecked if set to invisible from update method
262  enabledMemoryCheckboxes.push_back(memoryName);
263  }
264  }
265  return enabledMemoryCheckboxes;
266  }
267 
268  int
270  {
271  return _recursionDepthSpinner->value();
272  }
273 
274  void
275  QueryWidget::setRecursionDepthSpinnerEnabled(int state)
276  {
277  switch (state)
278  {
279  case Qt::Checked:
280  _recursionDepthSpinner->setEnabled(true);
281  break;
282  case Qt::Unchecked:
283  default:
284  _recursionDepthSpinner->setEnabled(false);
285  break;
286  }
287  }
288 
289  void
290  QueryWidget::deSelectMemoryServers()
291  {
292  if(!allMemoryServersSelected){
293  //ARMARX_INFO << "Selecting all memory servers";
294  //not all servers are selected -> select all memory servers:
295  std::scoped_lock l(enabledMemoriesMutex);
296 
297  // get information about memories already present and activate inactive ones if necessary
298  int maxIndex = _availableMemoriesGroupBox->layout()->count(); //as the (de)select button is counted as well
299  for (int i = 1; i < maxIndex; ++i)
300  {
301  auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
302  QCheckBox* box = static_cast<QCheckBox*>(w);
303  std::string memoryName = box->text().toStdString();
304  box->setChecked(true);
305  }
306  //set variables and text:
307  allMemoryServersSelected = true;
308  _de_selectMemoryServers->setText(deselectText);
309 
310  } else {
311  //ARMARX_INFO << "Deselecting all memory servers";
312  //memories should be deselected:
313  std::scoped_lock l(enabledMemoriesMutex);
314 
315  // get information about memories already present and activate inactive ones if necessary
316  int maxIndex = _availableMemoriesGroupBox->layout()->count(); //as the (de)select button is counted as well
317  for (int i = 1; i < maxIndex; ++i)
318  {
319  auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
320  QCheckBox* box = static_cast<QCheckBox*>(w);
321  box->setChecked(false);
322  }
323 
324  allMemoryServersSelected = false;
325  _de_selectMemoryServers->setText(selectText);
326  }
327  }
328 
329 } // namespace armarx::armem::gui
armarx::armem::gui::QueryWidget::storeInLTM
void storeInLTM()
armarx::armem::query::DataMode::WithData
@ WithData
Get structure and ARON data.
armarx::armem::gui::QueryWidget::QueryWidget
QueryWidget()
Definition: QueryWidget.cpp:23
armarx::armem::gui::QueryWidget::stopRecording
void stopRecording()
armarx::armem::gui::QueryWidget::ActiveMemoryState::NotFound
@ NotFound
armarx::armem::gui::QueryWidget::ActiveMemoryState::FoundAndNotChecked
@ FoundAndNotChecked
armarx::armem::query::DataMode
DataMode
Definition: DataMode.h:7
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::armem::gui::QueryWidget::update
void update(const std::vector< std::string > &memoryNames)
Definition: QueryWidget.cpp:150
QueryWidget.h
ExpressionException.h
armarx::armem::gui::QueryWidget::getAvailableMemoryStates
std::map< std::string, ActiveMemoryState > getAvailableMemoryStates() const
Definition: QueryWidget.cpp:220
armarx::armem::gui::QueryWidget::startRecording
void startRecording()
armarx::armem::gui::QueryWidget::dropRemovedMemories
bool dropRemovedMemories() const
Definition: QueryWidget.cpp:208
armarx::armem::gui::QueryWidget::getEnabledMemories
std::vector< std::string > getEnabledMemories() const
Definition: QueryWidget.cpp:248
armarx::armem::gui::QueryWidget::ActiveMemoryState::FoundAndChecked
@ FoundAndChecked
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::armem::gui
Definition: ActionsMenuBuilder.cpp:6
armarx::armem::query::DataMode::NoData
@ NoData
Just get the structure, but no ARON data.
armarx::armem::gui::QueryWidget::queryLinkRecursionDepth
int queryLinkRecursionDepth() const
Definition: QueryWidget.cpp:269
armarx::armem::gui::QueryWidget::dropDisabledMemories
bool dropDisabledMemories() const
Definition: QueryWidget.cpp:214
armarx::armem::gui::QueryWidget::dataMode
armem::query::DataMode dataMode() const
Definition: QueryWidget.cpp:201