11#include "kwidgetlister_p.h"
20class KWidgetListerPrivate
23 explicit KWidgetListerPrivate(KWidgetLister *qq)
28 ~KWidgetListerPrivate()
30 qDeleteAll(mWidgetList);
34 KWidgetLister *
const q;
42KWidgetLister::KWidgetLister(
int minWidgets,
int maxWidgets,
QWidget *parent)
44 , d(new KWidgetListerPrivate(this))
46 d->mMinWidgets = qMax(minWidgets, 1);
47 d->mMaxWidgets = qMax(maxWidgets, d->mMinWidgets + 1);
51KWidgetLister::~KWidgetLister() =
default;
53void KWidgetLister::init()
57 d->mLayout->setContentsMargins({});
58 d->mLayout->setSpacing(4);
60 d->mButtonBox =
new QWidget(
this);
61 auto mButtonBoxHBoxLayout =
new QHBoxLayout(d->mButtonBox);
62 mButtonBoxHBoxLayout->setContentsMargins({});
63 d->mLayout->addWidget(d->mButtonBox);
64 d->mLayout->addStretch(1);
67void KWidgetLister::slotMore()
71 assert((
int)d->mWidgetList.count() < d->mMaxWidgets);
76void KWidgetLister::slotFewer()
80 assert((
int)d->mWidgetList.count() > d->mMinWidgets);
85void KWidgetLister::slotClear()
87 setNumberOfShownWidgetsTo(d->mMinWidgets);
90 for (
QWidget *widget : std::as_const(d->mWidgetList)) {
94 Q_EMIT clearWidgets();
97void KWidgetLister::addWidgetAtEnd(
QWidget *widget)
100 widget = this->createWidget(
this);
103 d->mLayout->insertWidget(d->mLayout->indexOf(d->mButtonBox), widget);
104 d->mWidgetList.append(widget);
106 Q_EMIT widgetAdded();
107 Q_EMIT widgetAdded(widget);
110void KWidgetLister::removeLastWidget()
114 delete d->mWidgetList.takeLast();
115 Q_EMIT widgetRemoved();
118void KWidgetLister::clearWidget(
QWidget *widget)
128void KWidgetLister::setNumberOfShownWidgetsTo(
int aNum)
130 int superfluousWidgets = qMax((
int)d->mWidgetList.count() - aNum, 0);
131 int missingWidgets = qMax(aNum - (
int)d->mWidgetList.count(), 0);
134 for (; superfluousWidgets; superfluousWidgets--) {
139 for (; missingWidgets; missingWidgets--) {
146 return d->mWidgetList;
149int KWidgetLister::widgetsMinimum()
const
151 return d->mMinWidgets;
154int KWidgetLister::widgetsMaximum()
const
156 return d->mMaxWidgets;
159void KWidgetLister::removeWidget(
QWidget *widget)
164 if (d->mWidgetList.count() <= widgetsMinimum()) {
168 const int index = d->mWidgetList.
indexOf(widget);
169 QWidget *w = d->mWidgetList.takeAt(index);
172 Q_EMIT widgetRemoved(widget);
173 Q_EMIT widgetRemoved();
176void KWidgetLister::addWidgetAfterThisWidget(
QWidget *currentWidget,
QWidget *widget)
179 widget = this->createWidget(
this);
182 int index = d->mLayout->indexOf(currentWidget ? currentWidget : d->mButtonBox) + 1;
183 d->mLayout->insertWidget(index, widget);
185 index = d->mWidgetList.indexOf(currentWidget);
186 d->mWidgetList.insert(index + 1, widget);
188 d->mWidgetList.append(widget);
192 Q_EMIT widgetAdded();
193 Q_EMIT widgetAdded(widget);
196#include "moc_kwidgetlister_p.cpp"
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
qsizetype indexOf(const AT &value, qsizetype from) const const