DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
BatchChangeWidget.cpp
Go to the documentation of this file.
2
3#include "ui_BatchChangeWidget.h"
4
5#include "dbe/MainWindow.hpp"
6#include "dbe/config_api.hpp"
10#include "dbe/messenger.hpp"
11
12#include <QApplication>
13#include <boost/scope_exit.hpp>
14
16
18
20 :
21 QWidget ( parent ),
22 ui ( new Ui::BatchChangeWidget ),
23 UseTable ( false ),
24 ClassChanged ( true ),
25 TableClass ( "" ),
26 m_filter_table ( nullptr )
27{
28 ui->setupUi ( this );
29 ui->TitleLabel->setStyleSheet (
30 "QLabel { background-color:green; color:beige; font:bold14px; border-style:outset; border-width:2px;"
31 "border-radius:10px; border-color:beige; font:bold14px; min-width:10em; padding:6px}" );
32 ui->FirstStepLabel->setStyleSheet ( "QLabel { color:#DB5816; font: bold 14px;}" );
33 ui->SecondStepLabel->setStyleSheet ( "QLabel { color:#DE8A14; font: bold 14px;}" );
34 ui->ThirdStepLabel->setStyleSheet ( "QLabel { color:#8DAA2E; font: bold 14px;}" );
35 setWindowTitle ( "Batch Change" );
36
37 QStringList ListOfClasses ( dbe::config::api::info::onclass::allnames<QStringList>() );
38 ui->ClassCombo->addItems ( ListOfClasses );
39 ui->ClassCombo->setEditable ( true );
40 ui->ClassCombo->setEditText ( "Type/Select Classes" );
41
42 ui->AttributeBox->setEnabled ( false );
43 ui->RelationshipBox->setEnabled ( false );
44 ui->NewAttributeGroupBox->setEnabled ( false );
45 ui->NewRelationshipGroupBox->setEnabled ( false );
46 ui->MatchButton->setEnabled ( false );
47 ui->FilterUid->setText ( "*" );
48 ui->AttributeValueFilter->setText ( "*" );
49
51}
52
53dbe::BatchChangeWidget::BatchChangeWidget ( bool ObjectsFromTable, QString ClassName,
54 std::vector<dref> & Objects, QWidget * parent )
55 :
56 BatchChangeWidget ( parent )
57{
58 ClassChanged = false;
59
60 TableObjects = Objects;
61 UseTable = ObjectsFromTable;
62 TableClass = ClassName;
63
64 ui->ClassCombo->setCurrentIndex ( ui->ClassCombo->findText ( TableClass ) );
65 ui->SubclassesCheckBox->setEnabled ( false );
67
68 if ( UseTable )
69 {
70 setWindowTitle ( "Batch change - Table" );
71 ui->FilterGroupBox->hide();
72 ui->Label1->hide();
73 ui->Label2->hide();
74 ui->FirstStepLabel->hide();
75 ui->SecondStepLabel->hide();
76 ui->ThirdStepLabel->hide();
77 ui->Line1->hide();
78 ui->Line2->hide();
79 ui->TitleLabel->setText ( "Batch Change of multiple objects - Table" );
80 resize ( 540, 280 );
81 }
82}
83
85{
86 connect ( ui->ClassCombo, SIGNAL ( activated ( QString ) ), this,
87 SLOT ( FillInfo ( const QString & ) ) );
88
89 connect ( ui->ChangeButton, SIGNAL ( clicked() ), this, SLOT ( MakeChanges() ) );
90
91 connect ( ui->MatchButton, SIGNAL ( clicked() ), this, SLOT ( FindMatching() ) );
92
93 connect ( ui->NewRelationshipComboBox, SIGNAL ( activated ( QString ) ), this,
94 SLOT ( EnableCheckBox ( QString ) ) );
95 connect ( ui->CancelButton, SIGNAL ( clicked() ), this, SLOT ( close() ) );
96
97 connect ( ui->RelationshipComboBox, SIGNAL ( activated ( int ) ), this,
98 SLOT ( UpdateRelationshipFilter ( int ) ) );
99
100 connect ( ui->NewRelationshipComboBox, SIGNAL ( activated ( int ) ), this,
101 SLOT ( UpdateRelationshipNewValues ( int ) ) );
102
103 MainWindow * mainwin = MainWindow::findthis();
104
105 if ( mainwin != nullptr )
106 {
107 connect ( this, SIGNAL ( sig_batch_change_start() ), mainwin,
108 SLOT ( slot_batch_change_start() ), Qt::UniqueConnection );
109 connect ( this, SIGNAL ( sig_batch_change_stop(const QList<QPair<QString, QString>>&) ), mainwin,
110 SLOT ( slot_batch_change_stop(const QList<QPair<QString, QString>>&) ),
111 Qt::UniqueConnection );
112 }
113}
114
116 if (event->key() == Qt::Key_Escape) {
117 close();
118 }
119 QWidget::keyPressEvent(event);
120}
121
122void dbe::BatchChangeWidget::filter ( std::vector<dref> & objs, const QString & cname )
123{
124
126 cname.toStdString(), false );
127 std::vector<dunedaq::conffwk::attribute_t> const & attributes = cinfo.p_attributes;
128 std::vector<dunedaq::conffwk::relationship_t> const & relations = cinfo.p_relationships;
129
130 // Filtering UID
131 std::vector<dref> filtered;
132
133 if ( !ui->FilterUid->text().isEmpty() )
134 {
135 QRegExp uidfilter ( ui->FilterUid->text() );
136 uidfilter.setPatternSyntax ( QRegExp::Wildcard );
137
138 for ( dbe::dref const & it : objs )
139 {
140
141 if ( uidfilter.exactMatch ( QString::fromStdString ( it.ref().UID() ) ) )
142 {
143 filtered.push_back ( it );
144 }
145 }
146
147 objs = filtered;
148 filtered.clear();
149 }
150
152 QRegExp attribute_filter ( ui->AttributeValueFilter->text() );
153 attribute_filter.setPatternSyntax ( QRegExp::Wildcard );
154
155 std::string selected_attribute_name = ui->AttributeComboBox->currentText().toStdString();
156
157 std::vector<dunedaq::conffwk::attribute_t>::const_iterator attrdef = std::find_if (
158 std::begin ( attributes ), std::end ( attributes ),
159 [&selected_attribute_name] ( dunedaq::conffwk::attribute_t const & attr )
160 {
161 return attr.p_name == selected_attribute_name;
162 } );
163
164 if ( attrdef != std::end ( attributes ) )
165 {
166 for ( dbe::dref const & it : objs )
167 {
168 QStringList values
170
171 for ( QString & v : values )
172 if ( attribute_filter.exactMatch ( v ) )
173 {
174 filtered.push_back ( it );
175 break;
176 }
177 }
178
179 }
180
181 objs = filtered;
182 filtered.clear();
183
184 // Filter on Relationship
185
186 std::string selected_relation_name =
187 ui->RelationshipComboBox->currentText().toStdString();
188
189 std::vector<dunedaq::conffwk::relationship_t>::const_iterator relationdef = std::find_if (
190 std::begin ( relations ), std::end ( relations ),
191 [&selected_relation_name] ( dunedaq::conffwk::relationship_t const & rel )
192 {
193 return rel.p_name == selected_relation_name;
194 } );
195
196 if ( relationdef != std::end ( relations ) )
197 {
198 QRegExp relation_filter ( ui->RelationshipValueFilter->currentText() );
199 relation_filter.setPatternSyntax ( QRegExp::Wildcard );
200
201 for ( dbe::dref const & it : objs )
202 {
203 QStringList ValueList
204 {
205 dbegraph::linked::through::relation<QStringList> (
206 it.ref(), *relationdef ) };
207
208 if ( ValueList.isEmpty() and ( ui->RelationshipValueFilter->currentText() == "*"
209 or ui->RelationshipValueFilter->currentText() == "" ) )
210 {
211 filtered.push_back ( it );
212 }
213 else
214 {
215 for ( QString const & v : ValueList )
216 if ( relation_filter.exactMatch ( v ) )
217 {
218 filtered.push_back ( it );
219 break;
220 }
221 }
222 }
223
224 }
225
226 objs = filtered;
227 filtered.clear();
228}
229
230void dbe::BatchChangeWidget::FillInfo ( const QString & Name )
231{
232 ui->AttributeBox->setEnabled ( false );
233 ui->RelationshipBox->setEnabled ( false );
234 ui->NewAttributeGroupBox->setEnabled ( false );
235 ui->NewRelationshipGroupBox->setEnabled ( false );
236
238 Name.toStdString(),
239 false );
240 const std::vector<dunedaq::conffwk::attribute_t> AttributeList = ClassInfo.p_attributes;
241 const std::vector<dunedaq::conffwk::relationship_t> RelationshipList = ClassInfo
243
244 QStringList ValueList;
245
246 for ( dunedaq::conffwk::attribute_t Attribute : AttributeList )
247 {
248 ValueList.append ( QString ( Attribute.p_name.c_str() ) );
249 }
250
251 ui->AttributeComboBox->clear();
252 ui->NewAttributeComboBox->clear();
253 ui->AttributeComboBox->addItems ( ValueList );
254 ui->NewAttributeComboBox->addItems ( ValueList );
255
256 if ( !AttributeList.empty() && !UseTable )
257 {
258 ui->AttributeBox->setEnabled ( true );
259 ui->NewAttributeGroupBox->setEnabled ( true );
260 ui->MatchButton->setEnabled ( true );
261 }
262
263 if ( !AttributeList.empty() && UseTable )
264 {
265 ui->NewAttributeGroupBox->setEnabled ( true );
266 }
267
268 ValueList.clear();
269
270 for ( dunedaq::conffwk::relationship_t Relationship : RelationshipList )
271 {
272 ValueList.append ( QString ( Relationship.p_name.c_str() ) );
273 }
274
275 ui->RelationshipComboBox->clear();
276 ui->NewRelationshipComboBox->clear();
277 ui->RelationshipComboBox->addItems ( ValueList );
278 ui->NewRelationshipComboBox->addItems ( ValueList );
279
280 if ( !RelationshipList.empty() && !UseTable )
281 {
282 ui->RelationshipBox->setEnabled ( true );
283 ui->NewRelationshipGroupBox->setEnabled ( true );
284 ui->MatchButton->setEnabled ( true );
285 }
286
287 if ( !RelationshipList.empty() && UseTable )
288 {
289 ui->NewRelationshipGroupBox->setEnabled ( true );
290 }
291
292 if ( !UseTable )
293 {
294 UpdateRelationshipFilter ( 0 );
295 UpdateRelationshipNewValues ( 0 );
296 ui->RelationshipValueFilter->setCurrentIndex ( 0 );
297 ui->RelationshipEdit->setCurrentIndex ( -1 );
298 }
299 else
300 {
301 UpdateRelationshipNewValues ( 0 );
302 ui->RelationshipEdit->setCurrentIndex ( -1 );
303 }
304
305 if ( ClassChanged )
306 {
307 UseTable = false;
308 }
309
310 ClassChanged = true;
311}
312
314{
315 std::vector<dref> Objects;
316
317 BOOST_SCOPE_EXIT(&Objects, this_)
318 {
319 QList<QPair<QString, QString>> objs;
320 for(const auto& o : Objects) {
321 objs.append(QPair<QString, QString>(QString::fromStdString(o.class_name()),
322 QString::fromStdString(o.UID())));
323 }
324
325 this_->emit sig_batch_change_stop(objs);
326
327
328 QApplication::restoreOverrideCursor();
329 this_->ui->ChangeButton->setEnabled(true);
330 confaccessor::ref().blockSignals(false);
331 }
332 BOOST_SCOPE_EXIT_END
333
334 ui->ChangeButton->setEnabled(false);
335 QApplication::setOverrideCursor(Qt::WaitCursor);
336 confaccessor::ref().blockSignals(true);
337
338 bool SubClasses = ui->SubclassesCheckBox->isChecked();
339 QString ClassName = ui->ClassCombo->currentText();
340
341 std::unique_ptr<dbe::interface::messenger::batch_guard> batchmode (
343
344 emit sig_batch_change_start();
345
346 if ( not UseTable )
347 {
348 std::vector<tref> const & tmprefs = dbe::config::api::info::onclass::objects (
349 ClassName.toStdString(), SubClasses );
350
351 std::copy ( tmprefs.begin(), tmprefs.end(), std::back_inserter ( Objects ) );
352
353 if ( not Objects.empty() )
354 {
355 filter ( Objects, ClassName );
356 }
357 }
358 else
359 {
360 Objects = TableObjects;
361 }
362
363 if ( Objects.empty() ) QMessageBox::information ( this, tr ( "Message" ),
364 tr ( "No objects match filter!" ) );
365 else
366 {
368 ClassName.toStdString(), false );
369 const std::vector<dunedaq::conffwk::attribute_t> AttributeList = ClassInfo.p_attributes;
370 const std::vector<dunedaq::conffwk::relationship_t> RelationshipList = ClassInfo
372 dunedaq::conffwk::attribute_t AttributeChange;
373 dunedaq::conffwk::relationship_t RelationshipChange;
374
375 for ( dunedaq::conffwk::attribute_t i : AttributeList )
376 if ( i.p_name == ui->NewAttributeComboBox->currentText().toStdString() )
377 {
378 AttributeChange = i;
379 break;
380 }
381
382 for ( dunedaq::conffwk::relationship_t i : RelationshipList )
383 if ( i.p_name == ui->NewRelationshipComboBox->currentText().toStdString() )
384 {
385 RelationshipChange = i;
386 break;
387 }
388
389 QStringList AttributeChangeList;
390
391 if ( !ui->AttributeEdit->text().isEmpty() ) AttributeChangeList
392 << ui->AttributeEdit->text();
393
394 for ( auto objects_iter = Objects.begin(); objects_iter != Objects.end(); ++objects_iter )
395 {
396 tref Object = objects_iter->ref();
397
398 QStringList RelationshipChangeList;
399
400 if ( ui->ChangeAtrributeCheckBox->isChecked() )
401 {
402 if ( !AttributeChangeList.isEmpty() )
403 {
404 dbe::config::api::set::attribute ( Object, AttributeChange, AttributeChangeList );
405 }
406 }
407
408 if ( ui->ChangeRelationshipCheckBox->isChecked() )
409 {
410 if ( ui->AddCheckBox->isChecked() )
411 {
412 RelationshipChangeList =
413 dbegraph::linked::through::relation<QStringList> (
414 Object, RelationshipChange );
415 }
416
417 RelationshipChangeList << ui->RelationshipEdit->currentText();
418 dbe::config::api::set::relation ( Object, RelationshipChange, RelationshipChangeList );
419 }
420 }
421 }
422}
423
425{
426 QString ClassName = ui->ClassCombo->currentText();
427 bool SubClasses = ui->SubclassesCheckBox->isChecked();
428
429 std::vector<tref> const & tmprefs = dbe::config::api::info::onclass::objects (
430 ClassName.toStdString(), SubClasses );
431
432 std::vector<dref> candidates;
433 std::copy ( tmprefs.begin(), tmprefs.end(), std::back_inserter ( candidates ) );
434
435 if ( !candidates.empty() )
436 {
437 filter ( candidates, ClassName );
438 }
439
440 if ( candidates.empty() )
441 {
442 QMessageBox::information ( this, tr ( "Message" ), tr ( "No objects match filter!" ) );
443 }
444 else
445 {
446 int NumberOfColumns = 1;
447 bool AttributeEnabled = false;
448 bool RelationshipEnabled = false;
449
450 if ( ui->AttributeComboBox->isEnabled() )
451 {
452 AttributeEnabled = true;
453 NumberOfColumns++;
454 }
455
456 if ( ui->RelationshipComboBox->isEnabled() )
457 {
458 RelationshipEnabled = true;
459 NumberOfColumns++;
460 }
461
462 m_filter_table = std::unique_ptr<QTableWidget> ( new QTableWidget() );
463 m_filter_table->setWindowTitle ( tr ( "Objects matching filter" ) );
464 m_filter_table->setColumnCount ( NumberOfColumns );
465 m_filter_table->setRowCount ( candidates.size() );
466
467 for ( size_t i = 0; i < candidates.size(); i++ )
468 {
469 QTableWidgetItem * AttributeValue;
470 QTableWidgetItem * RelationshipValue;
471
472 QTableWidgetItem * ObjectUid = new QTableWidgetItem (
473 tr ( "%1@%2" ).arg ( candidates.at ( i ).UID().c_str() ).arg (
474 candidates.at ( i ).class_name().c_str() ) );
475 m_filter_table->setItem ( i, 0, ObjectUid );
476
477 if ( AttributeEnabled )
478 {
480 ui->AttributeComboBox->currentText(), ClassName );
481
482 QStringList AttributeValueList
483 {
484 dbe::config::api::get::attribute::list<QStringList> ( candidates.at ( i ).ref(),
485 Attribute ) };
486
487 AttributeValue = new QTableWidgetItem ( AttributeValueList.join ( " " ) );
488 m_filter_table->setItem ( i, 1, AttributeValue );
489 }
490
491 if ( RelationshipEnabled )
492 {
494 ui->RelationshipComboBox->currentText(), ClassName );
495
496 QStringList RelationshipList
497 {
498 dbegraph::linked::through::relation<QStringList> (
499 candidates.at ( i ).ref(), Relationship ) };
500
501 RelationshipValue = new QTableWidgetItem ( RelationshipList.join ( " " ) );
502 m_filter_table->setItem ( i, 2, RelationshipValue );
503 }
504 }
505
506 QStringList Headers;
507 Headers << "Object Id";
508
509 if ( AttributeEnabled )
510 {
511 Headers << ui->AttributeComboBox->currentText();
512 }
513
514 if ( RelationshipEnabled )
515 {
516 Headers << ui->RelationshipComboBox->currentText();
517 }
518
519 m_filter_table->setHorizontalHeaderLabels ( Headers );
520
521 QHeaderView * HeaderView = m_filter_table->horizontalHeader();
522 HeaderView->setStretchLastSection ( true );
523 m_filter_table->resize ( 500, 300 );
524 m_filter_table->show();
525 }
526}
527
528void dbe::BatchChangeWidget::EnableCheckBox ( QString RelationshipName )
529{
530 QString ClassName = ui->ClassCombo->currentText();
532 ClassName.toStdString(), false );
533 std::vector<dunedaq::conffwk::relationship_t> RelationshipList = ClassInfo.p_relationships;
534
535 for ( dunedaq::conffwk::relationship_t & i : RelationshipList )
536 {
537 if ( i.p_name == RelationshipName.toStdString() )
538 {
539 if ( ( i.p_cardinality == dunedaq::conffwk::zero_or_many ) || ( i.p_cardinality
541 {
542 ui->AddCheckBox->setEnabled ( true );
543 }
544 else
545 {
546 ui->AddCheckBox->setEnabled ( false );
547 }
548
549 break;
550 }
551 }
552}
553
555{
556 ui->RelationshipValueFilter->clear();
557 ui->RelationshipValueFilter->addItem ( "*" );
558
559 QString ClassName = ui->ClassCombo->currentText();
560 QString RelationshipName = ui->RelationshipComboBox->currentText();
561
563 ClassName.toStdString(), false );
564 std::vector<dunedaq::conffwk::relationship_t> RelationshipList = ClassInfo.p_relationships;
565
566 if ( RelationshipList.size() != 0 )
567 {
568 dunedaq::conffwk::relationship_t ChosenRelationship;
569
570 for ( dunedaq::conffwk::relationship_t & i : RelationshipList )
571 {
572 if ( i.p_name == RelationshipName.toStdString() )
573 {
574 ChosenRelationship = i;
575 break;
576 }
577 }
578
579 std::vector<tref> const & Objects = dbe::config::api::info::onclass::objects (
580 ChosenRelationship.p_type );
581
582 QStringList ObjectsUid;
583
584 for ( tref const & Object : Objects )
585 {
586 ObjectsUid.append ( Object.UID().c_str() );
587 }
588
589 ui->RelationshipValueFilter->addItems ( ObjectsUid );
590 }
591}
592
594{
595 ui->RelationshipEdit->clear();
596
597 QString ClassName = ui->ClassCombo->currentText();
598 QString RelationshipName = ui->NewRelationshipComboBox->currentText();
599
601 ClassName.toStdString(), false );
602 std::vector<dunedaq::conffwk::relationship_t> RelationshipList = ClassInfo.p_relationships;
603
604 if ( RelationshipList.size() != 0 )
605 {
606 dunedaq::conffwk::relationship_t ChosenRelationship;
607
608 for ( dunedaq::conffwk::relationship_t & i : RelationshipList )
609 {
610 if ( i.p_name == RelationshipName.toStdString() )
611 {
612 ChosenRelationship = i;
613 break;
614 }
615 }
616
617 std::vector<tref> const & Objects = dbe::config::api::info::onclass::objects (
618 ChosenRelationship.p_type );
619
620 QStringList ObjectsUid;
621
622 for ( tref const & Object : Objects )
623 {
624 ObjectsUid.append ( Object.UID().c_str() );
625 }
626
627 ui->RelationshipEdit->addItems ( ObjectsUid );
628 }
629}
BatchChangeWidget(QWidget *parent=nullptr)
void EnableCheckBox(QString RelationshipName)
void FillInfo(const QString &Name)
std::unique_ptr< dbe::Ui::BatchChangeWidget > ui
void keyPressEvent(QKeyEvent *event) override
void filter(std::vector< dref > &Objects, const QString &ClassName)
std::vector< dref > TableObjects
static MainWindow * findthis()
static confaccessor & ref()
static T list(dbe::inner::configobject::tref obj, dunedaq::conffwk::attribute_t const &attr)
static std::vector< dbe::inner::configobject::tref > objects(std::string const &cname, bool const keep_inherited=true)
static dunedaq::conffwk::class_t definition(std::string const &cn, bool direct_only)
dunedaq::conffwk::ConfigObject & ref(bool check_null=true) const
static std::unique_ptr< batch_guard > batchmode()
dunedaq::conffwk::relationship_t match(T const &, T const &)
dunedaq::conffwk::attribute_t attributematch(QString const &, QString const &)
void relation(dbe::inner::configobject::tref src, dunedaq::conffwk::relationship_t const &edge, QStringList const &targets)
void attribute(dbe::inner::configobject::tref objectref, dunedaq::conffwk::attribute_t const &attribute_info, QStringList const &attribute_values)
const std::vector< attribute_t > p_attributes
Definition Schema.hpp:163
const std::vector< relationship_t > p_relationships
Definition Schema.hpp:164