Skip to content

Commit a4d91cb

Browse files
committed
Added some more API functions
You may get a vector containing the index of all open ContentPanes. Also it is possible to allow multiple content panes to be open at once. With the collapsible option you can decide if one open has always to be open. Signed-off-by: Christian Rapp <0x2a@posteo.org>
1 parent 9eb792b commit a4d91cb

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
lines changed

include/qAccordion/qaccordion.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class QAccordion : public QWidget
9898
* @details
9999
* This is an overloaded method of addContentPane(QString), that
100100
* allows you to provide your own content frame.
101+
* @warning
102+
* Headers have to be unique
101103
*/
102104
int addContentPane(QString header, QFrame *contentFrame);
103105
/**
@@ -135,6 +137,8 @@ class QAccordion : public QWidget
135137
* This is an overloaded method of insertContentPane(uint, QString).
136138
* Use this method when you already created a content frame that you want to
137139
* insert.
140+
* @warning
141+
* Headers have to be unique
138142
*/
139143
bool insertContentPane(uint index, QString header, QFrame *contentFrame);
140144
/**
@@ -263,12 +267,48 @@ class QAccordion : public QWidget
263267
*/
264268
int getContentPaneIndex(ContentPane *contentPane);
265269

270+
/**
271+
* @brief Get the index of the active ContentPane
272+
* @return Vector with indexes of all active ContentPanes
273+
*
274+
* @details
275+
* This method will fill a vector with the index of all active ContentPanes.
276+
* The vector will be empty if no ContentPane is active
277+
*/
278+
void getActiveContentPaneIndex(std::vector<int> &indexVector);
279+
266280
/**
267281
* @brief Get the number of content panes
268282
* @return Number of content panes
269283
*/
270284
int getNumberOfContentPanes();
271285

286+
/**
287+
* @brief Allow multiple ContentPane to be open
288+
* @param status
289+
*
290+
* @details
291+
* This option allows you to open several ContentPane at the same time.
292+
* @note
293+
* Default value for this option is \p false.
294+
*/
295+
void setMultiActive(bool status);
296+
/**
297+
* @brief Check status of multiActive
298+
* @return bool
299+
*
300+
* @sa
301+
* setMultiActive()
302+
*/
303+
bool getMultiActive();
304+
305+
/**
306+
* @brief setCollapsible
307+
* @param status
308+
*/
309+
void setCollapsible(bool status);
310+
bool getCollapsible();
311+
272312
/**
273313
* @brief Get error string
274314
* @return Error string
@@ -298,6 +338,9 @@ public slots:
298338

299339
QString errorString;
300340

341+
bool multiActive;
342+
bool collapsible;
343+
301344
int internalAddContentPane(QString header, QFrame *cframe = nullptr,
302345
ContentPane *cpane = nullptr);
303346
bool internalInsertContentPane(uint index, QString header,
@@ -313,6 +356,9 @@ public slots:
313356
bool checkIndexError(uint index, bool sizeIndexAllowed,
314357
const QString &errMessage);
315358

359+
private slots:
360+
void numberOfPanesChanged(int number);
361+
316362
protected:
317363
/**
318364
* @brief paintEvent Reimplement paintEvent to use stylesheets in derived Widgets

src/qaccordion.cpp

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3744
int 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+
174198
int 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+
176208
QString QAccordion::getError() { return this->errorString; }
177209

178210
int 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+
383430
void QAccordion::paintEvent(__attribute__((unused)) QPaintEvent *event)
384431
{
385432
QStyleOption o;

0 commit comments

Comments
 (0)