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 
36 namespace armarx::armem::gui
37 {
39  memoryEntity(new QLineEdit()),
40  timestampInputSelector(new QComboBox()),
41  timestampLayout(new QHBoxLayout()),
42  instanceSpinner(new QSpinBox()),
43  predictionEngineSelector(new QComboBox()),
44  predictButton(new QPushButton("Predict")),
45  entityInfoRetriever(entityInfoRetriever)
46  {
47  auto* vlayout = new QVBoxLayout();
48  auto* hlayout = new QHBoxLayout();
49  hlayout->addWidget(new QLabel("Entity ID"));
50  hlayout->addWidget(memoryEntity);
51  vlayout->addLayout(hlayout);
52 
53  timestampInputSelector->addItems({"Absolute", "Relative to now"});
54  timestampLayout->addWidget(new QLabel("Time"));
55  timestampLayout->addWidget(timestampInputSelector);
56  vlayout->addLayout(timestampLayout);
57 
58  hlayout = new QHBoxLayout();
59  predictionEngineSelector->setSizeAdjustPolicy(QComboBox::AdjustToContents);
60  hlayout->addWidget(new QLabel("Engine"));
61  hlayout->addWidget(predictionEngineSelector);
62  hlayout->addStretch();
63  vlayout->addLayout(hlayout);
64 
65  vlayout->addWidget(predictButton);
66 
67  addTimestampInputMethod("Absolute", new AbsoluteTimestampInput());
68  addTimestampInputMethod("Relative to now", new RelativeTimestampInput());
69  timestampLayout->addStretch();
70 
71  setLayout(vlayout);
72 
73  showTimestampInputMethod("Absolute");
74 
75  connect(timestampInputSelector,
76  &QComboBox::currentTextChanged,
77  this,
78  &PredictionWidget::showTimestampInputMethod);
79 
80  connect(memoryEntity,
81  &QLineEdit::editingFinished,
82  this,
83  &PredictionWidget::updateCurrentEntity);
84 
85  connect(predictButton, &QPushButton::clicked, this, &PredictionWidget::startPrediction);
86  }
87 
88  void
89  PredictionWidget::addTimestampInputMethod(const QString& key, TimestampInput* input)
90  {
91  timestampInputs.emplace(key, input);
92  timestampLayout->addWidget(input);
93  }
94 
95  void
96  PredictionWidget::showTimestampInputMethod(const QString& key) // NOLINT
97  {
98  for (const auto& [inputKey, input] : timestampInputs)
99  {
100  input->setVisible(key == inputKey);
101  }
102  }
103 
104  void
105  PredictionWidget::updateCurrentEntity()
106  {
107  MemoryID entityID = MemoryID::fromString(memoryEntity->text().toStdString());
108  predictionEngineSelector->clear();
109  if (!entityID.hasGap() && entityID.hasEntityName())
110  {
111  auto info = entityInfoRetriever(entityID);
112  currentType = info.type;
113  currentEngines = info.engines;
114  for (const auto& engine : info.engines)
115  {
116  predictionEngineSelector->addItem(QString::fromStdString(engine.engineID));
117  }
118  }
119  else
120  {
121  currentType.reset();
122  currentEngines.clear();
123  }
124  }
125 
126  void
127  PredictionWidget::startPrediction()
128  {
129  MemoryID entityID = MemoryID::fromString(memoryEntity->text().toStdString());
130  armarx::DateTime timestamp;
131  for (const auto& [inputKey, input] : timestampInputs)
132  {
133  if (input->isVisible())
134  {
135  timestamp = input->retrieveTimeStamp();
136  }
137  }
138  std::string engineID = predictionEngineSelector->currentText().toStdString();
139  emit makePrediction(entityID, currentType, timestamp, engineID);
140  }
141 } // namespace armarx::armem::gui
armarx::armem::gui::TimestampInput
Definition: TimestampInput.h:39
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
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:63
armarx::armem::MemoryID::fromString
static MemoryID fromString(const std::string &string)
Alias for constructor from string.
Definition: MemoryID.cpp:183
armarx::armem::gui::PredictionWidget::GetEntityInfoFn
std::function< EntityInfo(const MemoryID &)> GetEntityInfoFn
Definition: PredictionWidget.h:57
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::gui
Definition: ActionsMenuBuilder.cpp:6
armarx::armem::gui::PredictionWidget::PredictionWidget
PredictionWidget(GetEntityInfoFn &&entityInfoRetriever)
Definition: PredictionWidget.cpp:38
armarx::armem::gui::AbsoluteTimestampInput
Definition: TimestampInput.h:48
TimestampInput.h
PredictionWidget.h
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29