Debian Codegraph @

widgets/buttonoverlay.cpp

raw symbols SCIP
1 #include "./buttonoverlay.h"
2 #include "./iconbutton.h"
3
4 #include <QAction>
5 #include <QComboBox>
6 #include <QCursor>
7 #include <QHBoxLayout>
8 #include <QIcon>
9 #include <QLineEdit>
10 #include <QStyle>
11 #include <QStyleOption>
12 #include <QToolTip>
13 #include <QWidget>
14
15 #include <functional>
16
17 namespace QtUtilities {
18
19 /*!
20 * \class ButtonOverlay
21 * \brief The ButtonOverlay class is used to display buttons on top of other widgets.
22 *
23 * This class had been created before QLineEdit's functions setClearButtonEnabled() and
24 * addAction() have been available. (These functions have been available only since Qt 5.2.)
25 *
26 * The downside of the "custom approach" compared to QLineEdit's own functions is that the
27 * buttons are shown over the text as the text margins are not updated accordingly. Hence
28 * the ButtonOverlay class has been updated to use QLineEdit's functions internally when the
29 * specified widget is QLineEdit-based and its QLineEdit has been passed to the constructor.
30 * However, when using any functions which can not be implemented using QLineEdit's own
31 * functions, the ButtonOverlay has to fallback to its "custom approach". All functions which
32 * cause this have a remark in their documentation.
33 *
34 * When QLineEdit's functions can not be used, the ButtonOverlay class creates a new layout
35 * manager and sets it to the widget specified when constructing an instance. Thus this widget
36 * must not already have a layout manager.
37 *
38 * The class is used to implement widget customization like ClearLineEidt and ClearComboBox
39 * and most of the times it makes sense to use these widgets instead of using ButtonOverlay
40 * directly.
41 */
42
43 /*!
44 * \brief Constructs a button overlay for the specified \a widget.
45 * \param widget Specifies the widget to display the buttons on.
46 * \remarks This function enforces the "custom approach" mentioned in the class documentation
47 * and should therefore be avoided.
48 */
49 ButtonOverlay::ButtonOverlay(QWidget *widget)
50 : m_widget(widget)
51 , m_buttonWidget(nullptr)
52 , m_buttonLayout(nullptr)
53 , m_clearButton(nullptr)
54 , m_infoButtonOrAction(nullptr)
55 {
56 fallbackToUsingCustomLayout();
57 }
58
59 /*!
60 * \brief Constructs a button overlay for the specified \a widget.
61 * \param widget Specifies the widget to display the buttons on.
62 * \param lineEdit Specifies the line edit used by \a widget to use the QLineEdit's functions
63 * for adding actions instead of a custom layout.
64 */
65 ButtonOverlay::ButtonOverlay(QWidget *widget, QLineEdit *lineEdit)
66 : m_widget(widget)
67 , m_buttonWidget(lineEdit)
68 , m_buttonLayout(nullptr)
69 , m_clearButton(nullptr)
70 , m_infoButtonOrAction(nullptr)
71 {
72 if (!m_buttonWidget) {
73 fallbackToUsingCustomLayout();
74 }
75 }
76
77 /*!
78 * \brief Destroys the button overlay.
79 */
80 ButtonOverlay::~ButtonOverlay()
81 {
82 }
83
84 /*!
85 * \brief Returns whether the "custom approach" mentioned in the class documentation is used.
86 */
87 bool ButtonOverlay::isUsingCustomLayout() const
88 {
89 return m_buttonLayout != nullptr;
90 }
91
92 /*!
93 * \brief Returns the layout manager holding the buttons.
94 * \remarks This function enforces the "custom approach" mentioned in the class documentation
95 * and should therefore be avoided.
96 */
97 QHBoxLayout *ButtonOverlay::buttonLayout()
98 {
99 fallbackToUsingCustomLayout();
100 return m_buttonLayout;
101 }
102
103 /*!
104 * \brief Returns whether the clear button is enabled.
105 */
106 bool ButtonOverlay::isClearButtonEnabled() const
107 {
108 if (isUsingCustomLayout()) {
109 return m_clearButton != nullptr;
110 }
111 return lineEditForWidget()->isClearButtonEnabled();
112 }
113
114 /*!
115 * \brief Returns whether the info button is enabled.
116 */
117 bool ButtonOverlay::isInfoButtonEnabled() const
118 {
119 return m_infoButtonOrAction != nullptr;
120 }
121
122 /*!
123 * \brief Sets whether the clear button is enabled.
124 */
125 void ButtonOverlay::setClearButtonEnabled(bool enabled)
126 {
127 if (auto *const le = lineEditForWidget()) {
128 le->setClearButtonEnabled(enabled);
129 return;
130 }
131 const auto clearButtonEnabled = isClearButtonEnabled();
132 if (clearButtonEnabled && !enabled) {
133 // disable clear button
134 m_buttonLayout->removeWidget(m_clearButton);
135 delete m_clearButton;
136 m_clearButton = nullptr;
137 } else if (!clearButtonEnabled && enabled) {
138 // enable clear button
139 m_clearButton = new IconButton;
140 m_clearButton->setHidden(isCleared());
141 m_clearButton->setPixmap(QIcon::fromTheme(QStringLiteral("edit-clear")).pixmap(IconButton::defaultPixmapSize));
142 m_clearButton->setGeometry(QRect(QPoint(), IconButton::defaultPixmapSize));
143 m_clearButton->setToolTip(QObject::tr("Clear"));
144 QObject::connect(m_clearButton, &IconButton::clicked, std::bind(&ButtonOverlay::handleClearButtonClicked, this));
145 m_buttonLayout->addWidget(m_clearButton);
146 }
147 }
148
149 /*!
150 * \brief Shows an info button with the specified \a pixmap and \a infoText.
151 *
152 * If there is already an info button enabled, it gets replaced with the new
153 * button.
154 *
155 * \sa ButtonOverlay::disableInfoButton()
156 */
157 void ButtonOverlay::enableInfoButton(const QPixmap &pixmap, const QString &infoText)
158 {
159 if (auto *const le = lineEditForWidget()) {
160 disableInfoButton();
161 auto *const action = le->addAction(QIcon(pixmap), QLineEdit::TrailingPosition);
162 action->setToolTip(infoText);
163 QObject::connect(action, &QAction::triggered, std::bind(&ButtonOverlay::showInfo, this));
164 m_infoButtonOrAction = action;
165 return;
166 }
167 auto *infoButton = static_cast<IconButton *>(m_infoButtonOrAction);
168 if (!infoButton) {
169 m_infoButtonOrAction = infoButton = new IconButton;
170 infoButton->setGeometry(QRect(QPoint(), IconButton::defaultPixmapSize));
171 if (m_clearButton) {
172 m_buttonLayout->insertWidget(m_buttonLayout->count() - 2, infoButton);
173 } else {
174 m_buttonLayout->addWidget(infoButton);
175 }
176 }
177 infoButton->setPixmap(pixmap);
178 infoButton->setToolTip(infoText);
179 }
180
181 /*!
182 * \brief Hides an info button if one is shown.
183 * \sa ButtonOverlay::enableInfoButton()
184 */
185 void ButtonOverlay::disableInfoButton()
186 {
187 if (auto *const le = lineEditForWidget()) {
188 if (auto *const infoAction = static_cast<QAction *>(m_infoButtonOrAction)) {
189 le->removeAction(infoAction);
190 m_infoButtonOrAction = nullptr;
191 }
192 return;
193 }
194 if (auto *infoButton = static_cast<IconButton *>(m_infoButtonOrAction)) {
195 m_buttonLayout->removeWidget(infoButton);
196 delete infoButton;
197 m_infoButtonOrAction = nullptr;
198 }
199 }
200
201 /*!
202 * \brief Adds a custom \a button.
203 *
204 * The button overlay takes ownership over the specified \a button.
205 *
206 * \remarks This function enforces the "custom approach" mentioned in the class documentation
207 * and should therefore be avoided.
208 */
209 void ButtonOverlay::addCustomButton(QWidget *button)
210 {
211 fallbackToUsingCustomLayout();
212 m_buttonLayout->addWidget(button);
213 }
214
215 /*!
216 * \brief Inserts a custom \a button at the specified \a index.
217 *
218 * The button overlay takes ownership over the specified \a button.
219 *
220 * \remarks This function enforces the "custom approach" mentioned in the class documentation
221 * and should therefore be avoided.
222 */
223 void ButtonOverlay::insertCustomButton(int index, QWidget *button)
224 {
225 fallbackToUsingCustomLayout();
226 m_buttonLayout->insertWidget(index, button);
227 }
228
229 /*!
230 * \brief Removes the specified custom \a button; does nothing if \a button has not been added.
231 *
232 * The ownership of widget remains the same as when it was added.
233 */
234 void ButtonOverlay::removeCustomButton(QWidget *button)
235 {
236 if (isUsingCustomLayout()) {
237 m_buttonLayout->removeWidget(button);
238 }
239 }
240
241 /*!
242 * \brief Adds a custom \a action.
243 */
244 void ButtonOverlay::addCustomAction(QAction *action)
245 {
246 if (auto *const le = lineEditForWidget()) {
247 le->addAction(action, QLineEdit::TrailingPosition);
248 } else {
249 addCustomButton(IconButton::fromAction(action, reinterpret_cast<std::uintptr_t>(this)));
250 }
251 }
252
253 /*!
254 * \brief Inserts a custom \a action at the specified \a index.
255 */
256 void ButtonOverlay::insertCustomAction(int index, QAction *action)
257 {
258 if (auto *const le = lineEditForWidget()) {
259 const auto actions = le->actions();
260 le->insertAction(index < actions.size() ? actions[index] : nullptr, action);
261 } else {
262 insertCustomButton(index, IconButton::fromAction(action, reinterpret_cast<std::uintptr_t>(this)));
263 }
264 }
265
266 /*!
267 * \brief Removes the specified custom \a action; does nothing if \a action has not been added.
268 */
269 void ButtonOverlay::removeCustomAction(QAction *action)
270 {
271 if (auto *const le = lineEditForWidget()) {
272 le->removeAction(action);
273 } else {
274 removeCustomButton(IconButton::fromAction(action, reinterpret_cast<std::uintptr_t>(this)));
275 }
276 }
277
278 /*!
279 * \brief Updates the visibility of the clear button.
280 *
281 * This function is meant to be called when subclassing.
282 */
283 void ButtonOverlay::updateClearButtonVisibility(bool visible)
284 {
285 if (m_clearButton) {
286 m_clearButton->setVisible(visible);
287 }
288 }
289
290 /*!
291 * \brief Clears the related widget.
292 *
293 * This function is meant to be implemented when subclassing to support the clear button.
294 */
295 void ButtonOverlay::handleClearButtonClicked()
296 {
297 }
298
299 /*!
300 * \brief Applies additional handling when the button layout has been created.
301 *
302 * This function is meant to be implemented when subclassing when additional handling is
303 * required.
304 */
305 void ButtonOverlay::handleCustomLayoutCreated()
306 {
307 }
308
309 /*!
310 * \brief Switches to the "custom approach".
311 * \remarks This function is internally used when any legacy function is called
312 * or when the QLineEdit for the specified widget can not be determined.
313 */
314 void ButtonOverlay::fallbackToUsingCustomLayout()
315 {
316 // skip if custom layout is already used
317 if (isUsingCustomLayout()) {
318 return;
319 }
320
321 // disable QLineEdit's clear button and actions; save configuration
322 auto clearButtonEnabled = false;
323 auto *iconAction = static_cast<QAction *>(m_infoButtonOrAction);
324 QPixmap infoPixmap;
325 QString infoText;
326 QList<QAction *> actions;
327 if (auto *const le = lineEditForWidget()) {
328 if ((clearButtonEnabled = le->isClearButtonEnabled())) {
329 setClearButtonEnabled(false);
330 }
331 if ((iconAction = static_cast<QAction *>(m_infoButtonOrAction))) {
332 const auto icon = iconAction->icon();
333 const auto sizes = icon.availableSizes();
334 infoPixmap = icon.pixmap(sizes.empty() ? IconButton::defaultPixmapSize : sizes.front());
335 infoText = iconAction->toolTip();
336 disableInfoButton();
337 }
338 actions = le->actions();
339 for (auto *const action : actions) {
340 le->removeAction(action);
341 }
342 }
343
344 // initialize custom layout
345 m_buttonLayout = new QHBoxLayout(m_buttonWidget);
346 m_buttonWidget = new QWidget(m_widget);
347 m_buttonLayout->setAlignment(Qt::AlignCenter | Qt::AlignRight);
348 m_widget->setLayout(m_buttonLayout);
349 handleCustomLayoutCreated();
350
351 // restore old configuration
352 if (clearButtonEnabled) {
353 setClearButtonEnabled(true);
354 }
355 if (iconAction) {
356 enableInfoButton(infoPixmap, infoText);
357 }
358 for (auto *const action : actions) {
359 addCustomAction(action);
360 }
361 }
362
363 /*!
364 * \brief Returns the QLineEdit used to implement the button overlay.
365 * \remarks This is always nullptr in case the "custom approach" is used.
366 */
367 QLineEdit *ButtonOverlay::lineEditForWidget() const
368 {
369 return isUsingCustomLayout() ? nullptr : static_cast<QLineEdit *>(m_buttonWidget);
370 }
371
372 /*!
373 * \brief Returns whether the related widget is cleared.
374 *
375 * This method is meant to be implemented when subclassing.
376 */
377 bool ButtonOverlay::isCleared() const
378 {
379 return false;
380 }
381
382 /*!
383 * \brief Shows the info text using a tool tip.
384 *
385 * This method is called when the info button is clicked.
386 *
387 * \remarks
388 * This function avoids using QCursor::pos() because it is problematic to use under Wayland. For the action case it seems not
389 * possible to avoid it because the position of QLineEditIconButton used by QLineEdit is not exposed.
390 */
391 void ButtonOverlay::showInfo()
392 {
393 if (lineEditForWidget()) {
394 if (auto *const infoAction = static_cast<QAction *>(m_infoButtonOrAction)) {
395 if (const auto pos = QCursor::pos(); !pos.isNull()) {
396 QToolTip::showText(pos, infoAction->toolTip(), m_widget);
397 }
398 }
399 return;
400 }
401 if (auto *const infoButton = static_cast<IconButton *>(m_infoButtonOrAction)) {
402 QToolTip::showText(infoButton->mapToGlobal(infoButton->rect().center()), infoButton->toolTip(), infoButton);
403 }
404 }
405
406 /*!
407 * \brief Sets the contents margins of the button layout so the overlay buttons will only be shown over the \a editFieldRect and
408 * not interfere with e.g. spin box buttons.
409 * \remarks This function enforces the "custom approach" mentioned in the class documentation
410 * and should therefore be avoided. Of course it makes sense to call it within handleCustomLayoutCreated().
411 */
412 void ButtonOverlay::setContentsMarginsFromEditFieldRectAndFrameWidth(const QRect &editFieldRect, int frameWidth, int padding)
413 {
414 const auto margins = m_widget->contentsMargins();
415 const auto buttonWidth = m_widget->width() - editFieldRect.width();
416 buttonLayout()->setContentsMargins(margins.left() + frameWidth + padding, margins.top() + frameWidth,
417 margins.right() + frameWidth + padding + buttonWidth, margins.bottom() + frameWidth);
418 }
419
420 } // namespace QtUtilities