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 | |
25 | class |
26 | { |
27 | public: |
28 | (KToolBarPopupAction *q) |
29 | : q(q) |
30 | , m_popupMode(KToolBarPopupAction::MenuButtonPopup) |
31 | , m_popupMenu(new QMenu()) |
32 | { |
33 | } |
34 | |
35 | void (QToolButton *button, KToolBarPopupAction::PopupMode mode) |
36 | { |
37 | if (mode == KToolBarPopupAction::NoPopup) { |
38 | button->setMenu(nullptr); |
39 | button->setPopupMode(QToolButton::InstantPopup); |
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 | |
50 | KToolBarPopupAction *; |
51 | KToolBarPopupAction::PopupMode ; |
52 | std::unique_ptr<QMenu> ; |
53 | }; |
54 | |
55 | KToolBarPopupAction::(const QIcon &icon, const QString &text, QObject *parent) |
56 | : QWidgetAction(parent) |
57 | , d(new KToolBarPopupActionPrivate(this)) |
58 | { |
59 | setIcon(icon); |
60 | setText(text); |
61 | } |
62 | |
63 | KToolBarPopupAction::() = default; |
64 | |
65 | QWidget *KToolBarPopupAction::(QWidget *_parent) |
66 | { |
67 | QToolBar *parent = qobject_cast<QToolBar *>(object: _parent); |
68 | if (!parent) { |
69 | return QWidgetAction::createWidget(parent: _parent); |
70 | } |
71 | QToolButton *button = new QToolButton(parent); |
72 | button->setAutoRaise(true); |
73 | button->setFocusPolicy(Qt::NoFocus); |
74 | button->setIconSize(parent->iconSize()); |
75 | button->setToolButtonStyle(parent->toolButtonStyle()); |
76 | button->setDefaultAction(this); |
77 | d->applyPopupMode(button, mode: d->m_popupMode); |
78 | |
79 | connect(sender: parent, signal: &QToolBar::iconSizeChanged, context: button, slot: &QAbstractButton::setIconSize); |
80 | connect(sender: parent, signal: &QToolBar::toolButtonStyleChanged, context: button, slot: &QToolButton::setToolButtonStyle); |
81 | connect(sender: button, signal: &QToolButton::triggered, context: parent, slot: &QToolBar::actionTriggered); |
82 | |
83 | return button; |
84 | } |
85 | |
86 | KToolBarPopupAction::PopupMode KToolBarPopupAction::() const |
87 | { |
88 | return d->m_popupMode; |
89 | } |
90 | |
91 | void KToolBarPopupAction::(KToolBarPopupAction::PopupMode ) |
92 | { |
93 | d->m_popupMode = popupMode; |
94 | |
95 | const auto widgets = createdWidgets(); |
96 | for (QWidget *widget : widgets) { |
97 | if (auto *button = qobject_cast<QToolButton *>(object: widget)) { |
98 | d->applyPopupMode(button, mode: popupMode); |
99 | } |
100 | } |
101 | } |
102 | |
103 | QMenu *KToolBarPopupAction::() const |
104 | { |
105 | return d->m_popupMenu.get(); |
106 | } |
107 | |
108 | #include "moc_ktoolbarpopupaction.cpp" |
109 | |