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