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