LTMWidget.cpp
Go to the documentation of this file.
1#include "LTMWidget.h"
2
3#include <QCheckBox>
4#include <QHBoxLayout>
5#include <QLineEdit>
6#include <QPushButton>
7#include <QTabWidget>
8#include <QTextEdit>
9#include <QVBoxLayout>
10#include <QWidget>
11#include <QScrollArea>
12#include <QGroupBox>
13
15
16namespace armarx::armem::gui
17{
18
20 {
21 auto* vlayout = new QVBoxLayout();
22 auto* hlayout = new QHBoxLayout();
23
24 {
25 _availableMemoriesGroupBox = new QGroupBox("Available memories");
26 auto vboxlayout = new QVBoxLayout();
27 vboxlayout->setMargin(1);
28 vboxlayout->setSizeConstraint(QLayout::SizeConstraint::SetMinAndMaxSize);
29 vboxlayout->setAlignment(Qt::AlignTop);
30 _de_selectMemoryServers = new QPushButton(_selectText);
31 _de_selectMemoryServers->setEnabled(false);
32 vboxlayout->addWidget(_de_selectMemoryServers);
33 _availableMemoriesGroupBox->setLayout(vboxlayout);
34
35 // make _availableMemoriesGroupBox scrollable
36 _availableMemoriesScrollArea = new QScrollArea();
37 _availableMemoriesScrollArea->setWidget(_availableMemoriesGroupBox);
38 _availableMemoriesScrollArea->setWidgetResizable(true);
39
40 hlayout->addWidget(_availableMemoriesScrollArea, 1);
41 }
42
43 QFrame* vFrame = new QFrame;
44 vFrame->setFrameShape(QFrame::VLine);
45 hlayout->addWidget(vFrame);
46
47 {
48 auto ltmButtonsGroupBox = new QGroupBox("LTM recording");
49 auto ltmButtonsLayout = new QVBoxLayout();
50 ltmButtonsLayout->setMargin(9);
51 ltmButtonsLayout->setSizeConstraint(QLayout::SizeConstraint::SetMinAndMaxSize);
52 ltmButtonsLayout->setAlignment(Qt::AlignTop);
53 ltmButtonsGroupBox->setLayout(ltmButtonsLayout);
54
55 _storeInLTMButton = new QPushButton("Query and store in LTM");
56 ltmButtonsLayout->addWidget(_storeInLTMButton);
57
58 auto* ltmRecHLayout = new QHBoxLayout();
59 _startLTMRecordingButton = new QPushButton("Start LTM Recording");
60 _stopLTMRecordingButton = new QPushButton("Stop LTM Recording");
61 ltmRecHLayout->addWidget(_startLTMRecordingButton);
62 ltmRecHLayout->addWidget(_stopLTMRecordingButton);
63
64 ltmButtonsLayout->addLayout(ltmRecHLayout);
65
66 hlayout->addWidget(ltmButtonsGroupBox, 1);
67
68 }
69 vlayout->addLayout(hlayout);
70
71 // Private connects
72 connect(_de_selectMemoryServers, &QPushButton::pressed, this, &This::deSelectMemoryServers);
73
74 // Public connections.
75 connect(_storeInLTMButton, &QPushButton::pressed, this, &This::storeInLTM);
76 connect(_startLTMRecordingButton, &QPushButton::pressed, this, &This::startRecording);
77 connect(_stopLTMRecordingButton, &QPushButton::pressed, this, &This::stopRecording);
78
79 setLayout(vlayout);
80 }
81
82 void
83 LTMWidget::update(const std::vector<std::string>& activeMemoryNames)
84 {
85
86 std::vector<std::string> alreadyPresentMemoryCheckboxes;
87
88 // get information about memories already present and activate inactive ones if necessary
89 int maxIndex = _availableMemoriesGroupBox->layout()
90 ->count(); //as the (de)select button is counted as well
91
92 for (int i = 1; i < maxIndex; ++i)
93 {
94 auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
95 QCheckBox* box = static_cast<QCheckBox*>(w);
96 std::string memoryName = box->text().toStdString();
97 if (box->isEnabled() &&
98 std::find(activeMemoryNames.begin(), activeMemoryNames.end(), memoryName) ==
99 activeMemoryNames.end())
100 {
101 // checkbox is enabled but memory is not available anymore
102 box->setVisible(false);
103 box->setChecked(false);
104 box->setEnabled(false);
105 }
106 if (not(box->isEnabled()) &&
107 std::find(activeMemoryNames.begin(), activeMemoryNames.end(), memoryName) !=
108 activeMemoryNames.end())
109 {
110 // checkbox is disabled but memory is available again
111 box->setVisible(true);
112 box->setChecked(false);
113 box->setEnabled(true);
114 }
115 alreadyPresentMemoryCheckboxes.push_back(memoryName);
116 }
117
118
119 // Add checkboxes for new memories
120 for (const auto& memoryName : activeMemoryNames)
121 {
122 if (std::find(alreadyPresentMemoryCheckboxes.begin(),
123 alreadyPresentMemoryCheckboxes.end(),
124 memoryName) == alreadyPresentMemoryCheckboxes.end())
125 {
126 // new memory available
127 auto box = new QCheckBox(QString::fromStdString(memoryName));
128 box->setChecked(
129 false); // we do not want all memories to be enabled on startup, to reduce communication overhead
130 box->setVisible(true);
131 box->setEnabled(true);
132 _availableMemoriesGroupBox->layout()->addWidget(box);
133
134 // necessary to select the correct select/deselect button
135 connect(box, &QCheckBox::stateChanged, this, &This::updateSelectAllButtonState);
136 }
137 }
138
139 // activate/deactivate button
140 _de_selectMemoryServers->setEnabled(!activeMemoryNames.empty());
141
142 }
143
144 std::vector<std::string>
146 {
147 int maxIndex = _availableMemoriesGroupBox->layout()->count();
148 std::vector<std::string> memoryNames;
149 memoryNames.reserve(maxIndex);
150 for (int i = 1; i < maxIndex; ++i)
151 {
152 auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
153 QCheckBox* box = static_cast<QCheckBox*>(w);
154 std::string memoryName = box->text().toStdString();
155 if (box->isEnabled() && box->isChecked())
156 {
157 memoryNames.push_back(memoryName);
158 }
159 }
160 return memoryNames;
161 }
162
163 void
164 LTMWidget::deSelectMemoryServers()
165 {
166 if (!_allMemoryServersSelected)
167 {
168
169 // get information about memories already present and activate inactive ones if necessary
170 int maxIndex = _availableMemoriesGroupBox->layout()
171 ->count(); //as the (de)select button is counted as well
172 for (int i = 1; i < maxIndex; ++i)
173 {
174 auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
175 QCheckBox* box = static_cast<QCheckBox*>(w);
176 box->setChecked(true);
177 }
178 //set variables and text:
179 _allMemoryServersSelected = true;
180 _de_selectMemoryServers->setText(_deselectText);
181 }
182 else
183 {
184
185 // get information about memories already present and activate inactive ones if necessary
186 int maxIndex = _availableMemoriesGroupBox->layout()
187 ->count(); //as the (de)select button is counted as well
188 for (int i = 1; i < maxIndex; ++i)
189 {
190 auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
191 QCheckBox* box = static_cast<QCheckBox*>(w);
192 box->setChecked(false);
193 }
194
195 _allMemoryServersSelected = false;
196 _de_selectMemoryServers->setText(_selectText);
197 }
198 }
199
200 void
201 LTMWidget::updateSelectAllButtonState()
202{
203
204 int maxIndex = _availableMemoriesGroupBox->layout()->count();
205 bool allSelected = true;
206
207 for (int i = 1; i < maxIndex; ++i)
208 {
209 auto w = _availableMemoriesGroupBox->layout()->itemAt(i)->widget();
210 QCheckBox* box = static_cast<QCheckBox*>(w);
211
212 if (box->isEnabled() && !box->isChecked())
213 {
214 allSelected = false;
215 break;
216 }
217 }
218
219 _allMemoryServersSelected = allSelected;
220 _de_selectMemoryServers->setText(allSelected ? _deselectText : _selectText);
221}
222
223} // namespace armarx::armem::gui
std::vector< std::string > getEnabledLTMMemories() const
void update(const std::vector< std::string > &memoryNames)
Definition LTMWidget.cpp:83