ATLAS Offline Software
Loading...
Searching...
No Matches
VP1Deserialise.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7// //
8// Implementation of class VP1Deserialise //
9// //
10// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11// Initial version: June 2008 //
12// //
14
24//#include "VP1AODSystems/JetCollectionSettingsButton.h"
25
26#include <Inventor/nodes/SoMaterial.h>
27
28#include <QSet>
29#include <QBuffer>
30#include <QByteArray>
31#include <QDataStream>
32
33#include <QCheckBox>
34#include <QGroupBox>
35#include <QComboBox>
36#include <QLineEdit>
37#include <QDoubleSpinBox>
38#include <QSpinBox>
39#include <QSlider>
40#include <QToolBox>
41#include <QRadioButton>
42
43//____________________________________________________________________
45public:
48 QByteArray byteArray;
49 QBuffer * buffer;
50 QDataStream * state;
52 qint32 version;
53
54 QSet<const QWidget*> handledWidgets;
55 QSet<QWidget*> ignoredWidgets;
56
58 void block(QWidget*w) {
59 assert(!widgetNeedingUnblock);
60 if (w&&!w->signalsBlocked()) {
61 w->blockSignals(true);
63 }
64 }
65 void unblock() {
67 widgetNeedingUnblock->blockSignals(false);
69 }
70 }
71
72 void handle(const QWidget*w) {
73 if (!w)
74 return;
75 if (handledWidgets.contains(w))
76 theclass->message("ERROR: Handled widget more than once: Type="
77 +QString(w->metaObject()->className())
78 +", name="+w->objectName());
79 handledWidgets.insert(w);
80 }
81 bool expectsPersistification(QWidget*w);
82
83 static unsigned numberOfInstantiations;
84};
85
87
88//____________________________________________________________________
93
94//____________________________________________________________________
99
100//____________________________________________________________________
101VP1Deserialise::VP1Deserialise(const QByteArray & ba, IVP1System * sys)
102 : VP1HelperClassBase(sys,"VP1Deserialise"), m_d(new Imp(this))
103{
105
106 m_d->byteArray = ba;
107 m_d->buffer = new QBuffer(&(m_d->byteArray));
108 if (!m_d->buffer->open(QIODevice::ReadOnly)) {
109 delete m_d->buffer;
110 m_d->buffer = 0;
111 m_d->version = -1;
112 //Fixme: ensure that other methods do not do anything (or are not called!)
113 //Ensure that we are at end of stream when finished - and print versions (data+preferred).
114 } else {
115 m_d->state = new QDataStream(m_d->buffer);
116 m_d->version = restoreInt();
117 }
118}
119
120//____________________________________________________________________
122{
123 if (!m_d->checkedUnused)
124 message("WARNING: warnUnrestored(..) was never called!");
125 if (!atEnd())
126 message("Destructor WARNING: Buffer not at end!");
127 m_d->buffer->close();
128 delete m_d->state;
129 m_d->state = 0;
130 delete m_d->buffer;
131 m_d->buffer = 0;
132 delete m_d;
133}
134
135//____________________________________________________________________
137{
138 return m_d->buffer->atEnd();
139}
140
141//____________________________________________________________________
143{
144 return m_d->version;
145}
146
147//____________________________________________________________________
149{
150 return m_d->state;
151}
152
153//____________________________________________________________________
154void VP1Deserialise::restore( SoMaterial* m )
155{
156 if(VP1Msg::debug()){
157 messageDebug("VP1Deserialise::restore(SoMaterial)");
158 }
159 QByteArray ba(restoreByteArray());
160 if (!m||ba.isEmpty())
161 return;
163}
164
165//____________________________________________________________________
167{
168 if(VP1Msg::debug()){
169 messageDebug("VP1Deserialise::restore(VP1MaterialButton)");
170 }
171 m_d->handle(mb);
172 SoMaterial * m(0);
173 QList<SoMaterial*> mats = mb ? mb->handledMaterials() : QList<SoMaterial*>();
174 bool tempmat = mats.isEmpty() || !mats.at(0);
175 m = tempmat ? new SoMaterial : mats.at(0);
176 m->ref();
177 restore(m);
178 if (mb) {
179 m_d->block(mb);
180 mb->copyValuesFromMaterial(m);
181 m_d->unblock();
182 }
183 if (tempmat)
184 m->unref();
185 else
186 m->unrefNoDelete();
187}
188
189//____________________________________________________________________
191{
192 QColor c;
193 (*(m_d->state)) >> c;
194 if(VP1Msg::debug()){
195 messageDebug("VP1Deserialise::restore(QColor) - name: " + c.name());
196 }
197 if (VP1Msg::verbose())
198 messageVerbose("Restoring color "+str(c));
199 return c;
200}
201
202//____________________________________________________________________
204{
205 if(VP1Msg::debug()){
206 messageDebug("VP1Deserialise::restore(VP1ColorSelectButton) - name: " + cb->objectName());
207 }
208 m_d->handle(cb);
209 QColor c = restoreColor();
210 if (c.isValid()&&cb&&cb->color()!=c) {
211 m_d->block(cb);
212 cb->setColor(c);
213 m_d->unblock();
214 }
215}
216
217//____________________________________________________________________
219{
220 if(VP1Msg::debug()){
221 messageDebug("VP1Deserialise::restore(PhiSectionWidget) - name: " + phi->objectName());
222 }
223 m_d->handle(phi);
224 QByteArray ba(restoreByteArray());
225 if (phi&&ba != QByteArray()) {
226 m_d->block(phi);
227 phi->setState(std::move(ba));
228 m_d->unblock();
229 }
230}
231
232//____________________________________________________________________
234{
235 QByteArray ba;
236 (*(m_d->state)) >> ba;
237 if (VP1Msg::verbose()){
238 messageVerbose("Restoring byte array (length = "+QString::number(ba.count())+")");
239 }
240 return ba;
241}
242
243//____________________________________________________________________
244void VP1Deserialise::restore(QCheckBox *cb )
245{
246 if(VP1Msg::debug()){
247 messageDebug("VP1Deserialise::restore(QCheckBox) - name: " + cb->objectName());
248 }
249
250 m_d->handle(cb);
251
252 bool b = restoreBool();
253
254 if(VP1Msg::debug()){
255 messageDebug("bool b: "+QString::number(b));
256 messageDebug("bool cb: "+QString::number( cb->isChecked() ));
257 }
258
259 if (cb->isChecked()!=b) {
260 m_d->block(cb);
261 if(VP1Msg::debug()){
262 messageDebug("setting - checked: "+QString::number( b ));
263 }
264 cb->setChecked(b);
265 m_d->unblock();
266 }
267}
268
269//____________________________________________________________________
270void VP1Deserialise::restore(QGroupBox*gb)
271{
272 if(VP1Msg::debug()){
273 messageDebug("VP1Deserialise::restore(QGroupBox) - name: " + gb->objectName());
274 }
275 if (!gb->isCheckable())
276 message("WARNING: Asked to handled GroupBox which is not checkable: "+gb->objectName());
277 m_d->handle(gb);
278 bool b = restoreBool();
279 if (gb->isChecked()!=b) {
280 m_d->block(gb);
281 gb->setChecked(b);
282 m_d->unblock();
283 }
284}
285
286//____________________________________________________________________
287void VP1Deserialise::restore(QComboBox *cb)
288{
289 if(VP1Msg::debug()){
290 messageDebug("VP1Deserialise::restore(QComboBox) - name: " + cb->objectName());
291 }
292 m_d->handle(cb);
293 QString t = restoreString();
294 if (t.isEmpty())
295 return;
296 int i = cb->findText(t);
297 if (i<0||i>cb->count()-1)
298 return;
299 if (cb->currentIndex()!=i) {
300 m_d->block(cb);
301 cb->setCurrentIndex(i);
302 m_d->unblock();
303 }
304}
305
306//____________________________________________________________________
307void VP1Deserialise::restore(QLineEdit* le)
308{
309 if(VP1Msg::debug()){
310 messageDebug("VP1Deserialise::restore(QLineEdit) - name: " + le->objectName());
311 }
312 m_d->handle(le);
313 QString s = restoreString();
314 if (s!=le->text()) {
315 m_d->block(le);
316 le->setText(s);
317 m_d->unblock();
318 }
319}
320
321//____________________________________________________________________
322void VP1Deserialise::restore(QDoubleSpinBox *sb, const double& unit )
323{
324 if(VP1Msg::debug()){
325 messageDebug("VP1Deserialise::restore(QDoubleSpinBox) - name: " + sb->objectName());
326 }
327 m_d->handle(sb);
328 double dbl = (unit == 1.0 ? restoreDouble() : restoreDouble()/unit);
329 dbl = std:: max(std::min(dbl,sb->maximum()),sb->minimum());
330 if (sb->value()!=dbl) {
331 m_d->block(sb);
332 sb->setValue(dbl);
333 m_d->unblock();
334 }
335}
336
337//____________________________________________________________________
338void VP1Deserialise::restore(QSpinBox *sb)
339{
340 if(VP1Msg::debug()){
341 messageDebug("VP1Deserialise::restore(QSpinBox) - name: " + sb->objectName());
342 }
343 m_d->handle(sb);
344 qint32 i = restoreInt();
345 i = std:: max(std::min(i,sb->maximum()),sb->minimum());
346 if (sb->value()!=i) {
347 m_d->block(sb);
348 sb->setValue(i);
349 m_d->unblock();
350 }
351}
352
353//____________________________________________________________________
355{
356 if(VP1Msg::debug()){
357 messageDebug("VP1Deserialise::restore(QSlider) - name: " + s->objectName());
358 }
359 m_d->handle(s);
360 qint32 i = restoreInt();
361 i = std:: max(std::min(i,s->maximum()),s->minimum());
362 if (s->value()!=i) {
363 m_d->block(s);
364 s->setValue(i);
365 m_d->unblock();
366 }
367}
368
369//____________________________________________________________________
370void VP1Deserialise::restore(QToolBox *tb)
371{
372 if(VP1Msg::debug()){
373 messageDebug("VP1Deserialise::restore(QToolBox) - name: " + tb->objectName());
374 }
375 m_d->handle(tb);
376 qint32 i = restoreInt();
377 if (i>=0&&i<tb->count()&&i!=tb->currentIndex()) {
378 m_d->block(tb);
379 tb->setCurrentIndex(i);
380 m_d->unblock();
381 }
382}
383
384//____________________________________________________________________
386{
387 if(VP1Msg::debug()){
388 messageDebug("VP1Deserialise::restore(QToolBox) - name: " + tb->objectName());
389 }
390 m_d->handle(tb);
391 QString s = restoreString();
392 int itarget (-1);
393 for (int i = 0; i < tb->count(); ++i) {
394 if (tb->itemText(i)==s) {
395 itarget = i;
396 break;
397 }
398 }
399 if (itarget>0&&itarget<tb->count()&&itarget!=tb->currentIndex()) {
400 m_d->block(tb);
401 tb->setCurrentIndex(itarget);
402 m_d->unblock();
403 }
404}
405
406
407//____________________________________________________________________
409{
410 QString t;
411 (*(m_d->state)) >> t;
412 if (VP1Msg::verbose()){
413 messageVerbose("Restoring string "+t);
414 }
415 return t;
416}
417//____________________________________________________________________
419{
420 bool b;
421 (*(m_d->state)) >> b;
422 if (VP1Msg::verbose()){
423 messageVerbose("Restoring bool "+str(b));
424 }
425 return b;
426}
427//____________________________________________________________________
429{
430 double dbl;
431 (*(m_d->state)) >> dbl;
432 if (VP1Msg::verbose()){
433 messageVerbose("Restoring double "+str(dbl));
434 }
435 return dbl;
436}
437
438//____________________________________________________________________
440{
441 qint32 i;
442 (*(m_d->state)) >> i;
443 if (VP1Msg::verbose()){
444 messageVerbose("Restoring int "+str(i));
445 }
446 return i;
447}
448
449//____________________________________________________________________
450void VP1Deserialise::restore( QRadioButton * rb0,
451 QRadioButton * rb1,
452 QRadioButton * rb2,
453 QRadioButton * rb3,
454 QRadioButton * rb4,
455 QRadioButton * rb5,
456 QRadioButton * rb6,
457 QRadioButton * rb7,
458 QRadioButton * rb8,
459 QRadioButton * rb9 )
460{
461 qint32 ichecked = restoreInt();
462 QList<QRadioButton *> l;
463 l << rb0 << rb1 << rb2 << rb3 << rb4 << rb5 << rb6 << rb7 << rb8 << rb9;
464 for (qint32 i = 0; i < l.count(); ++i) {
465 if (l.at(i)) {
466 if(VP1Msg::debug()){
467 messageDebug("VP1Deserialise::restore(QRadioButton) - name: " + l.at(i)->objectName());
468 }
469 m_d->handle(l.at(i));
470 }
471 }
472 //We only change any state if we have a pointer to the one needing to be checked:
473 if (ichecked<0||ichecked>=l.count()||!l.at(ichecked))
474 return;
475 for (qint32 i = 0; i < l.count(); ++i) {
476 QRadioButton * rb = l.at(i);
477 if (rb&&rb->isChecked()!=(i==ichecked)) {
478 m_d->block(rb);
479 rb->setChecked(i==ichecked);
480 m_d->unblock();
481 }
482 }
483}
484
485//____________________________________________________________________
487{
488 if(VP1Msg::debug()){
489 messageDebug("VP1Deserialise::restore(VP1CollectionWidget) - name: " + cw->objectName());
490 messageDebug("VP1Deserialise::restore(VP1CollectionWidget)- start...");
491 }
492 m_d->handle(cw);
493 ignoreWidget(cw);//To ignore all children of the collection widget.
494 QByteArray ba(restoreByteArray());
495 QBuffer buffer(&ba);
496 buffer.open(QIODevice::ReadOnly);
497 QDataStream state(&buffer);
498 qint32 version;
499 state >> version;
500 if (version!=0)
501 return;//We ignore wrong versions silently here.
502 VP1CollStates cwstates;
503 state >> cwstates;
504 buffer.close();
505 m_d->block(cw);
506 cw->addStateInfo(cwstates,true/*overwrite existing*/);
507 m_d->unblock();
508 if(VP1Msg::debug()){
509 messageDebug("VP1Deserialise::restore(VP1CollectionWidget)- end.");
510 }
511}
512
513//____________________________________________________________________
515{
516 if(VP1Msg::debug()){
517 messageDebug("VP1Deserialise::restore(VP1CollectionSettingsButtonBase) - name: " + w->objectName());
518 messageDebug("VP1Deserialise::restore(VP1CollectionSettingsButtonBase)- start...");
519 }
520 m_d->handle(w);
521 ignoreWidget(w);//To ignore all children of the widget.
522
523 QByteArray ba = restoreByteArray();
524 if (w&&ba!=QByteArray()) {
525 m_d->block(w);
526 w->restoreFromState(ba);
527 m_d->unblock();
528 }
529 if(VP1Msg::debug()){
530 messageDebug("VP1Deserialise::restore(VP1CollectionSettingsButtonBase)- end.");
531 }
532}
533
534//void VP1Deserialise::restore(JetCollectionSettingsButton*w)
535//{
536// m_d->handle(w);
537// ignoreWidget(w);//To ignore all children of the widget.
538//
539// QByteArray ba = restoreByteArray();
540// if (w&&ba!=QByteArray()) {
541// m_d->block(w);
542// w->restoreFromState(ba);
543// m_d->unblock();
544// }
545//}
546
547
548
549//____________________________________________________________________
551{
552 if(VP1Msg::debug()){
553 messageDebug("VP1Deserialise::restore(VP1EtaPhiCutWidget) - name: " + w->objectName());
554 }
555 m_d->handle(w);
556 ignoreWidget(w);//To ignore all children of the widget.
557
558 QByteArray ba = restoreByteArray();
559 if (w&&ba!=QByteArray()) {
560 m_d->block(w);
561 w->restoreFromState(ba);
562 m_d->unblock();
563 }
564}
565
566
567
568
569//____________________________________________________________________
571{
572 if(VP1Msg::debug()){
573 messageDebug("VP1Deserialise::restore(VP1DrawOptionsWidget) - name: " + w->objectName());
574 }
575 m_d->handle(w);
576 ignoreWidget(w);//To ignore all children of the widget.
577
578 QByteArray ba = restoreByteArray();
579 if (w&&ba!=QByteArray()) {
580 m_d->block(w);
581 w->applyState(ba);
582 m_d->unblock();
583 }
584}
585
586//____________________________________________________________________
588{
589 bool b;
590 (*(m_d->state)) >> b;
591 if (VP1Msg::verbose()){
592 messageVerbose("Ignoring bool "+str(b));
593 }
594}
595
596//____________________________________________________________________
598{
599 qint32 i;
600 (*(m_d->state)) >> i;
601 if (VP1Msg::verbose()){
602 messageVerbose("Ignoring int "+str(i));
603 }
604}
605
606//____________________________________________________________________
608{
609 double dbl;
610 (*(m_d->state)) >> dbl;
611 if (VP1Msg::verbose()){
612 messageVerbose("Ignoring double "+str(dbl));
613 }
614}
615
616//____________________________________________________________________
618{
619 QString t;
620 (*(m_d->state)) >> t;
621 if (VP1Msg::verbose()){
622 messageVerbose("Ignoring string "+t);
623 }
624}
625
626//____________________________________________________________________
628{
629 QByteArray ba;
630 (*(m_d->state)) >> ba;
631 if (VP1Msg::verbose()){
632 messageVerbose("Ignoring byte array (length = "+QString::number(ba.count())+")");
633 }
634}
635
636//____________________________________________________________________
638{
639 QPair<int,QList<int> > state;
640 (*(m_d->state)) >> state;
641 if (VP1Msg::verbose()){
642 messageVerbose("Ignoring obsolete phi section widget state");
643 }
644}
645
646//____________________________________________________________________
648{
649 if(VP1Msg::debug()){
650 messageDebug("VP1Deserialise::ignoreWidget(QWidget) - name: " + w->objectName());
651 }
652 if (w)
653 m_d->ignoredWidgets.insert(w);
654}
655
656//____________________________________________________________________
658{
659 m_d->handle(w);
660}
661
662//____________________________________________________________________
664{
665 //NB: Same code as in VP1Serialise::Imp::expectsPersistification
666 if (!w)
667 return false;
668
669 //Fixme: Something faster than string based? Or only do this in verbose mode?
670
671 if (w->inherits("QGroupBox"))
672 return static_cast<QGroupBox*>(w)->isCheckable();
673
674 return w->inherits("QCheckBox")
675 || w->inherits("QRadioButton")
676 || w->inherits("QComboBox")
677 || w->inherits("QAbstractSpinBox")
678 || w->inherits("QSlider")
679 || w->inherits("QToolBox")
680 || w->inherits("PhiSectionWidget")
681 || w->inherits("VP1EtaPhiCutWidget")
682 || w->inherits("VP1DrawOptionsWidget")
683 || w->inherits("QLineEdit")
684 || w->inherits("VP1ColorSelectButton")
685 || w->inherits("VP1MaterialButton");
686}
687
688//____________________________________________________________________
690{
691 m_d->checkedUnused = true;
692}
693
694//____________________________________________________________________
696{
697 //NB: Same code as in VP1Serialise::warnUnsaved
698
699 if (!m_d->checkedUnused)
700 m_d->checkedUnused = true;
701
702 if (!object)
703 return;
704
705 if (object->isWidgetType()&&m_d->ignoredWidgets.contains(static_cast<QWidget*>(object)))
706 return;
707
708 if (object->isWidgetType()&&!object->objectName().startsWith("qt_")) {
709 QWidget * wid = static_cast<QWidget*>(object);
710 if (!m_d->handledWidgets.contains(wid)&&m_d->expectsPersistification(wid)) {
711 QString s("WARNING Unrestored widget of type: "+QString(wid->metaObject()->className())+" and object name = "+wid->objectName());
712 if (VP1Msg::verbose()){
713 message(s);
714 }
715 if(VP1Msg::debug()){
716 messageDebug(s);
717 }
718 }
719 }
720 //Call recursively on all "children":
721 for (QObject* o : object->children())
722 warnUnrestored(static_cast<QWidget*>(o));
723}
Scalar phi() const
phi method
const PlainObject unit() const
This is a plugin that makes Eigen look like CLHEP & defines some convenience methods.
static Double_t tc
QMap< QByteArray, QByteArray > VP1CollStates
void addStateInfo(const VP1CollStates &, bool overwritesExisting=true)
void setColor(const QColor &)
bool expectsPersistification(QWidget *w)
void handle(const QWidget *w)
QSet< const QWidget * > handledWidgets
static unsigned numberOfInstantiations
VP1Deserialise * theclass
void block(QWidget *w)
Imp(VP1Deserialise *tc)
QSet< QWidget * > ignoredWidgets
VP1Deserialise(const QByteArray &, IVP1System *sys=0)
static unsigned numberOfInstantiations()
QByteArray restoreByteArray()
void ignoreWidget(QWidget *)
bool atEnd() const
void disableUnrestoredChecks()
QString restoreString()
virtual ~VP1Deserialise()
void ignoreObsoletePhiSectionWidgetState()
void warnUnrestored(QObject *)
static void decrementNumberOfInstantiations()
QDataStream * stream()
void widgetHandled(QWidget *)
qint32 version() const
void restoreByTitle(QToolBox *)
VP1HelperClassBase(IVP1System *sys=0, QString helpername="")
void messageVerbose(const QString &) const
void message(const QString &) const
void messageDebug(const QString &) const
static bool debug()
Definition VP1Msg.h:32
static bool verbose()
Definition VP1Msg.h:31
static bool deserialiseSoMaterial(QByteArray &, SoMaterial *&)
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146