@@ -23,6 +23,9 @@ QAccordion::QAccordion(QWidget *parent) : QWidget(parent)
2323 // make sure our resource file gets initialized
2424 Q_INIT_RESOURCE (qaccordionicons);
2525
26+ this ->multiActive = false ;
27+ this ->collapsible = true ;
28+
2629 // set our basic layout
2730 this ->setLayout (new QVBoxLayout ());
2831
@@ -32,6 +35,10 @@ QAccordion::QAccordion(QWidget *parent) : QWidget(parent)
3235 this ->layout ()->setContentsMargins (QMargins ());
3336 // TODO: Do we need to keep a pointer to the spacer?
3437 this ->spacer = dynamic_cast <QSpacerItem *>(this ->layout ()->itemAt (0 ));
38+
39+ // seome things we want to do if the number of panes change
40+ QObject::connect (this , &QAccordion::numberOfContentPanesChanged, this ,
41+ &QAccordion::numberOfPanesChanged);
3542}
3643
3744int QAccordion::numberOfContentPanes () { return this ->contentPanes .size (); }
@@ -171,8 +178,33 @@ int QAccordion::getContentPaneIndex(ContentPane *contentPane)
171178 return this ->findContentPaneIndex (" " , nullptr , contentPane);
172179}
173180
181+ void QAccordion::getActiveContentPaneIndex (std::vector<int > &indexVector)
182+ {
183+ // first of all make sure it is empty
184+ indexVector.clear ();
185+ std::vector<ContentPane *>::const_iterator it = this ->contentPanes .begin ();
186+ while (it != this ->contentPanes .end ()) {
187+ it = std::find_if (this ->contentPanes .begin (), this ->contentPanes .end (),
188+ [this , &indexVector](ContentPane *cpane) {
189+ return cpane->getOpen ();
190+ });
191+ if (it != this ->contentPanes .end ()) {
192+ indexVector.push_back (
193+ this ->findContentPaneIndex (" " , nullptr , (*it)));
194+ }
195+ }
196+ }
197+
174198int QAccordion::getNumberOfContentPanes () { return this ->contentPanes .size (); }
175199
200+ void QAccordion::setMultiActive (bool status) { this ->multiActive = status; }
201+
202+ bool QAccordion::getMultiActive () { return this ->multiActive ; }
203+
204+ void QAccordion::setCollapsible (bool status) { this ->collapsible = status; }
205+
206+ bool QAccordion::getCollapsible () { return this ->collapsible ; }
207+
176208QString QAccordion::getError () { return this ->errorString ; }
177209
178210int QAccordion::internalAddContentPane (QString header, QFrame *cframe,
@@ -205,14 +237,17 @@ int QAccordion::internalAddContentPane(QString header, QFrame *cframe,
205237 // panes that are already open.
206238 // TODO: Is it really necessary to search for more than one open cpane?
207239 if (!cpane->getOpen ()) {
208- std::vector<ContentPane *>::const_iterator it =
209- this ->contentPanes .begin ();
210- while (it != this ->contentPanes .end ()) {
211- it = std::find_if (
212- this ->contentPanes .begin (), this ->contentPanes .end (),
213- [](ContentPane *cpane) { return cpane->getOpen (); });
214- if (it != this ->contentPanes .end ()) {
215- (*it)->closeContentPane ();
240+ // check if multiple open is allowed
241+ if (!this ->getMultiActive ()) {
242+ std::vector<ContentPane *>::const_iterator it =
243+ this ->contentPanes .begin ();
244+ while (it != this ->contentPanes .end ()) {
245+ it = std::find_if (
246+ this ->contentPanes .begin (), this ->contentPanes .end (),
247+ [](ContentPane *cpane) { return cpane->getOpen (); });
248+ if (it != this ->contentPanes .end ()) {
249+ (*it)->closeContentPane ();
250+ }
216251 }
217252 }
218253 cpane->openContentPane ();
@@ -262,14 +297,17 @@ bool QAccordion::internalInsertContentPane(uint index, QString header,
262297 // panes that are already open.
263298 // TODO: Is it really necessary to search for more than one open cpane?
264299 if (!cpane->getOpen ()) {
265- std::vector<ContentPane *>::const_iterator it =
266- this ->contentPanes .begin ();
267- while (it != this ->contentPanes .end ()) {
268- it = std::find_if (
269- this ->contentPanes .begin (), this ->contentPanes .end (),
270- [](ContentPane *cpane) { return cpane->getOpen (); });
271- if (it != this ->contentPanes .end ()) {
272- (*it)->closeContentPane ();
300+ // check if multiple open is allowed
301+ if (!this ->getMultiActive ()) {
302+ std::vector<ContentPane *>::const_iterator it =
303+ this ->contentPanes .begin ();
304+ while (it != this ->contentPanes .end ()) {
305+ it = std::find_if (
306+ this ->contentPanes .begin (), this ->contentPanes .end (),
307+ [](ContentPane *cpane) { return cpane->getOpen (); });
308+ if (it != this ->contentPanes .end ()) {
309+ (*it)->closeContentPane ();
310+ }
273311 }
274312 }
275313 cpane->openContentPane ();
@@ -380,6 +418,15 @@ bool QAccordion::checkIndexError(uint index, bool sizeIndexAllowed,
380418 return false ;
381419}
382420
421+ void QAccordion::numberOfPanesChanged (int number)
422+ {
423+ // automatically open contentpane if we have only one and collapsible is
424+ // false
425+ if (number == 1 && this ->collapsible == false ) {
426+ this ->contentPanes .at (0 )->openContentPane ();
427+ }
428+ }
429+
383430void QAccordion::paintEvent (__attribute__((unused)) QPaintEvent *event)
384431{
385432 QStyleOption o;
0 commit comments