src/gui/graphicsview/qgraphicswidget.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qglobal.h"
       
    43 
       
    44 #ifndef QT_NO_GRAPHICSVIEW
       
    45 
       
    46 #include "qgraphicswidget.h"
       
    47 #include "qgraphicswidget_p.h"
       
    48 #include "qgraphicslayout.h"
       
    49 #include "qgraphicslayout_p.h"
       
    50 #include "qgraphicsscene.h"
       
    51 #include "qgraphicssceneevent.h"
       
    52 
       
    53 #ifndef QT_NO_ACTION
       
    54 #include <private/qaction_p.h>
       
    55 #endif
       
    56 #include <private/qapplication_p.h>
       
    57 #include <private/qgraphicsscene_p.h>
       
    58 #ifndef QT_NO_SHORTCUT
       
    59 #include <private/qshortcutmap_p.h>
       
    60 #endif
       
    61 #include <QtCore/qmutex.h>
       
    62 #include <QtGui/qapplication.h>
       
    63 #include <QtGui/qgraphicsview.h>
       
    64 #include <QtGui/qgraphicsproxywidget.h>
       
    65 #include <QtGui/qpalette.h>
       
    66 #include <QtGui/qstyleoption.h>
       
    67 
       
    68 #include <qdebug.h>
       
    69 
       
    70 QT_BEGIN_NAMESPACE
       
    71 
       
    72 /*!
       
    73     \class QGraphicsWidget
       
    74     \brief The QGraphicsWidget class is the base class for all widget
       
    75     items in a QGraphicsScene.
       
    76     \since 4.4
       
    77     \ingroup graphicsview-api
       
    78 
       
    79     QGraphicsWidget is an extended base item that provides extra functionality
       
    80     over QGraphicsItem. It is similar to QWidget in many ways:
       
    81 
       
    82     \list
       
    83         \o Provides a \l palette, a \l font and a \l style().
       
    84         \o Has a defined geometry().
       
    85         \o Supports layouts with setLayout() and layout().
       
    86         \o Supports shortcuts and actions with grabShortcut() and insertAction()
       
    87     \endlist
       
    88 
       
    89     Unlike QGraphicsItem, QGraphicsWidget is not an abstract class; you can
       
    90     create instances of a QGraphicsWidget without having to subclass it.
       
    91     This approach is useful for widgets that only serve the purpose of
       
    92     organizing child widgets into a layout.
       
    93 
       
    94     QGraphicsWidget can be used as a base item for your own custom item if
       
    95     you require advanced input focus handling, e.g., tab focus and activation, or
       
    96     layouts.
       
    97 
       
    98     Since QGraphicsWidget resembles QWidget and has similar API, it is
       
    99     easier to port a widget from QWidget to QGraphicsWidget, instead of
       
   100     QGraphicsItem.
       
   101 
       
   102     \note QWidget-based widgets can be directly embedded into a
       
   103     QGraphicsScene using QGraphicsProxyWidget.
       
   104 
       
   105     Noticeable differences between QGraphicsWidget and QWidget are:
       
   106 
       
   107     \table
       
   108     \header \o QGraphicsWidget
       
   109                 \o QWidget
       
   110     \row      \o Coordinates and geometry are defined with qreals (doubles or
       
   111                     floats, depending on the platform).
       
   112                 \o QWidget uses integer geometry (QPoint, QRect).
       
   113     \row      \o The widget is already visible by default; you do not have to
       
   114                     call show() to display the widget.
       
   115                 \o QWidget is hidden by default until you call show().
       
   116     \row      \o A subset of widget attributes are supported.
       
   117                 \o All widget attributes are supported.
       
   118     \row      \o A top-level item's style defaults to QGraphicsScene::style
       
   119                 \o A top-level widget's style defaults to QApplication::style
       
   120     \row      \o Graphics View provides a custom drag and drop framework, different
       
   121                     from QWidget.
       
   122                 \o Standard drag and drop framework.
       
   123     \row      \o Widget items do not support modality.
       
   124                 \o Full modality support.
       
   125     \endtable
       
   126 
       
   127     QGraphicsWidget supports a subset of Qt's widget attributes,
       
   128     (Qt::WidgetAttribute), as shown in the table below. Any attributes not
       
   129     listed in this table are unsupported, or otherwise unused.
       
   130 
       
   131     \table
       
   132     \header \o Widget Attribute                         \o Usage
       
   133     \row    \o Qt::WA_SetLayoutDirection
       
   134                     \o Set by setLayoutDirection(), cleared by
       
   135                         unsetLayoutDirection(). You can test this attribute to
       
   136                         check if the widget has been explicitly assigned a
       
   137                         \l{QGraphicsWidget::layoutDirection()}
       
   138                         {layoutDirection}. If the attribute is not set, the
       
   139                         \l{QGraphicsWidget::layoutDirection()}
       
   140                         {layoutDirection()} is inherited.
       
   141     \row    \o Qt::WA_RightToLeft
       
   142                     \o Toggled by setLayoutDirection(). Inherited from the
       
   143                         parent/scene. If set, the widget's layout will order
       
   144                         horizontally arranged widgets from right to left.
       
   145     \row    \o Qt::WA_SetStyle
       
   146                     \o Set and cleared by setStyle(). If this attribute is
       
   147                         set, the widget has been explicitly assigned a style.
       
   148                         If it is unset, the widget will use the scene's or the
       
   149                         application's style.
       
   150     \row    \o Qt::WA_Resized
       
   151                     \o Set by setGeometry() and resize().
       
   152     \row    \o Qt::WA_SetPalette
       
   153                     \o Set by setPalette().
       
   154     \row    \o Qt::WA_SetFont
       
   155                     \o Set by setPalette().
       
   156     \row    \o Qt::WA_WindowPropagation
       
   157                     \o Enables propagation to window widgets.
       
   158     \endtable
       
   159 
       
   160     Although QGraphicsWidget inherits from both QObject and QGraphicsItem,
       
   161     you should use the functions provided by QGraphicsItem, \e not QObject, to
       
   162     manage the relationships between parent and child items. These functions
       
   163     control the stacking order of items as well as their ownership.
       
   164 
       
   165     \note The QObject::parent() should always return 0 for QGraphicsWidgets,
       
   166     but this policy is not strictly defined.
       
   167 
       
   168     \sa QGraphicsProxyWidget, QGraphicsItem, {Widgets and Layouts}
       
   169 */
       
   170 
       
   171 /*!
       
   172     Constructs a QGraphicsWidget instance. The optional \a parent argument is
       
   173     passed to QGraphicsItem's constructor. The optional \a wFlags argument
       
   174     specifies the widget's window flags (e.g., whether the widget should be a
       
   175     window, a tool, a popup, etc).
       
   176 */
       
   177 QGraphicsWidget::QGraphicsWidget(QGraphicsItem *parent, Qt::WindowFlags wFlags)
       
   178     : QGraphicsObject(*new QGraphicsWidgetPrivate, 0, 0), QGraphicsLayoutItem(0, false)
       
   179 {
       
   180     Q_D(QGraphicsWidget);
       
   181     d->init(parent, wFlags);
       
   182 }
       
   183 
       
   184 /*!
       
   185     \internal
       
   186 
       
   187     Constructs a new QGraphicsWidget, using \a dd as parent.
       
   188 */
       
   189 QGraphicsWidget::QGraphicsWidget(QGraphicsWidgetPrivate &dd, QGraphicsItem *parent, QGraphicsScene *scene, Qt::WindowFlags wFlags)
       
   190     : QGraphicsObject(dd, 0, scene), QGraphicsLayoutItem(0, false)
       
   191 {
       
   192     Q_D(QGraphicsWidget);
       
   193     d->init(parent, wFlags);
       
   194 }
       
   195 
       
   196 /*
       
   197     \internal
       
   198     \class QGraphicsWidgetStyles
       
   199 
       
   200     We use this thread-safe class to maintain a hash of styles for widgets
       
   201     styles. Note that QApplication::style() itself isn't thread-safe, QStyle
       
   202     isn't thread-safe, and we don't have a thread-safe factory for creating
       
   203     the default style, nor cloning a style.
       
   204 */
       
   205 class QGraphicsWidgetStyles
       
   206 {
       
   207 public:
       
   208     QStyle *styleForWidget(const QGraphicsWidget *widget) const
       
   209     {
       
   210         QMutexLocker locker(&mutex);
       
   211         return styles.value(widget, 0);
       
   212     }
       
   213 
       
   214     void setStyleForWidget(QGraphicsWidget *widget, QStyle *style)
       
   215     {
       
   216         QMutexLocker locker(&mutex);
       
   217         if (style)
       
   218             styles[widget] = style;
       
   219         else
       
   220             styles.remove(widget);
       
   221     }
       
   222 
       
   223 private:
       
   224     QMap<const QGraphicsWidget *, QStyle *> styles;
       
   225     mutable QMutex mutex;
       
   226 };
       
   227 Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles)
       
   228 
       
   229 /*!
       
   230     Destroys the QGraphicsWidget instance.
       
   231 */
       
   232 QGraphicsWidget::~QGraphicsWidget()
       
   233 {
       
   234     Q_D(QGraphicsWidget);
       
   235 #ifndef QT_NO_ACTION
       
   236     // Remove all actions from this widget
       
   237     for (int i = 0; i < d->actions.size(); ++i) {
       
   238         QActionPrivate *apriv = d->actions.at(i)->d_func();
       
   239         apriv->graphicsWidgets.removeAll(this);
       
   240     }
       
   241     d->actions.clear();
       
   242 #endif
       
   243 
       
   244     if (QGraphicsScene *scn = scene()) {
       
   245         QGraphicsScenePrivate *sceneD = scn->d_func();
       
   246         if (sceneD->tabFocusFirst == this)
       
   247             sceneD->tabFocusFirst = (d->focusNext == this ? 0 : d->focusNext);
       
   248     }
       
   249     d->focusPrev->d_func()->focusNext = d->focusNext;
       
   250     d->focusNext->d_func()->focusPrev = d->focusPrev;
       
   251 
       
   252     // Play it really safe
       
   253     d->focusNext = this;
       
   254     d->focusPrev = this;
       
   255 
       
   256     clearFocus();
       
   257 
       
   258     //we check if we have a layout previously
       
   259     if (d->layout) {
       
   260         QGraphicsLayout *temp = d->layout;
       
   261         foreach (QGraphicsItem * item, childItems()) {
       
   262             // In case of a custom layout which doesn't remove and delete items, we ensure that
       
   263             // the parent layout item does not point to the deleted layout. This code is here to
       
   264             // avoid regression from 4.4 to 4.5, because according to 4.5 docs it is not really needed.
       
   265             if (item->isWidget()) {
       
   266                 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
       
   267                 if (widget->parentLayoutItem() == d->layout)
       
   268                     widget->setParentLayoutItem(0);
       
   269             }
       
   270         }
       
   271         d->layout = 0;
       
   272         delete temp;
       
   273     }
       
   274 
       
   275     // Remove this graphics widget from widgetStyles
       
   276     widgetStyles()->setStyleForWidget(this, 0);
       
   277 }
       
   278 
       
   279 /*!
       
   280     \property QGraphicsWidget::size
       
   281     \brief the size of the widget
       
   282 
       
   283     Calling resize() resizes the widget to a \a size bounded by minimumSize()
       
   284     and maximumSize(). This property only affects the widget's width and
       
   285     height (e.g., its right and bottom edges); the widget's position and
       
   286     top-left corner remains unaffected.
       
   287 
       
   288     Resizing a widget triggers the widget to immediately receive a
       
   289     \l{QEvent::GraphicsSceneResize}{GraphicsSceneResize} event with the
       
   290     widget's old and new size.  If the widget has a layout assigned when this
       
   291     event arrives, the layout will be activated and it will automatically
       
   292     update any child widgets's geometry.
       
   293 
       
   294     This property does not affect any layout of the parent widget. If the
       
   295     widget itself is managed by a parent layout; e.g., it has a parent widget
       
   296     with a layout assigned, that layout will not activate.
       
   297 
       
   298     By default, this property contains a size with zero width and height.
       
   299 
       
   300     \sa setGeometry(), QGraphicsSceneResizeEvent, QGraphicsLayout
       
   301 */
       
   302 QSizeF QGraphicsWidget::size() const
       
   303 {
       
   304     return QGraphicsLayoutItem::geometry().size();
       
   305 }
       
   306 
       
   307 void QGraphicsWidget::resize(const QSizeF &size)
       
   308 {
       
   309     setGeometry(QRectF(pos(), size));
       
   310 }
       
   311 
       
   312 /*!
       
   313     \fn void QGraphicsWidget::resize(qreal w, qreal h)
       
   314 
       
   315     This convenience function is equivalent to calling resize(QSizeF(w, h)).
       
   316 
       
   317     \sa setGeometry(), setTransform()
       
   318 */
       
   319 
       
   320 /*!
       
   321     \property QGraphicsWidget::geometry
       
   322     \brief the geometry of the widget
       
   323 
       
   324     Sets the item's geometry to \a rect. The item's position and size are
       
   325     modified as a result of calling this function. The item is first moved,
       
   326     then resized.
       
   327 
       
   328     A side effect of calling this function is that the widget will receive
       
   329     a move event and a resize event. Also, if the widget has a layout
       
   330     assigned, the layout will activate.
       
   331     
       
   332     \sa geometry(), resize()
       
   333 */
       
   334 void QGraphicsWidget::setGeometry(const QRectF &rect)
       
   335 {
       
   336     QGraphicsWidgetPrivate *wd = QGraphicsWidget::d_func();
       
   337     QGraphicsLayoutItemPrivate *d = QGraphicsLayoutItem::d_ptr.data();
       
   338     QRectF newGeom;
       
   339     QPointF oldPos = d->geom.topLeft();
       
   340     if (!wd->inSetPos) {
       
   341         setAttribute(Qt::WA_Resized);
       
   342         newGeom = rect;
       
   343         newGeom.setSize(rect.size().expandedTo(effectiveSizeHint(Qt::MinimumSize))
       
   344                                    .boundedTo(effectiveSizeHint(Qt::MaximumSize)));
       
   345         if (newGeom == d->geom)
       
   346             return;
       
   347 
       
   348         // setPos triggers ItemPositionChange, which can adjust position
       
   349         wd->inSetGeometry = 1;
       
   350         setPos(newGeom.topLeft());
       
   351         wd->inSetGeometry = 0;
       
   352         newGeom.moveTopLeft(pos());
       
   353 
       
   354         if (newGeom == d->geom)
       
   355            return;
       
   356 
       
   357          // Update and prepare to change the geometry (remove from index) if the size has changed.
       
   358         if (wd->scene) {
       
   359             if (rect.topLeft() == d->geom.topLeft()) {
       
   360                 prepareGeometryChange();
       
   361             }
       
   362         }
       
   363     }
       
   364 
       
   365     // Update the layout item geometry
       
   366     bool moved = oldPos != pos();
       
   367     if (moved) {
       
   368         // Send move event.
       
   369         QGraphicsSceneMoveEvent event;
       
   370         event.setOldPos(oldPos);
       
   371         event.setNewPos(pos());
       
   372         QApplication::sendEvent(this, &event);
       
   373         if (wd->inSetPos) {
       
   374             //set the new pos
       
   375             d->geom.moveTopLeft(pos());
       
   376             return;
       
   377         }
       
   378     }
       
   379     QSizeF oldSize = size();
       
   380     QGraphicsLayoutItem::setGeometry(newGeom);
       
   381 
       
   382     wd->invalidateCachedClipPathRecursively();
       
   383 
       
   384     // Send resize event
       
   385     bool resized = newGeom.size() != oldSize;
       
   386     if (resized) {
       
   387         QGraphicsSceneResizeEvent re;
       
   388         re.setOldSize(oldSize);
       
   389         re.setNewSize(newGeom.size());
       
   390         QApplication::sendEvent(this, &re);
       
   391     }
       
   392 }
       
   393 
       
   394 /*!
       
   395     \fn QRectF QGraphicsWidget::rect() const
       
   396 
       
   397     Returns the item's local rect as a QRectF. This function is equivalent
       
   398     to QRectF(QPointF(), size()).
       
   399 
       
   400     \sa setGeometry(), resize()
       
   401 */
       
   402 
       
   403 /*!
       
   404     \fn void QGraphicsWidget::setGeometry(qreal x, qreal y, qreal w, qreal h)
       
   405 
       
   406     This convenience function is equivalent to calling setGeometry(QRectF(
       
   407     \a x, \a y, \a w, \a h)).
       
   408 
       
   409     \sa geometry(), resize()
       
   410 */
       
   411 
       
   412 /*!
       
   413     Sets the widget's contents margins to \a left, \a top, \a right and \a
       
   414     bottom.
       
   415 
       
   416     Contents margins are used by the assigned layout to define the placement
       
   417     of subwidgets and layouts. Margins are particularily useful for widgets
       
   418     that constrain subwidgets to only a section of its own geometry. For
       
   419     example, a group box with a layout will place subwidgets inside its frame,
       
   420     but below the title.
       
   421 
       
   422     Changing a widget's contents margins will always trigger an update(), and
       
   423     any assigned layout will be activated automatically. The widget will then
       
   424     receive a \l{QEvent::ContentsRectChange}{ContentsRectChange} event.
       
   425 
       
   426     \sa getContentsMargins(), setGeometry()
       
   427 */
       
   428 void QGraphicsWidget::setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
       
   429 {
       
   430     Q_D(QGraphicsWidget);
       
   431 
       
   432     if (!d->margins && left == 0 && top == 0 && right == 0 && bottom == 0)
       
   433         return;
       
   434     d->ensureMargins();
       
   435     if (left == d->margins[d->Left]
       
   436         && top == d->margins[d->Top]
       
   437         && right == d->margins[d->Right]
       
   438         && bottom == d->margins[d->Bottom])
       
   439         return;
       
   440 
       
   441     d->margins[d->Left] = left;
       
   442     d->margins[d->Top] = top;
       
   443     d->margins[d->Right] = right;
       
   444     d->margins[d->Bottom] = bottom;
       
   445 
       
   446     if (QGraphicsLayout *l = d->layout)
       
   447         l->invalidate();
       
   448     else
       
   449         updateGeometry();
       
   450 
       
   451     QEvent e(QEvent::ContentsRectChange);
       
   452     QApplication::sendEvent(this, &e);
       
   453 }
       
   454 
       
   455 /*!
       
   456     Gets the widget's contents margins. The margins are stored in \a left, \a
       
   457     top, \a right and \a bottom, as pointers to qreals. Each argument can
       
   458     be \e {omitted} by passing 0.
       
   459 
       
   460     \sa setContentsMargins()
       
   461 */
       
   462 void QGraphicsWidget::getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
       
   463 {
       
   464     Q_D(const QGraphicsWidget);
       
   465     if (left || top || right || bottom)
       
   466         d->ensureMargins();
       
   467     if (left)
       
   468         *left = d->margins[d->Left];
       
   469     if (top)
       
   470         *top = d->margins[d->Top];
       
   471     if (right)
       
   472         *right = d->margins[d->Right];
       
   473     if (bottom)
       
   474         *bottom = d->margins[d->Bottom];
       
   475 }
       
   476 
       
   477 /*!
       
   478     Sets the widget's window frame margins to \a left, \a top, \a right and
       
   479     \a bottom. The default frame margins are provided by the style, and they
       
   480     depend on the current window flags.
       
   481 
       
   482     If you would like to draw your own window decoration, you can set your
       
   483     own frame margins to override the default margins.
       
   484 
       
   485     \sa unsetWindowFrameMargins(), getWindowFrameMargins(), windowFrameRect()
       
   486 */
       
   487 void QGraphicsWidget::setWindowFrameMargins(qreal left, qreal top, qreal right, qreal bottom)
       
   488 {
       
   489     Q_D(QGraphicsWidget);
       
   490 
       
   491     if (!d->windowFrameMargins && left == 0 && top == 0 && right == 0 && bottom == 0)
       
   492         return;
       
   493     d->ensureWindowFrameMargins();
       
   494     bool unchanged =
       
   495         d->windowFrameMargins[d->Left] == left
       
   496         && d->windowFrameMargins[d->Top] == top
       
   497         && d->windowFrameMargins[d->Right] == right
       
   498         && d->windowFrameMargins[d->Bottom] == bottom;
       
   499     if (d->setWindowFrameMargins && unchanged)
       
   500         return;
       
   501     if (!unchanged)
       
   502         prepareGeometryChange();
       
   503     d->windowFrameMargins[d->Left] = left;
       
   504     d->windowFrameMargins[d->Top] = top;
       
   505     d->windowFrameMargins[d->Right] = right;
       
   506     d->windowFrameMargins[d->Bottom] = bottom;
       
   507     d->setWindowFrameMargins = true;
       
   508 }
       
   509 
       
   510 /*!
       
   511     Gets the widget's window frame margins. The margins are stored in \a left,
       
   512     \a top, \a right and \a bottom as pointers to qreals. Each argument can
       
   513     be \e {omitted} by passing 0.
       
   514 
       
   515     \sa setWindowFrameMargins(), windowFrameRect()
       
   516 */
       
   517 void QGraphicsWidget::getWindowFrameMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
       
   518 {
       
   519     Q_D(const QGraphicsWidget);
       
   520     if (left || top || right || bottom)
       
   521         d->ensureWindowFrameMargins();
       
   522     if (left)
       
   523         *left = d->windowFrameMargins[d->Left];
       
   524     if (top)
       
   525         *top = d->windowFrameMargins[d->Top];
       
   526     if (right)
       
   527         *right = d->windowFrameMargins[d->Right];
       
   528     if (bottom)
       
   529         *bottom = d->windowFrameMargins[d->Bottom];
       
   530 }
       
   531 
       
   532 /*!
       
   533     Resets the window frame margins to the default value, provided by the style.
       
   534 
       
   535     \sa setWindowFrameMargins(), getWindowFrameMargins(), windowFrameRect()
       
   536 */
       
   537 void QGraphicsWidget::unsetWindowFrameMargins()
       
   538 {
       
   539     Q_D(QGraphicsWidget);
       
   540     if ((d->windowFlags & Qt::Window) && (d->windowFlags & Qt::WindowType_Mask) != Qt::Popup && 
       
   541          (d->windowFlags & Qt::WindowType_Mask) != Qt::ToolTip && !(d->windowFlags & Qt::FramelessWindowHint)) {
       
   542         QStyleOptionTitleBar bar;
       
   543         d->initStyleOptionTitleBar(&bar);
       
   544         QStyle *style = this->style();
       
   545         qreal margin = style->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth);
       
   546         qreal titleBarHeight  = d->titleBarHeight(bar);
       
   547         setWindowFrameMargins(margin, titleBarHeight, margin, margin);
       
   548     } else {
       
   549         setWindowFrameMargins(0, 0, 0, 0);
       
   550     }
       
   551     d->setWindowFrameMargins = false;
       
   552 }
       
   553 
       
   554 /*!
       
   555     Returns the widget's geometry in parent coordinates including any window
       
   556     frame.
       
   557 
       
   558     \sa windowFrameRect(), getWindowFrameMargins(), setWindowFrameMargins()
       
   559 */
       
   560 QRectF QGraphicsWidget::windowFrameGeometry() const
       
   561 {
       
   562     Q_D(const QGraphicsWidget);
       
   563     return d->windowFrameMargins
       
   564         ? geometry().adjusted(-d->windowFrameMargins[d->Left], -d->windowFrameMargins[d->Top],
       
   565                               d->windowFrameMargins[d->Right], d->windowFrameMargins[d->Bottom])
       
   566         : geometry();
       
   567 }
       
   568 
       
   569 /*!
       
   570     Returns the widget's local rect including any window frame.
       
   571 
       
   572     \sa windowFrameGeometry(), getWindowFrameMargins(), setWindowFrameMargins()
       
   573 */
       
   574 QRectF QGraphicsWidget::windowFrameRect() const
       
   575 {
       
   576     Q_D(const QGraphicsWidget);
       
   577     return d->windowFrameMargins
       
   578         ? rect().adjusted(-d->windowFrameMargins[d->Left], -d->windowFrameMargins[d->Top],
       
   579                           d->windowFrameMargins[d->Right], d->windowFrameMargins[d->Bottom])
       
   580         : rect();
       
   581 }
       
   582 
       
   583 /*!
       
   584     Populates a style option object for this widget based on its current
       
   585     state, and stores the output in \a option. The default implementation
       
   586     populates \a option with the following properties.
       
   587 
       
   588     \table
       
   589       \header
       
   590         \o Style Option Property
       
   591         \o Value
       
   592       \row
       
   593         \o state & QStyle::State_Enabled
       
   594         \o Corresponds to QGraphicsItem::isEnabled().
       
   595       \row
       
   596         \o state & QStyle::State_HasFocus
       
   597         \o Corresponds to QGraphicsItem::hasFocus().
       
   598       \row
       
   599         \o state & QStyle::State_MouseOver
       
   600         \o Corresponds to QGraphicsItem::isUnderMouse().
       
   601       \row
       
   602         \o direction
       
   603         \o Corresponds to QGraphicsWidget::layoutDirection().
       
   604       \row
       
   605         \o rect
       
   606         \o Corresponds to QGraphicsWidget::rect().toRect().
       
   607       \row
       
   608         \o palette
       
   609         \o Corresponds to QGraphicsWidget::palette().
       
   610       \row
       
   611         \o fontMetrics
       
   612         \o Corresponds to QFontMetrics(QGraphicsWidget::font()).
       
   613     \endtable
       
   614 
       
   615     Subclasses of QGraphicsWidget should call the base implementation, and
       
   616     then test the type of \a option using qstyleoption_cast<>() or test
       
   617     QStyleOption::Type before storing widget-specific options.
       
   618 
       
   619     For example:
       
   620 
       
   621     \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicswidget.cpp 0
       
   622 
       
   623     \sa QStyleOption::initFrom()
       
   624 */
       
   625 void QGraphicsWidget::initStyleOption(QStyleOption *option) const
       
   626 {
       
   627     Q_ASSERT(option);
       
   628 
       
   629     option->state = QStyle::State_None;
       
   630     if (isEnabled())
       
   631         option->state |= QStyle::State_Enabled;
       
   632     if (hasFocus())
       
   633         option->state |= QStyle::State_HasFocus;
       
   634     // if (window->testAttribute(Qt::WA_KeyboardFocusChange)) // ### Window
       
   635     //     option->state |= QStyle::State_KeyboardFocusChange;
       
   636     if (isUnderMouse())
       
   637         option->state |= QStyle::State_MouseOver;
       
   638     if (QGraphicsWidget *w = window()) {
       
   639         if (w->isActiveWindow())
       
   640             option->state |= QStyle::State_Active;
       
   641     }
       
   642     if (isWindow())
       
   643         option->state |= QStyle::State_Window;
       
   644     /*
       
   645       ###
       
   646 #ifdef Q_WS_MAC
       
   647     extern bool qt_mac_can_clickThrough(const QGraphicsWidget *w); //qwidget_mac.cpp
       
   648     if (!(option->state & QStyle::State_Active) && !qt_mac_can_clickThrough(widget))
       
   649         option->state &= ~QStyle::State_Enabled;
       
   650 
       
   651     switch (QMacStyle::widgetSizePolicy(widget)) {
       
   652     case QMacStyle::SizeSmall:
       
   653         option->state |= QStyle::State_Small;
       
   654         break;
       
   655     case QMacStyle::SizeMini:
       
   656         option->state |= QStyle::State_Mini;
       
   657         break;
       
   658     default:
       
   659         ;
       
   660     }
       
   661 #endif
       
   662 #ifdef QT_KEYPAD_NAVIGATION
       
   663     if (widget->hasEditFocus())
       
   664         state |= QStyle::State_HasEditFocus;
       
   665 #endif
       
   666     */
       
   667     option->direction = layoutDirection();
       
   668     option->rect = rect().toRect(); // ### truncation!
       
   669     option->palette = palette();
       
   670     if (!isEnabled()) {
       
   671         option->palette.setCurrentColorGroup(QPalette::Disabled);
       
   672     } else if (isActiveWindow()) {
       
   673         option->palette.setCurrentColorGroup(QPalette::Active);
       
   674     } else {
       
   675         option->palette.setCurrentColorGroup(QPalette::Inactive);
       
   676     }
       
   677     option->fontMetrics = QFontMetrics(font());
       
   678 }
       
   679 
       
   680 /*!
       
   681     \reimp
       
   682 */
       
   683 QSizeF QGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   684 {
       
   685     Q_D(const QGraphicsWidget);
       
   686     QSizeF sh;
       
   687     if (d->layout) {
       
   688         QSizeF marginSize(0,0);
       
   689         if (d->margins) {
       
   690             marginSize = QSizeF(d->margins[d->Left] + d->margins[d->Right],
       
   691                          d->margins[d->Top] + d->margins[d->Bottom]);
       
   692         }
       
   693         sh = d->layout->effectiveSizeHint(which, constraint - marginSize);
       
   694         sh += marginSize;
       
   695     } else {
       
   696         switch (which) {
       
   697             case Qt::MinimumSize:
       
   698                 sh = QSizeF(0, 0);
       
   699                 break;
       
   700             case Qt::PreferredSize:
       
   701                 sh = QSizeF(50, 50);    //rather arbitrary
       
   702                 break;
       
   703             case Qt::MaximumSize:
       
   704                 sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
       
   705                 break;
       
   706             default:
       
   707                 qWarning("QGraphicsWidget::sizeHint(): Don't know how to handle the value of 'which'");
       
   708                 break;
       
   709         }
       
   710     }
       
   711     return sh;
       
   712 }
       
   713 
       
   714 /*!
       
   715     Returns this widget's layout, or 0 if no layout is currently managing this
       
   716     widget.
       
   717 
       
   718     \sa setLayout()
       
   719 */
       
   720 QGraphicsLayout *QGraphicsWidget::layout() const
       
   721 {
       
   722     Q_D(const QGraphicsWidget);
       
   723     return d->layout;
       
   724 }
       
   725 
       
   726 /*!
       
   727     \fn void QGraphicsWidget::setLayout(QGraphicsLayout *layout)
       
   728 
       
   729     Sets the layout for this widget to \a layout. Any existing layout manager
       
   730     is deleted before the new layout is assigned. If \a layout is 0, the
       
   731     widget is left without a layout. Existing subwidgets' geometries will
       
   732     remain unaffected.
       
   733 
       
   734     All widgets that are currently managed by \a layout or all of its
       
   735     sublayouts, are automatically reparented to this item. The layout is then
       
   736     invalidated, and the child widget geometries are adjusted according to
       
   737     this item's geometry() and contentsMargins(). Children who are not
       
   738     explicitly managed by \a layout remain unaffected by the layout after
       
   739     it has been assigned to this widget.
       
   740 
       
   741     QGraphicsWidget takes ownership of \a layout.
       
   742 
       
   743     \sa layout(), QGraphicsLinearLayout::addItem(), QGraphicsLayout::invalidate()
       
   744 */
       
   745 void QGraphicsWidget::setLayout(QGraphicsLayout *l)
       
   746 {
       
   747     Q_D(QGraphicsWidget);
       
   748     if (d->layout == l)
       
   749         return;
       
   750     d->setLayout_helper(l);
       
   751     if (!l)
       
   752         return;
       
   753 
       
   754     // Prevent assigning a layout that is already assigned to another widget.
       
   755     QGraphicsLayoutItem *oldParent = l->parentLayoutItem();
       
   756     if (oldParent && oldParent != this) {
       
   757         qWarning("QGraphicsWidget::setLayout: Attempting to set a layout on %s"
       
   758                  " \"%s\", when the layout already has a parent",
       
   759                  metaObject()->className(), qPrintable(objectName()));
       
   760         return;
       
   761     }
       
   762 
       
   763     // Install and activate the layout.
       
   764     l->setParentLayoutItem(this);
       
   765     l->d_func()->reparentChildItems(this);
       
   766     l->invalidate();
       
   767 }
       
   768 
       
   769 /*!
       
   770     Adjusts the size of the widget to its effective preferred size hint.
       
   771 
       
   772     This function is called implicitly when the item is shown for the first
       
   773     time.
       
   774 
       
   775     \sa effectiveSizeHint(), Qt::MinimumSize
       
   776 */
       
   777 void QGraphicsWidget::adjustSize()
       
   778 {
       
   779     QSizeF sz = effectiveSizeHint(Qt::PreferredSize);
       
   780     // What if sz is not valid?!
       
   781     if (sz.isValid())
       
   782         resize(sz);
       
   783 }
       
   784 
       
   785 /*!
       
   786     \property QGraphicsWidget::layoutDirection
       
   787     \brief the layout direction for this widget.
       
   788 
       
   789     This property modifies this widget's and all of its descendants'
       
   790     Qt::WA_RightToLeft attribute. It also sets this widget's
       
   791     Qt::WA_SetLayoutDirection attribute.
       
   792 
       
   793     The widget's layout direction determines the order in which the layout
       
   794     manager horizontally arranges subwidgets of this widget. The default
       
   795     value depends on the language and locale of the application, and is
       
   796     typically in the same direction as words are read and written. With
       
   797     Qt::LeftToRight, the layout starts placing subwidgets from the left
       
   798     side of this widget towards the right. Qt::RightToLeft does the opposite -
       
   799     the layout will place widgets starting from the right edge moving towards
       
   800     the left.
       
   801 
       
   802     Subwidgets inherit their layout direction from the parent. Top-level
       
   803     widget items inherit their layout direction from
       
   804     QGraphicsScene::layoutDirection. If you change a widget's layout direction
       
   805     by calling setLayoutDirection(), the widget will send itself a
       
   806     \l{QEvent::LayoutDirectionChange}{LayoutDirectionChange} event, and then
       
   807     propagate the new layout direction to all its descendants.
       
   808 
       
   809     \sa QWidget::layoutDirection, QApplication::layoutDirection
       
   810 */
       
   811 Qt::LayoutDirection QGraphicsWidget::layoutDirection() const
       
   812 {
       
   813     return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
       
   814 }
       
   815 void QGraphicsWidget::setLayoutDirection(Qt::LayoutDirection direction)
       
   816 {
       
   817     Q_D(QGraphicsWidget);
       
   818     setAttribute(Qt::WA_SetLayoutDirection, true);
       
   819     d->setLayoutDirection_helper(direction);
       
   820 }
       
   821 void QGraphicsWidget::unsetLayoutDirection()
       
   822 {
       
   823     Q_D(QGraphicsWidget);
       
   824     setAttribute(Qt::WA_SetLayoutDirection, false);
       
   825     d->resolveLayoutDirection();
       
   826 }
       
   827 
       
   828 /*!
       
   829     Returns a pointer to the widget's style. If this widget does not have any
       
   830     explicitly assigned style, the scene's style is returned instead. In turn,
       
   831     if the scene does not have any assigned style, this function returns
       
   832     QApplication::style().
       
   833 
       
   834     \sa setStyle()
       
   835 */
       
   836 QStyle *QGraphicsWidget::style() const
       
   837 {
       
   838     if (QStyle *style = widgetStyles()->styleForWidget(this))
       
   839         return style;
       
   840     // ### This is not thread-safe. QApplication::style() is not thread-safe.
       
   841     return scene() ? scene()->style() : QApplication::style();
       
   842 }
       
   843 
       
   844 /*!
       
   845     Sets the widget's style to \a style. QGraphicsWidget does \e not take
       
   846     ownership of \a style.
       
   847 
       
   848     If no style is assigned, or \a style is 0, the widget will use
       
   849     QGraphicsScene::style() (if this has been set). Otherwise the widget will
       
   850     use QApplication::style().
       
   851 
       
   852     This function sets the Qt::WA_SetStyle attribute if \a style is not 0;
       
   853     otherwise it clears the attribute.
       
   854 
       
   855     \sa style()
       
   856 */
       
   857 void QGraphicsWidget::setStyle(QStyle *style)
       
   858 {
       
   859     setAttribute(Qt::WA_SetStyle, style != 0);
       
   860     widgetStyles()->setStyleForWidget(this, style);
       
   861 
       
   862     // Deliver StyleChange to the widget itself (doesn't propagate).
       
   863     QEvent event(QEvent::StyleChange);
       
   864     QApplication::sendEvent(this, &event);
       
   865 }
       
   866 
       
   867 /*!
       
   868     \property QGraphicsWidget::font
       
   869     \brief the widgets' font
       
   870 
       
   871     This property provides the widget's font.
       
   872 
       
   873     QFont consists of font properties that have been explicitly defined and
       
   874     properties implicitly inherited from the widget's parent. Hence, font()
       
   875     can return a different font compared to the one set with setFont().
       
   876     This scheme allows you to define single entries in a font without
       
   877     affecting the font's inherited entries.
       
   878 
       
   879     When a widget's font changes, it resolves its entries against its
       
   880     parent widget. If the widget does not have a parent widget, it resolves
       
   881     its entries against the scene. The widget then sends itself a
       
   882     \l{QEvent::FontChange}{FontChange} event and notifies all its
       
   883     descendants so that they can resolve their fonts as well.
       
   884 
       
   885     By default, this property contains the application's default font.
       
   886 
       
   887     \sa QApplication::font(), QGraphicsScene::font, QFont::resolve()
       
   888 */
       
   889 QFont QGraphicsWidget::font() const
       
   890 {
       
   891     Q_D(const QGraphicsWidget);
       
   892     return d->font;
       
   893 }
       
   894 void QGraphicsWidget::setFont(const QFont &font)
       
   895 {
       
   896     Q_D(QGraphicsWidget);
       
   897     setAttribute(Qt::WA_SetFont, font.resolve() != 0);
       
   898 
       
   899     QFont naturalFont = d->naturalWidgetFont();
       
   900     QFont resolvedFont = font.resolve(naturalFont);
       
   901     d->setFont_helper(resolvedFont);
       
   902 }
       
   903 
       
   904 /*!
       
   905     \property QGraphicsWidget::palette
       
   906     \brief the widget's palette
       
   907 
       
   908     This property provides the widget's palette. The palette provides colors
       
   909     and brushes for color groups (e.g., QPalette::Button) and states (e.g.,
       
   910     QPalette::Inactive), loosely defining the general look of the widget and
       
   911     its children.
       
   912 
       
   913     QPalette consists of color groups that have been explicitly defined, and
       
   914     groups that are implicitly inherited from the widget's parent. Because of
       
   915     this, palette() can return a different palette than what has been set with
       
   916     setPalette(). This scheme allows you to define single entries in a palette
       
   917     without affecting the palette's inherited entries.
       
   918 
       
   919     When a widget's palette changes, it resolves its entries against its
       
   920     parent widget, or if it doesn't have a parent widget, it resolves against
       
   921     the scene. It then sends itself a \l{QEvent::PaletteChange}{PaletteChange}
       
   922     event, and notifies all its descendants so they can resolve their palettes
       
   923     as well.
       
   924 
       
   925     By default, this property contains the application's default palette.
       
   926 
       
   927     \sa QApplication::palette(), QGraphicsScene::palette, QPalette::resolve()
       
   928 */
       
   929 QPalette QGraphicsWidget::palette() const
       
   930 {
       
   931     Q_D(const QGraphicsWidget);
       
   932     return d->palette;
       
   933 }
       
   934 void QGraphicsWidget::setPalette(const QPalette &palette)
       
   935 {
       
   936     Q_D(QGraphicsWidget);
       
   937     setAttribute(Qt::WA_SetPalette, palette.resolve() != 0);
       
   938 
       
   939     QPalette naturalPalette = d->naturalWidgetPalette();
       
   940     QPalette resolvedPalette = palette.resolve(naturalPalette);
       
   941     d->setPalette_helper(resolvedPalette);
       
   942 }
       
   943 
       
   944 /*!
       
   945     If this widget is currently managed by a layout, this function notifies
       
   946     the layout that the widget's size hints have changed and the layout
       
   947     may need to resize and reposition the widget accordingly.
       
   948 
       
   949     Call this function if the widget's sizeHint() has changed.
       
   950 
       
   951     \sa QGraphicsLayout::invalidate()
       
   952 */
       
   953 void QGraphicsWidget::updateGeometry()
       
   954 {
       
   955     QGraphicsLayoutItem::updateGeometry();
       
   956     QGraphicsLayoutItem *parentItem = parentLayoutItem();
       
   957 
       
   958     if (parentItem && parentItem->isLayout()) {
       
   959         parentItem->updateGeometry();
       
   960     } else {
       
   961         if (parentItem) {
       
   962             QGraphicsWidget *parentWid = parentWidget();    //###
       
   963             if (parentWid->isVisible())
       
   964                 QApplication::postEvent(parentWid, new QEvent(QEvent::LayoutRequest));
       
   965         }
       
   966         bool wasResized = testAttribute(Qt::WA_Resized);
       
   967         resize(size()); // this will restrict the size
       
   968         setAttribute(Qt::WA_Resized, wasResized);
       
   969     }
       
   970 }
       
   971 
       
   972 /*!
       
   973     \reimp
       
   974 
       
   975     QGraphicsWidget uses the base implementation of this function to catch and
       
   976     deliver events related to state changes in the item. Because of this, it is
       
   977     very important that subclasses call the base implementation.
       
   978 
       
   979     \a change specifies the type of change, and \a value is the new value.
       
   980 
       
   981     For example, QGraphicsWidget uses ItemVisibleChange to deliver
       
   982     \l{QEvent::Show} {Show} and \l{QEvent::Hide}{Hide} events,
       
   983     ItemPositionHasChanged to deliver \l{QEvent::Move}{Move} events,
       
   984     and ItemParentChange both to deliver \l{QEvent::ParentChange}
       
   985     {ParentChange} events, and for managing the focus chain.
       
   986 
       
   987     QGraphicsWidget enables the ItemSendsGeometryChanges flag by default in
       
   988     order to track position changes.
       
   989 
       
   990     \sa QGraphicsItem::itemChange()
       
   991 */
       
   992 QVariant QGraphicsWidget::itemChange(GraphicsItemChange change, const QVariant &value)
       
   993 {
       
   994     Q_D(QGraphicsWidget);
       
   995     switch (change) {
       
   996     case ItemEnabledHasChanged: {
       
   997         // Send EnabledChange after the enabled state has changed.
       
   998         QEvent event(QEvent::EnabledChange);
       
   999         QApplication::sendEvent(this, &event);
       
  1000         break;
       
  1001     }
       
  1002     case ItemVisibleChange:
       
  1003         if (value.toBool()) {
       
  1004             // Send Show event before the item has been shown.
       
  1005             QShowEvent event;
       
  1006             QApplication::sendEvent(this, &event);
       
  1007             bool resized = testAttribute(Qt::WA_Resized);
       
  1008             if (!resized) {
       
  1009                 adjustSize();
       
  1010                 setAttribute(Qt::WA_Resized, false);
       
  1011             }
       
  1012         }
       
  1013         break;
       
  1014     case ItemVisibleHasChanged:
       
  1015         if (!value.toBool()) {
       
  1016             // Send Hide event after the item has been hidden.
       
  1017             QHideEvent event;
       
  1018             QApplication::sendEvent(this, &event);
       
  1019         }
       
  1020         break;
       
  1021     case ItemPositionHasChanged:
       
  1022         if (!d->inSetGeometry) {
       
  1023             d->inSetPos = 1;
       
  1024             // Ensure setGeometry is called (avoid recursion when setPos is
       
  1025             // called from within setGeometry).
       
  1026             setGeometry(QRectF(pos(), size()));
       
  1027             d->inSetPos = 0 ;
       
  1028         }
       
  1029         break;
       
  1030     case ItemParentChange: {
       
  1031         QGraphicsItem *parent = qVariantValue<QGraphicsItem *>(value);
       
  1032         d->fixFocusChainBeforeReparenting((parent && parent->isWidget()) ? static_cast<QGraphicsWidget *>(parent) : 0);
       
  1033 
       
  1034         // Deliver ParentAboutToChange.
       
  1035         QEvent event(QEvent::ParentAboutToChange);
       
  1036         QApplication::sendEvent(this, &event);
       
  1037         break;
       
  1038     }
       
  1039     case ItemParentHasChanged: {
       
  1040         // Deliver ParentChange.
       
  1041         QEvent event(QEvent::ParentChange);
       
  1042         QApplication::sendEvent(this, &event);
       
  1043         break;
       
  1044     }
       
  1045     case ItemCursorChange: {
       
  1046         // Deliver CursorChange.
       
  1047         QEvent event(QEvent::CursorChange);
       
  1048         QApplication::sendEvent(this, &event);
       
  1049         break;
       
  1050     }
       
  1051     case ItemToolTipChange: {
       
  1052         // Deliver ToolTipChange.
       
  1053         QEvent event(QEvent::ToolTipChange);
       
  1054         QApplication::sendEvent(this, &event);
       
  1055         break;
       
  1056     }
       
  1057     default:
       
  1058         break;
       
  1059     }
       
  1060     return QGraphicsItem::itemChange(change, value);
       
  1061 }
       
  1062 
       
  1063 /*!
       
  1064     \internal
       
  1065 
       
  1066     This virtual function is used to notify changes to any property (both
       
  1067     dynamic properties, and registered with Q_PROPERTY) in the
       
  1068     widget. Depending on the property itself, the notification can be
       
  1069     delivered before or after the value has changed.
       
  1070 
       
  1071     \a propertyName is the name of the property (e.g., "size" or "font"), and
       
  1072     \a value is the (proposed) new value of the property. The function returns
       
  1073     the new value, which may be different from \a value if the notification
       
  1074     supports adjusting the property value. The base implementation simply
       
  1075     returns \a value for any \a propertyName.
       
  1076 
       
  1077     QGraphicsWidget delivers notifications for the following properties:
       
  1078 
       
  1079     \table     \o propertyName        \o Property
       
  1080     \row       \o layoutDirection     \o QGraphicsWidget::layoutDirection
       
  1081     \row       \o size                \o QGraphicsWidget::size
       
  1082     \row       \o font                \o QGraphicsWidget::font
       
  1083     \row       \o palette             \o QGraphicsWidget::palette
       
  1084     \endtable
       
  1085 
       
  1086     \sa itemChange()
       
  1087 */
       
  1088 QVariant QGraphicsWidget::propertyChange(const QString &propertyName, const QVariant &value)
       
  1089 {
       
  1090     Q_UNUSED(propertyName);
       
  1091     return value;
       
  1092 }
       
  1093 
       
  1094 /*!
       
  1095     QGraphicsWidget's implementation of sceneEvent() simply passes \a event to
       
  1096     QGraphicsWidget::event(). You can handle all events for your widget in
       
  1097     event() or in any of the convenience functions; you should not have to
       
  1098     reimplement this function in a subclass of QGraphicsWidget.
       
  1099 
       
  1100     \sa QGraphicsItem::sceneEvent()
       
  1101 */
       
  1102 bool QGraphicsWidget::sceneEvent(QEvent *event)
       
  1103 {
       
  1104     return QGraphicsItem::sceneEvent(event);
       
  1105 }
       
  1106 
       
  1107 /*!
       
  1108     This event handler, for \a event, receives events for the window frame if
       
  1109     this widget is a window. Its base implementation provides support for
       
  1110     default window frame interaction such as moving, resizing, etc.
       
  1111 
       
  1112     You can reimplement this handler in a subclass of QGraphicsWidget to
       
  1113     provide your own custom window frame interaction support.
       
  1114 
       
  1115     Returns true if \a event has been recognized and processed; otherwise,
       
  1116     returns false.
       
  1117     
       
  1118     \sa event()
       
  1119 */
       
  1120 bool QGraphicsWidget::windowFrameEvent(QEvent *event)
       
  1121 {
       
  1122     Q_D(QGraphicsWidget);
       
  1123     switch (event->type()) {
       
  1124     case QEvent::GraphicsSceneMousePress:
       
  1125         d->windowFrameMousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
  1126         break;
       
  1127     case QEvent::GraphicsSceneMouseMove:
       
  1128         d->ensureWindowData();
       
  1129         if (d->windowData->grabbedSection != Qt::NoSection) {
       
  1130             d->windowFrameMouseMoveEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
  1131             event->accept();
       
  1132         }
       
  1133         break;
       
  1134     case QEvent::GraphicsSceneMouseRelease:
       
  1135         d->windowFrameMouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
  1136         break;
       
  1137     case QEvent::GraphicsSceneHoverMove:
       
  1138         d->windowFrameHoverMoveEvent(static_cast<QGraphicsSceneHoverEvent *>(event));
       
  1139         break;
       
  1140     case QEvent::GraphicsSceneHoverLeave:
       
  1141         d->windowFrameHoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent *>(event));
       
  1142         break;
       
  1143     default:
       
  1144         break;
       
  1145     }
       
  1146     return event->isAccepted();
       
  1147 }
       
  1148 
       
  1149 /*!
       
  1150     \since 4.4
       
  1151 
       
  1152     Returns the window frame section at position \a pos, or
       
  1153     Qt::NoSection if there is no window frame section at this
       
  1154     position.
       
  1155 
       
  1156     This function is used in QGraphicsWidget's base implementation for window
       
  1157     frame interaction.
       
  1158 
       
  1159     You can reimplement this function if you want to customize how a window
       
  1160     can be interactively moved or resized.  For instance, if you only want to
       
  1161     allow a window to be resized by the bottom right corner, you can
       
  1162     reimplement this function to return Qt::NoSection for all sections except
       
  1163     Qt::BottomRightSection.
       
  1164 
       
  1165     \sa windowFrameEvent(), paintWindowFrame(), windowFrameGeometry()
       
  1166 */
       
  1167 Qt::WindowFrameSection QGraphicsWidget::windowFrameSectionAt(const QPointF &pos) const
       
  1168 {
       
  1169     Q_D(const QGraphicsWidget);
       
  1170 
       
  1171     const QRectF r = windowFrameRect();
       
  1172     if (!r.contains(pos))
       
  1173         return Qt::NoSection;
       
  1174     
       
  1175     const qreal left = r.left();
       
  1176     const qreal top = r.top();
       
  1177     const qreal right = r.right();
       
  1178     const qreal bottom = r.bottom();
       
  1179     const qreal x = pos.x();
       
  1180     const qreal y = pos.y();
       
  1181 
       
  1182     const qreal cornerMargin = 20;
       
  1183     //### Not sure of this one, it should be the same value for all edges.
       
  1184     const qreal windowFrameWidth = d->windowFrameMargins
       
  1185         ? d->windowFrameMargins[d->Left] : 0;
       
  1186 
       
  1187     Qt::WindowFrameSection s = Qt::NoSection;
       
  1188     if (x <= left + cornerMargin) {
       
  1189         if (y <= top + windowFrameWidth || (x <= left + windowFrameWidth && y <= top + cornerMargin)) {
       
  1190             s = Qt::TopLeftSection;
       
  1191         } else if (y >= bottom - windowFrameWidth || (x <= left + windowFrameWidth && y >= bottom - windowFrameWidth)) {
       
  1192             s = Qt::BottomLeftSection;
       
  1193         } else if (x <= left + windowFrameWidth) {
       
  1194             s = Qt::LeftSection;
       
  1195         }
       
  1196     } else if (x >= right - cornerMargin) {
       
  1197         if (y <= top + windowFrameWidth || (x >= right - windowFrameWidth && y <= top + cornerMargin)) {
       
  1198             s = Qt::TopRightSection;
       
  1199         } else if (y >= bottom - windowFrameWidth || (x >= right - windowFrameWidth && y >= bottom - windowFrameWidth)) {
       
  1200             s = Qt::BottomRightSection;
       
  1201         } else if (x >= right - windowFrameWidth) {
       
  1202             s = Qt::RightSection;
       
  1203         }
       
  1204     } else if (y <= top + windowFrameWidth) {
       
  1205         s = Qt::TopSection;
       
  1206     } else if (y >= bottom - windowFrameWidth) {
       
  1207         s = Qt::BottomSection;
       
  1208     }
       
  1209     if (s == Qt::NoSection) {
       
  1210         QRectF r1 = r;
       
  1211         r1.setHeight(d->windowFrameMargins
       
  1212                      ? d->windowFrameMargins[d->Top] : 0);
       
  1213         if (r1.contains(pos))
       
  1214             s = Qt::TitleBarArea;
       
  1215     }
       
  1216     return s;
       
  1217 }
       
  1218 
       
  1219 /*!
       
  1220     \reimp
       
  1221 
       
  1222     Handles the \a event.  QGraphicsWidget handles the following
       
  1223     events:
       
  1224 
       
  1225     \table   \o Event                 \o Usage
       
  1226     \row     \o Polish
       
  1227                     \o Delivered to the widget some time after it has been
       
  1228                         shown.
       
  1229     \row     \o GraphicsSceneMove
       
  1230                     \o Delivered to the widget after its local position has
       
  1231                         changed.
       
  1232     \row     \o GraphicsSceneResize
       
  1233                     \o Delivered to the widget after its size has changed.
       
  1234     \row     \o Show
       
  1235                     \o Delivered to the widget before it has been shown.
       
  1236     \row     \o Hide
       
  1237                     \o Delivered to the widget after it has been hidden.
       
  1238     \row     \o PaletteChange
       
  1239                     \o Delivered to the widget after its palette has changed.
       
  1240     \row     \o FontChange
       
  1241                     \o Delivered to the widget after its font has changed.
       
  1242     \row     \o EnabledChange
       
  1243                     \o Delivered to the widget after its enabled state has
       
  1244                         changed.
       
  1245     \row     \o StyleChange
       
  1246                     \o Delivered to the widget after its style has changed.
       
  1247     \row     \o LayoutDirectionChange
       
  1248                     \o Delivered to the widget after its layout direction has
       
  1249                         changed.
       
  1250     \row     \o ContentsRectChange
       
  1251                     \o Delivered to the widget after its contents margins/
       
  1252                         contents rect has changed.
       
  1253     \endtable
       
  1254 */
       
  1255 bool QGraphicsWidget::event(QEvent *event)
       
  1256 {
       
  1257     Q_D(QGraphicsWidget);
       
  1258     // Forward the event to the layout first.
       
  1259     if (d->layout)
       
  1260         d->layout->widgetEvent(event);
       
  1261 
       
  1262     // Handle the event itself.
       
  1263     switch (event->type()) {
       
  1264     case QEvent::GraphicsSceneMove:
       
  1265         moveEvent(static_cast<QGraphicsSceneMoveEvent *>(event));
       
  1266         break;
       
  1267     case QEvent::GraphicsSceneResize:
       
  1268         resizeEvent(static_cast<QGraphicsSceneResizeEvent *>(event));
       
  1269         break;
       
  1270     case QEvent::Show:
       
  1271         showEvent(static_cast<QShowEvent *>(event));
       
  1272         break;
       
  1273     case QEvent::Hide:
       
  1274         hideEvent(static_cast<QHideEvent *>(event));
       
  1275         break;
       
  1276     case QEvent::Polish:
       
  1277         polishEvent();
       
  1278         d->polished = true;
       
  1279         d->updateFont(d->font);
       
  1280         break;
       
  1281     case QEvent::WindowActivate:
       
  1282     case QEvent::WindowDeactivate:
       
  1283         update();
       
  1284         break;
       
  1285         // Taken from QWidget::event
       
  1286     case QEvent::ActivationChange:
       
  1287     case QEvent::EnabledChange:
       
  1288     case QEvent::FontChange:
       
  1289     case QEvent::StyleChange:
       
  1290     case QEvent::PaletteChange:
       
  1291     case QEvent::ParentChange:
       
  1292     case QEvent::ContentsRectChange:
       
  1293     case QEvent::LayoutDirectionChange:
       
  1294         changeEvent(event);
       
  1295         break;
       
  1296     case QEvent::Close:
       
  1297         closeEvent((QCloseEvent *)event);
       
  1298         break;
       
  1299     case QEvent::GrabMouse:
       
  1300         grabMouseEvent(event);
       
  1301         break;
       
  1302     case QEvent::UngrabMouse:
       
  1303         ungrabMouseEvent(event);
       
  1304         break;
       
  1305     case QEvent::GrabKeyboard:
       
  1306         grabKeyboardEvent(event);
       
  1307         break;
       
  1308     case QEvent::UngrabKeyboard:
       
  1309         ungrabKeyboardEvent(event);
       
  1310         break;
       
  1311     case QEvent::GraphicsSceneMousePress:
       
  1312         if (d->hasDecoration() && windowFrameEvent(event))
       
  1313             return true;
       
  1314     case QEvent::GraphicsSceneMouseMove:
       
  1315     case QEvent::GraphicsSceneMouseRelease:
       
  1316     case QEvent::GraphicsSceneMouseDoubleClick:
       
  1317         d->ensureWindowData();
       
  1318         if (d->hasDecoration() && d->windowData->grabbedSection != Qt::NoSection)
       
  1319             return windowFrameEvent(event);
       
  1320         break;
       
  1321     case QEvent::GraphicsSceneHoverEnter:
       
  1322     case QEvent::GraphicsSceneHoverMove:
       
  1323     case QEvent::GraphicsSceneHoverLeave:
       
  1324         if (d->hasDecoration()) {
       
  1325             windowFrameEvent(event);
       
  1326             // Filter out hover events if they were sent to us only because of the
       
  1327             // decoration (special case in QGraphicsScenePrivate::dispatchHoverEvent).
       
  1328             if (!acceptsHoverEvents())
       
  1329                 return true;
       
  1330         }
       
  1331         break;
       
  1332     default:
       
  1333         break;
       
  1334     }
       
  1335     return QObject::event(event);
       
  1336 }
       
  1337 
       
  1338 /*!
       
  1339    This event handler can be reimplemented to handle state changes.
       
  1340 
       
  1341    The state being changed in this event can be retrieved through \a event.
       
  1342 
       
  1343    Change events include: QEvent::ActivationChange, QEvent::EnabledChange,
       
  1344    QEvent::FontChange, QEvent::StyleChange, QEvent::PaletteChange,
       
  1345    QEvent::ParentChange, QEvent::LayoutDirectionChange, and
       
  1346    QEvent::ContentsRectChange.
       
  1347 */
       
  1348 void QGraphicsWidget::changeEvent(QEvent *event)
       
  1349 {
       
  1350     Q_D(QGraphicsWidget);
       
  1351     switch (event->type()) {
       
  1352     case QEvent::StyleChange:
       
  1353         // ### Don't unset if the margins are explicitly set.
       
  1354         unsetWindowFrameMargins();
       
  1355     case QEvent::FontChange:
       
  1356         update();
       
  1357         updateGeometry();
       
  1358         break;
       
  1359     case QEvent::PaletteChange:
       
  1360         update();
       
  1361         break;
       
  1362     case QEvent::ParentChange:
       
  1363         d->resolveFont(d->inheritedFontResolveMask);
       
  1364         d->resolvePalette(d->inheritedPaletteResolveMask);
       
  1365         break;
       
  1366     default:
       
  1367         break;
       
  1368     }
       
  1369 }
       
  1370 
       
  1371 /*!
       
  1372     This event handler, for \a event, can be reimplemented in a subclass to
       
  1373     receive widget close events.  The default implementation accepts the
       
  1374     event.
       
  1375 
       
  1376     \sa close(), QCloseEvent
       
  1377 */
       
  1378 void QGraphicsWidget::closeEvent(QCloseEvent *event)
       
  1379 {
       
  1380     event->accept();
       
  1381 }
       
  1382 
       
  1383 /*!
       
  1384     \reimp
       
  1385 */
       
  1386 void QGraphicsWidget::focusInEvent(QFocusEvent *event)
       
  1387 {
       
  1388     Q_UNUSED(event);
       
  1389     if (focusPolicy() != Qt::NoFocus)
       
  1390         update();
       
  1391 }
       
  1392 
       
  1393 /*!
       
  1394     Finds a new widget to give the keyboard focus to, as appropriate for Tab
       
  1395     and Shift+Tab, and returns true if it can find a new widget; returns false
       
  1396     otherwise. If \a next is true, this function searches forward; if \a next
       
  1397     is false, it searches backward.
       
  1398 
       
  1399     Sometimes, you will want to reimplement this function to provide special
       
  1400     focus handling for your widget and its subwidgets. For example, a web
       
  1401     browser might reimplement it to move its current active link forward or
       
  1402     backward, and call the base implementation only when it reaches the last
       
  1403     or first link on the page.
       
  1404 
       
  1405     Child widgets call focusNextPrevChild() on their parent widgets, but only
       
  1406     the window that contains the child widgets decides where to redirect
       
  1407     focus. By reimplementing this function for an object, you gain control of
       
  1408     focus traversal for all child widgets.
       
  1409 
       
  1410     \sa focusPolicy()
       
  1411 */
       
  1412 bool QGraphicsWidget::focusNextPrevChild(bool next)
       
  1413 {
       
  1414     Q_D(QGraphicsWidget);
       
  1415     // Let the parent's focusNextPrevChild implementation decide what to do.
       
  1416     QGraphicsWidget *parent = 0;
       
  1417     if (!isWindow() && (parent = parentWidget()))
       
  1418         return parent->focusNextPrevChild(next);
       
  1419     if (!d->scene)
       
  1420         return false;
       
  1421     if (d->scene->focusNextPrevChild(next))
       
  1422         return true;
       
  1423     if (isWindow()) {
       
  1424         setFocus(next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
       
  1425         if (hasFocus())
       
  1426             return true;
       
  1427     }
       
  1428     return false;
       
  1429 }
       
  1430 
       
  1431 /*!
       
  1432     \reimp
       
  1433 */
       
  1434 void QGraphicsWidget::focusOutEvent(QFocusEvent *event)
       
  1435 {
       
  1436     Q_UNUSED(event);
       
  1437     if (focusPolicy() != Qt::NoFocus)
       
  1438         update();
       
  1439 }
       
  1440 
       
  1441 /*!
       
  1442     This event handler, for \l{QEvent::Hide}{Hide} events, is delivered after
       
  1443     the widget has been hidden, for example, setVisible(false) has been called
       
  1444     for the widget or one of its ancestors when the widget was previously
       
  1445     shown.
       
  1446 
       
  1447     You can reimplement this event handler to detect when your widget is
       
  1448     hidden. Calling QEvent::accept() or QEvent::ignore() on \a event has no
       
  1449     effect.
       
  1450 
       
  1451     \sa showEvent(), QWidget::hideEvent(), ItemVisibleChange
       
  1452 */
       
  1453 void QGraphicsWidget::hideEvent(QHideEvent *event)
       
  1454 {
       
  1455     ///### focusNextPrevChild(true), don't lose focus when the focus widget
       
  1456     // is hidden.
       
  1457     Q_UNUSED(event);
       
  1458 }
       
  1459 
       
  1460 /*!
       
  1461     This event handler, for \l{QEvent::GraphicsSceneMove}{GraphicsSceneMove}
       
  1462     events, is delivered after the widget has moved (e.g., its local position
       
  1463     has changed).
       
  1464 
       
  1465     This event is only delivered when the item is moved locally. Calling
       
  1466     setTransform() or moving any of the item's ancestors does not affect the
       
  1467     item's local position.
       
  1468 
       
  1469     You can reimplement this event handler to detect when your widget has
       
  1470     moved. Calling QEvent::accept() or QEvent::ignore() on \a event has no
       
  1471     effect.
       
  1472 
       
  1473     \sa ItemPositionChange, ItemPositionHasChanged
       
  1474 */
       
  1475 void QGraphicsWidget::moveEvent(QGraphicsSceneMoveEvent *event)
       
  1476 {
       
  1477     // ### Last position is always == current position
       
  1478     Q_UNUSED(event);
       
  1479 }
       
  1480 
       
  1481 /*!
       
  1482     This event is delivered to the item by the scene at some point after it
       
  1483     has been constructed, but before it is shown or otherwise accessed through
       
  1484     the scene. You can use this event handler to do last-minute initializations
       
  1485     of the widget which require the item to be fully constructed.
       
  1486 
       
  1487     The base implementation does nothing.
       
  1488 */
       
  1489 void QGraphicsWidget::polishEvent()
       
  1490 {
       
  1491 }
       
  1492 
       
  1493 /*!
       
  1494     This event handler, for
       
  1495     \l{QEvent::GraphicsSceneResize}{GraphicsSceneResize} events, is
       
  1496     delivered after the widget has been resized (i.e., its local size has
       
  1497     changed). \a event contains both the old and the new size.
       
  1498 
       
  1499     This event is only delivered when the widget is resized locally; calling
       
  1500     setTransform() on the widget or any of its ancestors or view, does not
       
  1501     affect the widget's local size.
       
  1502 
       
  1503     You can reimplement this event handler to detect when your widget has been
       
  1504     resized. Calling QEvent::accept() or QEvent::ignore() on \a event has no
       
  1505     effect.
       
  1506 
       
  1507     \sa geometry(), setGeometry()
       
  1508 */
       
  1509 void QGraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
  1510 {
       
  1511     Q_UNUSED(event);
       
  1512 }
       
  1513 
       
  1514 /*!
       
  1515     This event handler, for \l{QEvent::Show}{Show} events, is delivered before
       
  1516     the widget has been shown, for example, setVisible(true) has been called
       
  1517     for the widget or one of its ancestors when the widget was previously
       
  1518     hidden.
       
  1519 
       
  1520     You can reimplement this event handler to detect when your widget is
       
  1521     shown. Calling QEvent::accept() or QEvent::ignore() on \a event has no
       
  1522     effect.
       
  1523 
       
  1524     \sa hideEvent(), QWidget::showEvent(), ItemVisibleChange
       
  1525 */
       
  1526 void QGraphicsWidget::showEvent(QShowEvent *event)
       
  1527 {
       
  1528     Q_UNUSED(event);
       
  1529 }
       
  1530 
       
  1531 /*!
       
  1532     \reimp
       
  1533 */
       
  1534 void QGraphicsWidget::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
       
  1535 {
       
  1536     Q_UNUSED(event);
       
  1537 }
       
  1538 
       
  1539 /*!
       
  1540     \reimp
       
  1541 */
       
  1542 void QGraphicsWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
       
  1543 {
       
  1544     Q_UNUSED(event);
       
  1545 }
       
  1546 
       
  1547 /*!
       
  1548     This event handler, for \a event, can be reimplemented in a subclass to
       
  1549     receive notifications for Qt::GrabMouse events.
       
  1550 
       
  1551     \sa grabMouse(), grabKeyboard()
       
  1552 */
       
  1553 void QGraphicsWidget::grabMouseEvent(QEvent *event)
       
  1554 {
       
  1555     Q_UNUSED(event);
       
  1556 }
       
  1557 
       
  1558 /*!
       
  1559     This event handler, for \a event, can be reimplemented in a subclass to
       
  1560     receive notifications for Qt::UngrabMouse events.
       
  1561 
       
  1562     \sa ungrabMouse(), ungrabKeyboard()
       
  1563 */
       
  1564 void QGraphicsWidget::ungrabMouseEvent(QEvent *event)
       
  1565 {
       
  1566     Q_UNUSED(event);
       
  1567 }
       
  1568 
       
  1569 /*!
       
  1570     This event handler, for \a event, can be reimplemented in a subclass to
       
  1571     receive notifications for Qt::GrabKeyboard events.
       
  1572 
       
  1573     \sa grabKeyboard(), grabMouse()
       
  1574 */
       
  1575 void QGraphicsWidget::grabKeyboardEvent(QEvent *event)
       
  1576 {
       
  1577     Q_UNUSED(event);
       
  1578 }
       
  1579 
       
  1580 /*!
       
  1581     This event handler, for \a event, can be reimplemented in a subclass to
       
  1582     receive notifications for Qt::UngrabKeyboard events.
       
  1583 
       
  1584     \sa ungrabKeyboard(), ungrabMouse()
       
  1585 */
       
  1586 void QGraphicsWidget::ungrabKeyboardEvent(QEvent *event)
       
  1587 {
       
  1588     Q_UNUSED(event);
       
  1589 }
       
  1590 
       
  1591 /*!
       
  1592     Returns the widgets window type.
       
  1593 
       
  1594     \sa windowFlags(), isWindow(), isPanel()
       
  1595 */
       
  1596 Qt::WindowType QGraphicsWidget::windowType() const
       
  1597 {
       
  1598     return Qt::WindowType(int(windowFlags()) & Qt::WindowType_Mask);
       
  1599 }
       
  1600 
       
  1601 /*!
       
  1602     \property QGraphicsWidget::windowFlags
       
  1603     \brief the widget's window flags
       
  1604 
       
  1605     Window flags are a combination of a window type (e.g., Qt::Dialog) and
       
  1606     several flags giving hints on the behavior of the window. The behavior
       
  1607     is platform-dependent.
       
  1608 
       
  1609     By default, this property contains no window flags.
       
  1610 
       
  1611     Windows are panels. If you set the Qt::Window flag, the ItemIsPanel flag
       
  1612     will be set automatically. If you clear the Qt::Window flag, the
       
  1613     ItemIsPanel flag is also cleared. Note that the ItemIsPanel flag can be
       
  1614     set independently of Qt::Window.
       
  1615 
       
  1616     \sa isWindow(), isPanel()
       
  1617 */
       
  1618 Qt::WindowFlags QGraphicsWidget::windowFlags() const
       
  1619 {
       
  1620     Q_D(const QGraphicsWidget);
       
  1621     return d->windowFlags;
       
  1622 }
       
  1623 void QGraphicsWidget::setWindowFlags(Qt::WindowFlags wFlags)
       
  1624 {
       
  1625     Q_D(QGraphicsWidget);
       
  1626     if (d->windowFlags == wFlags)
       
  1627         return;
       
  1628     bool wasPopup = (d->windowFlags & Qt::WindowType_Mask) == Qt::Popup;
       
  1629 
       
  1630     d->adjustWindowFlags(&wFlags);
       
  1631     d->windowFlags = wFlags;
       
  1632     if (!d->setWindowFrameMargins)
       
  1633         unsetWindowFrameMargins();
       
  1634 
       
  1635     setFlag(ItemIsPanel, d->windowFlags & Qt::Window);
       
  1636 
       
  1637     bool isPopup = (d->windowFlags & Qt::WindowType_Mask) == Qt::Popup;
       
  1638     if (d->scene && isVisible() && wasPopup != isPopup) {
       
  1639         // Popup state changed; update implicit mouse grab.
       
  1640         if (!isPopup)
       
  1641             d->scene->d_func()->removePopup(this);
       
  1642         else
       
  1643             d->scene->d_func()->addPopup(this);
       
  1644     }
       
  1645 
       
  1646     if (d->scene && d->scene->d_func()->allItemsIgnoreHoverEvents && d->hasDecoration()) {
       
  1647         d->scene->d_func()->allItemsIgnoreHoverEvents = false;
       
  1648         d->scene->d_func()->enableMouseTrackingOnViews();
       
  1649     }
       
  1650 }
       
  1651 
       
  1652 /*!
       
  1653     Returns true if this widget's window is in the active window, or if the
       
  1654     widget does not have a window but is in an active scene (i.e., a scene
       
  1655     that currently has focus).
       
  1656 
       
  1657     The active window is the window that either contains a child widget that
       
  1658     currently has input focus, or that itself has input focus.
       
  1659 
       
  1660     \sa QGraphicsScene::activeWindow(), QGraphicsScene::setActiveWindow(), isActive()
       
  1661 */
       
  1662 bool QGraphicsWidget::isActiveWindow() const
       
  1663 {
       
  1664     return isActive();
       
  1665 }
       
  1666 
       
  1667 /*!
       
  1668     \property QGraphicsWidget::windowTitle
       
  1669     \brief This property holds the window title (caption).
       
  1670 
       
  1671     This property is only used for windows.
       
  1672 
       
  1673     By default, if no title has been set, this property contains an
       
  1674     empty string.
       
  1675 */
       
  1676 void QGraphicsWidget::setWindowTitle(const QString &title)
       
  1677 {
       
  1678     Q_D(QGraphicsWidget);
       
  1679     d->ensureWindowData();
       
  1680     d->windowData->windowTitle = title;
       
  1681 }
       
  1682 QString QGraphicsWidget::windowTitle() const
       
  1683 {
       
  1684     Q_D(const QGraphicsWidget);
       
  1685     return d->windowData ? d->windowData->windowTitle : QString();
       
  1686 }
       
  1687 
       
  1688 /*!
       
  1689     \property QGraphicsWidget::focusPolicy
       
  1690     \brief the way the widget accepts keyboard focus
       
  1691 
       
  1692     The focus policy is Qt::TabFocus if the widget accepts keyboard focus by
       
  1693     tabbing, Qt::ClickFocus if the widget accepts focus by clicking,
       
  1694     Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it
       
  1695     does not accept focus at all.
       
  1696 
       
  1697     You must enable keyboard focus for a widget if it processes keyboard
       
  1698     events. This is normally done from the widget's constructor. For instance,
       
  1699     the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus).
       
  1700 
       
  1701     If you enable a focus policy (i.e., not Qt::NoFocus), QGraphicsWidget will
       
  1702     automatically enable the ItemIsFocusable flag.  Setting Qt::NoFocus on a
       
  1703     widget will clear the ItemIsFocusable flag. If the widget currently has
       
  1704     keyboard focus, the widget will automatically lose focus.
       
  1705 
       
  1706     \sa focusInEvent(), focusOutEvent(), keyPressEvent(), keyReleaseEvent(), enabled
       
  1707 */
       
  1708 Qt::FocusPolicy QGraphicsWidget::focusPolicy() const
       
  1709 {
       
  1710     Q_D(const QGraphicsWidget);
       
  1711     return d->focusPolicy;
       
  1712 }
       
  1713 void QGraphicsWidget::setFocusPolicy(Qt::FocusPolicy policy)
       
  1714 {
       
  1715     Q_D(QGraphicsWidget);
       
  1716     if (d->focusPolicy == policy)
       
  1717         return;
       
  1718     d->focusPolicy = policy;
       
  1719     if (hasFocus() && policy == Qt::NoFocus)
       
  1720         clearFocus();
       
  1721     setFlag(ItemIsFocusable, policy != Qt::NoFocus);
       
  1722 }
       
  1723 
       
  1724 /*!
       
  1725     If this widget, a child or descendant of this widget currently has input
       
  1726     focus, this function will return a pointer to that widget. If
       
  1727     no descendant widget has input focus, 0 is returned.
       
  1728 
       
  1729     \sa QGraphicsItem::focusItem(), QWidget::focusWidget()
       
  1730 */
       
  1731 QGraphicsWidget *QGraphicsWidget::focusWidget() const
       
  1732 {
       
  1733     Q_D(const QGraphicsWidget);
       
  1734     if (d->subFocusItem && d->subFocusItem->d_ptr->isWidget)
       
  1735         return static_cast<QGraphicsWidget *>(d->subFocusItem);
       
  1736     return 0;
       
  1737 }
       
  1738 
       
  1739 #ifndef QT_NO_SHORTCUT
       
  1740 /*!
       
  1741     \since 4.5
       
  1742 
       
  1743     Adds a shortcut to Qt's shortcut system that watches for the given key \a
       
  1744     sequence in the given \a context. If the \a context is
       
  1745     Qt::ApplicationShortcut, the shortcut applies to the application as a
       
  1746     whole. Otherwise, it is either local to this widget, Qt::WidgetShortcut,
       
  1747     or to the window itself, Qt::WindowShortcut. For widgets that are not part
       
  1748     of a window (i.e., top-level widgets and their children),
       
  1749     Qt::WindowShortcut shortcuts apply to the scene.
       
  1750 
       
  1751     If the same key \a sequence has been grabbed by several widgets,
       
  1752     when the key \a sequence occurs a QEvent::Shortcut event is sent
       
  1753     to all the widgets to which it applies in a non-deterministic
       
  1754     order, but with the ``ambiguous'' flag set to true.
       
  1755 
       
  1756     \warning You should not normally need to use this function;
       
  1757     instead create \l{QAction}s with the shortcut key sequences you
       
  1758     require (if you also want equivalent menu options and toolbar
       
  1759     buttons), or create \l{QShortcut}s if you just need key sequences.
       
  1760     Both QAction and QShortcut handle all the event filtering for you,
       
  1761     and provide signals which are triggered when the user triggers the
       
  1762     key sequence, so are much easier to use than this low-level
       
  1763     function.
       
  1764 
       
  1765     \sa releaseShortcut() setShortcutEnabled() QWidget::grabShortcut()
       
  1766 */
       
  1767 int QGraphicsWidget::grabShortcut(const QKeySequence &sequence, Qt::ShortcutContext context)
       
  1768 {
       
  1769     Q_ASSERT(qApp);
       
  1770     if (sequence.isEmpty())
       
  1771         return 0;
       
  1772     // ### setAttribute(Qt::WA_GrabbedShortcut);
       
  1773     return qApp->d_func()->shortcutMap.addShortcut(this, sequence, context);
       
  1774 }
       
  1775 
       
  1776 /*!
       
  1777     \since 4.5
       
  1778 
       
  1779     Removes the shortcut with the given \a id from Qt's shortcut
       
  1780     system. The widget will no longer receive QEvent::Shortcut events
       
  1781     for the shortcut's key sequence (unless it has other shortcuts
       
  1782     with the same key sequence).
       
  1783 
       
  1784     \warning You should not normally need to use this function since
       
  1785     Qt's shortcut system removes shortcuts automatically when their
       
  1786     parent widget is destroyed. It is best to use QAction or
       
  1787     QShortcut to handle shortcuts, since they are easier to use than
       
  1788     this low-level function. Note also that this is an expensive
       
  1789     operation.
       
  1790 
       
  1791     \sa grabShortcut() setShortcutEnabled() , QWidget::releaseShortcut()
       
  1792 */
       
  1793 void QGraphicsWidget::releaseShortcut(int id)
       
  1794 {
       
  1795     Q_ASSERT(qApp);
       
  1796     if (id)
       
  1797         qApp->d_func()->shortcutMap.removeShortcut(id, this, 0);
       
  1798 }
       
  1799 
       
  1800 /*!
       
  1801     \since 4.5
       
  1802 
       
  1803     If \a enabled is true, the shortcut with the given \a id is
       
  1804     enabled; otherwise the shortcut is disabled.
       
  1805 
       
  1806     \warning You should not normally need to use this function since
       
  1807     Qt's shortcut system enables/disables shortcuts automatically as
       
  1808     widgets become hidden/visible and gain or lose focus. It is best
       
  1809     to use QAction or QShortcut to handle shortcuts, since they are
       
  1810     easier to use than this low-level function.
       
  1811 
       
  1812     \sa grabShortcut() releaseShortcut(), QWidget::setShortcutEnabled()
       
  1813 */
       
  1814 void QGraphicsWidget::setShortcutEnabled(int id, bool enabled)
       
  1815 {
       
  1816     Q_ASSERT(qApp);
       
  1817     if (id)
       
  1818         qApp->d_func()->shortcutMap.setShortcutEnabled(enabled, id, this, 0);
       
  1819 }
       
  1820 
       
  1821 /*!
       
  1822     \since 4.5
       
  1823 
       
  1824     If \a enabled is true, auto repeat of the shortcut with the
       
  1825     given \a id is enabled; otherwise it is disabled.
       
  1826 
       
  1827     \sa grabShortcut() releaseShortcut() QWidget::setShortcutAutoRepeat()
       
  1828 */
       
  1829 void QGraphicsWidget::setShortcutAutoRepeat(int id, bool enabled)
       
  1830 {
       
  1831     Q_ASSERT(qApp);
       
  1832     if (id)
       
  1833         qApp->d_func()->shortcutMap.setShortcutAutoRepeat(enabled, id, this, 0);
       
  1834 }
       
  1835 #endif
       
  1836 
       
  1837 #ifndef QT_NO_ACTION
       
  1838 /*!
       
  1839     \since 4.5
       
  1840 
       
  1841     Appends the action \a action to this widget's list of actions.
       
  1842 
       
  1843     All QGraphicsWidgets have a list of \l{QAction}s, however they can be
       
  1844     represented graphically in many different ways. The default use of the
       
  1845     QAction list (as returned by actions()) is to create a context QMenu.
       
  1846 
       
  1847     A QGraphicsWidget should only have one of each action and adding an action
       
  1848     it already has will not cause the same action to be in the widget twice.
       
  1849 
       
  1850     \sa removeAction(), insertAction(), actions(), QWidget::addAction()
       
  1851 */
       
  1852 void QGraphicsWidget::addAction(QAction *action)
       
  1853 {
       
  1854     insertAction(0, action);
       
  1855 }
       
  1856 
       
  1857 /*!
       
  1858     \since 4.5
       
  1859 
       
  1860     Appends the actions \a actions to this widget's list of actions.
       
  1861 
       
  1862     \sa removeAction(), QMenu, addAction(), QWidget::addActions()
       
  1863 */
       
  1864 void QGraphicsWidget::addActions(QList<QAction *> actions)
       
  1865 {
       
  1866     for (int i = 0; i < actions.count(); ++i)
       
  1867         insertAction(0, actions.at(i));
       
  1868 }
       
  1869 
       
  1870 /*!
       
  1871     \since 4.5
       
  1872 
       
  1873     Inserts the action \a action to this widget's list of actions,
       
  1874     before the action \a before. It appends the action if \a before is 0 or
       
  1875     \a before is not a valid action for this widget.
       
  1876 
       
  1877     A QGraphicsWidget should only have one of each action.
       
  1878 
       
  1879     \sa removeAction(), addAction(), QMenu, actions(),
       
  1880     QWidget::insertActions()
       
  1881 */
       
  1882 void QGraphicsWidget::insertAction(QAction *before, QAction *action)
       
  1883 {
       
  1884     if (!action) {
       
  1885         qWarning("QWidget::insertAction: Attempt to insert null action");
       
  1886         return;
       
  1887     }
       
  1888 
       
  1889     Q_D(QGraphicsWidget);
       
  1890     int index = d->actions.indexOf(action);
       
  1891     if (index != -1)
       
  1892         d->actions.removeAt(index);
       
  1893 
       
  1894     int pos = d->actions.indexOf(before);
       
  1895     if (pos < 0) {
       
  1896         before = 0;
       
  1897         pos = d->actions.size();
       
  1898     }
       
  1899     d->actions.insert(pos, action);
       
  1900 
       
  1901     if (index == -1) {
       
  1902         QActionPrivate *apriv = action->d_func();
       
  1903         apriv->graphicsWidgets.append(this);
       
  1904     }
       
  1905 
       
  1906     QActionEvent e(QEvent::ActionAdded, action, before);
       
  1907     QApplication::sendEvent(this, &e);
       
  1908 }
       
  1909 
       
  1910 /*!
       
  1911     \since 4.5
       
  1912 
       
  1913     Inserts the actions \a actions to this widget's list of actions,
       
  1914     before the action \a before. It appends the action if \a before is 0 or
       
  1915     \a before is not a valid action for this widget.
       
  1916 
       
  1917     A QGraphicsWidget can have at most one of each action.
       
  1918 
       
  1919     \sa removeAction(), QMenu, insertAction(), QWidget::insertActions()
       
  1920 */
       
  1921 void QGraphicsWidget::insertActions(QAction *before, QList<QAction *> actions)
       
  1922 {
       
  1923     for (int i = 0; i < actions.count(); ++i)
       
  1924         insertAction(before, actions.at(i));
       
  1925 }
       
  1926 
       
  1927 /*!
       
  1928     \since 4.5
       
  1929 
       
  1930     Removes the action \a action from this widget's list of actions.
       
  1931 
       
  1932     \sa insertAction(), actions(), insertAction(), QWidget::removeAction()
       
  1933 */
       
  1934 void QGraphicsWidget::removeAction(QAction *action)
       
  1935 {
       
  1936     if (!action)
       
  1937         return;
       
  1938 
       
  1939     Q_D(QGraphicsWidget);
       
  1940 
       
  1941     QActionPrivate *apriv = action->d_func();
       
  1942     apriv->graphicsWidgets.removeAll(this);
       
  1943 
       
  1944     if (d->actions.removeAll(action)) {
       
  1945         QActionEvent e(QEvent::ActionRemoved, action);
       
  1946         QApplication::sendEvent(this, &e);
       
  1947     }
       
  1948 }
       
  1949 
       
  1950 /*!
       
  1951     \since 4.5
       
  1952 
       
  1953     Returns the (possibly empty) list of this widget's actions.
       
  1954 
       
  1955     \sa insertAction(), removeAction(), QWidget::actions(),
       
  1956     QAction::associatedWidgets(), QAction::associatedGraphicsWidgets()
       
  1957 */
       
  1958 QList<QAction *> QGraphicsWidget::actions() const
       
  1959 {
       
  1960     Q_D(const QGraphicsWidget);
       
  1961     return d->actions;
       
  1962 }
       
  1963 #endif
       
  1964 
       
  1965 /*!
       
  1966     Moves the \a second widget around the ring of focus widgets so that
       
  1967     keyboard focus moves from the \a first widget to the \a second widget when
       
  1968     the Tab key is pressed.
       
  1969 
       
  1970     Note that since the tab order of the \a second widget is changed, you
       
  1971     should order a chain like this:
       
  1972 
       
  1973     \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicswidget.cpp 1
       
  1974 
       
  1975     \e not like this:
       
  1976 
       
  1977     \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicswidget.cpp 2
       
  1978 
       
  1979     If \a first is 0, this indicates that \a second should be the first widget
       
  1980     to receive input focus should the scene gain Tab focus (i.e., the user
       
  1981     hits Tab so that focus passes into the scene). If \a second is 0, this
       
  1982     indicates that \a first should be the first widget to gain focus if the
       
  1983     scene gained BackTab focus.
       
  1984 
       
  1985     By default, tab order is defined implicitly using widget creation order.
       
  1986 
       
  1987     \sa focusPolicy, {Keyboard Focus}
       
  1988 */
       
  1989 void QGraphicsWidget::setTabOrder(QGraphicsWidget *first, QGraphicsWidget *second)
       
  1990 {
       
  1991     if (!first && !second) {
       
  1992         qWarning("QGraphicsWidget::setTabOrder(0, 0) is undefined");
       
  1993         return;
       
  1994     }
       
  1995     if ((first && second) && first->scene() != second->scene()) {
       
  1996         qWarning("QGraphicsWidget::setTabOrder: scenes %p and %p are different",
       
  1997                  first->scene(), second->scene());
       
  1998         return;
       
  1999     }
       
  2000     QGraphicsScene *scene = first ? first->scene() : second->scene();
       
  2001     if (!scene && (!first || !second)) {
       
  2002         qWarning("QGraphicsWidget::setTabOrder: assigning tab order from/to the"
       
  2003                  " scene requires the item to be in a scene.");
       
  2004         return;
       
  2005     }
       
  2006 
       
  2007     // If either first or second are 0, the scene's tabFocusFirst is updated
       
  2008     // to point to the first item in the scene's focus chain. Then first or
       
  2009     // second are set to point to tabFocusFirst.
       
  2010     QGraphicsScenePrivate *sceneD = scene->d_func();
       
  2011     if (!first) {
       
  2012         sceneD->tabFocusFirst = second;
       
  2013         return;
       
  2014     }
       
  2015     if (!second) {
       
  2016         sceneD->tabFocusFirst = first->d_func()->focusNext;
       
  2017         return;
       
  2018     }
       
  2019 
       
  2020     // Both first and second are != 0.
       
  2021     QGraphicsWidget *firstFocusNext = first->d_func()->focusNext;
       
  2022     if (firstFocusNext == second) {
       
  2023         // Nothing to do.
       
  2024         return;
       
  2025     }
       
  2026 
       
  2027     // Update the focus chain.
       
  2028     QGraphicsWidget *secondFocusPrev = second->d_func()->focusPrev;
       
  2029     QGraphicsWidget *secondFocusNext = second->d_func()->focusNext;
       
  2030     firstFocusNext->d_func()->focusPrev = second;
       
  2031     first->d_func()->focusNext = second;
       
  2032     second->d_func()->focusNext = firstFocusNext;
       
  2033     second->d_func()->focusPrev = first;
       
  2034     secondFocusPrev->d_func()->focusNext = secondFocusNext;
       
  2035     secondFocusNext->d_func()->focusPrev = secondFocusPrev;
       
  2036 
       
  2037     Q_ASSERT(first->d_func()->focusNext->d_func()->focusPrev == first);
       
  2038     Q_ASSERT(first->d_func()->focusPrev->d_func()->focusNext == first);
       
  2039 
       
  2040     Q_ASSERT(second->d_func()->focusNext->d_func()->focusPrev == second);
       
  2041     Q_ASSERT(second->d_func()->focusPrev->d_func()->focusNext == second);
       
  2042 
       
  2043 }
       
  2044 
       
  2045 /*!
       
  2046     If \a on is true, this function enables \a attribute; otherwise
       
  2047     \a attribute is disabled.
       
  2048 
       
  2049     See the class documentation for QGraphicsWidget for a complete list of
       
  2050     which attributes are supported, and what they are for.
       
  2051 
       
  2052     \sa testAttribute(), QWidget::setAttribute()
       
  2053 */
       
  2054 void QGraphicsWidget::setAttribute(Qt::WidgetAttribute attribute, bool on)
       
  2055 {
       
  2056     Q_D(QGraphicsWidget);
       
  2057     // ### most flags require some immediate action
       
  2058     // ### we might want to qWarn use of unsupported attributes
       
  2059     // ### we might want to not use Qt::WidgetAttribute, but roll our own instead
       
  2060     d->setAttribute(attribute, on);
       
  2061 }
       
  2062 
       
  2063 /*!
       
  2064     Returns true if \a attribute is enabled for this widget; otherwise,
       
  2065     returns false.
       
  2066 
       
  2067     \sa setAttribute()
       
  2068 */
       
  2069 bool QGraphicsWidget::testAttribute(Qt::WidgetAttribute attribute) const
       
  2070 {
       
  2071     Q_D(const QGraphicsWidget);
       
  2072     return d->testAttribute(attribute);
       
  2073 }
       
  2074 
       
  2075 /*!
       
  2076     \reimp
       
  2077 */
       
  2078 int QGraphicsWidget::type() const
       
  2079 {
       
  2080     return Type;
       
  2081 }
       
  2082 
       
  2083 /*!
       
  2084     \reimp
       
  2085 */
       
  2086 void QGraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
  2087 {
       
  2088     Q_UNUSED(painter);
       
  2089     Q_UNUSED(option);
       
  2090     Q_UNUSED(widget);
       
  2091 }
       
  2092 
       
  2093 /*!
       
  2094     This virtual function is called by QGraphicsScene to draw the window frame
       
  2095     for windows using \a painter, \a option, and \a widget, in local
       
  2096     coordinates. The base implementation uses the current style to render the
       
  2097     frame and title bar.
       
  2098 
       
  2099     You can reimplement this function in a subclass of QGraphicsWidget to
       
  2100     provide custom rendering of the widget's window frame.
       
  2101 
       
  2102     \sa QGraphicsItem::paint()
       
  2103 */
       
  2104 void QGraphicsWidget::paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,
       
  2105                                        QWidget *widget)
       
  2106 {
       
  2107     const bool fillBackground = !testAttribute(Qt::WA_OpaquePaintEvent)
       
  2108                                 && !testAttribute(Qt::WA_NoSystemBackground);
       
  2109     QGraphicsProxyWidget *proxy = qobject_cast<QGraphicsProxyWidget *>(this);
       
  2110     const bool embeddedWidgetFillsOwnBackground = proxy && proxy->widget();
       
  2111 
       
  2112     if (rect().contains(option->exposedRect)) {
       
  2113         if (fillBackground && !embeddedWidgetFillsOwnBackground)
       
  2114             painter->fillRect(option->exposedRect, palette().window());
       
  2115         return;
       
  2116     }
       
  2117 
       
  2118     Q_D(QGraphicsWidget);
       
  2119 
       
  2120     QRect windowFrameRect = QRect(QPoint(), windowFrameGeometry().size().toSize());
       
  2121     QStyleOptionTitleBar bar;
       
  2122     bar.QStyleOption::operator=(*option);
       
  2123     d->initStyleOptionTitleBar(&bar);   // this clear flags in bar.state
       
  2124     d->ensureWindowData();
       
  2125     if (d->windowData->buttonMouseOver)
       
  2126         bar.state |= QStyle::State_MouseOver;
       
  2127     else
       
  2128         bar.state &= ~QStyle::State_MouseOver;
       
  2129     if (d->windowData->buttonSunken)
       
  2130         bar.state |= QStyle::State_Sunken;
       
  2131     else
       
  2132         bar.state &= ~QStyle::State_Sunken;
       
  2133 
       
  2134     bar.rect = windowFrameRect;
       
  2135 
       
  2136     // translate painter to make the style happy
       
  2137     const QPointF styleOrigin = this->windowFrameRect().topLeft();
       
  2138     painter->translate(styleOrigin);
       
  2139 
       
  2140 #ifdef Q_WS_MAC
       
  2141     const QSize pixmapSize = windowFrameRect.size();
       
  2142     if (pixmapSize.width() <= 0 || pixmapSize.height() <= 0)
       
  2143         return;
       
  2144     QPainter *realPainter = painter;
       
  2145     QPixmap pm(pixmapSize);
       
  2146     painter = new QPainter(&pm);
       
  2147 #endif
       
  2148 
       
  2149     // Fill background
       
  2150     QStyleHintReturnMask mask;
       
  2151     bool setMask = style()->styleHint(QStyle::SH_WindowFrame_Mask, &bar, widget, &mask) && !mask.region.isEmpty();
       
  2152     bool hasBorder = !style()->styleHint(QStyle::SH_TitleBar_NoBorder, &bar, widget);
       
  2153     int frameWidth = style()->pixelMetric(QStyle::PM_MDIFrameWidth, &bar, widget);
       
  2154     if (setMask) {
       
  2155         painter->save();
       
  2156         painter->setClipRegion(mask.region, Qt::IntersectClip);
       
  2157     }
       
  2158     if (fillBackground) {
       
  2159         if (embeddedWidgetFillsOwnBackground) {
       
  2160             // Don't fill the background twice.
       
  2161             QPainterPath windowFrameBackground;
       
  2162             windowFrameBackground.addRect(windowFrameRect);
       
  2163             // Adjust with 0.5 to avoid border artifacts between
       
  2164             // widget background and frame background.
       
  2165             windowFrameBackground.addRect(rect().translated(-styleOrigin).adjusted(0.5, 0.5, -0.5, -0.5));
       
  2166             painter->fillPath(windowFrameBackground, palette().window());
       
  2167         } else {
       
  2168             painter->fillRect(windowFrameRect, palette().window());
       
  2169         }
       
  2170     }
       
  2171     painter->setRenderHint(QPainter::NonCosmeticDefaultPen);
       
  2172 
       
  2173     // Draw title
       
  2174     int height = (int)d->titleBarHeight(bar);
       
  2175     bar.rect.setHeight(height);
       
  2176     if (hasBorder) // Frame is painted by PE_FrameWindow
       
  2177         bar.rect.adjust(frameWidth, frameWidth, -frameWidth, 0);
       
  2178 
       
  2179     painter->save();
       
  2180     painter->setFont(QApplication::font("QWorkspaceTitleBar"));
       
  2181     style()->drawComplexControl(QStyle::CC_TitleBar, &bar, painter, widget);
       
  2182     painter->restore();
       
  2183     if (setMask)
       
  2184         painter->restore();
       
  2185     // Draw window frame
       
  2186     QStyleOptionFrame frameOptions;
       
  2187     frameOptions.QStyleOption::operator=(*option);
       
  2188     initStyleOption(&frameOptions);
       
  2189     if (!hasBorder)
       
  2190         painter->setClipRect(windowFrameRect.adjusted(0, +height, 0, 0), Qt::IntersectClip);
       
  2191     if (hasFocus()) {
       
  2192         frameOptions.state |= QStyle::State_HasFocus;
       
  2193     } else {
       
  2194         frameOptions.state &= ~QStyle::State_HasFocus;
       
  2195     }
       
  2196     bool isActive = isActiveWindow();
       
  2197     if (isActive) {
       
  2198         frameOptions.state |= QStyle::State_Active;
       
  2199     } else {
       
  2200         frameOptions.state &= ~QStyle::State_Active;
       
  2201     }
       
  2202 
       
  2203     frameOptions.palette.setCurrentColorGroup(isActive ? QPalette::Active : QPalette::Normal);
       
  2204     frameOptions.rect = windowFrameRect;
       
  2205     frameOptions.lineWidth = style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, 0, widget);
       
  2206     frameOptions.midLineWidth = 1;
       
  2207     style()->drawPrimitive(QStyle::PE_FrameWindow, &frameOptions, painter, widget);
       
  2208 
       
  2209 #ifdef Q_WS_MAC
       
  2210     realPainter->drawPixmap(QPoint(), pm);
       
  2211     delete painter;
       
  2212 #endif
       
  2213 }
       
  2214 
       
  2215 /*!
       
  2216     \reimp
       
  2217 */
       
  2218 QRectF QGraphicsWidget::boundingRect() const
       
  2219 {
       
  2220     return windowFrameRect();
       
  2221 }
       
  2222 
       
  2223 /*!
       
  2224     \reimp
       
  2225 */
       
  2226 QPainterPath QGraphicsWidget::shape() const
       
  2227 {
       
  2228     QPainterPath path;
       
  2229     path.addRect(rect());
       
  2230     return path;
       
  2231 }
       
  2232 
       
  2233 /*!
       
  2234     Call this function to close the widget.
       
  2235 
       
  2236     Returns true if the widget was closed; otherwise returns false.
       
  2237     This slot will first send a QCloseEvent to the widget, which may or may
       
  2238     not accept the event. If the event was ignored, nothing happens. If the
       
  2239     event was accepted, it will hide() the widget.
       
  2240 
       
  2241     If the widget has the Qt::WA_DeleteOnClose attribute set it will be
       
  2242     deleted.
       
  2243 */
       
  2244 bool QGraphicsWidget::close()
       
  2245 {
       
  2246     QCloseEvent closeEvent;
       
  2247     QApplication::sendEvent(this, &closeEvent);
       
  2248     if (!closeEvent.isAccepted()) {
       
  2249         return false;
       
  2250     }
       
  2251     // hide
       
  2252     if (isVisible()) {
       
  2253         hide();
       
  2254     }
       
  2255     if (testAttribute(Qt::WA_DeleteOnClose)) {
       
  2256         deleteLater();
       
  2257     }
       
  2258     return true;
       
  2259 }
       
  2260 
       
  2261 #ifdef Q_NO_USING_KEYWORD
       
  2262 /*!
       
  2263     \fn const QObjectList &QGraphicsWidget::children() const
       
  2264     \internal
       
  2265 
       
  2266     This function returns the same value as QObject::children(). It's
       
  2267     provided to differentiate between the obsolete member
       
  2268     QGraphicsItem::children() and QObject::children(). QGraphicsItem now
       
  2269     provides childItems() instead.
       
  2270 */
       
  2271 #endif
       
  2272 
       
  2273 #if 0
       
  2274 void QGraphicsWidget::dumpFocusChain()
       
  2275 {
       
  2276     qDebug() << "=========== Dumping focus chain ==============";
       
  2277     int i = 0;
       
  2278     QGraphicsWidget *next = this;
       
  2279     QSet<QGraphicsWidget*> visited;
       
  2280     do {
       
  2281         if (!next) {
       
  2282             qWarning("Found a focus chain that is not circular, (next == 0)");
       
  2283             break;
       
  2284         }
       
  2285         qDebug() << i++ << QString::number(uint(next), 16) << next->className() << next->data(0) << QString::fromAscii("focusItem:%1").arg(next->hasFocus() ? '1' : '0') << QLatin1String("next:") << next->d_func()->focusNext->data(0) << QLatin1String("prev:") << next->d_func()->focusPrev->data(0);
       
  2286         if (visited.contains(next)) {
       
  2287             qWarning("Already visited this node. However, I expected to dump until I found myself.");
       
  2288             break;
       
  2289         }
       
  2290         visited << next;
       
  2291         next = next->d_func()->focusNext;
       
  2292     } while (next != this);
       
  2293 }
       
  2294 #endif
       
  2295 
       
  2296 QT_END_NAMESPACE
       
  2297         
       
  2298 #endif //QT_NO_GRAPHICSVIEW