Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3 : // Fork baseline commit: dbe-02-12-17 (2022-05-12).
4 : // Renamed since fork: yes (from src/SchemaEditor/SchemaAttributeEditor.cpp to
5 : // apps/SchemaEditor/SchemaAttributeEditor.cpp).
6 :
7 : /// Including schema
8 : #include "dbe/SchemaAttributeEditor.hpp"
9 : #include "dbe/SchemaKernelWrapper.hpp"
10 : /// Including auto-generated ui
11 : #include "ui_SchemaAttributeEditor.h"
12 :
13 : using namespace dunedaq::oks;
14 :
15 0 : dbse::SchemaAttributeEditor::SchemaAttributeEditor(OksClass* ClassInfo, OksAttribute* AttributeData, QWidget* parent)
16 : : QWidget(parent)
17 0 : , ui(new Ui::SchemaAttributeEditor)
18 0 : , SchemaClass(ClassInfo)
19 0 : , SchemaAttribute(AttributeData)
20 0 : , UsedNew(false)
21 0 : , m_writable(KernelWrapper::GetInstance().IsFileWritable(SchemaClass->get_file()->get_full_file_name()))
22 : {
23 0 : QWidget::setAttribute(Qt::WA_DeleteOnClose);
24 0 : ui->setupUi(this);
25 0 : InitialSettings();
26 0 : SetController();
27 0 : }
28 :
29 0 : dbse::SchemaAttributeEditor::SchemaAttributeEditor(OksClass* ClassInfo, QWidget* parent)
30 : : QWidget(parent)
31 0 : , ui(new Ui::SchemaAttributeEditor)
32 0 : , SchemaClass(ClassInfo)
33 0 : , SchemaAttribute(nullptr)
34 0 : , UsedNew(true)
35 0 : , m_writable(KernelWrapper::GetInstance().IsFileWritable(SchemaClass->get_file()->get_full_file_name()))
36 : {
37 0 : QWidget::setAttribute(Qt::WA_DeleteOnClose);
38 0 : ui->setupUi(this);
39 0 : InitialSettings();
40 0 : SetController();
41 0 : }
42 :
43 0 : dbse::SchemaAttributeEditor::~SchemaAttributeEditor() = default;
44 :
45 : void
46 0 : dbse::SchemaAttributeEditor::FillInfo()
47 : {
48 0 : auto name = QString::fromStdString(SchemaClass->get_name() + "::" + SchemaAttribute->get_name());
49 0 : setWindowTitle(name);
50 0 : setObjectName(name);
51 0 : ui->AttributeNameLineEdit->setText(QString::fromStdString(SchemaAttribute->get_name()));
52 0 : ui->AttributeTypeComboBox->setCurrentIndex(
53 0 : ui->AttributeTypeComboBox->findData(QString::fromStdString(SchemaAttribute->get_type()), Qt::DisplayRole));
54 :
55 0 : if (SchemaAttribute->get_is_multi_values())
56 0 : ui->AttributeIsMultivariable->setCurrentIndex(0);
57 : else {
58 0 : ui->AttributeIsMultivariable->setCurrentIndex(1);
59 : }
60 :
61 0 : if (SchemaAttribute->get_is_no_null()) {
62 0 : ui->AttributeIsNotNull->setCurrentIndex(0);
63 : } else {
64 0 : ui->AttributeIsNotNull->setCurrentIndex(1);
65 : }
66 :
67 0 : ui->AttributeDescriptionTextBox->setPlainText(QString::fromStdString(SchemaAttribute->get_description()));
68 0 : ui->AttributeRangeLineEdit->setText(QString::fromStdString(SchemaAttribute->get_range()));
69 0 : ui->AttributeInitialValue->setText(QString::fromStdString(SchemaAttribute->get_init_value()));
70 :
71 0 : int Index = ui->AttributeTypeComboBox->currentIndex();
72 :
73 0 : if (Index == 0 || Index >= 9) {
74 0 : ui->FormatLayout->setEnabled(false);
75 0 : ui->FormatLabel->hide();
76 0 : ui->AttributeFormatComboBox->hide();
77 : } else {
78 0 : if (m_writable) {
79 0 : ui->FormatLayout->setEnabled(true);
80 : }
81 0 : ui->FormatLabel->show();
82 0 : ui->AttributeFormatComboBox->show();
83 :
84 0 : if (SchemaAttribute->get_format() == OksAttribute::Dec)
85 0 : ui->AttributeFormatComboBox->setCurrentIndex(0);
86 0 : else if (SchemaAttribute->get_format() == OksAttribute::Oct)
87 0 : ui->AttributeFormatComboBox->setCurrentIndex(1);
88 : else {
89 0 : ui->AttributeFormatComboBox->setCurrentIndex(2);
90 : }
91 : }
92 0 : }
93 :
94 : void
95 0 : dbse::SchemaAttributeEditor::keyPressEvent(QKeyEvent* event)
96 : {
97 0 : if (event->key() == Qt::Key_Escape) {
98 0 : close();
99 : }
100 0 : QWidget::keyPressEvent(event);
101 0 : }
102 :
103 : void
104 0 : dbse::SchemaAttributeEditor::InitialSettings()
105 : {
106 0 : QStringList Items{ "bool", "s8", "u8", "s16", "u16", "s32", "u32", "s64",
107 0 : "u64", "float", "double", "date", "time", "string", "enum", "class" };
108 0 : ui->AttributeTypeComboBox->addItems(Items);
109 0 : auto name = SchemaClass->get_name() + "::";
110 0 : setWindowTitle(QString::fromStdString(name + " New Attribute"));
111 0 : setObjectName(QString::fromStdString(name));
112 :
113 0 : if (!m_writable) {
114 0 : ui->AttributeTypeComboBox->setEnabled(false);
115 0 : ui->AttributeIsMultivariable->setEnabled(false);
116 0 : ui->AttributeIsNotNull->setEnabled(false);
117 0 : ui->AttributeRangeLineEdit->setEnabled(false);
118 0 : ui->AttributeInitialValue->setEnabled(false);
119 :
120 0 : ui->AttributeDescriptionTextBox->setReadOnly(true);
121 : }
122 :
123 0 : if (!UsedNew) {
124 0 : FillInfo();
125 : }
126 0 : }
127 :
128 : void
129 0 : dbse::SchemaAttributeEditor::SetController()
130 : {
131 0 : connect(ui->AttributeTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(ToggleFormat(int)));
132 0 : connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(ProxySlot()));
133 0 : connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(close()));
134 0 : connect(&KernelWrapper::GetInstance(), SIGNAL(ClassUpdated(QString)), this, SLOT(ClassUpdated(QString)));
135 0 : }
136 :
137 : void
138 0 : dbse::SchemaAttributeEditor::ClassUpdated(QString ClassName)
139 : {
140 0 : if (!UsedNew && ClassName.toStdString() == SchemaClass->get_name()) {
141 0 : if (SchemaClass->find_direct_attribute(SchemaAttribute->get_name()) != nullptr) {
142 0 : FillInfo();
143 : } else {
144 0 : QWidget::close();
145 : }
146 : }
147 0 : }
148 :
149 : void
150 0 : dbse::SchemaAttributeEditor::ParseToSave()
151 : {
152 0 : bool Changed = false;
153 0 : std::string AttributeName = ui->AttributeNameLineEdit->text().toStdString();
154 0 : std::string AttributeType = ui->AttributeTypeComboBox->currentText().toStdString();
155 0 : char AttributeFormatChar = (ui->AttributeFormatComboBox->currentText().toStdString())[0];
156 :
157 0 : OksAttribute::Format AttributeFormat;
158 :
159 0 : switch (AttributeFormatChar) {
160 : case 'D':
161 : AttributeFormat = OksAttribute::Format::Dec;
162 : break;
163 :
164 0 : case 'O':
165 0 : AttributeFormat = OksAttribute::Format::Oct;
166 0 : break;
167 :
168 0 : case 'H':
169 0 : AttributeFormat = OksAttribute::Format::Hex;
170 0 : break;
171 :
172 : default:
173 : AttributeFormat = OksAttribute::Format::Dec;
174 : break;
175 : }
176 :
177 : /// Making sure we dont get anything weird
178 0 : int Index = ui->AttributeTypeComboBox->currentIndex();
179 :
180 0 : if (Index == 0 || Index >= 9) {
181 0 : AttributeFormat = OksAttribute::Format::Dec;
182 : }
183 :
184 0 : bool IsMultivariable = false;
185 :
186 0 : switch (ui->AttributeIsMultivariable->currentIndex()) {
187 : case 0:
188 : IsMultivariable = true;
189 : break;
190 :
191 : case 1:
192 0 : IsMultivariable = false;
193 : break;
194 :
195 : default:
196 0 : IsMultivariable = false;
197 : break;
198 : }
199 :
200 0 : bool IsNotNull = false;
201 :
202 0 : switch (ui->AttributeIsNotNull->currentIndex()) {
203 : case 0:
204 : IsNotNull = true;
205 : break;
206 :
207 : case 1:
208 0 : IsNotNull = false;
209 : break;
210 :
211 : default:
212 0 : IsNotNull = false;
213 : break;
214 : }
215 :
216 0 : std::string AttributeDescription = ui->AttributeDescriptionTextBox->toPlainText().toStdString();
217 0 : std::string AttributeRange = ui->AttributeRangeLineEdit->text().toStdString();
218 0 : std::string AttributeInitial = ui->AttributeInitialValue->text().toStdString();
219 :
220 0 : if (AttributeName != SchemaAttribute->get_name()) {
221 0 : KernelWrapper::GetInstance().PushSetAttributeNameCommand(SchemaClass, SchemaAttribute, AttributeName);
222 0 : Changed = true;
223 : }
224 :
225 0 : if (AttributeType != SchemaAttribute->get_type()) {
226 0 : KernelWrapper::GetInstance().PushSetAttributeTypeCommand(SchemaClass, SchemaAttribute, AttributeType);
227 0 : Changed = true;
228 : }
229 :
230 0 : if (AttributeFormat != SchemaAttribute->get_format()) {
231 0 : KernelWrapper::GetInstance().PushSetAttributeFormatCommand(SchemaClass, SchemaAttribute, AttributeFormat);
232 : Changed = true;
233 : }
234 :
235 0 : if (IsMultivariable != SchemaAttribute->get_is_multi_values()) {
236 0 : KernelWrapper::GetInstance().PushSetAttributeMultiCommand(SchemaClass, SchemaAttribute, IsMultivariable);
237 : Changed = true;
238 : }
239 :
240 0 : if (IsNotNull != SchemaAttribute->get_is_no_null()) {
241 0 : KernelWrapper::GetInstance().PushSetAttributeIsNullCommand(SchemaClass, SchemaAttribute, IsNotNull);
242 : Changed = true;
243 : }
244 :
245 0 : if (AttributeDescription != SchemaAttribute->get_description()) {
246 0 : KernelWrapper::GetInstance().PushSetAttributeDescriptionCommand(SchemaClass, SchemaAttribute, AttributeDescription);
247 0 : Changed = true;
248 : }
249 :
250 0 : if (AttributeRange != SchemaAttribute->get_range()) {
251 0 : KernelWrapper::GetInstance().PushSetAttributeRangeCommand(SchemaClass, SchemaAttribute, AttributeRange);
252 0 : Changed = true;
253 : }
254 :
255 0 : if (AttributeInitial != SchemaAttribute->get_init_value()) {
256 0 : KernelWrapper::GetInstance().PushSetAttributeInitialValuesCommand(SchemaClass, SchemaAttribute, AttributeInitial);
257 0 : Changed = true;
258 : }
259 :
260 0 : if (Changed) {
261 0 : emit RebuildModel();
262 : }
263 :
264 0 : close();
265 0 : }
266 :
267 : void
268 0 : dbse::SchemaAttributeEditor::ParseToCreate()
269 : {
270 0 : std::string AttributeName = ui->AttributeNameLineEdit->text().toStdString();
271 0 : std::string AttributeType = ui->AttributeTypeComboBox->currentText().toStdString();
272 0 : char AttributeFormatChar = (ui->AttributeFormatComboBox->currentText().toStdString())[0];
273 :
274 0 : OksAttribute::Format AttributeFormat;
275 :
276 0 : switch (AttributeFormatChar) {
277 : case 'D':
278 : AttributeFormat = OksAttribute::Format::Dec;
279 : break;
280 :
281 0 : case 'O':
282 0 : AttributeFormat = OksAttribute::Format::Oct;
283 0 : break;
284 :
285 0 : case 'H':
286 0 : AttributeFormat = OksAttribute::Format::Hex;
287 0 : break;
288 :
289 : default:
290 : AttributeFormat = OksAttribute::Format::Dec;
291 : break;
292 : }
293 :
294 : /// Making sure we dont get anything weird
295 0 : int Index = ui->AttributeTypeComboBox->currentIndex();
296 :
297 0 : if (Index == 0 || Index >= 9) {
298 0 : AttributeFormat = OksAttribute::Format::Dec;
299 : }
300 :
301 0 : bool IsMultivariable = false;
302 :
303 0 : switch (ui->AttributeIsMultivariable->currentIndex()) {
304 : case 0:
305 : IsMultivariable = true;
306 : break;
307 :
308 : case 1:
309 0 : IsMultivariable = false;
310 : break;
311 :
312 : default:
313 0 : IsMultivariable = false;
314 : break;
315 : }
316 :
317 0 : bool IsNotNull = false;
318 :
319 0 : switch (ui->AttributeIsNotNull->currentIndex()) {
320 : case 0:
321 : IsNotNull = true;
322 : break;
323 :
324 : case 1:
325 0 : IsNotNull = false;
326 : break;
327 :
328 : default:
329 0 : IsNotNull = false;
330 : break;
331 : }
332 :
333 0 : std::string AttributeDescription = ui->AttributeDescriptionTextBox->toPlainText().toStdString();
334 0 : std::string AttributeRange = ui->AttributeRangeLineEdit->text().toStdString();
335 0 : std::string AttributeInitial = ui->AttributeInitialValue->text().toStdString();
336 :
337 : /// Creating attribute and updating
338 0 : KernelWrapper::GetInstance().PushAddAttributeCommand(SchemaClass,
339 : AttributeName,
340 : AttributeType,
341 : IsMultivariable,
342 : AttributeRange,
343 : AttributeInitial,
344 : AttributeDescription,
345 : IsNotNull,
346 : AttributeFormat);
347 0 : emit RebuildModel();
348 0 : close();
349 0 : }
350 :
351 : void
352 0 : dbse::SchemaAttributeEditor::ProxySlot()
353 : {
354 0 : if (UsedNew) {
355 0 : ParseToCreate();
356 : } else {
357 0 : ParseToSave();
358 : }
359 0 : }
360 :
361 : void
362 0 : dbse::SchemaAttributeEditor::ToggleFormat(int)
363 : {
364 0 : int Index = ui->AttributeTypeComboBox->currentIndex();
365 :
366 0 : if (Index == 0 || Index >= 9) {
367 0 : ui->FormatLayout->setEnabled(false);
368 0 : ui->FormatLabel->hide();
369 0 : ui->AttributeFormatComboBox->hide();
370 : } else {
371 0 : ui->FormatLayout->setEnabled(true);
372 0 : ui->FormatLabel->show();
373 0 : ui->AttributeFormatComboBox->show();
374 : }
375 0 : }
|