]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/widgetmenu.h
Add missing KF6::ColorScheme link
[dolphin.git] / src / search / widgetmenu.h
1 /*
2 SPDX-FileCopyrightText: 2025 Felix Ernst <felixernst@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7 #ifndef WIDGETMENU_H
8 #define WIDGETMENU_H
9
10 #include <QMenu>
11
12 class QMouseEvent;
13 class QShowEvent;
14
15 namespace Search
16 {
17
18 /**
19 * @brief A QMenu that contains nothing but a lazily constructed widget.
20 *
21 * Usually QMenus contain a list of actions. WidgetMenu allows showing any QWidget instead. This is useful to show popups, random text, or full user interfaces
22 * when a button is pressed or a menu action in a QMenu is hovered.
23 *
24 * This class also encapsulates lazy construction of the widget within. It will only be created when this menu is actually being opened.
25 */
26 class WidgetMenu : public QMenu
27 {
28 public:
29 explicit WidgetMenu(QWidget *parent = nullptr);
30
31 protected:
32 /**
33 * Overrides the weird QMenu Tab key handling with the usual QWidget one.
34 */
35 bool focusNextPrevChild(bool next) override;
36
37 /**
38 * Overrides the QMenu behaviour of closing itself when clicked with the non-closing QWidget one.
39 */
40 void mouseReleaseEvent(QMouseEvent *event) override;
41
42 /**
43 * This unfortuantely needs to be explicitly called to resize the WidgetMenu because the size of a QMenu will not automatically change to fit the QWidgets
44 * within.
45 */
46 void resizeToFitContents();
47
48 /**
49 * Move focus to the widget when this WidgetMenu is shown.
50 */
51 void showEvent(QShowEvent *event) override;
52
53 private:
54 /**
55 * @return the widget which is contained in this WidgetMenu. This method is at most called once per WidgetMenu object when the WidgetMenu is about to be
56 * shown for the first time. The ownership of the widget will be transfered to an internal QWidgetAction.
57 */
58 virtual QWidget *init() = 0;
59 };
60
61 }
62
63 #endif // WIDGETMENU_H