ATLAS Offline Software
Loading...
Searching...
No Matches
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
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//____________________________________________________________________
43public:
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//____________________________________________________________________
78
79//____________________________________________________________________
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;
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//____________________________________________________________________
114QDataStream * VP1Serialise::stream()
115{
116 return m_d->state;
117}
118
119
120//____________________________________________________________________
122{
123 return m_d->version;
124}
125
126
127//____________________________________________________________________
128const QByteArray& VP1Serialise::result()
129{
130 return m_d->byteArray;
131}
132
133//____________________________________________________________________
134void 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//____________________________________________________________________
144void 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//____________________________________________________________________
156void 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//____________________________________________________________________
166void 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//____________________________________________________________________
176void 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//____________________________________________________________________
186void 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//____________________________________________________________________
196void 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//____________________________________________________________________
215void VP1Serialise::save(qint32 i)
216{
217 if (VP1Msg::verbose()){
218 messageVerbose("Saving int "+str(i));
219 }
220 (*m_d->state) << i;
221}
222
223//____________________________________________________________________
224void 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//____________________________________________________________________
233void VP1Serialise::save(const QString& s)
234{
235 if (VP1Msg::verbose()){
236 messageVerbose("Saving string "+s);
237 }
238 (*(m_d->state)) << s;
239}
240
241//____________________________________________________________________
242void 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//____________________________________________________________________
251void 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//____________________________________________________________________
272void 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//____________________________________________________________________
298void 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//____________________________________________________________________
333void 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//____________________________________________________________________
432void 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//____________________________________________________________________
442void 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//____________________________________________________________________
480void 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}
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
QByteArray saveState() const
fill out with the state of the object (used for drag and drop etc)
VP1CollStates states() const
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 QByteArray serialiseSoMaterial(SoMaterial *)
Imp(VP1Serialise *tc)
QSet< const QWidget * > handledWidgets
bool expectsPersistification(const QWidget *w)
QDataStream * state
VP1Serialise * theclass
QSet< const QWidget * > ignoredWidgets
void handle(const QWidget *w)
static unsigned numberOfInstantiations
void saveByTitle(QToolBox *)
void disableUnsavedChecks()
void save(bool)
static unsigned numberOfInstantiations()
void ignoreWidget(const QWidget *)
qint32 version() const
VP1Serialise(qint32 version, IVP1System *sys=0)
QDataStream * stream()
static void decrementNumberOfInstantiations()
void warnUnsaved(const QObject *)
void widgetHandled(const QWidget *)
virtual ~VP1Serialise()
const QByteArray & result()
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146