Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/kernel/qwidgetaction.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qwidgetaction.h" | - | ||||||||||||
35 | #include "qdebug.h" | - | ||||||||||||
36 | - | |||||||||||||
37 | #ifndef QT_NO_ACTION | - | ||||||||||||
38 | #include "qwidgetaction_p.h" | - | ||||||||||||
39 | - | |||||||||||||
40 | QT_BEGIN_NAMESPACE | - | ||||||||||||
41 | - | |||||||||||||
42 | /*! | - | ||||||||||||
43 | \class QWidgetAction | - | ||||||||||||
44 | \since 4.2 | - | ||||||||||||
45 | \brief The QWidgetAction class extends QAction by an interface | - | ||||||||||||
46 | for inserting custom widgets into action based containers, such | - | ||||||||||||
47 | as toolbars. | - | ||||||||||||
48 | - | |||||||||||||
49 | \ingroup mainwindow-classes | - | ||||||||||||
50 | \inmodule QtWidgets | - | ||||||||||||
51 | - | |||||||||||||
52 | Most actions in an application are represented as items in menus or | - | ||||||||||||
53 | buttons in toolbars. However sometimes more complex widgets are | - | ||||||||||||
54 | necessary. For example a zoom action in a word processor may be | - | ||||||||||||
55 | realized using a QComboBox in a QToolBar, presenting a range | - | ||||||||||||
56 | of different zoom levels. QToolBar provides QToolBar::insertWidget() | - | ||||||||||||
57 | as convenience function for inserting a single widget. | - | ||||||||||||
58 | However if you want to implement an action that uses custom | - | ||||||||||||
59 | widgets for visualization in multiple containers then you have to | - | ||||||||||||
60 | subclass QWidgetAction. | - | ||||||||||||
61 | - | |||||||||||||
62 | If a QWidgetAction is added for example to a QToolBar then | - | ||||||||||||
63 | QWidgetAction::createWidget() is called. Reimplementations of that | - | ||||||||||||
64 | function should create a new custom widget with the specified parent. | - | ||||||||||||
65 | - | |||||||||||||
66 | If the action is removed from a container widget then | - | ||||||||||||
67 | QWidgetAction::deleteWidget() is called with the previously created custom | - | ||||||||||||
68 | widget as argument. The default implementation hides the widget and deletes | - | ||||||||||||
69 | it using QObject::deleteLater(). | - | ||||||||||||
70 | - | |||||||||||||
71 | If you have only one single custom widget then you can set it as default | - | ||||||||||||
72 | widget using setDefaultWidget(). That widget will then be used if the | - | ||||||||||||
73 | action is added to a QToolBar, or in general to an action container that | - | ||||||||||||
74 | supports QWidgetAction. If a QWidgetAction with only a default widget is | - | ||||||||||||
75 | added to two toolbars at the same time then the default widget is shown | - | ||||||||||||
76 | only in the first toolbar the action was added to. QWidgetAction takes | - | ||||||||||||
77 | over ownership of the default widget. | - | ||||||||||||
78 | - | |||||||||||||
79 | Note that it is up to the widget to activate the action, for example by | - | ||||||||||||
80 | reimplementing mouse event handlers and calling QAction::trigger(). | - | ||||||||||||
81 | - | |||||||||||||
82 | \b {\macos}: If you add a widget to a menu in the application's menu | - | ||||||||||||
83 | bar on \macos, the widget will be added and it will function but with some | - | ||||||||||||
84 | limitations: | - | ||||||||||||
85 | \list 1 | - | ||||||||||||
86 | \li The widget is reparented away from the QMenu to the native menu | - | ||||||||||||
87 | view. If you show the menu in some other place (e.g. as a popup menu), | - | ||||||||||||
88 | the widget will not be there. | - | ||||||||||||
89 | \li Focus/Keyboard handling of the widget is not possible. | - | ||||||||||||
90 | \li Due to Apple's design, mouse tracking on the widget currently does | - | ||||||||||||
91 | not work. | - | ||||||||||||
92 | \li Connecting the triggered() signal to a slot that opens a modal | - | ||||||||||||
93 | dialog will cause a crash in \macos 10.4 (known bug acknowledged | - | ||||||||||||
94 | by Apple), a workaround is to use a QueuedConnection instead of a | - | ||||||||||||
95 | DirectConnection. | - | ||||||||||||
96 | \endlist | - | ||||||||||||
97 | - | |||||||||||||
98 | \sa QAction, QActionGroup, QWidget | - | ||||||||||||
99 | */ | - | ||||||||||||
100 | - | |||||||||||||
101 | /*! | - | ||||||||||||
102 | Constructs an action with \a parent. | - | ||||||||||||
103 | */ | - | ||||||||||||
104 | QWidgetAction::QWidgetAction(QObject *parent) | - | ||||||||||||
105 | : QAction(*(new QWidgetActionPrivate), parent) | - | ||||||||||||
106 | { | - | ||||||||||||
107 | } never executed: end of block | 0 | ||||||||||||
108 | - | |||||||||||||
109 | /*! | - | ||||||||||||
110 | Destroys the object and frees allocated resources. | - | ||||||||||||
111 | */ | - | ||||||||||||
112 | QWidgetAction::~QWidgetAction() | - | ||||||||||||
113 | { | - | ||||||||||||
114 | Q_D(QWidgetAction); | - | ||||||||||||
115 | for (int i = 0; i < d->createdWidgets.count(); ++i)
| 0 | ||||||||||||
116 | disconnect(d->createdWidgets.at(i), SIGNAL(destroyed(QObject*)), never executed: disconnect(d->createdWidgets.at(i), qFlagLocation("2""destroyed(QObject*)" "\0" __FILE__ ":" "116"), this, qFlagLocation("1""_q_widgetDestroyed(QObject*)" "\0" __FILE__ ":" "117")); | 0 | ||||||||||||
117 | this, SLOT(_q_widgetDestroyed(QObject*))); never executed: disconnect(d->createdWidgets.at(i), qFlagLocation("2""destroyed(QObject*)" "\0" __FILE__ ":" "116"), this, qFlagLocation("1""_q_widgetDestroyed(QObject*)" "\0" __FILE__ ":" "117")); | 0 | ||||||||||||
118 | QList<QWidget *> widgetsToDelete = d->createdWidgets; | - | ||||||||||||
119 | d->createdWidgets.clear(); | - | ||||||||||||
120 | qDeleteAll(widgetsToDelete); | - | ||||||||||||
121 | delete d->defaultWidget; | - | ||||||||||||
122 | } never executed: end of block | 0 | ||||||||||||
123 | - | |||||||||||||
124 | /*! | - | ||||||||||||
125 | Sets \a widget to be the default widget. The ownership is | - | ||||||||||||
126 | transferred to QWidgetAction. Unless createWidget() is | - | ||||||||||||
127 | reimplemented by a subclass to return a new widget the default | - | ||||||||||||
128 | widget is used when a container widget requests a widget through | - | ||||||||||||
129 | requestWidget(). | - | ||||||||||||
130 | */ | - | ||||||||||||
131 | void QWidgetAction::setDefaultWidget(QWidget *widget) | - | ||||||||||||
132 | { | - | ||||||||||||
133 | Q_D(QWidgetAction); | - | ||||||||||||
134 | if (widget == d->defaultWidget || d->defaultWidgetInUse)
| 0 | ||||||||||||
135 | return; never executed: return; | 0 | ||||||||||||
136 | delete d->defaultWidget; | - | ||||||||||||
137 | d->defaultWidget = widget; | - | ||||||||||||
138 | if (!widget)
| 0 | ||||||||||||
139 | return; never executed: return; | 0 | ||||||||||||
140 | - | |||||||||||||
141 | setVisible(!(widget->isHidden() && widget->testAttribute(Qt::WA_WState_ExplicitShowHide))); | - | ||||||||||||
142 | d->defaultWidget->hide(); | - | ||||||||||||
143 | d->defaultWidget->setParent(0); | - | ||||||||||||
144 | d->defaultWidgetInUse = false; | - | ||||||||||||
145 | if (!isEnabled())
| 0 | ||||||||||||
146 | d->defaultWidget->setEnabled(false); never executed: d->defaultWidget->setEnabled(false); | 0 | ||||||||||||
147 | } never executed: end of block | 0 | ||||||||||||
148 | - | |||||||||||||
149 | /*! | - | ||||||||||||
150 | Returns the default widget. | - | ||||||||||||
151 | */ | - | ||||||||||||
152 | QWidget *QWidgetAction::defaultWidget() const | - | ||||||||||||
153 | { | - | ||||||||||||
154 | Q_D(const QWidgetAction); | - | ||||||||||||
155 | return d->defaultWidget; never executed: return d->defaultWidget; | 0 | ||||||||||||
156 | } | - | ||||||||||||
157 | - | |||||||||||||
158 | /*! | - | ||||||||||||
159 | Returns a widget that represents the action, with the given \a | - | ||||||||||||
160 | parent. | - | ||||||||||||
161 | - | |||||||||||||
162 | Container widgets that support actions can call this function to | - | ||||||||||||
163 | request a widget as visual representation of the action. | - | ||||||||||||
164 | - | |||||||||||||
165 | \sa releaseWidget(), createWidget(), defaultWidget() | - | ||||||||||||
166 | */ | - | ||||||||||||
167 | QWidget *QWidgetAction::requestWidget(QWidget *parent) | - | ||||||||||||
168 | { | - | ||||||||||||
169 | Q_D(QWidgetAction); | - | ||||||||||||
170 | - | |||||||||||||
171 | QWidget *w = createWidget(parent); | - | ||||||||||||
172 | if (!w) {
| 0 | ||||||||||||
173 | if (d->defaultWidgetInUse || !d->defaultWidget)
| 0 | ||||||||||||
174 | return 0; never executed: return 0; | 0 | ||||||||||||
175 | d->defaultWidget->setParent(parent); | - | ||||||||||||
176 | d->defaultWidgetInUse = true; | - | ||||||||||||
177 | return d->defaultWidget; never executed: return d->defaultWidget; | 0 | ||||||||||||
178 | } | - | ||||||||||||
179 | - | |||||||||||||
180 | connect(w, SIGNAL(destroyed(QObject*)), | - | ||||||||||||
181 | this, SLOT(_q_widgetDestroyed(QObject*))); | - | ||||||||||||
182 | d->createdWidgets.append(w); | - | ||||||||||||
183 | return w; never executed: return w; | 0 | ||||||||||||
184 | } | - | ||||||||||||
185 | - | |||||||||||||
186 | /*! | - | ||||||||||||
187 | Releases the specified \a widget. | - | ||||||||||||
188 | - | |||||||||||||
189 | Container widgets that support actions call this function when a widget | - | ||||||||||||
190 | action is removed. | - | ||||||||||||
191 | - | |||||||||||||
192 | \sa requestWidget(), deleteWidget(), defaultWidget() | - | ||||||||||||
193 | */ | - | ||||||||||||
194 | void QWidgetAction::releaseWidget(QWidget *widget) | - | ||||||||||||
195 | { | - | ||||||||||||
196 | Q_D(QWidgetAction); | - | ||||||||||||
197 | - | |||||||||||||
198 | if (widget == d->defaultWidget) {
| 0 | ||||||||||||
199 | d->defaultWidget->hide(); | - | ||||||||||||
200 | d->defaultWidget->setParent(0); | - | ||||||||||||
201 | d->defaultWidgetInUse = false; | - | ||||||||||||
202 | return; never executed: return; | 0 | ||||||||||||
203 | } | - | ||||||||||||
204 | - | |||||||||||||
205 | if (!d->createdWidgets.contains(widget))
| 0 | ||||||||||||
206 | return; never executed: return; | 0 | ||||||||||||
207 | - | |||||||||||||
208 | disconnect(widget, SIGNAL(destroyed(QObject*)), | - | ||||||||||||
209 | this, SLOT(_q_widgetDestroyed(QObject*))); | - | ||||||||||||
210 | d->createdWidgets.removeAll(widget); | - | ||||||||||||
211 | deleteWidget(widget); | - | ||||||||||||
212 | } never executed: end of block | 0 | ||||||||||||
213 | - | |||||||||||||
214 | /*! | - | ||||||||||||
215 | \reimp | - | ||||||||||||
216 | */ | - | ||||||||||||
217 | bool QWidgetAction::event(QEvent *event) | - | ||||||||||||
218 | { | - | ||||||||||||
219 | Q_D(QWidgetAction); | - | ||||||||||||
220 | if (event->type() == QEvent::ActionChanged) {
| 0 | ||||||||||||
221 | if (d->defaultWidget)
| 0 | ||||||||||||
222 | d->defaultWidget->setEnabled(isEnabled()); never executed: d->defaultWidget->setEnabled(isEnabled()); | 0 | ||||||||||||
223 | for (int i = 0; i < d->createdWidgets.count(); ++i)
| 0 | ||||||||||||
224 | d->createdWidgets.at(i)->setEnabled(isEnabled()); never executed: d->createdWidgets.at(i)->setEnabled(isEnabled()); | 0 | ||||||||||||
225 | } never executed: end of block | 0 | ||||||||||||
226 | return QAction::event(event); never executed: return QAction::event(event); | 0 | ||||||||||||
227 | } | - | ||||||||||||
228 | - | |||||||||||||
229 | /*! | - | ||||||||||||
230 | \reimp | - | ||||||||||||
231 | */ | - | ||||||||||||
232 | bool QWidgetAction::eventFilter(QObject *obj, QEvent *event) | - | ||||||||||||
233 | { | - | ||||||||||||
234 | return QAction::eventFilter(obj,event); never executed: return QAction::eventFilter(obj,event); | 0 | ||||||||||||
235 | } | - | ||||||||||||
236 | - | |||||||||||||
237 | /*! | - | ||||||||||||
238 | This function is called whenever the action is added to a container widget | - | ||||||||||||
239 | that supports custom widgets. If you don't want a custom widget to be | - | ||||||||||||
240 | used as representation of the action in the specified \a parent widget then | - | ||||||||||||
241 | 0 should be returned. | - | ||||||||||||
242 | - | |||||||||||||
243 | \sa deleteWidget() | - | ||||||||||||
244 | */ | - | ||||||||||||
245 | QWidget *QWidgetAction::createWidget(QWidget *parent) | - | ||||||||||||
246 | { | - | ||||||||||||
247 | Q_UNUSED(parent) | - | ||||||||||||
248 | return 0; never executed: return 0; | 0 | ||||||||||||
249 | } | - | ||||||||||||
250 | - | |||||||||||||
251 | /*! | - | ||||||||||||
252 | This function is called whenever the action is removed from a | - | ||||||||||||
253 | container widget that displays the action using a custom \a | - | ||||||||||||
254 | widget previously created using createWidget(). The default | - | ||||||||||||
255 | implementation hides the \a widget and schedules it for deletion | - | ||||||||||||
256 | using QObject::deleteLater(). | - | ||||||||||||
257 | - | |||||||||||||
258 | \sa createWidget() | - | ||||||||||||
259 | */ | - | ||||||||||||
260 | void QWidgetAction::deleteWidget(QWidget *widget) | - | ||||||||||||
261 | { | - | ||||||||||||
262 | widget->hide(); | - | ||||||||||||
263 | widget->deleteLater(); | - | ||||||||||||
264 | } never executed: end of block | 0 | ||||||||||||
265 | - | |||||||||||||
266 | /*! | - | ||||||||||||
267 | Returns the list of widgets that have been using createWidget() and | - | ||||||||||||
268 | are currently in use by widgets the action has been added to. | - | ||||||||||||
269 | */ | - | ||||||||||||
270 | QList<QWidget *> QWidgetAction::createdWidgets() const | - | ||||||||||||
271 | { | - | ||||||||||||
272 | Q_D(const QWidgetAction); | - | ||||||||||||
273 | return d->createdWidgets; never executed: return d->createdWidgets; | 0 | ||||||||||||
274 | } | - | ||||||||||||
275 | - | |||||||||||||
276 | QT_END_NAMESPACE | - | ||||||||||||
277 | - | |||||||||||||
278 | #include "moc_qwidgetaction.cpp" | - | ||||||||||||
279 | - | |||||||||||||
280 | #endif // QT_NO_ACTION | - | ||||||||||||
Source code | Switch to Preprocessed file |