util/src/gui/kernel/qapplication.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 "qplatformdefs.h"
       
    43 #include "qabstracteventdispatcher.h"
       
    44 #include "qaccessible.h"
       
    45 #include "qapplication.h"
       
    46 #include "qclipboard.h"
       
    47 #include "qcursor.h"
       
    48 #include "qdesktopwidget.h"
       
    49 #include "qdir.h"
       
    50 #include "qevent.h"
       
    51 #include "qfile.h"
       
    52 #include "qfileinfo.h"
       
    53 #include "qgraphicsscene.h"
       
    54 #include "qhash.h"
       
    55 #include "qset.h"
       
    56 #include "qlayout.h"
       
    57 #include "qsessionmanager.h"
       
    58 #include "qstyle.h"
       
    59 #include "qstylefactory.h"
       
    60 #include "qtextcodec.h"
       
    61 #include "qtranslator.h"
       
    62 #include "qvariant.h"
       
    63 #include "qwidget.h"
       
    64 #include "qdnd_p.h"
       
    65 #include "qcolormap.h"
       
    66 #include "qdebug.h"
       
    67 #include "private/qgraphicssystemfactory_p.h"
       
    68 #include "private/qstylesheetstyle_p.h"
       
    69 #include "private/qstyle_p.h"
       
    70 #include "qmessagebox.h"
       
    71 #include <QtGui/qgraphicsproxywidget.h>
       
    72 
       
    73 #include "qinputcontext.h"
       
    74 #include "qkeymapper_p.h"
       
    75 
       
    76 #ifdef Q_WS_X11
       
    77 #include <private/qt_x11_p.h>
       
    78 #endif
       
    79 
       
    80 #if defined(Q_WS_X11) || defined(Q_WS_S60)
       
    81 #include "qinputcontextfactory.h"
       
    82 #endif
       
    83 
       
    84 #include "qguiplatformplugin_p.h"
       
    85 
       
    86 #include <qthread.h>
       
    87 #include <private/qthread_p.h>
       
    88 
       
    89 #include <private/qfont_p.h>
       
    90 
       
    91 #include <stdlib.h>
       
    92 
       
    93 #include "qapplication_p.h"
       
    94 #include "qevent_p.h"
       
    95 #include "qwidget_p.h"
       
    96 
       
    97 #include "qapplication.h"
       
    98 
       
    99 #include "qgesture.h"
       
   100 #include "private/qgesturemanager_p.h"
       
   101 
       
   102 #ifndef QT_NO_LIBRARY
       
   103 #include "qlibrary.h"
       
   104 #endif
       
   105 
       
   106 #ifdef Q_WS_WINCE
       
   107 #include "qdatetime.h"
       
   108 #include "qguifunctions_wince.h"
       
   109 extern bool qt_wince_is_smartphone(); //qguifunctions_wince.cpp
       
   110 extern bool qt_wince_is_mobile();     //qguifunctions_wince.cpp
       
   111 extern bool qt_wince_is_pocket_pc();  //qguifunctions_wince.cpp
       
   112 #endif
       
   113 
       
   114 #include "qdatetime.h"
       
   115 
       
   116 #ifdef QT_MAC_USE_COCOA
       
   117 #include <private/qt_cocoa_helpers_mac_p.h>
       
   118 #endif
       
   119 
       
   120 //#define ALIEN_DEBUG
       
   121 
       
   122 static void initResources()
       
   123 {
       
   124 #if defined(Q_WS_WINCE)
       
   125     Q_INIT_RESOURCE(qstyle_wince);
       
   126 #elif defined(Q_OS_SYMBIAN)
       
   127     Q_INIT_RESOURCE(qstyle_s60);
       
   128 #else
       
   129     Q_INIT_RESOURCE(qstyle);
       
   130 #endif
       
   131 
       
   132     Q_INIT_RESOURCE(qmessagebox);
       
   133 #if !defined(QT_NO_PRINTDIALOG)
       
   134     Q_INIT_RESOURCE(qprintdialog);
       
   135 #endif
       
   136 
       
   137 }
       
   138 
       
   139 QT_BEGIN_NAMESPACE
       
   140 
       
   141 extern void qt_call_post_routines();
       
   142 
       
   143 int QApplicationPrivate::app_compile_version = 0x040000; //we don't know exactly, but it's at least 4.0.0
       
   144 
       
   145 QApplication::Type qt_appType=QApplication::Tty;
       
   146 QApplicationPrivate *QApplicationPrivate::self = 0;
       
   147 
       
   148 QInputContext *QApplicationPrivate::inputContext = 0;
       
   149 
       
   150 bool QApplicationPrivate::quitOnLastWindowClosed = true;
       
   151 
       
   152 #ifdef Q_WS_WINCE
       
   153 int QApplicationPrivate::autoMaximizeThreshold = -1;
       
   154 bool QApplicationPrivate::autoSipEnabled = false;
       
   155 #else
       
   156 bool QApplicationPrivate::autoSipEnabled = true;
       
   157 #endif
       
   158 
       
   159 QApplicationPrivate::QApplicationPrivate(int &argc, char **argv, QApplication::Type type)
       
   160     : QCoreApplicationPrivate(argc, argv)
       
   161 {
       
   162     application_type = type;
       
   163     qt_appType = type;
       
   164 
       
   165 #ifndef QT_NO_SESSIONMANAGER
       
   166     is_session_restored = false;
       
   167 #endif
       
   168 
       
   169     quitOnLastWindowClosed = true;
       
   170 
       
   171 #ifdef QT3_SUPPORT
       
   172     qt_compat_used = 0;
       
   173     qt_compat_resolved = 0;
       
   174     qt_tryAccelEvent = 0;
       
   175     qt_tryComposeUnicode = 0;
       
   176     qt_dispatchAccelEvent = 0;
       
   177 #endif
       
   178 #if defined(Q_WS_QWS) && !defined(QT_NO_DIRECTPAINTER)
       
   179     directPainters = 0;
       
   180 #endif
       
   181 
       
   182     gestureManager = 0;
       
   183     gestureWidget = 0;
       
   184 
       
   185     if (!self)
       
   186         self = this;
       
   187 }
       
   188 
       
   189 QApplicationPrivate::~QApplicationPrivate()
       
   190 {
       
   191     if (self == this)
       
   192         self = 0;
       
   193 }
       
   194 
       
   195 /*!
       
   196     \class QApplication
       
   197     \brief The QApplication class manages the GUI application's control
       
   198     flow and main settings.
       
   199 
       
   200     QApplication contains the main event loop, where all events from the window
       
   201     system and other sources are processed and dispatched. It also handles the
       
   202     application's initialization, finalization, and provides session
       
   203     management. In addition, QApplication handles most of the system-wide and
       
   204     application-wide settings.
       
   205 
       
   206     For any GUI application using Qt, there is precisely \bold one QApplication
       
   207     object, no matter whether the application has 0, 1, 2 or more windows at
       
   208     any given time. For non-GUI Qt applications, use QCoreApplication instead,
       
   209     as it does not depend on the \l QtGui library.
       
   210 
       
   211     The QApplication object is accessible through the instance() function that
       
   212     returns a pointer equivalent to the global qApp pointer.
       
   213 
       
   214     QApplication's main areas of responsibility are:
       
   215         \list
       
   216             \o  It initializes the application with the user's desktop settings
       
   217                 such as palette(), font() and doubleClickInterval(). It keeps
       
   218                 track of these properties in case the user changes the desktop
       
   219                 globally, for example through some kind of control panel.
       
   220 
       
   221             \o  It performs event handling, meaning that it receives events
       
   222                 from the underlying window system and dispatches them to the
       
   223                 relevant widgets. By using sendEvent() and postEvent() you can
       
   224                 send your own events to widgets.
       
   225 
       
   226             \o  It parses common command line arguments and sets its internal
       
   227                 state accordingly. See the \l{QApplication::QApplication()}
       
   228                 {constructor documentation} below for more details.
       
   229 
       
   230             \o  It defines the application's look and feel, which is
       
   231                 encapsulated in a QStyle object. This can be changed at runtime
       
   232                 with setStyle().
       
   233 
       
   234             \o  It specifies how the application is to allocate colors. See
       
   235                 setColorSpec() for details.
       
   236 
       
   237             \o  It provides localization of strings that are visible to the
       
   238                 user via translate().
       
   239 
       
   240             \o  It provides some magical objects like the desktop() and the
       
   241                 clipboard().
       
   242 
       
   243             \o  It knows about the application's windows. You can ask which
       
   244                 widget is at a certain position using widgetAt(), get a list of
       
   245                 topLevelWidgets() and closeAllWindows(), etc.
       
   246 
       
   247             \o  It manages the application's mouse cursor handling, see
       
   248                 setOverrideCursor()
       
   249 
       
   250             \o  On the X window system, it provides functions to flush and sync
       
   251                 the communication stream, see flushX() and syncX().
       
   252 
       
   253             \o  It provides support for sophisticated \l{Session Management}
       
   254                 {session management}. This makes it possible for applications
       
   255                 to terminate gracefully when the user logs out, to cancel a
       
   256                 shutdown process if termination isn't possible and even to
       
   257                 preserve the entire application's state for a future session.
       
   258                 See isSessionRestored(), sessionId() and commitData() and
       
   259                 saveState() for details.
       
   260         \endlist
       
   261 
       
   262     Since the QApplication object does so much initialization, it \e{must} be
       
   263     created before any other objects related to the user interface are created.
       
   264     QApplication also deals with common command line arguments. Hence, it is
       
   265     usually a good idea to create it \e before any interpretation or
       
   266     modification of \c argv is done in the application itself.
       
   267 
       
   268     \table
       
   269     \header
       
   270         \o{2,1} Groups of functions
       
   271 
       
   272         \row
       
   273         \o  System settings
       
   274         \o  desktopSettingsAware(),
       
   275             setDesktopSettingsAware(),
       
   276             cursorFlashTime(),
       
   277             setCursorFlashTime(),
       
   278             doubleClickInterval(),
       
   279             setDoubleClickInterval(),
       
   280             setKeyboardInputInterval(),
       
   281             wheelScrollLines(),
       
   282             setWheelScrollLines(),
       
   283             palette(),
       
   284             setPalette(),
       
   285             font(),
       
   286             setFont(),
       
   287             fontMetrics().
       
   288 
       
   289         \row
       
   290         \o  Event handling
       
   291         \o  exec(),
       
   292             processEvents(),
       
   293             exit(),
       
   294             quit().
       
   295             sendEvent(),
       
   296             postEvent(),
       
   297             sendPostedEvents(),
       
   298             removePostedEvents(),
       
   299             hasPendingEvents(),
       
   300             notify(),
       
   301             macEventFilter(),
       
   302             qwsEventFilter(),
       
   303             x11EventFilter(),
       
   304             x11ProcessEvent(),
       
   305             winEventFilter().
       
   306 
       
   307         \row
       
   308         \o  GUI Styles
       
   309         \o  style(),
       
   310             setStyle().
       
   311 
       
   312         \row
       
   313         \o  Color usage
       
   314         \o  colorSpec(),
       
   315             setColorSpec(),
       
   316             qwsSetCustomColors().
       
   317 
       
   318         \row
       
   319         \o  Text handling
       
   320         \o  installTranslator(),
       
   321             removeTranslator()
       
   322             translate().
       
   323 
       
   324         \row
       
   325         \o  Widgets
       
   326         \o  allWidgets(),
       
   327             topLevelWidgets(),
       
   328             desktop(),
       
   329             activePopupWidget(),
       
   330             activeModalWidget(),
       
   331             clipboard(),
       
   332             focusWidget(),
       
   333             activeWindow(),
       
   334             widgetAt().
       
   335 
       
   336         \row
       
   337         \o  Advanced cursor handling
       
   338         \o  overrideCursor(),
       
   339             setOverrideCursor(),
       
   340             restoreOverrideCursor().
       
   341 
       
   342         \row
       
   343         \o  X Window System synchronization
       
   344         \o  flushX(),
       
   345             syncX().
       
   346 
       
   347         \row
       
   348         \o  Session management
       
   349         \o  isSessionRestored(),
       
   350             sessionId(),
       
   351             commitData(),
       
   352             saveState().
       
   353 
       
   354         \row
       
   355         \o  Miscellaneous
       
   356         \o  closeAllWindows(),
       
   357             startingUp(),
       
   358             closingDown(),
       
   359             type().
       
   360     \endtable
       
   361 
       
   362     \sa QCoreApplication, QAbstractEventDispatcher, QEventLoop, QSettings
       
   363 */
       
   364 
       
   365 /*!
       
   366     \enum QApplication::Type
       
   367 
       
   368     \value Tty a console application
       
   369     \value GuiClient a GUI client application
       
   370     \value GuiServer a GUI server application (for Qt for Embedded Linux)
       
   371 */
       
   372 
       
   373 /*!
       
   374     \enum QApplication::ColorSpec
       
   375 
       
   376     \value NormalColor the default color allocation policy
       
   377     \value CustomColor the same as NormalColor for X11; allocates colors
       
   378     to a palette on demand under Windows
       
   379     \value ManyColor the right choice for applications that use thousands of
       
   380     colors
       
   381 
       
   382     See setColorSpec() for full details.
       
   383 */
       
   384 
       
   385 /*!
       
   386     \fn QWidget *QApplication::topLevelAt(const QPoint &point)
       
   387 
       
   388     Returns the top-level widget at the given \a point; returns 0 if
       
   389     there is no such widget.
       
   390 */
       
   391 
       
   392 /*!
       
   393     \fn QWidget *QApplication::topLevelAt(int x, int y)
       
   394 
       
   395     \overload
       
   396 
       
   397     Returns the top-level widget at the point (\a{x}, \a{y}); returns
       
   398     0 if there is no such widget.
       
   399 */
       
   400 
       
   401 
       
   402 /*
       
   403     The qt_init() and qt_cleanup() functions are implemented in the
       
   404     qapplication_xyz.cpp file.
       
   405 */
       
   406 
       
   407 void qt_init(QApplicationPrivate *priv, int type
       
   408 #ifdef Q_WS_X11
       
   409               , Display *display = 0, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0
       
   410 #endif
       
   411    );
       
   412 void qt_cleanup();
       
   413 
       
   414 Qt::MouseButtons QApplicationPrivate::mouse_buttons = Qt::NoButton;
       
   415 Qt::KeyboardModifiers QApplicationPrivate::modifier_buttons = Qt::NoModifier;
       
   416 
       
   417 QStyle *QApplicationPrivate::app_style = 0;        // default application style
       
   418 QString QApplicationPrivate::styleOverride;        // style override
       
   419 
       
   420 #ifndef QT_NO_STYLE_STYLESHEET
       
   421 QString QApplicationPrivate::styleSheet;           // default application stylesheet
       
   422 #endif
       
   423 QPointer<QWidget> QApplicationPrivate::leaveAfterRelease = 0;
       
   424 
       
   425 int QApplicationPrivate::app_cspec = QApplication::NormalColor;
       
   426 QPalette *QApplicationPrivate::app_pal = 0;        // default application palette
       
   427 QPalette *QApplicationPrivate::sys_pal = 0;        // default system palette
       
   428 QPalette *QApplicationPrivate::set_pal = 0;        // default palette set by programmer
       
   429 
       
   430 QGraphicsSystem *QApplicationPrivate::graphics_system = 0; // default graphics system
       
   431 QString QApplicationPrivate::graphics_system_name;         // graphics system id - for delayed initialization
       
   432 
       
   433 Q_GLOBAL_STATIC(QMutex, applicationFontMutex)
       
   434 QFont *QApplicationPrivate::app_font = 0;        // default application font
       
   435 QFont *QApplicationPrivate::sys_font = 0;        // default system font
       
   436 QFont *QApplicationPrivate::set_font = 0;        // default font set by programmer
       
   437 
       
   438 QIcon *QApplicationPrivate::app_icon = 0;
       
   439 QWidget *QApplicationPrivate::main_widget = 0;        // main application widget
       
   440 QWidget *QApplicationPrivate::focus_widget = 0;        // has keyboard input focus
       
   441 QWidget *QApplicationPrivate::hidden_focus_widget = 0; // will get keyboard input focus after show()
       
   442 QWidget *QApplicationPrivate::active_window = 0;        // toplevel with keyboard focus
       
   443 bool QApplicationPrivate::obey_desktop_settings = true;        // use winsys resources
       
   444 int QApplicationPrivate::cursor_flash_time = 1000;        // text caret flash time
       
   445 int QApplicationPrivate::mouse_double_click_time = 400;        // mouse dbl click limit
       
   446 int QApplicationPrivate::keyboard_input_time = 400; // keyboard input interval
       
   447 #ifndef QT_NO_WHEELEVENT
       
   448 int QApplicationPrivate::wheel_scroll_lines;   // number of lines to scroll
       
   449 #endif
       
   450 bool qt_is_gui_used;
       
   451 bool Q_GUI_EXPORT qt_tab_all_widgets = true;
       
   452 bool qt_in_tab_key_event = false;
       
   453 int qt_antialiasing_threshold = -1;
       
   454 static int drag_time = 500;
       
   455 #ifdef Q_OS_SYMBIAN
       
   456 // The screens are a bit too small to for your thumb when using only 4 pixels drag distance.
       
   457 static int drag_distance = 8;
       
   458 #else
       
   459 static int drag_distance = 4;
       
   460 #endif
       
   461 static Qt::LayoutDirection layout_direction = Qt::LeftToRight;
       
   462 QSize QApplicationPrivate::app_strut = QSize(0,0); // no default application strut
       
   463 bool QApplicationPrivate::animate_ui = true;
       
   464 bool QApplicationPrivate::animate_menu = false;
       
   465 bool QApplicationPrivate::fade_menu = false;
       
   466 bool QApplicationPrivate::animate_combo = false;
       
   467 bool QApplicationPrivate::animate_tooltip = false;
       
   468 bool QApplicationPrivate::fade_tooltip = false;
       
   469 bool QApplicationPrivate::animate_toolbox = false;
       
   470 bool QApplicationPrivate::widgetCount = false;
       
   471 bool QApplicationPrivate::load_testability = false;
       
   472 #ifdef QT_KEYPAD_NAVIGATION
       
   473 #  ifdef Q_OS_SYMBIAN
       
   474 Qt::NavigationMode QApplicationPrivate::navigationMode = Qt::NavigationModeKeypadDirectional;
       
   475 #  else
       
   476 Qt::NavigationMode QApplicationPrivate::navigationMode = Qt::NavigationModeKeypadTabOrder;
       
   477 #  endif
       
   478 QWidget *QApplicationPrivate::oldEditFocus = 0;
       
   479 #endif
       
   480 
       
   481 bool qt_tabletChokeMouse = false;
       
   482 static bool force_reverse = false;
       
   483 
       
   484 inline bool QApplicationPrivate::isAlien(QWidget *widget)
       
   485 {
       
   486     if (!widget)
       
   487         return false;
       
   488 #if defined(Q_WS_MAC) // Fake alien behavior on the Mac :)
       
   489     return !widget->isWindow() && widget->window()->testAttribute(Qt::WA_DontShowOnScreen);
       
   490 #elif defined(Q_WS_QWS)
       
   491     return !widget->isWindow()
       
   492 # ifdef Q_BACKINGSTORE_SUBSURFACES
       
   493         && !(widget->d_func()->maybeTopData() && widget->d_func()->maybeTopData()->windowSurface)
       
   494 # endif
       
   495         ;
       
   496 #else
       
   497     return !widget->internalWinId();
       
   498 #endif
       
   499 }
       
   500 
       
   501 // ######## move to QApplicationPrivate
       
   502 // Default application palettes and fonts (per widget type)
       
   503 Q_GLOBAL_STATIC(PaletteHash, app_palettes)
       
   504 PaletteHash *qt_app_palettes_hash()
       
   505 {
       
   506     return app_palettes();
       
   507 }
       
   508 
       
   509 Q_GLOBAL_STATIC(FontHash, app_fonts)
       
   510 FontHash *qt_app_fonts_hash()
       
   511 {
       
   512     return app_fonts();
       
   513 }
       
   514 
       
   515 QWidgetList *QApplicationPrivate::popupWidgets = 0;        // has keyboard input focus
       
   516 
       
   517 QDesktopWidget *qt_desktopWidget = 0;                // root window widgets
       
   518 #ifndef QT_NO_CLIPBOARD
       
   519 QClipboard              *qt_clipboard = 0;        // global clipboard object
       
   520 #endif
       
   521 QWidgetList * qt_modal_stack=0;                // stack of modal widgets
       
   522 
       
   523 /*!
       
   524     \internal
       
   525 */
       
   526 void QApplicationPrivate::process_cmdline()
       
   527 {
       
   528     // process platform-indep command line
       
   529     if (!qt_is_gui_used || !argc)
       
   530         return;
       
   531 
       
   532     int i, j;
       
   533 
       
   534     j = 1;
       
   535     for (i=1; i<argc; i++) { // if you add anything here, modify QCoreApplication::arguments()
       
   536         if (argv[i] && *argv[i] != '-') {
       
   537             argv[j++] = argv[i];
       
   538             continue;
       
   539         }
       
   540         QByteArray arg = argv[i];
       
   541         arg = arg;
       
   542         QString s;
       
   543         if (arg == "-qdevel" || arg == "-qdebug") {
       
   544             // obsolete argument
       
   545         } else if (arg.indexOf("-style=", 0) != -1) {
       
   546             s = QString::fromLocal8Bit(arg.right(arg.length() - 7).toLower());
       
   547         } else if (arg == "-style" && i < argc-1) {
       
   548             s = QString::fromLocal8Bit(argv[++i]).toLower();
       
   549 #ifndef QT_NO_SESSIONMANAGER
       
   550         } else if (arg == "-session" && i < argc-1) {
       
   551             ++i;
       
   552             if (argv[i] && *argv[i]) {
       
   553                 session_id = QString::fromLatin1(argv[i]);
       
   554                 int p = session_id.indexOf(QLatin1Char('_'));
       
   555                 if (p >= 0) {
       
   556                     session_key = session_id.mid(p +1);
       
   557                     session_id = session_id.left(p);
       
   558                 }
       
   559                 is_session_restored = true;
       
   560             }
       
   561 #endif
       
   562 #ifndef QT_NO_STYLE_STYLESHEET
       
   563         } else if (arg == "-stylesheet" && i < argc -1) {
       
   564             styleSheet = QLatin1String("file:///");
       
   565             styleSheet.append(QString::fromLocal8Bit(argv[++i]));
       
   566         } else if (arg.indexOf("-stylesheet=") != -1) {
       
   567             styleSheet = QLatin1String("file:///");
       
   568             styleSheet.append(QString::fromLocal8Bit(arg.right(arg.length() - 12)));
       
   569 #endif
       
   570         } else if (qstrcmp(arg, "-reverse") == 0) {
       
   571             force_reverse = true;
       
   572             QApplication::setLayoutDirection(Qt::RightToLeft);
       
   573         } else if (qstrcmp(arg, "-widgetcount") == 0) {
       
   574             widgetCount = true;
       
   575         } else if (qstrcmp(arg, "-testability") == 0) {
       
   576             load_testability = true;
       
   577         } else if (arg == "-graphicssystem" && i < argc-1) {
       
   578             graphics_system_name = QString::fromLocal8Bit(argv[++i]);
       
   579         } else {
       
   580             argv[j++] = argv[i];
       
   581         }
       
   582         if (!s.isEmpty()) {
       
   583             if (app_style) {
       
   584                 delete app_style;
       
   585                 app_style = 0;
       
   586             }
       
   587             styleOverride = s;
       
   588         }
       
   589     }
       
   590 
       
   591     if(j < argc) {
       
   592         argv[j] = 0;
       
   593         argc = j;
       
   594     }
       
   595 }
       
   596 
       
   597 /*!
       
   598     Initializes the window system and constructs an application object with
       
   599     \a argc command line arguments in \a argv.
       
   600 
       
   601     \warning The data referred to by \a argc and \a argv must stay valid for
       
   602     the entire lifetime of the QApplication object. In addition, \a argc must
       
   603     be greater than zero and \a argv must contain at least one valid character
       
   604     string.
       
   605 
       
   606     The global \c qApp pointer refers to this application object. Only one
       
   607     application object should be created.
       
   608 
       
   609     This application object must be constructed before any \l{QPaintDevice}
       
   610     {paint devices} (including widgets, pixmaps, bitmaps etc.).
       
   611 
       
   612     \note \a argc and \a argv might be changed as Qt removes command line
       
   613     arguments that it recognizes.
       
   614 
       
   615     Qt debugging options (not available if Qt was compiled without the QT_DEBUG
       
   616     flag defined):
       
   617     \list
       
   618         \o  -nograb, tells Qt that it must never grab the mouse or the
       
   619             keyboard.
       
   620         \o  -dograb (only under X11), running under a debugger can cause an
       
   621             implicit -nograb, use -dograb to override.
       
   622         \o  -sync (only under X11), switches to synchronous mode for
       
   623             debugging.
       
   624     \endlist
       
   625 
       
   626     See \l{Debugging Techniques} for a more detailed explanation.
       
   627 
       
   628     All Qt programs automatically support the following command line options:
       
   629     \list
       
   630         \o  -style= \e style, sets the application GUI style. Possible values
       
   631             are \c motif, \c windows, and \c platinum. If you compiled Qt with
       
   632             additional styles or have additional styles as plugins these will
       
   633             be available to the \c -style command line option.
       
   634         \o  -style \e style, is the same as listed above.
       
   635         \o  -stylesheet= \e stylesheet, sets the application \l styleSheet. The
       
   636             value must be a path to a file that contains the Style Sheet.
       
   637             \note Relative URLs in the Style Sheet file are relative to the
       
   638             Style Sheet file's path.
       
   639         \o  -stylesheet \e stylesheet, is the same as listed above.
       
   640         \o  -session= \e session, restores the application from an earlier
       
   641             \l{Session Management}{session}.
       
   642         \o  -session \e session, is the same as listed above.
       
   643         \o  -widgetcount, prints debug message at the end about number of
       
   644             widgets left undestroyed and maximum number of widgets existed at
       
   645             the same time
       
   646         \o  -reverse, sets the application's layout direction to
       
   647             Qt::RightToLeft
       
   648         \o  -graphicssystem, sets the backend to be used for on-screen widgets
       
   649             and QPixmaps. Available options are \c{raster} and \c{opengl}.
       
   650     \endlist
       
   651 
       
   652     The X11 version of Qt supports some traditional X11 command line options:
       
   653     \list
       
   654         \o  -display \e display, sets the X display (default is $DISPLAY).
       
   655         \o  -geometry \e geometry, sets the client geometry of the first window
       
   656             that is shown.
       
   657         \o  -fn or \c -font \e font, defines the application font. The font
       
   658             should be specified using an X logical font description.
       
   659         \o  -bg or \c -background \e color, sets the default background color
       
   660             and an application palette (light and dark shades are calculated).
       
   661         \o  -fg or \c -foreground \e color, sets the default foreground color.
       
   662         \o  -btn or \c -button \e color, sets the default button color.
       
   663         \o  -name \e name, sets the application name.
       
   664         \o  -title \e title, sets the application title.
       
   665         \o  -visual \c TrueColor, forces the application to use a TrueColor
       
   666             visual on an 8-bit display.
       
   667         \o  -ncols \e count, limits the number of colors allocated in the color
       
   668             cube on an 8-bit display, if the application is using the
       
   669             QApplication::ManyColor color specification. If \e count is 216
       
   670             then a 6x6x6 color cube is used (i.e. 6 levels of red, 6 of green,
       
   671             and 6 of blue); for other values, a cube approximately proportional
       
   672             to a 2x3x1 cube is used.
       
   673         \o  -cmap, causes the application to install a private color map on an
       
   674             8-bit display.
       
   675         \o  -im, sets the input method server (equivalent to setting the
       
   676             XMODIFIERS environment variable)
       
   677         \o  -inputstyle, defines how the input is inserted into the given
       
   678             widget, e.g., \c onTheSpot makes the input appear directly in the
       
   679             widget, while \c overTheSpot makes the input appear in a box
       
   680             floating over the widget and is not inserted until the editing is
       
   681             done.
       
   682     \endlist
       
   683 
       
   684     \sa arguments()
       
   685 */
       
   686 
       
   687 QApplication::QApplication(int &argc, char **argv)
       
   688     : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
       
   689 { Q_D(QApplication); d->construct(); }
       
   690 
       
   691 QApplication::QApplication(int &argc, char **argv, int _internal)
       
   692     : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
       
   693 { Q_D(QApplication); d->construct(); QApplicationPrivate::app_compile_version = _internal;}
       
   694 
       
   695 
       
   696 /*!
       
   697     Constructs an application object with \a argc command line arguments in
       
   698     \a argv. If \a GUIenabled is true, a GUI application is constructed,
       
   699     otherwise a non-GUI (console) application is created.
       
   700 
       
   701     \warning The data referred to by \a argc and \a argv must stay valid for
       
   702     the entire lifetime of the QApplication object. In addition, \a argc must
       
   703     be greater than zero and \a argv must contain at least one valid character
       
   704     string.
       
   705 
       
   706     Set \a GUIenabled to false for programs without a graphical user interface
       
   707     that should be able to run without a window system.
       
   708 
       
   709     On X11, the window system is initialized if \a GUIenabled is true. If
       
   710     \a GUIenabled is false, the application does not connect to the X server.
       
   711     On Windows and Mac OS, currently the window system is always initialized,
       
   712     regardless of the value of GUIenabled. This may change in future versions
       
   713     of Qt.
       
   714 
       
   715     The following example shows how to create an application that uses a
       
   716     graphical interface when available.
       
   717 
       
   718     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 0
       
   719 */
       
   720 
       
   721 QApplication::QApplication(int &argc, char **argv, bool GUIenabled )
       
   722     : QCoreApplication(*new QApplicationPrivate(argc, argv, GUIenabled ? GuiClient : Tty))
       
   723 { Q_D(QApplication); d->construct(); }
       
   724 
       
   725 QApplication::QApplication(int &argc, char **argv, bool GUIenabled , int _internal)
       
   726     : QCoreApplication(*new QApplicationPrivate(argc, argv, GUIenabled ? GuiClient : Tty))
       
   727 { Q_D(QApplication); d->construct();  QApplicationPrivate::app_compile_version = _internal;}
       
   728 
       
   729 
       
   730 
       
   731 /*!
       
   732     Constructs an application object with \a argc command line arguments in
       
   733     \a argv.
       
   734 
       
   735     \warning The data referred to by \a argc and \a argv must stay valid for
       
   736     the entire lifetime of the QApplication object. In addition, \a argc must
       
   737     be greater than zero and \a argv must contain at least one valid character
       
   738     string.
       
   739 
       
   740     With Qt for Embedded Linux, passing QApplication::GuiServer for \a type
       
   741     makes this application the server (equivalent to running with the
       
   742     \c -qws option).
       
   743 */
       
   744 QApplication::QApplication(int &argc, char **argv, Type type)
       
   745     : QCoreApplication(*new QApplicationPrivate(argc, argv, type))
       
   746 { Q_D(QApplication); d->construct(); }
       
   747 
       
   748 QApplication::QApplication(int &argc, char **argv, Type type , int _internal)
       
   749     : QCoreApplication(*new QApplicationPrivate(argc, argv, type))
       
   750 { Q_D(QApplication); d->construct();  QApplicationPrivate::app_compile_version = _internal;}
       
   751 
       
   752 
       
   753 /*!
       
   754     \internal
       
   755 */
       
   756 void QApplicationPrivate::construct(
       
   757 #ifdef Q_WS_X11
       
   758                                     Display *dpy, Qt::HANDLE visual, Qt::HANDLE cmap
       
   759 #endif
       
   760                                     )
       
   761 {
       
   762     initResources();
       
   763 
       
   764     qt_is_gui_used = (qt_appType != QApplication::Tty);
       
   765     process_cmdline();
       
   766     // Must be called before initialize()
       
   767     qt_init(this, qt_appType
       
   768 #ifdef Q_WS_X11
       
   769             , dpy, visual, cmap
       
   770 #endif
       
   771             );
       
   772     initialize();
       
   773     eventDispatcher->startingUp();
       
   774 
       
   775 #ifdef QT_EVAL
       
   776     extern void qt_gui_eval_init(uint);
       
   777     qt_gui_eval_init(application_type);
       
   778 #endif
       
   779 
       
   780 #ifndef QT_NO_LIBRARY
       
   781     if(load_testability) {
       
   782         QLibrary testLib(QLatin1String("qttestability"));
       
   783         if (testLib.load()) {
       
   784             typedef void (*TasInitialize)(void);
       
   785             TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
       
   786             if (initFunction) {
       
   787                 initFunction();
       
   788             } else {
       
   789                 qCritical("Library qttestability resolve failed!");
       
   790             }
       
   791         } else {
       
   792             qCritical("Library qttestability load failed!");
       
   793         }
       
   794     }
       
   795 
       
   796     //make sure the plugin is loaded
       
   797     if (qt_is_gui_used)
       
   798         qt_guiPlatformPlugin();
       
   799 #endif
       
   800 }
       
   801 
       
   802 #if defined(Q_WS_X11)
       
   803 // ### a string literal is a cont char*
       
   804 // ### using it as a char* is wrong and could lead to segfaults
       
   805 // ### if aargv is modified someday
       
   806 // ########## make it work with argc == argv == 0
       
   807 static int aargc = 1;
       
   808 static char *aargv[] = { (char*)"unknown", 0 };
       
   809 
       
   810 /*!
       
   811     \fn QApplication::QApplication(Display* display, Qt::HANDLE visual, Qt::HANDLE colormap)
       
   812 
       
   813     Creates an application, given an already open display \a display. If
       
   814     \a visual and \a colormap are non-zero, the application will use those
       
   815     values as the default Visual and Colormap contexts.
       
   816 
       
   817     \warning Qt only supports TrueColor visuals at depths higher than 8
       
   818     bits-per-pixel.
       
   819 
       
   820     This function is only available on X11.
       
   821 */
       
   822 QApplication::QApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE colormap)
       
   823     : QCoreApplication(*new QApplicationPrivate(aargc, aargv, GuiClient))
       
   824 {
       
   825     if (! dpy)
       
   826         qWarning("QApplication: Invalid Display* argument");
       
   827     Q_D(QApplication);
       
   828     d->construct(dpy, visual, colormap);
       
   829 }
       
   830 
       
   831 QApplication::QApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE colormap, int _internal)
       
   832     : QCoreApplication(*new QApplicationPrivate(aargc, aargv, GuiClient))
       
   833 {
       
   834     if (! dpy)
       
   835         qWarning("QApplication: Invalid Display* argument");
       
   836     Q_D(QApplication);
       
   837     d->construct(dpy, visual, colormap);
       
   838     QApplicationPrivate::app_compile_version = _internal;
       
   839 }
       
   840 
       
   841 /*!
       
   842     \fn QApplication::QApplication(Display *display, int &argc, char **argv,
       
   843         Qt::HANDLE visual, Qt::HANDLE colormap)
       
   844 
       
   845     Creates an application, given an already open \a display and using \a argc
       
   846     command line arguments in \a argv. If \a visual and \a colormap are
       
   847     non-zero, the application will use those values as the default Visual
       
   848     and Colormap contexts.
       
   849 
       
   850     \warning Qt only supports TrueColor visuals at depths higher than 8
       
   851     bits-per-pixel.
       
   852 
       
   853     This function is only available on X11.
       
   854 */
       
   855 QApplication::QApplication(Display *dpy, int &argc, char **argv,
       
   856                            Qt::HANDLE visual, Qt::HANDLE colormap)
       
   857     : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
       
   858 {
       
   859     if (! dpy)
       
   860         qWarning("QApplication: Invalid Display* argument");
       
   861     Q_D(QApplication);
       
   862     d->construct(dpy, visual, colormap);
       
   863 }
       
   864 
       
   865 QApplication::QApplication(Display *dpy, int &argc, char **argv,
       
   866                            Qt::HANDLE visual, Qt::HANDLE colormap, int _internal)
       
   867     : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
       
   868 {
       
   869     if (! dpy)
       
   870         qWarning("QApplication: Invalid Display* argument");
       
   871     Q_D(QApplication);
       
   872     d->construct(dpy, visual, colormap);
       
   873     QApplicationPrivate::app_compile_version = _internal;
       
   874 }
       
   875 
       
   876 #endif // Q_WS_X11
       
   877 
       
   878 extern void qInitDrawhelperAsm();
       
   879 extern int qRegisterGuiVariant();
       
   880 extern int qUnregisterGuiVariant();
       
   881 #ifndef QT_NO_STATEMACHINE
       
   882 extern int qRegisterGuiStateMachine();
       
   883 extern int qUnregisterGuiStateMachine();
       
   884 #endif
       
   885 
       
   886 /*!
       
   887   \fn void QApplicationPrivate::initialize()
       
   888 
       
   889   Initializes the QApplication object, called from the constructors.
       
   890 */
       
   891 void QApplicationPrivate::initialize()
       
   892 {
       
   893     QWidgetPrivate::mapper = new QWidgetMapper;
       
   894     QWidgetPrivate::allWidgets = new QWidgetSet;
       
   895 
       
   896 #if !defined(Q_WS_X11) && !defined(Q_WS_QWS) && !defined(Q_CC_NOKIAX86)
       
   897     // initialize the graphics system - on X11 this is initialized inside
       
   898     // qt_init() in qapplication_x11.cpp because of several reasons.
       
   899     // On QWS, the graphics system is set by the QScreen plugin.
       
   900     graphics_system = QGraphicsSystemFactory::create(graphics_system_name);
       
   901 #endif
       
   902 
       
   903     if (qt_appType != QApplication::Tty)
       
   904         (void) QApplication::style();  // trigger creation of application style
       
   905     // trigger registering of QVariant's GUI types
       
   906     qRegisterGuiVariant();
       
   907 #ifndef QT_NO_STATEMACHINE
       
   908     // trigger registering of QStateMachine's GUI types
       
   909     qRegisterGuiStateMachine();
       
   910 #endif
       
   911 
       
   912     is_app_running = true; // no longer starting up
       
   913 
       
   914     Q_Q(QApplication);
       
   915 #ifndef QT_NO_SESSIONMANAGER
       
   916     // connect to the session manager
       
   917     session_manager = new QSessionManager(q, session_id, session_key);
       
   918 #endif
       
   919 
       
   920     if (qgetenv("QT_USE_NATIVE_WINDOWS").toInt() > 0)
       
   921         q->setAttribute(Qt::AA_NativeWindows);
       
   922 
       
   923 #ifdef Q_WS_WINCE
       
   924 #ifdef QT_AUTO_MAXIMIZE_THRESHOLD
       
   925     autoMaximizeThreshold = QT_AUTO_MAXIMIZE_THRESHOLD;
       
   926 #else
       
   927     if (qt_wince_is_mobile())
       
   928         autoMaximizeThreshold = 50;
       
   929     else
       
   930         autoMaximizeThreshold = -1;
       
   931 #endif //QT_AUTO_MAXIMIZE_THRESHOLD
       
   932 #endif //Q_WS_WINCE
       
   933 
       
   934     // Set up which span functions should be used in raster engine...
       
   935     qInitDrawhelperAsm();
       
   936 
       
   937 #if defined(Q_CC_NOKIAX86)
       
   938     // initialize the graphics system - For symbian emulator, we create graphics system here, since
       
   939     // there is some unknown error launching the emulator with openVg when 
       
   940     // graphics system is created before style instance.
       
   941     graphics_system = QGraphicsSystemFactory::create(graphics_system_name);
       
   942 #endif
       
   943 
       
   944 #ifndef QT_NO_WHEELEVENT
       
   945     QApplicationPrivate::wheel_scroll_lines = 3;
       
   946 #endif
       
   947 
       
   948     if (qt_is_gui_used)
       
   949         initializeMultitouch();
       
   950 }
       
   951 
       
   952 /*!
       
   953     Returns the type of application (\l Tty, GuiClient, or
       
   954     GuiServer). The type is set when constructing the QApplication
       
   955     object.
       
   956 */
       
   957 QApplication::Type QApplication::type()
       
   958 {
       
   959     return qt_appType;
       
   960 }
       
   961 
       
   962 /*****************************************************************************
       
   963   Functions returning the active popup and modal widgets.
       
   964  *****************************************************************************/
       
   965 
       
   966 /*!
       
   967     Returns the active popup widget.
       
   968 
       
   969     A popup widget is a special top-level widget that sets the \c
       
   970     Qt::WType_Popup widget flag, e.g. the QMenu widget. When the application
       
   971     opens a popup widget, all events are sent to the popup. Normal widgets and
       
   972     modal widgets cannot be accessed before the popup widget is closed.
       
   973 
       
   974     Only other popup widgets may be opened when a popup widget is shown. The
       
   975     popup widgets are organized in a stack. This function returns the active
       
   976     popup widget at the top of the stack.
       
   977 
       
   978     \sa activeModalWidget(), topLevelWidgets()
       
   979 */
       
   980 
       
   981 QWidget *QApplication::activePopupWidget()
       
   982 {
       
   983     return QApplicationPrivate::popupWidgets && !QApplicationPrivate::popupWidgets->isEmpty() ?
       
   984         QApplicationPrivate::popupWidgets->last() : 0;
       
   985 }
       
   986 
       
   987 
       
   988 /*!
       
   989     Returns the active modal widget.
       
   990 
       
   991     A modal widget is a special top-level widget which is a subclass of QDialog
       
   992     that specifies the modal parameter of the constructor as true. A modal
       
   993     widget must be closed before the user can continue with other parts of the
       
   994     program.
       
   995 
       
   996     Modal widgets are organized in a stack. This function returns the active
       
   997     modal widget at the top of the stack.
       
   998 
       
   999     \sa activePopupWidget(), topLevelWidgets()
       
  1000 */
       
  1001 
       
  1002 QWidget *QApplication::activeModalWidget()
       
  1003 {
       
  1004     return qt_modal_stack && !qt_modal_stack->isEmpty() ? qt_modal_stack->first() : 0;
       
  1005 }
       
  1006 
       
  1007 /*!
       
  1008     Cleans up any window system resources that were allocated by this
       
  1009     application. Sets the global variable \c qApp to 0.
       
  1010 */
       
  1011 
       
  1012 QApplication::~QApplication()
       
  1013 {
       
  1014     Q_D(QApplication);
       
  1015 
       
  1016 #ifndef QT_NO_CLIPBOARD
       
  1017     // flush clipboard contents
       
  1018     if (qt_clipboard) {
       
  1019         QEvent event(QEvent::Clipboard);
       
  1020         QApplication::sendEvent(qt_clipboard, &event);
       
  1021     }
       
  1022 #endif
       
  1023 
       
  1024     //### this should probable be done even later
       
  1025     qt_call_post_routines();
       
  1026 
       
  1027     // kill timers before closing down the dispatcher
       
  1028     d->toolTipWakeUp.stop();
       
  1029     d->toolTipFallAsleep.stop();
       
  1030 
       
  1031     d->eventDispatcher->closingDown();
       
  1032     d->eventDispatcher = 0;
       
  1033     QApplicationPrivate::is_app_closing = true;
       
  1034     QApplicationPrivate::is_app_running = false;
       
  1035 
       
  1036     delete qt_desktopWidget;
       
  1037     qt_desktopWidget = 0;
       
  1038 
       
  1039 #ifndef QT_NO_CLIPBOARD
       
  1040     delete qt_clipboard;
       
  1041     qt_clipboard = 0;
       
  1042 #endif
       
  1043 
       
  1044     delete QWidgetPrivate::mapper;
       
  1045     QWidgetPrivate::mapper = 0;
       
  1046 
       
  1047     // delete all widgets
       
  1048     if (QWidgetPrivate::allWidgets) {
       
  1049         QWidgetSet *mySet = QWidgetPrivate::allWidgets;
       
  1050         QWidgetPrivate::allWidgets = 0;
       
  1051         for (QWidgetSet::ConstIterator it = mySet->constBegin(); it != mySet->constEnd(); ++it) {
       
  1052             register QWidget *w = *it;
       
  1053             if (!w->parent())                        // window
       
  1054                 w->destroy(true, true);
       
  1055         }
       
  1056         delete mySet;
       
  1057     }
       
  1058 
       
  1059     delete QApplicationPrivate::app_pal;
       
  1060     QApplicationPrivate::app_pal = 0;
       
  1061     delete QApplicationPrivate::sys_pal;
       
  1062     QApplicationPrivate::sys_pal = 0;
       
  1063     delete QApplicationPrivate::set_pal;
       
  1064     QApplicationPrivate::set_pal = 0;
       
  1065     app_palettes()->clear();
       
  1066 
       
  1067     {
       
  1068         QMutexLocker locker(applicationFontMutex());
       
  1069         delete QApplicationPrivate::app_font;
       
  1070         QApplicationPrivate::app_font = 0;
       
  1071     }
       
  1072     delete QApplicationPrivate::sys_font;
       
  1073     QApplicationPrivate::sys_font = 0;
       
  1074     delete QApplicationPrivate::set_font;
       
  1075     QApplicationPrivate::set_font = 0;
       
  1076     app_fonts()->clear();
       
  1077 
       
  1078     delete QApplicationPrivate::app_style;
       
  1079     QApplicationPrivate::app_style = 0;
       
  1080     delete QApplicationPrivate::app_icon;
       
  1081     QApplicationPrivate::app_icon = 0;
       
  1082 #ifndef QT_NO_CURSOR
       
  1083     d->cursor_list.clear();
       
  1084 #endif
       
  1085 
       
  1086 #ifndef QT_NO_DRAGANDDROP
       
  1087     if (qt_is_gui_used)
       
  1088         delete QDragManager::self();
       
  1089 #endif
       
  1090 
       
  1091     d->cleanupMultitouch();
       
  1092 
       
  1093     qt_cleanup();
       
  1094 
       
  1095     if (QApplicationPrivate::widgetCount)
       
  1096         qDebug("Widgets left: %i    Max widgets: %i \n", QWidgetPrivate::instanceCounter, QWidgetPrivate::maxInstances);
       
  1097 #ifndef QT_NO_SESSIONMANAGER
       
  1098     delete d->session_manager;
       
  1099     d->session_manager = 0;
       
  1100 #endif //QT_NO_SESSIONMANAGER
       
  1101 
       
  1102     QApplicationPrivate::obey_desktop_settings = true;
       
  1103     QApplicationPrivate::cursor_flash_time = 1000;
       
  1104     QApplicationPrivate::mouse_double_click_time = 400;
       
  1105     QApplicationPrivate::keyboard_input_time = 400;
       
  1106 
       
  1107     drag_time = 500;
       
  1108     drag_distance = 4;
       
  1109     layout_direction = Qt::LeftToRight;
       
  1110     QApplicationPrivate::app_strut = QSize(0, 0);
       
  1111     QApplicationPrivate::animate_ui = true;
       
  1112     QApplicationPrivate::animate_menu = false;
       
  1113     QApplicationPrivate::fade_menu = false;
       
  1114     QApplicationPrivate::animate_combo = false;
       
  1115     QApplicationPrivate::animate_tooltip = false;
       
  1116     QApplicationPrivate::fade_tooltip = false;
       
  1117     QApplicationPrivate::widgetCount = false;
       
  1118 
       
  1119 #ifndef QT_NO_STATEMACHINE
       
  1120     // trigger unregistering of QStateMachine's GUI types
       
  1121     qUnregisterGuiStateMachine();
       
  1122 #endif
       
  1123     // trigger unregistering of QVariant's GUI types
       
  1124     qUnregisterGuiVariant();
       
  1125 }
       
  1126 
       
  1127 
       
  1128 /*!
       
  1129     \fn QWidget *QApplication::widgetAt(const QPoint &point)
       
  1130 
       
  1131     Returns the widget at global screen position \a point, or 0 if there is no
       
  1132     Qt widget there.
       
  1133 
       
  1134     This function can be slow.
       
  1135 
       
  1136     \sa QCursor::pos(), QWidget::grabMouse(), QWidget::grabKeyboard()
       
  1137 */
       
  1138 QWidget *QApplication::widgetAt(const QPoint &p)
       
  1139 {
       
  1140     QWidget *window = QApplication::topLevelAt(p);
       
  1141     if (!window)
       
  1142         return 0;
       
  1143 
       
  1144     QWidget *child = 0;
       
  1145 
       
  1146     if (!window->testAttribute(Qt::WA_TransparentForMouseEvents))
       
  1147         child = window->childAt(window->mapFromGlobal(p));
       
  1148 
       
  1149     if (child)
       
  1150         return child;
       
  1151 
       
  1152     if (window->testAttribute(Qt::WA_TransparentForMouseEvents)) {
       
  1153         //shoot a hole in the widget and try once again,
       
  1154         //suboptimal on Qt for Embedded Linux where we do
       
  1155         //know the stacking order of the toplevels.
       
  1156         int x = p.x();
       
  1157         int y = p.y();
       
  1158         QRegion oldmask = window->mask();
       
  1159         QPoint wpoint = window->mapFromGlobal(QPoint(x, y));
       
  1160         QRegion newmask = (oldmask.isEmpty() ? QRegion(window->rect()) : oldmask)
       
  1161                           - QRegion(wpoint.x(), wpoint.y(), 1, 1);
       
  1162         window->setMask(newmask);
       
  1163         QWidget *recurse = 0;
       
  1164         if (QApplication::topLevelAt(p) != window) // verify recursion will terminate
       
  1165             recurse = widgetAt(x, y);
       
  1166         if (oldmask.isEmpty())
       
  1167             window->clearMask();
       
  1168         else
       
  1169             window->setMask(oldmask);
       
  1170         return recurse;
       
  1171     }
       
  1172     return window;
       
  1173 }
       
  1174 
       
  1175 /*!
       
  1176     \fn QWidget *QApplication::widgetAt(int x, int y)
       
  1177 
       
  1178     \overload
       
  1179 
       
  1180     Returns the widget at global screen position (\a x, \a y), or 0 if there is
       
  1181     no Qt widget there.
       
  1182 */
       
  1183 
       
  1184 /*!
       
  1185     \fn void QApplication::setArgs(int argc, char **argv)
       
  1186     \internal
       
  1187 */
       
  1188 
       
  1189 
       
  1190 
       
  1191 /*!
       
  1192     \internal
       
  1193 */
       
  1194 bool QApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
       
  1195 {
       
  1196     if ((event->type() == QEvent::UpdateRequest
       
  1197 #ifdef QT3_SUPPORT
       
  1198           || event->type() == QEvent::LayoutHint
       
  1199 #endif
       
  1200           || event->type() == QEvent::LayoutRequest
       
  1201           || event->type() == QEvent::Resize
       
  1202           || event->type() == QEvent::Move
       
  1203           || event->type() == QEvent::LanguageChange
       
  1204           || event->type() == QEvent::UpdateSoftKeys
       
  1205           || event->type() == QEvent::InputMethod)) {
       
  1206         for (int i = 0; i < postedEvents->size(); ++i) {
       
  1207             const QPostEvent &cur = postedEvents->at(i);
       
  1208             if (cur.receiver != receiver || cur.event == 0 || cur.event->type() != event->type())
       
  1209                 continue;
       
  1210             if (cur.event->type() == QEvent::LayoutRequest
       
  1211 #ifdef QT3_SUPPORT
       
  1212                  || cur.event->type() == QEvent::LayoutHint
       
  1213 #endif
       
  1214                  || cur.event->type() == QEvent::UpdateRequest) {
       
  1215                 ;
       
  1216             } else if (cur.event->type() == QEvent::Resize) {
       
  1217                 ((QResizeEvent *)(cur.event))->s = ((QResizeEvent *)event)->s;
       
  1218             } else if (cur.event->type() == QEvent::Move) {
       
  1219                 ((QMoveEvent *)(cur.event))->p = ((QMoveEvent *)event)->p;
       
  1220             } else if (cur.event->type() == QEvent::LanguageChange) {
       
  1221                 ;
       
  1222             } else if (cur.event->type() == QEvent::UpdateSoftKeys) {
       
  1223                 ;
       
  1224             } else if ( cur.event->type() == QEvent::InputMethod ) {
       
  1225                 *(QInputMethodEvent *)(cur.event) = *(QInputMethodEvent *)event;
       
  1226             } else {
       
  1227                 continue;
       
  1228             }
       
  1229             delete event;
       
  1230             return true;
       
  1231         }
       
  1232         return false;
       
  1233     }
       
  1234     return QCoreApplication::compressEvent(event, receiver, postedEvents);
       
  1235 }
       
  1236 
       
  1237 /*!
       
  1238     \property QApplication::styleSheet
       
  1239     \brief the application style sheet
       
  1240     \since 4.2
       
  1241 
       
  1242     By default, this property returns an empty string unless the user specifies
       
  1243     the \c{-stylesheet} option on the command line when running the application.
       
  1244 
       
  1245     \sa QWidget::setStyle(), {Qt Style Sheets}
       
  1246 */
       
  1247 
       
  1248 /*!
       
  1249     \property QApplication::autoMaximizeThreshold
       
  1250     \since 4.4
       
  1251     \brief defines a threshold for auto maximizing widgets
       
  1252 
       
  1253     \bold{The auto maximize threshold is only available as part of Qt for
       
  1254     Windows CE.}
       
  1255 
       
  1256     This property defines a threshold for the size of a window as a percentage
       
  1257     of the screen size. If the minimum size hint of a window exceeds the
       
  1258     threshold, calling show() will cause the window to be maximized
       
  1259     automatically.
       
  1260 
       
  1261     Setting the threshold to 100 or greater means that the widget will always
       
  1262     be maximized. Alternatively, setting the threshold to 50 means that the
       
  1263     widget will be maximized only if the vertical minimum size hint is at least
       
  1264     50% of the vertical screen size.
       
  1265 
       
  1266     Setting the threshold to -1 disables the feature.
       
  1267 
       
  1268     On Windows CE the default is -1 (i.e., it is disabled).
       
  1269     On Windows Mobile the default is 40.
       
  1270 */
       
  1271 
       
  1272 /*!
       
  1273     \property QApplication::autoSipEnabled
       
  1274     \since 4.5
       
  1275     \brief toggles automatic SIP (software input panel) visibility
       
  1276 
       
  1277     Set this property to \c true to automatically display the SIP when entering
       
  1278     widgets that accept keyboard input. This property only affects widgets with
       
  1279     the WA_InputMethodEnabled attribute set, and is typically used to launch
       
  1280     a virtual keyboard on devices which have very few or no keys.
       
  1281 
       
  1282     \bold{ The property only has an effect on platforms which use software input
       
  1283     panels, such as Windows CE and Symbian.}
       
  1284 
       
  1285     The default is platform dependent.
       
  1286 */
       
  1287 
       
  1288 #ifdef Q_WS_WINCE
       
  1289 void QApplication::setAutoMaximizeThreshold(const int threshold)
       
  1290 {
       
  1291     QApplicationPrivate::autoMaximizeThreshold = threshold;
       
  1292 }
       
  1293 
       
  1294 int QApplication::autoMaximizeThreshold() const
       
  1295 {
       
  1296     return QApplicationPrivate::autoMaximizeThreshold;
       
  1297 }
       
  1298 #endif
       
  1299 
       
  1300 void QApplication::setAutoSipEnabled(const bool enabled)
       
  1301 {
       
  1302     QApplicationPrivate::autoSipEnabled = enabled;
       
  1303 }
       
  1304 
       
  1305 bool QApplication::autoSipEnabled() const
       
  1306 {
       
  1307     return QApplicationPrivate::autoSipEnabled;
       
  1308 }
       
  1309 
       
  1310 #ifndef QT_NO_STYLE_STYLESHEET
       
  1311 
       
  1312 QString QApplication::styleSheet() const
       
  1313 {
       
  1314     return QApplicationPrivate::styleSheet;
       
  1315 }
       
  1316 
       
  1317 void QApplication::setStyleSheet(const QString& styleSheet)
       
  1318 {
       
  1319     QApplicationPrivate::styleSheet = styleSheet;
       
  1320     QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle*>(QApplicationPrivate::app_style);
       
  1321     if (styleSheet.isEmpty()) { // application style sheet removed
       
  1322         if (!proxy)
       
  1323             return; // there was no stylesheet before
       
  1324         setStyle(proxy->base);
       
  1325     } else if (proxy) { // style sheet update, just repolish
       
  1326         proxy->repolish(qApp);
       
  1327     } else { // stylesheet set the first time
       
  1328         QStyleSheetStyle *newProxy = new QStyleSheetStyle(QApplicationPrivate::app_style);
       
  1329         QApplicationPrivate::app_style->setParent(newProxy);
       
  1330         setStyle(newProxy);
       
  1331     }
       
  1332 }
       
  1333 
       
  1334 #endif // QT_NO_STYLE_STYLESHEET
       
  1335 
       
  1336 /*!
       
  1337     Returns the application's style object.
       
  1338 
       
  1339     \sa setStyle(), QStyle
       
  1340 */
       
  1341 QStyle *QApplication::style()
       
  1342 {
       
  1343     if (QApplicationPrivate::app_style)
       
  1344         return QApplicationPrivate::app_style;
       
  1345     if (!qt_is_gui_used) {
       
  1346         Q_ASSERT(!"No style available in non-gui applications!");
       
  1347         return 0;
       
  1348     }
       
  1349 
       
  1350     if (!QApplicationPrivate::app_style) {
       
  1351         // Compile-time search for default style
       
  1352         //
       
  1353         QString style;
       
  1354         if (!QApplicationPrivate::styleOverride.isEmpty())
       
  1355             style = QApplicationPrivate::styleOverride;
       
  1356         else
       
  1357             style = QApplicationPrivate::desktopStyleKey();
       
  1358 
       
  1359         QStyle *&app_style = QApplicationPrivate::app_style;
       
  1360         app_style = QStyleFactory::create(style);
       
  1361         if (!app_style) {
       
  1362             QStringList styles = QStyleFactory::keys();
       
  1363             for (int i = 0; i < styles.size(); ++i) {
       
  1364                 if ((app_style = QStyleFactory::create(styles.at(i))))
       
  1365                     break;
       
  1366             }
       
  1367         }
       
  1368         if (!app_style) {
       
  1369             Q_ASSERT(!"No styles available!");
       
  1370             return 0;
       
  1371         }
       
  1372     }
       
  1373     // take ownership of the style
       
  1374     QApplicationPrivate::app_style->setParent(qApp);
       
  1375 
       
  1376     if (!QApplicationPrivate::sys_pal)
       
  1377         QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette());
       
  1378     if (QApplicationPrivate::set_pal) // repolish set palette with the new style
       
  1379         QApplication::setPalette(*QApplicationPrivate::set_pal);
       
  1380 
       
  1381 #ifndef QT_NO_STYLE_STYLESHEET
       
  1382     if (!QApplicationPrivate::styleSheet.isEmpty()) {
       
  1383         qApp->setStyleSheet(QApplicationPrivate::styleSheet);
       
  1384     } else
       
  1385 #endif
       
  1386         QApplicationPrivate::app_style->polish(qApp);
       
  1387 
       
  1388     return QApplicationPrivate::app_style;
       
  1389 }
       
  1390 
       
  1391 /*!
       
  1392     Sets the application's GUI style to \a style. Ownership of the style object
       
  1393     is transferred to QApplication, so QApplication will delete the style
       
  1394     object on application exit or when a new style is set and the old style is
       
  1395     still the parent of the application object.
       
  1396 
       
  1397     Example usage:
       
  1398     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 1
       
  1399 
       
  1400     When switching application styles, the color palette is set back to the
       
  1401     initial colors or the system defaults. This is necessary since certain
       
  1402     styles have to adapt the color palette to be fully style-guide compliant.
       
  1403 
       
  1404     Setting the style before a palette has been se, i.e., before creating
       
  1405     QApplication, will cause the application to use QStyle::standardPalette()
       
  1406     for the palette.
       
  1407 
       
  1408     \warning Qt style sheets are currently not supported for custom QStyle
       
  1409     subclasses. We plan to address this in some future release.
       
  1410 
       
  1411     \sa style(), QStyle, setPalette(), desktopSettingsAware()
       
  1412 */
       
  1413 void QApplication::setStyle(QStyle *style)
       
  1414 {
       
  1415     if (!style || style == QApplicationPrivate::app_style)
       
  1416         return;
       
  1417 
       
  1418     QWidgetList all = allWidgets();
       
  1419 
       
  1420     // clean up the old style
       
  1421     if (QApplicationPrivate::app_style) {
       
  1422         if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {
       
  1423             for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) {
       
  1424                 register QWidget *w = *it;
       
  1425                 if (!(w->windowType() == Qt::Desktop) &&        // except desktop
       
  1426                      w->testAttribute(Qt::WA_WState_Polished)) { // has been polished
       
  1427                     QApplicationPrivate::app_style->unpolish(w);
       
  1428                 }
       
  1429             }
       
  1430         }
       
  1431         QApplicationPrivate::app_style->unpolish(qApp);
       
  1432     }
       
  1433 
       
  1434     QStyle *old = QApplicationPrivate::app_style; // save
       
  1435 
       
  1436 #ifndef QT_NO_STYLE_STYLESHEET
       
  1437     if (!QApplicationPrivate::styleSheet.isEmpty() && !qobject_cast<QStyleSheetStyle *>(style)) {
       
  1438         // we have a stylesheet already and a new style is being set
       
  1439         QStyleSheetStyle *newProxy = new QStyleSheetStyle(style);
       
  1440         style->setParent(newProxy);
       
  1441         QApplicationPrivate::app_style = newProxy;
       
  1442     } else
       
  1443 #endif // QT_NO_STYLE_STYLESHEET
       
  1444         QApplicationPrivate::app_style = style;
       
  1445     QApplicationPrivate::app_style->setParent(qApp); // take ownership
       
  1446 
       
  1447     // take care of possible palette requirements of certain gui
       
  1448     // styles. Do it before polishing the application since the style
       
  1449     // might call QApplication::setPalette() itself
       
  1450     if (QApplicationPrivate::set_pal) {
       
  1451         QApplication::setPalette(*QApplicationPrivate::set_pal);
       
  1452     } else if (QApplicationPrivate::sys_pal) {
       
  1453         QApplicationPrivate::initializeWidgetPaletteHash();
       
  1454         QApplicationPrivate::setPalette_helper(*QApplicationPrivate::sys_pal, /*className=*/0, /*clearWidgetPaletteHash=*/false);
       
  1455     } else if (!QApplicationPrivate::sys_pal) {
       
  1456         // Initialize the sys_pal if it hasn't happened yet...
       
  1457         QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette());
       
  1458     }
       
  1459 
       
  1460     // initialize the application with the new style
       
  1461     QApplicationPrivate::app_style->polish(qApp);
       
  1462 
       
  1463     // re-polish existing widgets if necessary
       
  1464     if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {
       
  1465         for (QWidgetList::ConstIterator it1 = all.constBegin(); it1 != all.constEnd(); ++it1) {
       
  1466             register QWidget *w = *it1;
       
  1467             if (w->windowType() != Qt::Desktop && w->testAttribute(Qt::WA_WState_Polished)) {
       
  1468                 if (w->style() == QApplicationPrivate::app_style)
       
  1469                     QApplicationPrivate::app_style->polish(w);                // repolish
       
  1470 #ifndef QT_NO_STYLE_STYLESHEET
       
  1471                 else
       
  1472                     w->setStyleSheet(w->styleSheet()); // touch
       
  1473 #endif
       
  1474             }
       
  1475         }
       
  1476 
       
  1477         for (QWidgetList::ConstIterator it2 = all.constBegin(); it2 != all.constEnd(); ++it2) {
       
  1478             register QWidget *w = *it2;
       
  1479             if (w->windowType() != Qt::Desktop && !w->testAttribute(Qt::WA_SetStyle)) {
       
  1480                     QEvent e(QEvent::StyleChange);
       
  1481                     QApplication::sendEvent(w, &e);
       
  1482 #ifdef QT3_SUPPORT
       
  1483                     if (old)
       
  1484                         w->styleChange(*old);
       
  1485 #endif
       
  1486                     w->update();
       
  1487             }
       
  1488         }
       
  1489     }
       
  1490 
       
  1491 #ifndef QT_NO_STYLE_STYLESHEET
       
  1492     if (QStyleSheetStyle *oldProxy = qobject_cast<QStyleSheetStyle *>(old)) {
       
  1493         oldProxy->deref();
       
  1494     } else
       
  1495 #endif
       
  1496     if (old && old->parent() == qApp) {
       
  1497         delete old;
       
  1498     }
       
  1499 
       
  1500     if (QApplicationPrivate::focus_widget) {
       
  1501         QFocusEvent in(QEvent::FocusIn, Qt::OtherFocusReason);
       
  1502         QApplication::sendEvent(QApplicationPrivate::focus_widget->style(), &in);
       
  1503         QApplicationPrivate::focus_widget->update();
       
  1504     }
       
  1505 }
       
  1506 
       
  1507 /*!
       
  1508     \overload
       
  1509 
       
  1510     Requests a QStyle object for \a style from the QStyleFactory.
       
  1511 
       
  1512     The string must be one of the QStyleFactory::keys(), typically one of
       
  1513     "windows", "motif", "cde", "plastique", "windowsxp", or "macintosh". Style
       
  1514     names are case insensitive.
       
  1515 
       
  1516     Returns 0 if an unknown \a style is passed, otherwise the QStyle object
       
  1517     returned is set as the application's GUI style.
       
  1518 
       
  1519     \warning To ensure that the application's style is set correctly, it is
       
  1520     best to call this function before the QApplication constructor, if
       
  1521     possible.
       
  1522 */
       
  1523 QStyle* QApplication::setStyle(const QString& style)
       
  1524 {
       
  1525     QStyle *s = QStyleFactory::create(style);
       
  1526     if (!s)
       
  1527         return 0;
       
  1528 
       
  1529     setStyle(s);
       
  1530     return s;
       
  1531 }
       
  1532 
       
  1533 /*!
       
  1534     \since 4.5
       
  1535 
       
  1536     Sets the default graphics backend to \a system, which will be used for
       
  1537     on-screen widgets and QPixmaps. The available systems are \c{"native"},
       
  1538     \c{"raster"} and \c{"opengl"}.
       
  1539 
       
  1540     This function call overrides both the application commandline
       
  1541     \c{-graphicssystem} switch and the configure \c{-graphicssystem} switch.
       
  1542 
       
  1543     \warning This function must be called before the QApplication constructor
       
  1544     is called.
       
  1545 
       
  1546     \note The \c{"opengl"} option is currently experimental.
       
  1547 */
       
  1548 
       
  1549 void QApplication::setGraphicsSystem(const QString &system)
       
  1550 {
       
  1551     QApplicationPrivate::graphics_system_name = system;
       
  1552 }
       
  1553 
       
  1554 /*!
       
  1555     Returns the color specification.
       
  1556 
       
  1557     \sa QApplication::setColorSpec()
       
  1558 */
       
  1559 
       
  1560 int QApplication::colorSpec()
       
  1561 {
       
  1562     return QApplicationPrivate::app_cspec;
       
  1563 }
       
  1564 
       
  1565 /*!
       
  1566     Sets the color specification for the application to \a spec.
       
  1567 
       
  1568     The color specification controls how the application allocates colors when
       
  1569     run on a display with a limited amount of colors, e.g. 8 bit / 256 color
       
  1570     displays.
       
  1571 
       
  1572     The color specification must be set before you create the QApplication
       
  1573     object.
       
  1574 
       
  1575     The options are:
       
  1576     \list
       
  1577         \o  QApplication::NormalColor. This is the default color allocation
       
  1578             strategy. Use this option if your application uses buttons, menus,
       
  1579             texts and pixmaps with few colors. With this option, the
       
  1580             application uses system global colors. This works fine for most
       
  1581             applications under X11, but on the Windows platform, it may cause
       
  1582             dithering of non-standard colors.
       
  1583         \o  QApplication::CustomColor. Use this option if your application
       
  1584             needs a small number of custom colors. On X11, this option is the
       
  1585             same as NormalColor. On Windows, Qt creates a Windows palette, and
       
  1586             allocates colors to it on demand.
       
  1587         \o  QApplication::ManyColor. Use this option if your application is
       
  1588             very color hungry, e.g., it requires thousands of colors. \br
       
  1589             Under X11 the effect is:
       
  1590             \list
       
  1591                 \o  For 256-color displays which have at best a 256 color true
       
  1592                     color visual, the default visual is used, and colors are
       
  1593                     allocated from a color cube. The color cube is the 6x6x6
       
  1594                     (216 color) "Web palette" (the red, green, and blue
       
  1595                     components always have one of the following values: 0x00,
       
  1596                     0x33, 0x66, 0x99, 0xCC, or 0xFF), but the number of colors
       
  1597                     can be changed by the \e -ncols option. The user can force
       
  1598                     the application to use the true color visual with the
       
  1599                     \l{QApplication::QApplication()}{-visual} option.
       
  1600                 \o  For 256-color displays which have a true color visual with
       
  1601                     more than 256 colors, use that visual. Silicon Graphics X
       
  1602                     servers this feature, for example. They provide an 8 bit
       
  1603                     visual by default but can deliver true color when asked.
       
  1604             \endlist
       
  1605             On Windows, Qt creates a Windows palette, and fills it with a color
       
  1606             cube.
       
  1607     \endlist
       
  1608 
       
  1609     Be aware that the CustomColor and ManyColor choices may lead to colormap
       
  1610     flashing: The foreground application gets (most) of the available colors,
       
  1611     while the background windows will look less attractive.
       
  1612 
       
  1613     Example:
       
  1614 
       
  1615     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 2
       
  1616 
       
  1617     \sa colorSpec()
       
  1618 */
       
  1619 
       
  1620 void QApplication::setColorSpec(int spec)
       
  1621 {
       
  1622     if (qApp)
       
  1623         qWarning("QApplication::setColorSpec: This function must be "
       
  1624                  "called before the QApplication object is created");
       
  1625     QApplicationPrivate::app_cspec = spec;
       
  1626 }
       
  1627 
       
  1628 /*!
       
  1629     \property QApplication::globalStrut
       
  1630     \brief the minimum size that any GUI element that the user can interact
       
  1631            with should have
       
  1632 
       
  1633     For example, no button should be resized to be smaller than the global
       
  1634     strut size. The strut size should be considered when reimplementing GUI
       
  1635     controls that may be used on touch-screens or similar I/O devices.
       
  1636 
       
  1637     Example:
       
  1638 
       
  1639     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 3
       
  1640 
       
  1641     By default, this property contains a QSize object with zero width and height.
       
  1642 */
       
  1643 QSize QApplication::globalStrut()
       
  1644 {
       
  1645     return QApplicationPrivate::app_strut;
       
  1646 }
       
  1647 
       
  1648 void QApplication::setGlobalStrut(const QSize& strut)
       
  1649 {
       
  1650     QApplicationPrivate::app_strut = strut;
       
  1651 }
       
  1652 
       
  1653 /*!
       
  1654     Returns the application palette.
       
  1655 
       
  1656     \sa setPalette(), QWidget::palette()
       
  1657 */
       
  1658 QPalette QApplication::palette()
       
  1659 {
       
  1660     if (!QApplicationPrivate::app_pal)
       
  1661         QApplicationPrivate::app_pal = new QPalette(Qt::black);
       
  1662     return *QApplicationPrivate::app_pal;
       
  1663 }
       
  1664 
       
  1665 /*!
       
  1666     \fn QPalette QApplication::palette(const QWidget* widget)
       
  1667     \overload
       
  1668 
       
  1669     If a \a widget is passed, the default palette for the widget's class is
       
  1670     returned. This may or may not be the application palette. In most cases
       
  1671     there is no special palette for certain types of widgets, but one notable
       
  1672     exception is the popup menu under Windows, if the user has defined a
       
  1673     special background color for menus in the display settings.
       
  1674 
       
  1675     \sa setPalette(), QWidget::palette()
       
  1676 */
       
  1677 QPalette QApplication::palette(const QWidget* w)
       
  1678 {
       
  1679     PaletteHash *hash = app_palettes();
       
  1680     if (w && hash && hash->size()) {
       
  1681         QHash<QByteArray, QPalette>::ConstIterator it = hash->constFind(w->metaObject()->className());
       
  1682         if (it != hash->constEnd())
       
  1683             return *it;
       
  1684         for (it = hash->constBegin(); it != hash->constEnd(); ++it) {
       
  1685             if (w->inherits(it.key()))
       
  1686                 return it.value();
       
  1687         }
       
  1688     }
       
  1689     return palette();
       
  1690 }
       
  1691 
       
  1692 /*!
       
  1693     \overload
       
  1694 
       
  1695     Returns the palette for widgets of the given \a className.
       
  1696 
       
  1697     \sa setPalette(), QWidget::palette()
       
  1698 */
       
  1699 QPalette QApplication::palette(const char *className)
       
  1700 {
       
  1701     if (!QApplicationPrivate::app_pal)
       
  1702         palette();
       
  1703     PaletteHash *hash = app_palettes();
       
  1704     if (className && hash && hash->size()) {
       
  1705         QHash<QByteArray, QPalette>::ConstIterator it = hash->constFind(className);
       
  1706         if (it != hash->constEnd())
       
  1707             return *it;
       
  1708     }
       
  1709     return *QApplicationPrivate::app_pal;
       
  1710 }
       
  1711 
       
  1712 void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char* className, bool clearWidgetPaletteHash)
       
  1713 {
       
  1714     QPalette pal = palette;
       
  1715 
       
  1716     if (QApplicationPrivate::app_style)
       
  1717         QApplicationPrivate::app_style->polish(pal); // NB: non-const reference
       
  1718 
       
  1719     bool all = false;
       
  1720     PaletteHash *hash = app_palettes();
       
  1721     if (!className) {
       
  1722         if (QApplicationPrivate::app_pal && pal.isCopyOf(*QApplicationPrivate::app_pal))
       
  1723             return;
       
  1724         if (!QApplicationPrivate::app_pal)
       
  1725             QApplicationPrivate::app_pal = new QPalette(pal);
       
  1726         else
       
  1727             *QApplicationPrivate::app_pal = pal;
       
  1728         if (hash && hash->size()) {
       
  1729             all = true;
       
  1730             if (clearWidgetPaletteHash)
       
  1731                 hash->clear();
       
  1732         }
       
  1733     } else if (hash) {
       
  1734         hash->insert(className, pal);
       
  1735     }
       
  1736 
       
  1737     if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {
       
  1738         // Send ApplicationPaletteChange to qApp itself, and to the widgets.
       
  1739         QEvent e(QEvent::ApplicationPaletteChange);
       
  1740         QApplication::sendEvent(QApplication::instance(), &e);
       
  1741 
       
  1742         QWidgetList wids = QApplication::allWidgets();
       
  1743         for (QWidgetList::ConstIterator it = wids.constBegin(); it != wids.constEnd(); ++it) {
       
  1744             register QWidget *w = *it;
       
  1745             if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class
       
  1746                 QApplication::sendEvent(w, &e);
       
  1747         }
       
  1748 
       
  1749         // Send to all scenes as well.
       
  1750 #ifndef QT_NO_GRAPHICSVIEW
       
  1751         QList<QGraphicsScene *> &scenes = qApp->d_func()->scene_list;
       
  1752         for (QList<QGraphicsScene *>::ConstIterator it = scenes.constBegin();
       
  1753              it != scenes.constEnd(); ++it) {
       
  1754             QApplication::sendEvent(*it, &e);
       
  1755         }
       
  1756 #endif //QT_NO_GRAPHICSVIEW
       
  1757     }
       
  1758     if (!className && (!QApplicationPrivate::sys_pal || !palette.isCopyOf(*QApplicationPrivate::sys_pal))) {
       
  1759         if (!QApplicationPrivate::set_pal)
       
  1760             QApplicationPrivate::set_pal = new QPalette(palette);
       
  1761         else
       
  1762             *QApplicationPrivate::set_pal = palette;
       
  1763     }
       
  1764 }
       
  1765 
       
  1766 /*!
       
  1767     Changes the default application palette to \a palette.
       
  1768 
       
  1769     If \a className is passed, the change applies only to widgets that inherit
       
  1770     \a className (as reported by QObject::inherits()). If \a className is left
       
  1771     0, the change affects all widgets, thus overriding any previously set class
       
  1772     specific palettes.
       
  1773 
       
  1774     The palette may be changed according to the current GUI style in
       
  1775     QStyle::polish().
       
  1776 
       
  1777     \warning Do not use this function in conjunction with \l{Qt Style Sheets}.
       
  1778     When using style sheets, the palette of a widget can be customized using
       
  1779     the "color", "background-color", "selection-color",
       
  1780     "selection-background-color" and "alternate-background-color".
       
  1781 
       
  1782     \note Some styles do not use the palette for all drawing, for instance, if
       
  1783     they make use of native theme engines. This is the case for the Windows XP,
       
  1784     Windows Vista, and Mac OS X styles.
       
  1785 
       
  1786     \sa QWidget::setPalette(), palette(), QStyle::polish()
       
  1787 */
       
  1788 
       
  1789 void QApplication::setPalette(const QPalette &palette, const char* className)
       
  1790 {
       
  1791     QApplicationPrivate::setPalette_helper(palette, className, /*clearWidgetPaletteHash=*/ true);
       
  1792 }
       
  1793 
       
  1794 
       
  1795 
       
  1796 void QApplicationPrivate::setSystemPalette(const QPalette &pal)
       
  1797 {
       
  1798     QPalette adjusted;
       
  1799 
       
  1800 #if 0
       
  1801     // adjust the system palette to avoid dithering
       
  1802     QColormap cmap = QColormap::instance();
       
  1803     if (cmap.depths() > 4 && cmap.depths() < 24) {
       
  1804         for (int g = 0; g < QPalette::NColorGroups; g++)
       
  1805             for (int i = 0; i < QPalette::NColorRoles; i++) {
       
  1806                 QColor color = pal.color((QPalette::ColorGroup)g, (QPalette::ColorRole)i);
       
  1807                 color = cmap.colorAt(cmap.pixel(color));
       
  1808                 adjusted.setColor((QPalette::ColorGroup)g, (QPalette::ColorRole) i, color);
       
  1809             }
       
  1810     }
       
  1811 #else
       
  1812     adjusted = pal;
       
  1813 #endif
       
  1814 
       
  1815     if (!sys_pal)
       
  1816         sys_pal = new QPalette(adjusted);
       
  1817     else
       
  1818         *sys_pal = adjusted;
       
  1819 
       
  1820 
       
  1821     if (!QApplicationPrivate::set_pal)
       
  1822         QApplication::setPalette(*sys_pal);
       
  1823 }
       
  1824 
       
  1825 /*!
       
  1826     Returns the default application font.
       
  1827 
       
  1828     \sa fontMetrics(), QWidget::font()
       
  1829 */
       
  1830 QFont QApplication::font()
       
  1831 {
       
  1832     QMutexLocker locker(applicationFontMutex());
       
  1833     if (!QApplicationPrivate::app_font)
       
  1834         QApplicationPrivate::app_font = new QFont(QLatin1String("Helvetica"));
       
  1835     return *QApplicationPrivate::app_font;
       
  1836 }
       
  1837 
       
  1838 /*!
       
  1839     \overload
       
  1840 
       
  1841     Returns the default font for the \a widget.
       
  1842 
       
  1843     \sa fontMetrics(), QWidget::setFont()
       
  1844 */
       
  1845 
       
  1846 QFont QApplication::font(const QWidget *widget)
       
  1847 {
       
  1848     FontHash *hash = app_fonts();
       
  1849 
       
  1850 #ifdef Q_WS_MAC
       
  1851     // short circuit for small and mini controls
       
  1852     if (widget->testAttribute(Qt::WA_MacSmallSize)) {
       
  1853         return hash->value("QSmallFont");
       
  1854     } else if (widget->testAttribute(Qt::WA_MacMiniSize)) {
       
  1855         return hash->value("QMiniFont");
       
  1856     }
       
  1857 #endif
       
  1858     if (widget && hash  && hash->size()) {
       
  1859         QHash<QByteArray, QFont>::ConstIterator it =
       
  1860                 hash->constFind(widget->metaObject()->className());
       
  1861         if (it != hash->constEnd())
       
  1862             return it.value();
       
  1863         for (it = hash->constBegin(); it != hash->constEnd(); ++it) {
       
  1864             if (widget->inherits(it.key()))
       
  1865                 return it.value();
       
  1866         }
       
  1867     }
       
  1868     return font();
       
  1869 }
       
  1870 
       
  1871 /*!
       
  1872     \overload
       
  1873 
       
  1874     Returns the font for widgets of the given \a className.
       
  1875 
       
  1876     \sa setFont(), QWidget::font()
       
  1877 */
       
  1878 QFont QApplication::font(const char *className)
       
  1879 {
       
  1880     FontHash *hash = app_fonts();
       
  1881     if (className && hash && hash->size()) {
       
  1882         QHash<QByteArray, QFont>::ConstIterator it = hash->constFind(className);
       
  1883         if (it != hash->constEnd())
       
  1884             return *it;
       
  1885     }
       
  1886     return font();
       
  1887 }
       
  1888 
       
  1889 
       
  1890 /*!
       
  1891     Changes the default application font to \a font. If \a className is passed,
       
  1892     the change applies only to classes that inherit \a className (as reported
       
  1893     by QObject::inherits()).
       
  1894 
       
  1895     On application start-up, the default font depends on the window system. It
       
  1896     can vary depending on both the window system version and the locale. This
       
  1897     function lets you override the default font; but overriding may be a bad
       
  1898     idea because, for example, some locales need extra large fonts to support
       
  1899     their special characters.
       
  1900 
       
  1901     \warning Do not use this function in conjunction with \l{Qt Style Sheets}.
       
  1902     The font of an application can be customized using the "font" style sheet
       
  1903     property. To set a bold font for all QPushButtons, set the application
       
  1904     styleSheet() as "QPushButton { font: bold }"
       
  1905 
       
  1906     \sa font(), fontMetrics(), QWidget::setFont()
       
  1907 */
       
  1908 
       
  1909 void QApplication::setFont(const QFont &font, const char *className)
       
  1910 {
       
  1911     bool all = false;
       
  1912     FontHash *hash = app_fonts();
       
  1913     if (!className) {
       
  1914         QMutexLocker locker(applicationFontMutex());
       
  1915         if (!QApplicationPrivate::app_font)
       
  1916             QApplicationPrivate::app_font = new QFont(font);
       
  1917         else
       
  1918             *QApplicationPrivate::app_font = font;
       
  1919         if (hash && hash->size()) {
       
  1920             all = true;
       
  1921             hash->clear();
       
  1922         }
       
  1923     } else if (hash) {
       
  1924         hash->insert(className, font);
       
  1925     }
       
  1926     if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {
       
  1927         // Send ApplicationFontChange to qApp itself, and to the widgets.
       
  1928         QEvent e(QEvent::ApplicationFontChange);
       
  1929         QApplication::sendEvent(QApplication::instance(), &e);
       
  1930 
       
  1931         QWidgetList wids = QApplication::allWidgets();
       
  1932         for (QWidgetList::ConstIterator it = wids.constBegin(); it != wids.constEnd(); ++it) {
       
  1933             register QWidget *w = *it;
       
  1934             if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class
       
  1935                 sendEvent(w, &e);
       
  1936         }
       
  1937 
       
  1938 #ifndef QT_NO_GRAPHICSVIEW
       
  1939         // Send to all scenes as well.
       
  1940         QList<QGraphicsScene *> &scenes = qApp->d_func()->scene_list;
       
  1941         for (QList<QGraphicsScene *>::ConstIterator it = scenes.constBegin();
       
  1942              it != scenes.constEnd(); ++it) {
       
  1943             QApplication::sendEvent(*it, &e);
       
  1944         }
       
  1945 #endif //QT_NO_GRAPHICSVIEW
       
  1946     }
       
  1947     if (!className && (!QApplicationPrivate::sys_font || !font.isCopyOf(*QApplicationPrivate::sys_font))) {
       
  1948         if (!QApplicationPrivate::set_font)
       
  1949             QApplicationPrivate::set_font = new QFont(font);
       
  1950         else
       
  1951             *QApplicationPrivate::set_font = font;
       
  1952     }
       
  1953 }
       
  1954 
       
  1955 /*! \internal
       
  1956 */
       
  1957 void QApplicationPrivate::setSystemFont(const QFont &font)
       
  1958 {
       
  1959      if (!sys_font)
       
  1960         sys_font = new QFont(font);
       
  1961     else
       
  1962         *sys_font = font;
       
  1963 
       
  1964     if (!QApplicationPrivate::set_font)
       
  1965         QApplication::setFont(*sys_font);
       
  1966 }
       
  1967 
       
  1968 /*! \internal
       
  1969 */
       
  1970 QString QApplicationPrivate::desktopStyleKey()
       
  1971 {
       
  1972     return qt_guiPlatformPlugin()->styleName();
       
  1973 }
       
  1974 
       
  1975 /*!
       
  1976     \property QApplication::windowIcon
       
  1977     \brief the default window icon
       
  1978 
       
  1979     \sa QWidget::setWindowIcon(), {Setting the Application Icon}
       
  1980 */
       
  1981 QIcon QApplication::windowIcon()
       
  1982 {
       
  1983     return QApplicationPrivate::app_icon ? *QApplicationPrivate::app_icon : QIcon();
       
  1984 }
       
  1985 
       
  1986 void QApplication::setWindowIcon(const QIcon &icon)
       
  1987 {
       
  1988     if (!QApplicationPrivate::app_icon)
       
  1989         QApplicationPrivate::app_icon = new QIcon();
       
  1990     *QApplicationPrivate::app_icon = icon;
       
  1991     if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {
       
  1992 #ifdef Q_WS_MAC
       
  1993         void qt_mac_set_app_icon(const QPixmap &); //qapplication_mac.cpp
       
  1994         QSize size = QApplicationPrivate::app_icon->actualSize(QSize(128, 128));
       
  1995         qt_mac_set_app_icon(QApplicationPrivate::app_icon->pixmap(size));
       
  1996 #endif
       
  1997         QEvent e(QEvent::ApplicationWindowIconChange);
       
  1998         QWidgetList all = QApplication::allWidgets();
       
  1999         for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) {
       
  2000             register QWidget *w = *it;
       
  2001             if (w->isWindow())
       
  2002                 sendEvent(w, &e);
       
  2003         }
       
  2004     }
       
  2005 }
       
  2006 
       
  2007 /*!
       
  2008     Returns a list of the top-level widgets (windows) in the application.
       
  2009 
       
  2010     \note Some of the top-level widgets may be hidden, for example a tooltip if
       
  2011     no tooltip is currently shown.
       
  2012 
       
  2013     Example:
       
  2014 
       
  2015     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 4
       
  2016 
       
  2017     \sa allWidgets(), QWidget::isWindow(), QWidget::isHidden()
       
  2018 */
       
  2019 QWidgetList QApplication::topLevelWidgets()
       
  2020 {
       
  2021     QWidgetList list;
       
  2022     QWidgetList all = allWidgets();
       
  2023 
       
  2024     for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) {
       
  2025         QWidget *w = *it;
       
  2026         if (w->isWindow() && w->windowType() != Qt::Desktop)
       
  2027             list.append(w);
       
  2028     }
       
  2029     return list;
       
  2030 }
       
  2031 
       
  2032 /*!
       
  2033     Returns a list of all the widgets in the application.
       
  2034 
       
  2035     The list is empty (QList::isEmpty()) if there are no widgets.
       
  2036 
       
  2037     \note Some of the widgets may be hidden.
       
  2038 
       
  2039     Example:
       
  2040     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 5
       
  2041 
       
  2042     \sa topLevelWidgets(), QWidget::isVisible()
       
  2043 */
       
  2044 
       
  2045 QWidgetList QApplication::allWidgets()
       
  2046 {
       
  2047     if (QWidgetPrivate::allWidgets)
       
  2048         return QWidgetPrivate::allWidgets->toList();
       
  2049     return QWidgetList();
       
  2050 }
       
  2051 
       
  2052 /*!
       
  2053     Returns the application widget that has the keyboard input focus, or 0 if
       
  2054     no widget in this application has the focus.
       
  2055 
       
  2056     \sa QWidget::setFocus(), QWidget::hasFocus(), activeWindow(), focusChanged()
       
  2057 */
       
  2058 
       
  2059 QWidget *QApplication::focusWidget()
       
  2060 {
       
  2061     return QApplicationPrivate::focus_widget;
       
  2062 }
       
  2063 
       
  2064 void QApplicationPrivate::setFocusWidget(QWidget *focus, Qt::FocusReason reason)
       
  2065 {
       
  2066 #ifndef QT_NO_GRAPHICSVIEW
       
  2067     if (focus && focus->window()->graphicsProxyWidget())
       
  2068         return;
       
  2069 #endif
       
  2070 
       
  2071     hidden_focus_widget = 0;
       
  2072 
       
  2073     if (focus != focus_widget) {
       
  2074         if (focus && focus->isHidden()) {
       
  2075             hidden_focus_widget = focus;
       
  2076             return;
       
  2077         }
       
  2078 
       
  2079         if (focus && (reason == Qt::BacktabFocusReason || reason == Qt::TabFocusReason)
       
  2080             && qt_in_tab_key_event)
       
  2081             focus->window()->setAttribute(Qt::WA_KeyboardFocusChange);
       
  2082         else if (focus && reason == Qt::ShortcutFocusReason) {
       
  2083             focus->window()->setAttribute(Qt::WA_KeyboardFocusChange);
       
  2084         }
       
  2085         QWidget *prev = focus_widget;
       
  2086         focus_widget = focus;
       
  2087 #ifndef QT_NO_IM
       
  2088         if (prev && ((reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
       
  2089             && prev->testAttribute(Qt::WA_InputMethodEnabled))
       
  2090             // Do reset the input context, in case the new focus widget won't accept keyboard input
       
  2091             // or it is not created fully yet.
       
  2092             || (focus_widget && (!focus_widget->testAttribute(Qt::WA_InputMethodEnabled)
       
  2093             || !focus_widget->testAttribute(Qt::WA_WState_Created))))) {
       
  2094              QInputContext *qic = prev->inputContext();
       
  2095             if(qic) {
       
  2096                 qic->reset();
       
  2097                 qic->setFocusWidget(0);
       
  2098             }
       
  2099         }
       
  2100 #endif //QT_NO_IM
       
  2101 
       
  2102         if(focus_widget)
       
  2103             focus_widget->d_func()->setFocus_sys();
       
  2104 
       
  2105         if (reason != Qt::NoFocusReason) {
       
  2106 
       
  2107             //send events
       
  2108             if (prev) {
       
  2109 #ifdef QT_KEYPAD_NAVIGATION
       
  2110                 if (QApplication::keypadNavigationEnabled()) {
       
  2111                     if (prev->hasEditFocus() && reason != Qt::PopupFocusReason
       
  2112 #ifdef Q_OS_SYMBIAN
       
  2113                             && reason != Qt::ActiveWindowFocusReason
       
  2114 #endif
       
  2115                             )
       
  2116                         prev->setEditFocus(false);
       
  2117                 }
       
  2118 #endif
       
  2119 #ifndef QT_NO_IM
       
  2120                 if (focus) {
       
  2121                     QInputContext *prevIc;
       
  2122                     prevIc = prev->inputContext();
       
  2123                     if (prevIc && prevIc != focus->inputContext()) {
       
  2124                         QEvent closeSIPEvent(QEvent::CloseSoftwareInputPanel);
       
  2125                         QApplication::sendEvent(prev, &closeSIPEvent);
       
  2126                     }
       
  2127                 }
       
  2128 #endif
       
  2129                 QFocusEvent out(QEvent::FocusOut, reason);
       
  2130                 QPointer<QWidget> that = prev;
       
  2131                 QApplication::sendEvent(prev, &out);
       
  2132                 if (that)
       
  2133                     QApplication::sendEvent(that->style(), &out);
       
  2134             }
       
  2135             if(focus && QApplicationPrivate::focus_widget == focus) {
       
  2136 #ifndef QT_NO_IM
       
  2137                 if (focus->testAttribute(Qt::WA_InputMethodEnabled)) {
       
  2138                     QInputContext *qic = focus->inputContext();
       
  2139                     if (qic && focus->testAttribute(Qt::WA_WState_Created)
       
  2140                         && focus->isEnabled())
       
  2141                         qic->setFocusWidget(focus);
       
  2142                 }
       
  2143 #endif //QT_NO_IM
       
  2144                 QFocusEvent in(QEvent::FocusIn, reason);
       
  2145                 QPointer<QWidget> that = focus;
       
  2146                 QApplication::sendEvent(focus, &in);
       
  2147                 if (that)
       
  2148                     QApplication::sendEvent(that->style(), &in);
       
  2149             }
       
  2150             emit qApp->focusChanged(prev, focus_widget);
       
  2151         }
       
  2152     }
       
  2153 }
       
  2154 
       
  2155 
       
  2156 /*!
       
  2157     Returns the application top-level window that has the keyboard input focus,
       
  2158     or 0 if no application window has the focus. There might be an
       
  2159     activeWindow() even if there is no focusWidget(), for example if no widget
       
  2160     in that window accepts key events.
       
  2161 
       
  2162     \sa QWidget::setFocus(), QWidget::hasFocus(), focusWidget()
       
  2163 */
       
  2164 
       
  2165 QWidget *QApplication::activeWindow()
       
  2166 {
       
  2167     return QApplicationPrivate::active_window;
       
  2168 }
       
  2169 
       
  2170 /*!
       
  2171     Returns display (screen) font metrics for the application font.
       
  2172 
       
  2173     \sa font(), setFont(), QWidget::fontMetrics(), QPainter::fontMetrics()
       
  2174 */
       
  2175 
       
  2176 QFontMetrics QApplication::fontMetrics()
       
  2177 {
       
  2178     return desktop()->fontMetrics();
       
  2179 }
       
  2180 
       
  2181 
       
  2182 /*!
       
  2183     Closes all top-level windows.
       
  2184 
       
  2185     This function is particularly useful for applications with many top-level
       
  2186     windows. It could, for example, be connected to a \gui{Exit} entry in the
       
  2187     \gui{File} menu:
       
  2188 
       
  2189     \snippet examples/mainwindows/mdi/mainwindow.cpp 0
       
  2190 
       
  2191     The windows are closed in random order, until one window does not accept
       
  2192     the close event. The application quits when the last window was
       
  2193     successfully closed; this can be turned off by setting
       
  2194     \l quitOnLastWindowClosed to false.
       
  2195 
       
  2196     \sa quitOnLastWindowClosed, lastWindowClosed(), QWidget::close(),
       
  2197     QWidget::closeEvent(), lastWindowClosed(), quit(), topLevelWidgets(),
       
  2198     QWidget::isWindow()
       
  2199 */
       
  2200 void QApplication::closeAllWindows()
       
  2201 {
       
  2202     bool did_close = true;
       
  2203     QWidget *w;
       
  2204     while((w = activeModalWidget()) && did_close) {
       
  2205         if(!w->isVisible())
       
  2206             break;
       
  2207         did_close = w->close();
       
  2208     }
       
  2209     QWidgetList list = QApplication::topLevelWidgets();
       
  2210     for (int i = 0; did_close && i < list.size(); ++i) {
       
  2211         w = list.at(i);
       
  2212         if (w->isVisible() && w->windowType() != Qt::Desktop) {
       
  2213             did_close = w->close();
       
  2214             list = QApplication::topLevelWidgets();
       
  2215             i = -1;
       
  2216         }
       
  2217     }
       
  2218 }
       
  2219 
       
  2220 /*!
       
  2221     Displays a simple message box about Qt. The message includes the version
       
  2222     number of Qt being used by the application.
       
  2223 
       
  2224     This is useful for inclusion in the \gui Help menu of an application, as
       
  2225     shown in the \l{mainwindows/menus}{Menus} example.
       
  2226 
       
  2227     This function is a convenience slot for QMessageBox::aboutQt().
       
  2228 */
       
  2229 void QApplication::aboutQt()
       
  2230 {
       
  2231 #ifndef QT_NO_MESSAGEBOX
       
  2232     QMessageBox::aboutQt(
       
  2233 #ifdef Q_WS_MAC
       
  2234             0
       
  2235 #else
       
  2236             activeWindow()
       
  2237 #endif // Q_WS_MAC
       
  2238             );
       
  2239 #endif // QT_NO_MESSAGEBOX
       
  2240 }
       
  2241 
       
  2242 
       
  2243 /*!
       
  2244     \fn void QApplication::lastWindowClosed()
       
  2245 
       
  2246     This signal is emitted from QApplication::exec() when the last visible
       
  2247     primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose
       
  2248     attribute set is closed.
       
  2249 
       
  2250     By default,
       
  2251 
       
  2252     \list
       
  2253         \o  this attribute is set for all widgets except transient windows such
       
  2254             as splash screens, tool windows, and popup menus
       
  2255 
       
  2256         \o  QApplication implicitly quits when this signal is emitted.
       
  2257     \endlist
       
  2258 
       
  2259     This feature can be turned off by setting \l quitOnLastWindowClosed to
       
  2260     false.
       
  2261 
       
  2262     \sa QWidget::close()
       
  2263 */
       
  2264 
       
  2265 /*!
       
  2266     \since 4.1
       
  2267     \fn void QApplication::focusChanged(QWidget *old, QWidget *now)
       
  2268 
       
  2269     This signal is emitted when the widget that has keyboard focus changed from
       
  2270     \a old to \a now, i.e., because the user pressed the tab-key, clicked into
       
  2271     a widget or changed the active window. Both \a old and \a now can be the
       
  2272     null-pointer.
       
  2273 
       
  2274     The signal is emitted after both widget have been notified about the change
       
  2275     through QFocusEvent.
       
  2276 
       
  2277     \sa QWidget::setFocus(), QWidget::clearFocus(), Qt::FocusReason
       
  2278 */
       
  2279 
       
  2280 /*!
       
  2281     \since 4.5
       
  2282     \fn void QApplication::fontDatabaseChanged()
       
  2283 
       
  2284     This signal is emitted when application fonts are loaded or removed.
       
  2285 
       
  2286     \sa QFontDatabase::addApplicationFont(),
       
  2287     QFontDatabase::addApplicationFontFromData(),
       
  2288     QFontDatabase::removeAllApplicationFonts(),
       
  2289     QFontDatabase::removeApplicationFont()
       
  2290 */
       
  2291 
       
  2292 #ifndef QT_NO_TRANSLATION
       
  2293 static bool qt_detectRTLLanguage()
       
  2294 {
       
  2295     return force_reverse ^
       
  2296         (QApplication::tr("QT_LAYOUT_DIRECTION",
       
  2297                          "Translate this string to the string 'LTR' in left-to-right"
       
  2298                          " languages or to 'RTL' in right-to-left languages (such as Hebrew"
       
  2299                          " and Arabic) to get proper widget layout.") == QLatin1String("RTL"));
       
  2300 }
       
  2301 #endif
       
  2302 
       
  2303 /*!\reimp
       
  2304 
       
  2305 */
       
  2306 bool QApplication::event(QEvent *e)
       
  2307 {
       
  2308     Q_D(QApplication);
       
  2309     if(e->type() == QEvent::Close) {
       
  2310         QCloseEvent *ce = static_cast<QCloseEvent*>(e);
       
  2311         ce->accept();
       
  2312         closeAllWindows();
       
  2313 
       
  2314         QWidgetList list = topLevelWidgets();
       
  2315         for (int i = 0; i < list.size(); ++i) {
       
  2316             QWidget *w = list.at(i);
       
  2317             if (w->isVisible() && !(w->windowType() == Qt::Desktop) && !(w->windowType() == Qt::Popup) &&
       
  2318                  (!(w->windowType() == Qt::Dialog) || !w->parentWidget())) {
       
  2319                 ce->ignore();
       
  2320                 break;
       
  2321             }
       
  2322         }
       
  2323         if(ce->isAccepted())
       
  2324             return true;
       
  2325     } else if(e->type() == QEvent::LanguageChange) {
       
  2326 #ifndef QT_NO_TRANSLATION
       
  2327         setLayoutDirection(qt_detectRTLLanguage()?Qt::RightToLeft:Qt::LeftToRight);
       
  2328 #endif
       
  2329         QWidgetList list = topLevelWidgets();
       
  2330         for (int i = 0; i < list.size(); ++i) {
       
  2331             QWidget *w = list.at(i);
       
  2332             if (!(w->windowType() == Qt::Desktop))
       
  2333                 postEvent(w, new QEvent(QEvent::LanguageChange));
       
  2334         }
       
  2335     } else if (e->type() == QEvent::Timer) {
       
  2336         QTimerEvent *te = static_cast<QTimerEvent*>(e);
       
  2337         Q_ASSERT(te != 0);
       
  2338         if (te->timerId() == d->toolTipWakeUp.timerId()) {
       
  2339             d->toolTipWakeUp.stop();
       
  2340             if (d->toolTipWidget) {
       
  2341                 QWidget *w = d->toolTipWidget->window();
       
  2342                 // show tooltip if WA_AlwaysShowToolTips is set, or if
       
  2343                 // any ancestor of d->toolTipWidget is the active
       
  2344                 // window
       
  2345                 bool showToolTip = w->testAttribute(Qt::WA_AlwaysShowToolTips);
       
  2346                 while (w && !showToolTip) {
       
  2347                     showToolTip = w->isActiveWindow();
       
  2348                     w = w->parentWidget();
       
  2349                     w = w ? w->window() : 0;
       
  2350                 }
       
  2351                 if (showToolTip) {
       
  2352                     QHelpEvent e(QEvent::ToolTip, d->toolTipPos, d->toolTipGlobalPos);
       
  2353                     QApplication::sendEvent(d->toolTipWidget, &e);
       
  2354                     if (e.isAccepted())
       
  2355                         d->toolTipFallAsleep.start(2000, this);
       
  2356                 }
       
  2357             }
       
  2358         } else if (te->timerId() == d->toolTipFallAsleep.timerId()) {
       
  2359             d->toolTipFallAsleep.stop();
       
  2360         }
       
  2361     }
       
  2362     return QCoreApplication::event(e);
       
  2363 }
       
  2364 #if !defined(Q_WS_X11)
       
  2365 
       
  2366 // The doc and X implementation of this function is in qapplication_x11.cpp
       
  2367 
       
  2368 void QApplication::syncX()        {}                // do nothing
       
  2369 
       
  2370 #endif
       
  2371 
       
  2372 /*!
       
  2373     \fn Qt::WindowsVersion QApplication::winVersion()
       
  2374 
       
  2375     Use \l QSysInfo::WindowsVersion instead.
       
  2376 */
       
  2377 
       
  2378 /*!
       
  2379     \fn void QApplication::setActiveWindow(QWidget* active)
       
  2380 
       
  2381     Sets the active window to the \a active widget in response to a system
       
  2382     event. The function is called from the platform specific event handlers.
       
  2383 
       
  2384     \warning This function does \e not set the keyboard focus to the active
       
  2385     widget. Call QWidget::activateWindow() instead.
       
  2386 
       
  2387     It sets the activeWindow() and focusWidget() attributes and sends proper
       
  2388     \l{QEvent::WindowActivate}{WindowActivate}/\l{QEvent::WindowDeactivate}
       
  2389     {WindowDeactivate} and \l{QEvent::FocusIn}{FocusIn}/\l{QEvent::FocusOut}
       
  2390     {FocusOut} events to all appropriate widgets. The window will then be
       
  2391     painted in active state (e.g. cursors in line edits will blink), and it
       
  2392     will have tool tips enabled.
       
  2393 
       
  2394     \sa activeWindow(), QWidget::activateWindow()
       
  2395 */
       
  2396 void QApplication::setActiveWindow(QWidget* act)
       
  2397 {
       
  2398     QWidget* window = act?act->window():0;
       
  2399 
       
  2400     if (QApplicationPrivate::active_window == window)
       
  2401         return;
       
  2402 
       
  2403 #ifndef QT_NO_GRAPHICSVIEW
       
  2404     if (window && window->graphicsProxyWidget()) {
       
  2405         // Activate the proxy's view->viewport() ?
       
  2406         return;
       
  2407     }
       
  2408 #endif
       
  2409 
       
  2410     QWidgetList toBeActivated;
       
  2411     QWidgetList toBeDeactivated;
       
  2412 
       
  2413     if (QApplicationPrivate::active_window) {
       
  2414         if (style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, QApplicationPrivate::active_window)) {
       
  2415             QWidgetList list = topLevelWidgets();
       
  2416             for (int i = 0; i < list.size(); ++i) {
       
  2417                 QWidget *w = list.at(i);
       
  2418                 if (w->isVisible() && w->isActiveWindow())
       
  2419                     toBeDeactivated.append(w);
       
  2420             }
       
  2421         } else {
       
  2422             toBeDeactivated.append(QApplicationPrivate::active_window);
       
  2423         }
       
  2424     }
       
  2425 
       
  2426 #if !defined(Q_WS_MAC)
       
  2427     QWidget *previousActiveWindow =  QApplicationPrivate::active_window;
       
  2428 #endif
       
  2429     QApplicationPrivate::active_window = window;
       
  2430 
       
  2431     if (QApplicationPrivate::active_window) {
       
  2432         if (style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, QApplicationPrivate::active_window)) {
       
  2433             QWidgetList list = topLevelWidgets();
       
  2434             for (int i = 0; i < list.size(); ++i) {
       
  2435                 QWidget *w = list.at(i);
       
  2436                 if (w->isVisible() && w->isActiveWindow())
       
  2437                     toBeActivated.append(w);
       
  2438             }
       
  2439         } else {
       
  2440             toBeActivated.append(QApplicationPrivate::active_window);
       
  2441         }
       
  2442 
       
  2443     }
       
  2444 
       
  2445     // first the activation/deactivation events
       
  2446     QEvent activationChange(QEvent::ActivationChange);
       
  2447     QEvent windowActivate(QEvent::WindowActivate);
       
  2448     QEvent windowDeactivate(QEvent::WindowDeactivate);
       
  2449 
       
  2450 #if !defined(Q_WS_MAC)
       
  2451     if (!previousActiveWindow) {
       
  2452         QEvent appActivate(QEvent::ApplicationActivate);
       
  2453         sendSpontaneousEvent(qApp, &appActivate);
       
  2454     }
       
  2455 #endif
       
  2456 
       
  2457     for (int i = 0; i < toBeActivated.size(); ++i) {
       
  2458         QWidget *w = toBeActivated.at(i);
       
  2459         sendSpontaneousEvent(w, &windowActivate);
       
  2460         sendSpontaneousEvent(w, &activationChange);
       
  2461     }
       
  2462 
       
  2463     for(int i = 0; i < toBeDeactivated.size(); ++i) {
       
  2464         QWidget *w = toBeDeactivated.at(i);
       
  2465         sendSpontaneousEvent(w, &windowDeactivate);
       
  2466         sendSpontaneousEvent(w, &activationChange);
       
  2467     }
       
  2468 
       
  2469 #if !defined(Q_WS_MAC)
       
  2470     if (!QApplicationPrivate::active_window) {
       
  2471         QEvent appDeactivate(QEvent::ApplicationDeactivate);
       
  2472         sendSpontaneousEvent(qApp, &appDeactivate);
       
  2473     }
       
  2474 #endif
       
  2475 
       
  2476     if (QApplicationPrivate::popupWidgets == 0) { // !inPopupMode()
       
  2477         // then focus events
       
  2478         if (!QApplicationPrivate::active_window && QApplicationPrivate::focus_widget) {
       
  2479             QApplicationPrivate::setFocusWidget(0, Qt::ActiveWindowFocusReason);
       
  2480         } else if (QApplicationPrivate::active_window) {
       
  2481             QWidget *w = QApplicationPrivate::active_window->focusWidget();
       
  2482             if (w && w->isVisible() /*&& w->focusPolicy() != QWidget::NoFocus*/)
       
  2483                 w->setFocus(Qt::ActiveWindowFocusReason);
       
  2484             else {
       
  2485                 w = QApplicationPrivate::focusNextPrevChild_helper(QApplicationPrivate::active_window, true);
       
  2486                 if (w) {
       
  2487                     w->setFocus(Qt::ActiveWindowFocusReason);
       
  2488                 } else {
       
  2489                     // If the focus widget is not in the activate_window, clear the focus
       
  2490                     w = QApplicationPrivate::focus_widget;
       
  2491                     if (!w && QApplicationPrivate::active_window->focusPolicy() != Qt::NoFocus)
       
  2492                         QApplicationPrivate::setFocusWidget(QApplicationPrivate::active_window, Qt::ActiveWindowFocusReason);
       
  2493                     else if (!QApplicationPrivate::active_window->isAncestorOf(w))
       
  2494                         QApplicationPrivate::setFocusWidget(0, Qt::ActiveWindowFocusReason);
       
  2495                 }
       
  2496             }
       
  2497         }
       
  2498     }
       
  2499 }
       
  2500 
       
  2501 /*!internal
       
  2502  * Helper function that returns the new focus widget, but does not set the focus reason.
       
  2503  * Returns 0 if a new focus widget could not be found.
       
  2504  * Shared with QGraphicsProxyWidgetPrivate::findFocusChild()
       
  2505 */
       
  2506 QWidget *QApplicationPrivate::focusNextPrevChild_helper(QWidget *toplevel, bool next)
       
  2507 {
       
  2508     uint focus_flag = qt_tab_all_widgets ? Qt::TabFocus : Qt::StrongFocus;
       
  2509 
       
  2510     QWidget *f = toplevel->focusWidget();
       
  2511     if (!f)
       
  2512         f = toplevel;
       
  2513 
       
  2514     QWidget *w = f;
       
  2515     QWidget *test = f->d_func()->focus_next;
       
  2516     while (test && test != f) {
       
  2517         if ((test->focusPolicy() & focus_flag) == focus_flag
       
  2518             && !(test->d_func()->extra && test->d_func()->extra->focus_proxy)
       
  2519             && test->isVisibleTo(toplevel) && test->isEnabled()
       
  2520             && !(w->windowType() == Qt::SubWindow && !w->isAncestorOf(test))
       
  2521             && (toplevel->windowType() != Qt::SubWindow || toplevel->isAncestorOf(test))) {
       
  2522             w = test;
       
  2523             if (next)
       
  2524                 break;
       
  2525         }
       
  2526         test = test->d_func()->focus_next;
       
  2527     }
       
  2528     if (w == f) {
       
  2529         if (qt_in_tab_key_event) {
       
  2530             w->window()->setAttribute(Qt::WA_KeyboardFocusChange);
       
  2531             w->update();
       
  2532         }
       
  2533         return 0;
       
  2534     }
       
  2535     return w;
       
  2536 }
       
  2537 
       
  2538 /*!
       
  2539     \fn void QApplicationPrivate::dispatchEnterLeave(QWidget* enter, QWidget* leave)
       
  2540     \internal
       
  2541 
       
  2542     Creates the proper Enter/Leave event when widget \a enter is entered and
       
  2543     widget \a leave is left.
       
  2544  */
       
  2545 void QApplicationPrivate::dispatchEnterLeave(QWidget* enter, QWidget* leave) {
       
  2546 #if 0
       
  2547     if (leave) {
       
  2548         QEvent e(QEvent::Leave);
       
  2549         QApplication::sendEvent(leave, & e);
       
  2550     }
       
  2551     if (enter) {
       
  2552         QEvent e(QEvent::Enter);
       
  2553         QApplication::sendEvent(enter, & e);
       
  2554     }
       
  2555     return;
       
  2556 #endif
       
  2557 
       
  2558     QWidget* w ;
       
  2559     if ((!enter && !leave) || (enter == leave))
       
  2560         return;
       
  2561 #ifdef ALIEN_DEBUG
       
  2562     qDebug() << "QApplicationPrivate::dispatchEnterLeave, ENTER:" << enter << "LEAVE:" << leave;
       
  2563 #endif
       
  2564     QWidgetList leaveList;
       
  2565     QWidgetList enterList;
       
  2566 
       
  2567     bool sameWindow = leave && enter && leave->window() == enter->window();
       
  2568     if (leave && !sameWindow) {
       
  2569         w = leave;
       
  2570         do {
       
  2571             leaveList.append(w);
       
  2572         } while (!w->isWindow() && (w = w->parentWidget()));
       
  2573     }
       
  2574     if (enter && !sameWindow) {
       
  2575         w = enter;
       
  2576         do {
       
  2577             enterList.prepend(w);
       
  2578         } while (!w->isWindow() && (w = w->parentWidget()));
       
  2579     }
       
  2580     if (sameWindow) {
       
  2581         int enterDepth = 0;
       
  2582         int leaveDepth = 0;
       
  2583         w = enter;
       
  2584         while (!w->isWindow() && (w = w->parentWidget()))
       
  2585             enterDepth++;
       
  2586         w = leave;
       
  2587         while (!w->isWindow() && (w = w->parentWidget()))
       
  2588             leaveDepth++;
       
  2589         QWidget* wenter = enter;
       
  2590         QWidget* wleave = leave;
       
  2591         while (enterDepth > leaveDepth) {
       
  2592             wenter = wenter->parentWidget();
       
  2593             enterDepth--;
       
  2594         }
       
  2595         while (leaveDepth > enterDepth) {
       
  2596             wleave = wleave->parentWidget();
       
  2597             leaveDepth--;
       
  2598         }
       
  2599         while (!wenter->isWindow() && wenter != wleave) {
       
  2600             wenter = wenter->parentWidget();
       
  2601             wleave = wleave->parentWidget();
       
  2602         }
       
  2603 
       
  2604         w = leave;
       
  2605         while (w != wleave) {
       
  2606             leaveList.append(w);
       
  2607             w = w->parentWidget();
       
  2608         }
       
  2609         w = enter;
       
  2610         while (w != wenter) {
       
  2611             enterList.prepend(w);
       
  2612             w = w->parentWidget();
       
  2613         }
       
  2614     }
       
  2615 
       
  2616     QEvent leaveEvent(QEvent::Leave);
       
  2617     for (int i = 0; i < leaveList.size(); ++i) {
       
  2618         w = leaveList.at(i);
       
  2619         if (!QApplication::activeModalWidget() || QApplicationPrivate::tryModalHelper(w, 0)) {
       
  2620 #if defined(Q_WS_WIN) || defined(Q_WS_X11)
       
  2621             if (leaveAfterRelease == w)
       
  2622                 leaveAfterRelease = 0;
       
  2623 #endif
       
  2624             QApplication::sendEvent(w, &leaveEvent);
       
  2625             if (w->testAttribute(Qt::WA_Hover) &&
       
  2626                 (!QApplication::activePopupWidget() || QApplication::activePopupWidget() == w->window())) {
       
  2627                 Q_ASSERT(instance());
       
  2628                 QHoverEvent he(QEvent::HoverLeave, QPoint(-1, -1), w->mapFromGlobal(QApplicationPrivate::instance()->hoverGlobalPos));
       
  2629                 qApp->d_func()->notify_helper(w, &he);
       
  2630             }
       
  2631         }
       
  2632     }
       
  2633     QPoint posEnter = QCursor::pos();
       
  2634     QEvent enterEvent(QEvent::Enter);
       
  2635     for (int i = 0; i < enterList.size(); ++i) {
       
  2636         w = enterList.at(i);
       
  2637         if (!QApplication::activeModalWidget() || QApplicationPrivate::tryModalHelper(w, 0)) {
       
  2638             QApplication::sendEvent(w, &enterEvent);
       
  2639             if (w->testAttribute(Qt::WA_Hover) &&
       
  2640                 (!QApplication::activePopupWidget() || QApplication::activePopupWidget() == w->window())) {
       
  2641                 QHoverEvent he(QEvent::HoverEnter, w->mapFromGlobal(posEnter), QPoint(-1, -1));
       
  2642                 qApp->d_func()->notify_helper(w, &he);
       
  2643             }
       
  2644         }
       
  2645     }
       
  2646 
       
  2647 #ifndef QT_NO_CURSOR
       
  2648     // Update cursor for alien/graphics widgets.
       
  2649 
       
  2650     const bool enterOnAlien = (enter && (isAlien(enter) || enter->testAttribute(Qt::WA_DontShowOnScreen)));
       
  2651 #if defined(Q_WS_X11)
       
  2652     //Whenever we leave an alien widget on X11, we need to reset its nativeParentWidget()'s cursor.
       
  2653     // This is not required on Windows as the cursor is reset on every single mouse move.
       
  2654     QWidget *parentOfLeavingCursor = 0;
       
  2655     for (int i = 0; i < leaveList.size(); ++i) {
       
  2656         w = leaveList.at(i);
       
  2657         if (!isAlien(w))
       
  2658             break;
       
  2659         if (w->testAttribute(Qt::WA_SetCursor)) {
       
  2660             QWidget *parent = w->parentWidget();
       
  2661             while (parent && parent->d_func()->data.in_destructor)
       
  2662                 parent = parent->parentWidget();
       
  2663             parentOfLeavingCursor = parent;
       
  2664             //continue looping, we need to find the downest alien widget with a cursor.
       
  2665             // (downest on the screen)
       
  2666         }
       
  2667     }
       
  2668     //check that we will not call qt_x11_enforce_cursor twice with the same native widget
       
  2669     if (parentOfLeavingCursor && (!enterOnAlien
       
  2670         || parentOfLeavingCursor->effectiveWinId() != enter->effectiveWinId())) {
       
  2671 #ifndef QT_NO_GRAPHICSVIEW
       
  2672         if (!parentOfLeavingCursor->window()->graphicsProxyWidget())
       
  2673 #endif
       
  2674         {
       
  2675             qt_x11_enforce_cursor(parentOfLeavingCursor,true);
       
  2676         }
       
  2677     }
       
  2678 #endif
       
  2679     if (enterOnAlien) {
       
  2680         QWidget *cursorWidget = enter;
       
  2681         while (!cursorWidget->isWindow() && !cursorWidget->isEnabled())
       
  2682             cursorWidget = cursorWidget->parentWidget();
       
  2683 
       
  2684         if (!cursorWidget)
       
  2685             return;
       
  2686 
       
  2687 #ifndef QT_NO_GRAPHICSVIEW
       
  2688         if (cursorWidget->window()->graphicsProxyWidget()) {
       
  2689             QWidgetPrivate::nearestGraphicsProxyWidget(cursorWidget)->setCursor(cursorWidget->cursor());
       
  2690         } else
       
  2691 #endif
       
  2692         {
       
  2693 #if defined(Q_WS_WIN)
       
  2694             qt_win_set_cursor(cursorWidget, true);
       
  2695 #elif defined(Q_WS_X11)
       
  2696             qt_x11_enforce_cursor(cursorWidget, true);
       
  2697 #elif defined(Q_WS_S60)
       
  2698             qt_symbian_set_cursor(cursorWidget, true);
       
  2699 #endif
       
  2700         }
       
  2701     }
       
  2702 #endif
       
  2703 }
       
  2704 
       
  2705 /* exported for the benefit of testing tools */
       
  2706 Q_GUI_EXPORT bool qt_tryModalHelper(QWidget *widget, QWidget **rettop)
       
  2707 {
       
  2708     return QApplicationPrivate::tryModalHelper(widget, rettop);
       
  2709 }
       
  2710 
       
  2711 /*! \internal
       
  2712     Returns true if \a widget is blocked by a modal window.
       
  2713  */
       
  2714 bool QApplicationPrivate::isBlockedByModal(QWidget *widget)
       
  2715 {
       
  2716     widget = widget->window();
       
  2717     if (!modalState())
       
  2718         return false;
       
  2719     if (QApplication::activePopupWidget() == widget)
       
  2720         return false;
       
  2721 
       
  2722     for (int i = 0; i < qt_modal_stack->size(); ++i) {
       
  2723         QWidget *modalWidget = qt_modal_stack->at(i);
       
  2724 
       
  2725         {
       
  2726             // check if the active modal widget is our widget or a parent of our widget
       
  2727             QWidget *w = widget;
       
  2728             while (w) {
       
  2729                 if (w == modalWidget)
       
  2730                     return false;
       
  2731                 w = w->parentWidget();
       
  2732             }
       
  2733 #ifdef Q_WS_WIN
       
  2734             if ((widget->testAttribute(Qt::WA_WState_Created) || widget->data->winid)
       
  2735                 && (modalWidget->testAttribute(Qt::WA_WState_Created) || modalWidget->data->winid)
       
  2736                 && IsChild(modalWidget->data->winid, widget->data->winid))
       
  2737                 return false;
       
  2738 #endif
       
  2739         }
       
  2740 
       
  2741         Qt::WindowModality windowModality = modalWidget->windowModality();
       
  2742         if (windowModality == Qt::NonModal) {
       
  2743             // determine the modality type if it hasn't been set on the
       
  2744             // modalWidget, this normally happens when waiting for a
       
  2745             // native dialog. use WindowModal if we are the child of a
       
  2746             // group leader; otherwise use ApplicationModal.
       
  2747             QWidget *m = modalWidget;
       
  2748             while (m && !m->testAttribute(Qt::WA_GroupLeader)) {
       
  2749                 m = m->parentWidget();
       
  2750                 if (m)
       
  2751                     m = m->window();
       
  2752             }
       
  2753             windowModality = (m && m->testAttribute(Qt::WA_GroupLeader))
       
  2754                              ? Qt::WindowModal
       
  2755                              : Qt::ApplicationModal;
       
  2756         }
       
  2757 
       
  2758         switch (windowModality) {
       
  2759         case Qt::ApplicationModal:
       
  2760             {
       
  2761                 QWidget *groupLeaderForWidget = widget;
       
  2762                 while (groupLeaderForWidget && !groupLeaderForWidget->testAttribute(Qt::WA_GroupLeader))
       
  2763                     groupLeaderForWidget = groupLeaderForWidget->parentWidget();
       
  2764 
       
  2765                 if (groupLeaderForWidget) {
       
  2766                     // if \a widget has WA_GroupLeader, it can only be blocked by ApplicationModal children
       
  2767                     QWidget *m = modalWidget;
       
  2768                     while (m && m != groupLeaderForWidget && !m->testAttribute(Qt::WA_GroupLeader))
       
  2769                         m = m->parentWidget();
       
  2770                     if (m == groupLeaderForWidget)
       
  2771                         return true;
       
  2772                 } else if (modalWidget != widget) {
       
  2773                     return true;
       
  2774                 }
       
  2775                 break;
       
  2776             }
       
  2777         case Qt::WindowModal:
       
  2778             {
       
  2779                 QWidget *w = widget;
       
  2780                 do {
       
  2781                     QWidget *m = modalWidget;
       
  2782                     do {
       
  2783                         if (m == w)
       
  2784                             return true;
       
  2785                         m = m->parentWidget();
       
  2786                         if (m)
       
  2787                             m = m->window();
       
  2788                     } while (m);
       
  2789                     w = w->parentWidget();
       
  2790                     if (w)
       
  2791                         w = w->window();
       
  2792                 } while (w);
       
  2793                 break;
       
  2794             }
       
  2795         default:
       
  2796             Q_ASSERT_X(false, "QApplication", "internal error, a modal widget cannot be modeless");
       
  2797             break;
       
  2798         }
       
  2799     }
       
  2800     return false;
       
  2801 }
       
  2802 
       
  2803 /*!\internal
       
  2804  */
       
  2805 void QApplicationPrivate::enterModal(QWidget *widget)
       
  2806 {
       
  2807     QSet<QWidget*> blocked;
       
  2808     QList<QWidget*> windows = QApplication::topLevelWidgets();
       
  2809     for (int i = 0; i < windows.count(); ++i) {
       
  2810         QWidget *window = windows.at(i);
       
  2811         if (window->windowType() != Qt::Tool && isBlockedByModal(window))
       
  2812             blocked.insert(window);
       
  2813     }
       
  2814 
       
  2815     enterModal_sys(widget);
       
  2816 
       
  2817     windows = QApplication::topLevelWidgets();
       
  2818     QEvent e(QEvent::WindowBlocked);
       
  2819     for (int i = 0; i < windows.count(); ++i) {
       
  2820         QWidget *window = windows.at(i);
       
  2821         if (!blocked.contains(window) && window->windowType() != Qt::Tool && isBlockedByModal(window))
       
  2822             QApplication::sendEvent(window, &e);
       
  2823     }
       
  2824 }
       
  2825 
       
  2826 /*!\internal
       
  2827  */
       
  2828 void QApplicationPrivate::leaveModal(QWidget *widget)
       
  2829 {
       
  2830     QSet<QWidget*> blocked;
       
  2831     QList<QWidget*> windows = QApplication::topLevelWidgets();
       
  2832     for (int i = 0; i < windows.count(); ++i) {
       
  2833         QWidget *window = windows.at(i);
       
  2834         if (window->windowType() != Qt::Tool && isBlockedByModal(window))
       
  2835             blocked.insert(window);
       
  2836     }
       
  2837 
       
  2838     leaveModal_sys(widget);
       
  2839 
       
  2840     windows = QApplication::topLevelWidgets();
       
  2841     QEvent e(QEvent::WindowUnblocked);
       
  2842     for (int i = 0; i < windows.count(); ++i) {
       
  2843         QWidget *window = windows.at(i);
       
  2844         if(blocked.contains(window) && window->windowType() != Qt::Tool && !isBlockedByModal(window))
       
  2845             QApplication::sendEvent(window, &e);
       
  2846     }
       
  2847 }
       
  2848 
       
  2849 
       
  2850 
       
  2851 /*!\internal
       
  2852 
       
  2853   Called from qapplication_\e{platform}.cpp, returns true
       
  2854   if the widget should accept the event.
       
  2855  */
       
  2856 bool QApplicationPrivate::tryModalHelper(QWidget *widget, QWidget **rettop)
       
  2857 {
       
  2858     QWidget *top = QApplication::activeModalWidget();
       
  2859     if (rettop)
       
  2860         *rettop = top;
       
  2861 
       
  2862     // the active popup widget always gets the input event
       
  2863     if (QApplication::activePopupWidget())
       
  2864         return true;
       
  2865 
       
  2866 #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
       
  2867     top = QApplicationPrivate::tryModalHelper_sys(top);
       
  2868     if (rettop)
       
  2869         *rettop = top;
       
  2870 #endif
       
  2871 
       
  2872     return !isBlockedByModal(widget->window());
       
  2873 }
       
  2874 
       
  2875 /*
       
  2876    \internal
       
  2877 */
       
  2878 QWidget *QApplicationPrivate::pickMouseReceiver(QWidget *candidate, const QPoint &globalPos,
       
  2879                                                 QPoint &pos, QEvent::Type type,
       
  2880                                                 Qt::MouseButtons buttons, QWidget *buttonDown,
       
  2881                                                 QWidget *alienWidget)
       
  2882 {
       
  2883     Q_ASSERT(candidate);
       
  2884 
       
  2885     QWidget *mouseGrabber = QWidget::mouseGrabber();
       
  2886     if (((type == QEvent::MouseMove && buttons) || (type == QEvent::MouseButtonRelease))
       
  2887             && !buttonDown && !mouseGrabber) {
       
  2888         return 0;
       
  2889     }
       
  2890 
       
  2891     if (alienWidget && alienWidget->internalWinId())
       
  2892         alienWidget = 0;
       
  2893 
       
  2894     QWidget *receiver = candidate;
       
  2895 
       
  2896     if (!mouseGrabber)
       
  2897         mouseGrabber = (buttonDown && !isBlockedByModal(buttonDown)) ? buttonDown : alienWidget;
       
  2898 
       
  2899     if (mouseGrabber && mouseGrabber != candidate) {
       
  2900         receiver = mouseGrabber;
       
  2901         pos = receiver->mapFromGlobal(globalPos);
       
  2902 #ifdef ALIEN_DEBUG
       
  2903         qDebug() << "  ** receiver adjusted to:" << receiver << "pos:" << pos;
       
  2904 #endif
       
  2905     }
       
  2906 
       
  2907     return receiver;
       
  2908 
       
  2909 }
       
  2910 
       
  2911 /*
       
  2912    \internal
       
  2913 */
       
  2914 bool QApplicationPrivate::sendMouseEvent(QWidget *receiver, QMouseEvent *event,
       
  2915                                          QWidget *alienWidget, QWidget *nativeWidget,
       
  2916                                          QWidget **buttonDown, QPointer<QWidget> &lastMouseReceiver,
       
  2917                                          bool spontaneous)
       
  2918 {
       
  2919     Q_ASSERT(receiver);
       
  2920     Q_ASSERT(event);
       
  2921     Q_ASSERT(nativeWidget);
       
  2922     Q_ASSERT(buttonDown);
       
  2923 
       
  2924     if (alienWidget && !isAlien(alienWidget))
       
  2925         alienWidget = 0;
       
  2926 
       
  2927     QPointer<QWidget> receiverGuard = receiver;
       
  2928     QPointer<QWidget> nativeGuard = nativeWidget;
       
  2929     QPointer<QWidget> alienGuard = alienWidget;
       
  2930     QPointer<QWidget> activePopupWidget = QApplication::activePopupWidget();
       
  2931 
       
  2932     const bool graphicsWidget = nativeWidget->testAttribute(Qt::WA_DontShowOnScreen);
       
  2933 
       
  2934     if (*buttonDown) {
       
  2935         if (!graphicsWidget) {
       
  2936             // Register the widget that shall receive a leave event
       
  2937             // after the last button is released.
       
  2938             if ((alienWidget || !receiver->internalWinId()) && !leaveAfterRelease && !QWidget::mouseGrabber())
       
  2939                 leaveAfterRelease = *buttonDown;
       
  2940             if (event->type() == QEvent::MouseButtonRelease && !event->buttons())
       
  2941                 *buttonDown = 0;
       
  2942         }
       
  2943     } else if (lastMouseReceiver) {
       
  2944         // Dispatch enter/leave if we move:
       
  2945         // 1) from an alien widget to another alien widget or
       
  2946         //    from a native widget to an alien widget (first OR case)
       
  2947         // 2) from an alien widget to a native widget (second OR case)
       
  2948         if ((alienWidget && alienWidget != lastMouseReceiver)
       
  2949             || (isAlien(lastMouseReceiver) && !alienWidget)) {
       
  2950             if (activePopupWidget) {
       
  2951                 if (!QWidget::mouseGrabber())
       
  2952                     dispatchEnterLeave(alienWidget ? alienWidget : nativeWidget, lastMouseReceiver);
       
  2953             } else {
       
  2954                 dispatchEnterLeave(receiver, lastMouseReceiver);
       
  2955             }
       
  2956 
       
  2957         }
       
  2958     }
       
  2959 
       
  2960 #ifdef ALIEN_DEBUG
       
  2961     qDebug() << "QApplicationPrivate::sendMouseEvent: receiver:" << receiver
       
  2962              << "pos:" << event->pos() << "alien" << alienWidget << "button down"
       
  2963              << *buttonDown << "last" << lastMouseReceiver << "leave after release"
       
  2964              << leaveAfterRelease;
       
  2965 #endif
       
  2966 
       
  2967     // We need this quard in case someone opens a modal dialog / popup. If that's the case
       
  2968     // leaveAfterRelease is set to null, but we shall not update lastMouseReceiver.
       
  2969     const bool wasLeaveAfterRelease = leaveAfterRelease != 0;
       
  2970     bool result;
       
  2971     if (spontaneous)
       
  2972         result = QApplication::sendSpontaneousEvent(receiver, event);
       
  2973     else
       
  2974         result = QApplication::sendEvent(receiver, event);
       
  2975 
       
  2976     if (!graphicsWidget && leaveAfterRelease && event->type() == QEvent::MouseButtonRelease
       
  2977         && !event->buttons() && QWidget::mouseGrabber() != leaveAfterRelease) {
       
  2978         // Dispatch enter/leave if:
       
  2979         // 1) the mouse grabber is an alien widget
       
  2980         // 2) the button is released on an alien widget
       
  2981 
       
  2982         QWidget *enter = 0;
       
  2983         if (nativeGuard)
       
  2984             enter = alienGuard ? alienWidget : nativeWidget;
       
  2985         else // The receiver is typically deleted on mouse release with drag'n'drop.
       
  2986             enter = QApplication::widgetAt(event->globalPos());
       
  2987 
       
  2988         dispatchEnterLeave(enter, leaveAfterRelease);
       
  2989         leaveAfterRelease = 0;
       
  2990         lastMouseReceiver = enter;
       
  2991     } else if (!wasLeaveAfterRelease) {
       
  2992         if (activePopupWidget) {
       
  2993             if (!QWidget::mouseGrabber())
       
  2994                 lastMouseReceiver = alienGuard ? alienWidget : (nativeGuard ? nativeWidget : 0);
       
  2995         } else {
       
  2996             lastMouseReceiver = receiverGuard ? receiver : QApplication::widgetAt(event->globalPos());
       
  2997         }
       
  2998     }
       
  2999 
       
  3000     return result;
       
  3001 }
       
  3002 
       
  3003 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS)
       
  3004 /*
       
  3005     This function should only be called when the widget changes visibility, i.e.
       
  3006     when the \a widget is shown, hidden or deleted. This function does nothing
       
  3007     if the widget is a top-level or native, i.e. not an alien widget. In that
       
  3008     case enter/leave events are genereated by the underlying windowing system.
       
  3009 */
       
  3010 extern QPointer<QWidget> qt_last_mouse_receiver;
       
  3011 extern QWidget *qt_button_down;
       
  3012 void QApplicationPrivate::sendSyntheticEnterLeave(QWidget *widget)
       
  3013 {
       
  3014 #ifndef QT_NO_CURSOR
       
  3015 #ifdef Q_WS_QWS
       
  3016     if (!widget || widget->isWindow())
       
  3017         return;
       
  3018 #else
       
  3019     if (!widget || widget->internalWinId() || widget->isWindow())
       
  3020         return;
       
  3021 #endif
       
  3022     const bool widgetInShow = widget->isVisible() && !widget->data->in_destructor;
       
  3023     if (!widgetInShow && widget != qt_last_mouse_receiver)
       
  3024         return; // Widget was not under the cursor when it was hidden/deleted.
       
  3025 
       
  3026     if (widgetInShow && widget->parentWidget()->data->in_show)
       
  3027         return; // Ingore recursive show.
       
  3028 
       
  3029     QWidget *mouseGrabber = QWidget::mouseGrabber();
       
  3030     if (mouseGrabber && mouseGrabber != widget)
       
  3031         return; // Someone else has the grab; enter/leave should not occur.
       
  3032 
       
  3033     QWidget *tlw = widget->window();
       
  3034     if (tlw->data->in_destructor || tlw->data->is_closing)
       
  3035         return; // Closing down the business.
       
  3036 
       
  3037     if (widgetInShow && (!qt_last_mouse_receiver || qt_last_mouse_receiver->window() != tlw))
       
  3038         return; // Mouse cursor not inside the widget's top-level.
       
  3039 
       
  3040     const QPoint globalPos(QCursor::pos());
       
  3041     QPoint pos = tlw->mapFromGlobal(globalPos);
       
  3042 
       
  3043     // Find the current widget under the mouse. If this function was called from
       
  3044     // the widget's destructor, we have to make sure childAt() doesn't take into
       
  3045     // account widgets that are about to be destructed.
       
  3046     QWidget *widgetUnderCursor = tlw->d_func()->childAt_helper(pos, widget->data->in_destructor);
       
  3047     if (!widgetUnderCursor)
       
  3048         widgetUnderCursor = tlw;
       
  3049     else
       
  3050         pos = widgetUnderCursor->mapFrom(tlw, pos);
       
  3051 
       
  3052     if (widgetInShow && widgetUnderCursor != widget && !widget->isAncestorOf(widgetUnderCursor))
       
  3053         return; // Mouse cursor not inside the widget or any of its children.
       
  3054 
       
  3055     if (widget->data->in_destructor && qt_button_down == widget)
       
  3056         qt_button_down = 0;
       
  3057 
       
  3058     // Send enter/leave events followed by a mouse move on the entered widget.
       
  3059     QMouseEvent e(QEvent::MouseMove, pos, globalPos, Qt::NoButton, Qt::NoButton, Qt::NoModifier);
       
  3060     sendMouseEvent(widgetUnderCursor, &e, widgetUnderCursor, tlw, &qt_button_down, qt_last_mouse_receiver);
       
  3061 #endif // QT_NO_CURSOR
       
  3062 }
       
  3063 #endif // Q_WS_WIN || Q_WS_X11
       
  3064 
       
  3065 /*!
       
  3066     Returns the desktop widget (also called the root window).
       
  3067 
       
  3068     The desktop may be composed of multiple screens, so it would be incorrect,
       
  3069     for example, to attempt to \e center some widget in the desktop's geometry.
       
  3070     QDesktopWidget has various functions for obtaining useful geometries upon
       
  3071     the desktop, such as QDesktopWidget::screenGeometry() and
       
  3072     QDesktopWidget::availableGeometry().
       
  3073 
       
  3074     On X11, it is also possible to draw on the desktop.
       
  3075 */
       
  3076 QDesktopWidget *QApplication::desktop()
       
  3077 {
       
  3078     if (!qt_desktopWidget || // not created yet
       
  3079          !(qt_desktopWidget->windowType() == Qt::Desktop)) { // reparented away
       
  3080         qt_desktopWidget = new QDesktopWidget();
       
  3081     }
       
  3082     return qt_desktopWidget;
       
  3083 }
       
  3084 
       
  3085 #ifndef QT_NO_CLIPBOARD
       
  3086 /*!
       
  3087     Returns a pointer to the application global clipboard.
       
  3088 
       
  3089     \note The QApplication object should already be constructed before
       
  3090     accessing the clipboard.
       
  3091 */
       
  3092 QClipboard *QApplication::clipboard()
       
  3093 {
       
  3094     if (qt_clipboard == 0) {
       
  3095         if (!qApp) {
       
  3096             qWarning("QApplication: Must construct a QApplication before accessing a QClipboard");
       
  3097             return 0;
       
  3098         }
       
  3099         qt_clipboard = new QClipboard(0);
       
  3100     }
       
  3101     return qt_clipboard;
       
  3102 }
       
  3103 #endif // QT_NO_CLIPBOARD
       
  3104 
       
  3105 /*!
       
  3106     Sets whether Qt should use the system's standard colors, fonts, etc., to
       
  3107     \a on. By default, this is true.
       
  3108 
       
  3109     This function must be called before creating the QApplication object, like
       
  3110     this:
       
  3111 
       
  3112     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 6
       
  3113 
       
  3114     \sa desktopSettingsAware()
       
  3115 */
       
  3116 void QApplication::setDesktopSettingsAware(bool on)
       
  3117 {
       
  3118     QApplicationPrivate::obey_desktop_settings = on;
       
  3119 }
       
  3120 
       
  3121 /*!
       
  3122     Returns true if Qt is set to use the system's standard colors, fonts, etc.;
       
  3123     otherwise returns false. The default is true.
       
  3124 
       
  3125     \sa setDesktopSettingsAware()
       
  3126 */
       
  3127 bool QApplication::desktopSettingsAware()
       
  3128 {
       
  3129     return QApplicationPrivate::obey_desktop_settings;
       
  3130 }
       
  3131 
       
  3132 /*!
       
  3133     Returns the current state of the modifier keys on the keyboard. The current
       
  3134     state is updated sychronously as the event queue is emptied of events that
       
  3135     will spontaneously change the keyboard state (QEvent::KeyPress and
       
  3136     QEvent::KeyRelease events).
       
  3137 
       
  3138     It should be noted this may not reflect the actual keys held on the input
       
  3139     device at the time of calling but rather the modifiers as last reported in
       
  3140     one of the above events. If no keys are being held Qt::NoModifier is
       
  3141     returned.
       
  3142 
       
  3143     \sa mouseButtons()
       
  3144 */
       
  3145 
       
  3146 Qt::KeyboardModifiers QApplication::keyboardModifiers()
       
  3147 {
       
  3148     return QApplicationPrivate::modifier_buttons;
       
  3149 }
       
  3150 
       
  3151 /*!
       
  3152     Returns the current state of the buttons on the mouse. The current state is
       
  3153     updated syncronously as the event queue is emptied of events that will
       
  3154     spontaneously change the mouse state (QEvent::MouseButtonPress and
       
  3155     QEvent::MouseButtonRelease events).
       
  3156 
       
  3157     It should be noted this may not reflect the actual buttons held on the
       
  3158     input device at the time of calling but rather the mouse buttons as last
       
  3159     reported in one of the above events. If no mouse buttons are being held
       
  3160     Qt::NoButton is returned.
       
  3161 
       
  3162     \sa keyboardModifiers()
       
  3163 */
       
  3164 
       
  3165 Qt::MouseButtons QApplication::mouseButtons()
       
  3166 {
       
  3167     return QApplicationPrivate::mouse_buttons;
       
  3168 }
       
  3169 
       
  3170 /*!
       
  3171     \fn bool QApplication::isSessionRestored() const
       
  3172 
       
  3173     Returns true if the application has been restored from an earlier
       
  3174     \l{Session Management}{session}; otherwise returns false.
       
  3175 
       
  3176     \sa sessionId(), commitData(), saveState()
       
  3177 */
       
  3178 
       
  3179 
       
  3180 /*!
       
  3181     \fn QString QApplication::sessionId() const
       
  3182 
       
  3183     Returns the current \l{Session Management}{session's} identifier.
       
  3184 
       
  3185     If the application has been restored from an earlier session, this
       
  3186     identifier is the same as it was in that previous session. The session
       
  3187     identifier is guaranteed to be unique both for different applications
       
  3188     and for different instances of the same application.
       
  3189 
       
  3190     \sa isSessionRestored(), sessionKey(), commitData(), saveState()
       
  3191 */
       
  3192 
       
  3193 /*!
       
  3194     \fn QString QApplication::sessionKey() const
       
  3195 
       
  3196     Returns the session key in the current \l{Session Management}{session}.
       
  3197 
       
  3198     If the application has been restored from an earlier session, this key is
       
  3199     the same as it was when the previous session ended.
       
  3200 
       
  3201     The session key changes with every call of commitData() or saveState().
       
  3202 
       
  3203     \sa isSessionRestored(), sessionId(), commitData(), saveState()
       
  3204 */
       
  3205 #ifndef QT_NO_SESSIONMANAGER
       
  3206 bool QApplication::isSessionRestored() const
       
  3207 {
       
  3208     Q_D(const QApplication);
       
  3209     return d->is_session_restored;
       
  3210 }
       
  3211 
       
  3212 QString QApplication::sessionId() const
       
  3213 {
       
  3214     Q_D(const QApplication);
       
  3215     return d->session_id;
       
  3216 }
       
  3217 
       
  3218 QString QApplication::sessionKey() const
       
  3219 {
       
  3220     Q_D(const QApplication);
       
  3221     return d->session_key;
       
  3222 }
       
  3223 #endif
       
  3224 
       
  3225 
       
  3226 
       
  3227 /*!
       
  3228     \since 4.2
       
  3229     \fn void QApplication::commitDataRequest(QSessionManager &manager)
       
  3230 
       
  3231     This signal deals with \l{Session Management}{session management}. It is
       
  3232     emitted when the QSessionManager wants the application to commit all its
       
  3233     data.
       
  3234 
       
  3235     Usually this means saving all open files, after getting permission from
       
  3236     the user. Furthermore you may want to provide a means by which the user
       
  3237     can cancel the shutdown.
       
  3238 
       
  3239     You should not exit the application within this signal. Instead,
       
  3240     the session manager may or may not do this afterwards, depending on the
       
  3241     context.
       
  3242 
       
  3243     \warning Within this signal, no user interaction is possible, \e
       
  3244     unless you ask the \a manager for explicit permission. See
       
  3245     QSessionManager::allowsInteraction() and
       
  3246     QSessionManager::allowsErrorInteraction() for details and example
       
  3247     usage.
       
  3248 
       
  3249     \note You should use Qt::DirectConnection when connecting to this signal.
       
  3250 
       
  3251     \sa isSessionRestored(), sessionId(), saveState(), {Session Management}
       
  3252 */
       
  3253 
       
  3254 /*!
       
  3255     This function deals with \l{Session Management}{session management}. It is
       
  3256     invoked when the QSessionManager wants the application to commit all its
       
  3257     data.
       
  3258 
       
  3259     Usually this means saving all open files, after getting permission from the
       
  3260     user. Furthermore you may want to provide a means by which the user can
       
  3261     cancel the shutdown.
       
  3262 
       
  3263     You should not exit the application within this function. Instead, the
       
  3264     session manager may or may not do this afterwards, depending on the
       
  3265     context.
       
  3266 
       
  3267     \warning Within this function, no user interaction is possible, \e
       
  3268     unless you ask the \a manager for explicit permission. See
       
  3269     QSessionManager::allowsInteraction() and
       
  3270     QSessionManager::allowsErrorInteraction() for details and example
       
  3271     usage.
       
  3272 
       
  3273     The default implementation requests interaction and sends a close event to
       
  3274     all visible top-level widgets. If any event was rejected, the shutdown is
       
  3275     canceled.
       
  3276 
       
  3277     \sa isSessionRestored(), sessionId(), saveState(), {Session Management}
       
  3278 */
       
  3279 #ifndef QT_NO_SESSIONMANAGER
       
  3280 void QApplication::commitData(QSessionManager& manager )
       
  3281 {
       
  3282     emit commitDataRequest(manager);
       
  3283     if (manager.allowsInteraction()) {
       
  3284         QWidgetList done;
       
  3285         QWidgetList list = QApplication::topLevelWidgets();
       
  3286         bool cancelled = false;
       
  3287         for (int i = 0; !cancelled && i < list.size(); ++i) {
       
  3288             QWidget* w = list.at(i);
       
  3289             if (w->isVisible() && !done.contains(w)) {
       
  3290                 cancelled = !w->close();
       
  3291                 if (!cancelled)
       
  3292                     done.append(w);
       
  3293                 list = QApplication::topLevelWidgets();
       
  3294                 i = -1;
       
  3295             }
       
  3296         }
       
  3297         if (cancelled)
       
  3298             manager.cancel();
       
  3299     }
       
  3300 }
       
  3301 
       
  3302 /*!
       
  3303     \since 4.2
       
  3304     \fn void QApplication::saveStateRequest(QSessionManager &manager)
       
  3305 
       
  3306     This signal deals with \l{Session Management}{session management}. It is
       
  3307     invoked when the \l{QSessionManager}{session manager} wants the application
       
  3308     to preserve its state for a future session.
       
  3309 
       
  3310     For example, a text editor would create a temporary file that includes the
       
  3311     current contents of its edit buffers, the location of the cursor and other
       
  3312     aspects of the current editing session.
       
  3313 
       
  3314     You should never exit the application within this signal. Instead, the
       
  3315     session manager may or may not do this afterwards, depending on the
       
  3316     context. Futhermore, most session managers will very likely request a saved
       
  3317     state immediately after the application has been started. This permits the
       
  3318     session manager to learn about the application's restart policy.
       
  3319 
       
  3320     \warning Within this function, no user interaction is possible, \e
       
  3321     unless you ask the \a manager for explicit permission. See
       
  3322     QSessionManager::allowsInteraction() and
       
  3323     QSessionManager::allowsErrorInteraction() for details.
       
  3324 
       
  3325     \note You should use Qt::DirectConnection when connecting to this signal.
       
  3326 
       
  3327     \sa isSessionRestored(), sessionId(), commitData(), {Session Management}
       
  3328 */
       
  3329 
       
  3330 /*!
       
  3331     This function deals with \l{Session Management}{session management}. It is
       
  3332     invoked when the \l{QSessionManager}{session manager} wants the application
       
  3333     to preserve its state for a future session.
       
  3334 
       
  3335     For example, a text editor would create a temporary file that includes the
       
  3336     current contents of its edit buffers, the location of the cursor and other
       
  3337     aspects of the current editing session.
       
  3338 
       
  3339     You should never exit the application within this function. Instead, the
       
  3340     session manager may or may not do this afterwards, depending on the
       
  3341     context. Futhermore, most session managers will very likely request a saved
       
  3342     state immediately after the application has been started. This permits the
       
  3343     session manager to learn about the application's restart policy.
       
  3344 
       
  3345     \warning Within this function, no user interaction is possible, \e
       
  3346     unless you ask the \a manager for explicit permission. See
       
  3347     QSessionManager::allowsInteraction() and
       
  3348     QSessionManager::allowsErrorInteraction() for details.
       
  3349 
       
  3350     \sa isSessionRestored(), sessionId(), commitData(), {Session Management}
       
  3351 */
       
  3352 
       
  3353 void QApplication::saveState(QSessionManager &manager)
       
  3354 {
       
  3355     emit saveStateRequest(manager);
       
  3356 }
       
  3357 #endif //QT_NO_SESSIONMANAGER
       
  3358 /*
       
  3359   Sets the time after which a drag should start to \a ms ms.
       
  3360 
       
  3361   \sa startDragTime()
       
  3362 */
       
  3363 
       
  3364 void QApplication::setStartDragTime(int ms)
       
  3365 {
       
  3366     drag_time = ms;
       
  3367 }
       
  3368 
       
  3369 /*!
       
  3370     \property QApplication::startDragTime
       
  3371     \brief the time in milliseconds that a mouse button must be held down
       
  3372     before a drag and drop operation will begin
       
  3373 
       
  3374     If you support drag and drop in your application, and want to start a drag
       
  3375     and drop operation after the user has held down a mouse button for a
       
  3376     certain amount of time, you should use this property's value as the delay.
       
  3377 
       
  3378     Qt also uses this delay internally, e.g. in QTextEdit and QLineEdit, for
       
  3379     starting a drag.
       
  3380 
       
  3381     The default value is 500 ms.
       
  3382 
       
  3383     \sa startDragDistance(), {Drag and Drop}
       
  3384 */
       
  3385 
       
  3386 int QApplication::startDragTime()
       
  3387 {
       
  3388     return drag_time;
       
  3389 }
       
  3390 
       
  3391 /*
       
  3392     Sets the distance after which a drag should start to \a l pixels.
       
  3393 
       
  3394     \sa startDragDistance()
       
  3395 */
       
  3396 
       
  3397 void QApplication::setStartDragDistance(int l)
       
  3398 {
       
  3399     drag_distance = l;
       
  3400 }
       
  3401 
       
  3402 /*!
       
  3403     \property QApplication::startDragDistance
       
  3404 
       
  3405     If you support drag and drop in your application, and want to start a drag
       
  3406     and drop operation after the user has moved the cursor a certain distance
       
  3407     with a button held down, you should use this property's value as the
       
  3408     minimum distance required.
       
  3409 
       
  3410     For example, if the mouse position of the click is stored in \c startPos
       
  3411     and the current position (e.g. in the mouse move event) is \c currentPos,
       
  3412     you can find out if a drag should be started with code like this:
       
  3413 
       
  3414     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 7
       
  3415 
       
  3416     Qt uses this value internally, e.g. in QFileDialog.
       
  3417 
       
  3418     The default value is 4 pixels.
       
  3419 
       
  3420     \sa startDragTime() QPoint::manhattanLength() {Drag and Drop}
       
  3421 */
       
  3422 
       
  3423 int QApplication::startDragDistance()
       
  3424 {
       
  3425     return drag_distance;
       
  3426 }
       
  3427 
       
  3428 /*!
       
  3429     \fn void QApplication::setReverseLayout(bool reverse)
       
  3430 
       
  3431     Use setLayoutDirection() instead.
       
  3432 */
       
  3433 
       
  3434 /*!
       
  3435     \fn void QApplication::reverseLayout()
       
  3436 
       
  3437     Use layoutDirection() instead.
       
  3438 */
       
  3439 
       
  3440 /*!
       
  3441     \fn bool QApplication::isRightToLeft()
       
  3442 
       
  3443     Returns true if the application's layout direction is
       
  3444     Qt::RightToLeft; otherwise returns false.
       
  3445 
       
  3446     \sa layoutDirection(), isLeftToRight()
       
  3447 */
       
  3448 
       
  3449 /*!
       
  3450     \fn bool QApplication::isLeftToRight()
       
  3451 
       
  3452     Returns true if the application's layout direction is
       
  3453     Qt::LeftToRight; otherwise returns false.
       
  3454 
       
  3455     \sa layoutDirection(), isRightToLeft()
       
  3456 */
       
  3457 
       
  3458 /*!
       
  3459     \property QApplication::layoutDirection
       
  3460     \brief the default layout direction for this application
       
  3461 
       
  3462     On system start-up, the default layout direction depends on the
       
  3463     application's language.
       
  3464 
       
  3465     \sa QWidget::layoutDirection, isLeftToRight(), isRightToLeft()
       
  3466  */
       
  3467 
       
  3468 void QApplication::setLayoutDirection(Qt::LayoutDirection direction)
       
  3469 {
       
  3470     if (layout_direction == direction)
       
  3471         return;
       
  3472 
       
  3473     layout_direction = direction;
       
  3474 
       
  3475     QWidgetList list = topLevelWidgets();
       
  3476     for (int i = 0; i < list.size(); ++i) {
       
  3477         QWidget *w = list.at(i);
       
  3478         QEvent ev(QEvent::ApplicationLayoutDirectionChange);
       
  3479         sendEvent(w, &ev);
       
  3480     }
       
  3481 }
       
  3482 
       
  3483 Qt::LayoutDirection QApplication::layoutDirection()
       
  3484 {
       
  3485     return layout_direction;
       
  3486 }
       
  3487 
       
  3488 
       
  3489 /*!
       
  3490     \obsolete
       
  3491 
       
  3492     Strips out vertical alignment flags and transforms an alignment \a align
       
  3493     of Qt::AlignLeft into Qt::AlignLeft or Qt::AlignRight according to the
       
  3494     language used.
       
  3495 */
       
  3496 
       
  3497 #ifdef QT3_SUPPORT
       
  3498 Qt::Alignment QApplication::horizontalAlignment(Qt::Alignment align)
       
  3499 {
       
  3500     return QStyle::visualAlignment(layoutDirection(), align);
       
  3501 }
       
  3502 #endif
       
  3503 
       
  3504 
       
  3505 /*!
       
  3506     \fn QCursor *QApplication::overrideCursor()
       
  3507 
       
  3508     Returns the active application override cursor.
       
  3509 
       
  3510     This function returns 0 if no application cursor has been defined (i.e. the
       
  3511     internal cursor stack is empty).
       
  3512 
       
  3513     \sa setOverrideCursor(), restoreOverrideCursor()
       
  3514 */
       
  3515 #ifndef QT_NO_CURSOR
       
  3516 QCursor *QApplication::overrideCursor()
       
  3517 {
       
  3518     return qApp->d_func()->cursor_list.isEmpty() ? 0 : &qApp->d_func()->cursor_list.first();
       
  3519 }
       
  3520 
       
  3521 /*!
       
  3522     Changes the currently active application override cursor to \a cursor.
       
  3523 
       
  3524     This function has no effect if setOverrideCursor() was not called.
       
  3525 
       
  3526     \sa setOverrideCursor(), overrideCursor(), restoreOverrideCursor(),
       
  3527     QWidget::setCursor()
       
  3528  */
       
  3529 void QApplication::changeOverrideCursor(const QCursor &cursor)
       
  3530 {
       
  3531     if (qApp->d_func()->cursor_list.isEmpty())
       
  3532         return;
       
  3533     qApp->d_func()->cursor_list.removeFirst();
       
  3534 #ifdef QT_MAC_USE_COCOA
       
  3535     // We use native NSCursor stacks in Cocoa. The currentCursor is the
       
  3536     // top of this stack. So to avoid flickering of cursor, we have to
       
  3537     // change the cusor instead of pop-ing the existing OverrideCursor
       
  3538     // and pushing the new one.
       
  3539     qApp->d_func()->cursor_list.prepend(cursor);
       
  3540     qt_cocoaChangeOverrideCursor(cursor);
       
  3541     return;
       
  3542 #endif
       
  3543     setOverrideCursor(cursor);
       
  3544 }
       
  3545 #endif
       
  3546 
       
  3547 /*!
       
  3548     \fn void QApplication::setOverrideCursor(const QCursor &cursor, bool replace)
       
  3549 
       
  3550     Use changeOverrideCursor(\a cursor) (if \a replace is true) or
       
  3551     setOverrideCursor(\a cursor) (if \a replace is false).
       
  3552 */
       
  3553 
       
  3554 /*!
       
  3555     Enters the main event loop and waits until exit() is called, then returns
       
  3556     the value that was set to exit() (which is 0 if exit() is called via
       
  3557     quit()).
       
  3558 
       
  3559     It is necessary to call this function to start event handling. The main
       
  3560     event loop receives events from the window system and dispatches these to
       
  3561     the application widgets.
       
  3562 
       
  3563     Generally, no user interaction can take place before calling exec(). As a
       
  3564     special case, modal widgets like QMessageBox can be used before calling
       
  3565     exec(), because modal widgets call exec() to start a local event loop.
       
  3566 
       
  3567     To make your application perform idle processing, i.e., executing a special
       
  3568     function whenever there are no pending events, use a QTimer with 0 timeout.
       
  3569     More advanced idle processing schemes can be achieved using processEvents().
       
  3570 
       
  3571     We recommend that you connect clean-up code to the
       
  3572     \l{QCoreApplication::}{aboutToQuit()} signal, instead of putting it in your
       
  3573     application's \c{main()} function. This is because, on some platforms the
       
  3574     QApplication::exec() call may not return. For example, on the Windows
       
  3575     platform, when the user logs off, the system terminates the process after Qt
       
  3576     closes all top-level windows. Hence, there is \e{no guarantee} that the
       
  3577     application will have time to exit its event loop and execute code at the
       
  3578     end of the \c{main()} function, after the QApplication::exec() call.
       
  3579 
       
  3580     \sa quitOnLastWindowClosed, quit(), exit(), processEvents(),
       
  3581         QCoreApplication::exec()
       
  3582 */
       
  3583 int QApplication::exec()
       
  3584 {
       
  3585 #ifndef QT_NO_ACCESSIBILITY
       
  3586     QAccessible::setRootObject(qApp);
       
  3587 #endif
       
  3588     return QCoreApplication::exec();
       
  3589 }
       
  3590 
       
  3591 /*! \reimp
       
  3592  */
       
  3593 bool QApplication::notify(QObject *receiver, QEvent *e)
       
  3594 {
       
  3595     Q_D(QApplication);
       
  3596     // no events are delivered after ~QCoreApplication() has started
       
  3597     if (QApplicationPrivate::is_app_closing)
       
  3598         return true;
       
  3599 
       
  3600     if (receiver == 0) {                        // serious error
       
  3601         qWarning("QApplication::notify: Unexpected null receiver");
       
  3602         return true;
       
  3603     }
       
  3604 
       
  3605 #ifndef QT_NO_DEBUG
       
  3606     d->checkReceiverThread(receiver);
       
  3607 #endif
       
  3608 
       
  3609 #ifdef QT3_SUPPORT
       
  3610     if (e->type() == QEvent::ChildRemoved && !receiver->d_func()->pendingChildInsertedEvents.isEmpty())
       
  3611         receiver->d_func()->removePendingChildInsertedEvents(static_cast<QChildEvent *>(e)->child());
       
  3612 #endif // QT3_SUPPORT
       
  3613 
       
  3614     // capture the current mouse/keyboard state
       
  3615     if(e->spontaneous()) {
       
  3616         if (e->type() == QEvent::KeyPress
       
  3617             || e->type() == QEvent::KeyRelease) {
       
  3618             QKeyEvent *ke = static_cast<QKeyEvent*>(e);
       
  3619             QApplicationPrivate::modifier_buttons = ke->modifiers();
       
  3620         } else if(e->type() == QEvent::MouseButtonPress
       
  3621             || e->type() == QEvent::MouseButtonRelease) {
       
  3622                 QMouseEvent *me = static_cast<QMouseEvent*>(e);
       
  3623                 QApplicationPrivate::modifier_buttons = me->modifiers();
       
  3624                 if(me->type() == QEvent::MouseButtonPress)
       
  3625                     QApplicationPrivate::mouse_buttons |= me->button();
       
  3626                 else
       
  3627                     QApplicationPrivate::mouse_buttons &= ~me->button();
       
  3628             }
       
  3629 #if !defined(QT_NO_WHEELEVENT) || !defined(QT_NO_TABLETEVENT)
       
  3630             else if (false
       
  3631 #  ifndef QT_NO_WHEELEVENT
       
  3632                      || e->type() == QEvent::Wheel
       
  3633 #  endif
       
  3634 #  ifndef QT_NO_TABLETEVENT
       
  3635                      || e->type() == QEvent::TabletMove
       
  3636                      || e->type() == QEvent::TabletPress
       
  3637                      || e->type() == QEvent::TabletRelease
       
  3638 #  endif
       
  3639                      ) {
       
  3640             QInputEvent *ie = static_cast<QInputEvent*>(e);
       
  3641             QApplicationPrivate::modifier_buttons = ie->modifiers();
       
  3642         }
       
  3643 #endif // !QT_NO_WHEELEVENT || !QT_NO_TABLETEVENT
       
  3644     }
       
  3645 
       
  3646     // walk through parents and check for gestures
       
  3647     if (d->gestureManager) {
       
  3648         switch (e->type()) {
       
  3649         case QEvent::Paint:
       
  3650         case QEvent::MetaCall:
       
  3651         case QEvent::DeferredDelete:
       
  3652         case QEvent::DragEnter: case QEvent::DragMove: case QEvent::DragLeave:
       
  3653         case QEvent::Drop: case QEvent::DragResponse:
       
  3654         case QEvent::ChildAdded: case QEvent::ChildPolished:
       
  3655 #ifdef QT3_SUPPORT
       
  3656         case QEvent::ChildInsertedRequest:
       
  3657         case QEvent::ChildInserted:
       
  3658         case QEvent::LayoutHint:
       
  3659 #endif
       
  3660         case QEvent::ChildRemoved:
       
  3661         case QEvent::UpdateRequest:
       
  3662         case QEvent::UpdateLater:
       
  3663         case QEvent::AccessibilityPrepare:
       
  3664         case QEvent::LocaleChange:
       
  3665         case QEvent::Style:
       
  3666         case QEvent::IconDrag:
       
  3667         case QEvent::StyleChange:
       
  3668         case QEvent::AccessibilityHelp:
       
  3669         case QEvent::AccessibilityDescription:
       
  3670         case QEvent::GraphicsSceneDragEnter:
       
  3671         case QEvent::GraphicsSceneDragMove:
       
  3672         case QEvent::GraphicsSceneDragLeave:
       
  3673         case QEvent::GraphicsSceneDrop:
       
  3674         case QEvent::DynamicPropertyChange:
       
  3675         case QEvent::NetworkReplyUpdated:
       
  3676             break;
       
  3677         default:
       
  3678             if (receiver->isWidgetType()) {
       
  3679                 if (d->gestureManager->filterEvent(static_cast<QWidget *>(receiver), e))
       
  3680                     return true;
       
  3681             } else {
       
  3682                 // a special case for events that go to QGesture objects.
       
  3683                 // We pass the object to the gesture manager and it'll figure
       
  3684                 // out if it's QGesture or not.
       
  3685                 if (d->gestureManager->filterEvent(receiver, e))
       
  3686                     return true;
       
  3687             }
       
  3688         }
       
  3689     }
       
  3690 
       
  3691 
       
  3692     // User input and window activation makes tooltips sleep
       
  3693     switch (e->type()) {
       
  3694     case QEvent::Wheel:
       
  3695     case QEvent::ActivationChange:
       
  3696     case QEvent::KeyPress:
       
  3697     case QEvent::KeyRelease:
       
  3698     case QEvent::FocusOut:
       
  3699     case QEvent::FocusIn:
       
  3700     case QEvent::MouseButtonPress:
       
  3701     case QEvent::MouseButtonRelease:
       
  3702     case QEvent::MouseButtonDblClick:
       
  3703         d->toolTipFallAsleep.stop();
       
  3704         // fall-through
       
  3705     case QEvent::Leave:
       
  3706         d->toolTipWakeUp.stop();
       
  3707     default:
       
  3708         break;
       
  3709     }
       
  3710 
       
  3711     bool res = false;
       
  3712     if (!receiver->isWidgetType()) {
       
  3713         res = d->notify_helper(receiver, e);
       
  3714     } else switch (e->type()) {
       
  3715 #if defined QT3_SUPPORT && !defined(QT_NO_SHORTCUT)
       
  3716     case QEvent::Accel:
       
  3717         {
       
  3718             if (d->use_compat()) {
       
  3719                 QKeyEvent* key = static_cast<QKeyEvent*>(e);
       
  3720                 res = d->notify_helper(receiver, e);
       
  3721 
       
  3722                 if (!res && !key->isAccepted())
       
  3723                     res = d->qt_dispatchAccelEvent(static_cast<QWidget *>(receiver), key);
       
  3724 
       
  3725                 // next lines are for compatibility with Qt <= 3.0.x: old
       
  3726                 // QAccel was listening on toplevel widgets
       
  3727                 if (!res && !key->isAccepted() && !static_cast<QWidget *>(receiver)->isWindow())
       
  3728                     res = d->notify_helper(static_cast<QWidget *>(receiver)->window(), e);
       
  3729             }
       
  3730             break;
       
  3731         }
       
  3732 #endif //QT3_SUPPORT && !QT_NO_SHORTCUT
       
  3733     case QEvent::ShortcutOverride:
       
  3734     case QEvent::KeyPress:
       
  3735     case QEvent::KeyRelease:
       
  3736         {
       
  3737             bool isWidget = receiver->isWidgetType();
       
  3738             bool isGraphicsWidget = false;
       
  3739 #ifndef QT_NO_GRAPHICSVIEW
       
  3740             isGraphicsWidget = !isWidget && qobject_cast<QGraphicsWidget *>(receiver);
       
  3741 #endif
       
  3742             if (!isWidget && !isGraphicsWidget) {
       
  3743                 res = d->notify_helper(receiver, e);
       
  3744                 break;
       
  3745             }
       
  3746 
       
  3747             QKeyEvent* key = static_cast<QKeyEvent*>(e);
       
  3748 #if defined QT3_SUPPORT && !defined(QT_NO_SHORTCUT)
       
  3749             if (d->use_compat() && d->qt_tryComposeUnicode(static_cast<QWidget*>(receiver), key))
       
  3750                 break;
       
  3751 #endif
       
  3752             if (key->type()==QEvent::KeyPress) {
       
  3753 #ifndef QT_NO_SHORTCUT
       
  3754                 // Try looking for a Shortcut before sending key events
       
  3755                 if ((res = qApp->d_func()->shortcutMap.tryShortcutEvent(receiver, key)))
       
  3756                     return res;
       
  3757 #endif
       
  3758                 qt_in_tab_key_event = (key->key() == Qt::Key_Backtab
       
  3759                                        || key->key() == Qt::Key_Tab
       
  3760                                        || key->key() == Qt::Key_Left
       
  3761                                        || key->key() == Qt::Key_Up
       
  3762                                        || key->key() == Qt::Key_Right
       
  3763                                        || key->key() == Qt::Key_Down);
       
  3764             }
       
  3765             bool def = key->isAccepted();
       
  3766             QPointer<QObject> pr = receiver;
       
  3767             while (receiver) {
       
  3768                 if (def)
       
  3769                     key->accept();
       
  3770                 else
       
  3771                     key->ignore();
       
  3772                 res = d->notify_helper(receiver, e);
       
  3773                 QWidget *w = isWidget ? static_cast<QWidget *>(receiver) : 0;
       
  3774 #ifndef QT_NO_GRAPHICSVIEW
       
  3775                 QGraphicsWidget *gw = isGraphicsWidget ? static_cast<QGraphicsWidget *>(receiver) : 0;
       
  3776 #endif
       
  3777 
       
  3778                 if ((res && key->isAccepted())
       
  3779                     /*
       
  3780                        QLineEdit will emit a signal on Key_Return, but
       
  3781                        ignore the event, and sometimes the connected
       
  3782                        slot deletes the QLineEdit (common in itemview
       
  3783                        delegates), so we have to check if the widget
       
  3784                        was destroyed even if the event was ignored (to
       
  3785                        prevent a crash)
       
  3786 
       
  3787                        note that we don't have to reset pw while
       
  3788                        propagating (because the original receiver will
       
  3789                        be destroyed if one of its ancestors is)
       
  3790                     */
       
  3791                     || !pr
       
  3792                     || (isWidget && (w->isWindow() || !w->parentWidget()))
       
  3793 #ifndef QT_NO_GRAPHICSVIEW
       
  3794                     || (isGraphicsWidget && (gw->isWindow() || !gw->parentWidget()))
       
  3795 #endif
       
  3796                     ) {
       
  3797                     break;
       
  3798                 }
       
  3799 
       
  3800 #ifndef QT_NO_GRAPHICSVIEW
       
  3801                 receiver = w ? (QObject *)w->parentWidget() : (QObject *)gw->parentWidget();
       
  3802 #else
       
  3803                 receiver = w->parentWidget();
       
  3804 #endif
       
  3805             }
       
  3806             qt_in_tab_key_event = false;
       
  3807         }
       
  3808         break;
       
  3809     case QEvent::MouseButtonPress:
       
  3810     case QEvent::MouseButtonRelease:
       
  3811     case QEvent::MouseButtonDblClick:
       
  3812     case QEvent::MouseMove:
       
  3813         {
       
  3814             QWidget* w = static_cast<QWidget *>(receiver);
       
  3815 
       
  3816             QMouseEvent* mouse = static_cast<QMouseEvent*>(e);
       
  3817             QPoint relpos = mouse->pos();
       
  3818 
       
  3819             if (e->spontaneous()) {
       
  3820 #ifndef QT_NO_IM
       
  3821                 QInputContext *ic = w->inputContext();
       
  3822                 if (ic
       
  3823                         && w->testAttribute(Qt::WA_InputMethodEnabled)
       
  3824                         && ic->filterEvent(mouse))
       
  3825                     return true;
       
  3826 #endif
       
  3827 
       
  3828                 if (e->type() == QEvent::MouseButtonPress) {
       
  3829                     QApplicationPrivate::giveFocusAccordingToFocusPolicy(w,
       
  3830                                                                          Qt::ClickFocus,
       
  3831                                                                          Qt::MouseFocusReason);
       
  3832                 }
       
  3833 
       
  3834                 // ### Qt 5 These dynamic tool tips should be an OPT-IN feature. Some platforms
       
  3835                 // like Mac OS X (probably others too), can optimize their views by not
       
  3836                 // dispatching mouse move events. We have attributes to control hover,
       
  3837                 // and mouse tracking, but as long as we are deciding to implement this
       
  3838                 // feature without choice of opting-in or out, you ALWAYS have to have
       
  3839                 // tracking enabled. Therefore, the other properties give a false sense of
       
  3840                 // performance enhancement.
       
  3841                 if (e->type() == QEvent::MouseMove && mouse->buttons() == 0) {
       
  3842                     d->toolTipWidget = w;
       
  3843                     d->toolTipPos = relpos;
       
  3844                     d->toolTipGlobalPos = mouse->globalPos();
       
  3845                     d->toolTipWakeUp.start(d->toolTipFallAsleep.isActive()?20:700, this);
       
  3846                 }
       
  3847             }
       
  3848 
       
  3849             bool eventAccepted = mouse->isAccepted();
       
  3850 
       
  3851             QPointer<QWidget> pw = w;
       
  3852             while (w) {
       
  3853                 QMouseEvent me(mouse->type(), relpos, mouse->globalPos(), mouse->button(), mouse->buttons(),
       
  3854                                mouse->modifiers());
       
  3855                 me.spont = mouse->spontaneous();
       
  3856                 // throw away any mouse-tracking-only mouse events
       
  3857                 if (!w->hasMouseTracking()
       
  3858                     && mouse->type() == QEvent::MouseMove && mouse->buttons() == 0) {
       
  3859                     // but still send them through all application event filters (normally done by notify_helper)
       
  3860                     for (int i = 0; i < d->eventFilters.size(); ++i) {
       
  3861                         register QObject *obj = d->eventFilters.at(i);
       
  3862                         if (!obj)
       
  3863                             continue;
       
  3864                         if (obj->d_func()->threadData != w->d_func()->threadData) {
       
  3865                             qWarning("QApplication: Object event filter cannot be in a different thread.");
       
  3866                             continue;
       
  3867                         }
       
  3868                         if (obj->eventFilter(w, w == receiver ? mouse : &me))
       
  3869                             break;
       
  3870                     }
       
  3871                     res = true;
       
  3872                 } else {
       
  3873                     w->setAttribute(Qt::WA_NoMouseReplay, false);
       
  3874                     res = d->notify_helper(w, w == receiver ? mouse : &me);
       
  3875                     e->spont = false;
       
  3876                 }
       
  3877                 eventAccepted = (w == receiver ? mouse : &me)->isAccepted();
       
  3878                 if (res && eventAccepted)
       
  3879                     break;
       
  3880                 if (w->isWindow() || w->testAttribute(Qt::WA_NoMousePropagation))
       
  3881                     break;
       
  3882                 relpos += w->pos();
       
  3883                 w = w->parentWidget();
       
  3884             }
       
  3885 
       
  3886             mouse->setAccepted(eventAccepted);
       
  3887 
       
  3888             if (e->type() == QEvent::MouseMove) {
       
  3889                 if (!pw)
       
  3890                     break;
       
  3891 
       
  3892                 w = static_cast<QWidget *>(receiver);
       
  3893                 relpos = mouse->pos();
       
  3894                 QPoint diff = relpos - w->mapFromGlobal(d->hoverGlobalPos);
       
  3895                 while (w) {
       
  3896                     if (w->testAttribute(Qt::WA_Hover) &&
       
  3897                         (!QApplication::activePopupWidget() || QApplication::activePopupWidget() == w->window())) {
       
  3898                         QHoverEvent he(QEvent::HoverMove, relpos, relpos - diff);
       
  3899                         d->notify_helper(w, &he);
       
  3900                     }
       
  3901                     if (w->isWindow() || w->testAttribute(Qt::WA_NoMousePropagation))
       
  3902                         break;
       
  3903                     relpos += w->pos();
       
  3904                     w = w->parentWidget();
       
  3905                 }
       
  3906             }
       
  3907 
       
  3908             d->hoverGlobalPos = mouse->globalPos();
       
  3909         }
       
  3910         break;
       
  3911 #ifndef QT_NO_WHEELEVENT
       
  3912     case QEvent::Wheel:
       
  3913         {
       
  3914             QWidget* w = static_cast<QWidget *>(receiver);
       
  3915             QWheelEvent* wheel = static_cast<QWheelEvent*>(e);
       
  3916             QPoint relpos = wheel->pos();
       
  3917             bool eventAccepted = wheel->isAccepted();
       
  3918 
       
  3919             if (e->spontaneous()) {
       
  3920                 QApplicationPrivate::giveFocusAccordingToFocusPolicy(w,
       
  3921                                                                      Qt::WheelFocus,
       
  3922                                                                      Qt::MouseFocusReason);
       
  3923             }
       
  3924 
       
  3925             while (w) {
       
  3926                 QWheelEvent we(relpos, wheel->globalPos(), wheel->delta(), wheel->buttons(),
       
  3927                                wheel->modifiers(), wheel->orientation());
       
  3928                 we.spont = wheel->spontaneous();
       
  3929                 res = d->notify_helper(w, w == receiver ? wheel : &we);
       
  3930                 eventAccepted = ((w == receiver) ? wheel : &we)->isAccepted();
       
  3931                 e->spont = false;
       
  3932                 if ((res && eventAccepted)
       
  3933                     || w->isWindow() || w->testAttribute(Qt::WA_NoMousePropagation))
       
  3934                     break;
       
  3935 
       
  3936                 relpos += w->pos();
       
  3937                 w = w->parentWidget();
       
  3938             }
       
  3939             wheel->setAccepted(eventAccepted);
       
  3940         }
       
  3941         break;
       
  3942 #endif
       
  3943 #ifndef QT_NO_CONTEXTMENU
       
  3944     case QEvent::ContextMenu:
       
  3945         {
       
  3946             QWidget* w = static_cast<QWidget *>(receiver);
       
  3947             QContextMenuEvent *context = static_cast<QContextMenuEvent*>(e);
       
  3948             QPoint relpos = context->pos();
       
  3949             bool eventAccepted = context->isAccepted();
       
  3950             while (w) {
       
  3951                 QContextMenuEvent ce(context->reason(), relpos, context->globalPos(), context->modifiers());
       
  3952                 ce.spont = e->spontaneous();
       
  3953                 res = d->notify_helper(w, w == receiver ? context : &ce);
       
  3954                 eventAccepted = ((w == receiver) ? context : &ce)->isAccepted();
       
  3955                 e->spont = false;
       
  3956 
       
  3957                 if ((res && eventAccepted)
       
  3958                     || w->isWindow() || w->testAttribute(Qt::WA_NoMousePropagation))
       
  3959                     break;
       
  3960 
       
  3961                 relpos += w->pos();
       
  3962                 w = w->parentWidget();
       
  3963             }
       
  3964             context->setAccepted(eventAccepted);
       
  3965         }
       
  3966         break;
       
  3967 #endif // QT_NO_CONTEXTMENU
       
  3968 #ifndef QT_NO_TABLETEVENT
       
  3969     case QEvent::TabletMove:
       
  3970     case QEvent::TabletPress:
       
  3971     case QEvent::TabletRelease:
       
  3972         {
       
  3973             QWidget *w = static_cast<QWidget *>(receiver);
       
  3974             QTabletEvent *tablet = static_cast<QTabletEvent*>(e);
       
  3975             QPoint relpos = tablet->pos();
       
  3976             bool eventAccepted = tablet->isAccepted();
       
  3977             while (w) {
       
  3978                 QTabletEvent te(tablet->type(), relpos, tablet->globalPos(),
       
  3979                                 tablet->hiResGlobalPos(), tablet->device(), tablet->pointerType(),
       
  3980                                 tablet->pressure(), tablet->xTilt(), tablet->yTilt(),
       
  3981                                 tablet->tangentialPressure(), tablet->rotation(), tablet->z(),
       
  3982                                 tablet->modifiers(), tablet->uniqueId());
       
  3983                 te.spont = e->spontaneous();
       
  3984                 res = d->notify_helper(w, w == receiver ? tablet : &te);
       
  3985                 eventAccepted = ((w == receiver) ? tablet : &te)->isAccepted();
       
  3986                 e->spont = false;
       
  3987                 if ((res && eventAccepted)
       
  3988                      || w->isWindow()
       
  3989                      || w->testAttribute(Qt::WA_NoMousePropagation))
       
  3990                     break;
       
  3991 
       
  3992                 relpos += w->pos();
       
  3993                 w = w->parentWidget();
       
  3994             }
       
  3995             tablet->setAccepted(eventAccepted);
       
  3996             qt_tabletChokeMouse = tablet->isAccepted();
       
  3997         }
       
  3998         break;
       
  3999 #endif // QT_NO_TABLETEVENT
       
  4000 
       
  4001 #if !defined(QT_NO_TOOLTIP) || !defined(QT_NO_WHATSTHIS)
       
  4002     case QEvent::ToolTip:
       
  4003     case QEvent::WhatsThis:
       
  4004     case QEvent::QueryWhatsThis:
       
  4005         {
       
  4006             QWidget* w = static_cast<QWidget *>(receiver);
       
  4007             QHelpEvent *help = static_cast<QHelpEvent*>(e);
       
  4008             QPoint relpos = help->pos();
       
  4009             bool eventAccepted = help->isAccepted();
       
  4010             while (w) {
       
  4011                 QHelpEvent he(help->type(), relpos, help->globalPos());
       
  4012                 he.spont = e->spontaneous();
       
  4013                 res = d->notify_helper(w, w == receiver ? help : &he);
       
  4014                 e->spont = false;
       
  4015                 eventAccepted = (w == receiver ? help : &he)->isAccepted();
       
  4016                 if ((res && eventAccepted) || w->isWindow())
       
  4017                     break;
       
  4018 
       
  4019                 relpos += w->pos();
       
  4020                 w = w->parentWidget();
       
  4021             }
       
  4022             help->setAccepted(eventAccepted);
       
  4023         }
       
  4024         break;
       
  4025 #endif
       
  4026 #if !defined(QT_NO_STATUSTIP) || !defined(QT_NO_WHATSTHIS)
       
  4027     case QEvent::StatusTip:
       
  4028     case QEvent::WhatsThisClicked:
       
  4029         {
       
  4030             QWidget *w = static_cast<QWidget *>(receiver);
       
  4031             while (w) {
       
  4032                 res = d->notify_helper(w, e);
       
  4033                 if ((res && e->isAccepted()) || w->isWindow())
       
  4034                     break;
       
  4035                 w = w->parentWidget();
       
  4036             }
       
  4037         }
       
  4038         break;
       
  4039 #endif
       
  4040 
       
  4041 #ifndef QT_NO_DRAGANDDROP
       
  4042     case QEvent::DragEnter: {
       
  4043             QWidget* w = static_cast<QWidget *>(receiver);
       
  4044             QDragEnterEvent *dragEvent = static_cast<QDragEnterEvent *>(e);
       
  4045 #ifdef Q_WS_MAC
       
  4046             // HIView has a slight difference in how it delivers events to children and parents
       
  4047             // It will not give a leave to a child's parent when it enters a child.
       
  4048             QWidget *currentTarget = QDragManager::self()->currentTarget();
       
  4049             if (currentTarget) {
       
  4050                 // Assume currentTarget did not get a leave
       
  4051                 QDragLeaveEvent event;
       
  4052                 QApplication::sendEvent(currentTarget, &event);
       
  4053             }
       
  4054 #endif
       
  4055 #ifndef QT_NO_GRAPHICSVIEW
       
  4056             // QGraphicsProxyWidget handles its own propagation,
       
  4057             // and we must not change QDragManagers currentTarget.
       
  4058             QWExtra *extra = w->window()->d_func()->extra;
       
  4059             if (extra && extra->proxyWidget) {
       
  4060                 res = d->notify_helper(w, dragEvent);
       
  4061                 break;
       
  4062             }
       
  4063 #endif
       
  4064             while (w) {
       
  4065                 if (w->isEnabled() && w->acceptDrops()) {
       
  4066                     res = d->notify_helper(w, dragEvent);
       
  4067                     if (res && dragEvent->isAccepted()) {
       
  4068                         QDragManager::self()->setCurrentTarget(w);
       
  4069                         break;
       
  4070                     }
       
  4071                 }
       
  4072                 if (w->isWindow())
       
  4073                     break;
       
  4074                 dragEvent->p = w->mapToParent(dragEvent->p);
       
  4075                 w = w->parentWidget();
       
  4076             }
       
  4077         }
       
  4078         break;
       
  4079     case QEvent::DragMove:
       
  4080     case QEvent::Drop:
       
  4081     case QEvent::DragLeave: {
       
  4082             QWidget* w = static_cast<QWidget *>(receiver);
       
  4083 #ifndef QT_NO_GRAPHICSVIEW
       
  4084             // QGraphicsProxyWidget handles its own propagation,
       
  4085             // and we must not change QDragManagers currentTarget.
       
  4086             QWExtra *extra = w->window()->d_func()->extra;
       
  4087             bool isProxyWidget = extra && extra->proxyWidget;
       
  4088             if (!isProxyWidget)
       
  4089 #endif
       
  4090                 w = QDragManager::self()->currentTarget();
       
  4091 
       
  4092             if (!w) {
       
  4093 #ifdef Q_WS_MAC
       
  4094                 // HIView has a slight difference in how it delivers events to children and parents
       
  4095                 // It will not give an enter to a child's parent when it leaves the child.
       
  4096                 if (e->type() == QEvent::DragLeave)
       
  4097                     break;
       
  4098                 // Assume that w did not get an enter.
       
  4099                 QDropEvent *dropEvent = static_cast<QDropEvent *>(e);
       
  4100                 QDragEnterEvent dragEnterEvent(dropEvent->pos(), dropEvent->possibleActions(),
       
  4101                                                dropEvent->mimeData(), dropEvent->mouseButtons(),
       
  4102                                                dropEvent->keyboardModifiers());
       
  4103                 QApplication::sendEvent(receiver, &dragEnterEvent);
       
  4104                 w = QDragManager::self()->currentTarget();
       
  4105                 if (!w)
       
  4106 #endif
       
  4107                     break;
       
  4108             }
       
  4109             if (e->type() == QEvent::DragMove || e->type() == QEvent::Drop) {
       
  4110                 QDropEvent *dragEvent = static_cast<QDropEvent *>(e);
       
  4111                 QWidget *origReciver = static_cast<QWidget *>(receiver);
       
  4112                 while (origReciver && w != origReciver) {
       
  4113                     dragEvent->p = origReciver->mapToParent(dragEvent->p);
       
  4114                     origReciver = origReciver->parentWidget();
       
  4115                 }
       
  4116             }
       
  4117             res = d->notify_helper(w, e);
       
  4118             if (e->type() != QEvent::DragMove
       
  4119 #ifndef QT_NO_GRAPHICSVIEW
       
  4120                 && !isProxyWidget
       
  4121 #endif
       
  4122                 )
       
  4123                 QDragManager::self()->setCurrentTarget(0, e->type() == QEvent::Drop);
       
  4124         }
       
  4125         break;
       
  4126 #endif
       
  4127     case QEvent::TouchBegin:
       
  4128     // Note: TouchUpdate and TouchEnd events are never propagated
       
  4129     {
       
  4130         QWidget *widget = static_cast<QWidget *>(receiver);
       
  4131         QTouchEvent *touchEvent = static_cast<QTouchEvent *>(e);
       
  4132         bool eventAccepted = touchEvent->isAccepted();
       
  4133         if (widget->testAttribute(Qt::WA_AcceptTouchEvents) && e->spontaneous()) {
       
  4134             // give the widget focus if the focus policy allows it
       
  4135             QApplicationPrivate::giveFocusAccordingToFocusPolicy(widget,
       
  4136                                                                  Qt::ClickFocus,
       
  4137                                                                  Qt::MouseFocusReason);
       
  4138         }
       
  4139 
       
  4140         while (widget) {
       
  4141             // first, try to deliver the touch event
       
  4142             bool acceptTouchEvents = widget->testAttribute(Qt::WA_AcceptTouchEvents);
       
  4143             touchEvent->setWidget(widget);
       
  4144             touchEvent->setAccepted(acceptTouchEvents);
       
  4145             QWeakPointer<QWidget> p = widget;
       
  4146             res = acceptTouchEvents && d->notify_helper(widget, touchEvent);
       
  4147             eventAccepted = touchEvent->isAccepted();
       
  4148             if (p.isNull()) {
       
  4149                 // widget was deleted
       
  4150                 widget = 0;
       
  4151             } else {
       
  4152                 widget->setAttribute(Qt::WA_WState_AcceptedTouchBeginEvent, res && eventAccepted);
       
  4153             }
       
  4154             touchEvent->spont = false;
       
  4155             if (res && eventAccepted) {
       
  4156                 // the first widget to accept the TouchBegin gets an implicit grab.
       
  4157                 for (int i = 0; i < touchEvent->touchPoints().count(); ++i) {
       
  4158                     const QTouchEvent::TouchPoint &touchPoint = touchEvent->touchPoints().at(i);
       
  4159                     d->widgetForTouchPointId[touchPoint.id()] = widget;
       
  4160                 }
       
  4161                 break;
       
  4162             } else if (p.isNull() || widget->isWindow() || widget->testAttribute(Qt::WA_NoMousePropagation)) {
       
  4163                 break;
       
  4164             }
       
  4165             QPoint offset = widget->pos();
       
  4166             widget = widget->parentWidget();
       
  4167             touchEvent->setWidget(widget);
       
  4168             for (int i = 0; i < touchEvent->_touchPoints.size(); ++i) {
       
  4169                 QTouchEvent::TouchPoint &pt = touchEvent->_touchPoints[i];
       
  4170                 QRectF rect = pt.rect();
       
  4171                 rect.moveCenter(offset);
       
  4172                 pt.d->rect = rect;
       
  4173                 pt.d->startPos = pt.startPos() + offset;
       
  4174                 pt.d->lastPos = pt.lastPos() + offset;
       
  4175             }
       
  4176         }
       
  4177 
       
  4178         touchEvent->setAccepted(eventAccepted);
       
  4179         break;
       
  4180     }
       
  4181     case QEvent::RequestSoftwareInputPanel:
       
  4182     case QEvent::CloseSoftwareInputPanel:
       
  4183 #ifndef QT_NO_IM
       
  4184         if (receiver->isWidgetType()) {
       
  4185             QWidget *w = static_cast<QWidget *>(receiver);
       
  4186             QInputContext *ic = w->inputContext();
       
  4187             if (ic && ic->filterEvent(e)) {
       
  4188                 break;
       
  4189             }
       
  4190         }
       
  4191 #endif
       
  4192         res = d->notify_helper(receiver, e);
       
  4193         break;
       
  4194 
       
  4195     case QEvent::NativeGesture:
       
  4196     {
       
  4197         // only propagate the first gesture event (after the GID_BEGIN)
       
  4198         QWidget *w = static_cast<QWidget *>(receiver);
       
  4199         while (w) {
       
  4200             e->ignore();
       
  4201             res = d->notify_helper(w, e);
       
  4202             if ((res && e->isAccepted()) || w->isWindow())
       
  4203                 break;
       
  4204             w = w->parentWidget();
       
  4205         }
       
  4206         break;
       
  4207     }
       
  4208     case QEvent::Gesture:
       
  4209     case QEvent::GestureOverride:
       
  4210     {
       
  4211         if (receiver->isWidgetType()) {
       
  4212             QWidget *w = static_cast<QWidget *>(receiver);
       
  4213             QGestureEvent *gestureEvent = static_cast<QGestureEvent *>(e);
       
  4214             QList<QGesture *> allGestures = gestureEvent->gestures();
       
  4215 
       
  4216             bool eventAccepted = gestureEvent->isAccepted();
       
  4217             bool wasAccepted = eventAccepted;
       
  4218             while (w) {
       
  4219                 // send only gestures the widget expects
       
  4220                 QList<QGesture *> gestures;
       
  4221                 QWidgetPrivate *wd = w->d_func();
       
  4222                 for (int i = 0; i < allGestures.size();) {
       
  4223                     QGesture *g = allGestures.at(i);
       
  4224                     Qt::GestureType type = g->gestureType();
       
  4225                     QMap<Qt::GestureType, Qt::GestureFlags>::iterator contextit =
       
  4226                             wd->gestureContext.find(type);
       
  4227                     bool deliver = contextit != wd->gestureContext.end() &&
       
  4228                         (g->state() == Qt::GestureStarted || w == receiver ||
       
  4229                          (contextit.value() & Qt::ReceivePartialGestures));
       
  4230                     if (deliver) {
       
  4231                         allGestures.removeAt(i);
       
  4232                         gestures.append(g);
       
  4233                     } else {
       
  4234                         ++i;
       
  4235                     }
       
  4236                 }
       
  4237                 if (!gestures.isEmpty()) { // we have gestures for this w
       
  4238                     QGestureEvent ge(gestures);
       
  4239                     ge.t = gestureEvent->t;
       
  4240                     ge.spont = gestureEvent->spont;
       
  4241                     ge.m_accept = wasAccepted;
       
  4242                     ge.d_func()->accepted = gestureEvent->d_func()->accepted;
       
  4243                     res = d->notify_helper(w, &ge);
       
  4244                     gestureEvent->spont = false;
       
  4245                     eventAccepted = ge.isAccepted();
       
  4246                     for (int i = 0; i < gestures.size(); ++i) {
       
  4247                         QGesture *g = gestures.at(i);
       
  4248                         if ((res && eventAccepted) || (!eventAccepted && ge.isAccepted(g))) {
       
  4249                             // if the gesture was accepted, mark the target widget for it
       
  4250                             gestureEvent->d_func()->targetWidgets[g->gestureType()] = w;
       
  4251                             gestureEvent->setAccepted(g, true);
       
  4252                         } else if (!eventAccepted && !ge.isAccepted(g)) {
       
  4253                             // if the gesture was explicitly ignored by the application,
       
  4254                             // put it back so a parent can get it
       
  4255                             allGestures.append(g);
       
  4256                         }
       
  4257                     }
       
  4258                 }
       
  4259                 if (allGestures.isEmpty()) // everything delivered
       
  4260                     break;
       
  4261                 if (w->isWindow())
       
  4262                     break;
       
  4263                 w = w->parentWidget();
       
  4264             }
       
  4265             foreach (QGesture *g, allGestures)
       
  4266                 gestureEvent->setAccepted(g, false);
       
  4267             gestureEvent->m_accept = false; // to make sure we check individual gestures
       
  4268         } else {
       
  4269             res = d->notify_helper(receiver, e);
       
  4270         }
       
  4271         break;
       
  4272     }
       
  4273     default:
       
  4274         res = d->notify_helper(receiver, e);
       
  4275         break;
       
  4276     }
       
  4277 
       
  4278     return res;
       
  4279 }
       
  4280 
       
  4281 bool QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e)
       
  4282 {
       
  4283     // send to all application event filters
       
  4284     if (sendThroughApplicationEventFilters(receiver, e))
       
  4285         return true;
       
  4286 
       
  4287     if (receiver->isWidgetType()) {
       
  4288         QWidget *widget = static_cast<QWidget *>(receiver);
       
  4289 
       
  4290 #if !defined(Q_WS_WINCE) || (defined(GWES_ICONCURS) && !defined(QT_NO_CURSOR))
       
  4291         // toggle HasMouse widget state on enter and leave
       
  4292         if ((e->type() == QEvent::Enter || e->type() == QEvent::DragEnter) &&
       
  4293             (!QApplication::activePopupWidget() || QApplication::activePopupWidget() == widget->window()))
       
  4294             widget->setAttribute(Qt::WA_UnderMouse, true);
       
  4295         else if (e->type() == QEvent::Leave || e->type() == QEvent::DragLeave)
       
  4296             widget->setAttribute(Qt::WA_UnderMouse, false);
       
  4297 #endif
       
  4298 
       
  4299         if (QLayout *layout=widget->d_func()->layout) {
       
  4300             layout->widgetEvent(e);
       
  4301         }
       
  4302     }
       
  4303 
       
  4304     // send to all receiver event filters
       
  4305     if (sendThroughObjectEventFilters(receiver, e))
       
  4306         return true;
       
  4307 
       
  4308     // deliver the event
       
  4309     bool consumed = receiver->event(e);
       
  4310     e->spont = false;
       
  4311     return consumed;
       
  4312 }
       
  4313 
       
  4314 
       
  4315 /*!
       
  4316     \class QSessionManager
       
  4317     \brief The QSessionManager class provides access to the session manager.
       
  4318 
       
  4319     A session manager in a desktop environment (in which Qt GUI applications
       
  4320     live) keeps track of a session, which is a group of running applications,
       
  4321     each of which has a particular state. The state of an application contains
       
  4322     (most notably) the documents the application has open and the position and
       
  4323     size of its windows.
       
  4324 
       
  4325     The session manager is used to save the session, e.g., when the machine is
       
  4326     shut down, and to restore a session, e.g., when the machine is started up.
       
  4327     We recommend that you use QSettings to save an application's settings,
       
  4328     for example, window positions, recently used files, etc. When the
       
  4329     application is restarted by the session manager, you can restore the
       
  4330     settings.
       
  4331 
       
  4332     QSessionManager provides an interface between the application and the
       
  4333     session manager so that the program can work well with the session manager.
       
  4334     In Qt, session management requests for action are handled by the two
       
  4335     virtual functions QApplication::commitData() and QApplication::saveState().
       
  4336     Both provide a reference to a session manager object as argument, to allow
       
  4337     the application to communicate with the session manager. The session
       
  4338     manager can only be accessed through these functions.
       
  4339 
       
  4340     No user interaction is possible \e unless the application gets explicit
       
  4341     permission from the session manager. You ask for permission by calling
       
  4342     allowsInteraction() or, if it is really urgent, allowsErrorInteraction().
       
  4343     Qt does not enforce this, but the session manager may.
       
  4344 
       
  4345     You can try to abort the shutdown process by calling cancel(). The default
       
  4346     commitData() function does this if some top-level window rejected its
       
  4347     closeEvent().
       
  4348 
       
  4349     For sophisticated session managers provided on Unix/X11, QSessionManager
       
  4350     offers further possibilities to fine-tune an application's session
       
  4351     management behavior: setRestartCommand(), setDiscardCommand(),
       
  4352     setRestartHint(), setProperty(), requestPhase2(). See the respective
       
  4353     function descriptions for further details.
       
  4354 
       
  4355     \sa QApplication, {Session Management}
       
  4356 */
       
  4357 
       
  4358 /*! \enum QSessionManager::RestartHint
       
  4359 
       
  4360     This enum type defines the circumstances under which this application wants
       
  4361     to be restarted by the session manager. The current values are:
       
  4362 
       
  4363     \value  RestartIfRunning    If the application is still running when the
       
  4364                                 session is shut down, it wants to be restarted
       
  4365                                 at the start of the next session.
       
  4366 
       
  4367     \value  RestartAnyway       The application wants to be started at the
       
  4368                                 start of the next session, no matter what.
       
  4369                                 (This is useful for utilities that run just
       
  4370                                 after startup and then quit.)
       
  4371 
       
  4372     \value  RestartImmediately  The application wants to be started immediately
       
  4373                                 whenever it is not running.
       
  4374 
       
  4375     \value  RestartNever        The application does not want to be restarted
       
  4376                                 automatically.
       
  4377 
       
  4378     The default hint is \c RestartIfRunning.
       
  4379 */
       
  4380 
       
  4381 
       
  4382 /*!
       
  4383     \fn QString QSessionManager::sessionId() const
       
  4384 
       
  4385     Returns the identifier of the current session.
       
  4386 
       
  4387     If the application has been restored from an earlier session, this
       
  4388     identifier is the same as it was in the earlier session.
       
  4389 
       
  4390     \sa sessionKey(), QApplication::sessionId()
       
  4391 */
       
  4392 
       
  4393 /*!
       
  4394     \fn QString QSessionManager::sessionKey() const
       
  4395 
       
  4396     Returns the session key in the current session.
       
  4397 
       
  4398     If the application has been restored from an earlier session, this key is
       
  4399     the same as it was when the previous session ended.
       
  4400 
       
  4401     The session key changes with every call of commitData() or saveState().
       
  4402 
       
  4403     \sa sessionId(), QApplication::sessionKey()
       
  4404 */
       
  4405 
       
  4406 /*!
       
  4407     \fn void* QSessionManager::handle() const
       
  4408 
       
  4409     \internal
       
  4410 */
       
  4411 
       
  4412 /*!
       
  4413     \fn bool QSessionManager::allowsInteraction()
       
  4414 
       
  4415     Asks the session manager for permission to interact with the user. Returns
       
  4416     true if interaction is permitted; otherwise returns false.
       
  4417 
       
  4418     The rationale behind this mechanism is to make it possible to synchronize
       
  4419     user interaction during a shutdown. Advanced session managers may ask all
       
  4420     applications simultaneously to commit their data, resulting in a much
       
  4421     faster shutdown.
       
  4422 
       
  4423     When the interaction is completed we strongly recommend releasing the user
       
  4424     interaction semaphore with a call to release(). This way, other
       
  4425     applications may get the chance to interact with the user while your
       
  4426     application is still busy saving data. (The semaphore is implicitly
       
  4427     released when the application exits.)
       
  4428 
       
  4429     If the user decides to cancel the shutdown process during the interaction
       
  4430     phase, you must tell the session manager that this has happened by calling
       
  4431     cancel().
       
  4432 
       
  4433     Here's an example of how an application's QApplication::commitData() might
       
  4434     be implemented:
       
  4435 
       
  4436     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 8
       
  4437 
       
  4438     If an error occurred within the application while saving its data, you may
       
  4439     want to try allowsErrorInteraction() instead.
       
  4440 
       
  4441     \sa QApplication::commitData(), release(), cancel()
       
  4442 */
       
  4443 
       
  4444 
       
  4445 /*!
       
  4446     \fn bool QSessionManager::allowsErrorInteraction()
       
  4447 
       
  4448     Returns true if error interaction is permitted; otherwise returns false.
       
  4449 
       
  4450     This is similar to allowsInteraction(), but also enables the application to
       
  4451     tell the user about any errors that occur. Session managers may give error
       
  4452     interaction requests higher priority, which means that it is more likely
       
  4453     that an error interaction is permitted. However, you are still not
       
  4454     guaranteed that the session manager will allow interaction.
       
  4455 
       
  4456     \sa allowsInteraction(), release(), cancel()
       
  4457 */
       
  4458 
       
  4459 /*!
       
  4460     \fn void QSessionManager::release()
       
  4461 
       
  4462     Releases the session manager's interaction semaphore after an interaction
       
  4463     phase.
       
  4464 
       
  4465     \sa allowsInteraction(), allowsErrorInteraction()
       
  4466 */
       
  4467 
       
  4468 /*!
       
  4469     \fn void QSessionManager::cancel()
       
  4470 
       
  4471     Tells the session manager to cancel the shutdown process.  Applications
       
  4472     should not call this function without asking the user first.
       
  4473 
       
  4474     \sa allowsInteraction(), allowsErrorInteraction()
       
  4475 */
       
  4476 
       
  4477 /*!
       
  4478     \fn void QSessionManager::setRestartHint(RestartHint hint)
       
  4479 
       
  4480     Sets the application's restart hint to \a hint. On application startup, the
       
  4481     hint is set to \c RestartIfRunning.
       
  4482 
       
  4483     \note These flags are only hints, a session manager may or may not respect
       
  4484     them.
       
  4485 
       
  4486     We recommend setting the restart hint in QApplication::saveState() because
       
  4487     most session managers perform a checkpoint shortly after an application's
       
  4488     startup.
       
  4489 
       
  4490     \sa restartHint()
       
  4491 */
       
  4492 
       
  4493 /*!
       
  4494     \fn QSessionManager::RestartHint QSessionManager::restartHint() const
       
  4495 
       
  4496     Returns the application's current restart hint. The default is
       
  4497     \c RestartIfRunning.
       
  4498 
       
  4499     \sa setRestartHint()
       
  4500 */
       
  4501 
       
  4502 /*!
       
  4503     \fn void QSessionManager::setRestartCommand(const QStringList& command)
       
  4504 
       
  4505     If the session manager is capable of restoring sessions it will execute
       
  4506     \a command in order to restore the application. The command defaults to
       
  4507 
       
  4508     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 9
       
  4509 
       
  4510     The \c -session option is mandatory; otherwise QApplication cannot tell
       
  4511     whether it has been restored or what the current session identifier is.
       
  4512     See QApplication::isSessionRestored() and QApplication::sessionId() for
       
  4513     details.
       
  4514 
       
  4515     If your application is very simple, it may be possible to store the entire
       
  4516     application state in additional command line options. This is usually a
       
  4517     very bad idea because command lines are often limited to a few hundred
       
  4518     bytes. Instead, use QSettings, temporary files, or a database for this
       
  4519     purpose. By marking the data with the unique sessionId(), you will be able
       
  4520     to restore the application in a future  session.
       
  4521 
       
  4522     \sa restartCommand(), setDiscardCommand(), setRestartHint()
       
  4523 */
       
  4524 
       
  4525 /*!
       
  4526     \fn QStringList QSessionManager::restartCommand() const
       
  4527 
       
  4528     Returns the currently set restart command.
       
  4529 
       
  4530     To iterate over the list, you can use the \l foreach pseudo-keyword:
       
  4531 
       
  4532     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 10
       
  4533 
       
  4534     \sa setRestartCommand(), restartHint()
       
  4535 */
       
  4536 
       
  4537 /*!
       
  4538     \fn void QSessionManager::setDiscardCommand(const QStringList& list)
       
  4539 
       
  4540     Sets the discard command to the given \a list.
       
  4541 
       
  4542     \sa discardCommand(), setRestartCommand()
       
  4543 */
       
  4544 
       
  4545 
       
  4546 /*!
       
  4547     \fn QStringList QSessionManager::discardCommand() const
       
  4548 
       
  4549     Returns the currently set discard command.
       
  4550 
       
  4551     To iterate over the list, you can use the \l foreach pseudo-keyword:
       
  4552 
       
  4553     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 11
       
  4554 
       
  4555     \sa setDiscardCommand(), restartCommand(), setRestartCommand()
       
  4556 */
       
  4557 
       
  4558 /*!
       
  4559     \fn void QSessionManager::setManagerProperty(const QString &name, const QString &value)
       
  4560     \overload
       
  4561 
       
  4562     Low-level write access to the application's identification and state
       
  4563     records are kept in the session manager.
       
  4564 
       
  4565     The property called \a name has its value set to the string \a value.
       
  4566 */
       
  4567 
       
  4568 /*!
       
  4569     \fn void QSessionManager::setManagerProperty(const QString& name,
       
  4570                                                  const QStringList& value)
       
  4571 
       
  4572     Low-level write access to the application's identification and state record
       
  4573     are kept in the session manager.
       
  4574 
       
  4575     The property called \a name has its value set to the string list \a value.
       
  4576 */
       
  4577 
       
  4578 /*!
       
  4579     \fn bool QSessionManager::isPhase2() const
       
  4580 
       
  4581     Returns true if the session manager is currently performing a second
       
  4582     session management phase; otherwise returns false.
       
  4583 
       
  4584     \sa requestPhase2()
       
  4585 */
       
  4586 
       
  4587 /*!
       
  4588     \fn void QSessionManager::requestPhase2()
       
  4589 
       
  4590     Requests a second session management phase for the application. The
       
  4591     application may then return immediately from the QApplication::commitData()
       
  4592     or QApplication::saveState() function, and they will be called again once
       
  4593     most or all other applications have finished their session management.
       
  4594 
       
  4595     The two phases are useful for applications such as the X11 window manager
       
  4596     that need to store information about another application's windows and
       
  4597     therefore have to wait until these applications have completed their
       
  4598     respective session management tasks.
       
  4599 
       
  4600     \note If another application has requested a second phase it may get called
       
  4601     before, simultaneously with, or after your application's second phase.
       
  4602 
       
  4603     \sa isPhase2()
       
  4604 */
       
  4605 
       
  4606 /*****************************************************************************
       
  4607   Stubbed session management support
       
  4608  *****************************************************************************/
       
  4609 #ifndef QT_NO_SESSIONMANAGER
       
  4610 #if defined(Q_WS_WIN) || defined(Q_WS_MAC) || defined(Q_WS_QWS)
       
  4611 
       
  4612 #if defined(Q_OS_WINCE)
       
  4613 HRESULT qt_CoCreateGuid(GUID* guid)
       
  4614 {
       
  4615     // We will use the following information to create the GUID
       
  4616     // 1. absolute path to application
       
  4617     wchar_t tempFilename[MAX_PATH];
       
  4618     if (!GetModuleFileName(0, tempFilename, MAX_PATH))
       
  4619         return S_FALSE;
       
  4620     unsigned int hash = qHash(QString::fromWCharArray(tempFilename));
       
  4621     guid->Data1 = hash;
       
  4622     // 2. creation time of file
       
  4623     QFileInfo info(QString::fromWCharArray(tempFilename));
       
  4624     guid->Data2 = qHash(info.created().toTime_t());
       
  4625     // 3. current system time
       
  4626     guid->Data3 = qHash(QDateTime::currentDateTime().toTime_t());
       
  4627     return S_OK;
       
  4628 }
       
  4629 #if !defined(OLE32_MCOMGUID) || defined(QT_WINCE_FORCE_CREATE_GUID)
       
  4630 #define CoCreateGuid qt_CoCreateGuid
       
  4631 #endif
       
  4632 
       
  4633 #endif
       
  4634 
       
  4635 class QSessionManagerPrivate : public QObjectPrivate
       
  4636 {
       
  4637 public:
       
  4638     QStringList restartCommand;
       
  4639     QStringList discardCommand;
       
  4640     QString sessionId;
       
  4641     QString sessionKey;
       
  4642     QSessionManager::RestartHint restartHint;
       
  4643 };
       
  4644 
       
  4645 QSessionManager* qt_session_manager_self = 0;
       
  4646 QSessionManager::QSessionManager(QApplication * app, QString &id, QString &key)
       
  4647     : QObject(*new QSessionManagerPrivate, app)
       
  4648 {
       
  4649     Q_D(QSessionManager);
       
  4650     setObjectName(QLatin1String("qt_sessionmanager"));
       
  4651     qt_session_manager_self = this;
       
  4652 #if defined(Q_WS_WIN)
       
  4653     wchar_t guidstr[40];
       
  4654     GUID guid;
       
  4655     CoCreateGuid(&guid);
       
  4656     StringFromGUID2(guid, guidstr, 40);
       
  4657     id = QString::fromWCharArray(guidstr);
       
  4658     CoCreateGuid(&guid);
       
  4659     StringFromGUID2(guid, guidstr, 40);
       
  4660     key = QString::fromWCharArray(guidstr);
       
  4661 #endif
       
  4662     d->sessionId = id;
       
  4663     d->sessionKey = key;
       
  4664     d->restartHint = RestartIfRunning;
       
  4665 }
       
  4666 
       
  4667 QSessionManager::~QSessionManager()
       
  4668 {
       
  4669     qt_session_manager_self = 0;
       
  4670 }
       
  4671 
       
  4672 QString QSessionManager::sessionId() const
       
  4673 {
       
  4674     Q_D(const QSessionManager);
       
  4675     return d->sessionId;
       
  4676 }
       
  4677 
       
  4678 QString QSessionManager::sessionKey() const
       
  4679 {
       
  4680     Q_D(const QSessionManager);
       
  4681     return d->sessionKey;
       
  4682 }
       
  4683 
       
  4684 
       
  4685 #if defined(Q_WS_X11) || defined(Q_WS_MAC)
       
  4686 void* QSessionManager::handle() const
       
  4687 {
       
  4688     return 0;
       
  4689 }
       
  4690 #endif
       
  4691 
       
  4692 #if !defined(Q_WS_WIN)
       
  4693 bool QSessionManager::allowsInteraction()
       
  4694 {
       
  4695     return true;
       
  4696 }
       
  4697 
       
  4698 bool QSessionManager::allowsErrorInteraction()
       
  4699 {
       
  4700     return true;
       
  4701 }
       
  4702 void QSessionManager::release()
       
  4703 {
       
  4704 }
       
  4705 
       
  4706 void QSessionManager::cancel()
       
  4707 {
       
  4708 }
       
  4709 #endif
       
  4710 
       
  4711 
       
  4712 void QSessionManager::setRestartHint(QSessionManager::RestartHint hint)
       
  4713 {
       
  4714     Q_D(QSessionManager);
       
  4715     d->restartHint = hint;
       
  4716 }
       
  4717 
       
  4718 QSessionManager::RestartHint QSessionManager::restartHint() const
       
  4719 {
       
  4720     Q_D(const QSessionManager);
       
  4721     return d->restartHint;
       
  4722 }
       
  4723 
       
  4724 void QSessionManager::setRestartCommand(const QStringList& command)
       
  4725 {
       
  4726     Q_D(QSessionManager);
       
  4727     d->restartCommand = command;
       
  4728 }
       
  4729 
       
  4730 QStringList QSessionManager::restartCommand() const
       
  4731 {
       
  4732     Q_D(const QSessionManager);
       
  4733     return d->restartCommand;
       
  4734 }
       
  4735 
       
  4736 void QSessionManager::setDiscardCommand(const QStringList& command)
       
  4737 {
       
  4738     Q_D(QSessionManager);
       
  4739     d->discardCommand = command;
       
  4740 }
       
  4741 
       
  4742 QStringList QSessionManager::discardCommand() const
       
  4743 {
       
  4744     Q_D(const QSessionManager);
       
  4745     return d->discardCommand;
       
  4746 }
       
  4747 
       
  4748 void QSessionManager::setManagerProperty(const QString&, const QString&)
       
  4749 {
       
  4750 }
       
  4751 
       
  4752 void QSessionManager::setManagerProperty(const QString&, const QStringList&)
       
  4753 {
       
  4754 }
       
  4755 
       
  4756 bool QSessionManager::isPhase2() const
       
  4757 {
       
  4758     return false;
       
  4759 }
       
  4760 
       
  4761 void QSessionManager::requestPhase2()
       
  4762 {
       
  4763 }
       
  4764 
       
  4765 #endif
       
  4766 #endif // QT_NO_SESSIONMANAGER
       
  4767 
       
  4768 /*!
       
  4769     \typedef QApplication::ColorMode
       
  4770     \compat
       
  4771 
       
  4772     Use ColorSpec instead.
       
  4773 */
       
  4774 
       
  4775 /*!
       
  4776     \fn Qt::MacintoshVersion QApplication::macVersion()
       
  4777 
       
  4778     Use QSysInfo::MacintoshVersion instead.
       
  4779 */
       
  4780 
       
  4781 /*!
       
  4782     \fn QApplication::ColorMode QApplication::colorMode()
       
  4783 
       
  4784     Use colorSpec() instead, and use ColorSpec as the enum type.
       
  4785 */
       
  4786 
       
  4787 /*!
       
  4788     \fn void QApplication::setColorMode(ColorMode mode)
       
  4789 
       
  4790     Use setColorSpec() instead, and pass a ColorSpec value instead.
       
  4791 */
       
  4792 
       
  4793 /*!
       
  4794     \fn bool QApplication::hasGlobalMouseTracking()
       
  4795 
       
  4796     This feature does not exist anymore. This function always returns true
       
  4797     in Qt 4.
       
  4798 */
       
  4799 
       
  4800 /*!
       
  4801     \fn void QApplication::setGlobalMouseTracking(bool dummy)
       
  4802 
       
  4803     This function does nothing in Qt 4. The \a dummy parameter is ignored.
       
  4804 */
       
  4805 
       
  4806 /*!
       
  4807     \fn void QApplication::flushX()
       
  4808 
       
  4809     Use flush() instead.
       
  4810 */
       
  4811 
       
  4812 /*!
       
  4813     \fn void QApplication::setWinStyleHighlightColor(const QColor &c)
       
  4814 
       
  4815     Use the palette instead.
       
  4816 
       
  4817     \oldcode
       
  4818     app.setWinStyleHighlightColor(color);
       
  4819     \newcode
       
  4820     QPalette palette(QApplication::palette());
       
  4821     palette.setColor(QPalette::Highlight, color);
       
  4822     QApplication::setPalette(palette);
       
  4823     \endcode
       
  4824 */
       
  4825 
       
  4826 /*!
       
  4827     \fn void QApplication::setPalette(const QPalette &pal, bool b, const char* className = 0)
       
  4828 
       
  4829     Use the two-argument overload instead.
       
  4830 */
       
  4831 
       
  4832 /*!
       
  4833     \fn void QApplication::setFont(const QFont &font, bool b, const char* className = 0)
       
  4834 
       
  4835     Use the two-argument overload instead.
       
  4836 */
       
  4837 
       
  4838 /*!
       
  4839     \fn const QColor &QApplication::winStyleHighlightColor()
       
  4840 
       
  4841     Use QApplication::palette().color(QPalette::Active, QPalette::Highlight) instead.
       
  4842 */
       
  4843 
       
  4844 /*!
       
  4845     \fn QWidget *QApplication::widgetAt(int x, int y, bool child)
       
  4846 
       
  4847     Use the two-argument widgetAt() overload to get the child widget. To get
       
  4848     the top-level widget do this:
       
  4849 
       
  4850     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 12
       
  4851 */
       
  4852 
       
  4853 /*!
       
  4854     \fn QWidget *QApplication::widgetAt(const QPoint &point, bool child)
       
  4855 
       
  4856     Use the single-argument widgetAt() overload to get the child widget. To get
       
  4857     the top-level widget do this:
       
  4858 
       
  4859     \snippet doc/src/snippets/code/src_gui_kernel_qapplication.cpp 13
       
  4860 */
       
  4861 
       
  4862 #ifdef QT3_SUPPORT
       
  4863 QWidget *QApplication::mainWidget()
       
  4864 {
       
  4865     return QApplicationPrivate::main_widget;
       
  4866 }
       
  4867 #endif
       
  4868 bool QApplicationPrivate::inPopupMode() const
       
  4869 {
       
  4870     return QApplicationPrivate::popupWidgets != 0;
       
  4871 }
       
  4872 
       
  4873 /*!
       
  4874     \property QApplication::quitOnLastWindowClosed
       
  4875 
       
  4876     \brief whether the application implicitly quits when the last window is
       
  4877     closed.
       
  4878 
       
  4879     The default is true.
       
  4880 
       
  4881     If this property is true, the applications quits when the last visible
       
  4882     primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose
       
  4883     attribute set is closed. By default this attribute is set for all widgets
       
  4884     except for sub-windows. Refer to \l{Qt::WindowType} for a detailed list of
       
  4885     Qt::Window objects.
       
  4886 
       
  4887     \sa quit(), QWidget::close()
       
  4888  */
       
  4889 
       
  4890 void QApplication::setQuitOnLastWindowClosed(bool quit)
       
  4891 {
       
  4892     QApplicationPrivate::quitOnLastWindowClosed = quit;
       
  4893 }
       
  4894 
       
  4895 bool QApplication::quitOnLastWindowClosed()
       
  4896 {
       
  4897     return QApplicationPrivate::quitOnLastWindowClosed;
       
  4898 }
       
  4899 
       
  4900 void QApplicationPrivate::emitLastWindowClosed()
       
  4901 {
       
  4902     if (qApp && qApp->d_func()->in_exec) {
       
  4903         if (QApplicationPrivate::quitOnLastWindowClosed) {
       
  4904             // get ready to quit, this event might be removed if the
       
  4905             // event loop is re-entered, however
       
  4906             QApplication::postEvent(qApp, new QEvent(QEvent::Quit));
       
  4907         }
       
  4908         emit qApp->lastWindowClosed();
       
  4909     }
       
  4910 }
       
  4911 
       
  4912 /*! \variable QApplication::NormalColors
       
  4913     \compat
       
  4914 
       
  4915     Use \l NormalColor instead.
       
  4916 */
       
  4917 
       
  4918 /*! \variable QApplication::CustomColors
       
  4919     \compat
       
  4920 
       
  4921     Use \l CustomColor instead.
       
  4922 */
       
  4923 
       
  4924 #ifdef QT_KEYPAD_NAVIGATION
       
  4925 /*!
       
  4926     Sets the kind of focus navigation Qt should use to \a mode.
       
  4927 
       
  4928     This feature is available in Qt for Embedded Linux, Symbian and Windows CE
       
  4929     only.
       
  4930 
       
  4931     \note On Windows CE this feature is disabled by default for touch device
       
  4932           mkspecs. To enable keypad navigation, build Qt with
       
  4933           QT_KEYPAD_NAVIGATION defined.
       
  4934 
       
  4935     \note On Symbian, setting the mode to Qt::NavigationModeCursorAuto will enable a
       
  4936           virtual mouse cursor on non touchscreen devices, which is controlled
       
  4937           by the cursor keys if there is no analog pointer device.
       
  4938           On other platforms and on touchscreen devices, it has the same
       
  4939           meaning as Qt::NavigationModeNone.
       
  4940 
       
  4941     \since 4.6
       
  4942 
       
  4943     \sa keypadNavigationEnabled()
       
  4944 */
       
  4945 void QApplication::setNavigationMode(Qt::NavigationMode mode)
       
  4946 {
       
  4947 #ifdef Q_OS_SYMBIAN
       
  4948     QApplicationPrivate::setNavigationMode(mode);
       
  4949 #else
       
  4950     QApplicationPrivate::navigationMode = mode;
       
  4951 #endif
       
  4952 }
       
  4953 
       
  4954 /*!
       
  4955     Returns what kind of focus navigation Qt is using.
       
  4956 
       
  4957     This feature is available in Qt for Embedded Linux, Symbian and Windows CE
       
  4958     only.
       
  4959 
       
  4960     \note On Windows CE this feature is disabled by default for touch device
       
  4961           mkspecs. To enable keypad navigation, build Qt with
       
  4962           QT_KEYPAD_NAVIGATION defined.
       
  4963 
       
  4964     \note On Symbian, the default mode is Qt::NavigationModeNone for touch
       
  4965           devices, and Qt::NavigationModeKeypadDirectional.
       
  4966 
       
  4967     \since 4.6
       
  4968 
       
  4969     \sa keypadNavigationEnabled()
       
  4970 */
       
  4971 Qt::NavigationMode QApplication::navigationMode()
       
  4972 {
       
  4973     return QApplicationPrivate::navigationMode;
       
  4974 }
       
  4975 
       
  4976 /*!
       
  4977     Sets whether Qt should use focus navigation suitable for use with a
       
  4978     minimal keypad.
       
  4979 
       
  4980     This feature is available in Qt for Embedded Linux, Symbian and Windows CE
       
  4981     only.
       
  4982 
       
  4983     \note On Windows CE this feature is disabled by default for touch device
       
  4984           mkspecs. To enable keypad navigation, build Qt with
       
  4985           QT_KEYPAD_NAVIGATION defined.
       
  4986 
       
  4987     \deprecated
       
  4988 
       
  4989     \sa setNavigationMode()
       
  4990 */
       
  4991 void QApplication::setKeypadNavigationEnabled(bool enable)
       
  4992 {
       
  4993     if (enable) {
       
  4994 #ifdef Q_OS_SYMBIAN
       
  4995         QApplication::setNavigationMode(Qt::NavigationModeKeypadDirectional);
       
  4996 #else
       
  4997         QApplication::setNavigationMode(Qt::NavigationModeKeypadTabOrder);
       
  4998 #endif
       
  4999     }
       
  5000     else {
       
  5001         QApplication::setNavigationMode(Qt::NavigationModeNone);
       
  5002     }
       
  5003 }
       
  5004 
       
  5005 /*!
       
  5006     Returns true if Qt is set to use keypad navigation; otherwise returns
       
  5007     false.  The default value is true on Symbian, but false on other platforms.
       
  5008 
       
  5009     This feature is available in Qt for Embedded Linux, Symbian and Windows CE
       
  5010     only.
       
  5011 
       
  5012     \note On Windows CE this feature is disabled by default for touch device
       
  5013           mkspecs. To enable keypad navigation, build Qt with
       
  5014           QT_KEYPAD_NAVIGATION defined.
       
  5015 
       
  5016     \deprecated
       
  5017 
       
  5018     \sa navigationMode()
       
  5019 */
       
  5020 bool QApplication::keypadNavigationEnabled()
       
  5021 {
       
  5022     return QApplicationPrivate::navigationMode == Qt::NavigationModeKeypadTabOrder ||
       
  5023         QApplicationPrivate::navigationMode == Qt::NavigationModeKeypadDirectional;
       
  5024 }
       
  5025 #endif
       
  5026 
       
  5027 /*!
       
  5028     \fn void QApplication::alert(QWidget *widget, int msec)
       
  5029     \since 4.3
       
  5030 
       
  5031     Causes an alert to be shown for \a widget if the window is not the active
       
  5032     window. The alert is shown for \a msec miliseconds. If \a msec is zero (the
       
  5033     default), then the alert is shown indefinitely until the window becomes
       
  5034     active again.
       
  5035 
       
  5036     Currently this function does nothing on Qt for Embedded Linux.
       
  5037 
       
  5038     On Mac OS X, this works more at the application level and will cause the
       
  5039     application icon to bounce in the dock.
       
  5040 
       
  5041     On Windows, this causes the window's taskbar entry to flash for a time. If
       
  5042     \a msec is zero, the flashing will stop and the taskbar entry will turn a
       
  5043     different color (currently orange).
       
  5044 
       
  5045     On X11, this will cause the window to be marked as "demands attention", the
       
  5046     window must not be hidden (i.e. not have hide() called on it, but be
       
  5047     visible in some sort of way) in order for this to work.
       
  5048 */
       
  5049 
       
  5050 /*!
       
  5051     \property QApplication::cursorFlashTime
       
  5052     \brief the text cursor's flash (blink) time in milliseconds
       
  5053 
       
  5054     The flash time is the time required to display, invert and restore the
       
  5055     caret display. Usually the text cursor is displayed for half the cursor
       
  5056     flash time, then hidden for the same amount of time, but this may vary.
       
  5057 
       
  5058     The default value on X11 is 1000 milliseconds. On Windows, the
       
  5059     \gui{Control Panel} value is used and setting this property sets the cursor
       
  5060     flash time for all applications.
       
  5061 
       
  5062     We recommend that widgets do not cache this value as it may change at any
       
  5063     time if the user changes the global desktop settings.
       
  5064 */
       
  5065 
       
  5066 /*!
       
  5067     \property QApplication::doubleClickInterval
       
  5068     \brief the time limit in milliseconds that distinguishes a double click
       
  5069     from two consecutive mouse clicks
       
  5070 
       
  5071     The default value on X11 is 400 milliseconds. On Windows and Mac OS, the
       
  5072     operating system's value is used. However, on Windows and Symbian OS,
       
  5073     calling this function sets the double click interval for all applications.
       
  5074 */
       
  5075 
       
  5076 /*!
       
  5077     \property QApplication::keyboardInputInterval
       
  5078     \brief the time limit in milliseconds that distinguishes a key press
       
  5079     from two consecutive key presses
       
  5080     \since 4.2
       
  5081 
       
  5082     The default value on X11 is 400 milliseconds. On Windows and Mac OS, the
       
  5083     operating system's value is used.
       
  5084 */
       
  5085 
       
  5086 /*!
       
  5087     \property QApplication::wheelScrollLines
       
  5088     \brief the number of lines to scroll a widget, when the
       
  5089     mouse wheel is rotated.
       
  5090 
       
  5091     If the value exceeds the widget's number of visible lines, the widget
       
  5092     should interpret the scroll operation as a single \e{page up} or
       
  5093     \e{page down}. If the widget is an \l{QAbstractItemView}{item view class},
       
  5094     then the result of scrolling one \e line depends on the setting of the
       
  5095     widget's \l{QAbstractItemView::verticalScrollMode()}{scroll mode}. Scroll
       
  5096     one \e line can mean \l{QAbstractItemView::ScrollPerItem}{scroll one item}
       
  5097     or \l{QAbstractItemView::ScrollPerPixel}{scroll one pixel}.
       
  5098 
       
  5099     By default, this property has a value of 3.
       
  5100 */
       
  5101 
       
  5102 /*!
       
  5103     \fn void QApplication::setEffectEnabled(Qt::UIEffect effect, bool enable)
       
  5104 
       
  5105     Enables the UI effect \a effect if \a enable is true, otherwise the effect
       
  5106     will not be used.
       
  5107 
       
  5108     \note All effects are disabled on screens running at less than 16-bit color
       
  5109     depth.
       
  5110 
       
  5111     \sa isEffectEnabled(), Qt::UIEffect, setDesktopSettingsAware()
       
  5112 */
       
  5113 
       
  5114 /*!
       
  5115     \fn bool QApplication::isEffectEnabled(Qt::UIEffect effect)
       
  5116 
       
  5117     Returns true if \a effect is enabled; otherwise returns false.
       
  5118 
       
  5119     By default, Qt will try to use the desktop settings. To prevent this, call
       
  5120     setDesktopSettingsAware(false).
       
  5121 
       
  5122     \note All effects are disabled on screens running at less than 16-bit color
       
  5123     depth.
       
  5124 
       
  5125     \sa setEffectEnabled(), Qt::UIEffect
       
  5126 */
       
  5127 
       
  5128 /*!
       
  5129     \fn QWidget *QApplication::mainWidget()
       
  5130 
       
  5131     Returns the main application widget, or 0 if there is no main widget.
       
  5132 */
       
  5133 
       
  5134 /*!
       
  5135     \fn void QApplication::setMainWidget(QWidget *mainWidget)
       
  5136 
       
  5137     Sets the application's main widget to \a mainWidget.
       
  5138 
       
  5139     In most respects the main widget is like any other widget, except that if
       
  5140     it is closed, the application exits. QApplication does \e not take
       
  5141     ownership of the \a mainWidget, so if you create your main widget on the
       
  5142     heap you must delete it yourself.
       
  5143 
       
  5144     You need not have a main widget; connecting lastWindowClosed() to quit()
       
  5145     is an alternative.
       
  5146 
       
  5147     On X11, this function also resizes and moves the main widget according
       
  5148     to the \e -geometry command-line option, so you should set the default
       
  5149     geometry (using \l QWidget::setGeometry()) before calling setMainWidget().
       
  5150 
       
  5151     \sa mainWidget(), exec(), quit()
       
  5152 */
       
  5153 
       
  5154 /*!
       
  5155     \fn void QApplication::beep()
       
  5156 
       
  5157     Sounds the bell, using the default volume and sound. The function is \e not
       
  5158     available in Qt for Embedded Linux.
       
  5159 */
       
  5160 
       
  5161 /*!
       
  5162     \fn void QApplication::setOverrideCursor(const QCursor &cursor)
       
  5163 
       
  5164     Sets the application override cursor to \a cursor.
       
  5165 
       
  5166     Application override cursors are intended for showing the user that the
       
  5167     application is in a special state, for example during an operation that
       
  5168     might take some time.
       
  5169 
       
  5170     This cursor will be displayed in all the application's widgets until
       
  5171     restoreOverrideCursor() or another setOverrideCursor() is called.
       
  5172 
       
  5173     Application cursors are stored on an internal stack. setOverrideCursor()
       
  5174     pushes the cursor onto the stack, and restoreOverrideCursor() pops the
       
  5175     active cursor off the stack. changeOverrideCursor() changes the curently
       
  5176     active application override cursor.
       
  5177 
       
  5178     Every setOverrideCursor() must eventually be followed by a corresponding
       
  5179     restoreOverrideCursor(), otherwise the stack will never be emptied.
       
  5180 
       
  5181     Example:
       
  5182     \snippet doc/src/snippets/code/src_gui_kernel_qapplication_x11.cpp 0
       
  5183 
       
  5184     \sa overrideCursor(), restoreOverrideCursor(), changeOverrideCursor(),
       
  5185     QWidget::setCursor()
       
  5186 */
       
  5187 
       
  5188 /*!
       
  5189     \fn void QApplication::restoreOverrideCursor()
       
  5190 
       
  5191     Undoes the last setOverrideCursor().
       
  5192 
       
  5193     If setOverrideCursor() has been called twice, calling
       
  5194     restoreOverrideCursor() will activate the first cursor set. Calling this
       
  5195     function a second time restores the original widgets' cursors.
       
  5196 
       
  5197     \sa setOverrideCursor(), overrideCursor()
       
  5198 */
       
  5199 
       
  5200 /*!
       
  5201     \macro qApp
       
  5202     \relates QApplication
       
  5203 
       
  5204     A global pointer referring to the unique application object. It is
       
  5205     equivalent to the pointer returned by the QCoreApplication::instance()
       
  5206     function except that, in GUI applications, it is a pointer to a
       
  5207     QApplication instance.
       
  5208 
       
  5209     Only one application object can be created.
       
  5210 
       
  5211     \sa QCoreApplication::instance()
       
  5212 */
       
  5213 
       
  5214 // ************************************************************************
       
  5215 // Input Method support
       
  5216 // ************************************************************************
       
  5217 
       
  5218 /*!
       
  5219     This function replaces the QInputContext instance used by the application
       
  5220     with \a inputContext.
       
  5221 
       
  5222     \sa inputContext()
       
  5223 */
       
  5224 void QApplication::setInputContext(QInputContext *inputContext)
       
  5225 {
       
  5226     Q_D(QApplication);
       
  5227     Q_UNUSED(d);// only static members being used.
       
  5228     if (!inputContext) {
       
  5229         qWarning("QApplication::setInputContext: called with 0 input context");
       
  5230         return;
       
  5231     }
       
  5232     delete d->inputContext;
       
  5233     d->inputContext = inputContext;
       
  5234 }
       
  5235 
       
  5236 /*!
       
  5237     Returns the QInputContext instance used by the application.
       
  5238 
       
  5239     \sa setInputContext()
       
  5240 */
       
  5241 QInputContext *QApplication::inputContext() const
       
  5242 {
       
  5243     Q_D(const QApplication);
       
  5244     Q_UNUSED(d);// only static members being used.
       
  5245     if (QApplicationPrivate::is_app_closing)
       
  5246         return d->inputContext;
       
  5247 #ifdef Q_WS_X11
       
  5248     if (!X11)
       
  5249         return 0;
       
  5250     if (!d->inputContext) {
       
  5251         QApplication *that = const_cast<QApplication *>(this);
       
  5252         QInputContext *qic = QInputContextFactory::create(X11->default_im, that);
       
  5253         // fallback to default X Input Method.
       
  5254         if (!qic)
       
  5255             qic = QInputContextFactory::create(QLatin1String("xim"), that);
       
  5256         that->d_func()->inputContext = qic;
       
  5257     }
       
  5258 #elif defined(Q_WS_S60)
       
  5259     if (!d->inputContext) {
       
  5260         QApplication *that = const_cast<QApplication *>(this);
       
  5261         that->d_func()->inputContext = QInputContextFactory::create(QString::fromLatin1("coefep"), that);
       
  5262     }
       
  5263 #endif
       
  5264     return d->inputContext;
       
  5265 }
       
  5266 
       
  5267 //Returns the current platform used by keyBindings
       
  5268 uint QApplicationPrivate::currentPlatform(){
       
  5269     uint platform = KB_Win;
       
  5270 #ifdef Q_WS_MAC
       
  5271     platform = KB_Mac;
       
  5272 #elif defined Q_WS_X11
       
  5273     platform = KB_X11;
       
  5274     if (X11->desktopEnvironment == DE_KDE)
       
  5275         platform |= KB_KDE;
       
  5276     if (X11->desktopEnvironment == DE_GNOME)
       
  5277         platform |= KB_Gnome;
       
  5278     if (X11->desktopEnvironment == DE_CDE)
       
  5279         platform |= KB_CDE;
       
  5280 #elif defined(Q_OS_SYMBIAN)
       
  5281     platform = KB_S60;
       
  5282 #endif
       
  5283     return platform;
       
  5284 }
       
  5285 
       
  5286 bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event)
       
  5287 {
       
  5288     return QCoreApplication::sendSpontaneousEvent(receiver, event);
       
  5289 }
       
  5290 
       
  5291 
       
  5292 /*!
       
  5293     \since 4.2
       
  5294 
       
  5295     Returns the current keyboard input locale.
       
  5296 */
       
  5297 QLocale QApplication::keyboardInputLocale()
       
  5298 {
       
  5299     if (!QApplicationPrivate::checkInstance("keyboardInputLocale"))
       
  5300         return QLocale::c();
       
  5301     return qt_keymapper_private()->keyboardInputLocale;
       
  5302 }
       
  5303 
       
  5304 /*!
       
  5305     \since 4.2
       
  5306 
       
  5307     Returns the current keyboard input direction.
       
  5308 */
       
  5309 Qt::LayoutDirection QApplication::keyboardInputDirection()
       
  5310 {
       
  5311     if (!QApplicationPrivate::checkInstance("keyboardInputDirection"))
       
  5312         return Qt::LeftToRight;
       
  5313     return qt_keymapper_private()->keyboardInputDirection;
       
  5314 }
       
  5315 
       
  5316 void QApplicationPrivate::giveFocusAccordingToFocusPolicy(QWidget *widget,
       
  5317                                                           Qt::FocusPolicy focusPolicy,
       
  5318                                                           Qt::FocusReason focusReason)
       
  5319 {
       
  5320     QWidget *focusWidget = widget;
       
  5321     while (focusWidget) {
       
  5322         if (focusWidget->isEnabled()
       
  5323             && QApplicationPrivate::shouldSetFocus(focusWidget, focusPolicy)) {
       
  5324             focusWidget->setFocus(focusReason);
       
  5325             break;
       
  5326         }
       
  5327         if (focusWidget->isWindow())
       
  5328             break;
       
  5329         focusWidget = focusWidget->parentWidget();
       
  5330     }
       
  5331 }
       
  5332 
       
  5333 bool QApplicationPrivate::shouldSetFocus(QWidget *w, Qt::FocusPolicy policy)
       
  5334 {
       
  5335     QWidget *f = w;
       
  5336     while (f->d_func()->extra && f->d_func()->extra->focus_proxy)
       
  5337         f = f->d_func()->extra->focus_proxy;
       
  5338 
       
  5339     if ((w->focusPolicy() & policy) != policy)
       
  5340         return false;
       
  5341     if (w != f && (f->focusPolicy() & policy) != policy)
       
  5342         return false;
       
  5343     return true;
       
  5344 }
       
  5345 
       
  5346 /*! \fn QDecoration &QApplication::qwsDecoration()
       
  5347     Return the QWSDecoration used for decorating windows.
       
  5348 
       
  5349     \warning This method is non-portable. It is only available in
       
  5350     Qt for Embedded Linux.
       
  5351 
       
  5352     \sa QDecoration
       
  5353 */
       
  5354 
       
  5355 /*!
       
  5356     \fn void QApplication::qwsSetDecoration(QDecoration *decoration)
       
  5357 
       
  5358     Sets the QDecoration derived class to use for decorating the
       
  5359     windows used by Qt for Embedded Linux to the \a decoration
       
  5360     specified.
       
  5361 
       
  5362     This method is non-portable. It is only available in Qt for Embedded Linux.
       
  5363 
       
  5364     \sa QDecoration
       
  5365 */
       
  5366 
       
  5367 /*! \fn QDecoration* QApplication::qwsSetDecoration(const QString &decoration)
       
  5368     \overload
       
  5369 
       
  5370     Requests a QDecoration object for \a decoration from the
       
  5371     QDecorationFactory.
       
  5372 
       
  5373     The string must be one of the QDecorationFactory::keys(). Keys are case
       
  5374     insensitive.
       
  5375 
       
  5376     A later call to the QApplication constructor will override the requested
       
  5377     style when a "-style" option is passed in as a commandline parameter.
       
  5378 
       
  5379     Returns 0 if an unknown \a decoration is passed, otherwise the QStyle object
       
  5380     returned is set as the application's GUI style.
       
  5381 */
       
  5382 
       
  5383 /*!
       
  5384     \fn bool QApplication::qwsEventFilter(QWSEvent *event)
       
  5385 
       
  5386     This virtual function is only implemented under Qt for Embedded Linux.
       
  5387 
       
  5388     If you create an application that inherits QApplication and
       
  5389     reimplement this function, you get direct access to all QWS (Q
       
  5390     Window System) events that the are received from the QWS master
       
  5391     process. The events are passed in the \a event parameter.
       
  5392 
       
  5393     Return true if you want to stop the event from being processed.
       
  5394     Return false for normal event dispatching. The default
       
  5395     implementation returns false.
       
  5396 */
       
  5397 
       
  5398 /*! \fn void QApplication::qwsSetCustomColors(QRgb *colorTable, int start, int numColors)
       
  5399     Set Qt for Embedded Linux custom color table.
       
  5400 
       
  5401     Qt for Embedded Linux on 8-bpp displays allocates a standard 216 color cube.
       
  5402     The remaining 40 colors may be used by setting a custom color
       
  5403     table in the QWS master process before any clients connect.
       
  5404 
       
  5405     \a colorTable is an array of up to 40 custom colors. \a start is
       
  5406     the starting index (0-39) and \a numColors is the number of colors
       
  5407     to be set (1-40).
       
  5408 
       
  5409     This method is non-portable. It is available \e only in
       
  5410     Qt for Embedded Linux.
       
  5411 
       
  5412     \note The custom colors will not be used by the default screen
       
  5413     driver. To make use of the new colors, implement a custom screen
       
  5414     driver, or use QDirectPainter.
       
  5415 */
       
  5416 
       
  5417 /*! \fn int QApplication::qwsProcessEvent(QWSEvent* event)
       
  5418     \internal
       
  5419 */
       
  5420 
       
  5421 /*! \fn int QApplication::x11ClientMessage(QWidget* w, XEvent* event, bool passive_only)
       
  5422     \internal
       
  5423 */
       
  5424 
       
  5425 /*! \fn int QApplication::x11ProcessEvent(XEvent* event)
       
  5426     This function does the core processing of individual X
       
  5427     \a{event}s, normally by dispatching Qt events to the right
       
  5428     destination.
       
  5429 
       
  5430     It returns 1 if the event was consumed by special handling, 0 if
       
  5431     the \a event was consumed by normal handling, and -1 if the \a
       
  5432     event was for an unrecognized widget.
       
  5433 
       
  5434     \sa x11EventFilter()
       
  5435 */
       
  5436 
       
  5437 /*!
       
  5438     \fn bool QApplication::x11EventFilter(XEvent *event)
       
  5439 
       
  5440     \warning This virtual function is only implemented under X11.
       
  5441 
       
  5442     If you create an application that inherits QApplication and
       
  5443     reimplement this function, you get direct access to all X events
       
  5444     that the are received from the X server. The events are passed in
       
  5445     the \a event parameter.
       
  5446 
       
  5447     Return true if you want to stop the event from being processed.
       
  5448     Return false for normal event dispatching. The default
       
  5449     implementation returns false.
       
  5450 
       
  5451     It is only the directly addressed messages that are filtered.
       
  5452     You must install an event filter directly on the event
       
  5453     dispatcher, which is returned by
       
  5454     QAbstractEventDispatcher::instance(), to handle system wide
       
  5455     messages.
       
  5456 
       
  5457     \sa x11ProcessEvent()
       
  5458 */
       
  5459 
       
  5460 /*! \fn void QApplication::winFocus(QWidget *widget, bool gotFocus)
       
  5461     \internal
       
  5462     \since 4.1
       
  5463 
       
  5464     If \a gotFocus is true, \a widget will become the active window.
       
  5465     Otherwise the active window is reset to 0.
       
  5466 */
       
  5467 
       
  5468 /*! \fn void QApplication::winMouseButtonUp()
       
  5469   \internal
       
  5470  */
       
  5471 
       
  5472 /*! \fn void QApplication::syncX()
       
  5473   Synchronizes with the X server in the X11 implementation.
       
  5474   This normally takes some time. Does nothing on other platforms.
       
  5475 */
       
  5476 
       
  5477 void QApplicationPrivate::updateTouchPointsForWidget(QWidget *widget, QTouchEvent *touchEvent)
       
  5478 {
       
  5479     for (int i = 0; i < touchEvent->touchPoints().count(); ++i) {
       
  5480         QTouchEvent::TouchPoint &touchPoint = touchEvent->_touchPoints[i];
       
  5481 
       
  5482         // preserve the sub-pixel resolution
       
  5483         QRectF rect = touchPoint.screenRect();
       
  5484         const QPointF screenPos = rect.center();
       
  5485         const QPointF delta = screenPos - screenPos.toPoint();
       
  5486 
       
  5487         rect.moveCenter(widget->mapFromGlobal(screenPos.toPoint()) + delta);
       
  5488         touchPoint.d->rect = rect;
       
  5489         if (touchPoint.state() == Qt::TouchPointPressed) {
       
  5490             touchPoint.d->startPos = widget->mapFromGlobal(touchPoint.startScreenPos().toPoint()) + delta;
       
  5491             touchPoint.d->lastPos = widget->mapFromGlobal(touchPoint.lastScreenPos().toPoint()) + delta;
       
  5492         }
       
  5493     }
       
  5494 }
       
  5495 
       
  5496 void QApplicationPrivate::initializeMultitouch()
       
  5497 {
       
  5498     widgetForTouchPointId.clear();
       
  5499     appCurrentTouchPoints.clear();
       
  5500 
       
  5501     initializeMultitouch_sys();
       
  5502 }
       
  5503 
       
  5504 void QApplicationPrivate::cleanupMultitouch()
       
  5505 {
       
  5506     cleanupMultitouch_sys();
       
  5507 
       
  5508     widgetForTouchPointId.clear();
       
  5509     appCurrentTouchPoints.clear();
       
  5510 }
       
  5511 
       
  5512 int QApplicationPrivate::findClosestTouchPointId(const QPointF &screenPos)
       
  5513 {
       
  5514     int closestTouchPointId = -1;
       
  5515     qreal closestDistance = qreal(0.);
       
  5516     foreach (const QTouchEvent::TouchPoint &touchPoint, appCurrentTouchPoints) {
       
  5517         qreal distance = QLineF(screenPos, touchPoint.screenPos()).length();
       
  5518         if (closestTouchPointId == -1 || distance < closestDistance) {
       
  5519             closestTouchPointId = touchPoint.id();
       
  5520             closestDistance = distance;
       
  5521         }
       
  5522     }
       
  5523     return closestTouchPointId;
       
  5524 }
       
  5525 
       
  5526 void QApplicationPrivate::translateRawTouchEvent(QWidget *window,
       
  5527                                                  QTouchEvent::DeviceType deviceType,
       
  5528                                                  const QList<QTouchEvent::TouchPoint> &touchPoints)
       
  5529 {
       
  5530     QApplicationPrivate *d = self;
       
  5531     typedef QPair<Qt::TouchPointStates, QList<QTouchEvent::TouchPoint> > StatesAndTouchPoints;
       
  5532     QHash<QWidget *, StatesAndTouchPoints> widgetsNeedingEvents;
       
  5533 
       
  5534     for (int i = 0; i < touchPoints.count(); ++i) {
       
  5535         QTouchEvent::TouchPoint touchPoint = touchPoints.at(i);
       
  5536         // explicitly detach from the original touch point that we got, so even
       
  5537         // if the touchpoint structs are reused, we will make a copy that we'll
       
  5538         // deliver to the user (which might want to store the struct for later use).
       
  5539         touchPoint.d = touchPoint.d->detach();
       
  5540 
       
  5541         // update state
       
  5542         QWeakPointer<QWidget> widget;
       
  5543         switch (touchPoint.state()) {
       
  5544         case Qt::TouchPointPressed:
       
  5545         {
       
  5546             if (deviceType == QTouchEvent::TouchPad) {
       
  5547                 // on touch-pads, send all touch points to the same widget
       
  5548                 widget = d->widgetForTouchPointId.isEmpty()
       
  5549                          ? QWeakPointer<QWidget>()
       
  5550                          : d->widgetForTouchPointId.constBegin().value();
       
  5551             }
       
  5552 
       
  5553             if (!widget) {
       
  5554                 // determine which widget this event will go to
       
  5555                 if (!window)
       
  5556                     window = QApplication::topLevelAt(touchPoint.screenPos().toPoint());
       
  5557                 if (!window)
       
  5558                     continue;
       
  5559                 widget = window->childAt(window->mapFromGlobal(touchPoint.screenPos().toPoint()));
       
  5560                 if (!widget)
       
  5561                     widget = window;
       
  5562             }
       
  5563 
       
  5564             if (deviceType == QTouchEvent::TouchScreen) {
       
  5565                 int closestTouchPointId = d->findClosestTouchPointId(touchPoint.screenPos());
       
  5566                 QWidget *closestWidget = d->widgetForTouchPointId.value(closestTouchPointId).data();
       
  5567                 if (closestWidget
       
  5568                     && (widget.data()->isAncestorOf(closestWidget) || closestWidget->isAncestorOf(widget.data()))) {
       
  5569                     widget = closestWidget;
       
  5570                 }
       
  5571             }
       
  5572 
       
  5573             d->widgetForTouchPointId[touchPoint.id()] = widget;
       
  5574             touchPoint.d->startScreenPos = touchPoint.screenPos();
       
  5575             touchPoint.d->lastScreenPos = touchPoint.screenPos();
       
  5576             touchPoint.d->startNormalizedPos = touchPoint.normalizedPos();
       
  5577             touchPoint.d->lastNormalizedPos = touchPoint.normalizedPos();
       
  5578             if (touchPoint.pressure() < qreal(0.))
       
  5579                 touchPoint.d->pressure = qreal(1.);
       
  5580 
       
  5581             d->appCurrentTouchPoints.insert(touchPoint.id(), touchPoint);
       
  5582             break;
       
  5583         }
       
  5584         case Qt::TouchPointReleased:
       
  5585         {
       
  5586             widget = d->widgetForTouchPointId.take(touchPoint.id());
       
  5587             if (!widget)
       
  5588                 continue;
       
  5589 
       
  5590             QTouchEvent::TouchPoint previousTouchPoint = d->appCurrentTouchPoints.take(touchPoint.id());
       
  5591             touchPoint.d->startScreenPos = previousTouchPoint.startScreenPos();
       
  5592             touchPoint.d->lastScreenPos = previousTouchPoint.screenPos();
       
  5593             touchPoint.d->startPos = previousTouchPoint.startPos();
       
  5594             touchPoint.d->lastPos = previousTouchPoint.pos();
       
  5595             touchPoint.d->startNormalizedPos = previousTouchPoint.startNormalizedPos();
       
  5596             touchPoint.d->lastNormalizedPos = previousTouchPoint.normalizedPos();
       
  5597             if (touchPoint.pressure() < qreal(0.))
       
  5598                 touchPoint.d->pressure = qreal(0.);
       
  5599             break;
       
  5600         }
       
  5601         default:
       
  5602             widget = d->widgetForTouchPointId.value(touchPoint.id());
       
  5603             if (!widget)
       
  5604                 continue;
       
  5605 
       
  5606             Q_ASSERT(d->appCurrentTouchPoints.contains(touchPoint.id()));
       
  5607             QTouchEvent::TouchPoint previousTouchPoint = d->appCurrentTouchPoints.value(touchPoint.id());
       
  5608             touchPoint.d->startScreenPos = previousTouchPoint.startScreenPos();
       
  5609             touchPoint.d->lastScreenPos = previousTouchPoint.screenPos();
       
  5610             touchPoint.d->startPos = previousTouchPoint.startPos();
       
  5611             touchPoint.d->lastPos = previousTouchPoint.pos();
       
  5612             touchPoint.d->startNormalizedPos = previousTouchPoint.startNormalizedPos();
       
  5613             touchPoint.d->lastNormalizedPos = previousTouchPoint.normalizedPos();
       
  5614             if (touchPoint.pressure() < qreal(0.))
       
  5615                 touchPoint.d->pressure = qreal(1.);
       
  5616             d->appCurrentTouchPoints[touchPoint.id()] = touchPoint;
       
  5617             break;
       
  5618         }
       
  5619         Q_ASSERT(widget.data() != 0);
       
  5620 
       
  5621         // make the *scene* functions return the same as the *screen* functions
       
  5622         touchPoint.d->sceneRect = touchPoint.screenRect();
       
  5623         touchPoint.d->startScenePos = touchPoint.startScreenPos();
       
  5624         touchPoint.d->lastScenePos = touchPoint.lastScreenPos();
       
  5625 
       
  5626         StatesAndTouchPoints &maskAndPoints = widgetsNeedingEvents[widget.data()];
       
  5627         maskAndPoints.first |= touchPoint.state();
       
  5628         if (touchPoint.isPrimary())
       
  5629             maskAndPoints.first |= Qt::TouchPointPrimary;
       
  5630         maskAndPoints.second.append(touchPoint);
       
  5631     }
       
  5632 
       
  5633     if (widgetsNeedingEvents.isEmpty())
       
  5634         return;
       
  5635 
       
  5636     QHash<QWidget *, StatesAndTouchPoints>::ConstIterator it = widgetsNeedingEvents.constBegin();
       
  5637     const QHash<QWidget *, StatesAndTouchPoints>::ConstIterator end = widgetsNeedingEvents.constEnd();
       
  5638     for (; it != end; ++it) {
       
  5639         QWidget *widget = it.key();
       
  5640         if (!QApplicationPrivate::tryModalHelper(widget, 0))
       
  5641             continue;
       
  5642 
       
  5643         QEvent::Type eventType;
       
  5644         switch (it.value().first & Qt::TouchPointStateMask) {
       
  5645         case Qt::TouchPointPressed:
       
  5646             eventType = QEvent::TouchBegin;
       
  5647             break;
       
  5648         case Qt::TouchPointReleased:
       
  5649             eventType = QEvent::TouchEnd;
       
  5650             break;
       
  5651         case Qt::TouchPointStationary:
       
  5652             // don't send the event if nothing changed
       
  5653             continue;
       
  5654         default:
       
  5655             eventType = QEvent::TouchUpdate;
       
  5656             break;
       
  5657         }
       
  5658 
       
  5659         QTouchEvent touchEvent(eventType,
       
  5660                                deviceType,
       
  5661                                QApplication::keyboardModifiers(),
       
  5662                                it.value().first,
       
  5663                                it.value().second);
       
  5664         updateTouchPointsForWidget(widget, &touchEvent);
       
  5665 
       
  5666         switch (touchEvent.type()) {
       
  5667         case QEvent::TouchBegin:
       
  5668         {
       
  5669             // if the TouchBegin handler recurses, we assume that means the event
       
  5670             // has been implicitly accepted and continue to send touch events
       
  5671             widget->setAttribute(Qt::WA_WState_AcceptedTouchBeginEvent);
       
  5672             (void ) QApplication::sendSpontaneousEvent(widget, &touchEvent);
       
  5673             break;
       
  5674         }
       
  5675         default:
       
  5676             if (widget->testAttribute(Qt::WA_WState_AcceptedTouchBeginEvent)) {
       
  5677                 if (touchEvent.type() == QEvent::TouchEnd)
       
  5678                     widget->setAttribute(Qt::WA_WState_AcceptedTouchBeginEvent, false);
       
  5679                 (void) QApplication::sendSpontaneousEvent(widget, &touchEvent);
       
  5680             }
       
  5681             break;
       
  5682         }
       
  5683     }
       
  5684 }
       
  5685 
       
  5686 Q_GUI_EXPORT void qt_translateRawTouchEvent(QWidget *window,
       
  5687                                             QTouchEvent::DeviceType deviceType,
       
  5688                                             const QList<QTouchEvent::TouchPoint> &touchPoints)
       
  5689 {
       
  5690     QApplicationPrivate::translateRawTouchEvent(window, deviceType, touchPoints);
       
  5691 }
       
  5692 
       
  5693 QGestureManager* QGestureManager::instance()
       
  5694 {
       
  5695     QApplicationPrivate *qAppPriv = QApplicationPrivate::instance();
       
  5696     if (!qAppPriv->gestureManager)
       
  5697         qAppPriv->gestureManager = new QGestureManager(qApp);
       
  5698     return qAppPriv->gestureManager;
       
  5699 }
       
  5700 
       
  5701 QT_END_NAMESPACE
       
  5702 
       
  5703 #include "moc_qapplication.cpp"