kernel/qwidgetaction.cpp

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

Generated by Squish Coco Non-Commercial