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