ATLAS Offline Software
VP1Serialise.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // //
8 // Implementation of class VP1Serialise //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial version: June 2008 //
12 // //
14 
15 #include "VP1Base/VP1Serialise.h"
24 
25 #include <Inventor/nodes/SoMaterial.h>
26 
27 #include <QBuffer>
28 #include <QByteArray>
29 #include <QDataStream>
30 #include <QSet>
31 #include <QCheckBox>
32 #include <QGroupBox>
33 #include <QComboBox>
34 #include <QLineEdit>
35 #include <QDoubleSpinBox>
36 #include <QSpinBox>
37 #include <QSlider>
38 #include <QToolBox>
39 #include <QRadioButton>
40 
41 //____________________________________________________________________
43 public:
44  Imp(VP1Serialise *tc) : theclass(tc), buffer(0), state(0),checkedUnused(false),version(0) {}
46  QByteArray byteArray;
47  QBuffer * buffer;
48  QDataStream * state;
50  qint32 version;
51 
52  QSet<const QWidget*> handledWidgets;
53  QSet<const QWidget*> ignoredWidgets;
54  void handle(QWidget*w) {
55  if (!w) {
56  if(VP1Msg::debug()){
57  theclass->messageDebug("VP1Serialize::handle() - Returning...");
58  }
59  return;
60  }
61  if (handledWidgets.contains(w))
62  theclass->message("ERROR: Handled widget more than once: Type="
63  +QString(w->metaObject()->className())
64  +", name="+w->objectName());
65  handledWidgets.insert(w);
66  }
67  void handle(const QWidget*w) { handle((QWidget*)(w)); }
68  bool expectsPersistification(const QWidget*w);
69 
70  static unsigned numberOfInstantiations;
71 };
72 
74 //____________________________________________________________________
76 {
78 }
79 
80 //____________________________________________________________________
82 {
84 }
85 
86 //____________________________________________________________________
88  : VP1HelperClassBase(sys,"VP1Serialise"), m_d(new Imp(this))
89 {
91 
92  m_d->byteArray.clear();
93  m_d->buffer = new QBuffer(&m_d->byteArray);
94  m_d->buffer->open(QIODevice::WriteOnly);
95  m_d->state = new QDataStream(m_d->buffer);
96  m_d->version = version;
97  save(version);
98 }
99 
100 //____________________________________________________________________
102 {
103  if (!m_d->checkedUnused)
104  message("WARNING: warnUnsaved(..) was never called!");
105 
106  m_d->buffer->close();
107  delete m_d->state;
108  m_d->state = 0;
109  delete m_d->buffer;
110  m_d->buffer = 0;
111  delete m_d;
112 }
113 
114 //____________________________________________________________________
115 QDataStream * VP1Serialise::stream()
116 {
117  return m_d->state;
118 }
119 
120 
121 //____________________________________________________________________
122 qint32 VP1Serialise::version() const
123 {
124  return m_d->version;
125 }
126 
127 
128 //____________________________________________________________________
130 {
131  return m_d->byteArray;
132 }
133 
134 //____________________________________________________________________
135 void VP1Serialise::save(QCheckBox*cb)
136 {
137  if(VP1Msg::debug()){
138  messageDebug("VP1Serialise::save(QCheckBox) - name: " + cb->objectName());
139  }
140  m_d->handle(cb);
141  save(cb->isChecked());
142 }
143 
144 //____________________________________________________________________
145 void VP1Serialise::save(QGroupBox*gb)
146 {
147  if(VP1Msg::debug()){
148  messageDebug("VP1Serialise::save(QGroupBox) - name: " + gb->objectName());
149  }
150  if (!gb->isCheckable())
151  message("WARNING: Asked to handled GroupBox which is not checkable: "+gb->objectName());
152  m_d->handle(gb);
153  save(gb->isChecked());
154 }
155 
156 //____________________________________________________________________
157 void VP1Serialise::save(QComboBox*cb)
158 {
159  if(VP1Msg::debug()){
160  messageDebug("VP1Serialise::save(QComboBox) - name: " + cb->objectName());
161  }
162  m_d->handle(cb);
163  save( cb->count() > 0 ? cb->currentText() : QString() );
164 }
165 
166 //____________________________________________________________________
167 void VP1Serialise::save(QLineEdit* le)
168 {
169  if(VP1Msg::debug()){
170  messageDebug("\nVP1Serialise::save(QLineEdit) - name: " + le->objectName());
171  }
172  m_d->handle(le);
173  save( le->text() );
174 }
175 
176 //____________________________________________________________________
177 void VP1Serialise::save(QDoubleSpinBox*sb,const double& unit)
178 {
179  if(VP1Msg::debug()){
180  messageDebug("\nVP1Serialise::save(QDoubleSpinBox) - name: " + sb->objectName());
181  }
182  m_d->handle(sb);
183  save(unit==1.0 ? sb->value() : sb->value() * unit );
184 }
185 
186 //____________________________________________________________________
187 void VP1Serialise::save(QSpinBox*sb)
188 {
189  if(VP1Msg::debug()){
190  messageDebug("\nVP1Serialise::save(QSpinBox) - name: " + sb->objectName());
191  }
192  m_d->handle(sb);
193  save(sb->value());
194 }
195 
196 //____________________________________________________________________
197 void VP1Serialise::save(QSlider*s)
198 {
199  if(VP1Msg::debug()){
200  messageDebug("\nVP1Serialise::save(QSlider) - name: " + s->objectName());
201  }
202  m_d->handle(s);
203  save(s->value());
204 }
205 
206 //____________________________________________________________________
208 {
209  if (VP1Msg::verbose()){
210  messageVerbose("Saving bool "+str(b));
211  }
212  (*m_d->state) << b;
213 }
214 
215 //____________________________________________________________________
216 void VP1Serialise::save(qint32 i)
217 {
218  if (VP1Msg::verbose()){
219  messageVerbose("Saving int "+str(i));
220  }
221  (*m_d->state) << i;
222 }
223 
224 //____________________________________________________________________
225 void VP1Serialise::save(const double& dbl)
226 {
227  if (VP1Msg::verbose()){
228  messageVerbose("Saving double "+str(dbl));
229  }
230  (*(m_d->state)) << dbl;
231 }
232 
233 //____________________________________________________________________
234 void VP1Serialise::save(const QString& s)
235 {
236  if (VP1Msg::verbose()){
237  messageVerbose("Saving string "+s);
238  }
239  (*(m_d->state)) << s;
240 }
241 
242 //____________________________________________________________________
243 void VP1Serialise::save(const QByteArray& ba)
244 {
245  if (VP1Msg::verbose()){
246  messageVerbose("Saving byte array (length = "+QString::number(ba.count())+")");
247  }
248  (*(m_d->state)) << ba;
249 }
250 
251 //____________________________________________________________________
252 void VP1Serialise::save(QToolBox*tb)
253 {
254  if(VP1Msg::debug()){
255  messageDebug("\nVP1Serialise::save(QToolBox) - name: " + tb->objectName());
256  }
257  m_d->handle(tb);
258  save( tb && tb->count() > 0 ? tb->currentIndex() : -1 );
259 }
260 
261 //____________________________________________________________________
263 {
264  if(VP1Msg::debug()){
265  messageDebug("\nVP1Serialise::save(QToolBox) - name: " + tb->objectName());
266  }
267  m_d->handle(tb);
268  int i = tb ? tb->currentIndex() : -1;
269  save( i>=0 && i<tb->count() ? tb->itemText(i) : QString() );
270 }
271 
272 //____________________________________________________________________
273 void VP1Serialise::save(SoMaterial* m)
274 {
275  if(VP1Msg::debug()){
276  messageDebug("\nVP1Serialise::save(SoMaterial)");
277  }
278  if (!m) {
279  save(QByteArray());
280  return;
281  }
282  m->ref();
284  m->unrefNoDelete();
285 }
286 
287 //____________________________________________________________________
289 {
290  if(VP1Msg::debug()){
291  messageDebug("\nVP1Serialise::save(VP1MaterialButton) - name: " + mb->objectName());
292  }
293  m_d->handle(mb);
294  QList<SoMaterial*> mats = mb ? mb->handledMaterials() : QList<SoMaterial*>();
295  save(mats.isEmpty() ? 0 : mats.at(0));
296 }
297 
298 //____________________________________________________________________
299 void VP1Serialise::save(const QColor& c)
300 {
301  if(VP1Msg::debug()){
302  messageDebug("\nVP1Serialise::save(QColor) - name: " + c.name());
303  }
304  if (VP1Msg::verbose()){
305  messageVerbose("Saving color "+str(c));
306  }
307  (*m_d->state) << c;
308 }
309 
310 //____________________________________________________________________
312 {
313  if(VP1Msg::debug()){
314  messageDebug("\nVP1Serialise::save(VP1ColorSelectButton) - name: " + cb->objectName());
315  }
316  m_d->handle(cb);
317  save(cb ? cb->color() : QColor());
318 }
319 
320 //____________________________________________________________________
322 {
323  if(VP1Msg::debug()){
324  messageDebug("\nVP1Serialise::save(PhiSectionWidget) - name: " + phi->objectName());
325  }
326  m_d->handle(phi);
327  if (VP1Msg::verbose()){
328  messageVerbose("Saving phisection widget state");
329  }
330  (*(m_d->state)) << (phi ? phi->state() : QByteArray());
331 }
332 
333 //____________________________________________________________________
334 void VP1Serialise::save( QRadioButton * rb0,
335  QRadioButton * rb1,
336  QRadioButton * rb2,
337  QRadioButton * rb3,
338  QRadioButton * rb4,
339  QRadioButton * rb5,
340  QRadioButton * rb6,
341  QRadioButton * rb7,
342  QRadioButton * rb8,
343  QRadioButton * rb9 )
344 {
345  QList<QRadioButton *> l;
346  l << rb0 << rb1 << rb2 << rb3 << rb4 << rb5 << rb6 << rb7 << rb8 << rb9;
347  for (qint32 i = 0; i < l.count(); ++i) {
348  if (l.at(i)) {
349  if(VP1Msg::debug()){
350  messageDebug("\nVP1Serialise::save(QRadioButton) - name: " + l.at(i)->objectName());
351  }
352  m_d->handle(l.at(i));
353  }
354  }
355  qint32 ichecked(-1);
356  for (qint32 i = 0; i < l.count(); ++i) {
357  if (l.at(i)&&l.at(i)->isChecked()) {
358  ichecked = i;
359  break;
360  }
361  }
362  save(ichecked);
363 }
364 
365 //____________________________________________________________________
367 {
368  if(VP1Msg::debug()){
369  messageDebug("\nVP1Serialise::save(VP1CollectionWidget) - name: " + cw->objectName());
370  messageDebug("VP1Serialise::save(VP1CollectionWidget)- start...");
371  }
372  m_d->handle(cw);
373  ignoreWidget(cw);//To ignore all children of the collection widget.
374  QByteArray ba;
375  QBuffer buffer(&ba);
376  buffer.open(QIODevice::WriteOnly);
377  QDataStream out(&buffer);
378  out << (qint32)0;//version
379  out << cw->states();
380  buffer.close();
381  save(ba);
382  if(VP1Msg::debug()){
383  messageDebug("VP1Serialise::save(VP1CollectionWidget)- end.");
384  }
385 }
386 
388 //void VP1Serialise::save(const JetCollectionSettingsButton* jcb)
389 //{
390 // m_d->handle(jcb);
391 // ignoreWidget(jcb);//To ignore all children of the etaphicut widget.
392 // save(jcb ? jcb->saveState() : QByteArray());
393 //}
394 //____________________________________________________________________
396 {
397  if(VP1Msg::debug()){
398  messageDebug("\nVP1Serialise::save(VP1CollectionSettingsButtonBase) - name: " + jcb->objectName());
399  messageDebug("VP1Serialise::save(VP1CollectionSettingsButtonBase)- start...");
400  }
401  m_d->handle(jcb);
402  ignoreWidget(jcb);//To ignore all children of the etaphicut widget.
403  save(jcb ? jcb->saveState() : QByteArray());
404  if(VP1Msg::debug()){
405  messageDebug("VP1Serialise::save(VP1CollectionSettingsButtonBase)- end.");
406  }
407 }
408 
409 
410 //____________________________________________________________________
412 {
413  if(VP1Msg::debug()){
414  messageDebug("\nVP1Serialise::save(VP1EtaPhiCutWidget) - name: " + w->objectName());
415  }
416  m_d->handle(w);
417  ignoreWidget(w);//To ignore all children of the etaphicut widget.
418  save(w ? w->saveState() : QByteArray());
419 }
420 
421 //____________________________________________________________________
423 {
424  if(VP1Msg::debug()){
425  messageDebug("\nVP1Serialise::save(VP1DrawOptionsWidget) - name: " + w->objectName());
426  }
427  m_d->handle(w);
428  ignoreWidget(w);//To ignore all children of the draw options widget.
429  save(w ? w->state() : QByteArray());
430 }
431 
432 //____________________________________________________________________
433 void VP1Serialise::ignoreWidget(const QWidget*w)
434 {
435  if(VP1Msg::debug()){
436  messageDebug("\nVP1Serialise::ignoreWidget(QWidget) - name: " + w->objectName());
437  }
438  if (w)
439  m_d->ignoredWidgets.insert(w);
440 }
441 
442 //____________________________________________________________________
443 void VP1Serialise::widgetHandled(const QWidget*w)
444 {
445  m_d->handle(w);
446 }
447 
448 //____________________________________________________________________
450 {
451  //NB: Same code as in VP1Deserialise::Imp::expectsPersistification
452  if (!w)
453  return false;
454 
455  //Fixme: Something faster than string based? Or only do this in verbose mode?
456 
457  if (w->inherits("QGroupBox"))
458  return static_cast<const QGroupBox*>(w)->isCheckable();
459 
460  return w->inherits("QCheckBox")
461  || w->inherits("QRadioButton")
462  || w->inherits("QComboBox")
463  || w->inherits("QAbstractSpinBox")
464  || w->inherits("QSlider")
465  || w->inherits("QToolBox")
466  || w->inherits("PhiSectionWidget")
467  || w->inherits("VP1EtaPhiCutWidget")
468  || w->inherits("VP1DrawOptionsWidget")
469  || w->inherits("QLineEdit")
470  || w->inherits("VP1ColorSelectButton")
471  || w->inherits("VP1MaterialButton");
472 }
473 
474 //____________________________________________________________________
476 {
477  m_d->checkedUnused = true;
478 }
479 
480 //____________________________________________________________________
481 void VP1Serialise::warnUnsaved(const QObject* object)
482 {
483  //NB: Same code as in VP1Deserialise::warnUnrestored
484 
485  if (!m_d->checkedUnused)
486  m_d->checkedUnused = true;
487 
488  if (!object)
489  return;
490 
491  if (object->isWidgetType()&&m_d->ignoredWidgets.contains(static_cast<const QWidget*>(object)))
492  return;
493 
494  if (object->isWidgetType()&&!object->objectName().startsWith("qt_")) {
495  const QWidget * wid = static_cast<const QWidget*>(object);
496  if (!m_d->handledWidgets.contains(wid)&&m_d->expectsPersistification(wid)) {
497  QString s("WARNING Unsaved widget of type: "+QString(wid->metaObject()->className())+" and object name = "+wid->objectName());
498  if (VP1Msg::verbose()){
499  message(s);
500  }
501  if(VP1Msg::debug()){
502  messageDebug(s);
503  }
504  }
505  }
506  //Call recursively on all "children":
507  for (const QObject* o : object->children())
508  warnUnsaved(static_cast<const QWidget*>(o));
509 }
VP1Serialise.h
VP1Serialise::Imp::version
qint32 version
Definition: VP1Serialise.cxx:50
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
VP1Serialise
Definition: VP1Serialise.h:45
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:64
VP1MaterialButton.h
VP1Serialise::Imp::state
QDataStream * state
Definition: VP1Serialise.cxx:48
VP1MaterialButton
Definition: VP1MaterialButton.h:46
VP1HelperClassBase::messageVerbose
void messageVerbose(const QString &) const
Definition: VP1HelperClassBase.cxx:78
VP1Serialise::Imp::Imp
Imp(VP1Serialise *tc)
Definition: VP1Serialise.cxx:44
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
VP1DrawOptionsWidget.h
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
VP1CollectionSettingsButtonBase.h
VP1Serialise::decrementNumberOfInstantiations
static void decrementNumberOfInstantiations()
Definition: VP1Serialise.cxx:81
VP1Serialise::warnUnsaved
void warnUnsaved(const QObject *)
Definition: VP1Serialise.cxx:481
VP1Serialise::widgetHandled
void widgetHandled(const QWidget *)
Definition: VP1Serialise.cxx:443
VP1Msg::debug
static bool debug()
Definition: VP1Msg.h:32
VP1Serialise::Imp::handle
void handle(const QWidget *w)
Definition: VP1Serialise.cxx:67
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
VP1Serialise::m_d
Imp * m_d
Definition: VP1Serialise.h:118
VP1String::str
static QString str(const QString &s)
Definition: VP1String.h:49
VP1EtaPhiCutWidget
Definition: VP1EtaPhiCutWidget.h:26
VP1ColorSelectButton
Definition: VP1ColorSelectButton.h:20
VP1HelperClassBase::messageDebug
void messageDebug(const QString &) const
Definition: VP1HelperClassBase.cxx:65
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
VP1EtaPhiCutWidget.h
VP1CollectionWidget.h
VP1QtInventorUtils.h
Execution.tb
tb
Definition: Execution.py:15
IVP1System
Definition: IVP1System.h:36
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
VP1Serialise::numberOfInstantiations
static unsigned numberOfInstantiations()
Definition: VP1Serialise.cxx:75
VP1Serialise::Imp::byteArray
QByteArray byteArray
Definition: VP1Serialise.cxx:46
lumiFormat.i
int i
Definition: lumiFormat.py:92
VP1Serialise::save
void save(bool)
Definition: VP1Serialise.cxx:207
VP1Serialise::Imp::handledWidgets
QSet< const QWidget * > handledWidgets
Definition: VP1Serialise.cxx:52
VP1CollectionSettingsButtonBase
Definition: VP1CollectionSettingsButtonBase.h:17
VP1QtInventorUtils::serialiseSoMaterial
static QByteArray serialiseSoMaterial(SoMaterial *)
Definition: VP1QtInventorUtils.cxx:1148
VP1Serialise::Imp::numberOfInstantiations
static unsigned numberOfInstantiations
Definition: VP1Serialise.cxx:70
VP1CollectionWidget::states
VP1CollStates states() const
Definition: VP1CollectionWidget.cxx:304
VP1Serialise::VP1Serialise
VP1Serialise(qint32 version, IVP1System *sys=0)
Definition: VP1Serialise.cxx:87
VP1Serialise::Imp::ignoredWidgets
QSet< const QWidget * > ignoredWidgets
Definition: VP1Serialise.cxx:53
PhiSectionWidget.h
VP1ColorSelectButton::color
QColor color() const
Definition: VP1ColorSelectButton.cxx:108
VP1Serialise::Imp
Definition: VP1Serialise.cxx:42
VP1ColorSelectButton.h
PhiSectionWidget
Definition: PhiSectionWidget.h:26
VP1HelperClassBase
Definition: VP1HelperClassBase.h:28
VP1Serialise::~VP1Serialise
virtual ~VP1Serialise()
Definition: VP1Serialise.cxx:101
VP1Serialise::Imp::checkedUnused
bool checkedUnused
Definition: VP1Serialise.cxx:49
VP1Serialise::Imp::theclass
VP1Serialise * theclass
Definition: VP1Serialise.cxx:45
keylayer_zslicemap.sb
sb
Definition: keylayer_zslicemap.py:192
VP1Serialise::Imp::buffer
QBuffer * buffer
Definition: VP1Serialise.cxx:47
python.selection.number
number
Definition: selection.py:20
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
VP1Serialise::saveByTitle
void saveByTitle(QToolBox *)
Definition: VP1Serialise.cxx:262
VP1Serialise::ignoreWidget
void ignoreWidget(const QWidget *)
Definition: VP1Serialise.cxx:433
get_generator_info.version
version
Definition: get_generator_info.py:33
VP1Serialise::disableUnsavedChecks
void disableUnsavedChecks()
Definition: VP1Serialise.cxx:475
VP1CollectionWidget
Definition: VP1CollectionWidget.h:32
VP1DrawOptionsWidget
Definition: VP1DrawOptionsWidget.h:25
TRT_PAI_physicsConstants::mb
const double mb
1mb to cm2
Definition: TRT_PAI_physicsConstants.h:15
unit
const PlainObject unit() const
This is a plugin that makes Eigen look like CLHEP & defines some convenience methods.
Definition: AmgMatrixBasePlugin.h:20
VP1Serialise::result
QByteArray result()
Definition: VP1Serialise.cxx:129
VP1Serialise::Imp::expectsPersistification
bool expectsPersistification(const QWidget *w)
Definition: VP1Serialise.cxx:449
VP1HelperClassBase::message
void message(const QString &) const
Definition: VP1HelperClassBase.cxx:49
pickleTool.object
object
Definition: pickleTool.py:30
VP1Msg::verbose
static bool verbose()
Definition: VP1Msg.h:31
VP1Serialise::stream
QDataStream * stream()
Definition: VP1Serialise.cxx:115
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
python.compressB64.c
def c
Definition: compressB64.py:93
VP1CollectionSettingsButtonBase::saveState
QByteArray saveState() const
fill out with the state of the object (used for drag and drop etc)
Definition: VP1CollectionSettingsButtonBase.cxx:78
VP1Serialise::Imp::handle
void handle(QWidget *w)
Definition: VP1Serialise.cxx:54
VP1Serialise::version
qint32 version() const
Definition: VP1Serialise.cxx:122