Akonadi Contacts

kwidgetlister.cpp
1/* -*- c++ -*-
2
3 kwidgetlister.cpp
4
5 This file is part of libkdepim.
6 SPDX-FileCopyrightText: 2001 Marc Mutz <mutz@kde.org>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include "kwidgetlister_p.h"
12
13#include <QHBoxLayout>
14
15#include <QPushButton>
16#include <QVBoxLayout>
17
18#include <cassert>
19
20class KWidgetListerPrivate
21{
22public:
23 explicit KWidgetListerPrivate(KWidgetLister *qq)
24 : q(qq)
25 {
26 }
27
28 ~KWidgetListerPrivate()
29 {
30 qDeleteAll(mWidgetList);
31 mWidgetList.clear();
32 }
33
34 KWidgetLister *const q;
35 QVBoxLayout *mLayout = nullptr;
36 QWidget *mButtonBox = nullptr;
37 QList<QWidget *> mWidgetList;
38 int mMinWidgets = 0;
39 int mMaxWidgets = 0;
40};
41
42KWidgetLister::KWidgetLister(int minWidgets, int maxWidgets, QWidget *parent)
43 : QWidget(parent)
44 , d(new KWidgetListerPrivate(this))
45{
46 d->mMinWidgets = qMax(minWidgets, 1);
47 d->mMaxWidgets = qMax(maxWidgets, d->mMinWidgets + 1);
48 init();
49}
50
51KWidgetLister::~KWidgetLister() = default;
52
53void KWidgetLister::init()
54{
55 //--------- the button box
56 d->mLayout = new QVBoxLayout(this);
57 d->mLayout->setContentsMargins({});
58 d->mLayout->setSpacing(4);
59
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);
65}
66
67void KWidgetLister::slotMore()
68{
69 // the class should make certain that slotMore can't
70 // be called when mMaxWidgets are on screen.
71 assert((int)d->mWidgetList.count() < d->mMaxWidgets);
72
73 addWidgetAtEnd();
74}
75
76void KWidgetLister::slotFewer()
77{
78 // the class should make certain that slotFewer can't
79 // be called when mMinWidgets are on screen.
80 assert((int)d->mWidgetList.count() > d->mMinWidgets);
81
82 removeLastWidget();
83}
84
85void KWidgetLister::slotClear()
86{
87 setNumberOfShownWidgetsTo(d->mMinWidgets);
88
89 // clear remaining widgets
90 for (QWidget *widget : std::as_const(d->mWidgetList)) {
91 clearWidget(widget);
92 }
93
94 Q_EMIT clearWidgets();
95}
96
97void KWidgetLister::addWidgetAtEnd(QWidget *widget)
98{
99 if (!widget) {
100 widget = this->createWidget(this);
101 }
102
103 d->mLayout->insertWidget(d->mLayout->indexOf(d->mButtonBox), widget);
104 d->mWidgetList.append(widget);
105 widget->show();
106 Q_EMIT widgetAdded();
107 Q_EMIT widgetAdded(widget);
108}
109
110void KWidgetLister::removeLastWidget()
111{
112 // The layout will take care that the
113 // widget is removed from screen, too.
114 delete d->mWidgetList.takeLast();
115 Q_EMIT widgetRemoved();
116}
117
118void KWidgetLister::clearWidget(QWidget *widget)
119{
120 Q_UNUSED(widget)
121}
122
123QWidget *KWidgetLister::createWidget(QWidget *parent)
124{
125 return new QWidget(parent);
126}
127
128void KWidgetLister::setNumberOfShownWidgetsTo(int aNum)
129{
130 int superfluousWidgets = qMax((int)d->mWidgetList.count() - aNum, 0);
131 int missingWidgets = qMax(aNum - (int)d->mWidgetList.count(), 0);
132
133 // remove superfluous widgets
134 for (; superfluousWidgets; superfluousWidgets--) {
135 removeLastWidget();
136 }
137
138 // add missing widgets
139 for (; missingWidgets; missingWidgets--) {
140 addWidgetAtEnd();
141 }
142}
143
144QList<QWidget *> KWidgetLister::widgets() const
145{
146 return d->mWidgetList;
147}
148
149int KWidgetLister::widgetsMinimum() const
150{
151 return d->mMinWidgets;
152}
153
154int KWidgetLister::widgetsMaximum() const
155{
156 return d->mMaxWidgets;
157}
158
159void KWidgetLister::removeWidget(QWidget *widget)
160{
161 // The layout will take care that the
162 // widget is removed from screen, too.
163
164 if (d->mWidgetList.count() <= widgetsMinimum()) {
165 return;
166 }
167
168 const int index = d->mWidgetList.indexOf(widget);
169 QWidget *w = d->mWidgetList.takeAt(index);
170 w->deleteLater();
171 w = nullptr;
172 Q_EMIT widgetRemoved(widget);
173 Q_EMIT widgetRemoved();
174}
175
176void KWidgetLister::addWidgetAfterThisWidget(QWidget *currentWidget, QWidget *widget)
177{
178 if (!widget) {
179 widget = this->createWidget(this);
180 }
181
182 int index = d->mLayout->indexOf(currentWidget ? currentWidget : d->mButtonBox) + 1;
183 d->mLayout->insertWidget(index, widget);
184 if (currentWidget) {
185 index = d->mWidgetList.indexOf(currentWidget);
186 d->mWidgetList.insert(index + 1, widget);
187 } else {
188 d->mWidgetList.append(widget);
189 }
190 widget->show();
191
192 Q_EMIT widgetAdded();
193 Q_EMIT widgetAdded(widget);
194}
195
196#include "moc_kwidgetlister_p.cpp"
QCA_EXPORT void init()
void clear()
qsizetype indexOf(const AT &value, qsizetype from) const const
void deleteLater()
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.