Qt 4.8
qwidgetaction.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
49 
115  : QAction(*(new QWidgetActionPrivate), parent)
116 {
117 }
118 
123 {
125  for (int i = 0; i < d->createdWidgets.count(); ++i)
126  disconnect(d->createdWidgets.at(i), SIGNAL(destroyed(QObject*)),
127  this, SLOT(_q_widgetDestroyed(QObject*)));
128  QList<QWidget *> widgetsToDelete = d->createdWidgets;
129  d->createdWidgets.clear();
130  qDeleteAll(widgetsToDelete);
131  delete d->defaultWidget;
132 }
133 
142 {
144  if (widget == d->defaultWidget || d->defaultWidgetInUse)
145  return;
146  delete d->defaultWidget;
147  d->defaultWidget = widget;
148  if (!widget)
149  return;
150 
152  d->defaultWidget->hide();
153  d->defaultWidget->setParent(0);
154  d->defaultWidgetInUse = false;
155  if (!isEnabled())
156  d->defaultWidget->setEnabled(false);
157 }
158 
163 {
164  Q_D(const QWidgetAction);
165  return d->defaultWidget;
166 }
167 
178 {
180 
181  QWidget *w = createWidget(parent);
182  if (!w) {
183  if (d->defaultWidgetInUse || !d->defaultWidget)
184  return 0;
185  d->defaultWidget->setParent(parent);
186  d->defaultWidgetInUse = true;
187  return d->defaultWidget;
188  }
189 
191  this, SLOT(_q_widgetDestroyed(QObject*)));
192  d->createdWidgets.append(w);
193  return w;
194 }
195 
205 {
207 
208  if (widget == d->defaultWidget) {
209  d->defaultWidget->hide();
210  d->defaultWidget->setParent(0);
211  d->defaultWidgetInUse = false;
212  return;
213  }
214 
215  if (!d->createdWidgets.contains(widget))
216  return;
217 
218  disconnect(widget, SIGNAL(destroyed(QObject*)),
219  this, SLOT(_q_widgetDestroyed(QObject*)));
220  d->createdWidgets.removeAll(widget);
221  deleteWidget(widget);
222 }
223 
228 {
230  if (event->type() == QEvent::ActionChanged) {
231  if (d->defaultWidget)
232  d->defaultWidget->setEnabled(isEnabled());
233  for (int i = 0; i < d->createdWidgets.count(); ++i)
234  d->createdWidgets.at(i)->setEnabled(isEnabled());
235  }
236  return QAction::event(event);
237 }
238 
243 {
244  return QAction::eventFilter(obj,event);
245 }
246 
256 {
257  Q_UNUSED(parent)
258  return 0;
259 }
260 
271 {
272  widget->hide();
273  widget->deleteLater();
274 }
275 
281 {
282  Q_D(const QWidgetAction);
283  return d->createdWidgets;
284 }
285 
287 
288 #include "moc_qwidgetaction.cpp"
289 
290 #endif // QT_NO_ACTION
double d
Definition: qnumeric_p.h:62
bool event(QEvent *)
Reimplemented Function
Definition: qaction.cpp:1256
bool isEnabled() const
Definition: qaction.cpp:1208
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
QWidgetAction(QObject *parent)
Constructs an action with parent.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition: qwidget.h:1008
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QWidget * requestWidget(QWidget *parent)
Returns a widget that represents the action, with the given parent.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
void setDefaultWidget(QWidget *w)
Sets widget to be the default widget.
void hide()
Hides the widget.
Definition: qwidget.h:501
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
virtual ~QWidgetAction()
Destroys the object and frees allocated resources.
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
The QWidgetAction class extends QAction by an interface for inserting custom widgets into action base...
Definition: qwidgetaction.h:57
void setVisible(bool)
Definition: qaction.cpp:1230
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
virtual QWidget * createWidget(QWidget *parent)
This function is called whenever the action is added to a container widget that supports custom widge...
QWidget * defaultWidget() const
Returns the default widget.
virtual bool eventFilter(QObject *, QEvent *)
Reimplemented Function
void releaseWidget(QWidget *widget)
Releases the specified widget.
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
virtual void deleteWidget(QWidget *widget)
This function is called whenever the action is removed from a container widget that displays the acti...
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
QList< QWidget * > createdWidgets() const
Returns the list of widgets that have been using createWidget() and are currently in use by widgets t...
virtual bool event(QEvent *)
Reimplemented Function