PredictionWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::armem::gui
17  * @author phesch ( ulila at student dot kit dot edu )
18  * @date 2022
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "PredictionWidget.h"
24 
25 #include <QComboBox>
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QPushButton>
30 #include <QSpinBox>
31 #include <QVBoxLayout>
32 
33 #include "TimestampInput.h"
34 
35 namespace armarx::armem::gui
36 {
38  memoryEntity(new QLineEdit()),
39  timestampInputSelector(new QComboBox()),
40  timestampLayout(new QHBoxLayout()),
41  instanceSpinner(new QSpinBox()),
42  predictionEngineSelector(new QComboBox()),
43  predictButton(new QPushButton("Predict")),
44  entityInfoRetriever(entityInfoRetriever)
45  {
46  auto* vlayout = new QVBoxLayout();
47  auto* hlayout = new QHBoxLayout();
48  hlayout->addWidget(new QLabel("Entity ID"));
49  hlayout->addWidget(memoryEntity);
50  vlayout->addLayout(hlayout);
51 
52  timestampInputSelector->addItems({"Absolute", "Relative to now"});
53  timestampLayout->addWidget(new QLabel("Time"));
54  timestampLayout->addWidget(timestampInputSelector);
55  vlayout->addLayout(timestampLayout);
56 
57  hlayout = new QHBoxLayout();
58  predictionEngineSelector->setSizeAdjustPolicy(QComboBox::AdjustToContents);
59  hlayout->addWidget(new QLabel("Engine"));
60  hlayout->addWidget(predictionEngineSelector);
61  hlayout->addStretch();
62  vlayout->addLayout(hlayout);
63 
64  vlayout->addWidget(predictButton);
65 
66  addTimestampInputMethod("Absolute", new AbsoluteTimestampInput());
67  addTimestampInputMethod("Relative to now", new RelativeTimestampInput());
68  timestampLayout->addStretch();
69 
70  setLayout(vlayout);
71 
72  showTimestampInputMethod("Absolute");
73 
74  connect(timestampInputSelector,
75  &QComboBox::currentTextChanged,
76  this,
77  &PredictionWidget::showTimestampInputMethod);
78 
79  connect(memoryEntity,
80  &QLineEdit::editingFinished,
81  this,
82  &PredictionWidget::updateCurrentEntity);
83 
84  connect(predictButton, &QPushButton::clicked, this, &PredictionWidget::startPrediction);
85  }
86 
87  void
88  PredictionWidget::addTimestampInputMethod(const QString& key, TimestampInput* input)
89  {
90  timestampInputs.emplace(key, input);
91  timestampLayout->addWidget(input);
92  }
93 
94  void
95  PredictionWidget::showTimestampInputMethod(const QString& key) // NOLINT
96  {
97  for (const auto& [inputKey, input] : timestampInputs)
98  {
99  input->setVisible(key == inputKey);
100  }
101  }
102 
103  void
104  PredictionWidget::updateCurrentEntity()
105  {
106  MemoryID entityID = MemoryID::fromString(memoryEntity->text().toStdString());
107  predictionEngineSelector->clear();
108  if (!entityID.hasGap() && entityID.hasEntityName())
109  {
110  auto info = entityInfoRetriever(entityID);
111  currentType = info.type;
112  currentEngines = info.engines;
113  for (const auto& engine : info.engines)
114  {
115  predictionEngineSelector->addItem(QString::fromStdString(engine.engineID));
116  }
117  }
118  else
119  {
120  currentType.reset();
121  currentEngines.clear();
122  }
123  }
124 
125  void
126  PredictionWidget::startPrediction()
127  {
128  MemoryID entityID = MemoryID::fromString(memoryEntity->text().toStdString());
129  armarx::DateTime timestamp;
130  for (const auto& [inputKey, input] : timestampInputs)
131  {
132  if (input->isVisible())
133  {
134  timestamp = input->retrieveTimeStamp();
135  }
136  }
137  std::string engineID = predictionEngineSelector->currentText().toStdString();
138  emit makePrediction(entityID, currentType, timestamp, engineID);
139  }
140 } // namespace armarx::armem::gui
armarx::armem::gui::TimestampInput
Definition: TimestampInput.h:40
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
armarx::armem::gui::PredictionWidget::makePrediction
void makePrediction(const MemoryID &entityID, const aron::type::ObjectPtr &entityType, const armarx::DateTime &timestamp, const std::string &engineID)
armarx::armem::gui::RelativeTimestampInput
Definition: TimestampInput.h:62
armarx::armem::MemoryID::fromString
static MemoryID fromString(const std::string &string)
Alias for constructor from string.
Definition: MemoryID.cpp:188
armarx::armem::gui::PredictionWidget::GetEntityInfoFn
std::function< EntityInfo(const MemoryID &)> GetEntityInfoFn
Definition: PredictionWidget.h:55
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::gui
Definition: ActionsMenuBuilder.cpp:5
armarx::armem::gui::PredictionWidget::PredictionWidget
PredictionWidget(GetEntityInfoFn &&entityInfoRetriever)
Definition: PredictionWidget.cpp:37
armarx::armem::gui::AbsoluteTimestampInput
Definition: TimestampInput.h:49
TimestampInput.h
PredictionWidget.h
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:28