Akonadi Contacts

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

KDE's Doxygen guidelines are available online.