]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/widgetmenu.cpp
2 SPDX-FileCopyrightText: 2025 Felix Ernst <felixernst@kde.org>
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 #include "widgetmenu.h"
9 #include <QApplication>
11 #include <QWidgetAction>
13 using namespace Search
;
15 Search::WidgetMenu::WidgetMenu(QWidget
*parent
)
23 auto widgetAction
= new QWidgetAction
{this};
26 widgetAction
->setDefaultWidget(widget
); // Transfers ownership to the widgetAction.
27 addAction(widgetAction
);
29 Qt::SingleShotConnection
);
32 bool WidgetMenu::focusNextPrevChild(bool next
)
34 return QWidget::focusNextPrevChild(next
);
37 void WidgetMenu::mouseReleaseEvent(QMouseEvent
*event
)
39 return QWidget::mouseReleaseEvent(event
);
42 void WidgetMenu::resizeToFitContents()
44 auto *widgetAction
= static_cast<QWidgetAction
*>(actions().constFirst());
45 auto focusedChildWidget
= QApplication::focusWidget();
46 if (!widgetAction
->defaultWidget()->isAncestorOf(focusedChildWidget
)) {
47 focusedChildWidget
= nullptr;
50 // Removing and readding the widget triggers the resize.
51 removeAction(widgetAction
);
52 addAction(widgetAction
);
54 // The previous removing and readding removed the focus from any child widgets. We return the focus to where it was.
55 if (focusedChildWidget
) {
56 focusedChildWidget
->setFocus();
60 void WidgetMenu::showEvent(QShowEvent
*event
)
62 if (!event
->spontaneous()) {
63 auto widgetAction
= static_cast<QWidgetAction
*>(actions().constFirst());
64 widgetAction
->defaultWidget()->setFocus();
66 QMenu::showEvent(event
);