@@ -184,7 +184,7 @@ void QAccordion::getActiveContentPaneIndex(std::vector<int> &indexVector)
184184 indexVector.clear ();
185185 std::for_each (this ->contentPanes .begin (), this ->contentPanes .end (),
186186 [&indexVector, this ](ContentPane *pane) {
187- if (pane->getOpen ()) {
187+ if (pane->getActive ()) {
188188 indexVector.push_back (
189189 this ->findContentPaneIndex (" " , nullptr , pane));
190190 }
@@ -223,27 +223,8 @@ int QAccordion::internalAddContentPane(QString header, QFrame *cframe,
223223 this ->contentPanes .push_back (cpane);
224224
225225 // manage the clicked signal in a lambda expression
226- QObject::connect (cpane, &ContentPane::clicked, [this , cpane]() {
227- // if the clicked content pane is open we simply close it and return
228- if (cpane->getOpen ()) {
229- cpane->closeContentPane ();
230- return ;
231- }
232- // if it is not open we will open it and search our vector for other
233- // panes that are already open.
234- // TODO: Is it really necessary to search for more than one open cpane?
235- if (!cpane->getOpen ()) {
236- // check if multiActive is allowed
237- if (!this ->getMultiActive ()) {
238- std::for_each (this ->contentPanes .begin (),
239- this ->contentPanes .end (), [](ContentPane *pane) {
240- if (pane->getOpen ())
241- pane->closeContentPane ();
242- });
243- }
244- cpane->openContentPane ();
245- }
246- });
226+ QObject::connect (cpane, &ContentPane::clicked,
227+ [this , cpane]() { this ->handleClickedSignal (cpane); });
247228
248229 emit numberOfContentPanesChanged (this ->contentPanes .size ());
249230
@@ -276,29 +257,9 @@ bool QAccordion::internalInsertContentPane(uint index, QString header,
276257
277258 this ->contentPanes .insert (this ->contentPanes .begin () + index, cpane);
278259
279- // TODO: This code has to be merged with internalAddContentPane.
280260 // manage the clicked signal in a lambda expression
281- QObject::connect (cpane, &ContentPane::clicked, [this , cpane]() {
282- // if the clicked content pane is open we simply close it and return
283- if (cpane->getOpen ()) {
284- cpane->closeContentPane ();
285- return ;
286- }
287- // if it is not open we will open it and search our vector for other
288- // panes that are already open.
289- // TODO: Is it really necessary to search for more than one open cpane?
290- if (!cpane->getOpen ()) {
291- // check if multiActive is allowed
292- if (!this ->getMultiActive ()) {
293- std::for_each (this ->contentPanes .begin (),
294- this ->contentPanes .end (), [](ContentPane *pane) {
295- if (pane->getOpen ())
296- pane->closeContentPane ();
297- });
298- }
299- cpane->openContentPane ();
300- }
301- });
261+ QObject::connect (cpane, &ContentPane::clicked,
262+ [this , cpane]() { this ->handleClickedSignal (cpane); });
302263
303264 emit numberOfContentPanesChanged (this ->contentPanes .size ());
304265
@@ -404,6 +365,42 @@ bool QAccordion::checkIndexError(uint index, bool sizeIndexAllowed,
404365 return false ;
405366}
406367
368+ void QAccordion::handleClickedSignal (ContentPane *cpane)
369+ {
370+ // if the clicked content pane is open we simply close it and return
371+ if (cpane->getActive ()) {
372+ // if collapsible and multiActive are false we are not allowed to close
373+ // this pane
374+ if (!this ->collapsible && !this ->multiActive ) {
375+ return ;
376+ }
377+ // when multiActive is true we have to check if there is any other open
378+ // cpane. if so we can close this one
379+ std::vector<int > activePanes;
380+ if (!this ->collapsible ) {
381+ this ->getActiveContentPaneIndex (activePanes);
382+ if (activePanes.size () == 1 )
383+ return ; // only one active --> good bye :)
384+ }
385+ cpane->closeContentPane ();
386+ return ;
387+ }
388+ // if it is not open we will open it and search our vector for other
389+ // panes that are already open.
390+ // TODO: Is it really necessary to search for more than one open cpane?
391+ if (!cpane->getActive ()) {
392+ // check if multiActive is allowed
393+ if (!this ->getMultiActive ()) {
394+ std::for_each (this ->contentPanes .begin (), this ->contentPanes .end (),
395+ [](ContentPane *pane) {
396+ if (pane->getActive ())
397+ pane->closeContentPane ();
398+ });
399+ }
400+ cpane->openContentPane ();
401+ }
402+ }
403+
407404void QAccordion::numberOfPanesChanged (int number)
408405{
409406 // automatically open contentpane if we have only one and collapsible is
0 commit comments