ATLAS Offline Software
Loading...
Searching...
No Matches
VP1CollectionWidget.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 VP1CollectionWidget //
9// //
10// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11// Initial version: June 2008 //
12// //
14
18#include "VP1Base/VP1Msg.h"
19
20#include <QSpacerItem>
21#include <QGridLayout>
22#include <QLabel>
23#include <QCheckBox>
24#include <QSet>
25#include <QList>
26#include <QRegExp>
27
28//____________________________________________________________________
30public:
33 QWidget * collWidget;
35 QList<QWidget *> widgetsFromCollections;
36 QList<VP1Collection*> collections;
39 QList<VP1StdCollection*> last_visibleStdCollections;
40 QList<qint32> visibleStdCollectionTypesFromVisStdCols(const QList<VP1StdCollection*>& l) const;
42 void clearGui(bool deleteGuiElements);
43
45
46};
47
48//____________________________________________________________________
50 : QWidget(parent), VP1HelperClassBase(0,"VP1CollectionWidget"), m_d(new Imp(this))
51{
52}
53
54//____________________________________________________________________
59
60//____________________________________________________________________
61const QList<VP1Collection*>& VP1CollectionWidget::collections() const
62{
63 return m_d->collections;
64}
65
66//____________________________________________________________________
67void VP1CollectionWidget::Imp::clearGui(bool deleteGuiElements)
68{
69 if (!deleteGuiElements) {
70 for(QWidget *w : widgetsFromCollections) {
71 w->hide();
72 w->setParent(0);
73 }
74 }
75 delete collWidget;
76 collWidget = 0;
77
78}
79
80//____________________________________________________________________
81void VP1CollectionWidget::clear(bool deleteCollections,bool deleteGuiElements)
82{
83 m_d->updateStatesWithCurrentInfo();
84 m_d->clearGui(deleteGuiElements);
85 m_d->widgetsFromCollections.clear();
86
87 if (deleteCollections) {
88 for(VP1Collection*col : m_d->collections)
89 delete col;
90 }
91 m_d->collections.clear();
92
93 //Fixme: update report-on-width
94 m_d->appropriateFixedWidth = 0;
95
98}
99
100//____________________________________________________________________
102{
103 theclass->setUpdatesEnabled(false);
104 if (collWidget)
105 clearGui(false);
106
107 collWidget = new QWidget();//We only add the widget in the end (to avoid needless updates).
108
109 QVBoxLayout * vlayout = new QVBoxLayout;
110 vlayout->setSpacing(0);
111 vlayout->setMargin(0);
112 if (collections.isEmpty()) {
113 QHBoxLayout * labellayout = new QHBoxLayout;
114 labellayout->setSpacing(0);
115 labellayout->addWidget(new QLabel("No collections in event."),0,Qt::AlignLeft);
116 labellayout->addStretch(1);
117 vlayout->addLayout(labellayout);
118 }
119
120 QSet<QString> sections;
121 for (VP1Collection* col :collections)
122 sections.insert(col->section());
123 QList<QString> sectionsSorted = sections.values();
124 std::sort(sectionsSorted.begin(), sectionsSorted.end());
125 theclass->sortSections(sectionsSorted);
126
127 const bool nosectionlabels = sectionsSorted.count() == 1 && sectionsSorted.at(0).isEmpty();
128
129 int maxFirstColumnCheckBoxWidth(0);
130 QList<QCheckBox*> firstColumnCheckBoxes;
131
132 int gridmargins(0);
133 for (const QString& section : sectionsSorted) {
134 //type section header:
135 QLabel * sectionlabel(0);
136 if (!nosectionlabels) {
137 QHBoxLayout * labellayout = new QHBoxLayout;
138 labellayout->setSpacing(0);
139 sectionlabel =new QLabel;
140 sectionlabel->setTextFormat(Qt::RichText);
141 sectionlabel->setText("<b>"+section+"</b>");
142 labellayout->addWidget(sectionlabel,0,Qt::AlignLeft);
143 labellayout->addStretch(1);
144 vlayout->addLayout(labellayout);
145 }
146 //Add individual widgets for collections:
147 QGridLayout * gridLayout = new QGridLayout;
148 gridLayout->setHorizontalSpacing(6);
149 gridLayout->setVerticalSpacing(0);
150 gridLayout->setMargin(0);
151 if (!nosectionlabels)
152 gridLayout->addItem(new QSpacerItem(10, 1, QSizePolicy::Fixed, QSizePolicy::Fixed),0,0);
153 vlayout->addLayout(gridLayout);
154 for (VP1Collection* col : collections) {
155 if (col->section()!=section)
156 continue;
157 if (sectionlabel) {
158 if (!col->sectionToolTip().isEmpty())
159 sectionlabel->setToolTip(col->sectionToolTip());
160 sectionlabel = 0;
161 }
162 int newrow = gridLayout->rowCount();
163 int i(1);
164 for (QWidget*w : col->widgetsForGuiRow()) {
165 if (!w) {
166 theclass->message("WARNING: Ignoring null widget provided by widgetsForGuiRow() (perhaps due to a VP1StdCollection you didn't init()?)");
167 continue;
168 }
169 //Smaller font:
170 QFont f(w->font());
171 // f.setPointSizeF(f.pointSizeF()*0.9);
172 w->setFont(f);
173 int maxheight = static_cast<int>(0.5+QFontMetricsF(f).height()*1.05+2);
174 w->setMaximumHeight(maxheight);
175 VP1MaterialButton * mb = dynamic_cast<VP1MaterialButton *>(w);
176 if (mb)
177 mb->setDimension(maxheight);
178 if (i==1) {
179 QCheckBox * cb = dynamic_cast<QCheckBox *>(w);
180 if (cb) {
181 firstColumnCheckBoxes << cb;
182 if (maxFirstColumnCheckBoxWidth<cb->sizeHint().width())
183 maxFirstColumnCheckBoxWidth = cb->sizeHint().width();
184 }
185 }
187 w->setParent(collWidget);
188 w->setVisible(true);
189 gridLayout->addWidget( w, newrow, i++);
190 }
191 gridLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding),newrow,i);
192 }
193 if (gridmargins==0) {
194 int left, top, right, bottom;
195 gridLayout->getContentsMargins ( &left, &top, &right, &bottom );
196 gridmargins = left+right;
197 }
198 }
199
200 if (maxFirstColumnCheckBoxWidth>0) {
201 for (QCheckBox *cb : firstColumnCheckBoxes)
202 cb->setMinimumWidth(maxFirstColumnCheckBoxWidth);
203 }
204
205 vlayout->addStretch(1);
206
207 QHBoxLayout * hlayout = new QHBoxLayout;
208 hlayout->setMargin(0);
209 hlayout->setSpacing(0);
210 hlayout->addLayout(vlayout);
211 hlayout->addStretch(1);
212 collWidget->setLayout(hlayout);
213
214 //Put collWidget into the collection widget:
215 delete theclass->layout();
216 QHBoxLayout * hlayoutPageCol = new QHBoxLayout;
217 collWidget->setParent(theclass);
218 hlayoutPageCol->addWidget(collWidget);
219 theclass->setLayout(hlayoutPageCol);
220 theclass->setContentsMargins(0,0,0,0);
221 collWidget->setContentsMargins(0,0,0,0);
222 hlayoutPageCol->setContentsMargins(0,0,0,0);
223 //Fixme: reshuffle the above or turn off updates to avoid flicker.
224
225 theclass->setContentsMargins(0,0,0,0);
226 collWidget->setContentsMargins(0,0,0,0);
227 appropriateFixedWidth = collWidget->sizeHint().width()//Fixme: before we add the collwidget.
228 +gridmargins;
229
230 theclass->setUpdatesEnabled(true);
231 theclass->visibleContentsChanged();
232
233}
234
235
236//____________________________________________________________________
237void VP1CollectionWidget::addCollections(const QList<VP1Collection*>& cols, bool applySavedStates )
238{
239 messageVerbose("addCollections called with "+str(cols.count())+" new collections");
240
241 m_d->collections << cols;//FIXME: Test not already there.
242
243 m_d->repopulateGUIFromCollections();
244
245 if (applySavedStates)
246 VP1Collection::applyStates(m_d->collections, m_d->states);
247
249 for (VP1Collection* col : cols) {
250 VP1StdCollection* stdcol = dynamic_cast<VP1StdCollection*>(col);
251 if (stdcol)
252 connect(stdcol,SIGNAL(visibilityChanged(bool)),this,SLOT(possibleChange_visibleStdCollections()));
253 }
254
255}
256
257//____________________________________________________________________
258void VP1CollectionWidget::setCollections(const QList<VP1Collection*>& cols, bool applySavedStates)
259{
260 m_d->collections = cols;
261
262 m_d->repopulateGUIFromCollections();
263
264 if (applySavedStates)
265 VP1Collection::applyStates(m_d->collections, m_d->states);
266
268 for (VP1Collection* col : cols) {
269 VP1StdCollection* stdcol = dynamic_cast<VP1StdCollection*>(col);
270 if (stdcol)
271 connect(stdcol,SIGNAL(visibilityChanged(bool)),this,SLOT(possibleChange_visibleStdCollections()));
272 }
273}
274
275//____________________________________________________________________
277{
278 return m_d->appropriateFixedWidth;
279}
280
281//____________________________________________________________________
287
288//____________________________________________________________________
289void VP1CollectionWidget::addStateInfo(const VP1CollStates& newinfo, bool overwritesExisting )
290{
291 m_d->updateStatesWithCurrentInfo();
292 QMapIterator<QByteArray,QByteArray> it(newinfo);
293 while (it.hasNext()) {
294 it.next();
295 if (!overwritesExisting&&m_d->states.contains(it.key()))
296 continue;
297 m_d->states.insert(it.key(),it.value());
298 }
299 if (overwritesExisting)
300 VP1Collection::applyStates(m_d->collections, newinfo);
301}
302
303//____________________________________________________________________
305{
306 m_d->updateStatesWithCurrentInfo();
307 return m_d->states;
308}
309
310
311//____________________________________________________________________
312QList<qint32> VP1CollectionWidget::Imp::visibleStdCollectionTypesFromVisStdCols(const QList<VP1StdCollection*>& l) const
313{
314 QSet<qint32> vt;
315 for (VP1StdCollection* stdcol : l)
316 vt.insert(stdcol->collTypeID());
317 QList<qint32> vistypes = vt.values();
318 std::sort(vistypes.begin(), vistypes.end());
319 return vistypes;
320
321}
322
323//____________________________________________________________________
325{
326 return m_d->visibleStdCollectionTypesFromVisStdCols(visibleStdCollections());
327}
328
329//____________________________________________________________________
330QList<VP1StdCollection*> VP1CollectionWidget::visibleStdCollections() const
331{
332 QList<VP1StdCollection*> l;
334 if (stdcol->visible())
335 l << stdcol;
336 }
337 return l;
338}
339
340//____________________________________________________________________
342{
343 QList<VP1StdCollection*> visstdcols = visibleStdCollections();
344 QList<qint32> vistypes = m_d->visibleStdCollectionTypesFromVisStdCols(visstdcols);
345
346 if ( m_d->last_visibleStdCollections != visstdcols ) {
347 m_d->last_visibleStdCollections = visstdcols;
349 }
350
351 if ( m_d->last_visibleStdCollectionTypes != vistypes ) {
352 m_d->last_visibleStdCollectionTypes = vistypes;
354 }
355
356}
357
358
359
360//____________________________________________________________________
361void VP1CollectionWidget::ensureFirst(const QString& wildcard,QList<QString>& strs)
362{
363 QRegExp rx(wildcard,Qt::CaseInsensitive,QRegExp::Wildcard);
364 QList<QString> l;
365 for (const QString& str : strs)
366 if (rx.exactMatch(str))
367 l << str;
368 for (const QString& str : l)
369 strs.removeAll(str);
370 for (const QString& str : strs)
371 l << str;
372 strs = std::move(l);
373
374}
375
376//____________________________________________________________________
377void VP1CollectionWidget::ensureLast(const QString& wildcard,QList<QString>& strs)
378{
379 QRegExp rx(wildcard,Qt::CaseInsensitive,QRegExp::Wildcard);
380 QList<QString> l;
381 for(const QString& str : strs)
382 if (rx.exactMatch(str))
383 l << str;
384 for (const QString& str : l)
385 strs.removeAll(str);
386 strs << l;
387}
static Double_t tc
const double width
void section(const std::string &sec)
@ top
QMap< QByteArray, QByteArray > VP1CollStates
Imp(VP1CollectionWidget *tc)
QList< VP1StdCollection * > last_visibleStdCollections
void clearGui(bool deleteGuiElements)
QList< VP1Collection * > collections
VP1CollectionWidget * theclass
QList< QWidget * > widgetsFromCollections
QList< qint32 > visibleStdCollectionTypesFromVisStdCols(const QList< VP1StdCollection * > &l) const
QList< qint32 > last_visibleStdCollectionTypes
void setCollections(const QList< VP1Collection * > &, bool applySavedStates=true)
void visibleStdCollectionsChanged(const QList< VP1StdCollection * > &)
VP1CollectionWidget(QWidget *parent=0)
void addCollections(const QList< VP1Collection * > &, bool applySavedStates=true)
void addStateInfo(const VP1CollStates &, bool overwritesExisting=true)
void visibleStdCollectionTypesChanged(const QList< qint32 > &)
const QList< VP1Collection * > & collections() const
static void ensureFirst(const QString &wildcard, QList< QString > &)
QList< qint32 > visibleStdCollectionTypes() const
static void ensureLast(const QString &wildcard, QList< QString > &)
QList< VP1StdCollection * > visibleStdCollections() const
VP1CollStates states() const
void clear(bool deleteCollections=true, bool deleteGuiElements=false)
void visibleContentsChanged()
static void updateStates(VP1CollStates &state, const VP1CollStates &newInfo)
static void applyStates(const QList< VP1Collection * > &, const VP1CollStates &)
static VP1CollStates getStates(const QList< VP1Collection * > &)
VP1HelperClassBase(IVP1System *sys=0, QString helpername="")
void messageVerbose(const QString &) const
static void messageDebug(const QString &)
Definition VP1Msg.cxx:39
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.