KWidgetsAddons

ktoolbarpopupaction.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
4 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
5 SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
6 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
7 SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
8 SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
9 SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
10 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
11 SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
12 SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
13 SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <kde@broulik.de>
14
15 SPDX-License-Identifier: LGPL-2.0-only
16*/
17
18#include "ktoolbarpopupaction.h"
19
20#include <QMenu>
21#include <QToolBar>
22
23#include <memory>
24
25class KToolBarPopupActionPrivate
26{
27public:
28 KToolBarPopupActionPrivate(KToolBarPopupAction *q)
29 : q(q)
30 , m_popupMode(KToolBarPopupAction::MenuButtonPopup)
31 , m_popupMenu(new QMenu())
32 {
33 }
34
35 void applyPopupMode(QToolButton *button, KToolBarPopupAction::PopupMode mode)
36 {
37 if (mode == KToolBarPopupAction::NoPopup) {
38 button->setMenu(nullptr);
40 } else {
41 // In case the menu is explicitly overridden, use it.
42 // QAction::menu() is automatically applied thanks to QToolButton::setDefaultAction().
43 if (!q->menu()) {
44 button->setMenu(m_popupMenu.get());
45 }
46 button->setPopupMode(static_cast<QToolButton::ToolButtonPopupMode>(mode));
47 }
48 }
49
52 std::unique_ptr<QMenu> m_popupMenu;
53};
54
56 : QWidgetAction(parent)
57 , d(new KToolBarPopupActionPrivate(this))
58{
61}
62
64
66{
68 if (!parent) {
70 }
71 QToolButton *button = new QToolButton(parent);
72 button->setAutoRaise(true);
74 button->setIconSize(parent->iconSize());
75 button->setToolButtonStyle(parent->toolButtonStyle());
76 button->setDefaultAction(this);
77 d->applyPopupMode(button, d->m_popupMode);
78
82
83 return button;
84}
85
86KToolBarPopupAction::PopupMode KToolBarPopupAction::popupMode() const
87{
88 return d->m_popupMode;
89}
90
92{
93 d->m_popupMode = popupMode;
94
95 const auto widgets = createdWidgets();
96 for (QWidget *widget : widgets) {
97 if (auto *button = qobject_cast<QToolButton *>(widget)) {
98 d->applyPopupMode(button, popupMode);
99 }
100 }
101}
102
104{
105 return d->m_popupMenu.get();
106}
107
108#include "moc_ktoolbarpopupaction.cpp"
This action is a normal action everywhere, except in a toolbar where it also has a popupmenu (optiona...
QWidget * createWidget(QWidget *parent) override
Reimplemented from QWidgetAction.
QMenu * popupMenu() const
The popup menu that is shown when clicking (some time) on the toolbar button.
PopupMode
The menu popup mode.
@ NoPopup
Behave as if the button had no menu.
void setPopupMode(PopupMode popupMode)
Determines the popup mode of the toolbar button.
KToolBarPopupAction(const QIcon &icon, const QString &text, QObject *parent)
Create a KToolBarPopupAction, with a text, an icon, a parent and a name.
~KToolBarPopupAction() override
Destroys the toolbar popup action.
void setIconSize(const QSize &size)
void setIcon(const QIcon &icon)
QMenu * menu() const const
void setText(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
void actionTriggered(QAction *action)
void iconSizeChanged(const QSize &iconSize)
void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)
void setAutoRaise(bool enable)
void setPopupMode(ToolButtonPopupMode mode)
void setDefaultAction(QAction *action)
void setMenu(QMenu *menu)
void setToolButtonStyle(Qt::ToolButtonStyle style)
void triggered(QAction *action)
void setFocusPolicy(Qt::FocusPolicy policy)
virtual QWidget * createWidget(QWidget *parent)
QList< QWidget * > createdWidgets() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 12:06:50 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.