NewEntityIdDialog.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 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
17 * @date 2021
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#include "NewEntityIdDialog.h"
23
24#include <memory>
25
26#include <QDialogButtonBox>
27#include <QGridLayout>
28#include <QLabel>
29#include <QLineEdit>
30#include <QPushButton>
31#include <QVBoxLayout>
32#include <qboxlayout.h>
33#include <qchar.h>
34#include <qcombobox.h>
35#include <qdialog.h>
36#include <qfont.h>
37#include <qgridlayout.h>
38#include <qlabel.h>
39#include <qobject.h>
40#include <qwidget.h>
41
44#include <ArmarXCore/interface/core/UserException.h>
45
47
48#include <range/v3/algorithm/contains.hpp>
49
51{
52
54 const std::vector<std::string>& providers,
55 QWidget* parent) :
56 QDialog(parent), coreSegmentID(std::make_unique<armem::MemoryID>(providerSegmentID))
57 {
58
59 QWidget* activeProviderWidget = nullptr;
60
61 if (providers.empty())
62 {
63 ARMARX_INFO << "No providers available. Will use free text.";
64 _providerSegmentName = new QLineEdit(this);
65
66 _providerSegmentName->setText(
67 QString::fromStdString(providerSegmentID.providerSegmentName));
68
69 activeProviderWidget = _providerSegmentName;
70 }
71 else
72 {
73 ARMARX_INFO << "Providers available. Will use dropdown list.";
74
75 _providerSegmentNameSelection = new QComboBox(this);
76
77 // add items
78 for (const auto& provider : providers)
79 {
80 _providerSegmentNameSelection->addItem(QString::fromStdString(provider));
81 }
82
83 // set default item if applicable
84 if (ranges::contains(providers, providerSegmentID.providerSegmentName))
85 {
86 _providerSegmentNameSelection->setCurrentText(
87 QString::fromStdString(providerSegmentID.providerSegmentName));
88 }
89 activeProviderWidget = _providerSegmentNameSelection;
90 }
91
92
93 _entityName = new QLineEdit(this);
94
95 _buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
96
97 QLabel* instruction =
98 new QLabel("Enter provider segment name and entity name for new entity:");
99 QFont font = instruction->font();
100 font.setBold(true);
101 instruction->setFont(font);
102
103 QGridLayout* grid = new QGridLayout();
104 int col = 0;
105
106 grid->addWidget(new QLabel("Core Segment ID"), 0, col);
107 grid->addWidget(
108 new QLabel(QString::fromStdString(providerSegmentID.getCoreSegmentID().str()) + "/"),
109 1,
110 col);
111 ++col;
112
113 grid->addWidget(new QLabel("Provider Segment Name"), 0, col);
114 ARMARX_CHECK_NOT_NULL(activeProviderWidget);
115 grid->addWidget(activeProviderWidget, 1, col);
116 ++col;
117
118 grid->addWidget(new QLabel("/"), 1, col);
119 ++col;
120
121 grid->addWidget(new QLabel("Entity Name"), 0, col);
122 grid->addWidget(_entityName, 1, col);
123 ++col;
124
125
126 QVBoxLayout* layout = new QVBoxLayout();
127 setLayout(layout);
128 layout->addWidget(instruction);
129 layout->addLayout(grid);
130 layout->addWidget(_buttonBox);
131
132
133 connect(_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
134 connect(_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
135
136 auto enableOkIfReady = [this]()
137 {
138 bool hasValidProviderName = false;
139 if (_providerSegmentNameSelection != nullptr)
140 {
141 hasValidProviderName = true;
142 }
143
144 if (_providerSegmentName != nullptr)
145 {
146 hasValidProviderName = not _providerSegmentName->text().isEmpty();
147 }
148
149 const bool hasEntity = not _entityName->text().isEmpty();
150
151 _buttonBox->button(QDialogButtonBox::Ok)
152 ->setEnabled(hasValidProviderName and hasEntity);
153 };
154
155 if (_providerSegmentName != nullptr)
156 {
157 connect(_providerSegmentName, &QLineEdit::textChanged, this, enableOkIfReady);
158 }
159
160 connect(_entityName, &QLineEdit::textChanged, this, enableOkIfReady);
161
162 enableOkIfReady();
163 }
164
168
169 QString
171 {
172 if (_providerSegmentName != nullptr)
173 {
174 return _providerSegmentName->text();
175 }
176
177 if (_providerSegmentNameSelection != nullptr)
178 {
179 return _providerSegmentNameSelection->currentText();
180 }
181
182 throw InvalidArgumentException();
183 }
184
185 QString
187 {
188 return _entityName->text();
189 }
190
193 {
195 id.providerSegmentName = providerSegmentName().toStdString();
196 id.entityName = entityName().toStdString();
197 return id;
198 }
199
202 {
204 ARMARX_CHECK_NOT_NULL(coreSegmentID);
205 entityID.setCoreSegmentID(*coreSegmentID);
206 return entityID;
207 }
208
209} // namespace armarx::navigation::qt_plugins::location_graph_editor
if(!yyvaluep)
Definition Grammar.cpp:645
std::string providerSegmentName
Definition MemoryID.h:52
NewEntityIdDialog(const armem::MemoryID &coreSegmentID, const std::vector< std::string > &providers, QWidget *parent=nullptr)
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...