Libplasma

qmenuitem.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Viranch Mehta <viranch.mehta@gmail.com>
3 SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "qmenuitem.h"
9
10QMenuItem::QMenuItem(QObject *parent)
11 : QObject(parent)
12 , m_action(nullptr)
13 , m_section(false)
14{
15 setAction(new QAction(this));
16}
17
18QAction *QMenuItem::action() const
19{
20 return m_action;
21}
22
23void QMenuItem::setAction(QAction *a)
24{
25 if (m_action != a) {
26 if (m_action) {
27 disconnect(m_action, nullptr, this, nullptr);
28
29 if (m_action->parent() == this) {
30 delete m_action;
31 m_action = nullptr;
32 }
33 }
34
35 if (a) {
36 m_action = a;
37 } else {
38 // don't end up with no action, create an invisible one instead
39 m_action = new QAction(this);
40 m_action->setVisible(false);
41 }
42
43 setVisible(m_action->isVisible());
44 setEnabled(m_action->isEnabled());
45
46 connect(m_action, &QAction::changed, this, &QMenuItem::textChanged);
47 connect(m_action, &QAction::checkableChanged, this, &QMenuItem::checkableChanged);
48 connect(m_action, &QAction::enabledChanged, this, &QMenuItem::enabledChanged);
49 connect(m_action, &QAction::visibleChanged, this, &QMenuItem::visibleChanged);
50 connect(m_action, &QAction::toggled, this, &QMenuItem::toggled);
51 connect(m_action, &QAction::triggered, this, &QMenuItem::clicked);
52 // HACK QMenuItem doesn't delete other people's QAction (see m_action->parent() check above)
53 // but it does not take kindly to the QAction being deleted under it
54 // as a workaround for crashing when this happens, replace it by a dummy action again
55 // TODO this entire ownership handling in QMenu(Item) needs to be refactored...
56 connect(m_action, &QObject::destroyed, this, [this] {
57 if (m_action->parent() != this) {
58 m_action = new QAction(this);
59 m_action->setVisible(false);
60 Q_EMIT actionChanged();
61 }
62 });
63
65
66 Q_EMIT actionChanged();
67 }
68}
69
70QVariant QMenuItem::icon() const
71{
72 return m_icon;
73}
74
75void QMenuItem::setIcon(const QVariant &i)
76{
77 m_icon = i;
78 if (i.canConvert<QIcon>()) {
79 m_action->setIcon(i.value<QIcon>());
80 } else if (i.canConvert<QString>()) {
81 m_action->setIcon(QIcon::fromTheme(i.toString()));
82 }
83 Q_EMIT iconChanged();
84}
85
86bool QMenuItem::separator() const
87{
88 return m_action->isSeparator();
89}
90
91void QMenuItem::setSeparator(bool s)
92{
93 m_action->setSeparator(s);
94}
95
96bool QMenuItem::section() const
97{
98 return m_section;
99}
100
101void QMenuItem::setSection(bool s)
102{
103 m_section = s;
104}
105
106QString QMenuItem::text() const
107{
108 return m_action->text();
109}
110
111void QMenuItem::setText(const QString &t)
112{
113 if (m_action->text() != t) {
114 m_action->setText(t);
115 // signal comes from m_action
116 }
117}
118
119bool QMenuItem::checkable() const
120{
121 return m_action->isCheckable();
122}
123
124void QMenuItem::setCheckable(bool checkable)
125{
126 m_action->setCheckable(checkable);
127}
128
129bool QMenuItem::checked() const
130{
131 return m_action->isChecked();
132}
133
134void QMenuItem::setChecked(bool checked)
135{
136 m_action->setChecked(checked);
137}
138
139bool QMenuItem::isEnabled() const
140{
141 return m_action->isEnabled();
142}
143
144void QMenuItem::setEnabled(bool enabled)
145{
146 m_action->setEnabled(enabled);
147}
148
149bool QMenuItem::isVisible() const
150{
151 return m_action->isVisible();
152}
153
154void QMenuItem::setVisible(bool visible)
155{
156 m_action->setVisible(visible);
157}
158
159#include "moc_qmenuitem.cpp"
void changed()
void checkableChanged(bool checkable)
bool isChecked() const const
bool isEnabled() const const
void setIcon(const QIcon &icon)
bool isSeparator() const const
void setSeparator(bool b)
void toggled(bool checked)
void triggered(bool checked)
void setVisible(bool)
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void destroyed(QObject *obj)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
bool canConvert() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.