summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel
diff options
context:
space:
mode:
authorQt by Nokia <qt-info@nokia.com>2011-04-27 12:05:43 +0200
committeraxis <qt-info@nokia.com>2011-04-27 12:05:43 +0200
commit38be0d13830efd2d98281c645c3a60afe05ffece (patch)
tree6ea73f3ec77f7d153333779883e8120f82820abe /src/corelib/kernel
Initial import from the monolithic Qt.
This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r--src/corelib/kernel/kernel.pri164
-rw-r--r--src/corelib/kernel/qabstracteventdispatcher.cpp556
-rw-r--r--src/corelib/kernel/qabstracteventdispatcher.h107
-rw-r--r--src/corelib/kernel/qabstracteventdispatcher_p.h79
-rw-r--r--src/corelib/kernel/qabstractitemmodel.cpp3454
-rw-r--r--src/corelib/kernel/qabstractitemmodel.h410
-rw-r--r--src/corelib/kernel/qabstractitemmodel_p.h176
-rw-r--r--src/corelib/kernel/qbasictimer.cpp138
-rw-r--r--src/corelib/kernel/qbasictimer.h74
-rw-r--r--src/corelib/kernel/qcore_mac.cpp82
-rw-r--r--src/corelib/kernel/qcore_mac_p.h163
-rw-r--r--src/corelib/kernel/qcore_symbian_p.cpp317
-rw-r--r--src/corelib/kernel/qcore_symbian_p.h282
-rw-r--r--src/corelib/kernel/qcore_unix.cpp106
-rw-r--r--src/corelib/kernel/qcore_unix_p.h333
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp2730
-rw-r--r--src/corelib/kernel/qcoreapplication.h297
-rw-r--r--src/corelib/kernel/qcoreapplication_mac.cpp66
-rw-r--r--src/corelib/kernel/qcoreapplication_p.h146
-rw-r--r--src/corelib/kernel/qcoreapplication_win.cpp1060
-rw-r--r--src/corelib/kernel/qcorecmdlineargs_p.h171
-rw-r--r--src/corelib/kernel/qcoreevent.cpp612
-rw-r--r--src/corelib/kernel/qcoreevent.h396
-rw-r--r--src/corelib/kernel/qcoreglobaldata.cpp53
-rw-r--r--src/corelib/kernel/qcoreglobaldata_p.h72
-rw-r--r--src/corelib/kernel/qcrashhandler.cpp423
-rw-r--r--src/corelib/kernel/qcrashhandler_p.h81
-rw-r--r--src/corelib/kernel/qeventdispatcher_glib.cpp601
-rw-r--r--src/corelib/kernel/qeventdispatcher_glib_p.h119
-rw-r--r--src/corelib/kernel/qeventdispatcher_symbian.cpp1310
-rw-r--r--src/corelib/kernel/qeventdispatcher_symbian_p.h327
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix.cpp979
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix_p.h208
-rw-r--r--src/corelib/kernel/qeventdispatcher_win.cpp1158
-rw-r--r--src/corelib/kernel/qeventdispatcher_win_p.h110
-rw-r--r--src/corelib/kernel/qeventloop.cpp329
-rw-r--r--src/corelib/kernel/qeventloop.h101
-rw-r--r--src/corelib/kernel/qfunctions_nacl.cpp156
-rw-r--r--src/corelib/kernel/qfunctions_nacl.h97
-rw-r--r--src/corelib/kernel/qfunctions_p.h79
-rw-r--r--src/corelib/kernel/qfunctions_vxworks.cpp202
-rw-r--r--src/corelib/kernel/qfunctions_vxworks.h153
-rw-r--r--src/corelib/kernel/qfunctions_wince.cpp451
-rw-r--r--src/corelib/kernel/qfunctions_wince.h397
-rw-r--r--src/corelib/kernel/qmath.cpp305
-rw-r--r--src/corelib/kernel/qmath.h288
-rw-r--r--src/corelib/kernel/qmath.qdoc155
-rw-r--r--src/corelib/kernel/qmetaobject.cpp2778
-rw-r--r--src/corelib/kernel/qmetaobject.h242
-rw-r--r--src/corelib/kernel/qmetaobject_p.h326
-rw-r--r--src/corelib/kernel/qmetatype.cpp1509
-rw-r--r--src/corelib/kernel/qmetatype.h416
-rw-r--r--src/corelib/kernel/qmimedata.cpp627
-rw-r--r--src/corelib/kernel/qmimedata.h104
-rw-r--r--src/corelib/kernel/qobject.cpp4310
-rw-r--r--src/corelib/kernel/qobject.h411
-rw-r--r--src/corelib/kernel/qobject_p.h305
-rw-r--r--src/corelib/kernel/qobjectcleanuphandler.cpp148
-rw-r--r--src/corelib/kernel/qobjectcleanuphandler.h78
-rw-r--r--src/corelib/kernel/qobjectdefs.h506
-rw-r--r--src/corelib/kernel/qpointer.cpp270
-rw-r--r--src/corelib/kernel/qpointer.h168
-rw-r--r--src/corelib/kernel/qsharedmemory.cpp612
-rw-r--r--src/corelib/kernel/qsharedmemory.h121
-rw-r--r--src/corelib/kernel/qsharedmemory_p.h179
-rw-r--r--src/corelib/kernel/qsharedmemory_symbian.cpp173
-rw-r--r--src/corelib/kernel/qsharedmemory_unix.cpp302
-rw-r--r--src/corelib/kernel/qsharedmemory_win.cpp195
-rw-r--r--src/corelib/kernel/qsignalmapper.cpp321
-rw-r--r--src/corelib/kernel/qsignalmapper.h100
-rw-r--r--src/corelib/kernel/qsocketnotifier.cpp323
-rw-r--r--src/corelib/kernel/qsocketnotifier.h93
-rw-r--r--src/corelib/kernel/qsystemerror.cpp220
-rw-r--r--src/corelib/kernel/qsystemerror_p.h107
-rw-r--r--src/corelib/kernel/qsystemsemaphore.cpp363
-rw-r--r--src/corelib/kernel/qsystemsemaphore.h103
-rw-r--r--src/corelib/kernel/qsystemsemaphore_p.h118
-rw-r--r--src/corelib/kernel/qsystemsemaphore_symbian.cpp138
-rw-r--r--src/corelib/kernel/qsystemsemaphore_unix.cpp238
-rw-r--r--src/corelib/kernel/qsystemsemaphore_win.cpp135
-rw-r--r--src/corelib/kernel/qtcore_eval.cpp571
-rw-r--r--src/corelib/kernel/qtimer.cpp394
-rw-r--r--src/corelib/kernel/qtimer.h116
-rw-r--r--src/corelib/kernel/qtranslator.cpp979
-rw-r--r--src/corelib/kernel/qtranslator.h104
-rw-r--r--src/corelib/kernel/qtranslator_p.h79
-rw-r--r--src/corelib/kernel/qvariant.cpp3237
-rw-r--r--src/corelib/kernel/qvariant.h618
-rw-r--r--src/corelib/kernel/qvariant_p.h153
-rw-r--r--src/corelib/kernel/qwineventnotifier_p.cpp134
-rw-r--r--src/corelib/kernel/qwineventnotifier_p.h94
91 files changed, 42591 insertions, 0 deletions
diff --git a/src/corelib/kernel/kernel.pri b/src/corelib/kernel/kernel.pri
new file mode 100644
index 0000000000..a3628b1be5
--- /dev/null
+++ b/src/corelib/kernel/kernel.pri
@@ -0,0 +1,164 @@
+# Qt core object module
+
+HEADERS += \
+ kernel/qabstracteventdispatcher.h \
+ kernel/qabstractitemmodel.h \
+ kernel/qabstractitemmodel_p.h \
+ kernel/qbasictimer.h \
+ kernel/qeventloop.h\
+ kernel/qpointer.h \
+ kernel/qcorecmdlineargs_p.h \
+ kernel/qcoreapplication.h \
+ kernel/qcoreevent.h \
+ kernel/qmetaobject.h \
+ kernel/qmetatype.h \
+ kernel/qmimedata.h \
+ kernel/qobject.h \
+ kernel/qobjectdefs.h \
+ kernel/qsignalmapper.h \
+ kernel/qsocketnotifier.h \
+ kernel/qtimer.h \
+ kernel/qtranslator.h \
+ kernel/qtranslator_p.h \
+ kernel/qvariant.h \
+ kernel/qabstracteventdispatcher_p.h \
+ kernel/qcoreapplication_p.h \
+ kernel/qobjectcleanuphandler.h \
+ kernel/qvariant_p.h \
+ kernel/qmetaobject_p.h \
+ kernel/qobject_p.h \
+ kernel/qcoreglobaldata_p.h \
+ kernel/qsharedmemory.h \
+ kernel/qsharedmemory_p.h \
+ kernel/qsystemsemaphore.h \
+ kernel/qsystemsemaphore_p.h \
+ kernel/qfunctions_p.h \
+ kernel/qmath.h \
+ kernel/qsystemerror_p.h
+
+SOURCES += \
+ kernel/qabstracteventdispatcher.cpp \
+ kernel/qabstractitemmodel.cpp \
+ kernel/qbasictimer.cpp \
+ kernel/qeventloop.cpp \
+ kernel/qcoreapplication.cpp \
+ kernel/qcoreevent.cpp \
+ kernel/qmetaobject.cpp \
+ kernel/qmetatype.cpp \
+ kernel/qmimedata.cpp \
+ kernel/qobject.cpp \
+ kernel/qobjectcleanuphandler.cpp \
+ kernel/qsignalmapper.cpp \
+ kernel/qsocketnotifier.cpp \
+ kernel/qtimer.cpp \
+ kernel/qtranslator.cpp \
+ kernel/qvariant.cpp \
+ kernel/qcoreglobaldata.cpp \
+ kernel/qsharedmemory.cpp \
+ kernel/qsystemsemaphore.cpp \
+ kernel/qpointer.cpp \
+ kernel/qmath.cpp \
+ kernel/qsystemerror.cpp
+
+win32 {
+ SOURCES += \
+ kernel/qeventdispatcher_win.cpp \
+ kernel/qcoreapplication_win.cpp \
+ kernel/qwineventnotifier_p.cpp \
+ kernel/qsharedmemory_win.cpp \
+ kernel/qsystemsemaphore_win.cpp
+ HEADERS += \
+ kernel/qeventdispatcher_win_p.h \
+ kernel/qwineventnotifier_p.h
+}
+
+
+wince*: {
+ SOURCES += \
+ kernel/qfunctions_wince.cpp
+ HEADERS += \
+ kernel/qfunctions_wince.h
+}
+
+mac:!embedded:!qpa{
+ SOURCES += \
+ kernel/qcoreapplication_mac.cpp
+}
+
+mac:!nacl {
+ SOURCES += \
+ kernel/qcore_mac.cpp
+}
+
+nacl {
+ SOURCES += \
+ kernel/qfunctions_nacl.cpp
+ HEADERS += \
+ kernel/qfunctions_nacl.h
+}
+
+unix:!symbian {
+ SOURCES += \
+ kernel/qcore_unix.cpp \
+ kernel/qcrashhandler.cpp \
+ kernel/qsharedmemory_unix.cpp \
+ kernel/qsystemsemaphore_unix.cpp
+ HEADERS += \
+ kernel/qcore_unix_p.h \
+ kernel/qcrashhandler_p.h
+
+ contains(QT_CONFIG, glib) {
+ SOURCES += \
+ kernel/qeventdispatcher_glib.cpp
+ HEADERS += \
+ kernel/qeventdispatcher_glib_p.h
+ QMAKE_CXXFLAGS += $$QT_CFLAGS_GLIB
+ LIBS_PRIVATE +=$$QT_LIBS_GLIB
+ }
+ SOURCES += \
+ kernel/qeventdispatcher_unix.cpp
+ HEADERS += \
+ kernel/qeventdispatcher_unix_p.h
+
+ contains(QT_CONFIG, clock-gettime):include($$QT_SOURCE_TREE/config.tests/unix/clock-gettime/clock-gettime.pri)
+}
+
+symbian {
+ SOURCES += \
+ kernel/qcore_unix.cpp \
+ kernel/qcrashhandler.cpp \
+ kernel/qeventdispatcher_symbian.cpp \
+ kernel/qcore_symbian_p.cpp \
+ kernel/qsharedmemory_symbian.cpp \
+ kernel/qsystemsemaphore_symbian.cpp
+
+ HEADERS += \
+ kernel/qcore_unix_p.h \
+ kernel/qcrashhandler_p.h \
+ kernel/qeventdispatcher_symbian_p.h \
+ kernel/qcore_symbian_p.h
+}
+
+vxworks {
+ SOURCES += \
+ kernel/qfunctions_vxworks.cpp
+ HEADERS += \
+ kernel/qfunctions_vxworks.h
+}
+
+
+integrity {
+ SOURCES += \
+ kernel/qcore_unix.cpp \
+ kernel/qcrashhandler.cpp \
+ kernel/qsharedmemory_unix.cpp \
+ kernel/qsystemsemaphore_unix.cpp \
+ kernel/qeventdispatcher_unix.cpp
+ HEADERS += \
+ kernel/qcore_unix_p.h \
+ kernel/qcrashhandler_p.h \
+ kernel/qeventdispatcher_unix_p.h
+
+ contains(QT_CONFIG, clock-gettime):include($$QT_SOURCE_TREE/config.tests/unix/clock-gettime/clock-gettime.pri)
+}
+
diff --git a/src/corelib/kernel/qabstracteventdispatcher.cpp b/src/corelib/kernel/qabstracteventdispatcher.cpp
new file mode 100644
index 0000000000..2949cd0d3a
--- /dev/null
+++ b/src/corelib/kernel/qabstracteventdispatcher.cpp
@@ -0,0 +1,556 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qabstracteventdispatcher.h"
+#include "qabstracteventdispatcher_p.h"
+
+#include "qthread.h"
+#include <private/qthread_p.h>
+#include <private/qcoreapplication_p.h>
+
+QT_BEGIN_NAMESPACE
+
+// we allow for 2^24 = 8^8 = 16777216 simultaneously running timers
+static const int TimerIdMask = 0x00ffffff;
+static const int TimerSerialMask = ~TimerIdMask & ~0x80000000;
+static const int TimerSerialCounter = TimerIdMask + 1;
+static const int MaxTimerId = TimerIdMask;
+
+static int FirstBucket[] = {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
+};
+
+enum {
+ FirstBucketOffset = 0,
+ SecondBucketOffset = sizeof(FirstBucket) / sizeof(FirstBucket[0]),
+ ThirdBucketOffset = 0x100,
+ FourthBucketOffset = 0x1000,
+ FifthBucketOffset = 0x10000,
+ SixthBucketOffset = 0x100000
+};
+
+enum {
+ FirstBucketSize = SecondBucketOffset,
+ SecondBucketSize = ThirdBucketOffset - SecondBucketOffset,
+ ThirdBucketSize = FourthBucketOffset - ThirdBucketOffset,
+ FourthBucketSize = FifthBucketOffset - FourthBucketOffset,
+ FifthBucketSize = SixthBucketOffset - FifthBucketOffset,
+ SixthBucketSize = MaxTimerId - SixthBucketOffset
+};
+
+static const int BucketSize[] = {
+ FirstBucketSize, SecondBucketSize, ThirdBucketSize,
+ FourthBucketSize, FifthBucketSize, SixthBucketSize
+};
+enum { NumberOfBuckets = sizeof(BucketSize) / sizeof(BucketSize[0]) };
+
+static const int BucketOffset[] = {
+ FirstBucketOffset, SecondBucketOffset, ThirdBucketOffset,
+ FourthBucketOffset, FifthBucketOffset, SixthBucketOffset
+};
+
+static QBasicAtomicPointer<int> timerIds[] =
+ { Q_BASIC_ATOMIC_INITIALIZER(FirstBucket),
+ Q_BASIC_ATOMIC_INITIALIZER(0),
+ Q_BASIC_ATOMIC_INITIALIZER(0),
+ Q_BASIC_ATOMIC_INITIALIZER(0),
+ Q_BASIC_ATOMIC_INITIALIZER(0),
+ Q_BASIC_ATOMIC_INITIALIZER(0) };
+
+static void timerIdsDestructorFunction()
+{
+ // start at one, the first bucket is pre-allocated
+ for (int i = 1; i < NumberOfBuckets; ++i)
+ delete [] static_cast<int *>(timerIds[i]);
+}
+Q_DESTRUCTOR_FUNCTION(timerIdsDestructorFunction)
+
+static QBasicAtomicInt nextFreeTimerId = Q_BASIC_ATOMIC_INITIALIZER(1);
+
+// avoid the ABA-problem by using 7 of the top 8 bits of the timerId as a serial number
+static inline int prepareNewValueWithSerialNumber(int oldId, int newId)
+{
+ return (newId & TimerIdMask) | ((oldId + TimerSerialCounter) & TimerSerialMask);
+}
+
+namespace {
+ template<bool> struct QStaticAssertType;
+ template<> struct QStaticAssertType<true> { enum { Value = 1 }; };
+}
+#define q_static_assert(expr) (void)QStaticAssertType<expr>::Value
+
+static inline int bucketOffset(int timerId)
+{
+ q_static_assert(sizeof BucketSize == sizeof BucketOffset);
+ q_static_assert(sizeof(timerIds) / sizeof(timerIds[0]) == NumberOfBuckets);
+
+ for (int i = 0; i < NumberOfBuckets; ++i) {
+ if (timerId < BucketSize[i])
+ return i;
+ timerId -= BucketSize[i];
+ }
+ qFatal("QAbstractEventDispatcher: INTERNAL ERROR, timer ID %d is too large", timerId);
+ return -1;
+}
+
+static inline int bucketIndex(int bucket, int timerId)
+{
+ return timerId - BucketOffset[bucket];
+}
+
+static inline int *allocateBucket(int bucket)
+{
+ // allocate a new bucket
+ const int size = BucketSize[bucket];
+ const int offset = BucketOffset[bucket];
+ int *b = new int[size];
+ for (int i = 0; i != size; ++i)
+ b[i] = offset + i + 1;
+ return b;
+}
+
+void QAbstractEventDispatcherPrivate::init()
+{
+ Q_Q(QAbstractEventDispatcher);
+ if (threadData->eventDispatcher != 0) {
+ qWarning("QAbstractEventDispatcher: An event dispatcher has already been created for this thread");
+ } else {
+ threadData->eventDispatcher = q;
+ }
+}
+
+// Timer IDs are implemented using a free-list;
+// there's a vector initialized with:
+// X[i] = i + 1
+// and nextFreeTimerId starts with 1.
+//
+// Allocating a timer ID involves taking the ID from
+// X[nextFreeTimerId]
+// updating nextFreeTimerId to this value and returning the old value
+//
+// When the timer ID is allocated, its cell in the vector is unused (it's a
+// free list). As an added protection, we use the cell to store an invalid
+// (negative) value that we can later check for integrity.
+//
+// (continues below).
+int QAbstractEventDispatcherPrivate::allocateTimerId()
+{
+ int timerId, newTimerId;
+ int at, *b;
+ do {
+ timerId = nextFreeTimerId; //.loadAcquire(); // ### FIXME Proper memory ordering semantics
+
+ // which bucket are we looking in?
+ int which = timerId & TimerIdMask;
+ int bucket = bucketOffset(which);
+ at = bucketIndex(bucket, which);
+ b = timerIds[bucket];
+
+ if (!b) {
+ // allocate a new bucket
+ b = allocateBucket(bucket);
+ if (!timerIds[bucket].testAndSetRelease(0, b)) {
+ // another thread won the race to allocate the bucket
+ delete [] b;
+ b = timerIds[bucket];
+ }
+ }
+
+ newTimerId = prepareNewValueWithSerialNumber(timerId, b[at]);
+ } while (!nextFreeTimerId.testAndSetRelaxed(timerId, newTimerId));
+
+ b[at] = -timerId;
+
+ return timerId;
+}
+
+// Releasing a timer ID requires putting the current ID back in the vector;
+// we do it by setting:
+// X[timerId] = nextFreeTimerId;
+// then we update nextFreeTimerId to the timer we've just released
+//
+// The extra code in allocateTimerId and releaseTimerId are ABA prevention
+// and bucket memory. The buckets are simply to make sure we allocate only
+// the necessary number of timers. See above.
+//
+// ABA prevention simply adds a value to 7 of the top 8 bits when resetting
+// nextFreeTimerId.
+void QAbstractEventDispatcherPrivate::releaseTimerId(int timerId)
+{
+ int which = timerId & TimerIdMask;
+ int bucket = bucketOffset(which);
+ int at = bucketIndex(bucket, which);
+ int *b = timerIds[bucket];
+
+ Q_ASSERT_X(timerId == -b[at], "QAbstractEventDispatcher::releaseTimerId",
+ "Internal error: timer ID not found");
+
+ int freeId, newTimerId;
+ do {
+ freeId = nextFreeTimerId;//.loadAcquire(); // ### FIXME Proper memory ordering semantics
+ b[at] = freeId & TimerIdMask;
+
+ newTimerId = prepareNewValueWithSerialNumber(freeId, timerId);
+ } while (!nextFreeTimerId.testAndSetRelease(freeId, newTimerId));
+}
+
+/*!
+ \class QAbstractEventDispatcher
+ \brief The QAbstractEventDispatcher class provides an interface to manage Qt's event queue.
+
+ \ingroup events
+
+ An event dispatcher receives events from the window system and other
+ sources. It then sends them to the QCoreApplication or QApplication
+ instance for processing and delivery. QAbstractEventDispatcher provides
+ fine-grained control over event delivery.
+
+ For simple control of event processing use
+ QCoreApplication::processEvents().
+
+ For finer control of the application's event loop, call
+ instance() and call functions on the QAbstractEventDispatcher
+ object that is returned. If you want to use your own instance of
+ QAbstractEventDispatcher or of a QAbstractEventDispatcher
+ subclass, you must create your instance \e before you create the
+ QApplication object.
+
+ The main event loop is started by calling
+ QCoreApplication::exec(), and stopped by calling
+ QCoreApplication::exit(). Local event loops can be created using
+ QEventLoop.
+
+ Programs that perform long operations can call processEvents()
+ with a bitwise OR combination of various QEventLoop::ProcessEventsFlag
+ values to control which events should be delivered.
+
+ QAbstractEventDispatcher also allows the integration of an
+ external event loop with the Qt event loop. For example, the
+ \l{Qt Solutions}{Motif Extension Qt Solution} includes a
+ reimplementation of QAbstractEventDispatcher that merges Qt and
+ Motif events together.
+
+ \sa QEventLoop, QCoreApplication
+*/
+
+/*!
+ Constructs a new event dispatcher with the given \a parent.
+*/
+QAbstractEventDispatcher::QAbstractEventDispatcher(QObject *parent)
+ : QObject(*new QAbstractEventDispatcherPrivate, parent)
+{
+ Q_D(QAbstractEventDispatcher);
+ d->init();
+}
+
+/*!
+ \internal
+*/
+QAbstractEventDispatcher::QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &dd,
+ QObject *parent)
+ : QObject(dd, parent)
+{
+ Q_D(QAbstractEventDispatcher);
+ d->init();
+}
+
+/*!
+ Destroys the event dispatcher.
+*/
+QAbstractEventDispatcher::~QAbstractEventDispatcher()
+{ }
+
+/*!
+ Returns a pointer to the event dispatcher object for the specified
+ \a thread. If \a thread is zero, the current thread is used. If no
+ event dispatcher exists for the specified thread, this function
+ returns 0.
+
+ \bold{Note:} If Qt is built without thread support, the \a thread
+ argument is ignored.
+ */
+QAbstractEventDispatcher *QAbstractEventDispatcher::instance(QThread *thread)
+{
+ QThreadData *data = thread ? QThreadData::get2(thread) : QThreadData::current();
+ return data->eventDispatcher;
+}
+
+/*!
+ \fn bool QAbstractEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
+
+ Processes pending events that match \a flags until there are no
+ more events to process. Returns true if an event was processed;
+ otherwise returns false.
+
+ This function is especially useful if you have a long running
+ operation and want to show its progress without allowing user
+ input; i.e. by using the QEventLoop::ExcludeUserInputEvents flag.
+
+ If the QEventLoop::WaitForMoreEvents flag is set in \a flags, the
+ behavior of this function is as follows:
+
+ \list
+
+ \i If events are available, this function returns after processing
+ them.
+
+ \i If no events are available, this function will wait until more
+ are available and return after processing newly available events.
+
+ \endlist
+
+ If the QEventLoop::WaitForMoreEvents flag is not set in \a flags,
+ and no events are available, this function will return
+ immediately.
+
+ \bold{Note:} This function does not process events continuously; it
+ returns after all available events are processed.
+
+ \sa hasPendingEvents()
+*/
+
+/*! \fn bool QAbstractEventDispatcher::hasPendingEvents()
+
+ Returns true if there is an event waiting; otherwise returns
+ false.
+*/
+
+/*!
+ \fn void QAbstractEventDispatcher::registerSocketNotifier(QSocketNotifier *notifier)
+
+ Registers \a notifier with the event loop. Subclasses must
+ implement this method to tie a socket notifier into another
+ event loop.
+*/
+
+/*! \fn void QAbstractEventDispatcher::unregisterSocketNotifier(QSocketNotifier *notifier)
+
+ Unregisters \a notifier from the event dispatcher. Subclasses must
+ reimplement this method to tie a socket notifier into another
+ event loop. Reimplementations must call the base
+ implementation.
+*/
+
+/*!
+ \fn int QAbstractEventDispatcher::registerTimer(int interval, QObject *object)
+
+ Registers a timer with the specified \a interval for the given \a object.
+*/
+int QAbstractEventDispatcher::registerTimer(int interval, QObject *object)
+{
+ int id = QAbstractEventDispatcherPrivate::allocateTimerId();
+ registerTimer(id, interval, object);
+ return id;
+}
+
+/*!
+ \fn void QAbstractEventDispatcher::registerTimer(int timerId, int interval, QObject *object)
+
+ Register a timer with the specified \a timerId and \a interval for
+ the given \a object.
+*/
+
+/*!
+ \fn bool QAbstractEventDispatcher::unregisterTimer(int timerId)
+
+ Unregisters the timer with the given \a timerId.
+ Returns true if successful; otherwise returns false.
+
+ \sa registerTimer(), unregisterTimers()
+*/
+
+/*!
+ \fn bool QAbstractEventDispatcher::unregisterTimers(QObject *object)
+
+ Unregisters all the timers associated with the given \a object.
+ Returns true if all timers were successful removed; otherwise returns false.
+
+ \sa unregisterTimer(), registeredTimers()
+*/
+
+/*!
+ \fn QList<TimerInfo> QAbstractEventDispatcher::registeredTimers(QObject *object) const
+
+ Returns a list of registered timers for \a object. The timer ID
+ is the first member in each pair; the interval is the second.
+*/
+
+/*! \fn void QAbstractEventDispatcher::wakeUp()
+ \threadsafe
+
+ Wakes up the event loop.
+
+ \sa awake()
+*/
+
+/*!
+ \fn void QAbstractEventDispatcher::interrupt()
+
+ Interrupts event dispatching; i.e. the event dispatcher will
+ return from processEvents() as soon as possible.
+*/
+
+/*! \fn void QAbstractEventDispatcher::flush()
+
+ Flushes the event queue. This normally returns almost
+ immediately. Does nothing on platforms other than X11.
+*/
+
+// ### DOC: Are these called when the _application_ starts/stops or just
+// when the current _event loop_ starts/stops?
+/*! \internal */
+void QAbstractEventDispatcher::startingUp()
+{ }
+
+/*! \internal */
+void QAbstractEventDispatcher::closingDown()
+{ }
+
+/*!
+ \typedef QAbstractEventDispatcher::TimerInfo
+
+ Typedef for QPair<int, int>. The first component of
+ the pair is the timer ID; the second component is
+ the interval.
+
+ \sa registeredTimers()
+*/
+
+/*!
+ \typedef QAbstractEventDispatcher::EventFilter
+
+ Typedef for a function with the signature
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstracteventdispatcher.cpp 0
+
+ Note that the type of the \a message is platform dependent. The
+ following table shows the \a {message}'s type on Windows, Mac, and
+ X11. You can do a static cast to these types.
+
+ \table
+ \header
+ \o Platform
+ \o type
+ \row
+ \o Windows
+ \o MSG
+ \row
+ \o X11
+ \o XEvent
+ \row
+ \o Mac
+ \o NSEvent
+ \endtable
+
+
+
+ \sa setEventFilter(), filterEvent()
+*/
+
+/*!
+ Replaces the event filter function for this
+ QAbstractEventDispatcher with \a filter and returns the replaced
+ event filter function. Only the current event filter function is
+ called. If you want to use both filter functions, save the
+ replaced EventFilter in a place where yours can call it.
+
+ The event filter function set here is called for all messages
+ taken from the system event loop before the event is dispatched to
+ the respective target, including the messages not meant for Qt
+ objects.
+
+ The event filter function should return true if the message should
+ be filtered, (i.e. stopped). It should return false to allow
+ processing the message to continue.
+
+ By default, no event filter function is set (i.e., this function
+ returns a null EventFilter the first time it is called).
+*/
+QAbstractEventDispatcher::EventFilter QAbstractEventDispatcher::setEventFilter(EventFilter filter)
+{
+ Q_D(QAbstractEventDispatcher);
+ EventFilter oldFilter = d->event_filter;
+ d->event_filter = filter;
+ return oldFilter;
+}
+
+/*!
+ Sends \a message through the event filter that was set by
+ setEventFilter(). If no event filter has been set, this function
+ returns false; otherwise, this function returns the result of the
+ event filter function.
+
+ Subclasses of QAbstractEventDispatcher \e must call this function
+ for \e all messages received from the system to ensure
+ compatibility with any extensions that may be used in the
+ application.
+
+ Note that the type of \a message is platform dependent. See
+ QAbstractEventDispatcher::EventFilter for details.
+
+ \sa setEventFilter()
+*/
+bool QAbstractEventDispatcher::filterEvent(void *message)
+{
+ Q_D(QAbstractEventDispatcher);
+ if (d->event_filter)
+ return d->event_filter(message);
+ return false;
+}
+
+/*! \fn void QAbstractEventDispatcher::awake()
+
+ This signal is emitted after the event loop returns from a
+ function that could block.
+
+ \sa wakeUp() aboutToBlock()
+*/
+
+/*! \fn void QAbstractEventDispatcher::aboutToBlock()
+
+ This signal is emitted before the event loop calls a function that
+ could block.
+
+ \sa awake()
+*/
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qabstracteventdispatcher.h b/src/corelib/kernel/qabstracteventdispatcher.h
new file mode 100644
index 0000000000..212b832ae2
--- /dev/null
+++ b/src/corelib/kernel/qabstracteventdispatcher.h
@@ -0,0 +1,107 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QABSTRACTEVENTDISPATCHER_H
+#define QABSTRACTEVENTDISPATCHER_H
+
+#include <QtCore/qobject.h>
+#include <QtCore/qeventloop.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Core)
+
+class QAbstractEventDispatcherPrivate;
+class QSocketNotifier;
+template <typename T1, typename T2> struct QPair;
+
+class Q_CORE_EXPORT QAbstractEventDispatcher : public QObject
+{
+ Q_OBJECT
+ Q_DECLARE_PRIVATE(QAbstractEventDispatcher)
+
+public:
+ typedef QPair<int, int> TimerInfo;
+
+ explicit QAbstractEventDispatcher(QObject *parent = 0);
+ ~QAbstractEventDispatcher();
+
+ static QAbstractEventDispatcher *instance(QThread *thread = 0);
+
+ virtual bool processEvents(QEventLoop::ProcessEventsFlags flags) = 0;
+ virtual bool hasPendingEvents() = 0;
+
+ virtual void registerSocketNotifier(QSocketNotifier *notifier) = 0;
+ virtual void unregisterSocketNotifier(QSocketNotifier *notifier) = 0;
+
+ int registerTimer(int interval, QObject *object);
+ virtual void registerTimer(int timerId, int interval, QObject *object) = 0;
+ virtual bool unregisterTimer(int timerId) = 0;
+ virtual bool unregisterTimers(QObject *object) = 0;
+ virtual QList<TimerInfo> registeredTimers(QObject *object) const = 0;
+
+ virtual void wakeUp() = 0;
+ virtual void interrupt() = 0;
+ virtual void flush() = 0;
+
+ virtual void startingUp();
+ virtual void closingDown();
+
+ typedef bool(*EventFilter)(void *message);
+ EventFilter setEventFilter(EventFilter filter);
+ bool filterEvent(void *message);
+
+Q_SIGNALS:
+ void aboutToBlock();
+ void awake();
+
+protected:
+ QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &,
+ QObject *parent);
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QABSTRACTEVENTDISPATCHER_H
diff --git a/src/corelib/kernel/qabstracteventdispatcher_p.h b/src/corelib/kernel/qabstracteventdispatcher_p.h
new file mode 100644
index 0000000000..4a55dfba09
--- /dev/null
+++ b/src/corelib/kernel/qabstracteventdispatcher_p.h
@@ -0,0 +1,79 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QABSTRACTEVENTDISPATCHER_P_H
+#define QABSTRACTEVENTDISPATCHER_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qabstracteventdispatcher.h"
+#include "private/qobject_p.h"
+
+QT_BEGIN_NAMESPACE
+
+Q_CORE_EXPORT uint qGlobalPostedEventsCount();
+
+class Q_CORE_EXPORT QAbstractEventDispatcherPrivate : public QObjectPrivate
+{
+ Q_DECLARE_PUBLIC(QAbstractEventDispatcher)
+public:
+ inline QAbstractEventDispatcherPrivate()
+ : event_filter(0)
+ { }
+ void init();
+ QAbstractEventDispatcher::EventFilter event_filter;
+
+ static int allocateTimerId();
+ static void releaseTimerId(int id);
+};
+
+QT_END_NAMESPACE
+
+#endif // QABSTRACTEVENTDISPATCHER_P_H
diff --git a/src/corelib/kernel/qabstractitemmodel.cpp b/src/corelib/kernel/qabstractitemmodel.cpp
new file mode 100644
index 0000000000..9dd22dc39b
--- /dev/null
+++ b/src/corelib/kernel/qabstractitemmodel.cpp
@@ -0,0 +1,3454 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qabstractitemmodel.h"
+#include <private/qabstractitemmodel_p.h>
+#include <qdatastream.h>
+#include <qstringlist.h>
+#include <qsize.h>
+#include <qmimedata.h>
+#include <qdebug.h>
+#include <qvector.h>
+#include <qstack.h>
+#include <qbitarray.h>
+
+#include <limits.h>
+
+QT_BEGIN_NAMESPACE
+
+QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index)
+{
+ Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list
+ QPersistentModelIndexData *d = 0;
+ QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model());
+ QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes;
+ const QHash<QModelIndex, QPersistentModelIndexData *>::iterator it = indexes.find(index);
+ if (it != indexes.end()) {
+ d = (*it);
+ } else {
+ d = new QPersistentModelIndexData(index);
+ indexes.insert(index, d);
+ }
+ Q_ASSERT(d);
+ return d;
+}
+
+void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
+{
+ Q_ASSERT(data);
+ Q_ASSERT(data->ref == 0);
+ QAbstractItemModel *model = const_cast<QAbstractItemModel *>(data->model);
+ // a valid persistent model index with a null model pointer can only happen if the model was destroyed
+ if (model) {
+ QAbstractItemModelPrivate *p = model->d_func();
+ Q_ASSERT(p);
+ p->removePersistentIndexData(data);
+ }
+ delete data;
+}
+
+/*!
+ \class QPersistentModelIndex
+
+ \brief The QPersistentModelIndex class is used to locate data in a data model.
+
+ \ingroup model-view
+
+ A QPersistentModelIndex is a model index that can be stored by an
+ application, and later used to access information in a model.
+ Unlike the QModelIndex class, it is safe to store a
+ QPersistentModelIndex since the model will ensure that references
+ to items will continue to be valid as long as they can be accessed
+ by the model.
+
+ It is good practice to check that persistent model indexes are valid
+ before using them.
+
+ \sa {Model/View Programming}, QModelIndex, QAbstractItemModel
+*/
+
+
+/*!
+ \fn QPersistentModelIndex::QPersistentModelIndex()
+
+ \internal
+*/
+
+QPersistentModelIndex::QPersistentModelIndex()
+ : d(0)
+{
+}
+
+/*!
+ \fn QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
+
+ Creates a new QPersistentModelIndex that is a copy of the \a other persistent
+ model index.
+*/
+
+QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
+ : d(other.d)
+{
+ if (d) d->ref.ref();
+}
+
+/*!
+ Creates a new QPersistentModelIndex that is a copy of the model \a index.
+*/
+
+QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index)
+ : d(0)
+{
+ if (index.isValid()) {
+ d = QPersistentModelIndexData::create(index);
+ d->ref.ref();
+ }
+}
+
+/*!
+ \fn QPersistentModelIndex::~QPersistentModelIndex()
+
+ \internal
+*/
+
+QPersistentModelIndex::~QPersistentModelIndex()
+{
+ if (d && !d->ref.deref()) {
+ QPersistentModelIndexData::destroy(d);
+ d = 0;
+ }
+}
+
+/*!
+ Returns true if this persistent model index is equal to the \a other
+ persistent model index; otherwise returns false.
+
+ All values in the persistent model index are used when comparing
+ with another persistent model index.
+*/
+
+bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const
+{
+ if (d && other.d)
+ return d->index == other.d->index;
+ return d == other.d;
+}
+
+/*!
+ \since 4.1
+
+ Returns true if this persistent model index is smaller than the \a other
+ persistent model index; otherwise returns false.
+
+ All values in the persistent model index are used when comparing
+ with another persistent model index.
+*/
+
+bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const
+{
+ if (d && other.d)
+ return d->index < other.d->index;
+
+ return d < other.d;
+}
+
+/*!
+ \fn bool QPersistentModelIndex::operator!=(const QPersistentModelIndex &other) const
+ \since 4.2
+
+ Returns true if this persistent model index is not equal to the \a
+ other persistent model index; otherwise returns false.
+*/
+
+/*!
+ Sets the persistent model index to refer to the same item in a model
+ as the \a other persistent model index.
+*/
+
+QPersistentModelIndex &QPersistentModelIndex::operator=(const QPersistentModelIndex &other)
+{
+ if (d == other.d)
+ return *this;
+ if (d && !d->ref.deref())
+ QPersistentModelIndexData::destroy(d);
+ d = other.d;
+ if (d) d->ref.ref();
+ return *this;
+}
+
+/*!
+ Sets the persistent model index to refer to the same item in a model
+ as the \a other model index.
+*/
+
+QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other)
+{
+ if (d && !d->ref.deref())
+ QPersistentModelIndexData::destroy(d);
+ if (other.isValid()) {
+ d = QPersistentModelIndexData::create(other);
+ if (d) d->ref.ref();
+ } else {
+ d = 0;
+ }
+ return *this;
+}
+
+/*!
+ \fn QPersistentModelIndex::operator const QModelIndex&() const
+
+ Cast operator that returns a const QModelIndex&.
+*/
+
+QPersistentModelIndex::operator const QModelIndex&() const
+{
+ static const QModelIndex invalid;
+ if (d)
+ return d->index;
+ return invalid;
+}
+
+/*!
+ \fn bool QPersistentModelIndex::operator==(const QModelIndex &other) const
+
+ Returns true if this persistent model index refers to the same location as
+ the \a other model index; otherwise returns false.
+
+ All values in the persistent model index are used when comparing with
+ another model index.
+*/
+
+bool QPersistentModelIndex::operator==(const QModelIndex &other) const
+{
+ if (d)
+ return d->index == other;
+ return !other.isValid();
+}
+
+/*!
+ \fn bool QPersistentModelIndex::operator!=(const QModelIndex &other) const
+
+ Returns true if this persistent model index does not refer to the same
+ location as the \a other model index; otherwise returns false.
+*/
+
+bool QPersistentModelIndex::operator!=(const QModelIndex &other) const
+{
+ if (d)
+ return d->index != other;
+ return other.isValid();
+}
+
+/*!
+ \fn int QPersistentModelIndex::row() const
+
+ Returns the row this persistent model index refers to.
+*/
+
+int QPersistentModelIndex::row() const
+{
+ if (d)
+ return d->index.row();
+ return -1;
+}
+
+/*!
+ \fn int QPersistentModelIndex::column() const
+
+ Returns the column this persistent model index refers to.
+*/
+
+int QPersistentModelIndex::column() const
+{
+ if (d)
+ return d->index.column();
+ return -1;
+}
+
+/*!
+ \fn void *QPersistentModelIndex::internalPointer() const
+
+ \internal
+
+ Returns a \c{void} \c{*} pointer used by the model to associate the index with
+ the internal data structure.
+*/
+
+void *QPersistentModelIndex::internalPointer() const
+{
+ if (d)
+ return d->index.internalPointer();
+ return 0;
+}
+
+/*!
+ \fn void *QPersistentModelIndex::internalId() const
+
+ \internal
+
+ Returns a \c{qint64} used by the model to associate the index with
+ the internal data structure.
+*/
+
+qint64 QPersistentModelIndex::internalId() const
+{
+ if (d)
+ return d->index.internalId();
+ return 0;
+}
+
+/*!
+ Returns the parent QModelIndex for this persistent index, or an invalid
+ QModelIndex if it has no parent.
+
+ \sa child() sibling() model()
+*/
+QModelIndex QPersistentModelIndex::parent() const
+{
+ if (d)
+ return d->index.parent();
+ return QModelIndex();
+}
+
+/*!
+ Returns the sibling at \a row and \a column or an invalid QModelIndex if
+ there is no sibling at this position.
+
+ \sa parent() child()
+*/
+
+QModelIndex QPersistentModelIndex::sibling(int row, int column) const
+{
+ if (d)
+ return d->index.sibling(row, column);
+ return QModelIndex();
+}
+
+/*!
+ Returns the child of the model index that is stored in the given \a row
+ and \a column.
+
+ \sa parent() sibling()
+*/
+
+QModelIndex QPersistentModelIndex::child(int row, int column) const
+{
+ if (d)
+ return d->index.child(row, column);
+ return QModelIndex();
+}
+
+/*!
+ Returns the data for the given \a role for the item referred to by the
+ index.
+
+ \sa Qt::ItemDataRole, QAbstractItemModel::setData()
+*/
+QVariant QPersistentModelIndex::data(int role) const
+{
+ if (d)
+ return d->index.data(role);
+ return QVariant();
+}
+
+/*!
+ \since 4.2
+
+ Returns the flags for the item referred to by the index.
+*/
+Qt::ItemFlags QPersistentModelIndex::flags() const
+{
+ if (d)
+ return d->index.flags();
+ return 0;
+}
+
+/*!
+ Returns the model that the index belongs to.
+*/
+const QAbstractItemModel *QPersistentModelIndex::model() const
+{
+ if (d)
+ return d->index.model();
+ return 0;
+}
+
+/*!
+ \fn bool QPersistentModelIndex::isValid() const
+
+ Returns true if this persistent model index is valid; otherwise returns
+ false.
+
+ A valid index belongs to a model, and has non-negative row and column
+ numbers.
+
+ \sa model(), row(), column()
+*/
+
+bool QPersistentModelIndex::isValid() const
+{
+ return d && d->index.isValid();
+}
+
+#ifndef QT_NO_DEBUG_STREAM
+QDebug operator<<(QDebug dbg, const QModelIndex &idx)
+{
+#ifndef Q_BROKEN_DEBUG_STREAM
+ dbg.nospace() << "QModelIndex(" << idx.row() << ',' << idx.column()
+ << ',' << idx.internalPointer() << ',' << idx.model() << ')';
+ return dbg.space();
+#else
+ qWarning("This compiler doesn't support streaming QModelIndex to QDebug");
+ return dbg;
+ Q_UNUSED(idx);
+#endif
+}
+
+QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)
+{
+ if (idx.d)
+ dbg << idx.d->index;
+ else
+ dbg << QModelIndex();
+ return dbg;
+}
+#endif
+
+class QEmptyItemModel : public QAbstractItemModel
+{
+public:
+ explicit QEmptyItemModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
+ QModelIndex index(int, int, const QModelIndex &) const { return QModelIndex(); }
+ QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
+ int rowCount(const QModelIndex &) const { return 0; }
+ int columnCount(const QModelIndex &) const { return 0; }
+ bool hasChildren(const QModelIndex &) const { return false; }
+ QVariant data(const QModelIndex &, int) const { return QVariant(); }
+};
+
+Q_GLOBAL_STATIC(QEmptyItemModel, qEmptyModel)
+
+QAbstractItemModel *QAbstractItemModelPrivate::staticEmptyModel()
+{
+ return qEmptyModel();
+}
+
+namespace {
+ struct DefaultRoleNames : public QHash<int, QByteArray>
+ {
+ DefaultRoleNames() {
+ (*this)[Qt::DisplayRole] = "display";
+ (*this)[Qt::DecorationRole] = "decoration";
+ (*this)[Qt::EditRole] = "edit";
+ (*this)[Qt::ToolTipRole] = "toolTip";
+ (*this)[Qt::StatusTipRole] = "statusTip";
+ (*this)[Qt::WhatsThisRole] = "whatsThis";
+ }
+ };
+}
+
+Q_GLOBAL_STATIC(DefaultRoleNames, qDefaultRoleNames)
+
+const QHash<int,QByteArray> &QAbstractItemModelPrivate::defaultRoleNames()
+{
+ return *qDefaultRoleNames();
+}
+
+
+static uint typeOfVariant(const QVariant &value)
+{
+ //return 0 for integer, 1 for floating point and 2 for other
+ switch (value.userType()) {
+ case QVariant::Bool:
+ case QVariant::Int:
+ case QVariant::UInt:
+ case QVariant::LongLong:
+ case QVariant::ULongLong:
+ case QVariant::Char:
+ case QMetaType::Short:
+ case QMetaType::UShort:
+ case QMetaType::UChar:
+ case QMetaType::ULong:
+ case QMetaType::Long:
+ return 0;
+ case QVariant::Double:
+ case QMetaType::Float:
+ return 1;
+ default:
+ return 2;
+ }
+}
+
+/*!
+ \internal
+ return true if \a value contains a numerical type
+
+ This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort.
+*/
+bool QAbstractItemModelPrivate::variantLessThan(const QVariant &v1, const QVariant &v2)
+{
+ switch(qMax(typeOfVariant(v1), typeOfVariant(v2)))
+ {
+ case 0: //integer type
+ return v1.toLongLong() < v2.toLongLong();
+ case 1: //floating point
+ return v1.toReal() < v2.toReal();
+ default:
+ return v1.toString().localeAwareCompare(v2.toString()) < 0;
+ }
+}
+
+void QAbstractItemModelPrivate::removePersistentIndexData(QPersistentModelIndexData *data)
+{
+ if (data->index.isValid()) {
+ int removed = persistent.indexes.remove(data->index);
+ Q_ASSERT_X(removed == 1, "QPersistentModelIndex::~QPersistentModelIndex",
+ "persistent model indexes corrupted"); //maybe the index was somewhat invalid?
+ // This assert may happen if the model use changePersistentIndex in a way that could result on two
+ // QPersistentModelIndex pointing to the same index.
+ Q_UNUSED(removed);
+ }
+ // make sure our optimization still works
+ for (int i = persistent.moved.count() - 1; i >= 0; --i) {
+ int idx = persistent.moved[i].indexOf(data);
+ if (idx >= 0)
+ persistent.moved[i].remove(idx);
+ }
+ // update the references to invalidated persistent indexes
+ for (int i = persistent.invalidated.count() - 1; i >= 0; --i) {
+ int idx = persistent.invalidated[i].indexOf(data);
+ if (idx >= 0)
+ persistent.invalidated[i].remove(idx);
+ }
+
+}
+
+void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
+ int first, int last)
+{
+ Q_Q(QAbstractItemModel);
+ Q_UNUSED(last);
+ QVector<QPersistentModelIndexData *> persistent_moved;
+ if (first < q->rowCount(parent)) {
+ for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
+ it != persistent.indexes.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ const QModelIndex &index = data->index;
+ if (index.row() >= first && index.isValid() && index.parent() == parent) {
+ persistent_moved.append(data);
+ }
+ }
+ }
+ persistent.moved.push(persistent_moved);
+}
+
+void QAbstractItemModelPrivate::rowsInserted(const QModelIndex &parent,
+ int first, int last)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
+ int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin();
+ it != persistent_moved.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ QModelIndex old = data->index;
+ persistent.indexes.erase(persistent.indexes.find(old));
+ data->index = q_func()->index(old.row() + count, old.column(), parent);
+ if (data->index.isValid()) {
+ persistent.insertMultiAtEnd(data->index, data);
+ } else {
+ qWarning() << "QAbstractItemModel::endInsertRows: Invalid index (" << old.row() + count << ',' << old.column() << ") in model" << q_func();
+ }
+ }
+}
+
+void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved_explicitly;
+ QVector<QPersistentModelIndexData *> persistent_moved_in_source;
+ QVector<QPersistentModelIndexData *> persistent_moved_in_destination;
+
+ QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it;
+ const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator begin = persistent.indexes.constBegin();
+ const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator end = persistent.indexes.constEnd();
+
+ const bool sameParent = (srcParent == destinationParent);
+ const bool movingUp = (srcFirst > destinationChild);
+
+ for ( it = begin; it != end; ++it) {
+ QPersistentModelIndexData *data = *it;
+ const QModelIndex &index = data->index;
+ const QModelIndex &parent = index.parent();
+ const bool isSourceIndex = (parent == srcParent);
+ const bool isDestinationIndex = (parent == destinationParent);
+
+ int childPosition;
+ if (orientation == Qt::Vertical)
+ childPosition = index.row();
+ else
+ childPosition = index.column();
+
+ if (!index.isValid() || !(isSourceIndex || isDestinationIndex ) )
+ continue;
+
+ if (!sameParent && isDestinationIndex) {
+ if (childPosition >= destinationChild)
+ persistent_moved_in_destination.append(data);
+ continue;
+ }
+
+ if (sameParent && movingUp && childPosition < destinationChild)
+ continue;
+
+ if (sameParent && !movingUp && childPosition < srcFirst )
+ continue;
+
+ if (!sameParent && childPosition < srcFirst)
+ continue;
+
+ if (sameParent && (childPosition > srcLast) && (childPosition >= destinationChild ))
+ continue;
+
+ if ((childPosition <= srcLast) && (childPosition >= srcFirst)) {
+ persistent_moved_explicitly.append(data);
+ } else {
+ persistent_moved_in_source.append(data);
+ }
+ }
+ persistent.moved.push(persistent_moved_explicitly);
+ persistent.moved.push(persistent_moved_in_source);
+ persistent.moved.push(persistent_moved_in_destination);
+}
+
+/*!
+ \internal
+
+ Moves persistent indexes \a indexes by amount \a change. The change will be either a change in row value or a change in
+ column value depending on the value of \a orientation. The indexes may also be moved to a different parent if \a parent
+ differs from the existing parent for the index.
+*/
+void QAbstractItemModelPrivate::movePersistentIndexes(QVector<QPersistentModelIndexData *> indexes, int change, const QModelIndex &parent, Qt::Orientation orientation)
+{
+ QVector<QPersistentModelIndexData *>::const_iterator it;
+ const QVector<QPersistentModelIndexData *>::const_iterator begin = indexes.constBegin();
+ const QVector<QPersistentModelIndexData *>::const_iterator end = indexes.constEnd();
+
+ for (it = begin; it != end; ++it)
+ {
+ QPersistentModelIndexData *data = *it;
+
+ int row = data->index.row();
+ int column = data->index.column();
+
+ if (Qt::Vertical == orientation)
+ row += change;
+ else
+ column += change;
+
+ persistent.indexes.erase(persistent.indexes.find(data->index));
+ data->index = q_func()->index(row, column, parent);
+ if (data->index.isValid()) {
+ persistent.insertMultiAtEnd(data->index, data);
+ } else {
+ qWarning() << "QAbstractItemModel::endMoveRows: Invalid index (" << row << "," << column << ") in model" << q_func();
+ }
+ }
+}
+
+void QAbstractItemModelPrivate::itemsMoved(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation)
+{
+ QVector<QPersistentModelIndexData *> moved_in_destination = persistent.moved.pop();
+ QVector<QPersistentModelIndexData *> moved_in_source = persistent.moved.pop();
+ QVector<QPersistentModelIndexData *> moved_explicitly = persistent.moved.pop();
+
+ const bool sameParent = (sourceParent == destinationParent);
+ const bool movingUp = (sourceFirst > destinationChild);
+
+ const int explicit_change = (!sameParent || movingUp) ? destinationChild - sourceFirst : destinationChild - sourceLast - 1 ;
+ const int source_change = (!sameParent || !movingUp) ? -1*(sourceLast - sourceFirst + 1) : sourceLast - sourceFirst + 1 ;
+ const int destination_change = sourceLast - sourceFirst + 1;
+
+ movePersistentIndexes(moved_explicitly, explicit_change, destinationParent, orientation);
+ movePersistentIndexes(moved_in_source, source_change, sourceParent, orientation);
+ movePersistentIndexes(moved_in_destination, destination_change, destinationParent, orientation);
+}
+
+void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
+ int first, int last)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved;
+ QVector<QPersistentModelIndexData *> persistent_invalidated;
+ // find the persistent indexes that are affected by the change, either by being in the removed subtree
+ // or by being on the same level and below the removed rows
+ for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
+ it != persistent.indexes.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ bool level_changed = false;
+ QModelIndex current = data->index;
+ while (current.isValid()) {
+ QModelIndex current_parent = current.parent();
+ if (current_parent == parent) { // on the same level as the change
+ if (!level_changed && current.row() > last) // below the removed rows
+ persistent_moved.append(data);
+ else if (current.row() <= last && current.row() >= first) // in the removed subtree
+ persistent_invalidated.append(data);
+ break;
+ }
+ current = current_parent;
+ level_changed = true;
+ }
+ }
+
+ persistent.moved.push(persistent_moved);
+ persistent.invalidated.push(persistent_invalidated);
+}
+
+void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent,
+ int first, int last)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
+ int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin();
+ it != persistent_moved.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ QModelIndex old = data->index;
+ persistent.indexes.erase(persistent.indexes.find(old));
+ data->index = q_func()->index(old.row() - count, old.column(), parent);
+ if (data->index.isValid()) {
+ persistent.insertMultiAtEnd(data->index, data);
+ } else {
+ qWarning() << "QAbstractItemModel::endRemoveRows: Invalid index (" << old.row() - count << ',' << old.column() << ") in model" << q_func();
+ }
+ }
+ QVector<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop();
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin();
+ it != persistent_invalidated.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ persistent.indexes.erase(persistent.indexes.find(data->index));
+ data->index = QModelIndex();
+ data->model = 0;
+ }
+}
+
+void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &parent,
+ int first, int last)
+{
+ Q_Q(QAbstractItemModel);
+ Q_UNUSED(last);
+ QVector<QPersistentModelIndexData *> persistent_moved;
+ if (first < q->columnCount(parent)) {
+ for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
+ it != persistent.indexes.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ const QModelIndex &index = data->index;
+ if (index.column() >= first && index.isValid() && index.parent() == parent)
+ persistent_moved.append(data);
+ }
+ }
+ persistent.moved.push(persistent_moved);
+}
+
+void QAbstractItemModelPrivate::columnsInserted(const QModelIndex &parent,
+ int first, int last)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
+ int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin();
+ it != persistent_moved.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ QModelIndex old = data->index;
+ persistent.indexes.erase(persistent.indexes.find(old));
+ data->index = q_func()->index(old.row(), old.column() + count, parent);
+ if (data->index.isValid()) {
+ persistent.insertMultiAtEnd(data->index, data);
+ } else {
+ qWarning() << "QAbstractItemModel::endInsertColumns: Invalid index (" << old.row() << ',' << old.column() + count << ") in model" << q_func();
+ }
+ }
+}
+
+void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &parent,
+ int first, int last)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved;
+ QVector<QPersistentModelIndexData *> persistent_invalidated;
+ // find the persistent indexes that are affected by the change, either by being in the removed subtree
+ // or by being on the same level and to the right of the removed columns
+ for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
+ it != persistent.indexes.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ bool level_changed = false;
+ QModelIndex current = data->index;
+ while (current.isValid()) {
+ QModelIndex current_parent = current.parent();
+ if (current_parent == parent) { // on the same level as the change
+ if (!level_changed && current.column() > last) // right of the removed columns
+ persistent_moved.append(data);
+ else if (current.column() <= last && current.column() >= first) // in the removed subtree
+ persistent_invalidated.append(data);
+ break;
+ }
+ current = current_parent;
+ level_changed = true;
+ }
+ }
+
+ persistent.moved.push(persistent_moved);
+ persistent.invalidated.push(persistent_invalidated);
+
+}
+
+void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent,
+ int first, int last)
+{
+ QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
+ int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin();
+ it != persistent_moved.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ QModelIndex old = data->index;
+ persistent.indexes.erase(persistent.indexes.find(old));
+ data->index = q_func()->index(old.row(), old.column() - count, parent);
+ if (data->index.isValid()) {
+ persistent.insertMultiAtEnd(data->index, data);
+ } else {
+ qWarning() << "QAbstractItemModel::endRemoveColumns: Invalid index (" << old.row() << ',' << old.column() - count << ") in model" << q_func();
+ }
+ }
+ QVector<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop();
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin();
+ it != persistent_invalidated.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ persistent.indexes.erase(persistent.indexes.find(data->index));
+ data->index = QModelIndex();
+ data->model = 0;
+ }
+}
+
+/*!
+ \class QModelIndex
+
+ \brief The QModelIndex class is used to locate data in a data model.
+
+ \ingroup model-view
+
+
+ This class is used as an index into item models derived from
+ QAbstractItemModel. The index is used by item views, delegates, and
+ selection models to locate an item in the model.
+
+ New QModelIndex objects are created by the model using the
+ QAbstractItemModel::createIndex() function. An \e invalid model index can
+ be constructed with the QModelIndex constructor. Invalid indexes are often
+ used as parent indexes when referring to top-level items in a model.
+
+ Model indexes refer to items in models, and contain all the information
+ required to specify their locations in those models. Each index is located
+ in a given row and column, and may have a parent index; use row(),
+ column(), and parent() to obtain this information. Each top-level item in a
+ model is represented by a model index that does not have a parent index -
+ in this case, parent() will return an invalid model index, equivalent to an
+ index constructed with the zero argument form of the QModelIndex()
+ constructor.
+
+ To obtain a model index that refers to an existing item in a model, call
+ QAbstractItemModel::index() with the required row and column values, and
+ the model index of the parent. When referring to top-level items in a
+ model, supply QModelIndex() as the parent index.
+
+ The model() function returns the model that the index references as a
+ QAbstractItemModel. The child() function is used to examine items held
+ under the index in the model. The sibling() function allows you to traverse
+ items in the model on the same level as the index.
+
+ \note Model indexes should be used immediately and then discarded. You
+ should not rely on indexes to remain valid after calling model functions
+ that change the structure of the model or delete items. If you need to
+ keep a model index over time use a QPersistentModelIndex.
+
+ \sa {Model/View Programming}, QPersistentModelIndex, QAbstractItemModel
+*/
+
+/*!
+ \fn QModelIndex::QModelIndex()
+
+ Creates a new empty model index. This type of model index is used to
+ indicate that the position in the model is invalid.
+
+ \sa isValid() QAbstractItemModel
+*/
+
+/*!
+ \fn QModelIndex::QModelIndex(int row, int column, void *data, const QAbstractItemModel *model)
+
+ \internal
+
+ Creates a new model index at the given \a row and \a column,
+ pointing to some \a data.
+*/
+
+/*!
+ \fn QModelIndex::QModelIndex(const QModelIndex &other)
+
+ Creates a new model index that is a copy of the \a other model
+ index.
+*/
+
+/*!
+ \fn QModelIndex::~QModelIndex()
+
+ Destroys the model index.
+*/
+
+/*!
+ \fn int QModelIndex::row() const
+
+ Returns the row this model index refers to.
+*/
+
+
+/*!
+ \fn int QModelIndex::column() const
+
+ Returns the column this model index refers to.
+*/
+
+
+/*!
+ \fn void *QModelIndex::internalPointer() const
+
+ Returns a \c{void} \c{*} pointer used by the model to associate
+ the index with the internal data structure.
+
+ \sa QAbstractItemModel::createIndex()
+*/
+
+/*!
+ \fn void *QModelIndex::internalId() const
+
+ Returns a \c{qint64} used by the model to associate
+ the index with the internal data structure.
+
+ \sa QAbstractItemModel::createIndex()
+*/
+
+/*!
+ \fn bool QModelIndex::isValid() const
+
+ Returns true if this model index is valid; otherwise returns false.
+
+ A valid index belongs to a model, and has non-negative row and column
+ numbers.
+
+ \sa model(), row(), column()
+*/
+
+/*!
+ \fn const QAbstractItemModel *QModelIndex::model() const
+
+ Returns a pointer to the model containing the item that this index
+ refers to.
+
+ A const pointer to the model is returned because calls to non-const
+ functions of the model might invalidate the model index and possibly
+ crash your application.
+*/
+
+/*!
+ \fn QModelIndex QModelIndex::sibling(int row, int column) const
+
+ Returns the sibling at \a row and \a column. If there is no sibling at this
+ position, an invalid QModelIndex is returned.
+
+ \sa parent(), child()
+*/
+
+/*!
+ \fn QModelIndex QModelIndex::child(int row, int column) const
+
+ Returns the child of the model index that is stored in the given \a row and
+ \a column.
+
+ \note This function does not work for an invalid model index which is often
+ used as the root index.
+
+ \sa parent(), sibling()
+*/
+
+/*!
+ \fn QVariant QModelIndex::data(int role) const
+
+ Returns the data for the given \a role for the item referred to by the
+ index.
+*/
+
+/*!
+ \fn Qt::ItemFlags QModelIndex::flags() const
+ \since 4.2
+
+ Returns the flags for the item referred to by the index.
+*/
+
+/*!
+ \fn bool QModelIndex::operator==(const QModelIndex &other) const
+
+ Returns true if this model index refers to the same location as the
+ \a other model index; otherwise returns false.
+
+ All values in the model index are used when comparing with another model
+ index.
+*/
+
+
+/*!
+ \fn bool QModelIndex::operator!=(const QModelIndex &other) const
+
+ Returns true if this model index does not refer to the same location as
+ the \a other model index; otherwise returns false.
+*/
+
+
+/*!
+ \fn QModelIndex QModelIndex::parent() const
+
+ Returns the parent of the model index, or QModelIndex() if it has no
+ parent.
+
+ \sa child(), sibling(), model()
+*/
+
+/*!
+ \class QAbstractItemModel
+
+ \brief The QAbstractItemModel class provides the abstract interface for
+ item model classes.
+
+ \ingroup model-view
+
+
+ The QAbstractItemModel class defines the standard interface that item
+ models must use to be able to interoperate with other components in the
+ model/view architecture. It is not supposed to be instantiated directly.
+ Instead, you should subclass it to create new models.
+
+ The QAbstractItemModel class is one of the \l{Model/View Classes}
+ and is part of Qt's \l{Model/View Programming}{model/view framework}.
+
+ If you need a model to use with a QListView or a QTableView, you should
+ consider subclassing QAbstractListModel or QAbstractTableModel instead of
+ this class.
+
+ The underlying data model is exposed to views and delegates as a hierarchy
+ of tables. If you do not make use of the hierarchy, then the model is a
+ simple table of rows and columns. Each item has a unique index specified by
+ a QModelIndex.
+
+ \image modelindex-no-parent.png
+
+ Every item of data that can be accessed via a model has an associated model
+ index. You can obtain this model index using the index() function. Each
+ index may have a sibling() index; child items have a parent() index.
+
+ Each item has a number of data elements associated with it and they can be
+ retrieved by specifying a role (see \l Qt::ItemDataRole) to the model's
+ data() function. Data for all available roles can be obtained at the same
+ time using the itemData() function.
+
+ Data for each role is set using a particular \l Qt::ItemDataRole. Data for
+ individual roles are set individually with setData(), or they can be set
+ for all roles with setItemData().
+
+ Items can be queried with flags() (see \l Qt::ItemFlag) to see if they can
+ be selected, dragged, or manipulated in other ways.
+
+ If an item has child objects, hasChildren() returns true for the
+ corresponding index.
+
+ The model has a rowCount() and a columnCount() for each level of the
+ hierarchy. Rows and columns can be inserted and removed with insertRows(),
+ insertColumns(), removeRows(), and removeColumns().
+
+ The model emits signals to indicate changes. For example, dataChanged() is
+ emitted whenever items of data made available by the model are changed.
+ Changes to the headers supplied by the model cause headerDataChanged() to
+ be emitted. If the structure of the underlying data changes, the model can
+ emit layoutChanged() to indicate to any attached views that they should
+ redisplay any items shown, taking the new structure into account.
+
+ The items available through the model can be searched for particular data
+ using the match() function.
+
+ To sort the model, you can use sort().
+
+
+ \section1 Subclassing
+
+ \note Some general guidelines for subclassing models are available in the
+ \l{Model Subclassing Reference}.
+
+ When subclassing QAbstractItemModel, at the very least you must implement
+ index(), parent(), rowCount(), columnCount(), and data(). These functions
+ are used in all read-only models, and form the basis of editable models.
+
+ You can also reimplement hasChildren() to provide special behavior for
+ models where the implementation of rowCount() is expensive. This makes it
+ possible for models to restrict the amount of data requested by views, and
+ can be used as a way to implement lazy population of model data.
+
+ To enable editing in your model, you must also implement setData(), and
+ reimplement flags() to ensure that \c ItemIsEditable is returned. You can
+ also reimplement headerData() and setHeaderData() to control the way the
+ headers for your model are presented.
+
+ The dataChanged() and headerDataChanged() signals must be emitted
+ explicitly when reimplementing the setData() and setHeaderData() functions,
+ respectively.
+
+ Custom models need to create model indexes for other components to use. To
+ do this, call createIndex() with suitable row and column numbers for the
+ item, and an identifier for it, either as a pointer or as an integer value.
+ The combination of these values must be unique for each item. Custom models
+ typically use these unique identifiers in other reimplemented functions to
+ retrieve item data and access information about the item's parents and
+ children. See the \l{Simple Tree Model Example} for more information about
+ unique identifiers.
+
+ It is not necessary to support every role defined in Qt::ItemDataRole.
+ Depending on the type of data contained within a model, it may only be
+ useful to implement the data() function to return valid information for
+ some of the more common roles. Most models provide at least a textual
+ representation of item data for the Qt::DisplayRole, and well-behaved
+ models should also provide valid information for the Qt::ToolTipRole and
+ Qt::WhatsThisRole. Supporting these roles enables models to be used with
+ standard Qt views. However, for some models that handle highly-specialized
+ data, it may be appropriate to provide data only for user-defined roles.
+
+ Models that provide interfaces to resizable data structures can provide
+ implementations of insertRows(), removeRows(), insertColumns(),and
+ removeColumns(). When implementing these functions, it is important to
+ notify any connected views about changes to the model's dimensions both
+ \e before and \e after they occur:
+
+ \list
+ \o An insertRows() implementation must call beginInsertRows() \e before
+ inserting new rows into the data structure, and endInsertRows()
+ \e{immediately afterwards}.
+ \o An insertColumns() implementation must call beginInsertColumns()
+ \e before inserting new columns into the data structure, and
+ endInsertColumns() \e{immediately afterwards}.
+ \o A removeRows() implementation must call beginRemoveRows() \e before
+ the rows are removed from the data structure, and endRemoveRows()
+ \e{immediately afterwards}.
+ \o A removeColumns() implementation must call beginRemoveColumns()
+ \e before the columns are removed from the data structure, and
+ endRemoveColumns() \e{immediately afterwards}.
+ \endlist
+
+ The \e private signals that these functions emit give attached components
+ the chance to take action before any data becomes unavailable. The
+ encapsulation of the insert and remove operations with these begin and end
+ functions also enables the model to manage \l{QPersistentModelIndex}
+ {persistent model indexes} correctly. \bold{If you want selections to be
+ handled properly, you must ensure that you call these functions.} If you
+ insert or remove an item with children, you do not need to call these
+ functions for the child items. In other words, the parent item will take
+ care of its child items.
+
+ To create models that populate incrementally, you can reimplement
+ fetchMore() and canFetchMore(). If the reimplementation of fetchMore() adds
+ rows to the model, \l{QAbstractItemModel::}{beginInsertRows()} and
+ \l{QAbstractItemModel::}{endInsertRows()} must be called.
+
+ \sa {Model Classes}, {Model Subclassing Reference}, QModelIndex,
+ QAbstractItemView, {Using drag and drop with item views},
+ {Simple DOM Model Example}, {Simple Tree Model Example},
+ {Editable Tree Model Example}, {Fetch More Example}
+*/
+
+/*!
+ \fn QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const = 0
+
+ Returns the index of the item in the model specified by the given \a row,
+ \a column and \a parent index.
+
+ When reimplementing this function in a subclass, call createIndex() to
+ generate model indexes that other components can use to refer to items in
+ your model.
+
+ \sa createIndex()
+*/
+
+/*!
+ \fn bool QAbstractItemModel::insertColumn(int column, const QModelIndex &parent)
+
+ Inserts a single column before the given \a column in the child items of
+ the \a parent specified.
+
+ Returns true if the column is inserted; otherwise returns false.
+
+ \sa insertColumns() insertRow() removeColumn()
+*/
+
+/*!
+ \fn bool QAbstractItemModel::insertRow(int row, const QModelIndex &parent)
+
+ \note The base class implementation of this function does nothing and
+ returns false.
+
+ Inserts a single row before the given \a row in the child items of the
+ \a parent specified.
+
+ Returns true if the row is inserted; otherwise returns false.
+
+ \sa insertRows() insertColumn() removeRow()
+*/
+
+/*!
+ \fn QObject *QAbstractItemModel::parent() const
+ \internal
+*/
+
+/*!
+ \fn QModelIndex QAbstractItemModel::parent(const QModelIndex &index) const = 0
+
+ Returns the parent of the model item with the given \a index. If the item
+ has no parent, an invalid QModelIndex is returned.
+
+ A common convention used in models that expose tree data structures is that
+ only items in the first column have children. For that case, when
+ reimplementing this function in a subclass the column of the returned
+ QModelIndex would be 0.
+
+ When reimplementing this function in a subclass, be careful to avoid
+ calling QModelIndex member functions, such as QModelIndex::parent(), since
+ indexes belonging to your model will simply call your implementation,
+ leading to infinite recursion.
+
+ \sa createIndex()
+*/
+
+/*!
+ \fn bool QAbstractItemModel::removeColumn(int column, const QModelIndex &parent)
+
+ Removes the given \a column from the child items of the \a parent
+ specified.
+
+ Returns true if the column is removed; otherwise returns false.
+
+ \sa removeColumns(), removeRow(), insertColumn()
+*/
+
+/*!
+ \fn bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent)
+
+ Removes the given \a row from the child items of the \a parent specified.
+
+ Returns true if the row is removed; otherwise returns false.
+
+ This is a convenience function that calls removeRows(). The
+ QAbstractItemModel implementation of removeRows() does nothing.
+
+ \sa removeRows(), removeColumn(), insertRow()
+*/
+
+/*!
+ \fn void QAbstractItemModel::headerDataChanged(Qt::Orientation orientation, int first, int last)
+
+ This signal is emitted whenever a header is changed. The \a orientation
+ indicates whether the horizontal or vertical header has changed. The
+ sections in the header from the \a first to the \a last need to be updated.
+
+ When reimplementing the setHeaderData() function, this signal must be
+ emitted explicitly.
+
+ If you are changing the number of columns or rows you do not need to emit
+ this signal, but use the begin/end functions (refer to the section on
+ subclassing in the QAbstractItemModel class description for details).
+
+ \sa headerData(), setHeaderData(), dataChanged()
+*/
+
+/*!
+ \fn void QAbstractItemModel::layoutAboutToBeChanged()
+ \since 4.2
+
+ This signal is emitted just before the layout of a model is changed.
+ Components connected to this signal use it to adapt to changes in the
+ model's layout.
+
+ Subclasses should update any persistent model indexes after emitting
+ layoutAboutToBeChanged().
+
+ \sa layoutChanged(), changePersistentIndex()
+*/
+
+/*!
+ \fn void QAbstractItemModel::layoutChanged()
+
+ This signal is emitted whenever the layout of items exposed by the model
+ has changed; for example, when the model has been sorted. When this signal
+ is received by a view, it should update the layout of items to reflect this
+ change.
+
+ When subclassing QAbstractItemModel or QAbstractProxyModel, ensure that you
+ emit layoutAboutToBeChanged() before changing the order of items or
+ altering the structure of the data you expose to views, and emit
+ layoutChanged() after changing the layout.
+
+ Subclasses should update any persistent model indexes before emitting
+ layoutChanged(). In other words, when the structure changes:
+
+ \list
+ \o emit layoutAboutToBeChanged
+ \o Remember the QModelIndex that will change
+ \o Update your internal data
+ \o Call changePersistentIndex()
+ \o emit layoutChanged
+ \endlist
+
+ \sa layoutAboutToBeChanged(), dataChanged(), headerDataChanged(), modelReset(),
+ changePersistentIndex()
+*/
+
+/*!
+ Constructs an abstract item model with the given \a parent.
+*/
+QAbstractItemModel::QAbstractItemModel(QObject *parent)
+ : QObject(*new QAbstractItemModelPrivate, parent)
+{
+}
+
+/*!
+ \internal
+*/
+QAbstractItemModel::QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent)
+ : QObject(dd, parent)
+{
+}
+
+/*!
+ Destroys the abstract item model.
+*/
+QAbstractItemModel::~QAbstractItemModel()
+{
+ d_func()->invalidatePersistentIndexes();
+}
+
+/*!
+ \fn QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const
+
+ Returns the sibling at \a row and \a column for the item at \a index, or an
+ invalid QModelIndex if there is no sibling at that location.
+
+ sibling() is just a convenience function that finds the item's parent, and
+ uses it to retrieve the index of the child item in the specified \a row and
+ \a column.
+
+ \sa index(), QModelIndex::row(), QModelIndex::column()
+*/
+
+
+/*!
+ \fn int QAbstractItemModel::rowCount(const QModelIndex &parent) const
+
+ Returns the number of rows under the given \a parent. When the parent is
+ valid it means that rowCount is returning the number of children of parent.
+
+ \note When implementing a table based model, rowCount() should return 0
+ when the parent is valid.
+
+ \sa columnCount()
+*/
+
+/*!
+ \fn int QAbstractItemModel::columnCount(const QModelIndex &parent) const
+
+ Returns the number of columns for the children of the given \a parent.
+
+ In most subclasses, the number of columns is independent of the \a parent.
+
+ For example:
+
+ \snippet examples/itemviews/simpledommodel/dommodel.cpp 2
+
+ \note When implementing a table based model, columnCount() should return 0
+ when the parent is valid.
+
+ \sa rowCount()
+*/
+
+/*!
+ \fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
+
+ This signal is emitted whenever the data in an existing item changes.
+
+ If the items are of the same parent, the affected ones are those between
+ \a topLeft and \a bottomRight inclusive. If the items do not have the same
+ parent, the behavior is undefined.
+
+ When reimplementing the setData() function, this signal must be emitted
+ explicitly.
+
+ \sa headerDataChanged(), setData(), layoutChanged()
+*/
+
+/*!
+ \fn void QAbstractItemModel::rowsInserted(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted after rows have been inserted into the
+ model. The new items are those between \a start and \a end
+ inclusive, under the given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes in the
+ model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa insertRows(), beginInsertRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted just before rows are inserted into the model. The
+ new items will be positioned between \a start and \a end inclusive, under
+ the given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa insertRows(), beginInsertRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::rowsRemoved(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted after rows have been removed from the model. The
+ removed items are those between \a start and \a end inclusive, under the
+ given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa removeRows(), beginRemoveRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted just before rows are removed from the model. The
+ items that will be removed are those between \a start and \a end inclusive,
+ under the given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa removeRows(), beginRemoveRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
+ \since 4.6
+
+ This signal is emitted after rows have been moved within the
+ model. The items between \a sourceStart and \a sourceEnd
+ inclusive, under the given \a sourceParent item have been moved to \a destinationParent
+ starting at the row \a destinationRow.
+
+ \bold{Note:} Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa beginMoveRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
+ \since 4.6
+
+ This signal is emitted just before rows are moved within the
+ model. The items that will be moved are those between \a sourceStart and \a sourceEnd
+ inclusive, under the given \a sourceParent item. They will be moved to \a destinationParent
+ starting at the row \a destinationRow.
+
+ \bold{Note:} Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa beginMoveRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
+ \since 4.6
+
+ This signal is emitted after columns have been moved within the
+ model. The items between \a sourceStart and \a sourceEnd
+ inclusive, under the given \a sourceParent item have been moved to \a destinationParent
+ starting at the column \a destinationColumn.
+
+ \bold{Note:} Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa beginMoveRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
+ \since 4.6
+
+ This signal is emitted just before columns are moved within the
+ model. The items that will be moved are those between \a sourceStart and \a sourceEnd
+ inclusive, under the given \a sourceParent item. They will be moved to \a destinationParent
+ starting at the column \a destinationColumn.
+
+ \bold{Note:} Components connected to this signal use it to adapt to changes
+ in the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa beginMoveRows()
+*/
+
+/*!
+ \fn void QAbstractItemModel::columnsInserted(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted after columns have been inserted into the model. The
+ new items are those between \a start and \a end inclusive, under the given
+ \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes in the
+ model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa insertColumns(), beginInsertColumns()
+*/
+
+/*!
+ \fn void QAbstractItemModel::columnsAboutToBeInserted(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted just before columns are inserted into the model. The
+ new items will be positioned between \a start and \a end inclusive, under
+ the given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes in the
+ model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa insertColumns(), beginInsertColumns()
+*/
+
+/*!
+ \fn void QAbstractItemModel::columnsRemoved(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted after columns have been removed from the model.
+ The removed items are those between \a start and \a end inclusive,
+ under the given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes in
+ the model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa removeColumns(), beginRemoveColumns()
+*/
+
+/*!
+ \fn void QAbstractItemModel::columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
+
+ This signal is emitted just before columns are removed from the model. The
+ items to be removed are those between \a start and \a end inclusive, under
+ the given \a parent item.
+
+ \note Components connected to this signal use it to adapt to changes in the
+ model's dimensions. It can only be emitted by the QAbstractItemModel
+ implementation, and cannot be explicitly emitted in subclass code.
+
+ \sa removeColumns(), beginRemoveColumns()
+*/
+
+/*!
+ Returns true if the model returns a valid QModelIndex for \a row and
+ \a column with \a parent, otherwise returns false.
+*/
+bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const
+{
+ if (row < 0 || column < 0)
+ return false;
+ return row < rowCount(parent) && column < columnCount(parent);
+}
+
+
+/*!
+ Returns true if \a parent has any children; otherwise returns false.
+
+ Use rowCount() on the parent to find out the number of children.
+
+ \sa parent() index()
+*/
+bool QAbstractItemModel::hasChildren(const QModelIndex &parent) const
+{
+ return (rowCount(parent) > 0) && (columnCount(parent) > 0);
+}
+
+
+/*!
+ Returns a map with values for all predefined roles in the model for the
+ item at the given \a index.
+
+ Reimplement this function if you want to extend the default behavior of
+ this function to include custom roles in the map.
+
+ \sa Qt::ItemDataRole, data()
+*/
+QMap<int, QVariant> QAbstractItemModel::itemData(const QModelIndex &index) const
+{
+ QMap<int, QVariant> roles;
+ for (int i = 0; i < Qt::UserRole; ++i) {
+ QVariant variantData = data(index, i);
+ if (variantData.isValid())
+ roles.insert(i, variantData);
+ }
+ return roles;
+}
+
+/*!
+ Sets the \a role data for the item at \a index to \a value.
+
+ Returns true if successful; otherwise returns false.
+
+ The dataChanged() signal should be emitted if the data was successfully
+ set.
+
+ The base class implementation returns false. This function and data() must
+ be reimplemented for editable models.
+
+ \sa Qt::ItemDataRole, data(), itemData()
+*/
+bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ Q_UNUSED(index);
+ Q_UNUSED(value);
+ Q_UNUSED(role);
+ return false;
+}
+
+/*!
+ \fn QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const = 0
+
+ Returns the data stored under the given \a role for the item referred to
+ by the \a index.
+
+ \note If you do not have a value to return, return an \bold invalid
+ QVariant instead of returning 0.
+
+ \sa Qt::ItemDataRole, setData(), headerData()
+*/
+
+/*!
+ Sets the role data for the item at \a index to the associated value in
+ \a roles, for every Qt::ItemDataRole.
+
+ Returns true if successful; otherwise returns false.
+
+ Roles that are not in \a roles will not be modified.
+
+ \sa setData() data() itemData()
+*/
+bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
+{
+ bool b = true;
+ for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
+ b = b && setData(index, it.value(), it.key());
+ return b;
+}
+
+/*!
+ Returns a list of MIME types that can be used to describe a list of model
+ indexes.
+
+ \sa mimeData()
+*/
+QStringList QAbstractItemModel::mimeTypes() const
+{
+ QStringList types;
+ types << QLatin1String("application/x-qabstractitemmodeldatalist");
+ return types;
+}
+
+/*!
+ Returns an object that contains serialized items of data corresponding to
+ the list of \a indexes specified. The formats used to describe the encoded
+ data is obtained from the mimeTypes() function.
+
+ If the list of indexes is empty, or there are no supported MIME types, 0 is
+ returned rather than a serialized empty list.
+
+ \sa mimeTypes(), dropMimeData()
+*/
+QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
+{
+ if (indexes.count() <= 0)
+ return 0;
+ QStringList types = mimeTypes();
+ if (types.isEmpty())
+ return 0;
+ QMimeData *data = new QMimeData();
+ QString format = types.at(0);
+ QByteArray encoded;
+ QDataStream stream(&encoded, QIODevice::WriteOnly);
+ encodeData(indexes, stream);
+ data->setData(format, encoded);
+ return data;
+}
+
+/*!
+ Handles the \a data supplied by a drag and drop operation that ended with
+ the given \a action.
+
+ Returns true if the data and action can be handled by the model; otherwise
+ returns false.
+
+ Although the specified \a row, \a column and \a parent indicate the
+ location of an item in the model where the operation ended, it is the
+ responsibility of the view to provide a suitable location for where the
+ data should be inserted.
+
+ For instance, a drop action on an item in a QTreeView can result in new
+ items either being inserted as children of the item specified by \a row,
+ \a column, and \a parent, or as siblings of the item.
+
+ When row and column are -1 it means that it is up to the model to decide
+ where to place the data. This can occur in a tree when data is dropped on
+ a parent. Models will usually append the data to the parent in this case.
+
+ \sa supportedDropActions(), {Using drag and drop with item views}
+*/
+bool QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent)
+{
+ // check if the action is supported
+ if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
+ return false;
+ // check if the format is supported
+ QStringList types = mimeTypes();
+ if (types.isEmpty())
+ return false;
+ QString format = types.at(0);
+ if (!data->hasFormat(format))
+ return false;
+ if (row > rowCount(parent))
+ row = rowCount(parent);
+ if (row == -1)
+ row = rowCount(parent);
+ if (column == -1)
+ column = 0;
+ // decode and insert
+ QByteArray encoded = data->data(format);
+ QDataStream stream(&encoded, QIODevice::ReadOnly);
+ return decodeData(row, column, parent, stream);
+}
+
+/*!
+ \since 4.2
+
+ Returns the drop actions supported by this model.
+
+ The default implementation returns Qt::CopyAction. Reimplement this
+ function if you wish to support additional actions. You must also
+ reimplement the dropMimeData() function to handle the additional
+ operations.
+
+ \sa dropMimeData(), Qt::DropActions, {Using drag and drop with item
+ views}
+*/
+Qt::DropActions QAbstractItemModel::supportedDropActions() const
+{
+ return Qt::CopyAction;
+}
+
+/*!
+ Returns the actions supported by the data in this model.
+
+ The default implementation returns supportedDropActions() unless specific
+ values have been set with setSupportedDragActions().
+
+ supportedDragActions() is used by QAbstractItemView::startDrag() as the
+ default values when a drag occurs.
+
+ \sa Qt::DropActions, {Using drag and drop with item views}
+*/
+Qt::DropActions QAbstractItemModel::supportedDragActions() const
+{
+ // ### Qt 5: make this virtual or these properties
+ Q_D(const QAbstractItemModel);
+ if (d->supportedDragActions != -1)
+ return d->supportedDragActions;
+ return supportedDropActions();
+}
+
+/*!
+ \since 4.2
+
+ Sets the supported drag \a actions for the items in the model.
+
+ \sa supportedDragActions(), {Using drag and drop with item views}
+*/
+void QAbstractItemModel::setSupportedDragActions(Qt::DropActions actions)
+{
+ Q_D(QAbstractItemModel);
+ d->supportedDragActions = actions;
+}
+
+/*!
+ \note The base class implementation of this function does nothing and
+ returns false.
+
+ On models that support this, inserts \a count rows into the model before
+ the given \a row. Items in the new row will be children of the item
+ represented by the \a parent model index.
+
+ If \a row is 0, the rows are prepended to any existing rows in the parent.
+
+ If \a row is rowCount(), the rows are appended to any existing rows in the
+ parent.
+
+ If \a parent has no children, a single column with \a count rows is
+ inserted.
+
+ Returns true if the rows were successfully inserted; otherwise returns
+ false.
+
+ If you implement your own model, you can reimplement this function if you
+ want to support insertions. Alternatively, you can provide your own API for
+ altering the data. In either case, you will need to call
+ beginInsertRows() and endInsertRows() to notify other components that the
+ model has changed.
+
+ \sa insertColumns(), removeRows(), beginInsertRows(), endInsertRows()
+*/
+bool QAbstractItemModel::insertRows(int, int, const QModelIndex &)
+{
+ return false;
+}
+
+/*!
+ On models that support this, inserts \a count new columns into the model
+ before the given \a column. The items in each new column will be children
+ of the item represented by the \a parent model index.
+
+ If \a column is 0, the columns are prepended to any existing columns.
+
+ If \a column is columnCount(), the columns are appended to any existing
+ columns.
+
+ If \a parent has no children, a single row with \a count columns is
+ inserted.
+
+ Returns true if the columns were successfully inserted; otherwise returns
+ false.
+
+ The base class implementation does nothing and returns false.
+
+ If you implement your own model, you can reimplement this function if you
+ want to support insertions. Alternatively, you can provide your own API for
+ altering the data.
+
+ \sa insertRows(), removeColumns(), beginInsertColumns(), endInsertColumns()
+*/
+bool QAbstractItemModel::insertColumns(int, int, const QModelIndex &)
+{
+ return false;
+}
+
+/*!
+ On models that support this, removes \a count rows starting with the given
+ \a row under parent \a parent from the model.
+
+ Returns true if the rows were successfully removed; otherwise returns
+ false.
+
+ The base class implementation does nothing and returns false.
+
+ If you implement your own model, you can reimplement this function if you
+ want to support removing. Alternatively, you can provide your own API for
+ altering the data.
+
+ \sa removeRow(), removeColumns(), insertColumns(), beginRemoveRows(),
+ endRemoveRows()
+*/
+bool QAbstractItemModel::removeRows(int, int, const QModelIndex &)
+{
+ return false;
+}
+
+/*!
+ On models that support this, removes \a count columns starting with the
+ given \a column under parent \a parent from the model.
+
+ Returns true if the columns were successfully removed; otherwise returns
+ false.
+
+ The base class implementation does nothing and returns false.
+
+ If you implement your own model, you can reimplement this function if you
+ want to support removing. Alternatively, you can provide your own API for
+ altering the data.
+
+ \sa removeColumn(), removeRows(), insertColumns(), beginRemoveColumns(),
+ endRemoveColumns()
+*/
+bool QAbstractItemModel::removeColumns(int, int, const QModelIndex &)
+{
+ return false;
+}
+
+/*!
+ Fetches any available data for the items with the parent specified by the
+ \a parent index.
+
+ Reimplement this if you are populating your model incrementally.
+
+ The default implementation does nothing.
+
+ \sa canFetchMore()
+*/
+void QAbstractItemModel::fetchMore(const QModelIndex &)
+{
+ // do nothing
+}
+
+/*!
+ Returns true if there is more data available for \a parent; otherwise
+ returns false.
+
+ The default implementation always returns false.
+
+ If canFetchMore() returns true, QAbstractItemView will call fetchMore().
+ However, the fetchMore() function is only called when the model is being
+ populated incrementally.
+
+ \sa fetchMore()
+*/
+bool QAbstractItemModel::canFetchMore(const QModelIndex &) const
+{
+ return false;
+}
+
+/*!
+ Returns the item flags for the given \a index.
+
+ The base class implementation returns a combination of flags that enables
+ the item (\c ItemIsEnabled) and allows it to be selected
+ (\c ItemIsSelectable).
+
+ \sa Qt::ItemFlags
+*/
+Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
+{
+ Q_D(const QAbstractItemModel);
+ if (!d->indexValid(index))
+ return 0;
+
+ return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
+}
+
+/*!
+ Sorts the model by \a column in the given \a order.
+
+ The base class implementation does nothing.
+*/
+void QAbstractItemModel::sort(int column, Qt::SortOrder order)
+{
+ Q_UNUSED(column);
+ Q_UNUSED(order);
+ // do nothing
+}
+
+/*!
+ Returns a model index for the buddy of the item represented by \a index.
+ When the user wants to edit an item, the view will call this function to
+ check whether another item in the model should be edited instead. Then, the
+ view will construct a delegate using the model index returned by the buddy
+ item.
+
+ The default implementation of this function has each item as its own buddy.
+*/
+QModelIndex QAbstractItemModel::buddy(const QModelIndex &index) const
+{
+ return index;
+}
+
+/*!
+ Returns a list of indexes for the items in the column of the \a start index
+ where data stored under the given \a role matches the specified \a value.
+ The way the search is performed is defined by the \a flags given. The list
+ that is returned may be empty.
+
+ The search begins from the \a start index, and continues until the number
+ of matching data items equals \a hits, the search reaches the last row, or
+ the search reaches \a start again - depending on whether \c MatchWrap is
+ specified in \a flags. If you want to search for all matching items, use
+ \a hits = -1.
+
+ By default, this function will perform a wrapping, string-based comparison
+ on all items, searching for items that begin with the search term specified
+ by \a value.
+
+ \note The default implementation of this function only searches columns.
+ Reimplement this function to include a different search behavior.
+*/
+QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
+ const QVariant &value, int hits,
+ Qt::MatchFlags flags) const
+{
+ QModelIndexList result;
+ uint matchType = flags & 0x0F;
+ Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
+ bool recurse = flags & Qt::MatchRecursive;
+ bool wrap = flags & Qt::MatchWrap;
+ bool allHits = (hits == -1);
+ QString text; // only convert to a string if it is needed
+ QModelIndex p = parent(start);
+ int from = start.row();
+ int to = rowCount(p);
+
+ // iterates twice if wrapping
+ for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
+ for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) {
+ QModelIndex idx = index(r, start.column(), p);
+ if (!idx.isValid())
+ continue;
+ QVariant v = data(idx, role);
+ // QVariant based matching
+ if (matchType == Qt::MatchExactly) {
+ if (value == v)
+ result.append(idx);
+ } else { // QString based matching
+ if (text.isEmpty()) // lazy conversion
+ text = value.toString();
+ QString t = v.toString();
+ switch (matchType) {
+ case Qt::MatchRegExp:
+ if (QRegExp(text, cs).exactMatch(t))
+ result.append(idx);
+ break;
+ case Qt::MatchWildcard:
+ if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t))
+ result.append(idx);
+ break;
+ case Qt::MatchStartsWith:
+ if (t.startsWith(text, cs))
+ result.append(idx);
+ break;
+ case Qt::MatchEndsWith:
+ if (t.endsWith(text, cs))
+ result.append(idx);
+ break;
+ case Qt::MatchFixedString:
+ if (t.compare(text, cs) == 0)
+ result.append(idx);
+ break;
+ case Qt::MatchContains:
+ default:
+ if (t.contains(text, cs))
+ result.append(idx);
+ }
+ }
+ if (recurse && hasChildren(idx)) { // search the hierarchy
+ result += match(index(0, idx.column(), idx), role,
+ (text.isEmpty() ? value : text),
+ (allHits ? -1 : hits - result.count()), flags);
+ }
+ }
+ // prepare for the next iteration
+ from = 0;
+ to = start.row();
+ }
+ return result;
+}
+
+/*!
+ Returns the row and column span of the item represented by \a index.
+
+ \note Currently, span is not used.
+*/
+
+QSize QAbstractItemModel::span(const QModelIndex &) const
+{
+ return QSize(1, 1);
+}
+
+/*!
+ \since 4.6
+
+ Sets the model's role names to \a roleNames.
+
+ This function allows mapping of role identifiers to role property names in
+ Declarative UI. This function must be called before the model is used.
+ Modifying the role names after the model has been set may result in
+ undefined behaviour.
+
+ \sa roleNames()
+*/
+void QAbstractItemModel::setRoleNames(const QHash<int,QByteArray> &roleNames)
+{
+ Q_D(QAbstractItemModel);
+ d->roleNames = roleNames;
+}
+
+/*!
+ \since 4.6
+
+ Returns the model's role names.
+
+ \sa setRoleNames()
+*/
+const QHash<int,QByteArray> &QAbstractItemModel::roleNames() const
+{
+ Q_D(const QAbstractItemModel);
+ return d->roleNames;
+}
+
+/*!
+ Lets the model know that it should submit cached information to permanent
+ storage. This function is typically used for row editing.
+
+ Returns true if there is no error; otherwise returns false.
+
+ \sa revert()
+*/
+
+bool QAbstractItemModel::submit()
+{
+ return true;
+}
+
+/*!
+ Lets the model know that it should discard cached information. This
+ function is typically used for row editing.
+
+ \sa submit()
+*/
+
+void QAbstractItemModel::revert()
+{
+ // do nothing
+}
+
+/*!
+ Returns the data for the given \a role and \a section in the header with
+ the specified \a orientation.
+
+ For horizontal headers, the section number corresponds to the column
+ number. Similarly, for vertical headers, the section number corresponds to
+ the row number.
+
+ \sa Qt::ItemDataRole, setHeaderData(), QHeaderView
+*/
+
+QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ Q_UNUSED(orientation);
+ if (role == Qt::DisplayRole)
+ return section + 1;
+ return QVariant();
+}
+
+/*!
+ Sets the data for the given \a role and \a section in the header with the
+ specified \a orientation to the \a value supplied.
+
+ Returns true if the header's data was updated; otherwise returns false.
+
+ When reimplementing this function, the headerDataChanged() signal must be
+ emitted explicitly.
+
+ \sa Qt::ItemDataRole, headerData()
+*/
+
+bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation,
+ const QVariant &value, int role)
+{
+ Q_UNUSED(section);
+ Q_UNUSED(orientation);
+ Q_UNUSED(value);
+ Q_UNUSED(role);
+ return false;
+}
+
+/*!
+ \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, void *ptr) const
+
+ Creates a model index for the given \a row and \a column with the internal
+ pointer \a ptr.
+
+ When using a QSortFilterProxyModel, its indexes have their own internal
+ pointer. It is not advisable to access this internal pointer outside of the
+ model. Use the data() function instead.
+
+ This function provides a consistent interface that model subclasses must
+ use to create model indexes.
+*/
+
+/*!
+ \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, int id) const
+ \obsolete
+
+ Use QModelIndex
+ QAbstractItemModel::createIndex(int row, int column, quint32 id) instead.
+*/
+
+/*!
+ \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, quint32 id) const
+
+ Creates a model index for the given \a row and \a column with the internal
+ identifier, \a id.
+
+ This function provides a consistent interface that model subclasses must
+ use to create model indexes.
+
+ \sa QModelIndex::internalId()
+*/
+
+/*!
+ \internal
+*/
+void QAbstractItemModel::encodeData(const QModelIndexList &indexes, QDataStream &stream) const
+{
+ QModelIndexList::ConstIterator it = indexes.begin();
+ for (; it != indexes.end(); ++it)
+ stream << (*it).row() << (*it).column() << itemData(*it);
+}
+
+/*!
+ \internal
+ */
+bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &parent,
+ QDataStream &stream)
+{
+ int top = INT_MAX;
+ int left = INT_MAX;
+ int bottom = 0;
+ int right = 0;
+ QVector<int> rows, columns;
+ QVector<QMap<int, QVariant> > data;
+
+ while (!stream.atEnd()) {
+ int r, c;
+ QMap<int, QVariant> v;
+ stream >> r >> c >> v;
+ rows.append(r);
+ columns.append(c);
+ data.append(v);
+ top = qMin(r, top);
+ left = qMin(c, left);
+ bottom = qMax(r, bottom);
+ right = qMax(c, right);
+ }
+
+ // insert the dragged items into the table, use a bit array to avoid overwriting items,
+ // since items from different tables can have the same row and column
+ int dragRowCount = 0;
+ int dragColumnCount = right - left + 1;
+
+ // Compute the number of continuous rows upon insertion and modify the rows to match
+ QVector<int> rowsToInsert(bottom + 1);
+ for (int i = 0; i < rows.count(); ++i)
+ rowsToInsert[rows.at(i)] = 1;
+ for (int i = 0; i < rowsToInsert.count(); ++i) {
+ if (rowsToInsert[i] == 1){
+ rowsToInsert[i] = dragRowCount;
+ ++dragRowCount;
+ }
+ }
+ for (int i = 0; i < rows.count(); ++i)
+ rows[i] = top + rowsToInsert[rows[i]];
+
+ QBitArray isWrittenTo(dragRowCount * dragColumnCount);
+
+ // make space in the table for the dropped data
+ int colCount = columnCount(parent);
+ if (colCount == 0) {
+ insertColumns(colCount, dragColumnCount - colCount, parent);
+ colCount = columnCount(parent);
+ }
+ insertRows(row, dragRowCount, parent);
+
+ row = qMax(0, row);
+ column = qMax(0, column);
+
+ QVector<QPersistentModelIndex> newIndexes(data.size());
+ // set the data in the table
+ for (int j = 0; j < data.size(); ++j) {
+ int relativeRow = rows.at(j) - top;
+ int relativeColumn = columns.at(j) - left;
+ int destinationRow = relativeRow + row;
+ int destinationColumn = relativeColumn + column;
+ int flat = (relativeRow * dragColumnCount) + relativeColumn;
+ // if the item was already written to, or we just can't fit it in the table, create a new row
+ if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
+ destinationColumn = qBound(column, destinationColumn, colCount - 1);
+ destinationRow = row + dragRowCount;
+ insertRows(row + dragRowCount, 1, parent);
+ flat = (dragRowCount * dragColumnCount) + relativeColumn;
+ isWrittenTo.resize(++dragRowCount * dragColumnCount);
+ }
+ if (!isWrittenTo.testBit(flat)) {
+ newIndexes[j] = index(destinationRow, destinationColumn, parent);
+ isWrittenTo.setBit(flat);
+ }
+ }
+
+ for(int k = 0; k < newIndexes.size(); k++) {
+ if (newIndexes.at(k).isValid())
+ setItemData(newIndexes.at(k), data.at(k));
+ }
+
+ return true;
+}
+
+/*!
+ Begins a row insertion operation.
+
+ When reimplementing insertRows() in a subclass, you must call this function
+ \e before inserting data into the model's underlying data store.
+
+ The \a parent index corresponds to the parent into which the new rows are
+ inserted; \a first and \a last are the row numbers that the new rows will
+ have after they have been inserted.
+
+ \table 80%
+ \row
+ \o \inlineimage modelview-begin-insert-rows.png Inserting rows
+ \o Specify the first and last row numbers for the span of rows you
+ want to insert into an item in a model.
+
+ For example, as shown in the diagram, we insert three rows before
+ row 2, so \a first is 2 and \a last is 4:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 0
+
+ This inserts the three new rows as rows 2, 3, and 4.
+ \row
+ \o \inlineimage modelview-begin-append-rows.png Appending rows
+ \o To append rows, insert them after the last row.
+
+ For example, as shown in the diagram, we append two rows to a
+ collection of 4 existing rows (ending in row 3), so \a first is 4
+ and \a last is 5:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 1
+
+ This appends the two new rows as rows 4 and 5.
+ \endtable
+
+ \note This function emits the rowsAboutToBeInserted() signal which
+ connected views (or proxies) must handle before the data is inserted.
+ Otherwise, the views may end up in an invalid state.
+ \sa endInsertRows()
+*/
+void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
+{
+ Q_ASSERT(first >= 0);
+ Q_ASSERT(last >= first);
+ Q_D(QAbstractItemModel);
+ d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
+ emit rowsAboutToBeInserted(parent, first, last);
+ d->rowsAboutToBeInserted(parent, first, last);
+}
+
+/*!
+ Ends a row insertion operation.
+
+ When reimplementing insertRows() in a subclass, you must call this function
+ \e after inserting data into the model's underlying data store.
+
+ \sa beginInsertRows()
+*/
+void QAbstractItemModel::endInsertRows()
+{
+ Q_D(QAbstractItemModel);
+ QAbstractItemModelPrivate::Change change = d->changes.pop();
+ d->rowsInserted(change.parent, change.first, change.last);
+ emit rowsInserted(change.parent, change.first, change.last);
+}
+
+/*!
+ Begins a row removal operation.
+
+ When reimplementing removeRows() in a subclass, you must call this
+ function \e before removing data from the model's underlying data store.
+
+ The \a parent index corresponds to the parent from which the new rows are
+ removed; \a first and \a last are the row numbers of the rows to be
+ removed.
+
+ \table 80%
+ \row
+ \o \inlineimage modelview-begin-remove-rows.png Removing rows
+ \o Specify the first and last row numbers for the span of rows you
+ want to remove from an item in a model.
+
+ For example, as shown in the diagram, we remove the two rows from
+ row 2 to row 3, so \a first is 2 and \a last is 3:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 2
+ \endtable
+
+ \note This function emits the rowsAboutToBeRemoved() signal which connected
+ views (or proxies) must handle before the data is removed. Otherwise, the
+ views may end up in an invalid state.
+
+ \sa endRemoveRows()
+*/
+void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last)
+{
+ Q_ASSERT(first >= 0);
+ Q_ASSERT(last >= first);
+ Q_D(QAbstractItemModel);
+ d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
+ emit rowsAboutToBeRemoved(parent, first, last);
+ d->rowsAboutToBeRemoved(parent, first, last);
+}
+
+/*!
+ Ends a row removal operation.
+
+ When reimplementing removeRows() in a subclass, you must call this function
+ \e after removing data from the model's underlying data store.
+
+ \sa beginRemoveRows()
+*/
+void QAbstractItemModel::endRemoveRows()
+{
+ Q_D(QAbstractItemModel);
+ QAbstractItemModelPrivate::Change change = d->changes.pop();
+ d->rowsRemoved(change.parent, change.first, change.last);
+ emit rowsRemoved(change.parent, change.first, change.last);
+}
+
+/*!
+ Returns whether a move operation is valid.
+
+ A move operation is not allowed if it moves a continuous range of rows to a destination within
+ itself, or if it attempts to move a row to one of its own descendants.
+
+ \internal
+*/
+bool QAbstractItemModelPrivate::allowMove(const QModelIndex &srcParent, int start, int end, const QModelIndex &destinationParent, int destinationStart, Qt::Orientation orientation)
+{
+ // Don't move the range within itself.
+ if (destinationParent == srcParent)
+ return !(destinationStart >= start && destinationStart <= end + 1);
+
+ QModelIndex destinationAncestor = destinationParent;
+ int pos = (Qt::Vertical == orientation) ? destinationAncestor.row() : destinationAncestor.column();
+ forever {
+ if (destinationAncestor == srcParent) {
+ if (pos >= start && pos <= end)
+ return false;
+ break;
+ }
+
+ if (!destinationAncestor.isValid())
+ break;
+
+ pos = (Qt::Vertical == orientation) ? destinationAncestor.row() : destinationAncestor.column();
+ destinationAncestor = destinationAncestor.parent();
+ }
+
+ return true;
+}
+
+/*!
+ \since 4.6
+
+ Begins a row move operation.
+
+ When reimplementing a subclass, this method simplifies moving
+ entities in your model. This method is responsible for moving
+ persistent indexes in the model, which you would otherwise be
+ required to do yourself. Using beginMoveRows and endMoveRows
+ is an alternative to emitting layoutAboutToBeChanged and
+ layoutChanged directly along with changePersistentIndexes.
+ layoutAboutToBeChanged is emitted by this method for compatibility
+ reasons.
+
+ The \a sourceParent index corresponds to the parent from which the
+ rows are moved; \a sourceFirst and \a sourceLast are the first and last
+ row numbers of the rows to be moved. The \a destinationParent index
+ corresponds to the parent into which those rows are moved. The \a
+ destinationChild is the row to which the rows will be moved. That
+ is, the index at row \a sourceFirst in \a sourceParent will become
+ row \a destinationChild in \a destinationParent, followed by all other
+ rows up to \a sourceLast.
+
+ However, when moving rows down in the same parent (\a sourceParent
+ and \a destinationParent are equal), the rows will be placed before the
+ \a destinationChild index. That is, if you wish to move rows 0 and 1 so
+ they will become rows 1 and 2, \a destinationChild should be 3. In this
+ case, the new index for the source row \c i (which is between
+ \a sourceFirst and \a sourceLast) is equal to
+ \c {(destinationChild-sourceLast-1+i)}.
+
+ Note that if \a sourceParent and \a destinationParent are the same,
+ you must ensure that the \a destinationChild is not within the range
+ of \a sourceFirst and \a sourceLast + 1. You must also ensure that you
+ do not attempt to move a row to one of its own children or ancestors.
+ This method returns false if either condition is true, in which case you
+ should abort your move operation.
+
+ \table 80%
+ \row
+ \o \inlineimage modelview-move-rows-1.png Moving rows to another parent
+ \o Specify the first and last row numbers for the span of rows in
+ the source parent you want to move in the model. Also specify
+ the row in the destination parent to move the span to.
+
+ For example, as shown in the diagram, we move three rows from
+ row 2 to 4 in the source, so \a sourceFirst is 2 and \a sourceLast is 4.
+ We move those items to above row 2 in the destination, so \a destinationChild is 2.
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 6
+
+ This moves the three rows rows 2, 3, and 4 in the source to become 2, 3 and 4 in
+ the destination. Other affected siblings are displaced accordingly.
+ \row
+ \o \inlineimage modelview-move-rows-2.png Moving rows to append to another parent
+ \o To append rows to another parent, move them to after the last row.
+
+ For example, as shown in the diagram, we move three rows to a
+ collection of 6 existing rows (ending in row 5), so \a destinationChild is 6:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 7
+
+ This moves the target rows to the end of the target parent as 6, 7 and 8.
+ \row
+ \o \inlineimage modelview-move-rows-3.png Moving rows in the same parent up
+ \o To move rows within the same parent, specify the row to move them to.
+
+ For example, as shown in the diagram, we move one item from row 2 to row 0,
+ so \a sourceFirst and \a sourceLast are 2 and \a destinationChild is 0.
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 8
+
+ Note that other rows may be displaced accordingly. Note also that when moving
+ items within the same parent you should not attempt invalid or no-op moves. In
+ the above example, item 2 is at row 2 before the move, so it can not be moved
+ to row 2 (where it is already) or row 3 (no-op as row 3 means above row 3, where
+ it is already)
+
+ \row
+ \o \inlineimage modelview-move-rows-4.png Moving rows in the same parent down
+ \o To move rows within the same parent, specify the row to move them to.
+
+ For example, as shown in the diagram, we move one item from row 2 to row 4,
+ so \a sourceFirst and \a sourceLast are 2 and \a destinationChild is 4.
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 9
+
+ Note that other rows may be displaced accordingly.
+ \endtable
+
+ \sa endMoveRows()
+*/
+bool QAbstractItemModel::beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
+{
+ Q_ASSERT(sourceFirst >= 0);
+ Q_ASSERT(sourceLast >= sourceFirst);
+ Q_ASSERT(destinationChild >= 0);
+ Q_D(QAbstractItemModel);
+
+ if (!d->allowMove(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical)) {
+ return false;
+ }
+
+ QAbstractItemModelPrivate::Change sourceChange(sourceParent, sourceFirst, sourceLast);
+ sourceChange.needsAdjust = sourceParent.isValid() && sourceParent.row() >= destinationChild && sourceParent.parent() == destinationParent;
+ d->changes.push(sourceChange);
+ int destinationLast = destinationChild + (sourceLast - sourceFirst);
+ QAbstractItemModelPrivate::Change destinationChange(destinationParent, destinationChild, destinationLast);
+ destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent;
+ d->changes.push(destinationChange);
+
+ emit rowsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild);
+ emit layoutAboutToBeChanged();
+ d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical);
+ return true;
+}
+
+/*!
+ Ends a row move operation.
+
+ When implementing a subclass, you must call this
+ function \e after moving data within the model's underlying data
+ store.
+
+ layoutChanged is emitted by this method for compatibility reasons.
+
+ \sa beginMoveRows()
+
+ \since 4.6
+*/
+void QAbstractItemModel::endMoveRows()
+{
+ Q_D(QAbstractItemModel);
+
+ QAbstractItemModelPrivate::Change insertChange = d->changes.pop();
+ QAbstractItemModelPrivate::Change removeChange = d->changes.pop();
+
+ QModelIndex adjustedSource = removeChange.parent;
+ QModelIndex adjustedDestination = insertChange.parent;
+
+ const int numMoved = removeChange.last - removeChange.first + 1;
+ if (insertChange.needsAdjust)
+ adjustedDestination = createIndex(adjustedDestination.row() - numMoved, adjustedDestination.column(), adjustedDestination.internalPointer());
+
+ if (removeChange.needsAdjust)
+ adjustedSource = createIndex(adjustedSource.row() + numMoved, adjustedSource.column(), adjustedSource.internalPointer());
+
+ d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Vertical);
+
+ emit rowsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first);
+ emit layoutChanged();
+}
+
+/*!
+ Begins a column insertion operation.
+
+ When reimplementing insertColumns() in a subclass, you must call this
+ function \e before inserting data into the model's underlying data store.
+
+ The \a parent index corresponds to the parent into which the new columns
+ are inserted; \a first and \a last are the column numbers of the new
+ columns will have after they have been inserted.
+
+ \table 80%
+ \row
+ \o \inlineimage modelview-begin-insert-columns.png Inserting columns
+ \o Specify the first and last column numbers for the span of columns
+ you want to insert into an item in a model.
+
+ For example, as shown in the diagram, we insert three columns
+ before column 4, so \a first is 4 and \a last is 6:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 3
+
+ This inserts the three new columns as columns 4, 5, and 6.
+ \row
+ \o \inlineimage modelview-begin-append-columns.png Appending columns
+ \o To append columns, insert them after the last column.
+
+ For example, as shown in the diagram, we append three columns to a
+ collection of six existing columns (ending in column 5), so
+ \a first is 6 and \a last is 8:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 4
+
+ This appends the two new columns as columns 6, 7, and 8.
+ \endtable
+
+ \note This function emits the columnsAboutToBeInserted() signal which
+ connected views (or proxies) must handle before the data is inserted.
+ Otherwise, the views may end up in an invalid state.
+
+ \sa endInsertColumns()
+*/
+void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first, int last)
+{
+ Q_ASSERT(first >= 0);
+ Q_ASSERT(last >= first);
+ Q_D(QAbstractItemModel);
+ d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
+ emit columnsAboutToBeInserted(parent, first, last);
+ d->columnsAboutToBeInserted(parent, first, last);
+}
+
+/*!
+ Ends a column insertion operation.
+
+ When reimplementing insertColumns() in a subclass, you must call this
+ function \e after inserting data into the model's underlying data
+ store.
+
+ \sa beginInsertColumns()
+*/
+void QAbstractItemModel::endInsertColumns()
+{
+ Q_D(QAbstractItemModel);
+ QAbstractItemModelPrivate::Change change = d->changes.pop();
+ d->columnsInserted(change.parent, change.first, change.last);
+ emit columnsInserted(change.parent, change.first, change.last);
+}
+
+/*!
+ Begins a column removal operation.
+
+ When reimplementing removeColumns() in a subclass, you must call this
+ function \e before removing data from the model's underlying data store.
+
+ The \a parent index corresponds to the parent from which the new columns
+ are removed; \a first and \a last are the column numbers of the first and
+ last columns to be removed.
+
+ \table 80%
+ \row
+ \o \inlineimage modelview-begin-remove-columns.png Removing columns
+ \o Specify the first and last column numbers for the span of columns
+ you want to remove from an item in a model.
+
+ For example, as shown in the diagram, we remove the three columns
+ from column 4 to column 6, so \a first is 4 and \a last is 6:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp 5
+ \endtable
+
+ \note This function emits the columnsAboutToBeRemoved() signal which
+ connected views (or proxies) must handle before the data is removed.
+ Otherwise, the views may end up in an invalid state.
+
+ \sa endRemoveColumns()
+*/
+void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first, int last)
+{
+ Q_ASSERT(first >= 0);
+ Q_ASSERT(last >= first);
+ Q_D(QAbstractItemModel);
+ d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
+ emit columnsAboutToBeRemoved(parent, first, last);
+ d->columnsAboutToBeRemoved(parent, first, last);
+}
+
+/*!
+ Ends a column removal operation.
+
+ When reimplementing removeColumns() in a subclass, you must call this
+ function \e after removing data from the model's underlying data store.
+
+ \sa beginRemoveColumns()
+*/
+void QAbstractItemModel::endRemoveColumns()
+{
+ Q_D(QAbstractItemModel);
+ QAbstractItemModelPrivate::Change change = d->changes.pop();
+ d->columnsRemoved(change.parent, change.first, change.last);
+ emit columnsRemoved(change.parent, change.first, change.last);
+}
+
+/*!
+ Begins a column move operation.
+
+ When reimplementing a subclass, this method simplifies moving
+ entities in your model. This method is responsible for moving
+ persistent indexes in the model, which you would otherwise be
+ required to do yourself. Using beginMoveRows and endMoveRows
+ is an alternative to emitting layoutAboutToBeChanged and
+ layoutChanged directly along with changePersistentIndexes.
+ layoutAboutToBeChanged is emitted by this method for compatibility
+ reasons.
+
+ The \a sourceParent index corresponds to the parent from which the
+ columns are moved; \a sourceFirst and \a sourceLast are the first and last
+ column numbers of the columns to be moved. The \a destinationParent index
+ corresponds to the parent into which those columns are moved. The \a
+ destinationChild is the column to which the columns will be moved. That
+ is, the index at column \a sourceFirst in \a sourceParent will become
+ column \a destinationChild in \a destinationParent, followed by all other
+ columns up to \a sourceLast.
+
+ However, when moving columns down in the same parent (\a sourceParent
+ and \a destinationParent are equal), the columnss will be placed before the
+ \a destinationChild index. That is, if you wish to move columns 0 and 1 so
+ they will become columns 1 and 2, \a destinationChild should be 3. In this
+ case, the new index for the source column \c i (which is between
+ \a sourceFirst and \a sourceLast) is equal to
+ \c {(destinationChild-sourceLast-1+i)}.
+
+ Note that if \a sourceParent and \a destinationParent are the same,
+ you must ensure that the \a destinationChild is not within the range
+ of \a sourceFirst and \a sourceLast + 1. You must also ensure that you
+ do not attempt to move a column to one of its own children or ancestors.
+ This method returns false if either condition is true, in which case you
+ should abort your move operation.
+
+ \sa endMoveColumns()
+
+ \since 4.6
+*/
+bool QAbstractItemModel::beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
+{
+ Q_ASSERT(sourceFirst >= 0);
+ Q_ASSERT(sourceLast >= sourceFirst);
+ Q_ASSERT(destinationChild >= 0);
+ Q_D(QAbstractItemModel);
+
+ if (!d->allowMove(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal)) {
+ return false;
+ }
+
+ QAbstractItemModelPrivate::Change sourceChange(sourceParent, sourceFirst, sourceLast);
+ sourceChange.needsAdjust = sourceParent.isValid() && sourceParent.row() >= destinationChild && sourceParent.parent() == destinationParent;
+ d->changes.push(sourceChange);
+ int destinationLast = destinationChild + (sourceLast - sourceFirst);
+ QAbstractItemModelPrivate::Change destinationChange(destinationParent, destinationChild, destinationLast);
+ destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent;
+ d->changes.push(destinationChange);
+
+ d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal);
+
+ emit columnsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild);
+ emit layoutAboutToBeChanged();
+ return true;
+}
+
+/*!
+ Ends a column move operation.
+
+ When implementing a subclass, you must call this
+ function \e after moving data within the model's underlying data
+ store.
+
+ layoutChanged is emitted by this method for compatibility reasons.
+
+ \sa beginMoveColumns()
+
+ \since 4.6
+*/
+void QAbstractItemModel::endMoveColumns()
+{
+ Q_D(QAbstractItemModel);
+
+ QAbstractItemModelPrivate::Change insertChange = d->changes.pop();
+ QAbstractItemModelPrivate::Change removeChange = d->changes.pop();
+
+ QModelIndex adjustedSource = removeChange.parent;
+ QModelIndex adjustedDestination = insertChange.parent;
+
+ const int numMoved = removeChange.last - removeChange.first + 1;
+ if (insertChange.needsAdjust)
+ adjustedDestination = createIndex(adjustedDestination.row(), adjustedDestination.column() - numMoved, adjustedDestination.internalPointer());
+
+ if (removeChange.needsAdjust)
+ adjustedSource = createIndex(adjustedSource.row(), adjustedSource.column() + numMoved, adjustedSource.internalPointer());
+
+ d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Horizontal);
+
+ emit columnsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first);
+ emit layoutChanged();
+}
+
+/*!
+ Resets the model to its original state in any attached views.
+
+ \note Use beginResetModel() and endResetModel() instead whenever possible.
+ Use this method only if there is no way to call beginResetModel() before invalidating the model.
+ Otherwise it could lead to unexpected behaviour, especially when used with proxy models.
+*/
+void QAbstractItemModel::reset()
+{
+ Q_D(QAbstractItemModel);
+ emit modelAboutToBeReset();
+ d->invalidatePersistentIndexes();
+ emit modelReset();
+}
+
+/*!
+ Begins a model reset operation.
+
+ A reset operation resets the model to its current state in any attached views.
+
+ \note Any views attached to this model will be reset as well.
+
+ When a model is reset it means that any previous data reported from the
+ model is now invalid and has to be queried for again. This also means that
+ the current item and any selected items will become invalid.
+
+ When a model radically changes its data it can sometimes be easier to just
+ call this function rather than emit dataChanged() to inform other
+ components when the underlying data source, or its structure, has changed.
+
+ You must call this function before resetting any internal data structures in your model
+ or proxy model.
+
+ \sa modelAboutToBeReset(), modelReset(), endResetModel()
+ \since 4.6
+*/
+void QAbstractItemModel::beginResetModel()
+{
+ emit modelAboutToBeReset();
+}
+
+/*!
+ Completes a model reset operation.
+
+ You must call this function after resetting any internal data structure in your model
+ or proxy model.
+
+ \sa beginResetModel()
+ \since 4.6
+*/
+void QAbstractItemModel::endResetModel()
+{
+ Q_D(QAbstractItemModel);
+ d->invalidatePersistentIndexes();
+ emit modelReset();
+}
+
+/*!
+ Changes the QPersistentModelIndex that is equal to the given \a from model
+ index to the given \a to model index.
+
+ If no persistent model index equal to the given \a from model index was
+ found, nothing is changed.
+
+ \sa persistentIndexList(), changePersistentIndexList()
+*/
+void QAbstractItemModel::changePersistentIndex(const QModelIndex &from, const QModelIndex &to)
+{
+ Q_D(QAbstractItemModel);
+ if (d->persistent.indexes.isEmpty())
+ return;
+ // find the data and reinsert it sorted
+ const QHash<QModelIndex, QPersistentModelIndexData *>::iterator it = d->persistent.indexes.find(from);
+ if (it != d->persistent.indexes.end()) {
+ QPersistentModelIndexData *data = *it;
+ d->persistent.indexes.erase(it);
+ data->index = to;
+ if (to.isValid())
+ d->persistent.insertMultiAtEnd(to, data);
+ else
+ data->model = 0;
+ }
+}
+
+/*!
+ \since 4.1
+
+ Changes the QPersistentModelIndexes that is equal to the indexes in the
+ given \a from model index list to the given \a to model index list.
+
+ If no persistent model indexes equal to the indexes in the given \a from
+ model index list was found, nothing is changed.
+
+ \sa persistentIndexList(), changePersistentIndex()
+*/
+void QAbstractItemModel::changePersistentIndexList(const QModelIndexList &from,
+ const QModelIndexList &to)
+{
+ Q_D(QAbstractItemModel);
+ if (d->persistent.indexes.isEmpty())
+ return;
+ QVector<QPersistentModelIndexData *> toBeReinserted;
+ toBeReinserted.reserve(to.count());
+ for (int i = 0; i < from.count(); ++i) {
+ if (from.at(i) == to.at(i))
+ continue;
+ const QHash<QModelIndex, QPersistentModelIndexData *>::iterator it = d->persistent.indexes.find(from.at(i));
+ if (it != d->persistent.indexes.end()) {
+ QPersistentModelIndexData *data = *it;
+ d->persistent.indexes.erase(it);
+ data->index = to.at(i);
+ if (data->index.isValid())
+ toBeReinserted << data;
+ else
+ data->model = 0;
+ }
+ }
+
+ for (QVector<QPersistentModelIndexData *>::const_iterator it = toBeReinserted.constBegin();
+ it != toBeReinserted.constEnd() ; ++it) {
+ QPersistentModelIndexData *data = *it;
+ d->persistent.insertMultiAtEnd(data->index, data);
+ }
+}
+
+/*!
+ \since 4.2
+
+ Returns the list of indexes stored as persistent indexes in the model.
+*/
+QModelIndexList QAbstractItemModel::persistentIndexList() const
+{
+ Q_D(const QAbstractItemModel);
+ QModelIndexList result;
+ for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = d->persistent.indexes.constBegin();
+ it != d->persistent.indexes.constEnd(); ++it) {
+ QPersistentModelIndexData *data = *it;
+ result.append(data->index);
+ }
+ return result;
+}
+
+
+/*!
+ \class QAbstractTableModel
+ \brief The QAbstractTableModel class provides an abstract model that can be
+ subclassed to create table models.
+
+ \ingroup model-view
+
+ QAbstractTableModel provides a standard interface for models that represent
+ their data as a two-dimensional array of items. It is not used directly,
+ but must be subclassed.
+
+ Since the model provides a more specialized interface than
+ QAbstractItemModel, it is not suitable for use with tree views, although it
+ can be used to provide data to a QListView. If you need to represent a
+ simple list of items, and only need a model to contain a single column of
+ data, subclassing the QAbstractListModel may be more appropriate.
+
+ The rowCount() and columnCount() functions return the dimensions of the
+ table. To retrieve a model index corresponding to an item in the model, use
+ index() and provide only the row and column numbers.
+
+ \section1 Subclassing
+
+ When subclassing QAbstractTableModel, you must implement rowCount(),
+ columnCount(), and data(). Default implementations of the index() and
+ parent() functions are provided by QAbstractTableModel.
+ Well behaved models will also implement headerData().
+
+ Editable models need to implement setData(), and implement flags() to
+ return a value containing
+ \l{Qt::ItemFlags}{Qt::ItemIsEditable}.
+
+ Models that provide interfaces to resizable data structures can
+ provide implementations of insertRows(), removeRows(), insertColumns(),
+ and removeColumns(). When implementing these functions, it is
+ important to call the appropriate functions so that all connected views
+ are aware of any changes:
+
+ \list
+ \o An insertRows() implementation must call beginInsertRows()
+ \e before inserting new rows into the data structure, and it must
+ call endInsertRows() \e{immediately afterwards}.
+ \o An insertColumns() implementation must call beginInsertColumns()
+ \e before inserting new columns into the data structure, and it must
+ call endInsertColumns() \e{immediately afterwards}.
+ \o A removeRows() implementation must call beginRemoveRows()
+ \e before the rows are removed from the data structure, and it must
+ call endRemoveRows() \e{immediately afterwards}.
+ \o A removeColumns() implementation must call beginRemoveColumns()
+ \e before the columns are removed from the data structure, and it must
+ call endRemoveColumns() \e{immediately afterwards}.
+ \endlist
+
+ \note Some general guidelines for subclassing models are available in the
+ \l{Model Subclassing Reference}.
+
+ \note
+
+ \sa {Model Classes}, QAbstractItemModel, QAbstractListModel,
+ {Pixelator Example}
+*/
+
+/*!
+ Constructs an abstract table model for the given \a parent.
+*/
+
+QAbstractTableModel::QAbstractTableModel(QObject *parent)
+ : QAbstractItemModel(parent)
+{
+
+}
+
+/*!
+ \internal
+
+ Constructs an abstract table model with \a dd and the given \a parent.
+*/
+
+QAbstractTableModel::QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent)
+ : QAbstractItemModel(dd, parent)
+{
+
+}
+
+/*!
+ Destroys the abstract table model.
+*/
+
+QAbstractTableModel::~QAbstractTableModel()
+{
+
+}
+
+/*!
+ \fn QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
+
+ Returns the index of the data in \a row and \a column with \a parent.
+
+ \sa parent()
+*/
+
+QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const
+{
+ return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
+}
+
+/*!
+ \fn QModelIndex QAbstractTableModel::parent(const QModelIndex &index) const
+
+ Returns the parent of the model item with the given \a index.
+
+ \sa index() hasChildren()
+*/
+
+QModelIndex QAbstractTableModel::parent(const QModelIndex &) const
+{
+ return QModelIndex();
+}
+
+bool QAbstractTableModel::hasChildren(const QModelIndex &parent) const
+{
+ if (parent.model() == this || !parent.isValid())
+ return rowCount(parent) > 0 && columnCount(parent) > 0;
+ return false;
+}
+
+/*!
+ \class QAbstractListModel
+ \brief The QAbstractListModel class provides an abstract model that can be
+ subclassed to create one-dimensional list models.
+
+ \ingroup model-view
+
+ QAbstractListModel provides a standard interface for models that represent
+ their data as a simple non-hierarchical sequence of items. It is not used
+ directly, but must be subclassed.
+
+ Since the model provides a more specialized interface than
+ QAbstractItemModel, it is not suitable for use with tree views; you will
+ need to subclass QAbstractItemModel if you want to provide a model for
+ that purpose. If you need to use a number of list models to manage data,
+ it may be more appropriate to subclass QAbstractTableModel class instead.
+
+ Simple models can be created by subclassing this class and implementing
+ the minimum number of required functions. For example, we could implement
+ a simple read-only QStringList-based model that provides a list of strings
+ to a QListView widget. In such a case, we only need to implement the
+ rowCount() function to return the number of items in the list, and the
+ data() function to retrieve items from the list.
+
+ Since the model represents a one-dimensional structure, the rowCount()
+ function returns the total number of items in the model. The columnCount()
+ function is implemented for interoperability with all kinds of views, but
+ by default informs views that the model contains only one column.
+
+ \section1 Subclassing
+
+ When subclassing QAbstractListModel, you must provide implementations
+ of the rowCount() and data() functions. Well behaved models also provide
+ a headerData() implementation.
+
+ For editable list models, you must also provide an implementation of
+ setData(), implement the flags() function so that it returns a value
+ containing \l{Qt::ItemFlags}{Qt::ItemIsEditable}.
+
+ Note that QAbstractListModel provides a default implementation of
+ columnCount() that informs views that there is only a single column
+ of items in this model.
+
+ Models that provide interfaces to resizable list-like data structures
+ can provide implementations of insertRows() and removeRows(). When
+ implementing these functions, it is important to call the appropriate
+ functions so that all connected views are aware of any changes:
+
+ \list
+ \o An insertRows() implementation must call beginInsertRows()
+ \e before inserting new rows into the data structure, and it must
+ call endInsertRows() \e{immediately afterwards}.
+ \o A removeRows() implementation must call beginRemoveRows()
+ \e before the rows are removed from the data structure, and it must
+ call endRemoveRows() \e{immediately afterwards}.
+ \endlist
+
+ \note Some general guidelines for subclassing models are available in the
+ \l{Model Subclassing Reference}.
+
+ \sa {Model Classes}, {Model Subclassing Reference}, QAbstractItemView,
+ QAbstractTableModel, {Item Views Puzzle Example}
+*/
+
+/*!
+ Constructs an abstract list model with the given \a parent.
+*/
+
+QAbstractListModel::QAbstractListModel(QObject *parent)
+ : QAbstractItemModel(parent)
+{
+
+}
+
+/*!
+ \internal
+
+ Constructs an abstract list model with \a dd and the given \a parent.
+*/
+
+QAbstractListModel::QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent)
+ : QAbstractItemModel(dd, parent)
+{
+
+}
+
+/*!
+ Destroys the abstract list model.
+*/
+
+QAbstractListModel::~QAbstractListModel()
+{
+
+}
+
+/*!
+ \fn QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
+
+ Returns the index of the data in \a row and \a column with \a parent.
+
+ \sa parent()
+*/
+
+QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent) const
+{
+ return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
+}
+
+/*!
+ Returns the parent of the model item with the given \a index.
+
+ \sa index() hasChildren()
+*/
+
+QModelIndex QAbstractListModel::parent(const QModelIndex & /* index */) const
+{
+ return QModelIndex();
+}
+
+/*!
+ \internal
+
+ Returns the number of columns in the list with the given \a parent.
+
+ \sa rowCount()
+*/
+
+int QAbstractListModel::columnCount(const QModelIndex &parent) const
+{
+ return parent.isValid() ? 0 : 1;
+}
+
+bool QAbstractListModel::hasChildren(const QModelIndex &parent) const
+{
+ return parent.isValid() ? false : (rowCount() > 0);
+}
+
+/*!
+ \typedef QModelIndexList
+ \relates QModelIndex
+
+ Synonym for QList<QModelIndex>.
+*/
+
+/*!
+ \reimp
+*/
+bool QAbstractTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent)
+{
+ if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
+ return false;
+
+ QStringList types = mimeTypes();
+ if (types.isEmpty())
+ return false;
+ QString format = types.at(0);
+ if (!data->hasFormat(format))
+ return false;
+
+ QByteArray encoded = data->data(format);
+ QDataStream stream(&encoded, QIODevice::ReadOnly);
+
+ // if the drop is on an item, replace the data in the items
+ if (parent.isValid() && row == -1 && column == -1) {
+ int top = INT_MAX;
+ int left = INT_MAX;
+ QVector<int> rows, columns;
+ QVector<QMap<int, QVariant> > data;
+
+ while (!stream.atEnd()) {
+ int r, c;
+ QMap<int, QVariant> v;
+ stream >> r >> c >> v;
+ rows.append(r);
+ columns.append(c);
+ data.append(v);
+ top = qMin(r, top);
+ left = qMin(c, left);
+ }
+
+ for (int i = 0; i < data.size(); ++i) {
+ int r = (rows.at(i) - top) + parent.row();
+ int c = (columns.at(i) - left) + parent.column();
+ if (hasIndex(r, c))
+ setItemData(index(r, c), data.at(i));
+ }
+
+ return true;
+ }
+
+ // otherwise insert new rows for the data
+ return decodeData(row, column, parent, stream);
+}
+
+/*!
+ \reimp
+*/
+bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent)
+{
+ if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
+ return false;
+
+ QStringList types = mimeTypes();
+ if (types.isEmpty())
+ return false;
+ QString format = types.at(0);
+ if (!data->hasFormat(format))
+ return false;
+
+ QByteArray encoded = data->data(format);
+ QDataStream stream(&encoded, QIODevice::ReadOnly);
+
+ // if the drop is on an item, replace the data in the items
+ if (parent.isValid() && row == -1 && column == -1) {
+ int top = INT_MAX;
+ int left = INT_MAX;
+ QVector<int> rows, columns;
+ QVector<QMap<int, QVariant> > data;
+
+ while (!stream.atEnd()) {
+ int r, c;
+ QMap<int, QVariant> v;
+ stream >> r >> c >> v;
+ rows.append(r);
+ columns.append(c);
+ data.append(v);
+ top = qMin(r, top);
+ left = qMin(c, left);
+ }
+
+ for (int i = 0; i < data.size(); ++i) {
+ int r = (rows.at(i) - top) + parent.row();
+ if (columns.at(i) == left && hasIndex(r, 0))
+ setItemData(index(r), data.at(i));
+ }
+
+ return true;
+ }
+
+ if (row == -1)
+ row = rowCount(parent);
+
+ // otherwise insert new rows for the data
+ return decodeData(row, column, parent, stream);
+}
+
+/*!
+ \fn QAbstractItemModel::modelAboutToBeReset()
+ \since 4.2
+
+ This signal is emitted when reset() is called, before the model's internal
+ state (e.g. persistent model indexes) has been invalidated.
+
+ \sa beginResetModel(), modelReset()
+*/
+
+/*!
+ \fn QAbstractItemModel::modelReset()
+ \since 4.1
+
+ This signal is emitted when reset() is called, after the model's internal
+ state (e.g. persistent model indexes) has been invalidated.
+
+ \sa endResetModel(), modelAboutToBeReset()
+*/
+
+/*!
+ \fn bool QModelIndex::operator<(const QModelIndex &other) const
+ \since 4.1
+
+ Returns true if this model index is smaller than the \a other
+ model index; otherwise returns false.
+*/
+
+/*!
+ \fn uint qHash(const QPersistentModelIndex &index)
+ \since 4.5
+
+ Returns a hash of the QPersistentModelIndex
+ */
+
+
+/*!
+ \internal
+ QHash::insertMulti insert the value before the old value. and find() return the new value.
+ We need insertMultiAtEnd because we don't want to overwrite the old one, which should be removed later
+
+ There should be only one instance QPersistentModelIndexData per index, but in some intermediate state there may be
+ severals of PersistantModelIndex pointing to the same index, but one is already updated, and the other one is not.
+ This make sure than when updating the first one we don't overwrite the second one in the hash, and the second one
+ will be updated right later.
+ */
+void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data)
+{
+ QHash<QModelIndex,QPersistentModelIndexData *>::iterator newIt =
+ indexes.insertMulti(key, data);
+ QHash<QModelIndex,QPersistentModelIndexData *>::iterator it = newIt + 1;
+ while (it != indexes.end() && it.key() == key) {
+ qSwap(*newIt,*it);
+ newIt = it;
+ ++it;
+ }
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qabstractitemmodel.h b/src/corelib/kernel/qabstractitemmodel.h
new file mode 100644
index 0000000000..6de3bf5e4f
--- /dev/null
+++ b/src/corelib/kernel/qabstractitemmodel.h
@@ -0,0 +1,410 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QABSTRACTITEMMODEL_H
+#define QABSTRACTITEMMODEL_H
+
+#include <QtCore/qvariant.h>
+#include <QtCore/qobject.h>
+#include <QtCore/qhash.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Core)
+
+class QAbstractItemModel;
+class QPersistentModelIndex;
+
+class Q_CORE_EXPORT QModelIndex
+{
+ friend class QAbstractItemModel;
+ friend class QProxyModel;
+public:
+ inline QModelIndex() : r(-1), c(-1), p(0), m(0) {}
+ inline QModelIndex(const QModelIndex &other)
+ : r(other.r), c(other.c), p(other.p), m(other.m) {}
+ inline ~QModelIndex() { p = 0; m = 0; }
+ inline int row() const { return r; }
+ inline int column() const { return c; }
+ inline void *internalPointer() const { return p; }
+ inline qint64 internalId() const { return reinterpret_cast<qint64>(p); }
+ inline QModelIndex parent() const;
+ inline QModelIndex sibling(int row, int column) const;
+ inline QModelIndex child(int row, int column) const;
+ inline QVariant data(int role = Qt::DisplayRole) const;
+ inline Qt::ItemFlags flags() const;
+ inline const QAbstractItemModel *model() const { return m; }
+ inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); }
+ inline bool operator==(const QModelIndex &other) const
+ { return (other.r == r) && (other.p == p) && (other.c == c) && (other.m == m); }
+ inline bool operator!=(const QModelIndex &other) const
+ { return !(*this == other); }
+ inline bool operator<(const QModelIndex &other) const
+ {
+ if (r < other.r) return true;
+ if (r == other.r) {
+ if (c < other.c) return true;
+ if (c == other.c) {
+ if (p < other.p) return true;
+ if (p == other.p) return m < other.m;
+ }
+ }
+ return false; }
+private:
+ inline QModelIndex(int row, int column, void *ptr, const QAbstractItemModel *model);
+ int r, c;
+ void *p;
+ const QAbstractItemModel *m;
+};
+Q_DECLARE_TYPEINFO(QModelIndex, Q_MOVABLE_TYPE);
+
+#ifndef QT_NO_DEBUG_STREAM
+Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &);
+#endif
+
+class QPersistentModelIndexData;
+
+class Q_CORE_EXPORT QPersistentModelIndex
+{
+public:
+ QPersistentModelIndex();
+ QPersistentModelIndex(const QModelIndex &index);
+ QPersistentModelIndex(const QPersistentModelIndex &other);
+ ~QPersistentModelIndex();
+ bool operator<(const QPersistentModelIndex &other) const;
+ bool operator==(const QPersistentModelIndex &other) const;
+ inline bool operator!=(const QPersistentModelIndex &other) const
+ { return !operator==(other); }
+ QPersistentModelIndex &operator=(const QPersistentModelIndex &other);
+ bool operator==(const QModelIndex &other) const;
+ bool operator!=(const QModelIndex &other) const;
+ QPersistentModelIndex &operator=(const QModelIndex &other);
+ operator const QModelIndex&() const;
+ int row() const;
+ int column() const;
+ void *internalPointer() const;
+ qint64 internalId() const;
+ QModelIndex parent() const;
+ QModelIndex sibling(int row, int column) const;
+ QModelIndex child(int row, int column) const;
+ QVariant data(int role = Qt::DisplayRole) const;
+ Qt::ItemFlags flags() const;
+ const QAbstractItemModel *model() const;
+ bool isValid() const;
+private:
+ QPersistentModelIndexData *d;
+ friend uint qHash(const QPersistentModelIndex &);
+#ifndef QT_NO_DEBUG_STREAM
+ friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
+#endif
+};
+Q_DECLARE_TYPEINFO(QPersistentModelIndex, Q_MOVABLE_TYPE);
+
+inline uint qHash(const QPersistentModelIndex &index)
+{ return qHash(index.d); }
+
+
+#ifndef QT_NO_DEBUG_STREAM
+Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
+#endif
+
+template<typename T> class QList;
+typedef QList<QModelIndex> QModelIndexList;
+
+class QMimeData;
+class QAbstractItemModelPrivate;
+template <class Key, class T> class QMap;
+
+
+class Q_CORE_EXPORT QAbstractItemModel : public QObject
+{
+ Q_OBJECT
+
+ friend class QPersistentModelIndexData;
+ friend class QAbstractItemViewPrivate;
+public:
+
+ explicit QAbstractItemModel(QObject *parent = 0);
+ virtual ~QAbstractItemModel();
+
+ bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const;
+ virtual QModelIndex index(int row, int column,
+ const QModelIndex &parent = QModelIndex()) const = 0;
+ virtual QModelIndex parent(const QModelIndex &child) const = 0;
+
+ inline QModelIndex sibling(int row, int column, const QModelIndex &idx) const
+ { return index(row, column, parent(idx)); }
+
+ virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0;
+ virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
+ virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
+
+ virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0;
+ virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
+
+ virtual QVariant headerData(int section, Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+ virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
+ int role = Qt::EditRole);
+
+ virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
+ virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
+
+ virtual QStringList mimeTypes() const;
+ virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
+ virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent);
+ virtual Qt::DropActions supportedDropActions() const;
+
+ Qt::DropActions supportedDragActions() const;
+ void setSupportedDragActions(Qt::DropActions);
+
+ virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
+ virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex());
+ virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
+ virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
+
+ inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
+ inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
+ inline bool removeRow(int row, const QModelIndex &parent = QModelIndex());
+ inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex());
+
+ virtual void fetchMore(const QModelIndex &parent);
+ virtual bool canFetchMore(const QModelIndex &parent) const;
+ virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+ virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
+ virtual QModelIndex buddy(const QModelIndex &index) const;
+ virtual QModelIndexList match(const QModelIndex &start, int role,
+ const QVariant &value, int hits = 1,
+ Qt::MatchFlags flags =
+ Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
+ virtual QSize span(const QModelIndex &index) const;
+
+ const QHash<int,QByteArray> &roleNames() const;
+
+#ifdef Q_NO_USING_KEYWORD
+ inline QObject *parent() const { return QObject::parent(); }
+#else
+ using QObject::parent;
+#endif
+
+Q_SIGNALS:
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+ void headerDataChanged(Qt::Orientation orientation, int first, int last);
+ void layoutChanged();
+ void layoutAboutToBeChanged();
+
+#if !defined(Q_MOC_RUN) && !defined(qdoc)
+private: // can only be emitted by QAbstractItemModel
+#endif
+ void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
+ void rowsInserted(const QModelIndex &parent, int first, int last);
+
+ void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
+ void rowsRemoved(const QModelIndex &parent, int first, int last);
+
+ void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
+ void columnsInserted(const QModelIndex &parent, int first, int last);
+
+ void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
+ void columnsRemoved(const QModelIndex &parent, int first, int last);
+
+ void modelAboutToBeReset();
+ void modelReset();
+
+ void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow );
+ void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row );
+
+ void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn );
+ void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column );
+
+
+public Q_SLOTS:
+ virtual bool submit();
+ virtual void revert();
+
+protected:
+ QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = 0);
+
+ inline QModelIndex createIndex(int row, int column, void *data = 0) const;
+ inline QModelIndex createIndex(int row, int column, int id) const;
+ inline QModelIndex createIndex(int row, int column, quint32 id) const;
+
+ void encodeData(const QModelIndexList &indexes, QDataStream &stream) const;
+ bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream);
+
+ void beginInsertRows(const QModelIndex &parent, int first, int last);
+ void endInsertRows();
+
+ void beginRemoveRows(const QModelIndex &parent, int first, int last);
+ void endRemoveRows();
+
+ bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
+ void endMoveRows();
+
+ void beginInsertColumns(const QModelIndex &parent, int first, int last);
+ void endInsertColumns();
+
+ void beginRemoveColumns(const QModelIndex &parent, int first, int last);
+ void endRemoveColumns();
+
+ bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);
+ void endMoveColumns();
+
+ void reset();
+
+ void beginResetModel();
+ void endResetModel();
+
+ void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
+ void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
+ QModelIndexList persistentIndexList() const;
+
+ void setRoleNames(const QHash<int,QByteArray> &roleNames);
+
+private:
+ Q_DECLARE_PRIVATE(QAbstractItemModel)
+ Q_DISABLE_COPY(QAbstractItemModel)
+};
+
+inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
+{ return insertRows(arow, 1, aparent); }
+inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
+{ return insertColumns(acolumn, 1, aparent); }
+inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
+{ return removeRows(arow, 1, aparent); }
+inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
+{ return removeColumns(acolumn, 1, aparent); }
+
+inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
+{ return QModelIndex(arow, acolumn, adata, this); }
+inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, int aid) const
+#if defined(Q_CC_MSVC)
+#pragma warning( push )
+#pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit
+#endif
+{ return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); }
+#if defined(Q_CC_MSVC)
+#pragma warning( pop )
+#endif
+inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quint32 aid) const
+#if defined(Q_CC_MSVC)
+#pragma warning( push )
+#pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit
+#endif
+{ return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); }
+#if defined(Q_CC_MSVC)
+#pragma warning( pop )
+#endif
+
+
+class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel
+{
+ Q_OBJECT
+
+public:
+ explicit QAbstractTableModel(QObject *parent = 0);
+ ~QAbstractTableModel();
+
+ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
+ bool dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent);
+protected:
+ QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent);
+
+private:
+ Q_DISABLE_COPY(QAbstractTableModel)
+ QModelIndex parent(const QModelIndex &child) const;
+ bool hasChildren(const QModelIndex &parent) const;
+};
+
+class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel
+{
+ Q_OBJECT
+
+public:
+ explicit QAbstractListModel(QObject *parent = 0);
+ ~QAbstractListModel();
+
+ QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
+ bool dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent);
+protected:
+ QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent);
+
+private:
+ Q_DISABLE_COPY(QAbstractListModel)
+ QModelIndex parent(const QModelIndex &child) const;
+ int columnCount(const QModelIndex &parent) const;
+ bool hasChildren(const QModelIndex &parent) const;
+};
+
+// inline implementations
+
+inline QModelIndex::QModelIndex(int arow, int acolumn, void *adata,
+ const QAbstractItemModel *amodel)
+ : r(arow), c(acolumn), p(adata), m(amodel) {}
+
+inline QModelIndex QModelIndex::parent() const
+{ return m ? m->parent(*this) : QModelIndex(); }
+
+inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const
+{ return m ? (r == arow && c == acolumn) ? *this : m->index(arow, acolumn, m->parent(*this)) : QModelIndex(); }
+
+inline QModelIndex QModelIndex::child(int arow, int acolumn) const
+{ return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
+
+inline QVariant QModelIndex::data(int arole) const
+{ return m ? m->data(*this, arole) : QVariant(); }
+
+inline Qt::ItemFlags QModelIndex::flags() const
+{ return m ? m->flags(*this) : Qt::ItemFlags(0); }
+
+inline uint qHash(const QModelIndex &index)
+{ return uint((index.row() << 4) + index.column() + index.internalId()); }
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QABSTRACTITEMMODEL_H
diff --git a/src/corelib/kernel/qabstractitemmodel_p.h b/src/corelib/kernel/qabstractitemmodel_p.h
new file mode 100644
index 0000000000..dfe5cef86a
--- /dev/null
+++ b/src/corelib/kernel/qabstractitemmodel_p.h
@@ -0,0 +1,176 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QABSTRACTITEMMODEL_P_H
+#define QABSTRACTITEMMODEL_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of QAbstractItemModel*. This header file may change from version
+// to version without notice, or even be removed.
+//
+// We mean it.
+//
+//
+
+#include "private/qobject_p.h"
+#include "QtCore/qstack.h"
+#include "QtCore/qset.h"
+#include "QtCore/qhash.h"
+
+QT_BEGIN_NAMESPACE
+
+class QPersistentModelIndexData
+{
+public:
+ QPersistentModelIndexData() : model(0) {}
+ QPersistentModelIndexData(const QModelIndex &idx) : index(idx), model(idx.model()) {}
+ QModelIndex index;
+ QAtomicInt ref;
+ const QAbstractItemModel *model;
+ static QPersistentModelIndexData *create(const QModelIndex &index);
+ static void destroy(QPersistentModelIndexData *data);
+};
+
+class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate
+{
+ Q_DECLARE_PUBLIC(QAbstractItemModel)
+
+public:
+ QAbstractItemModelPrivate() : QObjectPrivate(), supportedDragActions(-1), roleNames(defaultRoleNames()) {}
+ void removePersistentIndexData(QPersistentModelIndexData *data);
+ void movePersistentIndexes(QVector<QPersistentModelIndexData *> indexes, int change, const QModelIndex &parent, Qt::Orientation orientation);
+ void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
+ void rowsInserted(const QModelIndex &parent, int first, int last);
+ void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
+ void rowsRemoved(const QModelIndex &parent, int first, int last);
+ void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
+ void columnsInserted(const QModelIndex &parent, int first, int last);
+ void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
+ void columnsRemoved(const QModelIndex &parent, int first, int last);
+ static QAbstractItemModel *staticEmptyModel();
+ static bool variantLessThan(const QVariant &v1, const QVariant &v2);
+
+ void itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation);
+ void itemsMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
+ bool allowMove(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
+
+ inline QModelIndex createIndex(int row, int column, void *data = 0) const {
+ return q_func()->createIndex(row, column, data);
+ }
+
+ inline QModelIndex createIndex(int row, int column, int id) const {
+ return q_func()->createIndex(row, column, id);
+ }
+
+ inline bool indexValid(const QModelIndex &index) const {
+ return (index.row() >= 0) && (index.column() >= 0) && (index.model() == q_func());
+ }
+
+ inline void invalidatePersistentIndexes() {
+ foreach (QPersistentModelIndexData *data, persistent.indexes) {
+ data->index = QModelIndex();
+ data->model = 0;
+ }
+ persistent.indexes.clear();
+ }
+
+ /*!
+ \internal
+ clean the QPersistentModelIndex relative to the index if there is one.
+ To be used before an index is invalided
+ */
+ inline void invalidatePersistentIndex(const QModelIndex &index) {
+ QHash<QModelIndex, QPersistentModelIndexData *>::iterator it = persistent.indexes.find(index);
+ if(it != persistent.indexes.end()) {
+ QPersistentModelIndexData *data = *it;
+ persistent.indexes.erase(it);
+ data->index = QModelIndex();
+ data->model = 0;
+ }
+ }
+
+ struct Change {
+ Change() : first(-1), last(-1) {}
+ Change(const Change &c) : parent(c.parent), first(c.first), last(c.last), needsAdjust(c.needsAdjust) {}
+ Change(const QModelIndex &p, int f, int l) : parent(p), first(f), last(l), needsAdjust(false) {}
+ QModelIndex parent;
+ int first, last;
+
+
+ // In cases such as this:
+ // - A
+ // - B
+ // - C
+ // - - D
+ // - - E
+ // - - F
+ //
+ // If B is moved to above E, C is the source parent in the signal and its row is 2. When the move is
+ // completed however, C is at row 1 and there is no row 2 at the same level in the model at all.
+ // The QModelIndex is adjusted to correct that in those cases before reporting it though the
+ // rowsMoved signal.
+ bool needsAdjust;
+
+ bool isValid() { return first >= 0 && last >= 0; }
+ };
+ QStack<Change> changes;
+
+ struct Persistent {
+ Persistent() {}
+ QHash<QModelIndex, QPersistentModelIndexData *> indexes;
+ QStack<QVector<QPersistentModelIndexData *> > moved;
+ QStack<QVector<QPersistentModelIndexData *> > invalidated;
+ void insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data);
+ } persistent;
+
+ Qt::DropActions supportedDragActions;
+
+ QHash<int,QByteArray> roleNames;
+ static const QHash<int,QByteArray> &defaultRoleNames();
+};
+
+QT_END_NAMESPACE
+
+#endif // QABSTRACTITEMMODEL_P_H
diff --git a/src/corelib/kernel/qbasictimer.cpp b/src/corelib/kernel/qbasictimer.cpp
new file mode 100644
index 0000000000..6924848773
--- /dev/null
+++ b/src/corelib/kernel/qbasictimer.cpp
@@ -0,0 +1,138 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qbasictimer.h"
+#include "qcoreapplication.h"
+#include "qabstracteventdispatcher.h"
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \class QBasicTimer
+ \brief The QBasicTimer class provides timer events for objects.
+
+ \ingroup events
+
+ This is a fast, lightweight, and low-level class used by Qt
+ internally. We recommend using the higher-level QTimer class
+ rather than this class if you want to use timers in your
+ applications. Note that this timer is a repeating timer that
+ will send subsequent timer events unless the stop() function is called.
+
+ To use this class, create a QBasicTimer, and call its start()
+ function with a timeout interval and with a pointer to a QObject
+ subclass. When the timer times out it will send a timer event to
+ the QObject subclass. The timer can be stopped at any time using
+ stop(). isActive() returns true for a timer that is running;
+ i.e. it has been started, has not reached the timeout time, and
+ has not been stopped. The timer's ID can be retrieved using
+ timerId().
+
+ The \l{widgets/wiggly}{Wiggly} example uses QBasicTimer to repaint
+ a widget at regular intervals.
+
+ \sa QTimer, QTimerEvent, QObject::timerEvent(), Timers, {Wiggly Example}
+*/
+
+
+/*!
+ \fn QBasicTimer::QBasicTimer()
+
+ Contructs a basic timer.
+
+ \sa start()
+*/
+/*!
+ \fn QBasicTimer::~QBasicTimer()
+
+ Destroys the basic timer.
+*/
+
+/*!
+ \fn bool QBasicTimer::isActive() const
+
+ Returns true if the timer is running and has not been stopped; otherwise
+ returns false.
+
+ \sa start() stop()
+*/
+
+/*!
+ \fn int QBasicTimer::timerId() const
+
+ Returns the timer's ID.
+
+ \sa QTimerEvent::timerId()
+*/
+
+/*!
+ \fn void QBasicTimer::start(int msec, QObject *object)
+
+ Starts (or restarts) the timer with a \a msec milliseconds
+ timeout.
+
+ The given \a object will receive timer events.
+
+ \sa stop() isActive() QObject::timerEvent()
+ */
+void QBasicTimer::start(int msec, QObject *obj)
+{
+ stop();
+ if (obj)
+ id = obj->startTimer(msec);
+}
+
+/*!
+ Stops the timer.
+
+ \sa start() isActive()
+*/
+void QBasicTimer::stop()
+{
+ if (id) {
+ QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance();
+ if (eventDispatcher)
+ eventDispatcher->unregisterTimer(id);
+ }
+ id = 0;
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qbasictimer.h b/src/corelib/kernel/qbasictimer.h
new file mode 100644
index 0000000000..2b56624086
--- /dev/null
+++ b/src/corelib/kernel/qbasictimer.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QBASICTIMER_H
+#define QBASICTIMER_H
+
+#include <QtCore/qglobal.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Core)
+
+class QObject;
+
+class Q_CORE_EXPORT QBasicTimer
+{
+ int id;
+public:
+ inline QBasicTimer() : id(0) {}
+ inline ~QBasicTimer() { if (id) stop(); }
+
+ inline bool isActive() const { return id != 0; }
+ inline int timerId() const { return id; }
+
+ void start(int msec, QObject *obj);
+ void stop();
+};
+Q_DECLARE_TYPEINFO(QBasicTimer, Q_MOVABLE_TYPE);
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QBASICTIMER_H
diff --git a/src/corelib/kernel/qcore_mac.cpp b/src/corelib/kernel/qcore_mac.cpp
new file mode 100644
index 0000000000..7b3f35e308
--- /dev/null
+++ b/src/corelib/kernel/qcore_mac.cpp
@@ -0,0 +1,82 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <private/qcore_mac_p.h>
+#include <new>
+#include "qvarlengtharray.h"
+
+QT_BEGIN_NAMESPACE
+
+QString QCFString::toQString(CFStringRef str)
+{
+ if(!str)
+ return QString();
+ CFIndex length = CFStringGetLength(str);
+ const UniChar *chars = CFStringGetCharactersPtr(str);
+ if (chars)
+ return QString(reinterpret_cast<const QChar *>(chars), length);
+
+ QVarLengthArray<UniChar> buffer(length);
+ CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data());
+ return QString(reinterpret_cast<const QChar *>(buffer.constData()), length);
+}
+
+QCFString::operator QString() const
+{
+ if (string.isEmpty() && type)
+ const_cast<QCFString*>(this)->string = toQString(type);
+ return string;
+}
+
+CFStringRef QCFString::toCFStringRef(const QString &string)
+{
+ return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(string.unicode()),
+ string.length());
+}
+
+QCFString::operator CFStringRef() const
+{
+ if (!type)
+ const_cast<QCFString*>(this)->type = toCFStringRef(string);
+ return type;
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcore_mac_p.h b/src/corelib/kernel/qcore_mac_p.h
new file mode 100644
index 0000000000..f7f6bfe483
--- /dev/null
+++ b/src/corelib/kernel/qcore_mac_p.h
@@ -0,0 +1,163 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCORE_MAC_P_H
+#define QCORE_MAC_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of other Qt classes. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#ifndef __IMAGECAPTURE__
+# define __IMAGECAPTURE__
+#endif
+
+#undef OLD_DEBUG
+#ifdef DEBUG
+# define OLD_DEBUG DEBUG
+# undef DEBUG
+#endif
+#define DEBUG 0
+#ifdef qDebug
+# define old_qDebug qDebug
+# undef qDebug
+#endif
+
+#if defined(QT_BUILD_QMAKE) || defined(QT_BOOTSTRAPPED)
+#include <ApplicationServices/ApplicationServices.h>
+#else
+#include <CoreFoundation/CoreFoundation.h>
+#endif
+
+#ifndef QT_NO_CORESERVICES
+#include <CoreServices/CoreServices.h>
+#endif
+
+#undef DEBUG
+#ifdef OLD_DEBUG
+# define DEBUG OLD_DEBUG
+# undef OLD_DEBUG
+#endif
+
+#ifdef old_qDebug
+# undef qDebug
+# define qDebug QT_NO_QDEBUG_MACRO
+# undef old_qDebug
+#endif
+
+#include "qstring.h"
+
+QT_BEGIN_NAMESPACE
+
+/*
+ Helper class that automates refernce counting for CFtypes.
+ After constructing the QCFType object, it can be copied like a
+ value-based type.
+
+ Note that you must own the object you are wrapping.
+ This is typically the case if you get the object from a Core
+ Foundation function with the word "Create" or "Copy" in it. If
+ you got the object from a "Get" function, either retain it or use
+ constructFromGet(). One exception to this rule is the
+ HIThemeGet*Shape functions, which in reality are "Copy" functions.
+*/
+template <typename T>
+class Q_CORE_EXPORT QCFType
+{
+public:
+ inline QCFType(const T &t = 0) : type(t) {}
+ inline QCFType(const QCFType &helper) : type(helper.type) { if (type) CFRetain(type); }
+ inline ~QCFType() { if (type) CFRelease(type); }
+ inline operator T() { return type; }
+ inline QCFType operator =(const QCFType &helper)
+ {
+ if (helper.type)
+ CFRetain(helper.type);
+ CFTypeRef type2 = type;
+ type = helper.type;
+ if (type2)
+ CFRelease(type2);
+ return *this;
+ }
+ inline T *operator&() { return &type; }
+ static QCFType constructFromGet(const T &t)
+ {
+ CFRetain(t);
+ return QCFType<T>(t);
+ }
+protected:
+ T type;
+};
+
+class Q_CORE_EXPORT QCFString : public QCFType<CFStringRef>
+{
+public:
+ inline QCFString(const QString &str) : QCFType<CFStringRef>(0), string(str) {}
+ inline QCFString(const CFStringRef cfstr = 0) : QCFType<CFStringRef>(cfstr) {}
+ inline QCFString(const QCFType<CFStringRef> &other) : QCFType<CFStringRef>(other) {}
+ operator QString() const;
+ operator CFStringRef() const;
+ static QString toQString(CFStringRef cfstr);
+ static CFStringRef toCFStringRef(const QString &str);
+private:
+ QString string;
+};
+
+QT_END_NAMESPACE
+
+#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
+#ifndef __LP64__
+ typedef float CGFloat;
+ typedef int NSInteger;
+ typedef unsigned int NSUInteger;
+ #define SRefCon SInt32
+ #define URefCon UInt32
+#endif
+#endif
+
+#endif // QCORE_MAC_P_H
diff --git a/src/corelib/kernel/qcore_symbian_p.cpp b/src/corelib/kernel/qcore_symbian_p.cpp
new file mode 100644
index 0000000000..c717cb9f69
--- /dev/null
+++ b/src/corelib/kernel/qcore_symbian_p.cpp
@@ -0,0 +1,317 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <exception>
+#include <e32base.h>
+#include <e32uid.h>
+#include "qcore_symbian_p.h"
+#include <string>
+#include <in_sock.h>
+
+QT_BEGIN_NAMESPACE
+
+/*
+ Helper function for calling into Symbian classes that expect a TDes&.
+ This function converts a QString to a TDes by allocating memory that
+ must be deleted by the caller.
+*/
+
+Q_CORE_EXPORT HBufC* qt_QString2HBufC(const QString& aString)
+{
+ HBufC *buffer;
+#ifdef QT_NO_UNICODE
+ TPtrC8 ptr(reinterpret_cast<const TUint8*>(aString.toLocal8Bit().constData()));
+#else
+ TPtrC16 ptr(qt_QString2TPtrC(aString));
+#endif
+ buffer = HBufC::New(ptr.Length());
+ Q_CHECK_PTR(buffer);
+ buffer->Des().Copy(ptr);
+ return buffer;
+}
+
+Q_CORE_EXPORT QString qt_TDesC2QString(const TDesC& aDescriptor)
+{
+#ifdef QT_NO_UNICODE
+ return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
+#else
+ return QString(reinterpret_cast<const QChar *>(aDescriptor.Ptr()), aDescriptor.Length());
+#endif
+}
+
+QHBufC::QHBufC()
+ : m_hBufC(0)
+{
+}
+
+QHBufC::QHBufC(const QHBufC &src)
+ : m_hBufC(src.m_hBufC->Alloc())
+{
+ Q_CHECK_PTR(m_hBufC);
+}
+
+/*!
+ \internal
+ Constructs a QHBufC from an HBufC. Note that the QHBufC instance takes
+ ownership of the HBufC.
+*/
+QHBufC::QHBufC(HBufC *src)
+ : m_hBufC(src)
+{
+}
+
+QHBufC::QHBufC(const QString &src)
+{
+ m_hBufC = qt_QString2HBufC(src);
+}
+
+QHBufC::~QHBufC()
+{
+ if (m_hBufC)
+ delete m_hBufC;
+}
+
+class QS60PluginResolver
+{
+public:
+ QS60PluginResolver()
+ : initTried(false) {}
+
+ ~QS60PluginResolver() {
+ lib.Close();
+ }
+
+ TLibraryFunction resolve(int ordinal) {
+ if (!initTried) {
+ init();
+ initTried = true;
+ }
+
+ if (lib.Handle())
+ return lib.Lookup(ordinal);
+ else
+ return reinterpret_cast<TLibraryFunction>(NULL);
+ }
+
+private:
+ void init()
+ {
+ _LIT(KLibName_3_1, "qts60plugin_3_1" QT_LIBINFIX_UNICODE L".dll");
+ _LIT(KLibName_3_2, "qts60plugin_3_2" QT_LIBINFIX_UNICODE L".dll");
+ _LIT(KLibName_5_0, "qts60plugin_5_0" QT_LIBINFIX_UNICODE L".dll");
+
+ TPtrC libName;
+ TInt uidValue;
+ switch (QSysInfo::s60Version()) {
+ case QSysInfo::SV_S60_3_1:
+ libName.Set(KLibName_3_1);
+ uidValue = 0x2001E620;
+ break;
+ case QSysInfo::SV_S60_3_2:
+ libName.Set(KLibName_3_2);
+ uidValue = 0x2001E621;
+ break;
+ case QSysInfo::SV_S60_5_0: // Fall through to default
+ default:
+ // Default to 5.0 version, as any unknown platform is likely to be newer than that
+ libName.Set(KLibName_5_0);
+ uidValue = 0x2001E622;
+ break;
+ }
+
+ TUidType libUid(KDynamicLibraryUid, KSharedLibraryUid, TUid::Uid(uidValue));
+ lib.Load(libName, libUid);
+
+ // Duplicate lib handle to enable process wide access to it. Since Duplicate overwrites
+ // existing handle without closing it, store original for subsequent closing.
+ RLibrary origHandleCloser = lib;
+ lib.Duplicate(RThread(), EOwnerProcess);
+ origHandleCloser.Close();
+ }
+
+ RLibrary lib;
+ bool initTried;
+};
+
+Q_GLOBAL_STATIC(QS60PluginResolver, qt_s60_plugin_resolver);
+
+/*!
+ \internal
+ Resolves a platform version specific function from S60 plugin.
+ If plugin is missing or resolving fails for another reason, NULL is returned.
+*/
+Q_CORE_EXPORT TLibraryFunction qt_resolveS60PluginFunc(int ordinal)
+{
+ return qt_s60_plugin_resolver()->resolve(ordinal);
+}
+
+class QS60RFsSession
+{
+public:
+ QS60RFsSession() {
+ qt_symbian_throwIfError(iFs.Connect());
+ qt_symbian_throwIfError(iFs.ShareProtected());
+ }
+
+ ~QS60RFsSession() {
+ iFs.Close();
+ }
+
+ RFs& GetRFs() {
+ return iFs;
+ }
+
+private:
+
+ RFs iFs;
+};
+
+uint qHash(const RSubSessionBase& key)
+{
+ return qHash(key.SubSessionHandle());
+}
+
+Q_GLOBAL_STATIC(QS60RFsSession, qt_s60_RFsSession);
+
+Q_CORE_EXPORT RFs& qt_s60GetRFs()
+{
+ return qt_s60_RFsSession()->GetRFs();
+}
+
+QSymbianSocketManager::QSymbianSocketManager() :
+ iNextSocket(0), iDefaultConnection(0)
+{
+ TSessionPref preferences;
+ // ### In future this could be changed to KAfInet6 when that is more common than IPv4
+ preferences.iAddrFamily = KAfInet;
+ preferences.iProtocol = KProtocolInetIp;
+ //use global message pool, as we do not know how many sockets app will use
+ //TODO: is this the right choice?
+ qt_symbian_throwIfError(iSocketServ.Connect(preferences, -1));
+ qt_symbian_throwIfError(iSocketServ.ShareAuto());
+}
+
+QSymbianSocketManager::~QSymbianSocketManager()
+{
+ iSocketServ.Close();
+ if(!socketMap.isEmpty()) {
+ qWarning("leaked %d sockets on exit", socketMap.count());
+ }
+}
+
+RSocketServ& QSymbianSocketManager::getSocketServer() {
+ return iSocketServ;
+}
+
+int QSymbianSocketManager::addSocket(const RSocket& socket) {
+ QHashableSocket sock(static_cast<const QHashableSocket &>(socket));
+ QMutexLocker l(&iMutex);
+ Q_ASSERT(!socketMap.contains(sock));
+ if(socketMap.contains(sock))
+ return socketMap.value(sock);
+ // allocate socket number
+ int guard = 0;
+ while(reverseSocketMap.contains(iNextSocket)) {
+ iNextSocket++;
+ iNextSocket %= max_sockets;
+ guard++;
+ if(guard > max_sockets)
+ return -1;
+ }
+ int id = iNextSocket;
+
+ socketMap[sock] = id;
+ reverseSocketMap[id] = sock;
+ return id + socket_offset;
+}
+
+bool QSymbianSocketManager::removeSocket(const RSocket &socket) {
+ QHashableSocket sock(static_cast<const QHashableSocket &>(socket));
+ QMutexLocker l(&iMutex);
+ if(!socketMap.contains(sock))
+ return false;
+ int id = socketMap.value(sock);
+ socketMap.remove(sock);
+ reverseSocketMap.remove(id);
+ return true;
+}
+
+int QSymbianSocketManager::lookupSocket(const RSocket& socket) const {
+ QHashableSocket sock(static_cast<const QHashableSocket &>(socket));
+ QMutexLocker l(&iMutex);
+ if(!socketMap.contains(sock))
+ return -1;
+ int id = socketMap.value(sock);
+ return id + socket_offset;
+}
+
+bool QSymbianSocketManager::lookupSocket(int fd, RSocket& socket) const {
+ QMutexLocker l(&iMutex);
+ int id = fd - socket_offset;
+ if(!reverseSocketMap.contains(id))
+ return false;
+ socket = reverseSocketMap.value(id);
+ return true;
+}
+
+void QSymbianSocketManager::setDefaultConnection(RConnection* con)
+{
+ iDefaultConnection = con;
+}
+
+RConnection* QSymbianSocketManager::defaultConnection() const
+{
+ return iDefaultConnection;
+}
+
+Q_GLOBAL_STATIC(QSymbianSocketManager, qt_symbianSocketManager);
+
+QSymbianSocketManager& QSymbianSocketManager::instance()
+{
+ return *(qt_symbianSocketManager());
+}
+
+Q_CORE_EXPORT RSocketServ& qt_symbianGetSocketServer()
+{
+ return QSymbianSocketManager::instance().getSocketServer();
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcore_symbian_p.h b/src/corelib/kernel/qcore_symbian_p.h
new file mode 100644
index 0000000000..cbe84a8aa1
--- /dev/null
+++ b/src/corelib/kernel/qcore_symbian_p.h
@@ -0,0 +1,282 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCORE_SYMBIAN_P_H
+#define QCORE_SYMBIAN_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of other Qt classes. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <e32std.h>
+#include <QtCore/qglobal.h>
+#include <QtCore/qmutex.h>
+#include <qstring.h>
+#include <qrect.h>
+#include <qhash.h>
+#include <f32file.h>
+#include <es_sock.h>
+
+#define QT_LSTRING2(x) L##x
+#define QT_LSTRING(x) QT_LSTRING2(x)
+
+#if defined(QT_LIBINFIX)
+# define QT_LIBINFIX_UNICODE QT_LSTRING(QT_LIBINFIX)
+#else
+# define QT_LIBINFIX_UNICODE L""
+#endif
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+Q_CORE_EXPORT HBufC* qt_QString2HBufC(const QString& aString);
+
+Q_CORE_EXPORT QString qt_TDesC2QString(const TDesC& aDescriptor);
+inline QString qt_TDes2QString(const TDes& aDescriptor) { return qt_TDesC2QString(aDescriptor); }
+
+static inline QSize qt_TSize2QSize(const TSize& ts)
+{
+ return QSize(ts.iWidth, ts.iHeight);
+}
+
+static inline TSize qt_QSize2TSize(const QSize& qs)
+{
+ return TSize(qs.width(), qs.height());
+}
+
+static inline QRect qt_TRect2QRect(const TRect& tr)
+{
+ return QRect(tr.iTl.iX, tr.iTl.iY, tr.Width(), tr.Height());
+}
+
+static inline TRect qt_QRect2TRect(const QRect& qr)
+{
+ return TRect(TPoint(qr.left(), qr.top()), TSize(qr.width(), qr.height()));
+}
+
+// Returned TPtrC is valid as long as the given parameter is valid and unmodified
+static inline TPtrC qt_QString2TPtrC( const QString& string )
+{
+ return TPtrC16(static_cast<const TUint16*>(string.utf16()), string.length());
+}
+
+/*!
+ \internal
+ This class is a wrapper around the Symbian HBufC descriptor class.
+ It makes sure that the heap allocated HBufC class is freed when it is
+ destroyed.
+*/
+class Q_CORE_EXPORT QHBufC
+{
+public:
+ QHBufC();
+ QHBufC(const QHBufC &src);
+ QHBufC(HBufC *src);
+ QHBufC(const QString &src);
+ ~QHBufC();
+
+ inline operator HBufC *() { return m_hBufC; }
+ inline operator const HBufC *() const { return m_hBufC; }
+ inline HBufC *data() { return m_hBufC; }
+ inline const HBufC *data() const { return m_hBufC; }
+ inline HBufC & operator*() { return *m_hBufC; }
+ inline const HBufC & operator*() const { return *m_hBufC; }
+ inline HBufC * operator->() { return m_hBufC; }
+ inline const HBufC * operator->() const { return m_hBufC; }
+
+ inline bool operator==(const QHBufC &param) const { return data() == param.data(); }
+ inline bool operator!=(const QHBufC &param) const { return data() != param.data(); }
+
+private:
+ HBufC *m_hBufC;
+};
+
+inline uint qHash(TUid uid)
+{
+ return qHash(uid.iUid);
+}
+
+// S60 version specific function ordinals that can be resolved
+enum S60PluginFuncOrdinals
+{
+ S60Plugin_TimeFormatL = 1,
+ S60Plugin_GetTimeFormatSpec = 2,
+ S60Plugin_GetLongDateFormatSpec = 3,
+ S60Plugin_GetShortDateFormatSpec = 4,
+ S60Plugin_LocalizedDirectoryName = 5,
+ S60Plugin_GetSystemDrive = 6
+};
+
+Q_CORE_EXPORT TLibraryFunction qt_resolveS60PluginFunc(int ordinal);
+
+Q_CORE_EXPORT RFs& qt_s60GetRFs();
+Q_CORE_EXPORT RSocketServ& qt_symbianGetSocketServer();
+
+// Defined in qlocale_symbian.cpp.
+Q_CORE_EXPORT QByteArray qt_symbianLocaleName(int code);
+
+template <typename R>
+struct QScopedPointerRCloser
+{
+ static inline void cleanup(R *rPointer)
+ {
+ // Enforce a complete type.
+ // If you get a compile error here, read the section on forward declared
+ // classes in the QScopedPointer documentation.
+ typedef char IsIncompleteType[ sizeof(R) ? 1 : -1 ];
+ (void) sizeof(IsIncompleteType);
+
+ if (rPointer)
+ rPointer->Close();
+ }
+};
+
+//Wrapper for RSocket so it can be used as a key in QHash or QMap
+class QHashableSocket : public RSocket
+{
+public:
+ bool operator==(const QHashableSocket &other) const
+ {
+ return SubSessionHandle() == other.SubSessionHandle()
+ && Session().Handle() == other.Session().Handle();
+ }
+ bool operator<(const QHashableSocket &other) const
+ {
+ if (Session().Handle() == other.Session().Handle())
+ return SubSessionHandle() < other.SubSessionHandle();
+ return Session().Handle() < other.Session().Handle();
+ }
+};
+
+uint qHash(const RSubSessionBase& key);
+
+/*!
+ \internal
+ This class exists in QtCore for the benefit of QSocketNotifier, which uses integer
+ file descriptors in its public API.
+ So we need a way to map between int and RSocket.
+ Additionally, it is used to host the global RSocketServ session
+*/
+class Q_CORE_EXPORT QSymbianSocketManager
+{
+public:
+ QSymbianSocketManager();
+ ~QSymbianSocketManager();
+
+ /*!
+ \internal
+ \return handle to the socket server
+ */
+ RSocketServ& getSocketServer();
+ /*!
+ \internal
+ Adds a symbian socket to the global map
+ \param an open socket
+ \return pseudo file descriptor, -1 if out of resources
+ */
+ int addSocket(const RSocket &sock);
+ /*!
+ \internal
+ Removes a symbian socket from the global map
+ \param an open socket
+ \return true if the socket was in the map
+ */
+ bool removeSocket(const RSocket &sock);
+ /*!
+ \internal
+ Get pseudo file descriptor for a socket
+ \param an open socket
+ \return integer handle, or -1 if not in map
+ */
+ int lookupSocket(const RSocket &sock) const;
+ /*!
+ \internal
+ Get socket for a pseudo file descriptor
+ \param an open socket fd
+ \param sock (out) socket handle
+ \return true on success or false if not in map
+ */
+ bool lookupSocket(int fd, RSocket& sock) const;
+
+ /*!
+ \internal
+ Set the default connection to use for new sockets
+ \param an open connection
+ */
+ void setDefaultConnection(RConnection* con);
+ /*!
+ \internal
+ Get the default connection to use for new sockets
+ \return the connection, or null pointer if there is none set
+ */
+ RConnection *defaultConnection() const;
+
+ /*!
+ \internal
+ Gets a reference to the singleton socket manager
+ */
+ static QSymbianSocketManager& instance();
+private:
+ int allocateSocket();
+
+ const static int max_sockets = 0x20000; //covers all TCP and UDP ports, probably run out of memory first
+ const static int socket_offset = 0x40000000; //hacky way of separating sockets from file descriptors
+ int iNextSocket;
+ QHash<QHashableSocket, int> socketMap;
+ QHash<int, RSocket> reverseSocketMap;
+ mutable QMutex iMutex;
+ RSocketServ iSocketServ;
+ RConnection *iDefaultConnection;
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif //QCORE_SYMBIAN_P_H
diff --git a/src/corelib/kernel/qcore_unix.cpp b/src/corelib/kernel/qcore_unix.cpp
new file mode 100644
index 0000000000..834f05b208
--- /dev/null
+++ b/src/corelib/kernel/qcore_unix.cpp
@@ -0,0 +1,106 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcore_unix_p.h"
+#include "qelapsedtimer.h"
+
+#ifdef Q_OS_NACL
+#elif !defined (Q_OS_VXWORKS)
+# if !defined(Q_OS_HPUX) || defined(__ia64)
+# include <sys/select.h>
+# endif
+# include <sys/time.h>
+#else
+# include <selectLib.h>
+#endif
+
+#include <stdlib.h>
+
+#ifdef Q_OS_MAC
+#include <mach/mach_time.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+static inline bool time_update(struct timeval *tv, const struct timeval &start,
+ const struct timeval &timeout)
+{
+ if (!QElapsedTimer::isMonotonic()) {
+ // we cannot recalculate the timeout without a monotonic clock as the time may have changed
+ return false;
+ }
+
+ // clock source is monotonic, so we can recalculate how much timeout is left
+ struct timeval now = qt_gettime();
+ *tv = timeout + start - now;
+ return tv->tv_sec >= 0;
+}
+
+int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
+ const struct timeval *orig_timeout)
+{
+ if (!orig_timeout) {
+ // no timeout -> block forever
+ register int ret;
+ EINTR_LOOP(ret, select(nfds, fdread, fdwrite, fdexcept, 0));
+ return ret;
+ }
+
+ timeval start = qt_gettime();
+ timeval timeout = *orig_timeout;
+
+ // loop and recalculate the timeout as needed
+ int ret;
+ forever {
+ ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeout);
+ if (ret != -1 || errno != EINTR)
+ return ret;
+
+ // recalculate the timeout
+ if (!time_update(&timeout, start, *orig_timeout)) {
+ // timeout during update
+ // or clock reset, fake timeout error
+ return 0;
+ }
+ }
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcore_unix_p.h b/src/corelib/kernel/qcore_unix_p.h
new file mode 100644
index 0000000000..33df0daab4
--- /dev/null
+++ b/src/corelib/kernel/qcore_unix_p.h
@@ -0,0 +1,333 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCORE_UNIX_P_H
+#define QCORE_UNIX_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of Qt code on Unix. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "qplatformdefs.h"
+
+#ifndef Q_OS_UNIX
+# error "qcore_unix_p.h included on a non-Unix system"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#if defined(Q_OS_VXWORKS)
+# include <ioLib.h>
+#endif
+
+struct sockaddr;
+
+#if defined(Q_OS_LINUX) && defined(O_CLOEXEC)
+# define QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC 1
+QT_BEGIN_NAMESPACE
+namespace QtLibcSupplement {
+ inline int accept4(int, sockaddr *, QT_SOCKLEN_T *, int)
+ { errno = ENOSYS; return -1; }
+ inline int dup3(int, int, int)
+ { errno = ENOSYS; return -1; }
+ inline int pipe2(int [], int )
+ { errno = ENOSYS; return -1; }
+}
+QT_END_NAMESPACE
+using namespace QT_PREPEND_NAMESPACE(QtLibcSupplement);
+
+#else
+# define QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC 0
+#endif
+
+#define EINTR_LOOP(var, cmd) \
+ do { \
+ var = cmd; \
+ } while (var == -1 && errno == EINTR)
+
+QT_BEGIN_NAMESPACE
+
+// Internal operator functions for timevals
+inline timeval &normalizedTimeval(timeval &t)
+{
+ while (t.tv_usec > 1000000l) {
+ ++t.tv_sec;
+ t.tv_usec -= 1000000l;
+ }
+ while (t.tv_usec < 0l) {
+ --t.tv_sec;
+ t.tv_usec += 1000000l;
+ }
+ return t;
+}
+inline bool operator<(const timeval &t1, const timeval &t2)
+{ return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec); }
+inline bool operator==(const timeval &t1, const timeval &t2)
+{ return t1.tv_sec == t2.tv_sec && t1.tv_usec == t2.tv_usec; }
+inline timeval &operator+=(timeval &t1, const timeval &t2)
+{
+ t1.tv_sec += t2.tv_sec;
+ t1.tv_usec += t2.tv_usec;
+ return normalizedTimeval(t1);
+}
+inline timeval operator+(const timeval &t1, const timeval &t2)
+{
+ timeval tmp;
+ tmp.tv_sec = t1.tv_sec + t2.tv_sec;
+ tmp.tv_usec = t1.tv_usec + t2.tv_usec;
+ return normalizedTimeval(tmp);
+}
+inline timeval operator-(const timeval &t1, const timeval &t2)
+{
+ timeval tmp;
+ tmp.tv_sec = t1.tv_sec - (t2.tv_sec - 1);
+ tmp.tv_usec = t1.tv_usec - (t2.tv_usec + 1000000);
+ return normalizedTimeval(tmp);
+}
+inline timeval operator*(const timeval &t1, int mul)
+{
+ timeval tmp;
+ tmp.tv_sec = t1.tv_sec * mul;
+ tmp.tv_usec = t1.tv_usec * mul;
+ return normalizedTimeval(tmp);
+}
+
+// don't call QT_OPEN or ::open
+// call qt_safe_open
+static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 0777)
+{
+#ifdef O_CLOEXEC
+ flags |= O_CLOEXEC;
+#endif
+ register int fd;
+ EINTR_LOOP(fd, QT_OPEN(pathname, flags, mode));
+
+ // unknown flags are ignored, so we have no way of verifying if
+ // O_CLOEXEC was accepted
+ if (fd != -1)
+ ::fcntl(fd, F_SETFD, FD_CLOEXEC);
+ return fd;
+}
+#undef QT_OPEN
+#define QT_OPEN qt_safe_open
+
+#ifndef Q_OS_VXWORKS // no POSIX pipes in VxWorks
+// don't call ::pipe
+// call qt_safe_pipe
+static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
+{
+#ifdef O_CLOEXEC
+ Q_ASSERT((flags & ~(O_CLOEXEC | O_NONBLOCK)) == 0);
+#else
+ Q_ASSERT((flags & ~O_NONBLOCK) == 0);
+#endif
+
+ register int ret;
+#if QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC && defined(O_CLOEXEC)
+ // use pipe2
+ flags |= O_CLOEXEC;
+ ret = ::pipe2(pipefd, flags); // pipe2 is Linux-specific and is documented not to return EINTR
+ if (ret == 0 || errno != ENOSYS)
+ return ret;
+#endif
+
+ ret = ::pipe(pipefd);
+ if (ret == -1)
+ return -1;
+
+ ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
+ ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
+
+ // set non-block too?
+ if (flags & O_NONBLOCK) {
+ ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
+ ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
+ }
+
+ return 0;
+}
+
+#endif // Q_OS_VXWORKS
+
+// don't call dup or fcntl(F_DUPFD)
+static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC)
+{
+ Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
+
+ register int ret;
+#ifdef F_DUPFD_CLOEXEC
+ // use this fcntl
+ if (flags & FD_CLOEXEC) {
+ ret = ::fcntl(oldfd, F_DUPFD_CLOEXEC, atleast);
+ if (ret != -1 || errno != EINVAL)
+ return ret;
+ }
+#endif
+
+ // use F_DUPFD
+ ret = ::fcntl(oldfd, F_DUPFD, atleast);
+
+ if (flags && ret != -1)
+ ::fcntl(ret, F_SETFD, flags);
+ return ret;
+}
+
+// don't call dup2
+// call qt_safe_dup2
+static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
+{
+ Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
+
+ register int ret;
+#if QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC && defined(O_CLOEXEC)
+ // use dup3
+ if (flags & FD_CLOEXEC) {
+ EINTR_LOOP(ret, ::dup3(oldfd, newfd, O_CLOEXEC));
+ if (ret == 0 || errno != ENOSYS)
+ return ret;
+ }
+#endif
+ EINTR_LOOP(ret, ::dup2(oldfd, newfd));
+ if (ret == -1)
+ return -1;
+
+ if (flags)
+ ::fcntl(newfd, F_SETFD, flags);
+ return 0;
+}
+
+static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
+{
+ qint64 ret = 0;
+ EINTR_LOOP(ret, QT_READ(fd, data, maxlen));
+ return ret;
+}
+#undef QT_READ
+#define QT_READ qt_safe_read
+
+static inline qint64 qt_safe_write(int fd, const void *data, qint64 len)
+{
+ qint64 ret = 0;
+ EINTR_LOOP(ret, QT_WRITE(fd, data, len));
+ return ret;
+}
+#undef QT_WRITE
+#define QT_WRITE qt_safe_write
+
+static inline int qt_safe_close(int fd)
+{
+ register int ret;
+ EINTR_LOOP(ret, QT_CLOSE(fd));
+ return ret;
+}
+#undef QT_CLOSE
+#define QT_CLOSE qt_safe_close
+
+// - Open C does not (yet?) implement these on Symbian OS
+// - VxWorks doesn't have processes
+#if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_VXWORKS)
+static inline int qt_safe_execve(const char *filename, char *const argv[],
+ char *const envp[])
+{
+ register int ret;
+ EINTR_LOOP(ret, ::execve(filename, argv, envp));
+ return ret;
+}
+
+static inline int qt_safe_execv(const char *path, char *const argv[])
+{
+ register int ret;
+ EINTR_LOOP(ret, ::execv(path, argv));
+ return ret;
+}
+
+static inline int qt_safe_execvp(const char *file, char *const argv[])
+{
+ register int ret;
+ EINTR_LOOP(ret, ::execvp(file, argv));
+ return ret;
+}
+#endif
+
+#ifndef Q_OS_VXWORKS // no processes on VxWorks
+static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
+{
+ register int ret;
+ EINTR_LOOP(ret, ::waitpid(pid, status, options));
+ return ret;
+}
+
+#endif // Q_OS_VXWORKS
+
+#if !defined(_POSIX_MONOTONIC_CLOCK)
+# define _POSIX_MONOTONIC_CLOCK -1
+#endif
+
+timeval qt_gettime(); // in qelapsedtimer_mac.cpp or qtimestamp_unix.cpp
+
+Q_CORE_EXPORT int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
+ const struct timeval *tv);
+
+// according to X/OPEN we have to define semun ourselves
+// we use prefix as on some systems sem.h will have it
+struct semid_ds;
+union qt_semun {
+ int val; /* value for SETVAL */
+ struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
+ unsigned short *array; /* array for GETALL, SETALL */
+};
+
+QT_END_NAMESPACE
+
+#endif
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
new file mode 100644
index 0000000000..be86c58d47
--- /dev/null
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -0,0 +1,2730 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcoreapplication.h"
+#include "qcoreapplication_p.h"
+
+#include "qabstracteventdispatcher.h"
+#include "qcoreevent.h"
+#include "qeventloop.h"
+#include "qcorecmdlineargs_p.h"
+#include <qdatastream.h>
+#include <qdebug.h>
+#include <qdir.h>
+#include <qfile.h>
+#include <qfileinfo.h>
+#include <qhash.h>
+#include <private/qprocess_p.h>
+#include <qtextcodec.h>
+#include <qthread.h>
+#include <qthreadpool.h>
+#include <qthreadstorage.h>
+#include <private/qthread_p.h>
+#include <qelapsedtimer.h>
+#include <qlibraryinfo.h>
+#include <qvarlengtharray.h>
+#include <private/qfactoryloader_p.h>
+#include <private/qfunctions_p.h>
+#include <private/qlocale_p.h>
+
+#ifdef Q_OS_SYMBIAN
+# include <exception>
+# include <f32file.h>
+# include <e32ldr.h>
+# include "qeventdispatcher_symbian_p.h"
+# include "private/qcore_symbian_p.h"
+# include "private/qfilesystemengine_p.h"
+# include <apacmdln.h>
+#elif defined(Q_OS_UNIX)
+# if !defined(QT_NO_GLIB)
+# include "qeventdispatcher_glib_p.h"
+# endif
+# include "qeventdispatcher_unix_p.h"
+#endif
+
+#ifdef Q_OS_WIN
+# include "qeventdispatcher_win_p.h"
+#endif
+
+#ifdef Q_OS_MAC
+# include "qcore_mac_p.h"
+#endif
+
+#include <stdlib.h>
+
+#ifdef Q_OS_UNIX
+# include <locale.h>
+#endif
+
+#ifdef Q_OS_VXWORKS
+# include <taskLib.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+class QMutexUnlocker
+{
+public:
+ inline explicit QMutexUnlocker(QMutex *m)
+ : mtx(m)
+ { }
+ inline ~QMutexUnlocker() { unlock(); }
+ inline void unlock() { if (mtx) mtx->unlock(); mtx = 0; }
+
+private:
+ Q_DISABLE_COPY(QMutexUnlocker)
+
+ QMutex *mtx;
+};
+
+#ifdef Q_OS_SYMBIAN
+typedef TDriveNumber (*SystemDriveFunc)(RFs&);
+static SystemDriveFunc PtrGetSystemDrive = 0;
+static CApaCommandLine* apaCommandLine = 0;
+static char *apaTail = 0;
+static QVector<char *> *apaArgv = 0;
+
+static void qt_cleanup_apa_cmd_line()
+{
+ delete apaCommandLine;
+ apaCommandLine = 0;
+ delete apaArgv;
+ apaArgv = 0;
+ delete apaTail;
+ apaTail = 0;
+}
+
+static inline void qt_init_symbian_apa_arguments(int &argc, char **&argv)
+{
+ // If app is launched via CApaCommandLine::StartApp(), normal arguments only contain
+ // application name.
+ if (argc == 1) {
+ CApaCommandLine* commandLine = QCoreApplicationPrivate::symbianCommandLine();
+ if(commandLine) {
+ TPtrC8 apaCmdLine = commandLine->TailEnd();
+ int tailLen = apaCmdLine.Length();
+ if (tailLen) {
+ apaTail = reinterpret_cast<char *>(qMalloc(tailLen + 1));
+ qMemCopy(apaTail, reinterpret_cast<const char *>(apaCmdLine.Ptr()), tailLen);
+ apaTail[tailLen] = '\0';
+ apaArgv = new QVector<char *>(8);
+ // Reuse windows command line parsing
+ *apaArgv = qWinCmdLine<char>(apaTail, tailLen, argc);
+ apaArgv->insert(0, argv[0]);
+ argc++;
+ argv = apaArgv->data();
+ }
+ }
+ }
+}
+
+CApaCommandLine* QCoreApplicationPrivate::symbianCommandLine()
+{
+ // Getting of Apa command line needs to be static as it can only be called successfully
+ // once per process.
+ if (!apaCommandLine) {
+ TInt err = CApaCommandLine::GetCommandLineFromProcessEnvironment(apaCommandLine);
+ if (err == KErrNone) {
+ qAddPostRoutine(qt_cleanup_apa_cmd_line);
+ }
+ }
+ return apaCommandLine;
+}
+
+#endif
+
+#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
+extern QString qAppFileName();
+#endif
+
+int QCoreApplicationPrivate::app_compile_version = 0x040000; //we don't know exactly, but it's at least 4.0.0
+#if defined(QT3_SUPPORT)
+bool QCoreApplicationPrivate::useQt3Support = true;
+#endif
+
+#if !defined(Q_OS_WIN)
+#ifdef Q_OS_MAC
+QString QCoreApplicationPrivate::macMenuBarName()
+{
+ QString bundleName;
+ CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), CFSTR("CFBundleName"));
+ if (string)
+ bundleName = QCFString::toQString(static_cast<CFStringRef>(string));
+ return bundleName;
+}
+#endif
+QString QCoreApplicationPrivate::appName() const
+{
+ static QString applName;
+#ifdef Q_OS_MAC
+ applName = macMenuBarName();
+#endif
+ if (applName.isEmpty() && argv[0]) {
+ char *p = strrchr(argv[0], '/');
+ applName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
+ }
+ return applName;
+}
+#endif
+
+bool QCoreApplicationPrivate::checkInstance(const char *function)
+{
+ bool b = (QCoreApplication::self != 0);
+ if (!b)
+ qWarning("QApplication::%s: Please instantiate the QApplication object first", function);
+ return b;
+}
+
+// Support for introspection
+
+QSignalSpyCallbackSet Q_CORE_EXPORT qt_signal_spy_callback_set = { 0, 0, 0, 0 };
+
+void qt_register_signal_spy_callbacks(const QSignalSpyCallbackSet &callback_set)
+{
+ qt_signal_spy_callback_set = callback_set;
+}
+
+extern "C" void Q_CORE_EXPORT qt_startup_hook()
+{
+}
+
+typedef QList<QtCleanUpFunction> QVFuncList;
+Q_GLOBAL_STATIC(QVFuncList, postRList)
+
+void qAddPostRoutine(QtCleanUpFunction p)
+{
+ QVFuncList *list = postRList();
+ if (!list)
+ return;
+ list->prepend(p);
+}
+
+void qRemovePostRoutine(QtCleanUpFunction p)
+{
+ QVFuncList *list = postRList();
+ if (!list)
+ return;
+ list->removeAll(p);
+}
+
+void Q_CORE_EXPORT qt_call_post_routines()
+{
+ QVFuncList *list = 0;
+ QT_TRY {
+ list = postRList();
+ } QT_CATCH(const std::bad_alloc &) {
+ // ignore - if we can't allocate a post routine list,
+ // there's a high probability that there's no post
+ // routine to be executed :)
+ }
+ if (!list)
+ return;
+ while (!list->isEmpty())
+ (list->takeFirst())();
+}
+
+
+// app starting up if false
+bool QCoreApplicationPrivate::is_app_running = false;
+ // app closing down if true
+bool QCoreApplicationPrivate::is_app_closing = false;
+// initialized in qcoreapplication and in qtextstream autotest when setlocale is called.
+Q_CORE_EXPORT bool qt_locale_initialized = false;
+
+
+Q_CORE_EXPORT uint qGlobalPostedEventsCount()
+{
+ QThreadData *currentThreadData = QThreadData::current();
+ return currentThreadData->postEventList.size() - currentThreadData->postEventList.startOffset;
+}
+
+
+void qt_set_current_thread_to_main_thread()
+{
+ QCoreApplicationPrivate::theMainThread = QThread::currentThread();
+}
+
+
+
+QCoreApplication *QCoreApplication::self = 0;
+QAbstractEventDispatcher *QCoreApplicationPrivate::eventDispatcher = 0;
+uint QCoreApplicationPrivate::attribs;
+
+#ifdef Q_OS_UNIX
+Qt::HANDLE qt_application_thread_id = 0;
+#endif
+
+struct QCoreApplicationData {
+ QCoreApplicationData() {
+#ifndef QT_NO_LIBRARY
+ app_libpaths = 0;
+#endif
+ }
+ ~QCoreApplicationData() {
+#ifndef QT_NO_LIBRARY
+ delete app_libpaths;
+#endif
+
+ // cleanup the QAdoptedThread created for the main() thread
+ if (QCoreApplicationPrivate::theMainThread) {
+ QThreadData *data = QThreadData::get2(QCoreApplicationPrivate::theMainThread);
+ QCoreApplicationPrivate::theMainThread = 0;
+ data->deref(); // deletes the data and the adopted thread
+ }
+ }
+ QString orgName, orgDomain, application;
+ QString applicationVersion;
+
+#ifndef QT_NO_LIBRARY
+ QStringList *app_libpaths;
+#endif
+
+};
+
+Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
+
+QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv, uint flags)
+ : QObjectPrivate(), argc(aargc), argv(aargv), application_type(0), eventFilter(0),
+ in_exec(false), aboutToQuitEmitted(false)
+{
+ app_compile_version = flags & 0xffffff;
+#if defined(QT3_SUPPORT)
+ useQt3Support = !(flags & 0x01000000);
+#endif
+ static const char *const empty = "";
+ if (argc == 0 || argv == 0) {
+ argc = 0;
+ argv = (char **)&empty; // ouch! careful with QCoreApplication::argv()!
+ }
+ QCoreApplicationPrivate::is_app_closing = false;
+
+#ifdef Q_OS_SYMBIAN
+ qt_init_symbian_apa_arguments(argc, argv);
+#endif
+
+#ifdef Q_OS_UNIX
+ qt_application_thread_id = QThread::currentThreadId();
+#endif
+
+ // note: this call to QThread::currentThread() may end up setting theMainThread!
+ if (QThread::currentThread() != theMainThread)
+ qWarning("WARNING: QApplication was not created in the main() thread.");
+}
+
+QCoreApplicationPrivate::~QCoreApplicationPrivate()
+{
+ if (threadData) {
+#ifndef QT_NO_THREAD
+ void *data = &threadData->tls;
+ QThreadStorageData::finish((void **)data);
+#endif
+
+ // need to clear the state of the mainData, just in case a new QCoreApplication comes along.
+ QMutexLocker locker(&threadData->postEventList.mutex);
+ for (int i = 0; i < threadData->postEventList.size(); ++i) {
+ const QPostEvent &pe = threadData->postEventList.at(i);
+ if (pe.event) {
+ --pe.receiver->d_func()->postedEvents;
+ pe.event->posted = false;
+ delete pe.event;
+ }
+ }
+ threadData->postEventList.clear();
+ threadData->postEventList.recursion = 0;
+ threadData->quitNow = false;
+ }
+}
+
+void QCoreApplicationPrivate::createEventDispatcher()
+{
+ Q_Q(QCoreApplication);
+#if defined(Q_OS_SYMBIAN)
+ eventDispatcher = new QEventDispatcherSymbian(q);
+#elif defined(Q_OS_UNIX)
+# if !defined(QT_NO_GLIB)
+ if (qgetenv("QT_NO_GLIB").isEmpty() && QEventDispatcherGlib::versionSupported())
+ eventDispatcher = new QEventDispatcherGlib(q);
+ else
+# endif
+ eventDispatcher = new QEventDispatcherUNIX(q);
+#elif defined(Q_OS_WIN)
+ eventDispatcher = new QEventDispatcherWin32(q);
+#else
+# error "QEventDispatcher not yet ported to this platform"
+#endif
+}
+
+QThread *QCoreApplicationPrivate::theMainThread = 0;
+QThread *QCoreApplicationPrivate::mainThread()
+{
+ Q_ASSERT(theMainThread != 0);
+ return theMainThread;
+}
+
+#if !defined (QT_NO_DEBUG) || defined (QT_MAC_FRAMEWORK_BUILD)
+void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
+{
+ QThread *currentThread = QThread::currentThread();
+ QThread *thr = receiver->thread();
+ Q_ASSERT_X(currentThread == thr || !thr,
+ "QCoreApplication::sendEvent",
+ QString::fromLatin1("Cannot send events to objects owned by a different thread. "
+ "Current thread %1. Receiver '%2' (of type '%3') was created in thread %4")
+ .arg(QString::number((quintptr) currentThread, 16))
+ .arg(receiver->objectName())
+ .arg(QLatin1String(receiver->metaObject()->className()))
+ .arg(QString::number((quintptr) thr, 16))
+ .toLocal8Bit().data());
+ Q_UNUSED(currentThread);
+ Q_UNUSED(thr);
+}
+#elif defined(Q_OS_SYMBIAN) && defined (QT_NO_DEBUG)
+// no implementation in release builds, but keep the symbol present
+void QCoreApplicationPrivate::checkReceiverThread(QObject * /* receiver */)
+{
+}
+#endif
+
+void QCoreApplicationPrivate::appendApplicationPathToLibraryPaths()
+{
+#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
+ QStringList *app_libpaths = coreappdata()->app_libpaths;
+ Q_ASSERT(app_libpaths);
+# if defined(Q_OS_SYMBIAN)
+ QString app_location( QCoreApplication::applicationDirPath() );
+ // File existence check for application's private dir requires additional '\' or
+ // platform security will not allow it.
+ if (app_location != QLibraryInfo::location(QLibraryInfo::PluginsPath) && QFile::exists(app_location + QLatin1Char('\\')) && !app_libpaths->contains(app_location))
+# else
+ QString app_location( QCoreApplication::applicationFilePath() );
+ app_location.truncate(app_location.lastIndexOf(QLatin1Char('/')));
+ app_location = QDir(app_location).canonicalPath();
+ if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
+# endif
+ app_libpaths->append(app_location);
+#endif
+}
+
+QString qAppName()
+{
+ if (!QCoreApplicationPrivate::checkInstance("qAppName"))
+ return QString();
+ return QCoreApplication::instance()->d_func()->appName();
+}
+
+/*!
+ \class QCoreApplication
+ \brief The QCoreApplication class provides an event loop for console Qt
+ applications.
+
+ This class is used by non-GUI applications to provide their event
+ loop. For non-GUI application that uses Qt, there should be exactly
+ one QCoreApplication object. For GUI applications, see
+ QApplication.
+
+ QCoreApplication contains the main event loop, where all events
+ from the operating system (e.g., timer and network events) and
+ other sources are processed and dispatched. It also handles the
+ application's initialization and finalization, as well as
+ system-wide and application-wide settings.
+
+ \section1 The Event Loop and Event Handling
+
+ The event loop is started with a call to exec(). Long running
+ operations can call processEvents() to keep the application
+ responsive.
+
+ In general, we recommend that you create a QCoreApplication or a
+ QApplication object in your \c main() function as early as
+ possible. exec() will not return until the event loop exits; e.g.,
+ when quit() is called.
+
+ Several static convenience functions are also provided. The
+ QCoreApplication object is available from instance(). Events can
+ be sent or posted using sendEvent(), postEvent(), and
+ sendPostedEvents(). Pending events can be removed with
+ removePostedEvents() or flushed with flush().
+
+ The class provides a quit() slot and an aboutToQuit() signal.
+
+ \section1 Application and Library Paths
+
+ An application has an applicationDirPath() and an
+ applicationFilePath(). Library paths (see QLibrary) can be retrieved
+ with libraryPaths() and manipulated by setLibraryPaths(), addLibraryPath(),
+ and removeLibraryPath().
+
+ \section1 Internationalization and Translations
+
+ Translation files can be added or removed
+ using installTranslator() and removeTranslator(). Application
+ strings can be translated using translate(). The QObject::tr()
+ and QObject::trUtf8() functions are implemented in terms of
+ translate().
+
+ \section1 Accessing Command Line Arguments
+
+ The command line arguments which are passed to QCoreApplication's
+ constructor should be accessed using the arguments() function.
+ Note that some arguments supplied by the user may have been
+ processed and removed by QCoreApplication.
+
+ In cases where command line arguments need to be obtained using the
+ argv() function, you must convert them from the local string encoding
+ using QString::fromLocal8Bit().
+
+ \section1 Locale Settings
+
+ On Unix/Linux Qt is configured to use the system locale settings by
+ default. This can cause a conflict when using POSIX functions, for
+ instance, when converting between data types such as floats and
+ strings, since the notation may differ between locales. To get
+ around this problem, call the POSIX function \c{setlocale(LC_NUMERIC,"C")}
+ right after initializing QApplication or QCoreApplication to reset
+ the locale that is used for number formatting to "C"-locale.
+
+ \sa QApplication, QAbstractEventDispatcher, QEventLoop,
+ {Semaphores Example}, {Wait Conditions Example}
+*/
+
+/*!
+ \fn static QCoreApplication *QCoreApplication::instance()
+
+ Returns a pointer to the application's QCoreApplication (or
+ QApplication) instance.
+
+ If no instance has been allocated, \c null is returned.
+*/
+
+/*!\internal
+ */
+QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
+ : QObject(p, 0)
+{
+ init();
+ // note: it is the subclasses' job to call
+ // QCoreApplicationPrivate::eventDispatcher->startingUp();
+}
+
+/*!
+ Flushes the platform specific event queues.
+
+ If you are doing graphical changes inside a loop that does not
+ return to the event loop on asynchronous window systems like X11
+ or double buffered window systems like Mac OS X, and you want to
+ visualize these changes immediately (e.g. Splash Screens), call
+ this function.
+
+ \sa sendPostedEvents()
+*/
+void QCoreApplication::flush()
+{
+ if (self && self->d_func()->eventDispatcher)
+ self->d_func()->eventDispatcher->flush();
+}
+
+/*!
+ Constructs a Qt kernel application. Kernel applications are
+ applications without a graphical user interface. These type of
+ applications are used at the console or as server processes.
+
+ The \a argc and \a argv arguments are processed by the application,
+ and made available in a more convenient form by the arguments()
+ function.
+
+ \warning The data referred to by \a argc and \a argv must stay valid
+ for the entire lifetime of the QCoreApplication object. In addition,
+ \a argc must be greater than zero and \a argv must contain at least
+ one valid character string.
+*/
+QCoreApplication::QCoreApplication(int &argc, char **argv)
+ : QObject(*new QCoreApplicationPrivate(argc, argv, 0x040000))
+{
+ init();
+ QCoreApplicationPrivate::eventDispatcher->startingUp();
+#if defined(Q_OS_SYMBIAN) && !defined(QT_NO_LIBRARY)
+ // Refresh factoryloader, as text codecs are requested during lib path
+ // resolving process and won't be therefore properly loaded.
+ // Unknown if this is symbian specific issue.
+ QFactoryLoader::refreshAll();
+#endif
+
+#if defined(Q_OS_SYMBIAN) && !defined(QT_NO_SYSTEMLOCALE)
+ d_func()->symbianInit();
+#endif
+}
+
+QCoreApplication::QCoreApplication(int &argc, char **argv, int _internal)
+: QObject(*new QCoreApplicationPrivate(argc, argv, _internal))
+{
+ init();
+ QCoreApplicationPrivate::eventDispatcher->startingUp();
+#if defined(Q_OS_SYMBIAN)
+#ifndef QT_NO_LIBRARY
+ // Refresh factoryloader, as text codecs are requested during lib path
+ // resolving process and won't be therefore properly loaded.
+ // Unknown if this is symbian specific issue.
+ QFactoryLoader::refreshAll();
+#endif
+#ifndef QT_NO_SYSTEMLOCALE
+ d_func()->symbianInit();
+#endif
+#endif //Q_OS_SYMBIAN
+}
+
+
+// ### move to QCoreApplicationPrivate constructor?
+void QCoreApplication::init()
+{
+ Q_D(QCoreApplication);
+
+#ifdef Q_OS_UNIX
+ setlocale(LC_ALL, ""); // use correct char set mapping
+ qt_locale_initialized = true;
+#endif
+
+ Q_ASSERT_X(!self, "QCoreApplication", "there should be only one application object");
+ QCoreApplication::self = this;
+
+#ifdef Q_OS_SYMBIAN
+ //ensure temp and working directories exist
+ QFileSystemEngine::createDirectory(QFileSystemEntry(QFileSystemEngine::tempPath()), true);
+ QFileSystemEngine::createDirectory(QFileSystemEntry(QFileSystemEngine::currentPath()), true);
+#endif
+
+#ifndef QT_NO_THREAD
+ QThread::initialize();
+#endif
+
+ // use the event dispatcher created by the app programmer (if any)
+ if (!QCoreApplicationPrivate::eventDispatcher)
+ QCoreApplicationPrivate::eventDispatcher = d->threadData->eventDispatcher;
+ // otherwise we create one
+ if (!QCoreApplicationPrivate::eventDispatcher)
+ d->createEventDispatcher();
+ Q_ASSERT(QCoreApplicationPrivate::eventDispatcher != 0);
+
+ if (!QCoreApplicationPrivate::eventDispatcher->parent())
+ QCoreApplicationPrivate::eventDispatcher->moveToThread(d->threadData->thread);
+
+ d->threadData->eventDispatcher = QCoreApplicationPrivate::eventDispatcher;
+
+#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
+ if (!coreappdata()->app_libpaths) {
+ // make sure that library paths is initialized
+ libraryPaths();
+ } else {
+ d->appendApplicationPathToLibraryPaths();
+ }
+#endif
+
+#if defined(Q_OS_UNIX) && !(defined(QT_NO_PROCESS))
+ // Make sure the process manager thread object is created in the main
+ // thread.
+ QProcessPrivate::initializeProcessManager();
+#endif
+
+#ifdef QT_EVAL
+ extern void qt_core_eval_init(uint);
+ qt_core_eval_init(d->application_type);
+#endif
+
+#if defined(Q_OS_SYMBIAN) \
+ && defined(Q_CC_NOKIAX86) \
+ && defined(QT_DEBUG)
+ /**
+ * Prevent the executable from being locked in the Symbian emulator. The
+ * code dramatically simplifies debugging on Symbian, but beyond that has
+ * no impact.
+ *
+ * Force the ZLazyUnloadTimer to fire and therefore unload code segments
+ * immediately. The code affects Symbian's file server and on the other
+ * hand needs only to be run once in each emulator run.
+ */
+ {
+ RLoader loader;
+ CleanupClosePushL(loader);
+ User::LeaveIfError(loader.Connect());
+ User::LeaveIfError(loader.CancelLazyDllUnload());
+ CleanupStack::PopAndDestroy(&loader);
+ }
+#endif
+
+ qt_startup_hook();
+}
+
+#if defined(Q_OS_SYMBIAN) && !defined(QT_NO_SYSTEMLOCALE)
+void QCoreApplicationPrivate::symbianInit()
+{
+ if (!environmentChangeNotifier)
+ environmentChangeNotifier.reset(new QEnvironmentChangeNotifier);
+}
+#endif
+
+
+/*!
+ Destroys the QCoreApplication object.
+*/
+QCoreApplication::~QCoreApplication()
+{
+ qt_call_post_routines();
+
+ self = 0;
+ QCoreApplicationPrivate::is_app_closing = true;
+ QCoreApplicationPrivate::is_app_running = false;
+
+#if !defined(QT_NO_THREAD)
+#if !defined(QT_NO_CONCURRENT)
+ // Synchronize and stop the global thread pool threads.
+ QThreadPool *globalThreadPool = 0;
+ QT_TRY {
+ globalThreadPool = QThreadPool::globalInstance();
+ } QT_CATCH (...) {
+ // swallow the exception, since destructors shouldn't throw
+ }
+ if (globalThreadPool)
+ globalThreadPool->waitForDone();
+#endif
+ QThread::cleanup();
+#endif
+
+ d_func()->threadData->eventDispatcher = 0;
+ if (QCoreApplicationPrivate::eventDispatcher)
+ QCoreApplicationPrivate::eventDispatcher->closingDown();
+ QCoreApplicationPrivate::eventDispatcher = 0;
+
+#ifndef QT_NO_LIBRARY
+ delete coreappdata()->app_libpaths;
+ coreappdata()->app_libpaths = 0;
+#endif
+}
+
+
+/*!
+ Sets the attribute \a attribute if \a on is true;
+ otherwise clears the attribute.
+
+ One of the attributes that can be set with this method is
+ Qt::AA_ImmediateWidgetCreation. It tells Qt to create toplevel
+ windows immediately. Normally, resources for widgets are allocated
+ on demand to improve efficiency and minimize resource usage.
+ Therefore, if it is important to minimize resource consumption, do
+ not set this attribute.
+
+ \sa testAttribute()
+*/
+void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute, bool on)
+{
+ if (on)
+ QCoreApplicationPrivate::attribs |= 1 << attribute;
+ else
+ QCoreApplicationPrivate::attribs &= ~(1 << attribute);
+#ifdef Q_OS_MAC
+ // Turn on the no native menubar here, since we used to
+ // do this implicitly. We DO NOT flip it off if someone sets
+ // it to false.
+ // Ideally, we'd have magic that would be something along the lines of
+ // "follow MacPluginApplication" unless explicitly set.
+ // Considering this attribute isn't only at the beginning
+ // it's unlikely it will ever be a problem, but I want
+ // to have the behavior documented here.
+ if (attribute == Qt::AA_MacPluginApplication && on
+ && !testAttribute(Qt::AA_DontUseNativeMenuBar)) {
+ setAttribute(Qt::AA_DontUseNativeMenuBar, true);
+ }
+#endif
+}
+
+/*!
+ Returns true if attribute \a attribute is set;
+ otherwise returns false.
+
+ \sa setAttribute()
+ */
+bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
+{
+ return QCoreApplicationPrivate::testAttribute(attribute);
+}
+
+
+/*!
+ \internal
+
+ This function is here to make it possible for Qt extensions to
+ hook into event notification without subclassing QApplication
+*/
+bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event)
+{
+ // Make it possible for Qt Jambi and QSA to hook into events even
+ // though QApplication is subclassed...
+ bool result = false;
+ void *cbdata[] = { receiver, event, &result };
+ if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
+ return result;
+ }
+
+ // Qt enforces the rule that events can only be sent to objects in
+ // the current thread, so receiver->d_func()->threadData is
+ // equivalent to QThreadData::current(), just without the function
+ // call overhead.
+ QObjectPrivate *d = receiver->d_func();
+ QThreadData *threadData = d->threadData;
+ ++threadData->loopLevel;
+
+#ifdef QT_JAMBI_BUILD
+ int deleteWatch = 0;
+ int *oldDeleteWatch = QObjectPrivate::setDeleteWatch(d, &deleteWatch);
+
+ bool inEvent = d->inEventHandler;
+ d->inEventHandler = true;
+#endif
+
+ bool returnValue;
+ QT_TRY {
+ returnValue = notify(receiver, event);
+ } QT_CATCH (...) {
+ --threadData->loopLevel;
+ QT_RETHROW;
+ }
+
+#ifdef QT_JAMBI_BUILD
+ // Restore the previous state if the object was not deleted..
+ if (!deleteWatch) {
+ d->inEventHandler = inEvent;
+ }
+ QObjectPrivate::resetDeleteWatch(d, oldDeleteWatch, deleteWatch);
+#endif
+ --threadData->loopLevel;
+ return returnValue;
+}
+
+
+/*!
+ Sends \a event to \a receiver: \a {receiver}->event(\a event).
+ Returns the value that is returned from the receiver's event
+ handler. Note that this function is called for all events sent to
+ any object in any thread.
+
+ For certain types of events (e.g. mouse and key events),
+ the event will be propagated to the receiver's parent and so on up to
+ the top-level object if the receiver is not interested in the event
+ (i.e., it returns false).
+
+ There are five different ways that events can be processed;
+ reimplementing this virtual function is just one of them. All five
+ approaches are listed below:
+ \list 1
+ \i Reimplementing paintEvent(), mousePressEvent() and so
+ on. This is the commonest, easiest and least powerful way.
+
+ \i Reimplementing this function. This is very powerful, providing
+ complete control; but only one subclass can be active at a time.
+
+ \i Installing an event filter on QCoreApplication::instance(). Such
+ an event filter is able to process all events for all widgets, so
+ it's just as powerful as reimplementing notify(); furthermore, it's
+ possible to have more than one application-global event filter.
+ Global event filters even see mouse events for
+ \l{QWidget::isEnabled()}{disabled widgets}. Note that application
+ event filters are only called for objects that live in the main
+ thread.
+
+ \i Reimplementing QObject::event() (as QWidget does). If you do
+ this you get Tab key presses, and you get to see the events before
+ any widget-specific event filters.
+
+ \i Installing an event filter on the object. Such an event filter gets all
+ the events, including Tab and Shift+Tab key press events, as long as they
+ do not change the focus widget.
+ \endlist
+
+ \sa QObject::event(), installEventFilter()
+*/
+
+bool QCoreApplication::notify(QObject *receiver, QEvent *event)
+{
+ Q_D(QCoreApplication);
+ // no events are delivered after ~QCoreApplication() has started
+ if (QCoreApplicationPrivate::is_app_closing)
+ return true;
+
+ if (receiver == 0) { // serious error
+ qWarning("QCoreApplication::notify: Unexpected null receiver");
+ return true;
+ }
+
+#ifndef QT_NO_DEBUG
+ d->checkReceiverThread(receiver);
+#endif
+
+ return receiver->isWidgetType() ? false : d->notify_helper(receiver, event);
+}
+
+bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
+{
+ if (receiver->d_func()->threadData == this->threadData) {
+ // application event filters are only called for objects in the GUI thread
+ for (int i = 0; i < eventFilters.size(); ++i) {
+ register QObject *obj = eventFilters.at(i);
+ if (!obj)
+ continue;
+ if (obj->d_func()->threadData != threadData) {
+ qWarning("QCoreApplication: Application event filter cannot be in a different thread.");
+ continue;
+ }
+ if (obj->eventFilter(receiver, event))
+ return true;
+ }
+ }
+ return false;
+}
+
+bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
+{
+ Q_Q(QCoreApplication);
+ if (receiver != q) {
+ for (int i = 0; i < receiver->d_func()->eventFilters.size(); ++i) {
+ register QObject *obj = receiver->d_func()->eventFilters.at(i);
+ if (!obj)
+ continue;
+ if (obj->d_func()->threadData != receiver->d_func()->threadData) {
+ qWarning("QCoreApplication: Object event filter cannot be in a different thread.");
+ continue;
+ }
+ if (obj->eventFilter(receiver, event))
+ return true;
+ }
+ }
+ return false;
+}
+
+/*!\internal
+
+ Helper function called by notify()
+ */
+bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
+{
+ // send to all application event filters
+ if (sendThroughApplicationEventFilters(receiver, event))
+ return true;
+ // send to all receiver event filters
+ if (sendThroughObjectEventFilters(receiver, event))
+ return true;
+ // deliver the event
+ return receiver->event(event);
+}
+
+/*!
+ Returns true if an application object has not been created yet;
+ otherwise returns false.
+
+ \sa closingDown()
+*/
+
+bool QCoreApplication::startingUp()
+{
+ return !QCoreApplicationPrivate::is_app_running;
+}
+
+/*!
+ Returns true if the application objects are being destroyed;
+ otherwise returns false.
+
+ \sa startingUp()
+*/
+
+bool QCoreApplication::closingDown()
+{
+ return QCoreApplicationPrivate::is_app_closing;
+}
+
+
+/*!
+ Processes all pending events for the calling thread according to
+ the specified \a flags until there are no more events to process.
+
+ You can call this function occasionally when your program is busy
+ performing a long operation (e.g. copying a file).
+
+ In event you are running a local loop which calls this function
+ continuously, without an event loop, the
+ \l{QEvent::DeferredDelete}{DeferredDelete} events will
+ not be processed. This can affect the behaviour of widgets,
+ e.g. QToolTip, that rely on \l{QEvent::DeferredDelete}{DeferredDelete}
+ events to function properly. An alternative would be to call
+ \l{QCoreApplication::sendPostedEvents()}{sendPostedEvents()} from
+ within that local loop.
+
+ Calling this function processes events only for the calling thread.
+
+ \threadsafe
+
+ \sa exec(), QTimer, QEventLoop::processEvents(), flush(), sendPostedEvents()
+*/
+void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags)
+{
+ QThreadData *data = QThreadData::current();
+ if (!data->eventDispatcher)
+ return;
+ if (flags & QEventLoop::DeferredDeletion)
+ QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
+ data->eventDispatcher->processEvents(flags);
+}
+
+/*!
+ \overload processEvents()
+
+ Processes pending events for the calling thread for \a maxtime
+ milliseconds or until there are no more events to process,
+ whichever is shorter.
+
+ You can call this function occasionally when you program is busy
+ doing a long operation (e.g. copying a file).
+
+ Calling this function processes events only for the calling thread.
+
+ \threadsafe
+
+ \sa exec(), QTimer, QEventLoop::processEvents()
+*/
+void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime)
+{
+ QThreadData *data = QThreadData::current();
+ if (!data->eventDispatcher)
+ return;
+ QElapsedTimer start;
+ start.start();
+ if (flags & QEventLoop::DeferredDeletion)
+ QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
+ while (data->eventDispatcher->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) {
+ if (start.elapsed() > maxtime)
+ break;
+ if (flags & QEventLoop::DeferredDeletion)
+ QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
+ }
+}
+
+/*****************************************************************************
+ Main event loop wrappers
+ *****************************************************************************/
+
+/*!
+ Enters the main event loop and waits until exit() is called.
+ Returns the value that was set to exit() (which is 0 if exit() is
+ called via quit()).
+
+ It is necessary to call this function to start event handling. The
+ main event loop receives events from the window system and
+ dispatches these to the application widgets.
+
+ To make your application perform idle processing (i.e. executing a
+ special function whenever there are no pending events), use a
+ QTimer with 0 timeout. More advanced idle processing schemes can
+ be achieved using processEvents().
+
+ We recommend that you connect clean-up code to the
+ \l{QCoreApplication::}{aboutToQuit()} signal, instead of putting it in
+ your application's \c{main()} function because on some platforms the
+ QCoreApplication::exec() call may not return. For example, on Windows
+ when the user logs off, the system terminates the process after Qt
+ closes all top-level windows. Hence, there is no guarantee that the
+ application will have time to exit its event loop and execute code at
+ the end of the \c{main()} function after the QCoreApplication::exec()
+ call.
+
+ \sa quit(), exit(), processEvents(), QApplication::exec()
+*/
+int QCoreApplication::exec()
+{
+ if (!QCoreApplicationPrivate::checkInstance("exec"))
+ return -1;
+
+ QThreadData *threadData = self->d_func()->threadData;
+ if (threadData != QThreadData::current()) {
+ qWarning("%s::exec: Must be called from the main thread", self->metaObject()->className());
+ return -1;
+ }
+ if (!threadData->eventLoops.isEmpty()) {
+ qWarning("QCoreApplication::exec: The event loop is already running");
+ return -1;
+ }
+
+ threadData->quitNow = false;
+ QEventLoop eventLoop;
+ self->d_func()->in_exec = true;
+ self->d_func()->aboutToQuitEmitted = false;
+ int returnCode = eventLoop.exec();
+ threadData->quitNow = false;
+ if (self) {
+ self->d_func()->in_exec = false;
+ if (!self->d_func()->aboutToQuitEmitted)
+ emit self->aboutToQuit();
+ self->d_func()->aboutToQuitEmitted = true;
+ sendPostedEvents(0, QEvent::DeferredDelete);
+ }
+
+ return returnCode;
+}
+
+
+/*!
+ Tells the application to exit with a return code.
+
+ After this function has been called, the application leaves the
+ main event loop and returns from the call to exec(). The exec()
+ function returns \a returnCode. If the event loop is not running,
+ this function does nothing.
+
+ By convention, a \a returnCode of 0 means success, and any non-zero
+ value indicates an error.
+
+ Note that unlike the C library function of the same name, this
+ function \e does return to the caller -- it is event processing that
+ stops.
+
+ \sa quit(), exec()
+*/
+void QCoreApplication::exit(int returnCode)
+{
+ if (!self)
+ return;
+ QThreadData *data = self->d_func()->threadData;
+ data->quitNow = true;
+ for (int i = 0; i < data->eventLoops.size(); ++i) {
+ QEventLoop *eventLoop = data->eventLoops.at(i);
+ eventLoop->exit(returnCode);
+ }
+}
+
+/*****************************************************************************
+ QCoreApplication management of posted events
+ *****************************************************************************/
+
+/*!
+ \fn bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
+
+ Sends event \a event directly to receiver \a receiver, using the
+ notify() function. Returns the value that was returned from the
+ event handler.
+
+ The event is \e not deleted when the event has been sent. The normal
+ approach is to create the event on the stack, for example:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 0
+
+ \sa postEvent(), notify()
+*/
+
+/*!
+ Adds the event \a event, with the object \a receiver as the
+ receiver of the event, to an event queue and returns immediately.
+
+ The event must be allocated on the heap since the post event queue
+ will take ownership of the event and delete it once it has been
+ posted. It is \e {not safe} to access the event after
+ it has been posted.
+
+ When control returns to the main event loop, all events that are
+ stored in the queue will be sent using the notify() function.
+
+ Events are processed in the order posted. For more control over
+ the processing order, use the postEvent() overload below, which
+ takes a priority argument. This function posts all event with a
+ Qt::NormalEventPriority.
+
+ \threadsafe
+
+ \sa sendEvent(), notify(), sendPostedEvents()
+*/
+
+void QCoreApplication::postEvent(QObject *receiver, QEvent *event)
+{
+ postEvent(receiver, event, Qt::NormalEventPriority);
+}
+
+
+/*!
+ \overload postEvent()
+ \since 4.3
+
+ Adds the event \a event, with the object \a receiver as the
+ receiver of the event, to an event queue and returns immediately.
+
+ The event must be allocated on the heap since the post event queue
+ will take ownership of the event and delete it once it has been
+ posted. It is \e {not safe} to access the event after
+ it has been posted.
+
+ When control returns to the main event loop, all events that are
+ stored in the queue will be sent using the notify() function.
+
+ Events are sorted in descending \a priority order, i.e. events
+ with a high \a priority are queued before events with a lower \a
+ priority. The \a priority can be any integer value, i.e. between
+ INT_MAX and INT_MIN, inclusive; see Qt::EventPriority for more
+ details. Events with equal \a priority will be processed in the
+ order posted.
+
+ \threadsafe
+
+ \sa sendEvent(), notify(), sendPostedEvents(), Qt::EventPriority
+*/
+void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority)
+{
+ if (receiver == 0) {
+ qWarning("QCoreApplication::postEvent: Unexpected null receiver");
+ delete event;
+ return;
+ }
+
+ QThreadData * volatile * pdata = &receiver->d_func()->threadData;
+ QThreadData *data = *pdata;
+ if (!data) {
+ // posting during destruction? just delete the event to prevent a leak
+ delete event;
+ return;
+ }
+
+ // lock the post event mutex
+ data->postEventList.mutex.lock();
+
+ // if object has moved to another thread, follow it
+ while (data != *pdata) {
+ data->postEventList.mutex.unlock();
+
+ data = *pdata;
+ if (!data) {
+ // posting during destruction? just delete the event to prevent a leak
+ delete event;
+ return;
+ }
+
+ data->postEventList.mutex.lock();
+ }
+
+ QMutexUnlocker locker(&data->postEventList.mutex);
+
+ // if this is one of the compressible events, do compression
+ if (receiver->d_func()->postedEvents
+ && self && self->compressEvent(event, receiver, &data->postEventList)) {
+ return;
+ }
+
+ if (event->type() == QEvent::DeferredDelete && data == QThreadData::current()) {
+ // remember the current running eventloop for DeferredDelete
+ // events posted in the receiver's thread
+ event->d = reinterpret_cast<QEventPrivate *>(quintptr(data->loopLevel));
+ }
+
+ // delete the event on exceptions to protect against memory leaks till the event is
+ // properly owned in the postEventList
+ QScopedPointer<QEvent> eventDeleter(event);
+ if (data->postEventList.isEmpty() || data->postEventList.last().priority >= priority) {
+ // optimization: we can simply append if the last event in
+ // the queue has higher or equal priority
+ data->postEventList.append(QPostEvent(receiver, event, priority));
+ } else {
+ // insert event in descending priority order, using upper
+ // bound for a given priority (to ensure proper ordering
+ // of events with the same priority)
+ QPostEventList::iterator begin = data->postEventList.begin()
+ + data->postEventList.insertionOffset,
+ end = data->postEventList.end();
+ QPostEventList::iterator at = qUpperBound(begin, end, priority);
+ data->postEventList.insert(at, QPostEvent(receiver, event, priority));
+ }
+ eventDeleter.take();
+ event->posted = true;
+ ++receiver->d_func()->postedEvents;
+ data->canWait = false;
+ locker.unlock();
+
+ if (data->eventDispatcher)
+ data->eventDispatcher->wakeUp();
+}
+
+/*!
+ \internal
+ Returns true if \a event was compressed away (possibly deleted) and should not be added to the list.
+*/
+bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
+{
+#ifdef Q_WS_WIN
+ Q_ASSERT(event);
+ Q_ASSERT(receiver);
+ Q_ASSERT(postedEvents);
+
+ // compress posted timers to this object.
+ if (event->type() == QEvent::Timer && receiver->d_func()->postedEvents > 0) {
+ int timerId = ((QTimerEvent *) event)->timerId();
+ for (int i=0; i<postedEvents->size(); ++i) {
+ const QPostEvent &e = postedEvents->at(i);
+ if (e.receiver == receiver && e.event && e.event->type() == QEvent::Timer
+ && ((QTimerEvent *) e.event)->timerId() == timerId) {
+ delete event;
+ return true;
+ }
+ }
+ } else
+#endif
+ if ((event->type() == QEvent::DeferredDelete
+ || event->type() == QEvent::Quit)
+ && receiver->d_func()->postedEvents > 0) {
+ for (int i = 0; i < postedEvents->size(); ++i) {
+ const QPostEvent &cur = postedEvents->at(i);
+ if (cur.receiver != receiver
+ || cur.event == 0
+ || cur.event->type() != event->type())
+ continue;
+ // found an event for this receiver
+ delete event;
+ return true;
+ }
+ }
+ return false;
+}
+
+/*!
+ \fn void QCoreApplication::sendPostedEvents()
+ \overload sendPostedEvents()
+
+ Dispatches all posted events, i.e. empties the event queue.
+*/
+
+/*!
+ Immediately dispatches all events which have been previously queued
+ with QCoreApplication::postEvent() and which are for the object \a receiver
+ and have the event type \a event_type.
+
+ Events from the window system are \e not dispatched by this
+ function, but by processEvents().
+
+ If \a receiver is null, the events of \a event_type are sent for all
+ objects. If \a event_type is 0, all the events are sent for \a receiver.
+
+ \note This method must be called from the same thread as its QObject parameter, \a receiver.
+
+ \sa flush(), postEvent()
+*/
+
+void QCoreApplication::sendPostedEvents(QObject *receiver, int event_type)
+{
+ QThreadData *data = QThreadData::current();
+
+ QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
+}
+
+void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type,
+ QThreadData *data)
+{
+ if (event_type == -1) {
+ // we were called by an obsolete event dispatcher.
+ event_type = 0;
+ }
+
+ if (receiver && receiver->d_func()->threadData != data) {
+ qWarning("QCoreApplication::sendPostedEvents: Cannot send "
+ "posted events for objects in another thread");
+ return;
+ }
+
+ ++data->postEventList.recursion;
+
+#ifdef QT3_SUPPORT
+ if (event_type == QEvent::ChildInserted) {
+ if (receiver) {
+ // optimize sendPostedEvents(w, QEvent::ChildInserted) calls away
+ receiver->d_func()->sendPendingChildInsertedEvents();
+ --data->postEventList.recursion;
+ return;
+ }
+
+ // ChildInserted events are sent in response to *Request
+ event_type = QEvent::ChildInsertedRequest;
+ }
+#endif
+
+ QMutexLocker locker(&data->postEventList.mutex);
+
+ // by default, we assume that the event dispatcher can go to sleep after
+ // processing all events. if any new events are posted while we send
+ // events, canWait will be set to false.
+ data->canWait = (data->postEventList.size() == 0);
+
+ if (data->postEventList.size() == 0 || (receiver && !receiver->d_func()->postedEvents)) {
+ --data->postEventList.recursion;
+ return;
+ }
+
+ data->canWait = true;
+
+ // okay. here is the tricky loop. be careful about optimizing
+ // this, it looks the way it does for good reasons.
+ int startOffset = data->postEventList.startOffset;
+ int &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
+ data->postEventList.insertionOffset = data->postEventList.size();
+
+ while (i < data->postEventList.size()) {
+ // avoid live-lock
+ if (i >= data->postEventList.insertionOffset)
+ break;
+
+ const QPostEvent &pe = data->postEventList.at(i);
+ ++i;
+
+ if (!pe.event)
+ continue;
+ if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
+ data->canWait = false;
+ continue;
+ }
+
+ if (pe.event->type() == QEvent::DeferredDelete) {
+ // DeferredDelete events are only sent when we are explicitly asked to
+ // (s.a. QEvent::DeferredDelete), and then only if the event loop that
+ // posted the event has returned.
+ const bool allowDeferredDelete =
+ (quintptr(pe.event->d) > unsigned(data->loopLevel)
+ || (!quintptr(pe.event->d) && data->loopLevel > 0)
+ || (event_type == QEvent::DeferredDelete
+ && quintptr(pe.event->d) == unsigned(data->loopLevel)));
+ if (!allowDeferredDelete) {
+ // cannot send deferred delete
+ if (!event_type && !receiver) {
+ // don't lose the event
+ data->postEventList.append(pe);
+ const_cast<QPostEvent &>(pe).event = 0;
+ }
+ continue;
+ }
+ }
+
+ // first, we diddle the event so that we can deliver
+ // it, and that no one will try to touch it later.
+ pe.event->posted = false;
+ QEvent * e = pe.event;
+ QObject * r = pe.receiver;
+
+ --r->d_func()->postedEvents;
+ Q_ASSERT(r->d_func()->postedEvents >= 0);
+
+ // next, update the data structure so that we're ready
+ // for the next event.
+ const_cast<QPostEvent &>(pe).event = 0;
+
+ locker.unlock();
+ // after all that work, it's time to deliver the event.
+#ifdef QT_NO_EXCEPTIONS
+ QCoreApplication::sendEvent(r, e);
+#else
+ try {
+ QCoreApplication::sendEvent(r, e);
+ } catch (...) {
+ delete e;
+ locker.relock();
+
+ // since we were interrupted, we need another pass to make sure we clean everything up
+ data->canWait = false;
+
+ // uglehack: copied from below
+ --data->postEventList.recursion;
+ if (!data->postEventList.recursion && !data->canWait && data->eventDispatcher)
+ data->eventDispatcher->wakeUp();
+ throw; // rethrow
+ }
+#endif
+
+ delete e;
+ locker.relock();
+
+ // careful when adding anything below this point - the
+ // sendEvent() call might invalidate any invariants this
+ // function depends on.
+ }
+
+ --data->postEventList.recursion;
+ if (!data->postEventList.recursion && !data->canWait && data->eventDispatcher)
+ data->eventDispatcher->wakeUp();
+
+ // clear the global list, i.e. remove everything that was
+ // delivered.
+ if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
+ const QPostEventList::iterator it = data->postEventList.begin();
+ data->postEventList.erase(it, it + data->postEventList.startOffset);
+ data->postEventList.insertionOffset -= data->postEventList.startOffset;
+ Q_ASSERT(data->postEventList.insertionOffset >= 0);
+ data->postEventList.startOffset = 0;
+ }
+}
+
+/*!
+ Removes all events posted using postEvent() for \a receiver.
+
+ The events are \e not dispatched, instead they are removed from the
+ queue. You should never need to call this function. If you do call it,
+ be aware that killing events may cause \a receiver to break one or
+ more invariants.
+
+ \threadsafe
+*/
+
+void QCoreApplication::removePostedEvents(QObject *receiver)
+{
+ removePostedEvents(receiver, 0);
+}
+
+/*!
+ \overload removePostedEvents()
+ \since 4.3
+
+ Removes all events of the given \a eventType that were posted
+ using postEvent() for \a receiver.
+
+ The events are \e not dispatched, instead they are removed from
+ the queue. You should never need to call this function. If you do
+ call it, be aware that killing events may cause \a receiver to
+ break one or more invariants.
+
+ If \a receiver is null, the events of \a eventType are removed for
+ all objects. If \a eventType is 0, all the events are removed for
+ \a receiver.
+
+ \threadsafe
+*/
+
+void QCoreApplication::removePostedEvents(QObject *receiver, int eventType)
+{
+#ifdef QT3_SUPPORT
+ if (eventType == QEvent::ChildInserted)
+ eventType = QEvent::ChildInsertedRequest;
+#endif
+
+ QThreadData *data = receiver ? receiver->d_func()->threadData : QThreadData::current();
+ QMutexLocker locker(&data->postEventList.mutex);
+
+ // the QObject destructor calls this function directly. this can
+ // happen while the event loop is in the middle of posting events,
+ // and when we get here, we may not have any more posted events
+ // for this object.
+ if (receiver && !receiver->d_func()->postedEvents)
+ return;
+
+ //we will collect all the posted events for the QObject
+ //and we'll delete after the mutex was unlocked
+ QVarLengthArray<QEvent*> events;
+ int n = data->postEventList.size();
+ int j = 0;
+
+ for (int i = 0; i < n; ++i) {
+ const QPostEvent &pe = data->postEventList.at(i);
+
+ if ((!receiver || pe.receiver == receiver)
+ && (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
+ --pe.receiver->d_func()->postedEvents;
+#ifdef QT3_SUPPORT
+ if (pe.event->type() == QEvent::ChildInsertedRequest)
+ pe.receiver->d_func()->pendingChildInsertedEvents.clear();
+#endif
+ pe.event->posted = false;
+ events.append(pe.event);
+ const_cast<QPostEvent &>(pe).event = 0;
+ } else if (!data->postEventList.recursion) {
+ if (i != j)
+ data->postEventList.swap(i, j);
+ ++j;
+ }
+ }
+
+#ifdef QT_DEBUG
+ if (receiver && eventType == 0) {
+ Q_ASSERT(!receiver->d_func()->postedEvents);
+ }
+#endif
+
+ if (!data->postEventList.recursion) {
+ // truncate list
+ data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
+ }
+
+ locker.unlock();
+ for (int i = 0; i < events.count(); ++i) {
+ delete events[i];
+ }
+}
+
+/*!
+ Removes \a event from the queue of posted events, and emits a
+ warning message if appropriate.
+
+ \warning This function can be \e really slow. Avoid using it, if
+ possible.
+
+ \threadsafe
+*/
+
+void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
+{
+ if (!event || !event->posted)
+ return;
+
+ QThreadData *data = QThreadData::current();
+
+ QMutexLocker locker(&data->postEventList.mutex);
+
+ if (data->postEventList.size() == 0) {
+#if defined(QT_DEBUG)
+ qDebug("QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
+ (void*)event, event->type());
+ return;
+#endif
+ }
+
+ for (int i = 0; i < data->postEventList.size(); ++i) {
+ const QPostEvent & pe = data->postEventList.at(i);
+ if (pe.event == event) {
+#ifndef QT_NO_DEBUG
+ qWarning("QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
+ event->type(),
+ pe.receiver->metaObject()->className(),
+ pe.receiver->objectName().toLocal8Bit().data());
+#endif
+ --pe.receiver->d_func()->postedEvents;
+ pe.event->posted = false;
+ delete pe.event;
+ const_cast<QPostEvent &>(pe).event = 0;
+ return;
+ }
+ }
+}
+
+/*!\reimp
+
+*/
+bool QCoreApplication::event(QEvent *e)
+{
+ if (e->type() == QEvent::Quit) {
+ quit();
+ return true;
+ }
+ return QObject::event(e);
+}
+
+/*! \enum QCoreApplication::Encoding
+
+ This enum type defines the 8-bit encoding of character string
+ arguments to translate():
+
+ \value CodecForTr The encoding specified by
+ QTextCodec::codecForTr() (Latin-1 if none has
+ been set).
+ \value UnicodeUTF8 UTF-8.
+ \value DefaultCodec (Obsolete) Use CodecForTr instead.
+
+ \sa QObject::tr(), QObject::trUtf8(), QString::fromUtf8()
+*/
+
+/*!
+ Tells the application to exit with return code 0 (success).
+ Equivalent to calling QCoreApplication::exit(0).
+
+ It's common to connect the QApplication::lastWindowClosed() signal
+ to quit(), and you also often connect e.g. QAbstractButton::clicked() or
+ signals in QAction, QMenu, or QMenuBar to it.
+
+ Example:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 1
+
+ \sa exit(), aboutToQuit(), QApplication::lastWindowClosed()
+*/
+
+void QCoreApplication::quit()
+{
+ exit(0);
+}
+
+/*!
+ \fn void QCoreApplication::aboutToQuit()
+
+ This signal is emitted when the application is about to quit the
+ main event loop, e.g. when the event loop level drops to zero.
+ This may happen either after a call to quit() from inside the
+ application or when the users shuts down the entire desktop session.
+
+ The signal is particularly useful if your application has to do some
+ last-second cleanup. Note that no user interaction is possible in
+ this state.
+
+ \sa quit()
+*/
+
+#ifndef QT_NO_TRANSLATION
+/*!
+ Adds the translation file \a translationFile to the list of
+ translation files to be used for translations.
+
+ Multiple translation files can be installed. Translations are
+ searched for in the reverse order in which they were installed,
+ so the most recently installed translation file is searched first
+ and the first translation file installed is searched last.
+ The search stops as soon as a translation containing a matching
+ string is found.
+
+ Installing or removing a QTranslator, or changing an installed QTranslator
+ generates a \l{QEvent::LanguageChange}{LanguageChange} event for the
+ QCoreApplication instance. A QApplication instance will propagate the event
+ to all toplevel windows, where a reimplementation of changeEvent can
+ re-translate the user interface by passing user-visible strings via the
+ tr() function to the respective property setters. User-interface classes
+ generated by \l{Qt Designer} provide a \c retranslateUi() function that can be
+ called.
+
+ \sa removeTranslator() translate() QTranslator::load() {Dynamic Translation}
+*/
+
+void QCoreApplication::installTranslator(QTranslator *translationFile)
+{
+ if (!translationFile)
+ return;
+
+ if (!QCoreApplicationPrivate::checkInstance("installTranslator"))
+ return;
+ QCoreApplicationPrivate *d = self->d_func();
+ d->translators.prepend(translationFile);
+
+#ifndef QT_NO_TRANSLATION_BUILDER
+ if (translationFile->isEmpty())
+ return;
+#endif
+
+ QEvent ev(QEvent::LanguageChange);
+ QCoreApplication::sendEvent(self, &ev);
+}
+
+/*!
+ Removes the translation file \a translationFile from the list of
+ translation files used by this application. (It does not delete the
+ translation file from the file system.)
+
+ \sa installTranslator() translate(), QObject::tr()
+*/
+
+void QCoreApplication::removeTranslator(QTranslator *translationFile)
+{
+ if (!translationFile)
+ return;
+ if (!QCoreApplicationPrivate::checkInstance("removeTranslator"))
+ return;
+ QCoreApplicationPrivate *d = self->d_func();
+ if (d->translators.removeAll(translationFile) && !self->closingDown()) {
+ QEvent ev(QEvent::LanguageChange);
+ QCoreApplication::sendEvent(self, &ev);
+ }
+}
+
+/*!
+ \overload translate()
+*/
+QString QCoreApplication::translate(const char *context, const char *sourceText,
+ const char *disambiguation, Encoding encoding)
+{
+ return translate(context, sourceText, disambiguation, encoding, -1);
+}
+
+static void replacePercentN(QString *result, int n)
+{
+ if (n >= 0) {
+ int percentPos = 0;
+ int len = 0;
+ while ((percentPos = result->indexOf(QLatin1Char('%'), percentPos + len)) != -1) {
+ len = 1;
+ QString fmt;
+ if (result->at(percentPos + len) == QLatin1Char('L')) {
+ ++len;
+ fmt = QLatin1String("%L1");
+ } else {
+ fmt = QLatin1String("%1");
+ }
+ if (result->at(percentPos + len) == QLatin1Char('n')) {
+ fmt = fmt.arg(n);
+ ++len;
+ result->replace(percentPos, len, fmt);
+ len = fmt.length();
+ }
+ }
+ }
+}
+
+/*!
+ \reentrant
+ \since 4.5
+
+ Returns the translation text for \a sourceText, by querying the
+ installed translation files. The translation files are searched
+ from the most recently installed file back to the first
+ installed file.
+
+ QObject::tr() and QObject::trUtf8() provide this functionality
+ more conveniently.
+
+ \a context is typically a class name (e.g., "MyDialog") and \a
+ sourceText is either English text or a short identifying text.
+
+ \a disambiguation is an identifying string, for when the same \a
+ sourceText is used in different roles within the same context. By
+ default, it is null.
+
+ See the \l QTranslator and \l QObject::tr() documentation for
+ more information about contexts, disambiguations and comments.
+
+ \a encoding indicates the 8-bit encoding of character strings.
+
+ \a n is used in conjunction with \c %n to support plural forms.
+ See QObject::tr() for details.
+
+ If none of the translation files contain a translation for \a
+ sourceText in \a context, this function returns a QString
+ equivalent of \a sourceText. The encoding of \a sourceText is
+ specified by \e encoding; it defaults to CodecForTr.
+
+ This function is not virtual. You can use alternative translation
+ techniques by subclassing \l QTranslator.
+
+ \warning This method is reentrant only if all translators are
+ installed \e before calling this method. Installing or removing
+ translators while performing translations is not supported. Doing
+ so will most likely result in crashes or other undesirable
+ behavior.
+
+ \sa QObject::tr() installTranslator() QTextCodec::codecForTr()
+*/
+
+
+QString QCoreApplication::translate(const char *context, const char *sourceText,
+ const char *disambiguation, Encoding encoding, int n)
+{
+ QString result;
+
+ if (!sourceText)
+ return result;
+
+ if (self && !self->d_func()->translators.isEmpty()) {
+ QList<QTranslator*>::ConstIterator it;
+ QTranslator *translationFile;
+ for (it = self->d_func()->translators.constBegin(); it != self->d_func()->translators.constEnd(); ++it) {
+ translationFile = *it;
+ result = translationFile->translate(context, sourceText, disambiguation, n);
+ if (!result.isEmpty())
+ break;
+ }
+ }
+
+ if (result.isEmpty()) {
+#ifdef QT_NO_TEXTCODEC
+ Q_UNUSED(encoding)
+#else
+ if (encoding == UnicodeUTF8)
+ result = QString::fromUtf8(sourceText);
+ else if (QTextCodec::codecForTr() != 0)
+ result = QTextCodec::codecForTr()->toUnicode(sourceText);
+ else
+#endif
+ result = QString::fromLatin1(sourceText);
+ }
+
+ replacePercentN(&result, n);
+ return result;
+}
+
+// Declared in qglobal.h
+QString qtTrId(const char *id, int n)
+{
+ return QCoreApplication::translate(0, id, 0, QCoreApplication::UnicodeUTF8, n);
+}
+
+bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
+{
+ return QCoreApplication::self
+ && QCoreApplication::self->d_func()->translators.contains(translator);
+}
+
+#endif //QT_NO_TRANSLATE
+
+/*!
+ Returns the directory that contains the application executable.
+
+ For example, if you have installed Qt in the \c{C:\Trolltech\Qt}
+ directory, and you run the \c{regexp} example, this function will
+ return "C:/Trolltech/Qt/examples/tools/regexp".
+
+ On Mac OS X this will point to the directory actually containing the
+ executable, which may be inside of an application bundle (if the
+ application is bundled).
+
+ \warning On Linux, this function will try to get the path from the
+ \c {/proc} file system. If that fails, it assumes that \c
+ {argv[0]} contains the absolute file name of the executable. The
+ function also assumes that the current directory has not been
+ changed by the application.
+
+ In Symbian this function will return the application private directory,
+ not the path to executable itself, as those are always in \c {/sys/bin}.
+ If the application is in a read only drive, i.e. ROM, then the private path
+ on the system drive will be returned.
+
+ \sa applicationFilePath()
+*/
+QString QCoreApplication::applicationDirPath()
+{
+ if (!self) {
+ qWarning("QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
+ return QString();
+ }
+
+ QCoreApplicationPrivate *d = self->d_func();
+ if (d->cachedApplicationDirPath.isNull())
+#if defined(Q_OS_SYMBIAN)
+ {
+ QString appPath;
+ RFs& fs = qt_s60GetRFs();
+ TChar driveChar;
+ QChar qDriveChar;
+ driveChar = (RProcess().FileName())[0];
+
+ //Check if the process is installed in a read only drive (typically ROM),
+ //and use the system drive (typically C:) if so.
+ TInt drive;
+ TDriveInfo driveInfo;
+ TInt err = fs.CharToDrive(driveChar, drive);
+ if (err == KErrNone) {
+ err = fs.Drive(driveInfo, drive);
+ }
+ if (err != KErrNone || (driveInfo.iDriveAtt & KDriveAttRom) || (driveInfo.iMediaAtt
+ & KMediaAttWriteProtected)) {
+ if(!PtrGetSystemDrive)
+ PtrGetSystemDrive = reinterpret_cast<SystemDriveFunc>(qt_resolveS60PluginFunc(S60Plugin_GetSystemDrive));
+ Q_ASSERT(PtrGetSystemDrive);
+ drive = PtrGetSystemDrive(fs);
+ fs.DriveToChar(drive, driveChar);
+ }
+
+ qDriveChar = QChar(QLatin1Char(driveChar)).toUpper();
+
+ TFileName privatePath;
+ fs.PrivatePath(privatePath);
+ appPath = qt_TDesC2QString(privatePath);
+ appPath.prepend(QLatin1Char(':')).prepend(qDriveChar);
+
+ // Create the appPath if it doesn't exist. Non-existing appPath will cause
+ // Platform Security violations later on if the app doesn't have AllFiles capability.
+ err = fs.CreatePrivatePath(drive);
+ if (err != KErrNone)
+ qWarning("QCoreApplication::applicationDirPath: Failed to create private path.");
+
+ d->cachedApplicationDirPath = QFileInfo(appPath).path();
+ }
+#else
+ d->cachedApplicationDirPath = QFileInfo(applicationFilePath()).path();
+#endif
+ return d->cachedApplicationDirPath;
+}
+
+/*!
+ Returns the file path of the application executable.
+
+ For example, if you have installed Qt in the \c{/usr/local/qt}
+ directory, and you run the \c{regexp} example, this function will
+ return "/usr/local/qt/examples/tools/regexp/regexp".
+
+ \warning On Linux, this function will try to get the path from the
+ \c {/proc} file system. If that fails, it assumes that \c
+ {argv[0]} contains the absolute file name of the executable. The
+ function also assumes that the current directory has not been
+ changed by the application.
+
+ \sa applicationDirPath()
+*/
+QString QCoreApplication::applicationFilePath()
+{
+ if (!self) {
+ qWarning("QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
+ return QString();
+ }
+
+ QCoreApplicationPrivate *d = self->d_func();
+ if (!d->cachedApplicationFilePath.isNull())
+ return d->cachedApplicationFilePath;
+
+#if defined(Q_WS_WIN)
+ d->cachedApplicationFilePath = QFileInfo(qAppFileName()).filePath();
+ return d->cachedApplicationFilePath;
+#elif defined(Q_WS_MAC)
+ QString qAppFileName_str = qAppFileName();
+ if(!qAppFileName_str.isEmpty()) {
+ QFileInfo fi(qAppFileName_str);
+ d->cachedApplicationFilePath = fi.exists() ? fi.canonicalFilePath() : QString();
+ return d->cachedApplicationFilePath;
+ }
+#endif
+#if defined(Q_OS_SYMBIAN)
+ QString appPath;
+ RProcess proc;
+ TInt err = proc.Open(proc.Id());
+ if (err == KErrNone) {
+ TFileName procName = proc.FileName();
+ appPath.append(QString(reinterpret_cast<const QChar*>(procName.Ptr()), procName.Length()));
+ proc.Close();
+ }
+
+ d->cachedApplicationFilePath = appPath;
+ return d->cachedApplicationFilePath;
+
+#elif defined( Q_OS_UNIX )
+# ifdef Q_OS_LINUX
+ // Try looking for a /proc/<pid>/exe symlink first which points to
+ // the absolute path of the executable
+ QFileInfo pfi(QString::fromLatin1("/proc/%1/exe").arg(getpid()));
+ if (pfi.exists() && pfi.isSymLink()) {
+ d->cachedApplicationFilePath = pfi.canonicalFilePath();
+ return d->cachedApplicationFilePath;
+ }
+# endif
+
+ QString argv0 = QFile::decodeName(QByteArray(argv()[0]));
+ QString absPath;
+
+ if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) {
+ /*
+ If argv0 starts with a slash, it is already an absolute
+ file path.
+ */
+ absPath = argv0;
+ } else if (argv0.contains(QLatin1Char('/'))) {
+ /*
+ If argv0 contains one or more slashes, it is a file path
+ relative to the current directory.
+ */
+ absPath = QDir::current().absoluteFilePath(argv0);
+ } else {
+ /*
+ Otherwise, the file path has to be determined using the
+ PATH environment variable.
+ */
+ QByteArray pEnv = qgetenv("PATH");
+ QDir currentDir = QDir::current();
+ QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1Char(':'));
+ for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) {
+ if ((*p).isEmpty())
+ continue;
+ QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0);
+ QFileInfo candidate_fi(candidate);
+ if (candidate_fi.exists() && !candidate_fi.isDir()) {
+ absPath = candidate;
+ break;
+ }
+ }
+ }
+
+ absPath = QDir::cleanPath(absPath);
+
+ QFileInfo fi(absPath);
+ d->cachedApplicationFilePath = fi.exists() ? fi.canonicalFilePath() : QString();
+ return d->cachedApplicationFilePath;
+#endif
+}
+
+/*!
+ \since 4.4
+
+ Returns the current process ID for the application.
+*/
+qint64 QCoreApplication::applicationPid()
+{
+#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
+ return GetCurrentProcessId();
+#elif defined(Q_OS_VXWORKS)
+ return (pid_t) taskIdCurrent;
+#else
+ return getpid();
+#endif
+}
+
+/*!
+ \obsolete
+
+ Use arguments().size() instead.
+*/
+int QCoreApplication::argc()
+{
+ if (!self) {
+ qWarning("QCoreApplication::argc: Please instantiate the QApplication object first");
+ return 0;
+ }
+ return self->d_func()->argc;
+}
+
+
+/*!
+ \obsolete
+
+ Use arguments() instead.
+*/
+char **QCoreApplication::argv()
+{
+ if (!self) {
+ qWarning("QCoreApplication::argv: Please instantiate the QApplication object first");
+ return 0;
+ }
+ return self->d_func()->argv;
+}
+
+/*!
+ \since 4.1
+
+ Returns the list of command-line arguments.
+
+ Usually arguments().at(0) is the program name, arguments().at(1)
+ is the first argument, and arguments().last() is the last
+ argument. See the note below about Windows.
+
+ Calling this function is slow - you should store the result in a variable
+ when parsing the command line.
+
+ \warning On Unix, this list is built from the argc and argv parameters passed
+ to the constructor in the main() function. The string-data in argv is
+ interpreted using QString::fromLocal8Bit(); hence it is not possible to
+ pass, for example, Japanese command line arguments on a system that runs in a
+ Latin1 locale. Most modern Unix systems do not have this limitation, as they are
+ Unicode-based.
+
+ On NT-based Windows, this limitation does not apply either.
+ On Windows, the arguments() are not built from the contents of argv/argc, as
+ the content does not support Unicode. Instead, the arguments() are constructed
+ from the return value of
+ \l{http://msdn2.microsoft.com/en-us/library/ms683156(VS.85).aspx}{GetCommandLine()}.
+ As a result of this, the string given by arguments().at(0) might not be
+ the program name on Windows, depending on how the application was started.
+
+ For Symbian applications started with \c RApaLsSession::StartApp one can specify
+ arguments using \c CApaCommandLine::SetTailEndL function. Such arguments are only
+ available via this method; they will not be passed to \c main function. Also note
+ that only 8-bit string data set with \c CApaCommandLine::SetTailEndL is supported
+ by this function.
+
+ \sa applicationFilePath()
+*/
+
+QStringList QCoreApplication::arguments()
+{
+ QStringList list;
+
+ if (!self) {
+ qWarning("QCoreApplication::arguments: Please instantiate the QApplication object first");
+ return list;
+ }
+#ifdef Q_OS_WIN
+ QString cmdline = QString::fromWCharArray(GetCommandLine());
+
+#if defined(Q_OS_WINCE)
+ wchar_t tempFilename[MAX_PATH+1];
+ if (GetModuleFileName(0, tempFilename, MAX_PATH)) {
+ tempFilename[MAX_PATH] = 0;
+ cmdline.prepend(QLatin1Char('\"') + QString::fromWCharArray(tempFilename) + QLatin1String("\" "));
+ }
+#endif // Q_OS_WINCE
+
+ list = qWinCmdArgs(cmdline);
+ if (self->d_func()->application_type) { // GUI app? Skip known - see qapplication.cpp
+ QStringList stripped;
+ for (int a = 0; a < list.count(); ++a) {
+ QString arg = list.at(a);
+ QByteArray l1arg = arg.toLatin1();
+ if (l1arg == "-qdevel" ||
+ l1arg == "-qdebug" ||
+ l1arg == "-reverse" ||
+ l1arg == "-stylesheet" ||
+ l1arg == "-widgetcount")
+ ;
+ else if (l1arg.startsWith("-style=") ||
+ l1arg.startsWith("-qmljsdebugger="))
+ ;
+ else if (l1arg == "-style" ||
+ l1arg == "-session" ||
+ l1arg == "-graphicssystem" ||
+ l1arg == "-testability")
+ ++a;
+ else
+ stripped += arg;
+ }
+ list = stripped;
+ }
+#else
+ const int ac = self->d_func()->argc;
+ char ** const av = self->d_func()->argv;
+ for (int a = 0; a < ac; ++a) {
+ list << QString::fromLocal8Bit(av[a]);
+ }
+#endif
+
+ return list;
+}
+
+/*!
+ \property QCoreApplication::organizationName
+ \brief the name of the organization that wrote this application
+
+ The value is used by the QSettings class when it is constructed
+ using the empty constructor. This saves having to repeat this
+ information each time a QSettings object is created.
+
+ On Mac, QSettings uses organizationDomain() as the organization
+ if it's not an empty string; otherwise it uses
+ organizationName(). On all other platforms, QSettings uses
+ organizationName() as the organization.
+
+ \sa organizationDomain applicationName
+*/
+
+void QCoreApplication::setOrganizationName(const QString &orgName)
+{
+ coreappdata()->orgName = orgName;
+}
+
+QString QCoreApplication::organizationName()
+{
+ return coreappdata()->orgName;
+}
+
+/*!
+ \property QCoreApplication::organizationDomain
+ \brief the Internet domain of the organization that wrote this application
+
+ The value is used by the QSettings class when it is constructed
+ using the empty constructor. This saves having to repeat this
+ information each time a QSettings object is created.
+
+ On Mac, QSettings uses organizationDomain() as the organization
+ if it's not an empty string; otherwise it uses organizationName().
+ On all other platforms, QSettings uses organizationName() as the
+ organization.
+
+ \sa organizationName applicationName applicationVersion
+*/
+void QCoreApplication::setOrganizationDomain(const QString &orgDomain)
+{
+ coreappdata()->orgDomain = orgDomain;
+}
+
+QString QCoreApplication::organizationDomain()
+{
+ return coreappdata()->orgDomain;
+}
+
+/*!
+ \property QCoreApplication::applicationName
+ \brief the name of this application
+
+ The value is used by the QSettings class when it is constructed
+ using the empty constructor. This saves having to repeat this
+ information each time a QSettings object is created.
+
+ \sa organizationName organizationDomain applicationVersion
+*/
+void QCoreApplication::setApplicationName(const QString &application)
+{
+ coreappdata()->application = application;
+}
+
+QString QCoreApplication::applicationName()
+{
+ return coreappdata()->application;
+}
+
+/*!
+ \property QCoreApplication::applicationVersion
+ \since 4.4
+ \brief the version of this application
+
+ \sa applicationName organizationName organizationDomain
+*/
+void QCoreApplication::setApplicationVersion(const QString &version)
+{
+ coreappdata()->applicationVersion = version;
+}
+
+QString QCoreApplication::applicationVersion()
+{
+ return coreappdata()->applicationVersion;
+}
+
+#ifndef QT_NO_LIBRARY
+
+Q_GLOBAL_STATIC_WITH_ARGS(QMutex, libraryPathMutex, (QMutex::Recursive))
+
+/*!
+ Returns a list of paths that the application will search when
+ dynamically loading libraries.
+
+ Qt provides default library paths, but they can also be set using
+ a \l{Using qt.conf}{qt.conf} file. Paths specified in this file
+ will override default values.
+
+ This list will include the installation directory for plugins if
+ it exists (the default installation directory for plugins is \c
+ INSTALL/plugins, where \c INSTALL is the directory where Qt was
+ installed). The directory of the application executable (NOT the
+ working directory) is always added, as well as the colon separated
+ entries of the QT_PLUGIN_PATH environment variable.
+
+ If you want to iterate over the list, you can use the \l foreach
+ pseudo-keyword:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 2
+
+ \sa setLibraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary,
+ {How to Create Qt Plugins}
+*/
+QStringList QCoreApplication::libraryPaths()
+{
+ QMutexLocker locker(libraryPathMutex());
+ if (!coreappdata()->app_libpaths) {
+ QStringList *app_libpaths = coreappdata()->app_libpaths = new QStringList;
+ QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath);
+#if defined(Q_OS_SYMBIAN)
+ // Add existing path on all drives for relative PluginsPath in Symbian
+ if (installPathPlugins.at(1) != QChar(QLatin1Char(':'))) {
+ QString tempPath = installPathPlugins;
+ if (tempPath.at(tempPath.length() - 1) != QDir::separator()) {
+ tempPath += QDir::separator();
+ }
+ RFs& fs = qt_s60GetRFs();
+ TPtrC tempPathPtr(reinterpret_cast<const TText*> (tempPath.constData()));
+ TFindFile finder(fs);
+ TInt err = finder.FindByDir(tempPathPtr, tempPathPtr);
+ while (err == KErrNone) {
+ QString foundDir(reinterpret_cast<const QChar *>(finder.File().Ptr()),
+ finder.File().Length());
+ foundDir = QDir(foundDir).canonicalPath();
+ if (!app_libpaths->contains(foundDir))
+ app_libpaths->append(foundDir);
+ err = finder.Find();
+ }
+ }
+#else
+ if (QFile::exists(installPathPlugins)) {
+ // Make sure we convert from backslashes to slashes.
+ installPathPlugins = QDir(installPathPlugins).canonicalPath();
+ if (!app_libpaths->contains(installPathPlugins))
+ app_libpaths->append(installPathPlugins);
+ }
+#endif
+
+ // If QCoreApplication is not yet instantiated,
+ // make sure we add the application path when we construct the QCoreApplication
+ if (self) self->d_func()->appendApplicationPathToLibraryPaths();
+
+ const QByteArray libPathEnv = qgetenv("QT_PLUGIN_PATH");
+ if (!libPathEnv.isEmpty()) {
+#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
+ QLatin1Char pathSep(';');
+#else
+ QLatin1Char pathSep(':');
+#endif
+ QStringList paths = QString::fromLatin1(libPathEnv).split(pathSep, QString::SkipEmptyParts);
+ for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
+ QString canonicalPath = QDir(*it).canonicalPath();
+ if (!canonicalPath.isEmpty()
+ && !app_libpaths->contains(canonicalPath)) {
+ app_libpaths->append(canonicalPath);
+ }
+ }
+ }
+ }
+ return *(coreappdata()->app_libpaths);
+}
+
+
+
+/*!
+
+ Sets the list of directories to search when loading libraries to
+ \a paths. All existing paths will be deleted and the path list
+ will consist of the paths given in \a paths.
+
+ In Symbian this function is only useful for setting paths for
+ finding Qt extension plugin stubs, since the OS can only
+ load libraries from the \c{/sys/bin} directory.
+
+ \sa libraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary
+ */
+void QCoreApplication::setLibraryPaths(const QStringList &paths)
+{
+ QMutexLocker locker(libraryPathMutex());
+ if (!coreappdata()->app_libpaths)
+ coreappdata()->app_libpaths = new QStringList;
+ *(coreappdata()->app_libpaths) = paths;
+ locker.unlock();
+ QFactoryLoader::refreshAll();
+}
+
+/*!
+ Prepends \a path to the beginning of the library path list, ensuring that
+ it is searched for libraries first. If \a path is empty or already in the
+ path list, the path list is not changed.
+
+ The default path list consists of a single entry, the installation
+ directory for plugins. The default installation directory for plugins
+ is \c INSTALL/plugins, where \c INSTALL is the directory where Qt was
+ installed.
+
+ In Symbian this function is only useful for adding paths for
+ finding Qt extension plugin stubs, since the OS can only
+ load libraries from the \c{/sys/bin} directory.
+
+ \sa removeLibraryPath(), libraryPaths(), setLibraryPaths()
+ */
+void QCoreApplication::addLibraryPath(const QString &path)
+{
+ if (path.isEmpty())
+ return;
+
+ QMutexLocker locker(libraryPathMutex());
+
+ // make sure that library paths is initialized
+ libraryPaths();
+
+ QString canonicalPath = QDir(path).canonicalPath();
+ if (!canonicalPath.isEmpty()
+ && !coreappdata()->app_libpaths->contains(canonicalPath)) {
+ coreappdata()->app_libpaths->prepend(canonicalPath);
+ locker.unlock();
+ QFactoryLoader::refreshAll();
+ }
+}
+
+/*!
+ Removes \a path from the library path list. If \a path is empty or not
+ in the path list, the list is not changed.
+
+ \sa addLibraryPath(), libraryPaths(), setLibraryPaths()
+*/
+void QCoreApplication::removeLibraryPath(const QString &path)
+{
+ if (path.isEmpty())
+ return;
+
+ QMutexLocker locker(libraryPathMutex());
+
+ // make sure that library paths is initialized
+ libraryPaths();
+
+ QString canonicalPath = QDir(path).canonicalPath();
+ coreappdata()->app_libpaths->removeAll(canonicalPath);
+ QFactoryLoader::refreshAll();
+}
+
+#endif //QT_NO_LIBRARY
+
+/*!
+ \typedef QCoreApplication::EventFilter
+
+ A function with the following signature that can be used as an
+ event filter:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 3
+
+ \sa setEventFilter()
+*/
+
+/*!
+ \fn EventFilter QCoreApplication::setEventFilter(EventFilter filter)
+
+ Replaces the event filter function for the QCoreApplication with
+ \a filter and returns the pointer to the replaced event filter
+ function. Only the current event filter function is called. If you
+ want to use both filter functions, save the replaced EventFilter
+ in a place where yours can call it.
+
+ The event filter function set here is called for all messages
+ received by all threads meant for all Qt objects. It is \e not
+ called for messages that are not meant for Qt objects.
+
+ The event filter function should return true if the message should
+ be filtered, (i.e. stopped). It should return false to allow
+ processing the message to continue.
+
+ By default, no event filter function is set (i.e., this function
+ returns a null EventFilter the first time it is called).
+
+ \note The filter function set here receives native messages,
+ i.e. MSG or XEvent structs, that are going to Qt objects. It is
+ called by QCoreApplication::filterEvent(). If the filter function
+ returns false to indicate the message should be processed further,
+ the native message can then be translated into a QEvent and
+ handled by the standard Qt \l{QEvent} {event} filering, e.g.
+ QObject::installEventFilter().
+
+ \note The filter function set here is different form the filter
+ function set via QAbstractEventDispatcher::setEventFilter(), which
+ gets all messages received by its thread, even messages meant for
+ objects that are not handled by Qt.
+
+ \sa QObject::installEventFilter(), QAbstractEventDispatcher::setEventFilter()
+*/
+QCoreApplication::EventFilter
+QCoreApplication::setEventFilter(QCoreApplication::EventFilter filter)
+{
+ Q_D(QCoreApplication);
+ EventFilter old = d->eventFilter;
+ d->eventFilter = filter;
+ return old;
+}
+
+/*!
+ Sends \a message through the event filter that was set by
+ setEventFilter(). If no event filter has been set, this function
+ returns false; otherwise, this function returns the result of the
+ event filter function in the \a result parameter.
+
+ \sa setEventFilter()
+*/
+bool QCoreApplication::filterEvent(void *message, long *result)
+{
+ Q_D(QCoreApplication);
+ if (result)
+ *result = 0;
+ if (d->eventFilter)
+ return d->eventFilter(message, result);
+#ifdef Q_OS_WIN
+ return winEventFilter(reinterpret_cast<MSG *>(message), result);
+#else
+ return false;
+#endif
+}
+
+/*!
+ This function returns true if there are pending events; otherwise
+ returns false. Pending events can be either from the window
+ system or posted events using postEvent().
+
+ \sa QAbstractEventDispatcher::hasPendingEvents()
+*/
+bool QCoreApplication::hasPendingEvents()
+{
+ QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance();
+ if (eventDispatcher)
+ return eventDispatcher->hasPendingEvents();
+ return false;
+}
+
+#ifdef QT3_SUPPORT
+/*! \fn void QCoreApplication::lock()
+
+ In Qt 3, this function locked the Qt library mutex, allowing
+ non-GUI threads to perform basic printing operations using
+ QPainter.
+
+ In Qt 4, this is no longer supported, since painting is only
+ supported from within a paint event handler. This function does
+ nothing.
+
+ \sa QWidget::paintEvent()
+*/
+
+/*! \fn void QCoreApplication::unlock(bool wakeUpGui)
+
+ In Qt 3, this function unlocked the Qt library mutex. The mutex
+ allowed non-GUI threads to perform basic printing operations
+ using QPainter.
+
+ In Qt 4, this is no longer supported, since painting is only
+ supported from within a paint event handler. This function does
+ nothing.
+*/
+
+/*! \fn bool QCoreApplication::locked()
+
+ This function does nothing. It is there to keep old code working.
+ It always returns false.
+
+ See lock() for details.
+*/
+
+/*! \fn bool QCoreApplication::tryLock()
+
+ This function does nothing. It is there to keep old code working.
+ It always returns false.
+
+ See lock() for details.
+*/
+
+/*! \fn void QCoreApplication::processOneEvent()
+ \obsolete
+
+ Waits for an event to occur, processes it, then returns.
+
+ This function is useful for adapting Qt to situations where the
+ event processing must be grafted onto existing program loops.
+
+ Using this function in new applications may be an indication of design
+ problems.
+
+ \sa processEvents(), exec(), QTimer
+*/
+
+/*! \obsolete
+
+ This function enters the main event loop (recursively). Do not call
+ it unless you really know what you are doing.
+*/
+int QCoreApplication::enter_loop()
+{
+ if (!QCoreApplicationPrivate::checkInstance("enter_loop"))
+ return -1;
+ if (QThreadData::current() != self->d_func()->threadData) {
+ qWarning("QCoreApplication::enter_loop: Must be called from the main thread");
+ return -1;
+ }
+ QEventLoop eventLoop;
+ int returnCode = eventLoop.exec();
+ return returnCode;
+}
+
+/*! \obsolete
+
+ This function exits from a recursive call to the main event loop.
+ Do not call it unless you are an expert.
+*/
+void QCoreApplication::exit_loop()
+{
+ if (!QCoreApplicationPrivate::checkInstance("exit_loop"))
+ return;
+ QThreadData *data = QThreadData::current();
+ if (data != self->d_func()->threadData) {
+ qWarning("QCoreApplication::exit_loop: Must be called from the main thread");
+ return;
+ }
+ if (!data->eventLoops.isEmpty())
+ data->eventLoops.top()->exit();
+}
+
+/*! \obsolete
+
+ Returns the current loop level.
+*/
+int QCoreApplication::loopLevel()
+{
+ if (!QCoreApplicationPrivate::checkInstance("loopLevel"))
+ return -1;
+ return self->d_func()->threadData->eventLoops.size();
+}
+#endif
+
+/*
+ \fn void QCoreApplication::watchUnixSignal(int signal, bool watch)
+ \internal
+*/
+
+/*!
+ \fn void QCoreApplication::unixSignal(int number)
+ \internal
+
+ This signal is emitted whenever a Unix signal is received by the
+ application. The Unix signal received is specified by its \a number.
+*/
+
+/*!
+ \fn void qAddPostRoutine(QtCleanUpFunction ptr)
+ \relates QCoreApplication
+
+ Adds a global routine that will be called from the QApplication
+ destructor. This function is normally used to add cleanup routines
+ for program-wide functionality.
+
+ The function specified by \a ptr should take no arguments and should
+ return nothing. For example:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 4
+
+ Note that for an application- or module-wide cleanup,
+ qAddPostRoutine() is often not suitable. For example, if the
+ program is split into dynamically loaded modules, the relevant
+ module may be unloaded long before the QApplication destructor is
+ called.
+
+ For modules and libraries, using a reference-counted
+ initialization manager or Qt's parent-child deletion mechanism may
+ be better. Here is an example of a private class that uses the
+ parent-child mechanism to call a cleanup function at the right
+ time:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 5
+
+ By selecting the right parent object, this can often be made to
+ clean up the module's data at the right moment.
+*/
+
+/*!
+ \macro Q_DECLARE_TR_FUNCTIONS(context)
+ \relates QCoreApplication
+
+ The Q_DECLARE_TR_FUNCTIONS() macro declares and implements two
+ translation functions, \c tr() and \c trUtf8(), with these
+ signatures:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 6
+
+ This macro is useful if you want to use QObject::tr() or
+ QObject::trUtf8() in classes that don't inherit from QObject.
+
+ Q_DECLARE_TR_FUNCTIONS() must appear at the very top of the
+ class definition (before the first \c{public:} or \c{protected:}).
+ For example:
+
+ \snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 7
+
+ The \a context parameter is normally the class name, but it can
+ be any string.
+
+ \sa Q_OBJECT, QObject::tr(), QObject::trUtf8()
+*/
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h
new file mode 100644
index 0000000000..3957158d83
--- /dev/null
+++ b/src/corelib/kernel/qcoreapplication.h
@@ -0,0 +1,297 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCOREAPPLICATION_H
+#define QCOREAPPLICATION_H
+
+#include <QtCore/qobject.h>
+#include <QtCore/qcoreevent.h>
+#include <QtCore/qeventloop.h>
+
+#ifdef QT_INCLUDE_COMPAT
+#include <QtCore/qstringlist.h>
+#endif
+
+#if defined(Q_WS_WIN) && !defined(tagMSG)
+typedef struct tagMSG MSG;
+#endif
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Core)
+
+class QCoreApplicationPrivate;
+class QTextCodec;
+class QTranslator;
+class QPostEventList;
+class QStringList;
+
+#define qApp QCoreApplication::instance()
+
+class Q_CORE_EXPORT QCoreApplication : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName)
+ Q_PROPERTY(QString applicationVersion READ applicationVersion WRITE setApplicationVersion)
+ Q_PROPERTY(QString organizationName READ organizationName WRITE setOrganizationName)
+ Q_PROPERTY(QString organizationDomain READ organizationDomain WRITE setOrganizationDomain)
+
+ Q_DECLARE_PRIVATE(QCoreApplication)
+public:
+ enum { ApplicationFlags = QT_VERSION
+#if !defined(QT3_SUPPORT)
+ | 0x01000000
+#endif
+ };
+
+#if defined(QT_BUILD_CORE_LIB) || defined(qdoc)
+ QCoreApplication(int &argc, char **argv); // ### Qt5 remove
+#endif
+#if !defined(qdoc)
+ QCoreApplication(int &argc, char **argv, int
+#if !defined(QT_BUILD_CORE_LIB)
+ = ApplicationFlags
+#endif
+ );
+#endif
+
+ ~QCoreApplication();
+
+#ifdef QT_DEPRECATED
+ QT_DEPRECATED static int argc();
+ QT_DEPRECATED static char **argv();
+#endif
+ static QStringList arguments();
+
+ static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
+ static bool testAttribute(Qt::ApplicationAttribute attribute);
+
+ static void setOrganizationDomain(const QString &orgDomain);
+ static QString organizationDomain();
+ static void setOrganizationName(const QString &orgName);
+ static QString organizationName();
+ static void setApplicationName(const QString &application);
+ static QString applicationName();
+ static void setApplicationVersion(const QString &version);
+ static QString applicationVersion();
+
+ static QCoreApplication *instance() { return self; }
+
+ static int exec();
+ static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
+ static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
+ static void exit(int retcode=0);
+
+ static bool sendEvent(QObject *receiver, QEvent *event);
+ static void postEvent(QObject *receiver, QEvent *event);
+ static void postEvent(QObject *receiver, QEvent *event, int priority);
+ static void sendPostedEvents(QObject *receiver, int event_type);
+ static void sendPostedEvents();
+ static void removePostedEvents(QObject *receiver);
+ static void removePostedEvents(QObject *receiver, int eventType);
+ static bool hasPendingEvents();
+
+ virtual bool notify(QObject *, QEvent *);
+
+ static bool startingUp();
+ static bool closingDown();
+
+ static QString applicationDirPath();
+ static QString applicationFilePath();
+ static qint64 applicationPid();
+
+#ifndef QT_NO_LIBRARY
+ static void setLibraryPaths(const QStringList &);
+ static QStringList libraryPaths();
+ static void addLibraryPath(const QString &);
+ static void removeLibraryPath(const QString &);
+#endif // QT_NO_LIBRARY
+
+#ifndef QT_NO_TRANSLATION
+ static void installTranslator(QTranslator * messageFile);
+ static void removeTranslator(QTranslator * messageFile);
+#endif
+ enum Encoding { CodecForTr, UnicodeUTF8, DefaultCodec = CodecForTr };
+ // ### Qt 5: merge
+ static QString translate(const char * context,
+ const char * key,
+ const char * disambiguation = 0,
+ Encoding encoding = CodecForTr);
+ static QString translate(const char * context,
+ const char * key,
+ const char * disambiguation,
+ Encoding encoding, int n);
+
+ static void flush();
+
+#if defined(QT3_SUPPORT)
+ inline QT3_SUPPORT void lock() {}
+ inline QT3_SUPPORT void unlock(bool = true) {}
+ inline QT3_SUPPORT bool locked() { return false; }
+ inline QT3_SUPPORT bool tryLock() { return false; }
+
+ static inline QT3_SUPPORT void processOneEvent()
+ { processEvents(QEventLoop::WaitForMoreEvents); }
+ static QT3_SUPPORT int enter_loop();
+ static QT3_SUPPORT void exit_loop();
+ static QT3_SUPPORT int loopLevel();
+#endif
+
+#if defined(Q_WS_WIN)
+ virtual bool winEventFilter(MSG *message, long *result);
+#endif
+
+#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
+ static void watchUnixSignal(int signal, bool watch);
+#endif
+
+ typedef bool (*EventFilter)(void *message, long *result);
+ EventFilter setEventFilter(EventFilter filter);
+ bool filterEvent(void *message, long *result);
+
+public Q_SLOTS:
+ static void quit();
+
+Q_SIGNALS:
+ void aboutToQuit();
+ void unixSignal(int);
+
+protected:
+ bool event(QEvent *);
+
+ virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
+
+protected:
+ QCoreApplication(QCoreApplicationPrivate &p);
+
+private:
+ static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
+ bool notifyInternal(QObject *receiver, QEvent *event);
+
+ void init();
+
+ static QCoreApplication *self;
+
+ Q_DISABLE_COPY(QCoreApplication)
+
+ friend class QEventDispatcherUNIXPrivate;
+ friend class QApplication;
+ friend class QApplicationPrivate;
+ friend class QETWidget;
+ friend class Q3AccelManager;
+ friend class QShortcutMap;
+ friend class QWidget;
+ friend class QWidgetPrivate;
+ friend bool qt_sendSpontaneousEvent(QObject*, QEvent*);
+ friend Q_CORE_EXPORT QString qAppName();
+ friend class QClassFactory;
+};
+
+inline bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
+{ if (event) event->spont = false; return self ? self->notifyInternal(receiver, event) : false; }
+
+inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
+{ if (event) event->spont = true; return self ? self->notifyInternal(receiver, event) : false; }
+
+inline void QCoreApplication::sendPostedEvents() { sendPostedEvents(0, 0); }
+
+#ifdef QT_NO_TRANSLATION
+// Simple versions
+inline QString QCoreApplication::translate(const char *, const char *sourceText,
+ const char *, Encoding encoding)
+{
+#ifndef QT_NO_TEXTCODEC
+ if (encoding == UnicodeUTF8)
+ return QString::fromUtf8(sourceText);
+#else
+ Q_UNUSED(encoding)
+#endif
+ return QString::fromLatin1(sourceText);
+}
+
+// Simple versions
+inline QString QCoreApplication::translate(const char *, const char *sourceText,
+ const char *, Encoding encoding, int)
+{
+#ifndef QT_NO_TEXTCODEC
+ if (encoding == UnicodeUTF8)
+ return QString::fromUtf8(sourceText);
+#else
+ Q_UNUSED(encoding)
+#endif
+ return QString::fromLatin1(sourceText);
+}
+#endif
+
+// ### merge the four functions into two (using "int n = -1")
+#define Q_DECLARE_TR_FUNCTIONS(context) \
+public: \
+ static inline QString tr(const char *sourceText, const char *disambiguation = 0) \
+ { return QCoreApplication::translate(#context, sourceText, disambiguation); } \
+ static inline QString trUtf8(const char *sourceText, const char *disambiguation = 0) \
+ { return QCoreApplication::translate(#context, sourceText, disambiguation, \
+ QCoreApplication::UnicodeUTF8); } \
+ static inline QString tr(const char *sourceText, const char *disambiguation, int n) \
+ { return QCoreApplication::translate(#context, sourceText, disambiguation, \
+ QCoreApplication::CodecForTr, n); } \
+ static inline QString trUtf8(const char *sourceText, const char *disambiguation, int n) \
+ { return QCoreApplication::translate(#context, sourceText, disambiguation, \
+ QCoreApplication::UnicodeUTF8, n); } \
+private:
+
+typedef void (*QtCleanUpFunction)();
+
+Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction);
+Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction);
+Q_CORE_EXPORT QString qAppName(); // get application name
+
+#if defined(Q_WS_WIN) && !defined(QT_NO_DEBUG_STREAM)
+Q_CORE_EXPORT QString decodeMSG(const MSG &);
+Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
+#endif
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QCOREAPPLICATION_H
diff --git a/src/corelib/kernel/qcoreapplication_mac.cpp b/src/corelib/kernel/qcoreapplication_mac.cpp
new file mode 100644
index 0000000000..0734c6ccd4
--- /dev/null
+++ b/src/corelib/kernel/qcoreapplication_mac.cpp
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcoreapplication.h"
+#include "private/qcoreapplication_p.h"
+#include <private/qcore_mac_p.h>
+
+QT_BEGIN_NAMESPACE
+
+/*****************************************************************************
+ QCoreApplication utility functions
+ *****************************************************************************/
+QString qAppFileName()
+{
+ static QString appFileName;
+ if (appFileName.isEmpty()) {
+ QCFType<CFURLRef> bundleURL(CFBundleCopyExecutableURL(CFBundleGetMainBundle()));
+ if(bundleURL) {
+ QCFString cfPath(CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle));
+ if(cfPath)
+ appFileName = cfPath;
+ }
+ }
+ return appFileName;
+}
+
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h
new file mode 100644
index 0000000000..add2a3553e
--- /dev/null
+++ b/src/corelib/kernel/qcoreapplication_p.h
@@ -0,0 +1,146 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCOREAPPLICATION_P_H
+#define QCOREAPPLICATION_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qcoreapplication.h"
+#include "QtCore/qtranslator.h"
+#include "private/qobject_p.h"
+
+#ifdef Q_OS_SYMBIAN
+#include <f32file.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+typedef QList<QTranslator*> QTranslatorList;
+
+#if defined(Q_OS_SYMBIAN)
+# if !defined(QT_NO_SYSTEMLOCALE)
+class QEnvironmentChangeNotifier;
+# endif
+class CApaCommandLine;
+#endif
+class QAbstractEventDispatcher;
+
+class Q_CORE_EXPORT QCoreApplicationPrivate : public QObjectPrivate
+{
+ Q_DECLARE_PUBLIC(QCoreApplication)
+
+public:
+ QCoreApplicationPrivate(int &aargc, char **aargv, uint flags);
+ ~QCoreApplicationPrivate();
+
+ bool sendThroughApplicationEventFilters(QObject *, QEvent *);
+ bool sendThroughObjectEventFilters(QObject *, QEvent *);
+ bool notify_helper(QObject *, QEvent *);
+
+ virtual QString appName() const;
+ virtual void createEventDispatcher();
+ static void removePostedEvent(QEvent *);
+#ifdef Q_OS_WIN
+ static void removePostedTimerEvent(QObject *object, int timerId);
+#endif
+
+#ifdef Q_OS_MAC
+ static QString macMenuBarName();
+#endif
+
+ static QThread *theMainThread;
+ static QThread *mainThread();
+ static bool checkInstance(const char *method);
+ static void sendPostedEvents(QObject *receiver, int event_type, QThreadData *data);
+
+#if !defined (QT_NO_DEBUG) || defined (QT_MAC_FRAMEWORK_BUILD) || defined (Q_OS_SYMBIAN)
+ void checkReceiverThread(QObject *receiver);
+#endif
+ int &argc;
+ char **argv;
+ void appendApplicationPathToLibraryPaths(void);
+
+#ifndef QT_NO_TRANSLATION
+ QTranslatorList translators;
+#endif
+ uint application_type;
+
+ QCoreApplication::EventFilter eventFilter;
+
+ bool in_exec;
+ bool aboutToQuitEmitted;
+ QString cachedApplicationDirPath;
+ QString cachedApplicationFilePath;
+#if defined(Q_OS_SYMBIAN)
+# if !defined(QT_NO_SYSTEMLOCALE)
+ QScopedPointer<QEnvironmentChangeNotifier> environmentChangeNotifier;
+ void symbianInit();
+# endif
+ static CApaCommandLine* symbianCommandLine();
+#endif
+
+ static bool isTranslatorInstalled(QTranslator *translator);
+
+ static QAbstractEventDispatcher *eventDispatcher;
+ static bool is_app_running;
+ static bool is_app_closing;
+
+ static uint attribs;
+ static inline bool testAttribute(uint flag) { return attribs & (1 << flag); }
+ static int app_compile_version;
+#if defined(QT3_SUPPORT)
+ static bool useQt3Support;
+#endif
+};
+
+QT_END_NAMESPACE
+
+#endif // QCOREAPPLICATION_P_H
diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp
new file mode 100644
index 0000000000..a6f43a8900
--- /dev/null
+++ b/src/corelib/kernel/qcoreapplication_win.cpp
@@ -0,0 +1,1060 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcoreapplication.h"
+#include "qcoreapplication_p.h"
+#include "qstringlist.h"
+#include "qt_windows.h"
+#include "qvector.h"
+#include "qmutex.h"
+#include "qfileinfo.h"
+#include "qcorecmdlineargs_p.h"
+#include <private/qthread_p.h>
+#include <ctype.h>
+
+QT_BEGIN_NAMESPACE
+
+bool usingWinMain = false; // whether the qWinMain() is used or not
+int appCmdShow = 0;
+
+Q_CORE_EXPORT HINSTANCE qWinAppInst() // get Windows app handle
+{
+ return GetModuleHandle(0);
+}
+
+Q_CORE_EXPORT HINSTANCE qWinAppPrevInst() // get Windows prev app handle
+{
+ return 0;
+}
+
+Q_CORE_EXPORT int qWinAppCmdShow() // get main window show command
+{
+#if defined(Q_OS_WINCE)
+ return appCmdShow;
+#else
+ STARTUPINFO startupInfo;
+ GetStartupInfo(&startupInfo);
+
+ return (startupInfo.dwFlags & STARTF_USESHOWWINDOW)
+ ? startupInfo.wShowWindow
+ : SW_SHOWDEFAULT;
+#endif
+}
+
+Q_CORE_EXPORT QString qAppFileName() // get application file name
+{
+ // We do MAX_PATH + 2 here, and request with MAX_PATH + 1, so we can handle all paths
+ // up to, and including MAX_PATH size perfectly fine with string termination, as well
+ // as easily detect if the file path is indeed larger than MAX_PATH, in which case we
+ // need to use the heap instead. This is a work-around, since contrary to what the
+ // MSDN documentation states, GetModuleFileName sometimes doesn't set the
+ // ERROR_INSUFFICIENT_BUFFER error number, and we thus cannot rely on this value if
+ // GetModuleFileName(0, buffer, MAX_PATH) == MAX_PATH.
+ // GetModuleFileName(0, buffer, MAX_PATH + 1) == MAX_PATH just means we hit the normal
+ // file path limit, and we handle it normally, if the result is MAX_PATH + 1, we use
+ // heap (even if the result _might_ be exactly MAX_PATH + 1, but that's ok).
+ wchar_t buffer[MAX_PATH + 2];
+ DWORD v = GetModuleFileName(0, buffer, MAX_PATH + 1);
+ buffer[MAX_PATH + 1] = 0;
+
+ if (v == 0)
+ return QString();
+ else if (v <= MAX_PATH)
+ return QString::fromWCharArray(buffer);
+
+ // MAX_PATH sized buffer wasn't large enough to contain the full path, use heap
+ wchar_t *b = 0;
+ int i = 1;
+ size_t size;
+ do {
+ ++i;
+ size = MAX_PATH * i;
+ b = reinterpret_cast<wchar_t *>(realloc(b, (size + 1) * sizeof(wchar_t)));
+ if (b)
+ v = GetModuleFileName(NULL, b, size);
+ } while (b && v == size);
+
+ if (b)
+ *(b + size) = 0;
+ QString res = QString::fromWCharArray(b);
+ free(b);
+
+ return res;
+}
+
+QString QCoreApplicationPrivate::appName() const
+{
+ return QFileInfo(qAppFileName()).baseName();
+}
+
+class QWinMsgHandlerCriticalSection
+{
+ CRITICAL_SECTION cs;
+public:
+ QWinMsgHandlerCriticalSection()
+ { InitializeCriticalSection(&cs); }
+ ~QWinMsgHandlerCriticalSection()
+ { DeleteCriticalSection(&cs); }
+
+ void lock()
+ { EnterCriticalSection(&cs); }
+ void unlock()
+ { LeaveCriticalSection(&cs); }
+};
+
+Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char* str)
+{
+ Q_UNUSED(t);
+ // OutputDebugString is not threadsafe.
+
+ // cannot use QMutex here, because qWarning()s in the QMutex
+ // implementation may cause this function to recurse
+ static QWinMsgHandlerCriticalSection staticCriticalSection;
+
+ if (!str)
+ str = "(null)";
+
+ staticCriticalSection.lock();
+
+ QString s(QString::fromLocal8Bit(str));
+ s += QLatin1Char('\n');
+ OutputDebugString((wchar_t*)s.utf16());
+
+ staticCriticalSection.unlock();
+}
+
+
+/*****************************************************************************
+ qWinMain() - Initializes Windows. Called from WinMain() in qtmain_win.cpp
+ *****************************************************************************/
+
+#if defined(Q_OS_WINCE)
+Q_CORE_EXPORT void __cdecl qWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdParam,
+ int cmdShow, int &argc, QVector<char *> &argv)
+#else
+Q_CORE_EXPORT
+void qWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdParam,
+ int cmdShow, int &argc, QVector<char *> &argv)
+#endif
+{
+ static bool already_called = false;
+
+ if (already_called) {
+ qWarning("Qt: Internal error: qWinMain should be called only once");
+ return;
+ }
+ already_called = true;
+ usingWinMain = true;
+
+ // Install default debug handler
+ qInstallMsgHandler(qWinMsgHandler);
+
+ // Create command line
+ argv = qWinCmdLine<char>(cmdParam, int(strlen(cmdParam)), argc);
+
+ appCmdShow = cmdShow;
+
+ // Ignore Windows parameters
+ Q_UNUSED(instance);
+ Q_UNUSED(prevInstance);
+}
+
+/*!
+ The message procedure calls this function for every message
+ received. Reimplement this function if you want to process window
+ messages \a msg that are not processed by Qt. If you don't want
+ the event to be processed by Qt, then return true and set \a result
+ to the value that the window procedure should return. Otherwise
+ return false.
+
+ It is only directly addressed messages that are filtered. To
+ handle system wide messages, such as messages from a registered
+ hot key, you need to install an event filter on the event
+ dispatcher, which is returned from
+ QAbstractEventDispatcher::instance().
+*/
+bool QCoreApplication::winEventFilter(MSG *msg, long *result) // Windows event filter
+{
+ Q_UNUSED(msg);
+ Q_UNUSED(result);
+ return false;
+}
+
+void QCoreApplicationPrivate::removePostedTimerEvent(QObject *object, int timerId)
+{
+ QThreadData *data = object->d_func()->threadData;
+
+ QMutexLocker locker(&data->postEventList.mutex);
+ if (data->postEventList.size() == 0)
+ return;
+ for (int i = 0; i < data->postEventList.size(); ++i) {
+ const QPostEvent & pe = data->postEventList.at(i);
+ if (pe.receiver == object
+ && pe.event
+ && (pe.event->type() == QEvent::Timer || pe.event->type() == QEvent::ZeroTimerEvent)
+ && static_cast<QTimerEvent *>(pe.event)->timerId() == timerId) {
+ --pe.receiver->d_func()->postedEvents;
+ pe.event->posted = false;
+ delete pe.event;
+ const_cast<QPostEvent &>(pe).event = 0;
+ return;
+ }
+ }
+}
+
+#if defined(Q_WS_WIN) && !defined(QT_NO_DEBUG_STREAM)
+/*****************************************************************************
+ Convenience functions for convert WM_* messages into human readable strings,
+ including a nifty QDebug operator<< for simpel QDebug() << msg output.
+ *****************************************************************************/
+QT_BEGIN_INCLUDE_NAMESPACE
+#include <windowsx.h>
+#include "qdebug.h"
+QT_END_INCLUDE_NAMESPACE
+
+#if !defined(GET_X_LPARAM)
+# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
+# define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
+#endif
+#ifdef _WIN32_WCE
+# ifndef WM_NCACTIVATE
+# define WM_NCACTIVATE 0x86
+# endif
+#endif
+
+// The values below should never change. Note that none of the usual
+// WM_...FIRST & WM_...LAST values are in the list, as they normally have other
+// WM_... representations
+struct KnownWM {
+ uint WM;
+ const char* str;
+} knownWM[] =
+{{ 0x0000, "WM_NULL" },
+ { 0x0001, "WM_CREATE" },
+ { 0x0002, "WM_DESTROY" },
+ { 0x0003, "WM_MOVE" },
+ { 0x0005, "WM_SIZE" },
+ { 0x0006, "WM_ACTIVATE" },
+ { 0x0007, "WM_SETFOCUS" },
+ { 0x0008, "WM_KILLFOCUS" },
+ { 0x000A, "WM_ENABLE" },
+ { 0x000B, "WM_SETREDRAW" },
+ { 0x000C, "WM_SETTEXT" },
+ { 0x000D, "WM_GETTEXT" },
+ { 0x000E, "WM_GETTEXTLENGTH" },
+ { 0x000F, "WM_PAINT" },
+ { 0x0010, "WM_CLOSE" },
+ { 0x0011, "WM_QUERYENDSESSION" },
+ { 0x0013, "WM_QUERYOPEN" },
+ { 0x0016, "WM_ENDSESSION" },
+ { 0x0012, "WM_QUIT" },
+ { 0x0014, "WM_ERASEBKGND" },
+ { 0x0015, "WM_SYSCOLORCHANGE" },
+ { 0x0018, "WM_SHOWWINDOW" },
+ { 0x001A, "WM_WININICHANGE" },
+ { 0x001B, "WM_DEVMODECHANGE" },
+ { 0x001C, "WM_ACTIVATEAPP" },
+ { 0x001D, "WM_FONTCHANGE" },
+ { 0x001E, "WM_TIMECHANGE" },
+ { 0x001F, "WM_CANCELMODE" },
+ { 0x0020, "WM_SETCURSOR" },
+ { 0x0021, "WM_MOUSEACTIVATE" },
+ { 0x0022, "WM_CHILDACTIVATE" },
+ { 0x0023, "WM_QUEUESYNC" },
+ { 0x0024, "WM_GETMINMAXINFO" },
+ { 0x0026, "WM_PAINTICON" },
+ { 0x0027, "WM_ICONERASEBKGND" },
+ { 0x0028, "WM_NEXTDLGCTL" },
+ { 0x002A, "WM_SPOOLERSTATUS" },
+ { 0x002B, "WM_DRAWITEM" },
+ { 0x002C, "WM_MEASUREITEM" },
+ { 0x002D, "WM_DELETEITEM" },
+ { 0x002E, "WM_VKEYTOITEM" },
+ { 0x002F, "WM_CHARTOITEM" },
+ { 0x0030, "WM_SETFONT" },
+ { 0x0031, "WM_GETFONT" },
+ { 0x0032, "WM_SETHOTKEY" },
+ { 0x0033, "WM_GETHOTKEY" },
+ { 0x0037, "WM_QUERYDRAGICON" },
+ { 0x0039, "WM_COMPAREITEM" },
+ { 0x003D, "WM_GETOBJECT" },
+ { 0x0041, "WM_COMPACTING" },
+ { 0x0044, "WM_COMMNOTIFY" },
+ { 0x0046, "WM_WINDOWPOSCHANGING" },
+ { 0x0047, "WM_WINDOWPOSCHANGED" },
+ { 0x0048, "WM_POWER" },
+ { 0x004A, "WM_COPYDATA" },
+ { 0x004B, "WM_CANCELJOURNAL" },
+ { 0x004E, "WM_NOTIFY" },
+ { 0x0050, "WM_INPUTLANGCHANGEREQUEST" },
+ { 0x0051, "WM_INPUTLANGCHANGE" },
+ { 0x0052, "WM_TCARD" },
+ { 0x0053, "WM_HELP" },
+ { 0x0054, "WM_USERCHANGED" },
+ { 0x0055, "WM_NOTIFYFORMAT" },
+ { 0x007B, "WM_CONTEXTMENU" },
+ { 0x007C, "WM_STYLECHANGING" },
+ { 0x007D, "WM_STYLECHANGED" },
+ { 0x007E, "WM_DISPLAYCHANGE" },
+ { 0x007F, "WM_GETICON" },
+ { 0x0080, "WM_SETICON" },
+ { 0x0081, "WM_NCCREATE" },
+ { 0x0082, "WM_NCDESTROY" },
+ { 0x0083, "WM_NCCALCSIZE" },
+ { 0x0084, "WM_NCHITTEST" },
+ { 0x0085, "WM_NCPAINT" },
+ { 0x0086, "WM_NCACTIVATE" },
+ { 0x0087, "WM_GETDLGCODE" },
+ { 0x0088, "WM_SYNCPAINT" },
+ { 0x00A0, "WM_NCMOUSEMOVE" },
+ { 0x00A1, "WM_NCLBUTTONDOWN" },
+ { 0x00A2, "WM_NCLBUTTONUP" },
+ { 0x00A3, "WM_NCLBUTTONDBLCLK" },
+ { 0x00A4, "WM_NCRBUTTONDOWN" },
+ { 0x00A5, "WM_NCRBUTTONUP" },
+ { 0x00A6, "WM_NCRBUTTONDBLCLK" },
+ { 0x00A7, "WM_NCMBUTTONDOWN" },
+ { 0x00A8, "WM_NCMBUTTONUP" },
+ { 0x00A9, "WM_NCMBUTTONDBLCLK" },
+ { 0x00AB, "WM_NCXBUTTONDOWN" },
+ { 0x00AC, "WM_NCXBUTTONUP" },
+ { 0x00AD, "WM_NCXBUTTONDBLCLK" },
+ { 0x00FF, "WM_INPUT" },
+ { 0x0100, "WM_KEYDOWN" },
+ { 0x0101, "WM_KEYUP" },
+ { 0x0102, "WM_CHAR" },
+ { 0x0103, "WM_DEADCHAR" },
+ { 0x0104, "WM_SYSKEYDOWN" },
+ { 0x0105, "WM_SYSKEYUP" },
+ { 0x0106, "WM_SYSCHAR" },
+ { 0x0107, "WM_SYSDEADCHAR" },
+ { 0x0109, "WM_UNICHAR" },
+ { 0x010D, "WM_IME_STARTCOMPOSITION" },
+ { 0x010E, "WM_IME_ENDCOMPOSITION" },
+ { 0x010F, "WM_IME_COMPOSITION" },
+ { 0x0110, "WM_INITDIALOG" },
+ { 0x0111, "WM_COMMAND" },
+ { 0x0112, "WM_SYSCOMMAND" },
+ { 0x0113, "WM_TIMER" },
+ { 0x0114, "WM_HSCROLL" },
+ { 0x0115, "WM_VSCROLL" },
+ { 0x0116, "WM_INITMENU" },
+ { 0x0117, "WM_INITMENUPOPUP" },
+ { 0x011F, "WM_MENUSELECT" },
+ { 0x0120, "WM_MENUCHAR" },
+ { 0x0121, "WM_ENTERIDLE" },
+ { 0x0122, "WM_MENURBUTTONUP" },
+ { 0x0123, "WM_MENUDRAG" },
+ { 0x0124, "WM_MENUGETOBJECT" },
+ { 0x0125, "WM_UNINITMENUPOPUP" },
+ { 0x0126, "WM_MENUCOMMAND" },
+ { 0x0127, "WM_CHANGEUISTATE" },
+ { 0x0128, "WM_UPDATEUISTATE" },
+ { 0x0129, "WM_QUERYUISTATE" },
+ { 0x0132, "WM_CTLCOLORMSGBOX" },
+ { 0x0133, "WM_CTLCOLOREDIT" },
+ { 0x0134, "WM_CTLCOLORLISTBOX" },
+ { 0x0135, "WM_CTLCOLORBTN" },
+ { 0x0136, "WM_CTLCOLORDLG" },
+ { 0x0137, "WM_CTLCOLORSCROLLBAR" },
+ { 0x0138, "WM_CTLCOLORSTATIC" },
+ { 0x0200, "WM_MOUSEMOVE" },
+ { 0x0201, "WM_LBUTTONDOWN" },
+ { 0x0202, "WM_LBUTTONUP" },
+ { 0x0203, "WM_LBUTTONDBLCLK" },
+ { 0x0204, "WM_RBUTTONDOWN" },
+ { 0x0205, "WM_RBUTTONUP" },
+ { 0x0206, "WM_RBUTTONDBLCLK" },
+ { 0x0207, "WM_MBUTTONDOWN" },
+ { 0x0208, "WM_MBUTTONUP" },
+ { 0x0209, "WM_MBUTTONDBLCLK" },
+ { 0x020A, "WM_MOUSEWHEEL" },
+ { 0x020B, "WM_XBUTTONDOWN" },
+ { 0x020C, "WM_XBUTTONUP" },
+ { 0x020D, "WM_XBUTTONDBLCLK" },
+ { 0x020E, "WM_MOUSEHWHEEL" },
+ { 0x0210, "WM_PARENTNOTIFY" },
+ { 0x0211, "WM_ENTERMENULOOP" },
+ { 0x0212, "WM_EXITMENULOOP" },
+ { 0x0213, "WM_NEXTMENU" },
+ { 0x0214, "WM_SIZING" },
+ { 0x0215, "WM_CAPTURECHANGED" },
+ { 0x0216, "WM_MOVING" },
+ { 0x0218, "WM_POWERBROADCAST" },
+ { 0x0219, "WM_DEVICECHANGE" },
+ { 0x0220, "WM_MDICREATE" },
+ { 0x0221, "WM_MDIDESTROY" },
+ { 0x0222, "WM_MDIACTIVATE" },
+ { 0x0223, "WM_MDIRESTORE" },
+ { 0x0224, "WM_MDINEXT" },
+ { 0x0225, "WM_MDIMAXIMIZE" },
+ { 0x0226, "WM_MDITILE" },
+ { 0x0227, "WM_MDICASCADE" },
+ { 0x0228, "WM_MDIICONARRANGE" },
+ { 0x0229, "WM_MDIGETACTIVE" },
+ { 0x0230, "WM_MDISETMENU" },
+ { 0x0231, "WM_ENTERSIZEMOVE" },
+ { 0x0232, "WM_EXITSIZEMOVE" },
+ { 0x0233, "WM_DROPFILES" },
+ { 0x0234, "WM_MDIREFRESHMENU" },
+ { 0x0281, "WM_IME_SETCONTEXT" },
+ { 0x0282, "WM_IME_NOTIFY" },
+ { 0x0283, "WM_IME_CONTROL" },
+ { 0x0284, "WM_IME_COMPOSITIONFULL" },
+ { 0x0285, "WM_IME_SELECT" },
+ { 0x0286, "WM_IME_CHAR" },
+ { 0x0288, "WM_IME_REQUEST" },
+ { 0x0290, "WM_IME_KEYDOWN" },
+ { 0x0291, "WM_IME_KEYUP" },
+ { 0x02A0, "WM_NCMOUSEHOVER" },
+ { 0x02A1, "WM_MOUSEHOVER" },
+ { 0x02A2, "WM_NCMOUSELEAVE" },
+ { 0x02A3, "WM_MOUSELEAVE" },
+ { 0x02B1, "WM_WTSSESSION_CHANGE" },
+ { 0x02C0, "WM_TABLET_FIRST" },
+ { 0x02C1, "WM_TABLET_FIRST + 1" },
+ { 0x02C2, "WM_TABLET_FIRST + 2" },
+ { 0x02C3, "WM_TABLET_FIRST + 3" },
+ { 0x02C4, "WM_TABLET_FIRST + 4" },
+ { 0x02C5, "WM_TABLET_FIRST + 5" },
+ { 0x02C6, "WM_TABLET_FIRST + 6" },
+ { 0x02C7, "WM_TABLET_FIRST + 7" },
+ { 0x02C8, "WM_TABLET_FIRST + 8" },
+ { 0x02C9, "WM_TABLET_FIRST + 9" },
+ { 0x02CA, "WM_TABLET_FIRST + 10" },
+ { 0x02CB, "WM_TABLET_FIRST + 11" },
+ { 0x02CC, "WM_TABLET_FIRST + 12" },
+ { 0x02CD, "WM_TABLET_FIRST + 13" },
+ { 0x02CE, "WM_TABLET_FIRST + 14" },
+ { 0x02CF, "WM_TABLET_FIRST + 15" },
+ { 0x02D0, "WM_TABLET_FIRST + 16" },
+ { 0x02D1, "WM_TABLET_FIRST + 17" },
+ { 0x02D2, "WM_TABLET_FIRST + 18" },
+ { 0x02D3, "WM_TABLET_FIRST + 19" },
+ { 0x02D4, "WM_TABLET_FIRST + 20" },
+ { 0x02D5, "WM_TABLET_FIRST + 21" },
+ { 0x02D6, "WM_TABLET_FIRST + 22" },
+ { 0x02D7, "WM_TABLET_FIRST + 23" },
+ { 0x02D8, "WM_TABLET_FIRST + 24" },
+ { 0x02D9, "WM_TABLET_FIRST + 25" },
+ { 0x02DA, "WM_TABLET_FIRST + 26" },
+ { 0x02DB, "WM_TABLET_FIRST + 27" },
+ { 0x02DC, "WM_TABLET_FIRST + 28" },
+ { 0x02DD, "WM_TABLET_FIRST + 29" },
+ { 0x02DE, "WM_TABLET_FIRST + 30" },
+ { 0x02DF, "WM_TABLET_LAST" },
+ { 0x0300, "WM_CUT" },
+ { 0x0301, "WM_COPY" },
+ { 0x0302, "WM_PASTE" },
+ { 0x0303, "WM_CLEAR" },
+ { 0x0304, "WM_UNDO" },
+ { 0x0305, "WM_RENDERFORMAT" },
+ { 0x0306, "WM_RENDERALLFORMATS" },
+ { 0x0307, "WM_DESTROYCLIPBOARD" },
+ { 0x0308, "WM_DRAWCLIPBOARD" },
+ { 0x0309, "WM_PAINTCLIPBOARD" },
+ { 0x030A, "WM_VSCROLLCLIPBOARD" },
+ { 0x030B, "WM_SIZECLIPBOARD" },
+ { 0x030C, "WM_ASKCBFORMATNAME" },
+ { 0x030D, "WM_CHANGECBCHAIN" },
+ { 0x030E, "WM_HSCROLLCLIPBOARD" },
+ { 0x030F, "WM_QUERYNEWPALETTE" },
+ { 0x0310, "WM_PALETTEISCHANGING" },
+ { 0x0311, "WM_PALETTECHANGED" },
+ { 0x0312, "WM_HOTKEY" },
+ { 0x0317, "WM_PRINT" },
+ { 0x0318, "WM_PRINTCLIENT" },
+ { 0x0319, "WM_APPCOMMAND" },
+ { 0x031A, "WM_THEMECHANGED" },
+ { 0x0358, "WM_HANDHELDFIRST" },
+ { 0x0359, "WM_HANDHELDFIRST + 1" },
+ { 0x035A, "WM_HANDHELDFIRST + 2" },
+ { 0x035B, "WM_HANDHELDFIRST + 3" },
+ { 0x035C, "WM_HANDHELDFIRST + 4" },
+ { 0x035D, "WM_HANDHELDFIRST + 5" },
+ { 0x035E, "WM_HANDHELDFIRST + 6" },
+ { 0x035F, "WM_HANDHELDLAST" },
+ { 0x0360, "WM_AFXFIRST" },
+ { 0x0361, "WM_AFXFIRST + 1" },
+ { 0x0362, "WM_AFXFIRST + 2" },
+ { 0x0363, "WM_AFXFIRST + 3" },
+ { 0x0364, "WM_AFXFIRST + 4" },
+ { 0x0365, "WM_AFXFIRST + 5" },
+ { 0x0366, "WM_AFXFIRST + 6" },
+ { 0x0367, "WM_AFXFIRST + 7" },
+ { 0x0368, "WM_AFXFIRST + 8" },
+ { 0x0369, "WM_AFXFIRST + 9" },
+ { 0x036A, "WM_AFXFIRST + 10" },
+ { 0x036B, "WM_AFXFIRST + 11" },
+ { 0x036C, "WM_AFXFIRST + 12" },
+ { 0x036D, "WM_AFXFIRST + 13" },
+ { 0x036E, "WM_AFXFIRST + 14" },
+ { 0x036F, "WM_AFXFIRST + 15" },
+ { 0x0370, "WM_AFXFIRST + 16" },
+ { 0x0371, "WM_AFXFIRST + 17" },
+ { 0x0372, "WM_AFXFIRST + 18" },
+ { 0x0373, "WM_AFXFIRST + 19" },
+ { 0x0374, "WM_AFXFIRST + 20" },
+ { 0x0375, "WM_AFXFIRST + 21" },
+ { 0x0376, "WM_AFXFIRST + 22" },
+ { 0x0377, "WM_AFXFIRST + 23" },
+ { 0x0378, "WM_AFXFIRST + 24" },
+ { 0x0379, "WM_AFXFIRST + 25" },
+ { 0x037A, "WM_AFXFIRST + 26" },
+ { 0x037B, "WM_AFXFIRST + 27" },
+ { 0x037C, "WM_AFXFIRST + 28" },
+ { 0x037D, "WM_AFXFIRST + 29" },
+ { 0x037E, "WM_AFXFIRST + 30" },
+ { 0x037F, "WM_AFXLAST" },
+ { 0x0380, "WM_PENWINFIRST" },
+ { 0x0381, "WM_PENWINFIRST + 1" },
+ { 0x0382, "WM_PENWINFIRST + 2" },
+ { 0x0383, "WM_PENWINFIRST + 3" },
+ { 0x0384, "WM_PENWINFIRST + 4" },
+ { 0x0385, "WM_PENWINFIRST + 5" },
+ { 0x0386, "WM_PENWINFIRST + 6" },
+ { 0x0387, "WM_PENWINFIRST + 7" },
+ { 0x0388, "WM_PENWINFIRST + 8" },
+ { 0x0389, "WM_PENWINFIRST + 9" },
+ { 0x038A, "WM_PENWINFIRST + 10" },
+ { 0x038B, "WM_PENWINFIRST + 11" },
+ { 0x038C, "WM_PENWINFIRST + 12" },
+ { 0x038D, "WM_PENWINFIRST + 13" },
+ { 0x038E, "WM_PENWINFIRST + 14" },
+ { 0x038F, "WM_PENWINLAST" },
+ { 0x0400, "WM_USER" },
+ { 0x8000, "WM_APP" },
+ { 0,0 }}; // End of known messages
+
+// Looks up the WM_ message in the table above
+static const char* findWMstr(uint msg)
+{
+ uint i = 0;
+ const char* result = 0;
+ // Known WM_'s
+ while (knownWM[i].str && (knownWM[i].WM != msg))
+ ++i;
+ result = knownWM[i].str;
+ return result;
+};
+
+// Convenience function for converting flags and values into readable strings
+struct FLAG_STRING_STRUCT
+{
+ uint value;
+ const char* str;
+};
+
+FLAG_STRING_STRUCT FLAG_STRING(int value = 0, const char *c = 0)
+{
+ FLAG_STRING_STRUCT s = {value, c};
+ return s;
+}
+
+#define FLGSTR(x) FLAG_STRING(x, #x)
+
+// Returns an ORed (" | ") together string for the flags active in the actual
+// value. (...) must consist of FLAG_STRING, with a FLAG_STRING() as the last
+// value in the list passed to the function
+QString flagCheck(uint actual, ...)
+{
+ va_list ap;
+ va_start(ap, actual);
+
+ QString result;
+ int count = 0;
+ FLAG_STRING_STRUCT v;
+ while((v=va_arg(ap,FLAG_STRING_STRUCT)).str) {
+ if ((actual & v.value) == v.value) {
+ if (count++)
+ result += QLatin1String(" | ");
+ result += QString::fromLatin1(v.str);
+ }
+ }
+ va_end(ap);
+ return result;
+};
+
+// Returns the string representation of the value in 'actual'. (...) must
+// consist of FLAG_STRING, with a FLAG_STRING() as the last value in the list
+// passed to the function
+QString valueCheck(uint actual, ...)
+{
+ va_list ap;
+ va_start(ap, actual);
+
+ QString result;
+ FLAG_STRING_STRUCT v;
+ while((v=va_arg(ap,FLAG_STRING_STRUCT)).str && (actual != v.value))
+ ;
+ result = QString::fromLatin1(v.str);
+
+ va_end(ap);
+ return result;
+};
+
+#ifdef Q_CC_BOR
+
+QString decodeMSG(const MSG& msg)
+{
+ return QString::fromLatin1("THis is not supported on Borland");
+}
+
+#else
+
+// Returns a "human readable" string representation of the MSG and the
+// information it points to
+QString decodeMSG(const MSG& msg)
+{
+ const WPARAM wParam = msg.wParam;
+ const LPARAM lParam = msg.lParam;
+ QString wmmsg = QString::fromLatin1(findWMstr(msg.message));
+ // Unknown WM_, so use number
+ if (wmmsg.isEmpty())
+ wmmsg = QString::fromLatin1("WM_(%1)").arg(msg.message);
+
+ QString rawParameters;
+ rawParameters.sprintf("hwnd(0x%p) ", (void *)msg.hwnd);
+
+ // Custom WM_'s
+ if (msg.message > WM_APP)
+ wmmsg = QString::fromLatin1("WM_APP + %1").arg(msg.message - WM_APP);
+ else if (msg.message > WM_USER)
+ wmmsg = QString::fromLatin1("WM_USER + %1").arg(msg.message - WM_USER);
+
+ QString parameters;
+ switch (msg.message) {
+#ifdef WM_ACTIVATE
+ case WM_ACTIVATE:
+ {
+ QString activation = valueCheck(wParam,
+ FLAG_STRING(WA_ACTIVE, "Activate"),
+ FLAG_STRING(WA_INACTIVE, "Deactivate"),
+ FLAG_STRING(WA_CLICKACTIVE, "Activate by mouseclick"),
+ FLAG_STRING());
+ parameters.sprintf("%s Hwnd (0x%p)", activation.toLatin1().data(), (void *)msg.hwnd);
+ }
+ break;
+#endif
+#ifdef WM_CAPTURECHANGED
+ case WM_CAPTURECHANGED:
+ parameters.sprintf("Hwnd gaining capture (0x%p)", (void *)lParam);
+ break;
+#endif
+#ifdef WM_CREATE
+ case WM_CREATE:
+ {
+ LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
+ QString styles = flagCheck(lpcs->style,
+ FLGSTR(WS_BORDER),
+ FLGSTR(WS_CAPTION),
+ FLGSTR(WS_CHILD),
+ FLGSTR(WS_CLIPCHILDREN),
+ FLGSTR(WS_CLIPSIBLINGS),
+ FLGSTR(WS_DISABLED),
+ FLGSTR(WS_DLGFRAME),
+ FLGSTR(WS_GROUP),
+ FLGSTR(WS_HSCROLL),
+ FLGSTR(WS_OVERLAPPED),
+#if defined(WS_OVERLAPPEDWINDOW) && (WS_OVERLAPPEDWINDOW != 0)
+ FLGSTR(WS_OVERLAPPEDWINDOW),
+#endif
+#ifdef WS_ICONIC
+ FLGSTR(WS_ICONIC),
+#endif
+ FLGSTR(WS_MAXIMIZE),
+ FLGSTR(WS_MAXIMIZEBOX),
+ FLGSTR(WS_MINIMIZE),
+ FLGSTR(WS_MINIMIZEBOX),
+ FLGSTR(WS_OVERLAPPEDWINDOW),
+ FLGSTR(WS_POPUP),
+#ifdef WS_POPUPWINDOW
+ FLGSTR(WS_POPUPWINDOW),
+#endif
+ FLGSTR(WS_SIZEBOX),
+ FLGSTR(WS_SYSMENU),
+ FLGSTR(WS_TABSTOP),
+ FLGSTR(WS_THICKFRAME),
+#ifdef WS_TILED
+ FLGSTR(WS_TILED),
+#endif
+#ifdef WS_TILEDWINDOW
+ FLGSTR(WS_TILEDWINDOW),
+#endif
+ FLGSTR(WS_VISIBLE),
+ FLGSTR(WS_VSCROLL),
+ FLAG_STRING());
+
+ QString exStyles = flagCheck(lpcs->dwExStyle,
+#ifdef WS_EX_ACCEPTFILES
+ FLGSTR(WS_EX_ACCEPTFILES),
+#endif
+#ifdef WS_EX_APPWINDOW
+ FLGSTR(WS_EX_APPWINDOW),
+#endif
+ FLGSTR(WS_EX_CLIENTEDGE),
+ FLGSTR(WS_EX_DLGMODALFRAME),
+#ifdef WS_EX_LEFT
+ FLGSTR(WS_EX_LEFT),
+#endif
+ FLGSTR(WS_EX_LEFTSCROLLBAR),
+#ifdef WS_EX_LTRREADING
+ FLGSTR(WS_EX_LTRREADING),
+#endif
+#ifdef WS_EX_MDICHILD
+ FLGSTR(WS_EX_MDICHILD),
+#endif
+#ifdef WS_EX_NOACTIVATE
+ FLGSTR(WS_EX_NOACTIVATE),
+#endif
+#ifdef WS_EX_NOANIMATION
+ FLGSTR(WS_EX_NOANIMATION),
+#endif
+ FLGSTR(WS_EX_NOPARENTNOTIFY),
+ FLGSTR(WS_EX_OVERLAPPEDWINDOW),
+#ifdef WS_EX_PALETTEWINDOW
+ FLGSTR(WS_EX_PALETTEWINDOW),
+#endif
+#ifdef WS_EX_RIGHT
+ FLGSTR(WS_EX_RIGHT),
+#endif
+#ifdef WS_EX_RIGHTSCROLLBAR
+ FLGSTR(WS_EX_RIGHTSCROLLBAR),
+#endif
+#ifdef WS_EX_RTLREADING
+ FLGSTR(WS_EX_RTLREADING),
+#endif
+ FLGSTR(WS_EX_STATICEDGE),
+ FLGSTR(WS_EX_TOOLWINDOW),
+ FLGSTR(WS_EX_TOPMOST),
+#ifdef WS_EX_TRANSPARENT
+ FLGSTR(WS_EX_TRANSPARENT),
+#endif
+ FLGSTR(WS_EX_WINDOWEDGE),
+#ifdef WS_EX_CAPTIONOKBTN
+ FLGSTR(WS_EX_CAPTIONOKBTN),
+#endif
+ FLAG_STRING());
+
+ QString className;
+ if (lpcs->lpszClass != 0) {
+ if (HIWORD(lpcs->lpszClass) == 0) // Atom
+ className = QString::number(LOWORD(lpcs->lpszClass), 16);
+ else // String
+ className = QString((QChar*)lpcs->lpszClass,
+ (int)wcslen(reinterpret_cast<const wchar_t *>(lpcs->lpszClass)));
+ }
+
+ QString windowName;
+ if (lpcs->lpszName != 0)
+ windowName = QString((QChar*)lpcs->lpszName,
+ (int)wcslen(reinterpret_cast<const wchar_t *>(lpcs->lpszName)));
+
+ parameters.sprintf("x,y(%4d,%4d) w,h(%4d,%4d) className(%s) windowName(%s) parent(0x%p) style(%s) exStyle(%s)",
+ lpcs->x, lpcs->y, lpcs->cx, lpcs->cy, className.toLatin1().data(),
+ windowName.toLatin1().data(), (void *)lpcs->hwndParent,
+ styles.toLatin1().data(), exStyles.toLatin1().data());
+ }
+ break;
+#endif
+#ifdef WM_DESTROY
+ case WM_DESTROY:
+ parameters.sprintf("Destroy hwnd (0x%p)", (void *)msg.hwnd);
+ break;
+#endif
+#ifdef WM_IME_NOTIFY
+ case WM_IME_NOTIFY:
+ {
+ QString imnCommand = valueCheck(wParam,
+ FLGSTR(IMN_CHANGECANDIDATE),
+ FLGSTR(IMN_CLOSECANDIDATE),
+ FLGSTR(IMN_CLOSESTATUSWINDOW),
+ FLGSTR(IMN_GUIDELINE),
+ FLGSTR(IMN_OPENCANDIDATE),
+ FLGSTR(IMN_OPENSTATUSWINDOW),
+ FLGSTR(IMN_SETCANDIDATEPOS),
+ FLGSTR(IMN_SETCOMPOSITIONFONT),
+ FLGSTR(IMN_SETCOMPOSITIONWINDOW),
+ FLGSTR(IMN_SETCONVERSIONMODE),
+ FLGSTR(IMN_SETOPENSTATUS),
+ FLGSTR(IMN_SETSENTENCEMODE),
+ FLGSTR(IMN_SETSTATUSWINDOWPOS),
+ FLAG_STRING());
+ parameters.sprintf("Command(%s : 0x%p)", imnCommand.toLatin1().data(), (void *)lParam);
+ }
+ break;
+#endif
+#ifdef WM_IME_SETCONTEXT
+ case WM_IME_SETCONTEXT:
+ {
+ bool fSet = (BOOL)wParam;
+ DWORD fShow = (DWORD)lParam;
+ QString showFlgs = flagCheck(fShow,
+#ifdef ISC_SHOWUICOMPOSITIONWINDOW
+ FLGSTR(ISC_SHOWUICOMPOSITIONWINDOW),
+#endif
+#ifdef ISC_SHOWUIGUIDWINDOW
+ FLGSTR(ISC_SHOWUIGUIDWINDOW),
+#endif
+#ifdef ISC_SHOWUISOFTKBD
+ FLGSTR(ISC_SHOWUISOFTKBD),
+#endif
+ FLGSTR(ISC_SHOWUICANDIDATEWINDOW),
+ FLGSTR(ISC_SHOWUICANDIDATEWINDOW << 1),
+ FLGSTR(ISC_SHOWUICANDIDATEWINDOW << 2),
+ FLGSTR(ISC_SHOWUICANDIDATEWINDOW << 3),
+ FLAG_STRING());
+ parameters.sprintf("Input context(%s) Show flags(%s)", (fSet? "Active" : "Inactive"), showFlgs.toLatin1().data());
+ }
+ break;
+#endif
+#ifdef WM_KILLFOCUS
+ case WM_KILLFOCUS:
+ parameters.sprintf("Hwnd gaining keyboard focus (0x%p)", (void *)wParam);
+ break;
+#endif
+#ifdef WM_CHAR
+ case WM_CHAR:
+#endif
+#ifdef WM_IME_CHAR
+ case WM_IME_CHAR:
+#endif
+#ifdef WM_KEYDOWN
+ case WM_KEYDOWN:
+#endif
+#ifdef WM_KEYUP
+ case WM_KEYUP:
+ {
+ int nVirtKey = (int)wParam;
+ long lKeyData = (long)lParam;
+ int repCount = (lKeyData & 0xffff); // Bit 0-15
+ int scanCode = (lKeyData & 0xf0000) >> 16; // Bit 16-23
+ bool contextCode = (lKeyData && 0x20000000); // Bit 29
+ bool prevState = (lKeyData && 0x40000000); // Bit 30
+ bool transState = (lKeyData && 0x80000000); // Bit 31
+ parameters.sprintf("Virual-key(0x%x) Scancode(%d) Rep(%d) Contextcode(%d), Prev state(%d), Trans state(%d)",
+ nVirtKey, scanCode, repCount, contextCode, prevState, transState);
+ }
+ break;
+#endif
+#ifdef WM_NCACTIVATE
+ case WM_NCACTIVATE:
+ {
+ parameters = (msg.wParam? QLatin1String("Active Titlebar") : QLatin1String("Inactive Titlebar"));
+ }
+ break;
+#endif
+#ifdef WM_MOUSEACTIVATE
+ case WM_MOUSEACTIVATE:
+ {
+ QString mouseMsg = QString::fromLatin1(findWMstr(HIWORD(lParam)));
+ parameters.sprintf("TLW(0x%p) HittestCode(0x%x) MouseMsg(%s)", (void *)wParam, LOWORD(lParam), mouseMsg.toLatin1().data());
+ }
+ break;
+#endif
+#ifdef WM_MOUSELEAVE
+ case WM_MOUSELEAVE:
+ break; // wParam & lParam not used
+#endif
+#ifdef WM_MOUSEHOVER
+ case WM_MOUSEHOVER:
+#endif
+#ifdef WM_MOUSEWHEEL
+ case WM_MOUSEWHEEL:
+#endif
+#ifdef WM_MOUSEHWHEEL
+ case WM_MOUSEHWHEEL:
+#endif
+#ifdef WM_LBUTTONDBLCLK
+ case WM_LBUTTONDBLCLK:
+#endif
+#ifdef WM_LBUTTONDOWN
+ case WM_LBUTTONDOWN:
+#endif
+#ifdef WM_LBUTTONUP
+ case WM_LBUTTONUP:
+#endif
+#ifdef WM_MBUTTONDBLCLK
+ case WM_MBUTTONDBLCLK:
+#endif
+#ifdef WM_MBUTTONDOWN
+ case WM_MBUTTONDOWN:
+#endif
+#ifdef WM_MBUTTONUP
+ case WM_MBUTTONUP:
+#endif
+#ifdef WM_RBUTTONDBLCLK
+ case WM_RBUTTONDBLCLK:
+#endif
+#ifdef WM_RBUTTONDOWN
+ case WM_RBUTTONDOWN:
+#endif
+#ifdef WM_RBUTTONUP
+ case WM_RBUTTONUP:
+#endif
+#ifdef WM_MOUSEMOVE
+ case WM_MOUSEMOVE:
+ {
+ QString vrtKeys = flagCheck(wParam,
+ FLGSTR(MK_CONTROL),
+ FLGSTR(MK_LBUTTON),
+ FLGSTR(MK_MBUTTON),
+ FLGSTR(MK_RBUTTON),
+ FLGSTR(MK_SHIFT),
+#ifdef MK_XBUTTON1
+ FLGSTR(MK_XBUTTON1),
+#endif
+#ifdef MK_XBUTTON2
+ FLGSTR(MK_XBUTTON2),
+#endif
+ FLAG_STRING());
+ parameters.sprintf("x,y(%4d,%4d) Virtual Keys(%s)", GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), vrtKeys.toLatin1().data());
+ }
+ break;
+#endif
+#ifdef WM_MOVE
+ case WM_MOVE:
+ parameters.sprintf("x,y(%4d,%4d)", LOWORD(lParam), HIWORD(lParam));
+ break;
+#endif
+#if defined(WM_PAINT) && defined(WM_ERASEBKGND)
+ case WM_ERASEBKGND:
+ case WM_PAINT:
+ parameters.sprintf("hdc(0x%p)", (void *)wParam);
+ break;
+#endif
+#ifdef WM_QUERYNEWPALETTE
+ case WM_QUERYNEWPALETTE:
+ break; // lParam & wParam are unused
+#endif
+#ifdef WM_SETCURSOR
+ case WM_SETCURSOR:
+ {
+ QString mouseMsg = QString::fromLatin1(findWMstr(HIWORD(lParam)));
+ parameters.sprintf("HitTestCode(0x%x) MouseMsg(%s)", LOWORD(lParam), mouseMsg.toLatin1().data());
+ }
+ break;
+#endif
+#ifdef WM_SETFOCUS
+ case WM_SETFOCUS:
+ parameters.sprintf("Lost Focus (0x%p)", (void *)wParam);
+ break;
+#endif
+#ifdef WM_SETTEXT
+ case WM_SETTEXT:
+ parameters.sprintf("Set Text (%s)", QString((QChar*)lParam, (int)wcslen(reinterpret_cast<const wchar_t *>(lParam))).toLatin1().data()); //Unicode string
+ break;
+#endif
+#ifdef WM_SIZE
+ case WM_SIZE:
+ {
+ QString showMode = valueCheck(wParam,
+ FLGSTR(SIZE_MAXHIDE),
+ FLGSTR(SIZE_MAXIMIZED),
+ FLGSTR(SIZE_MAXSHOW),
+ FLGSTR(SIZE_MINIMIZED),
+ FLGSTR(SIZE_RESTORED),
+ FLAG_STRING());
+
+ parameters.sprintf("w,h(%4d,%4d) showmode(%s)", LOWORD(lParam), HIWORD(lParam), showMode.toLatin1().data());
+ }
+ break;
+#endif
+#ifdef WM_WINDOWPOSCHANGED
+ case WM_WINDOWPOSCHANGED:
+ {
+ LPWINDOWPOS winPos = (LPWINDOWPOS)lParam;
+ if (!winPos)
+ break;
+ QString hwndAfter = valueCheck(quint64(winPos->hwndInsertAfter),
+ FLAG_STRING((qptrdiff)HWND_BOTTOM, "HWND_BOTTOM"),
+ FLAG_STRING((qptrdiff)HWND_NOTOPMOST, "HWND_NOTOPMOST"),
+ FLAG_STRING((qptrdiff)HWND_TOP, "HWND_TOP"),
+ FLAG_STRING((qptrdiff)HWND_TOPMOST, "HWND_TOPMOST"),
+ FLAG_STRING());
+ if (hwndAfter.isEmpty())
+ hwndAfter = QString::number((quintptr)winPos->hwndInsertAfter, 16);
+ QString flags = flagCheck(winPos->flags,
+ FLGSTR(SWP_DRAWFRAME),
+ FLGSTR(SWP_FRAMECHANGED),
+ FLGSTR(SWP_HIDEWINDOW),
+ FLGSTR(SWP_NOACTIVATE),
+#ifdef SWP_NOCOPYBITS
+ FLGSTR(SWP_NOCOPYBITS),
+#endif
+ FLGSTR(SWP_NOMOVE),
+ FLGSTR(SWP_NOOWNERZORDER),
+ FLGSTR(SWP_NOREDRAW),
+ FLGSTR(SWP_NOREPOSITION),
+#ifdef SWP_NOSENDCHANGING
+ FLGSTR(SWP_NOSENDCHANGING),
+#endif
+ FLGSTR(SWP_NOSIZE),
+ FLGSTR(SWP_NOZORDER),
+ FLGSTR(SWP_SHOWWINDOW),
+ FLAG_STRING());
+ parameters.sprintf("x,y(%4d,%4d) w,h(%4d,%4d) flags(%s) hwndAfter(%s)", winPos->x, winPos->y, winPos->cx, winPos->cy, flags.toLatin1().data(), hwndAfter.toLatin1().data());
+ }
+ break;
+#endif
+ default:
+ parameters.sprintf("wParam(0x%p) lParam(0x%p)", (void *)wParam, (void *)lParam);
+ break;
+ }
+ // Yes, we want to give the WM_ names 20 chars of space before showing the
+ // decoded message, since some of the common messages are quite long, and
+ // we don't want the decoded information to vary in output position
+ QString message = QString::fromLatin1("%1: ").arg(wmmsg, 20);
+ message += rawParameters;
+ message += parameters;
+ return message;
+}
+
+#endif
+
+QDebug operator<<(QDebug dbg, const MSG &msg)
+{
+ dbg << decodeMSG(msg);
+ return dbg.nospace();
+}
+#endif
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcorecmdlineargs_p.h b/src/corelib/kernel/qcorecmdlineargs_p.h
new file mode 100644
index 0000000000..cdde7828f7
--- /dev/null
+++ b/src/corelib/kernel/qcorecmdlineargs_p.h
@@ -0,0 +1,171 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCORECMDLINEARGS_P_H
+#define QCORECMDLINEARGS_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qstring.h"
+#include "QtCore/qstringlist.h"
+
+QT_BEGIN_NAMESPACE
+
+#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
+
+QT_BEGIN_INCLUDE_NAMESPACE
+#include "QtCore/qvector.h"
+#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
+# include "qt_windows.h"
+#endif
+QT_END_INCLUDE_NAMESPACE
+
+// template implementation of the parsing algorithm
+// this is used from qcoreapplication_win.cpp and the tools (rcc, uic...)
+
+template<typename Char>
+static QVector<Char*> qWinCmdLine(Char *cmdParam, int length, int &argc)
+{
+ QVector<Char*> argv(8);
+ Char *p = cmdParam;
+ Char *p_end = p + length;
+
+ argc = 0;
+
+ while (*p && p < p_end) { // parse cmd line arguments
+ while (QChar((short)(*p)).isSpace()) // skip white space
+ p++;
+ if (*p && p < p_end) { // arg starts
+ int quote;
+ Char *start, *r;
+ if (*p == Char('\"') || *p == Char('\'')) { // " or ' quote
+ quote = *p;
+ start = ++p;
+ } else {
+ quote = 0;
+ start = p;
+ }
+ r = start;
+ while (*p && p < p_end) {
+ if (quote) {
+ if (*p == quote) {
+ p++;
+ if (QChar((short)(*p)).isSpace())
+ break;
+ quote = 0;
+ }
+ }
+ if (*p == '\\') { // escape char?
+ p++;
+ if (*p == Char('\"') || *p == Char('\''))
+ ; // yes
+ else
+ p--; // treat \ literally
+ } else {
+ if (!quote && (*p == Char('\"') || *p == Char('\''))) { // " or ' quote
+ quote = *p++;
+ continue;
+ } else if (QChar((short)(*p)).isSpace() && !quote)
+ break;
+ }
+ if (*p)
+ *r++ = *p++;
+ }
+ if (*p && p < p_end)
+ p++;
+ *r = Char('\0');
+
+ if (argc >= (int)argv.size()-1) // expand array
+ argv.resize(argv.size()*2);
+ argv[argc++] = start;
+ }
+ }
+ argv[argc] = 0;
+
+ return argv;
+}
+
+#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
+static inline QStringList qWinCmdArgs(QString cmdLine) // not const-ref: this might be modified
+{
+ QStringList args;
+
+ int argc = 0;
+ QVector<wchar_t*> argv = qWinCmdLine<wchar_t>((wchar_t *)cmdLine.utf16(), cmdLine.length(), argc);
+ for (int a = 0; a < argc; ++a) {
+ args << QString::fromWCharArray(argv[a]);
+ }
+
+ return args;
+}
+
+static inline QStringList qCmdLineArgs(int argc, char *argv[])
+{
+ Q_UNUSED(argc)
+ Q_UNUSED(argv)
+ QString cmdLine = QString::fromWCharArray(GetCommandLine());
+ return qWinCmdArgs(cmdLine);
+}
+#endif
+#else // !Q_OS_WIN || !Q_OS_SYMBIAN
+
+static inline QStringList qCmdLineArgs(int argc, char *argv[])
+{
+ QStringList args;
+ for (int i = 0; i != argc; ++i)
+ args += QString::fromLocal8Bit(argv[i]);
+ return args;
+}
+
+#endif // Q_OS_WIN || Q_OS_SYMBIAN
+
+QT_END_NAMESPACE
+
+#endif // QCORECMDLINEARGS_WIN_P_H
diff --git a/src/corelib/kernel/qcoreevent.cpp b/src/corelib/kernel/qcoreevent.cpp
new file mode 100644
index 0000000000..fbb08faaf9
--- /dev/null
+++ b/src/corelib/kernel/qcoreevent.cpp
@@ -0,0 +1,612 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcoreevent.h"
+#include "qcoreapplication.h"
+#include "qcoreapplication_p.h"
+
+#include "qmutex.h"
+#include "qset.h"
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \class QEvent
+ \brief The QEvent class is the base class of all
+ event classes. Event objects contain event parameters.
+
+ \ingroup events
+
+ Qt's main event loop (QCoreApplication::exec()) fetches native
+ window system events from the event queue, translates them into
+ QEvents, and sends the translated events to \l{QObject}s.
+
+ In general, events come from the underlying window system
+ (spontaneous() returns true), but it is also possible to manually
+ send events using QCoreApplication::sendEvent() and
+ QCoreApplication::postEvent() (spontaneous() returns false).
+
+ QObjects receive events by having their QObject::event() function
+ called. The function can be reimplemented in subclasses to
+ customize event handling and add additional event types;
+ QWidget::event() is a notable example. By default, events are
+ dispatched to event handlers like QObject::timerEvent() and
+ QWidget::mouseMoveEvent(). QObject::installEventFilter() allows an
+ object to intercept events destined for another object.
+
+ The basic QEvent contains only an event type parameter and an
+ "accept" flag. The accept flag set with accept(), and cleared
+ with ignore(). It is set by default, but don't rely on this as
+ subclasses may choose to clear it in their constructor.
+
+ Subclasses of QEvent contain additional parameters that describe
+ the particular event.
+
+ \sa QObject::event(), QObject::installEventFilter(),
+ QWidget::event(), QCoreApplication::sendEvent(),
+ QCoreApplication::postEvent(), QCoreApplication::processEvents()
+*/
+
+
+/*!
+ \enum QEvent::Type
+
+ This enum type defines the valid event types in Qt. The event
+ types and the specialized classes for each type are as follows:
+
+ \value None Not an event.
+ \value AccessibilityDescription Used to query accessibility description texts (QAccessibleEvent).
+ \value AccessibilityHelp Used to query accessibility help texts (QAccessibleEvent).
+ \value AccessibilityPrepare Accessibility information is requested.
+ \value ActionAdded A new action has been added (QActionEvent).
+ \value ActionChanged An action has been changed (QActionEvent).
+ \value ActionRemoved An action has been removed (QActionEvent).
+ \value ActivationChange A widget's top-level window activation state has changed.
+ \value ApplicationActivate The application has been made available to the user.
+ \value ApplicationActivated This enum has been deprecated. Use ApplicationActivate instead.
+ \value ApplicationDeactivate The application has been suspended, and is unavailable to the user.
+ \value ApplicationFontChange The default application font has changed.
+ \value ApplicationLayoutDirectionChange The default application layout direction has changed.
+ \value ApplicationPaletteChange The default application palette has changed.
+ \value ApplicationWindowIconChange The application's icon has changed.
+ \value ChildAdded An object gets a child (QChildEvent).
+ \value ChildInserted An object gets a child (QChildEvent). Qt3Support only, use ChildAdded instead.
+ \value ChildPolished A widget child gets polished (QChildEvent).
+ \value ChildRemoved An object loses a child (QChildEvent).
+ \value Clipboard The clipboard contents have changed (QClipboardEvent).
+ \value Close Widget was closed (QCloseEvent).
+ \value CloseSoftwareInputPanel A widget wants to close the software input panel (SIP).
+ \value ContentsRectChange The margins of the widget's content rect changed.
+ \value ContextMenu Context popup menu (QContextMenuEvent).
+ \value CursorChange The widget's cursor has changed.
+ \value DeferredDelete The object will be deleted after it has cleaned up.
+ \value DragEnter The cursor enters a widget during a drag and drop operation (QDragEnterEvent).
+ \value DragLeave The cursor leaves a widget during a drag and drop operation (QDragLeaveEvent).
+ \value DragMove A drag and drop operation is in progress (QDragMoveEvent).
+ \value Drop A drag and drop operation is completed (QDropEvent).
+ \value EnabledChange Widget's enabled state has changed.
+ \value Enter Mouse enters widget's boundaries.
+ \value EnterEditFocus An editor widget gains focus for editing.
+ \value EnterWhatsThisMode Send to toplevel widgets when the application enters "What's This?" mode.
+ \value FileOpen File open request (QFileOpenEvent).
+ \value FocusIn Widget gains keyboard focus (QFocusEvent).
+ \value FocusOut Widget loses keyboard focus (QFocusEvent).
+ \value FontChange Widget's font has changed.
+ \value GrabKeyboard Item gains keyboard grab (QGraphicsItem only).
+ \value GrabMouse Item gains mouse grab (QGraphicsItem only).
+ \value GraphicsSceneContextMenu Context popup menu over a graphics scene (QGraphicsSceneContextMenuEvent).
+ \value GraphicsSceneDragEnter The cursor enters a graphics scene during a drag and drop operation (QGraphicsSceneDragDropEvent).
+ \value GraphicsSceneDragLeave The cursor leaves a graphics scene during a drag and drop operation (QGraphicsSceneDragDropEvent).
+ \value GraphicsSceneDragMove A drag and drop operation is in progress over a scene (QGraphicsSceneDragDropEvent).
+ \value GraphicsSceneDrop A drag and drop operation is completed over a scene (QGraphicsSceneDragDropEvent).
+ \value GraphicsSceneHelp The user requests help for a graphics scene (QHelpEvent).
+ \value GraphicsSceneHoverEnter The mouse cursor enters a hover item in a graphics scene (QGraphicsSceneHoverEvent).
+ \value GraphicsSceneHoverLeave The mouse cursor leaves a hover item in a graphics scene (QGraphicsSceneHoverEvent).
+ \value GraphicsSceneHoverMove The mouse cursor moves inside a hover item in a graphics scene (QGraphicsSceneHoverEvent).
+ \value GraphicsSceneMouseDoubleClick Mouse press again (double click) in a graphics scene (QGraphicsSceneMouseEvent).
+ \value GraphicsSceneMouseMove Move mouse in a graphics scene (QGraphicsSceneMouseEvent).
+ \value GraphicsSceneMousePress Mouse press in a graphics scene (QGraphicsSceneMouseEvent).
+ \value GraphicsSceneMouseRelease Mouse release in a graphics scene (QGraphicsSceneMouseEvent).
+ \value GraphicsSceneMove Widget was moved (QGraphicsSceneMoveEvent).
+ \value GraphicsSceneResize Widget was resized (QGraphicsSceneResizeEvent).
+ \value GraphicsSceneWheel Mouse wheel rolled in a graphics scene (QGraphicsSceneWheelEvent).
+ \value Hide Widget was hidden (QHideEvent).
+ \value HideToParent A child widget has been hidden.
+ \value HoverEnter The mouse cursor enters a hover widget (QHoverEvent).
+ \value HoverLeave The mouse cursor leaves a hover widget (QHoverEvent).
+ \value HoverMove The mouse cursor moves inside a hover widget (QHoverEvent).
+ \value IconDrag The main icon of a window has been dragged away (QIconDragEvent).
+ \value IconTextChange Widget's icon text has been changed.
+ \value InputMethod An input method is being used (QInputMethodEvent).
+ \value KeyPress Key press (QKeyEvent).
+ \value KeyRelease Key release (QKeyEvent).
+ \value LanguageChange The application translation changed.
+ \value LayoutDirectionChange The direction of layouts changed.
+ \value LayoutRequest Widget layout needs to be redone.
+ \value Leave Mouse leaves widget's boundaries.
+ \value LeaveEditFocus An editor widget loses focus for editing.
+ \value LeaveWhatsThisMode Send to toplevel widgets when the application leaves "What's This?" mode.
+ \value LocaleChange The system locale has changed.
+ \value NonClientAreaMouseButtonDblClick A mouse double click occurred outside the client area.
+ \value NonClientAreaMouseButtonPress A mouse button press occurred outside the client area.
+ \value NonClientAreaMouseButtonRelease A mouse button release occurred outside the client area.
+ \value NonClientAreaMouseMove A mouse move occurred outside the client area.
+ \value MacSizeChange The user changed his widget sizes (Mac OS X only).
+ \value MenubarUpdated The window's menu bar has been updated.
+ \value MetaCall An asynchronous method invocation via QMetaObject::invokeMethod().
+ \value ModifiedChange Widgets modification state has been changed.
+ \value MouseButtonDblClick Mouse press again (QMouseEvent).
+ \value MouseButtonPress Mouse press (QMouseEvent).
+ \value MouseButtonRelease Mouse release (QMouseEvent).
+ \value MouseMove Mouse move (QMouseEvent).
+ \value MouseTrackingChange The mouse tracking state has changed.
+ \value Move Widget's position changed (QMoveEvent).
+ \value Paint Screen update necessary (QPaintEvent).
+ \value PaletteChange Palette of the widget changed.
+ \value ParentAboutToChange The widget parent is about to change.
+ \value ParentChange The widget parent has changed.
+ \value Polish The widget is polished.
+ \value PolishRequest The widget should be polished.
+ \value QueryWhatsThis The widget should accept the event if it has "What's This?" help.
+ \value RequestSoftwareInputPanel A widget wants to open a software input panel (SIP).
+ \value Resize Widget's size changed (QResizeEvent).
+ \value Shortcut Key press in child for shortcut key handling (QShortcutEvent).
+ \value ShortcutOverride Key press in child, for overriding shortcut key handling (QKeyEvent).
+ \value Show Widget was shown on screen (QShowEvent).
+ \value ShowToParent A child widget has been shown.
+ \value SockAct Socket activated, used to implement QSocketNotifier.
+ \value StateMachineSignal A signal delivered to a state machine (QStateMachine::SignalEvent).
+ \value StateMachineWrapped The event is a wrapper for, i.e., contains, another event (QStateMachine::WrappedEvent).
+ \value StatusTip A status tip is requested (QStatusTipEvent).
+ \value StyleChange Widget's style has been changed.
+ \value TabletMove Wacom tablet move (QTabletEvent).
+ \value TabletPress Wacom tablet press (QTabletEvent).
+ \value TabletRelease Wacom tablet release (QTabletEvent).
+ \value OkRequest Ok button in decoration pressed. Supported only for Windows CE.
+ \value TabletEnterProximity Wacom tablet enter proximity event (QTabletEvent), sent to QApplication.
+ \value TabletLeaveProximity Wacom tablet leave proximity event (QTabletEvent), sent to QApplication.
+ \value Timer Regular timer events (QTimerEvent).
+ \value ToolBarChange The toolbar button is toggled on Mac OS X.
+ \value ToolTip A tooltip was requested (QHelpEvent).
+ \value ToolTipChange The widget's tooltip has changed.
+ \value UngrabKeyboard Item loses keyboard grab (QGraphicsItem only).
+ \value UngrabMouse Item loses mouse grab (QGraphicsItem only).
+ \value UpdateLater The widget should be queued to be repainted at a later time.
+ \value UpdateRequest The widget should be repainted.
+ \value WhatsThis The widget should reveal "What's This?" help (QHelpEvent).
+ \value WhatsThisClicked A link in a widget's "What's This?" help was clicked.
+ \value Wheel Mouse wheel rolled (QWheelEvent).
+ \value WinEventAct A Windows-specific activation event has occurred.
+ \value WindowActivate Window was activated.
+ \value WindowBlocked The window is blocked by a modal dialog.
+ \value WindowDeactivate Window was deactivated.
+ \value WindowIconChange The window's icon has changed.
+ \value WindowStateChange The \l{QWidget::windowState()}{window's state} (minimized, maximized or full-screen) has changed (QWindowStateChangeEvent).
+ \value WindowTitleChange The window title has changed.
+ \value WindowUnblocked The window is unblocked after a modal dialog exited.
+ \value ZOrderChange The widget's z-order has changed. This event is never sent to top level windows.
+ \value KeyboardLayoutChange The keyboard layout has changed.
+ \value DynamicPropertyChange A dynamic property was added, changed or removed from the object.
+ \value TouchBegin Beginning of a sequence of touch-screen and/or track-pad events (QTouchEvent)
+ \value TouchUpdate Touch-screen event (QTouchEvent)
+ \value TouchEnd End of touch-event sequence (QTouchEvent)
+ \value WinIdChange The window system identifer for this native widget has changed
+ \value Gesture A gesture was triggered (QGestureEvent)
+ \value GestureOverride A gesture override was triggered (QGestureEvent)
+ \value ScrollPrepare The object needs to fill in its geometry information (QScrollPrepareEvent)
+ \value Scroll The object needs to scroll to the supplied position (QScrollEvent)
+
+ User events should have values between \c User and \c{MaxUser}:
+
+ \value User User-defined event.
+ \value MaxUser Last user event ID.
+
+ For convenience, you can use the registerEventType() function to
+ register and reserve a custom event type for your
+ application. Doing so will allow you to avoid accidentally
+ re-using a custom event type already in use elsewhere in your
+ application.
+
+ \omitvalue Accel
+ \omitvalue AccelAvailable
+ \omitvalue AccelOverride
+ \omitvalue AcceptDropsChange
+ \omitvalue ActivateControl
+ \omitvalue CaptionChange
+ \omitvalue ChildInsertedRequest
+ \omitvalue ChildInserted
+ \omitvalue Create
+ \omitvalue DeactivateControl
+ \omitvalue Destroy
+ \omitvalue DragResponse
+ \omitvalue EmbeddingControl
+ \omitvalue HelpRequest
+ \omitvalue IconChange
+ \omitvalue LayoutHint
+ \omitvalue Quit
+ \omitvalue Reparent
+ \omitvalue ShowWindowRequest
+ \omitvalue Speech
+ \omitvalue Style
+ \omitvalue ThreadChange
+ \omitvalue ZeroTimerEvent
+ \omitvalue ApplicationActivated
+ \omitvalue ApplicationDeactivated
+ \omitvalue MacGLWindowChange
+ \omitvalue MacGLClearDrawable
+ \omitvalue NetworkReplyUpdated
+ \omitvalue FutureCallOut
+ \omitvalue UpdateSoftKeys
+ \omitvalue NativeGesture
+*/
+
+/*!
+ Contructs an event object of type \a type.
+*/
+QEvent::QEvent(Type type)
+ : d(0), t(type), posted(false), spont(false), m_accept(true)
+{}
+
+/*!
+ Destroys the event. If it was \link
+ QCoreApplication::postEvent() posted \endlink,
+ it will be removed from the list of events to be posted.
+*/
+
+QEvent::~QEvent()
+{
+ if (posted && QCoreApplication::instance())
+ QCoreApplicationPrivate::removePostedEvent(this);
+}
+
+
+/*!
+ \property QEvent::accepted
+ the accept flag of the event object
+
+ Setting the accept parameter indicates that the event receiver
+ wants the event. Unwanted events might be propagated to the parent
+ widget. By default, isAccepted() is set to true, but don't rely on
+ this as subclasses may choose to clear it in their constructor.
+
+ For convenience, the accept flag can also be set with accept(),
+ and cleared with ignore().
+*/
+
+/*!
+ \fn void QEvent::accept()
+
+ Sets the accept flag of the event object, the equivalent of
+ calling setAccepted(true).
+
+ Setting the accept parameter indicates that the event receiver
+ wants the event. Unwanted events might be propagated to the parent
+ widget.
+
+ \sa ignore()
+*/
+
+
+/*!
+ \fn void QEvent::ignore()
+
+ Clears the accept flag parameter of the event object, the
+ equivalent of calling setAccepted(false).
+
+ Clearing the accept parameter indicates that the event receiver
+ does not want the event. Unwanted events might be propagated to the
+ parent widget.
+
+ \sa accept()
+*/
+
+
+/*!
+ \fn QEvent::Type QEvent::type() const
+
+ Returns the event type.
+*/
+
+/*!
+ \fn bool QEvent::spontaneous() const
+
+ Returns true if the event originated outside the application (a
+ system event); otherwise returns false.
+
+ The return value of this function is not defined for paint events.
+*/
+
+class QEventUserEventRegistration
+{
+public:
+ QMutex mutex;
+ QSet<int> set;
+};
+Q_GLOBAL_STATIC(QEventUserEventRegistration, userEventRegistrationHelper)
+
+/*!
+ \since 4.4
+ \threadsafe
+
+ Registers and returns a custom event type. The \a hint provided
+ will be used if it is available, otherwise it will return a value
+ between QEvent::User and QEvent::MaxUser that has not yet been
+ registered. The \a hint is ignored if its value is not between
+ QEvent::User and QEvent::MaxUser.
+*/
+int QEvent::registerEventType(int hint)
+{
+ QEventUserEventRegistration *userEventRegistration
+ = userEventRegistrationHelper();
+ if (!userEventRegistration)
+ return -1;
+
+ QMutexLocker locker(&userEventRegistration->mutex);
+
+ // if the type hint hasn't been registered yet, take it
+ if (hint >= QEvent::User && hint <= QEvent::MaxUser && !userEventRegistration->set.contains(hint)) {
+ userEventRegistration->set.insert(hint);
+ return hint;
+ }
+
+ // find a free event type, starting at MaxUser and decreasing
+ int id = QEvent::MaxUser;
+ while (userEventRegistration->set.contains(id) && id >= QEvent::User)
+ --id;
+ if (id >= QEvent::User) {
+ userEventRegistration->set.insert(id);
+ return id;
+ }
+ return -1;
+}
+
+/*!
+ \class QTimerEvent
+ \brief The QTimerEvent class contains parameters that describe a
+ timer event.
+
+ \ingroup events
+
+ Timer events are sent at regular intervals to objects that have
+ started one or more timers. Each timer has a unique identifier. A
+ timer is started with QObject::startTimer().
+
+ The QTimer class provides a high-level programming interface that
+ uses signals instead of events. It also provides single-shot timers.
+
+ The event handler QObject::timerEvent() receives timer events.
+
+ \sa QTimer, QObject::timerEvent(), QObject::startTimer(),
+ QObject::killTimer()
+*/
+
+/*!
+ Constructs a timer event object with the timer identifier set to
+ \a timerId.
+*/
+QTimerEvent::QTimerEvent(int timerId)
+ : QEvent(Timer), id(timerId)
+{}
+
+/*! \internal
+*/
+QTimerEvent::~QTimerEvent()
+{
+}
+
+/*!
+ \fn int QTimerEvent::timerId() const
+
+ Returns the unique timer identifier, which is the same identifier
+ as returned from QObject::startTimer().
+*/
+
+/*!
+ \class QChildEvent
+ \brief The QChildEvent class contains event parameters for child object
+ events.
+
+ \ingroup events
+
+ Child events are sent immediately to objects when children are
+ added or removed.
+
+ In both cases you can only rely on the child being a QObject (or,
+ if QObject::isWidgetType() returns true, a QWidget). This is
+ because in the QEvent::ChildAdded case the child is not yet fully
+ constructed; in the QEvent::ChildRemoved case it might have
+ already been destructed.
+
+ The handler for these events is QObject::childEvent().
+*/
+
+/*!
+ Constructs a child event object of a particular \a type for the
+ \a child.
+
+ \a type can be QEvent::ChildAdded, QEvent::ChildRemoved,
+ QEvent::ChildPolished, or QEvent::ChildRemoved.
+
+ \sa child()
+*/
+QChildEvent::QChildEvent(Type type, QObject *child)
+ : QEvent(type), c(child)
+{}
+
+/*! \internal
+*/
+QChildEvent::~QChildEvent()
+{
+}
+
+/*!
+ \fn QObject *QChildEvent::child() const
+
+ Returns the child object that was added or removed.
+*/
+
+/*!
+ \fn bool QChildEvent::added() const
+
+ Returns true if type() is QEvent::ChildAdded; otherwise returns
+ false.
+*/
+
+/*!
+ \fn bool QChildEvent::removed() const
+
+ Returns true if type() is QEvent::ChildRemoved; otherwise returns
+ false.
+*/
+
+/*!
+ \fn bool QChildEvent::polished() const
+
+ Returns true if type() is QEvent::ChildPolished; otherwise returns
+ false.
+*/
+
+/*!
+ \class QCustomEvent
+ \brief The QCustomEvent class provides support for custom events.
+
+ \compat
+
+ QCustomEvent has a \c{void *} that can be used to store custom
+ data.
+
+ In Qt 3, QObject::customEvent() took a QCustomEvent pointer. We
+ found out that this approach was unsatisfactory, because
+ there was often no safe way of deleting the data held in the
+ \c{void *}.
+
+ In Qt 4, QObject::customEvent() takes a plain QEvent pointer.
+ You can add custom data by subclassing.
+
+ \sa QObject::customEvent(), QCoreApplication::notify()
+*/
+
+/*!
+ \fn QCustomEvent::QCustomEvent(int type, void *data)
+
+ Constructs a custom event object with the event \a type and a
+ pointer to \a data. The value of \a type must be at least as
+ large as QEvent::User. By default, the data pointer is set to 0.
+*/
+#ifdef QT3_SUPPORT
+QCustomEvent::QCustomEvent(int type, void *data)
+ : QEvent(static_cast<Type>(type))
+{
+ d = reinterpret_cast<QEventPrivate *>(data);
+}
+
+/*! \internal
+*/
+QCustomEvent::~QCustomEvent()
+{
+}
+#endif
+/*!
+ \fn void QCustomEvent::setData(void *data)
+
+ \compat
+
+ Sets the generic data pointer to \a data.
+
+ \sa data()
+*/
+
+/*!
+ \fn void *QCustomEvent::data() const
+
+ \compat
+
+ Returns a pointer to the generic event data.
+
+ \sa setData()
+*/
+
+/*!
+ \fn bool QChildEvent::inserted() const
+
+ \compat
+
+ A child has been inserted if the event's type() is ChildInserted.
+*/
+
+/*!
+ \class QDynamicPropertyChangeEvent
+ \since 4.2
+ \brief The QDynamicPropertyChangeEvent class contains event parameters for dynamic
+ property change events.
+
+ \ingroup events
+
+ Dynamic property change events are sent to objects when properties are
+ dynamically added, changed or removed using QObject::setProperty().
+*/
+
+/*!
+ Constructs a dynamic property change event object with the property name set to
+ \a name.
+*/
+QDynamicPropertyChangeEvent::QDynamicPropertyChangeEvent(const QByteArray &name)
+ : QEvent(QEvent::DynamicPropertyChange), n(name)
+{
+}
+
+/*!
+ \internal
+*/
+QDynamicPropertyChangeEvent::~QDynamicPropertyChangeEvent()
+{
+}
+
+/*!
+ \fn QByteArray QDynamicPropertyChangeEvent::propertyName() const
+
+ Returns the name of the dynamic property that was added, changed or
+ removed.
+
+ \sa QObject::setProperty(), QObject::dynamicPropertyNames()
+*/
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcoreevent.h b/src/corelib/kernel/qcoreevent.h
new file mode 100644
index 0000000000..c9d311ae35
--- /dev/null
+++ b/src/corelib/kernel/qcoreevent.h
@@ -0,0 +1,396 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCOREEVENT_H
+#define QCOREEVENT_H
+
+#include <QtCore/qnamespace.h>
+#include <QtCore/qbytearray.h>
+#include <QtCore/qobjectdefs.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Core)
+
+class QEventPrivate;
+class Q_CORE_EXPORT QEvent // event base class
+{
+ Q_GADGET
+ QDOC_PROPERTY(bool accepted READ isAccepted WRITE setAccepted)
+ Q_ENUMS(Type)
+public:
+ enum Type {
+ /*
+ If you get a strange compiler error on the line with None,
+ it's probably because you're also including X11 headers,
+ which #define the symbol None. Put the X11 includes after
+ the Qt includes to solve this problem.
+ */
+ None = 0, // invalid event
+ Timer = 1, // timer event
+ MouseButtonPress = 2, // mouse button pressed
+ MouseButtonRelease = 3, // mouse button released
+ MouseButtonDblClick = 4, // mouse button double click
+ MouseMove = 5, // mouse move
+ KeyPress = 6, // key pressed
+ KeyRelease = 7, // key released
+ FocusIn = 8, // keyboard focus received
+ FocusOut = 9, // keyboard focus lost
+ Enter = 10, // mouse enters widget
+ Leave = 11, // mouse leaves widget
+ Paint = 12, // paint widget
+ Move = 13, // move widget
+ Resize = 14, // resize widget
+ Create = 15, // after widget creation
+ Destroy = 16, // during widget destruction
+ Show = 17, // widget is shown
+ Hide = 18, // widget is hidden
+ Close = 19, // request to close widget
+ Quit = 20, // request to quit application
+ ParentChange = 21, // widget has been reparented
+ ParentAboutToChange = 131, // sent just before the parent change is done
+#ifdef QT3_SUPPORT
+ Reparent = ParentChange,
+#endif
+ ThreadChange = 22, // object has changed threads
+ WindowActivate = 24, // window was activated
+ WindowDeactivate = 25, // window was deactivated
+ ShowToParent = 26, // widget is shown to parent
+ HideToParent = 27, // widget is hidden to parent
+ Wheel = 31, // wheel event
+ WindowTitleChange = 33, // window title changed
+ WindowIconChange = 34, // icon changed
+ ApplicationWindowIconChange = 35, // application icon changed
+ ApplicationFontChange = 36, // application font changed
+ ApplicationLayoutDirectionChange = 37, // application layout direction changed
+ ApplicationPaletteChange = 38, // application palette changed
+ PaletteChange = 39, // widget palette changed
+ Clipboard = 40, // internal clipboard event
+ Speech = 42, // reserved for speech input
+ MetaCall = 43, // meta call event
+ SockAct = 50, // socket activation
+ WinEventAct = 132, // win event activation
+ DeferredDelete = 52, // deferred delete event
+ DragEnter = 60, // drag moves into widget
+ DragMove = 61, // drag moves in widget
+ DragLeave = 62, // drag leaves or is cancelled
+ Drop = 63, // actual drop
+ DragResponse = 64, // drag accepted/rejected
+ ChildAdded = 68, // new child widget
+ ChildPolished = 69, // polished child widget
+#ifdef QT3_SUPPORT
+ ChildInsertedRequest = 67, // send ChildInserted compatibility events to receiver
+ ChildInserted = 70, // compatibility child inserted
+ LayoutHint = 72, // compatibility relayout request
+#endif
+ ChildRemoved = 71, // deleted child widget
+ ShowWindowRequest = 73, // widget's window should be mapped
+ PolishRequest = 74, // widget should be polished
+ Polish = 75, // widget is polished
+ LayoutRequest = 76, // widget should be relayouted
+ UpdateRequest = 77, // widget should be repainted
+ UpdateLater = 78, // request update() later
+
+ EmbeddingControl = 79, // ActiveX embedding
+ ActivateControl = 80, // ActiveX activation
+ DeactivateControl = 81, // ActiveX deactivation
+ ContextMenu = 82, // context popup menu
+ InputMethod = 83, // input method
+ AccessibilityPrepare = 86, // accessibility information is requested
+ TabletMove = 87, // Wacom tablet event
+ LocaleChange = 88, // the system locale changed
+ LanguageChange = 89, // the application language changed
+ LayoutDirectionChange = 90, // the layout direction changed
+ Style = 91, // internal style event
+ TabletPress = 92, // tablet press
+ TabletRelease = 93, // tablet release
+ OkRequest = 94, // CE (Ok) button pressed
+ HelpRequest = 95, // CE (?) button pressed
+
+ IconDrag = 96, // proxy icon dragged
+
+ FontChange = 97, // font has changed
+ EnabledChange = 98, // enabled state has changed
+ ActivationChange = 99, // window activation has changed
+ StyleChange = 100, // style has changed
+ IconTextChange = 101, // icon text has changed
+ ModifiedChange = 102, // modified state has changed
+ MouseTrackingChange = 109, // mouse tracking state has changed
+
+ WindowBlocked = 103, // window is about to be blocked modally
+ WindowUnblocked = 104, // windows modal blocking has ended
+ WindowStateChange = 105,
+
+ ToolTip = 110,
+ WhatsThis = 111,
+ StatusTip = 112,
+
+ ActionChanged = 113,
+ ActionAdded = 114,
+ ActionRemoved = 115,
+
+ FileOpen = 116, // file open request
+
+ Shortcut = 117, // shortcut triggered
+ ShortcutOverride = 51, // shortcut override request
+
+#ifdef QT3_SUPPORT
+ Accel = 30, // accelerator event
+ AccelAvailable = 32, // accelerator available event
+ AccelOverride = ShortcutOverride, // accelerator override event
+#endif
+
+ WhatsThisClicked = 118,
+
+#ifdef QT3_SUPPORT
+ CaptionChange = WindowTitleChange,
+ IconChange = WindowIconChange,
+#endif
+ ToolBarChange = 120, // toolbar visibility toggled
+
+ ApplicationActivate = 121, // application has been changed to active
+ ApplicationActivated = ApplicationActivate, // deprecated
+ ApplicationDeactivate = 122, // application has been changed to inactive
+ ApplicationDeactivated = ApplicationDeactivate, // deprecated
+
+ QueryWhatsThis = 123, // query what's this widget help
+ EnterWhatsThisMode = 124,
+ LeaveWhatsThisMode = 125,
+
+ ZOrderChange = 126, // child widget has had its z-order changed
+
+ HoverEnter = 127, // mouse cursor enters a hover widget
+ HoverLeave = 128, // mouse cursor leaves a hover widget
+ HoverMove = 129, // mouse cursor move inside a hover widget
+
+ AccessibilityHelp = 119, // accessibility help text request
+ AccessibilityDescription = 130, // accessibility description text request
+
+ // last event id used = 132
+
+#ifdef QT_KEYPAD_NAVIGATION
+ EnterEditFocus = 150, // enter edit mode in keypad navigation
+ LeaveEditFocus = 151, // enter edit mode in keypad navigation
+#endif
+ AcceptDropsChange = 152,
+
+ MenubarUpdated = 153, // Support event for Q3MainWindow, which needs to
+ // knwow when QMenubar is updated.
+
+ ZeroTimerEvent = 154, // Used for Windows Zero timer events
+
+ GraphicsSceneMouseMove = 155, // GraphicsView
+ GraphicsSceneMousePress = 156,
+ GraphicsSceneMouseRelease = 157,
+ GraphicsSceneMouseDoubleClick = 158,
+ GraphicsSceneContextMenu = 159,
+ GraphicsSceneHoverEnter = 160,
+ GraphicsSceneHoverMove = 161,
+ GraphicsSceneHoverLeave = 162,
+ GraphicsSceneHelp = 163,
+ GraphicsSceneDragEnter = 164,
+ GraphicsSceneDragMove = 165,
+ GraphicsSceneDragLeave = 166,
+ GraphicsSceneDrop = 167,
+ GraphicsSceneWheel = 168,
+
+ KeyboardLayoutChange = 169, // keyboard layout changed
+
+ DynamicPropertyChange = 170, // A dynamic property was changed through setProperty/property
+
+ TabletEnterProximity = 171,
+ TabletLeaveProximity = 172,
+
+ NonClientAreaMouseMove = 173,
+ NonClientAreaMouseButtonPress = 174,
+ NonClientAreaMouseButtonRelease = 175,
+ NonClientAreaMouseButtonDblClick = 176,
+
+ MacSizeChange = 177, // when the Qt::WA_Mac{Normal,Small,Mini}Size changes
+
+ ContentsRectChange = 178, // sent by QWidget::setContentsMargins (internal)
+
+ MacGLWindowChange = 179, // Internal! the window of the GLWidget has changed
+
+ FutureCallOut = 180,
+
+ GraphicsSceneResize = 181,
+ GraphicsSceneMove = 182,
+
+ CursorChange = 183,
+ ToolTipChange = 184,
+
+ NetworkReplyUpdated = 185, // Internal for QNetworkReply
+
+ GrabMouse = 186,
+ UngrabMouse = 187,
+ GrabKeyboard = 188,
+ UngrabKeyboard = 189,
+ MacGLClearDrawable = 191, // Internal Cocoa, the window has changed, so we must clear
+
+ StateMachineSignal = 192,
+ StateMachineWrapped = 193,
+
+ TouchBegin = 194,
+ TouchUpdate = 195,
+ TouchEnd = 196,
+
+#ifndef QT_NO_GESTURES
+ NativeGesture = 197, // Internal for platform gesture support
+#endif
+ RequestSoftwareInputPanel = 199,
+ CloseSoftwareInputPanel = 200,
+
+ UpdateSoftKeys = 201, // Internal for compressing soft key updates
+
+ WinIdChange = 203,
+#ifndef QT_NO_GESTURES
+ Gesture = 198,
+ GestureOverride = 202,
+#endif
+ ScrollPrepare = 204,
+ Scroll = 205,
+
+ // 512 reserved for Qt Jambi's MetaCall event
+ // 513 reserved for Qt Jambi's DeleteOnMainThread event
+
+ User = 1000, // first user event id
+ MaxUser = 65535 // last user event id
+ };
+
+ QEvent(Type type);
+ virtual ~QEvent();
+ inline Type type() const { return static_cast<Type>(t); }
+ inline bool spontaneous() const { return spont; }
+
+ inline void setAccepted(bool accepted) { m_accept = accepted; }
+ inline bool isAccepted() const { return m_accept; }
+
+ inline void accept() { m_accept = true; }
+ inline void ignore() { m_accept = false; }
+
+ static int registerEventType(int hint = -1);
+
+protected:
+ QEventPrivate *d;
+ ushort t;
+
+private:
+ ushort posted : 1;
+ ushort spont : 1;
+ ushort m_accept : 1;
+ ushort reserved : 13;
+
+ friend class QCoreApplication;
+ friend class QCoreApplicationPrivate;
+ friend class QThreadData;
+ friend class QApplication;
+ friend class QApplicationPrivate;
+ friend class Q3AccelManager;
+ friend class QShortcutMap;
+ friend class QETWidget;
+ friend class QGraphicsView;
+ friend class QGraphicsViewPrivate;
+ friend class QGraphicsScene;
+ friend class QGraphicsScenePrivate;
+#ifndef QT_NO_GESTURES
+ friend class QGestureManager;
+#endif
+};
+
+class Q_CORE_EXPORT QTimerEvent : public QEvent
+{
+public:
+ QTimerEvent( int timerId );
+ ~QTimerEvent();
+ int timerId() const { return id; }
+protected:
+ int id;
+};
+
+class QObject;
+
+class Q_CORE_EXPORT QChildEvent : public QEvent
+{
+public:
+ QChildEvent( Type type, QObject *child );
+ ~QChildEvent();
+ QObject *child() const { return c; }
+ bool added() const { return type() == ChildAdded; }
+#ifdef QT3_SUPPORT
+ QT3_SUPPORT bool inserted() const { return type() == ChildInserted; }
+#endif
+ bool polished() const { return type() == ChildPolished; }
+ bool removed() const { return type() == ChildRemoved; }
+protected:
+ QObject *c;
+};
+
+#ifdef QT3_SUPPORT
+class Q_CORE_EXPORT QCustomEvent : public QEvent
+{
+public:
+ QT3_SUPPORT_CONSTRUCTOR QCustomEvent(int type, void *data = 0);
+ ~QCustomEvent();
+ QT3_SUPPORT void *data() const { return d; }
+ QT3_SUPPORT void setData(void* aData) { d = reinterpret_cast<QEventPrivate *>(aData); }
+};
+#endif
+
+class Q_CORE_EXPORT QDynamicPropertyChangeEvent : public QEvent
+{
+public:
+ QDynamicPropertyChangeEvent(const QByteArray &name);
+ ~QDynamicPropertyChangeEvent();
+
+ inline QByteArray propertyName() const { return n; }
+
+private:
+ QByteArray n;
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QCOREEVENT_H
diff --git a/src/corelib/kernel/qcoreglobaldata.cpp b/src/corelib/kernel/qcoreglobaldata.cpp
new file mode 100644
index 0000000000..d90b46cb26
--- /dev/null
+++ b/src/corelib/kernel/qcoreglobaldata.cpp
@@ -0,0 +1,53 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcoreglobaldata_p.h"
+
+QT_BEGIN_NAMESPACE
+
+Q_GLOBAL_STATIC(QCoreGlobalData, globalInstance)
+
+QCoreGlobalData *QCoreGlobalData::instance()
+{
+ return globalInstance();
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qcoreglobaldata_p.h b/src/corelib/kernel/qcoreglobaldata_p.h
new file mode 100644
index 0000000000..aeb7bd8840
--- /dev/null
+++ b/src/corelib/kernel/qcoreglobaldata_p.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCOREGLOBALDATA_P_H
+#define QCOREGLOBALDATA_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qmap.h"
+#include "QtCore/qstringlist.h"
+#include "QtCore/qreadwritelock.h"
+
+QT_BEGIN_NAMESPACE
+
+struct QCoreGlobalData {
+ QMap<QString, QStringList> dirSearchPaths;
+ QReadWriteLock dirSearchPathsLock;
+
+ static QCoreGlobalData *instance();
+};
+
+
+QT_END_NAMESPACE
+#endif // QCOREGLOBALDATA_P_H
+
diff --git a/src/corelib/kernel/qcrashhandler.cpp b/src/corelib/kernel/qcrashhandler.cpp
new file mode 100644
index 0000000000..fbdbac0b5c
--- /dev/null
+++ b/src/corelib/kernel/qcrashhandler.cpp
@@ -0,0 +1,423 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*************************************************************************
+ *
+ * stacktrace.c 1.2 1998/12/21
+ *
+ * Copyright (c) 1998 by Bjorn Reese <breese@imada.ou.dk>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************/
+
+#include "qplatformdefs.h"
+#include "private/qcrashhandler_p.h"
+#include "qbytearray.h" // for qvsnprintf()
+
+#ifndef QT_NO_CRASHHANDLER
+
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+
+QT_BEGIN_NAMESPACE
+
+QtCrashHandler QSegfaultHandler::callback = 0;
+
+#if defined(__GLIBC__) && (__GLIBC__ >= 2) && !defined(__UCLIBC__) && !defined(QT_LINUXBASE)
+QT_BEGIN_INCLUDE_NAMESPACE
+# include "qstring.h"
+# include <execinfo.h>
+QT_END_INCLUDE_NAMESPACE
+
+static void print_backtrace(FILE *outb)
+{
+ void *stack[128];
+ int stack_size = backtrace(stack, sizeof(stack) / sizeof(void *));
+ char **stack_symbols = backtrace_symbols(stack, stack_size);
+ fprintf(outb, "Stack [%d]:\n", stack_size);
+ if(FILE *cppfilt = popen("c++filt", "rw")) {
+ dup2(fileno(outb), fileno(cppfilt));
+ for(int i = stack_size-1; i>=0; --i)
+ fwrite(stack_symbols[i], 1, strlen(stack_symbols[i]), cppfilt);
+ pclose(cppfilt);
+ } else {
+ for(int i = stack_size-1; i>=0; --i)
+ fprintf(outb, "#%d %p [%s]\n", i, stack[i], stack_symbols[i]);
+ }
+}
+static void init_backtrace(char **, int)
+{
+}
+
+#else /* Don't use the GLIBC callback */
+/* Code sourced from: */
+QT_BEGIN_INCLUDE_NAMESPACE
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#if defined(Q_OS_IRIX) && defined(USE_LIBEXC)
+# include <libexc.h>
+#endif
+QT_END_INCLUDE_NAMESPACE
+
+
+static char *globalProgName = NULL;
+static bool backtrace_command(FILE *outb, const char *format, ...)
+{
+
+ bool ret = false;
+ char buffer[50];
+
+ /*
+ * Please note that vsnprintf() is not ASync safe (ie. cannot safely
+ * be used from a signal handler.) If this proves to be a problem
+ * then the cmd string can be built by more basic functions such as
+ * strcpy, strcat, and a home-made integer-to-ascii function.
+ */
+ va_list args;
+ char cmd[512];
+ va_start(args, format);
+ qvsnprintf(cmd, 512, format, args);
+ va_end(args);
+
+ char *foo = cmd;
+#if 0
+ foo = "echo hi";
+#endif
+ if(FILE *inb = popen(foo, "r")) {
+ while(!feof(inb)) {
+ int len = fread(buffer, 1, sizeof(buffer), inb);
+ if(!len)
+ break;
+ if(!ret) {
+ fwrite("Output from ", 1, strlen("Output from "), outb);
+ strtok(cmd, " ");
+ fwrite(cmd, 1, strlen(cmd), outb);
+ fwrite("\n", 1, 1, outb);
+ ret = true;
+ }
+ fwrite(buffer, 1, len, outb);
+ }
+ fclose(inb);
+ }
+ return ret;
+}
+
+static void init_backtrace(char **argv, int argc)
+{
+ if(argc >= 1)
+ globalProgName = argv[0];
+}
+
+static void print_backtrace(FILE *outb)
+{
+ /*
+ * In general dbx seems to do a better job than gdb.
+ *
+ * Different dbx implementations require different flags/commands.
+ */
+#if defined(Q_OS_AIX)
+ if(backtrace_command(outb, "dbx -a %d 2>/dev/null <<EOF\n"
+ "where\n"
+ "detach\n"
+ "EOF\n",
+ (int)getpid()))
+ return;
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#elif defined(Q_OS_FREEBSD)
+ /*
+ * FreeBSD insists on sending a SIGSTOP to the process we
+ * attach to, so we let the debugger send a SIGCONT to that
+ * process after we have detached.
+ */
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "detach\n"
+ "shell kill -CONT %d\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid(), (int)getpid()))
+ return;
+#elif defined(Q_OS_HPUX)
+ /*
+ * HP decided to call their debugger xdb.
+ *
+ * This does not seem to work properly yet. The debugger says
+ * "Note: Stack traces may not be possible until you are
+ * stopped in user code." on HP-UX 09.01
+ *
+ * -L = line-oriented interface.
+ * "T [depth]" gives a stacktrace with local variables.
+ * The final "y" is confirmation to the quit command.
+ */
+ if(backtrace_command(outb, "xdb -P %d -L %s 2>&1 <<EOF\n"
+ "T 50\n"
+ "q\ny\n"
+ "EOF\n",
+ (int)getpid(), globalProgName))
+ return;
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#elif defined(Q_OS_IRIX)
+ /*
+ * "set $page=0" drops hold mode
+ * "dump ." displays the contents of the variables
+ */
+ if(backtrace_command(outb, "dbx -p %d 2>/dev/null <<EOF\n"
+ "set \\$page=0\n"
+ "where\n"
+# if !defined(__GNUC__)
+ /* gcc does not generate this information */
+ "dump .\n"
+# endif
+ "detach\n"
+ "EOF\n",
+ (int)getpid()))
+ return;
+
+# if defined(USE_LIBEXC)
+ if(trace_back_stack_and_print())
+ return;
+# endif
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "echo ---\\n\n"
+ "frame 5\n" /* Skip signal handler frames */
+ "set \\$x = 50\n"
+ "while (\\$x)\n" /* Print local variables for each frame */
+ "info locals\n"
+ "up\n"
+ "set \\$x--\n"
+ "end\n"
+ "echo ---\\n\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#elif defined(Q_OS_OSF)
+ if(backtrace_command(outb, "dbx -pid %d %s 2>/dev/null <<EOF\n"
+ "where\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ (int)getpid(), globalProgName))
+ return;
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#elif defined(Q_OS_SCO)
+ /*
+ * SCO OpenServer dbx is like a catch-22. The 'detach' command
+ * depends on whether ptrace(S) support detaching or not. If it
+ * is supported then 'detach' must be used, otherwise the process
+ * will be killed upon dbx exit. If it isn't supported then 'detach'
+ * will cause the process to be killed. We do not want it to be
+ * killed.
+ *
+ * Out of two evils, the omission of 'detach' was chosen because
+ * it worked on our system.
+ */
+ if(backtrace_command(outb, "dbx %s %d 2>/dev/null <<EOF\n"
+ "where\n"
+ "quit\nEOF\n",
+ globalProgName, (int)getpid()))
+ return;
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#elif defined(Q_OS_SOLARIS)
+ if(backtrace_command(outb, "dbx %s %d 2>/dev/null <<EOF\n"
+ "where\n"
+ "detach\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+ "echo ---\\n\n"
+ "frame 5\n" /* Skip signal handler frames */
+ "set \\$x = 50\n"
+ "while (\\$x)\n" /* Print local variables for each frame */
+ "info locals\n"
+ "up\n"
+ "set \\$x--\n"
+ "end\n"
+ "echo ---\\n\n"
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+ if(backtrace_command(outb, "/usr/proc/bin/pstack %d",
+ (int)getpid()))
+ return;
+ /*
+ * Other Unices (AIX, HPUX, SCO) also have adb, but
+ * they seem unable to attach to a running process.)
+ */
+ if(backtrace_command(outb, "adb %s 2>&1 <<EOF\n"
+ "0t%d:A\n" /* Attach to pid */
+ "\\$c\n" /* print stacktrace */
+ ":R\n" /* Detach */
+ "\\$q\n" /* Quit */
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#elif defined(Q_OS_INTEGRITY)
+ /* abort */
+ CheckSuccess(Failure);
+#else /* All other platforms */
+ /*
+ * TODO: SCO/UnixWare 7 must be something like (not tested)
+ * debug -i c <pid> <<EOF\nstack -f 4\nquit\nEOF\n
+ */
+# if !defined(__GNUC__)
+ if(backtrace_command(outb, "dbx %s %d 2>/dev/null <<EOF\n"
+ "where\n"
+ "detach\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+# endif
+ if(backtrace_command(outb, "gdb -q %s %d 2>/dev/null <<EOF\n"
+ "set prompt\n"
+ "where\n"
+#if 0
+ "echo ---\\n\n"
+ "frame 4\n"
+ "set \\$x = 50\n"
+ "while (\\$x)\n"
+ "info locals\n"
+ "up\n"
+ "set \\$x--\n"
+ "end\n"
+ "echo ---\\n\n"
+#endif
+ "detach\n"
+ "quit\n"
+ "EOF\n",
+ globalProgName, (int)getpid()))
+ return;
+#endif
+ const char debug_err[] = "No debugger found\n";
+ fwrite(debug_err, strlen(debug_err), 1, outb);
+}
+/* end of copied code */
+#endif
+
+
+void qt_signal_handler(int sig)
+{
+ signal(sig, SIG_DFL);
+ if(QSegfaultHandler::callback) {
+ (*QSegfaultHandler::callback)();
+ _exit(1);
+ }
+ FILE *outb = stderr;
+ if(char *crash_loc = ::getenv("QT_CRASH_OUTPUT")) {
+ if(FILE *new_outb = fopen(crash_loc, "w")) {
+ fprintf(stderr, "Crash (backtrace written to %s)!!!\n", crash_loc);
+ outb = new_outb;
+ }
+ } else {
+ fprintf(outb, "Crash!!!\n");
+ }
+ print_backtrace(outb);
+ if(outb != stderr)
+ fclose(outb);
+ _exit(1);
+}
+
+
+void
+QSegfaultHandler::initialize(char **argv, int argc)
+{
+ init_backtrace(argv, argc);
+
+ struct sigaction SignalAction;
+ SignalAction.sa_flags = 0;
+ SignalAction.sa_handler = qt_signal_handler;
+ sigemptyset(&SignalAction.sa_mask);
+ sigaction(SIGSEGV, &SignalAction, NULL);
+ sigaction(SIGBUS, &SignalAction, NULL);
+}
+
+QT_END_NAMESPACE
+
+#endif // QT_NO_CRASHHANDLER
diff --git a/src/corelib/kernel/qcrashhandler_p.h b/src/corelib/kernel/qcrashhandler_p.h
new file mode 100644
index 0000000000..1d41a3c693
--- /dev/null
+++ b/src/corelib/kernel/qcrashhandler_p.h
@@ -0,0 +1,81 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCRASHHANDLER_P_H
+#define QCRASHHANDLER_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QtCore/qglobal.h>
+
+#ifndef QT_NO_CRASHHANDLER
+
+QT_BEGIN_NAMESPACE
+
+typedef void (*QtCrashHandler)();
+
+class Q_CORE_EXPORT QSegfaultHandler
+{
+ friend void qt_signal_handler(int);
+ static QtCrashHandler callback;
+public:
+ static void initialize(char **, int);
+
+ inline static void installCrashHandler(QtCrashHandler h) { callback = h; }
+ inline static QtCrashHandler crashHandler() { return callback; }
+
+private:
+};
+
+QT_END_NAMESPACE
+
+#endif // QT_NO_CRASHHANDLER
+
+#endif // QCRASHHANDLER_P_H
diff --git a/src/corelib/kernel/qeventdispatcher_glib.cpp b/src/corelib/kernel/qeventdispatcher_glib.cpp
new file mode 100644
index 0000000000..4e19414231
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_glib.cpp
@@ -0,0 +1,601 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qeventdispatcher_glib_p.h"
+#include "qeventdispatcher_unix_p.h"
+
+#include <private/qmutexpool_p.h>
+#include <private/qthread_p.h>
+
+#include "qcoreapplication.h"
+#include "qsocketnotifier.h"
+
+#include <QtCore/qhash.h>
+#include <QtCore/qlist.h>
+#include <QtCore/qpair.h>
+
+#include <glib.h>
+
+QT_BEGIN_NAMESPACE
+
+struct GPollFDWithQSocketNotifier
+{
+ GPollFD pollfd;
+ QSocketNotifier *socketNotifier;
+};
+
+struct GSocketNotifierSource
+{
+ GSource source;
+ QList<GPollFDWithQSocketNotifier *> pollfds;
+};
+
+static gboolean socketNotifierSourcePrepare(GSource *, gint *timeout)
+{
+ if (timeout)
+ *timeout = -1;
+ return false;
+}
+
+static gboolean socketNotifierSourceCheck(GSource *source)
+{
+ GSocketNotifierSource *src = reinterpret_cast<GSocketNotifierSource *>(source);
+
+ bool pending = false;
+ for (int i = 0; !pending && i < src->pollfds.count(); ++i) {
+ GPollFDWithQSocketNotifier *p = src->pollfds.at(i);
+
+ if (p->pollfd.revents & G_IO_NVAL) {
+ // disable the invalid socket notifier
+ static const char *t[] = { "Read", "Write", "Exception" };
+ qWarning("QSocketNotifier: Invalid socket %d and type '%s', disabling...",
+ p->pollfd.fd, t[int(p->socketNotifier->type())]);
+ // ### note, modifies src->pollfds!
+ p->socketNotifier->setEnabled(false);
+ }
+
+ pending = ((p->pollfd.revents & p->pollfd.events) != 0);
+ }
+
+ return pending;
+}
+
+static gboolean socketNotifierSourceDispatch(GSource *source, GSourceFunc, gpointer)
+{
+ QEvent event(QEvent::SockAct);
+
+ GSocketNotifierSource *src = reinterpret_cast<GSocketNotifierSource *>(source);
+ for (int i = 0; i < src->pollfds.count(); ++i) {
+ GPollFDWithQSocketNotifier *p = src->pollfds.at(i);
+
+ if ((p->pollfd.revents & p->pollfd.events) != 0)
+ QCoreApplication::sendEvent(p->socketNotifier, &event);
+ }
+
+ return true; // ??? don't remove, right?
+}
+
+static GSourceFuncs socketNotifierSourceFuncs = {
+ socketNotifierSourcePrepare,
+ socketNotifierSourceCheck,
+ socketNotifierSourceDispatch,
+ NULL,
+ NULL,
+ NULL
+};
+
+struct GTimerSource
+{
+ GSource source;
+ QTimerInfoList timerList;
+ QEventLoop::ProcessEventsFlags processEventsFlags;
+ bool runWithIdlePriority;
+};
+
+static gboolean timerSourcePrepareHelper(GTimerSource *src, gint *timeout)
+{
+ timeval tv = { 0l, 0l };
+ if (!(src->processEventsFlags & QEventLoop::X11ExcludeTimers) && src->timerList.timerWait(tv))
+ *timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+ else
+ *timeout = -1;
+
+ return (*timeout == 0);
+}
+
+static gboolean timerSourceCheckHelper(GTimerSource *src)
+{
+ if (src->timerList.isEmpty()
+ || (src->processEventsFlags & QEventLoop::X11ExcludeTimers))
+ return false;
+
+ if (src->timerList.updateCurrentTime() < src->timerList.first()->timeout)
+ return false;
+
+ return true;
+}
+
+static gboolean timerSourcePrepare(GSource *source, gint *timeout)
+{
+ gint dummy;
+ if (!timeout)
+ timeout = &dummy;
+
+ GTimerSource *src = reinterpret_cast<GTimerSource *>(source);
+ if (src->runWithIdlePriority) {
+ if (timeout)
+ *timeout = -1;
+ return false;
+ }
+
+ return timerSourcePrepareHelper(src, timeout);
+}
+
+static gboolean timerSourceCheck(GSource *source)
+{
+ GTimerSource *src = reinterpret_cast<GTimerSource *>(source);
+ if (src->runWithIdlePriority)
+ return false;
+ return timerSourceCheckHelper(src);
+}
+
+static gboolean timerSourceDispatch(GSource *source, GSourceFunc, gpointer)
+{
+ GTimerSource *timerSource = reinterpret_cast<GTimerSource *>(source);
+ if (timerSource->processEventsFlags & QEventLoop::X11ExcludeTimers)
+ return true;
+ timerSource->runWithIdlePriority = true;
+ (void) timerSource->timerList.activateTimers();
+ return true; // ??? don't remove, right again?
+}
+
+static GSourceFuncs timerSourceFuncs = {
+ timerSourcePrepare,
+ timerSourceCheck,
+ timerSourceDispatch,
+ NULL,
+ NULL,
+ NULL
+};
+
+struct GIdleTimerSource
+{
+ GSource source;
+ GTimerSource *timerSource;
+};
+
+static gboolean idleTimerSourcePrepare(GSource *source, gint *timeout)
+{
+ GIdleTimerSource *idleTimerSource = reinterpret_cast<GIdleTimerSource *>(source);
+ GTimerSource *timerSource = idleTimerSource->timerSource;
+ if (!timerSource->runWithIdlePriority) {
+ // Yield to the normal priority timer source
+ if (timeout)
+ *timeout = -1;
+ return false;
+ }
+
+ return timerSourcePrepareHelper(timerSource, timeout);
+}
+
+static gboolean idleTimerSourceCheck(GSource *source)
+{
+ GIdleTimerSource *idleTimerSource = reinterpret_cast<GIdleTimerSource *>(source);
+ GTimerSource *timerSource = idleTimerSource->timerSource;
+ if (!timerSource->runWithIdlePriority) {
+ // Yield to the normal priority timer source
+ return false;
+ }
+ return timerSourceCheckHelper(timerSource);
+}
+
+static gboolean idleTimerSourceDispatch(GSource *source, GSourceFunc, gpointer)
+{
+ GTimerSource *timerSource = reinterpret_cast<GIdleTimerSource *>(source)->timerSource;
+ (void) timerSourceDispatch(&timerSource->source, 0, 0);
+ return true;
+}
+
+static GSourceFuncs idleTimerSourceFuncs = {
+ idleTimerSourcePrepare,
+ idleTimerSourceCheck,
+ idleTimerSourceDispatch,
+ NULL,
+ NULL,
+ NULL
+};
+
+struct GPostEventSource
+{
+ GSource source;
+ QAtomicInt serialNumber;
+ int lastSerialNumber;
+ QEventDispatcherGlibPrivate *d;
+};
+
+static gboolean postEventSourcePrepare(GSource *s, gint *timeout)
+{
+ QThreadData *data = QThreadData::current();
+ if (!data)
+ return false;
+
+ gint dummy;
+ if (!timeout)
+ timeout = &dummy;
+ *timeout = data->canWait ? -1 : 0;
+
+ GPostEventSource *source = reinterpret_cast<GPostEventSource *>(s);
+ return (!data->canWait
+ || (source->serialNumber != source->lastSerialNumber));
+}
+
+static gboolean postEventSourceCheck(GSource *source)
+{
+ return postEventSourcePrepare(source, 0);
+}
+
+static gboolean postEventSourceDispatch(GSource *s, GSourceFunc, gpointer)
+{
+ GPostEventSource *source = reinterpret_cast<GPostEventSource *>(s);
+ source->lastSerialNumber = source->serialNumber;
+ QCoreApplication::sendPostedEvents();
+ source->d->runTimersOnceWithNormalPriority();
+ return true; // i dunno, george...
+}
+
+static GSourceFuncs postEventSourceFuncs = {
+ postEventSourcePrepare,
+ postEventSourceCheck,
+ postEventSourceDispatch,
+ NULL,
+ NULL,
+ NULL
+};
+
+
+QEventDispatcherGlibPrivate::QEventDispatcherGlibPrivate(GMainContext *context)
+ : mainContext(context)
+{
+ if (qgetenv("QT_NO_THREADED_GLIB").isEmpty()) {
+ static int dummyValue = 0; // only used for its address
+ QMutexLocker locker(QMutexPool::instance()->get(&dummyValue));
+ if (!g_thread_supported())
+ g_thread_init(NULL);
+ }
+
+ if (mainContext) {
+ g_main_context_ref(mainContext);
+ } else {
+ QCoreApplication *app = QCoreApplication::instance();
+ if (app && QThread::currentThread() == app->thread()) {
+ mainContext = g_main_context_default();
+ g_main_context_ref(mainContext);
+ } else {
+ mainContext = g_main_context_new();
+ }
+ }
+
+#if GLIB_CHECK_VERSION (2, 22, 0)
+ g_main_context_push_thread_default (mainContext);
+#endif
+
+ // setup post event source
+ postEventSource = reinterpret_cast<GPostEventSource *>(g_source_new(&postEventSourceFuncs,
+ sizeof(GPostEventSource)));
+ postEventSource->serialNumber = 1;
+ postEventSource->d = this;
+ g_source_set_can_recurse(&postEventSource->source, true);
+ g_source_attach(&postEventSource->source, mainContext);
+
+ // setup socketNotifierSource
+ socketNotifierSource =
+ reinterpret_cast<GSocketNotifierSource *>(g_source_new(&socketNotifierSourceFuncs,
+ sizeof(GSocketNotifierSource)));
+ (void) new (&socketNotifierSource->pollfds) QList<GPollFDWithQSocketNotifier *>();
+ g_source_set_can_recurse(&socketNotifierSource->source, true);
+ g_source_attach(&socketNotifierSource->source, mainContext);
+
+ // setup normal and idle timer sources
+ timerSource = reinterpret_cast<GTimerSource *>(g_source_new(&timerSourceFuncs,
+ sizeof(GTimerSource)));
+ (void) new (&timerSource->timerList) QTimerInfoList();
+ timerSource->processEventsFlags = QEventLoop::AllEvents;
+ timerSource->runWithIdlePriority = false;
+ g_source_set_can_recurse(&timerSource->source, true);
+ g_source_attach(&timerSource->source, mainContext);
+
+ idleTimerSource = reinterpret_cast<GIdleTimerSource *>(g_source_new(&idleTimerSourceFuncs,
+ sizeof(GIdleTimerSource)));
+ idleTimerSource->timerSource = timerSource;
+ g_source_set_can_recurse(&idleTimerSource->source, true);
+ g_source_set_priority(&idleTimerSource->source, G_PRIORITY_DEFAULT_IDLE);
+ g_source_attach(&idleTimerSource->source, mainContext);
+}
+
+void QEventDispatcherGlibPrivate::runTimersOnceWithNormalPriority()
+{
+ timerSource->runWithIdlePriority = false;
+}
+
+QEventDispatcherGlib::QEventDispatcherGlib(QObject *parent)
+ : QAbstractEventDispatcher(*(new QEventDispatcherGlibPrivate), parent)
+{
+}
+
+QEventDispatcherGlib::QEventDispatcherGlib(GMainContext *mainContext, QObject *parent)
+ : QAbstractEventDispatcher(*(new QEventDispatcherGlibPrivate(mainContext)), parent)
+{ }
+
+QEventDispatcherGlib::~QEventDispatcherGlib()
+{
+ Q_D(QEventDispatcherGlib);
+
+ // destroy all timer sources
+ qDeleteAll(d->timerSource->timerList);
+ d->timerSource->timerList.~QTimerInfoList();
+ g_source_destroy(&d->timerSource->source);
+ g_source_unref(&d->timerSource->source);
+ d->timerSource = 0;
+ g_source_destroy(&d->idleTimerSource->source);
+ g_source_unref(&d->idleTimerSource->source);
+ d->idleTimerSource = 0;
+
+ // destroy socket notifier source
+ for (int i = 0; i < d->socketNotifierSource->pollfds.count(); ++i) {
+ GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds[i];
+ g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd);
+ delete p;
+ }
+ d->socketNotifierSource->pollfds.~QList<GPollFDWithQSocketNotifier *>();
+ g_source_destroy(&d->socketNotifierSource->source);
+ g_source_unref(&d->socketNotifierSource->source);
+ d->socketNotifierSource = 0;
+
+ // destroy post event source
+ g_source_destroy(&d->postEventSource->source);
+ g_source_unref(&d->postEventSource->source);
+ d->postEventSource = 0;
+
+ Q_ASSERT(d->mainContext != 0);
+#if GLIB_CHECK_VERSION (2, 22, 0)
+ g_main_context_pop_thread_default (d->mainContext);
+#endif
+ g_main_context_unref(d->mainContext);
+ d->mainContext = 0;
+}
+
+bool QEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags)
+{
+ Q_D(QEventDispatcherGlib);
+
+ const bool canWait = (flags & QEventLoop::WaitForMoreEvents);
+ if (canWait)
+ emit aboutToBlock();
+ else
+ emit awake();
+
+ // tell postEventSourcePrepare() and timerSource about any new flags
+ QEventLoop::ProcessEventsFlags savedFlags = d->timerSource->processEventsFlags;
+ d->timerSource->processEventsFlags = flags;
+
+ if (!(flags & QEventLoop::EventLoopExec)) {
+ // force timers to be sent at normal priority
+ d->timerSource->runWithIdlePriority = false;
+ }
+
+ bool result = g_main_context_iteration(d->mainContext, canWait);
+ while (!result && canWait)
+ result = g_main_context_iteration(d->mainContext, canWait);
+
+ d->timerSource->processEventsFlags = savedFlags;
+
+ if (canWait)
+ emit awake();
+
+ return result;
+}
+
+bool QEventDispatcherGlib::hasPendingEvents()
+{
+ Q_D(QEventDispatcherGlib);
+ return g_main_context_pending(d->mainContext);
+}
+
+void QEventDispatcherGlib::registerSocketNotifier(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+ int sockfd = notifier->socket();
+ int type = notifier->type();
+#ifndef QT_NO_DEBUG
+ if (sockfd < 0) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread()
+ || thread() != QThread::currentThread()) {
+ qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherGlib);
+
+
+ GPollFDWithQSocketNotifier *p = new GPollFDWithQSocketNotifier;
+ p->pollfd.fd = sockfd;
+ switch (type) {
+ case QSocketNotifier::Read:
+ p->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
+ break;
+ case QSocketNotifier::Write:
+ p->pollfd.events = G_IO_OUT | G_IO_ERR;
+ break;
+ case QSocketNotifier::Exception:
+ p->pollfd.events = G_IO_PRI | G_IO_ERR;
+ break;
+ }
+ p->socketNotifier = notifier;
+
+ d->socketNotifierSource->pollfds.append(p);
+
+ g_source_add_poll(&d->socketNotifierSource->source, &p->pollfd);
+}
+
+void QEventDispatcherGlib::unregisterSocketNotifier(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+#ifndef QT_NO_DEBUG
+ int sockfd = notifier->socket();
+ if (sockfd < 0) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread()
+ || thread() != QThread::currentThread()) {
+ qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherGlib);
+
+ for (int i = 0; i < d->socketNotifierSource->pollfds.count(); ++i) {
+ GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds.at(i);
+ if (p->socketNotifier == notifier) {
+ // found it
+ g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd);
+
+ d->socketNotifierSource->pollfds.removeAt(i);
+ delete p;
+
+ return;
+ }
+ }
+}
+
+void QEventDispatcherGlib::registerTimer(int timerId, int interval, QObject *object)
+{
+#ifndef QT_NO_DEBUG
+ if (timerId < 1 || interval < 0 || !object) {
+ qWarning("QEventDispatcherGlib::registerTimer: invalid arguments");
+ return;
+ } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QObject::startTimer: timers cannot be started from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherGlib);
+ d->timerSource->timerList.registerTimer(timerId, interval, object);
+}
+
+bool QEventDispatcherGlib::unregisterTimer(int timerId)
+{
+#ifndef QT_NO_DEBUG
+ if (timerId < 1) {
+ qWarning("QEventDispatcherGlib::unregisterTimer: invalid argument");
+ return false;
+ } else if (thread() != QThread::currentThread()) {
+ qWarning("QObject::killTimer: timers cannot be stopped from another thread");
+ return false;
+ }
+#endif
+
+ Q_D(QEventDispatcherGlib);
+ return d->timerSource->timerList.unregisterTimer(timerId);
+}
+
+bool QEventDispatcherGlib::unregisterTimers(QObject *object)
+{
+#ifndef QT_NO_DEBUG
+ if (!object) {
+ qWarning("QEventDispatcherGlib::unregisterTimers: invalid argument");
+ return false;
+ } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QObject::killTimers: timers cannot be stopped from another thread");
+ return false;
+ }
+#endif
+
+ Q_D(QEventDispatcherGlib);
+ return d->timerSource->timerList.unregisterTimers(object);
+}
+
+QList<QEventDispatcherGlib::TimerInfo> QEventDispatcherGlib::registeredTimers(QObject *object) const
+{
+ if (!object) {
+ qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument");
+ return QList<TimerInfo>();
+ }
+
+ Q_D(const QEventDispatcherGlib);
+ return d->timerSource->timerList.registeredTimers(object);
+}
+
+void QEventDispatcherGlib::interrupt()
+{
+ wakeUp();
+}
+
+void QEventDispatcherGlib::wakeUp()
+{
+ Q_D(QEventDispatcherGlib);
+ d->postEventSource->serialNumber.ref();
+ g_main_context_wakeup(d->mainContext);
+}
+
+void QEventDispatcherGlib::flush()
+{
+}
+
+bool QEventDispatcherGlib::versionSupported()
+{
+#if !defined(GLIB_MAJOR_VERSION) || !defined(GLIB_MINOR_VERSION) || !defined(GLIB_MICRO_VERSION)
+ return false;
+#else
+ return ((GLIB_MAJOR_VERSION << 16) + (GLIB_MINOR_VERSION << 8) + GLIB_MICRO_VERSION) >= 0x020301;
+#endif
+}
+
+QEventDispatcherGlib::QEventDispatcherGlib(QEventDispatcherGlibPrivate &dd, QObject *parent)
+ : QAbstractEventDispatcher(dd, parent)
+{
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qeventdispatcher_glib_p.h b/src/corelib/kernel/qeventdispatcher_glib_p.h
new file mode 100644
index 0000000000..65eff72fca
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_glib_p.h
@@ -0,0 +1,119 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QEVENTDISPATCHER_GLIB_P_H
+#define QEVENTDISPATCHER_GLIB_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of the QLibrary class. This header file may change from
+// version to version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "qabstracteventdispatcher.h"
+#include "qabstracteventdispatcher_p.h"
+
+#include <QtCore/qhash.h>
+
+typedef struct _GMainContext GMainContext;
+
+QT_BEGIN_NAMESPACE
+
+class QEventDispatcherGlibPrivate;
+
+class Q_CORE_EXPORT QEventDispatcherGlib : public QAbstractEventDispatcher
+{
+ Q_OBJECT
+ Q_DECLARE_PRIVATE(QEventDispatcherGlib)
+
+public:
+ explicit QEventDispatcherGlib(QObject *parent = 0);
+ explicit QEventDispatcherGlib(GMainContext *context, QObject *parent = 0);
+ ~QEventDispatcherGlib();
+
+ bool processEvents(QEventLoop::ProcessEventsFlags flags);
+ bool hasPendingEvents();
+
+ void registerSocketNotifier(QSocketNotifier *socketNotifier);
+ void unregisterSocketNotifier(QSocketNotifier *socketNotifier);
+
+ void registerTimer(int timerId, int interval, QObject *object);
+ bool unregisterTimer(int timerId);
+ bool unregisterTimers(QObject *object);
+ QList<TimerInfo> registeredTimers(QObject *object) const;
+
+ void wakeUp();
+ void interrupt();
+ void flush();
+
+ static bool versionSupported();
+
+protected:
+ QEventDispatcherGlib(QEventDispatcherGlibPrivate &dd, QObject *parent);
+};
+
+struct GPostEventSource;
+struct GSocketNotifierSource;
+struct GTimerSource;
+struct GIdleTimerSource;
+
+class Q_CORE_EXPORT QEventDispatcherGlibPrivate : public QAbstractEventDispatcherPrivate
+{
+
+public:
+ QEventDispatcherGlibPrivate(GMainContext *context = 0);
+ GMainContext *mainContext;
+ GPostEventSource *postEventSource;
+ GSocketNotifierSource *socketNotifierSource;
+ GTimerSource *timerSource;
+ GIdleTimerSource *idleTimerSource;
+
+ void runTimersOnceWithNormalPriority();
+};
+
+QT_END_NAMESPACE
+
+#endif // QEVENTDISPATCHER_GLIB_P_H
diff --git a/src/corelib/kernel/qeventdispatcher_symbian.cpp b/src/corelib/kernel/qeventdispatcher_symbian.cpp
new file mode 100644
index 0000000000..e0eeb0819a
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_symbian.cpp
@@ -0,0 +1,1310 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qeventdispatcher_symbian_p.h"
+#include <private/qthread_p.h>
+#include <qcoreapplication.h>
+#include <private/qcoreapplication_p.h>
+#include <qsemaphore.h>
+
+#include <unistd.h>
+#include <errno.h>
+
+QT_BEGIN_NAMESPACE
+
+#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
+// when the system UI is Qt based, priority drop is not needed as CPU starved processes will not be killed.
+#undef QT_SYMBIAN_PRIORITY_DROP
+#else
+#define QT_SYMBIAN_PRIORITY_DROP
+#endif
+
+#define WAKE_UP_PRIORITY CActive::EPriorityStandard
+#define TIMER_PRIORITY CActive::EPriorityHigh
+#define NULLTIMER_PRIORITY CActive::EPriorityLow
+#define COMPLETE_DEFERRED_ACTIVE_OBJECTS_PRIORITY CActive::EPriorityIdle
+
+static inline int qt_pipe_write(int socket, const char *data, qint64 len)
+{
+ return ::write(socket, data, len);
+}
+#if defined(write)
+# undef write
+#endif
+
+static inline int qt_pipe_close(int socket)
+{
+ return ::close(socket);
+}
+#if defined(close)
+# undef close
+#endif
+
+static inline int qt_pipe_fcntl(int socket, int command)
+{
+ return ::fcntl(socket, command);
+}
+static inline int qt_pipe2_fcntl(int socket, int command, int option)
+{
+ return ::fcntl(socket, command, option);
+}
+#if defined(fcntl)
+# undef fcntl
+#endif
+
+static inline int qt_socket_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
+{
+ return ::select(nfds, readfds, writefds, exceptfds, timeout);
+}
+
+// This simply interrupts the select and locks the mutex until destroyed.
+class QSelectMutexGrabber
+{
+public:
+ QSelectMutexGrabber(int writeFd, int readFd, QMutex *mutex)
+ : m_mutex(mutex)
+ {
+ if (m_mutex->tryLock())
+ return;
+
+ char dummy = 0;
+ qt_pipe_write(writeFd, &dummy, 1);
+
+ m_mutex->lock();
+
+ char buffer;
+ while (::read(readFd, &buffer, 1) > 0) {}
+ }
+
+ ~QSelectMutexGrabber()
+ {
+ m_mutex->unlock();
+ }
+
+private:
+ QMutex *m_mutex;
+};
+
+/*
+ * This class is designed to aid in implementing event handling in a more round robin fashion. We
+ * cannot change active objects that we do not own, but the active objects that Qt owns will use
+ * this as a base class with convenience functions.
+ *
+ * Here is how it works: On every RunL, the deriving class should call maybeQueueForLater().
+ * This will return whether the active object has been queued, or whether it should run immediately.
+ * Queued objects will run again after other events have been processed.
+ *
+ * The QCompleteDeferredAOs class is a special object that runs after all others, which will
+ * reactivate the objects that were previously not run.
+ */
+QActiveObject::QActiveObject(TInt priority, QEventDispatcherSymbian *dispatcher)
+ : CActive(priority),
+ m_dispatcher(dispatcher),
+ m_hasAlreadyRun(false),
+ m_hasRunAgain(false),
+ m_iterationCount(1)
+{
+}
+
+QActiveObject::~QActiveObject()
+{
+ if (m_hasRunAgain)
+ m_dispatcher->removeDeferredActiveObject(this);
+}
+
+bool QActiveObject::maybeQueueForLater()
+{
+ Q_ASSERT(!m_hasRunAgain);
+
+ if (!m_hasAlreadyRun || m_dispatcher->iterationCount() != m_iterationCount) {
+ // First occurrence of this event in this iteration.
+ m_hasAlreadyRun = true;
+ m_iterationCount = m_dispatcher->iterationCount();
+ return false;
+ } else {
+ // The event has already occurred.
+ m_dispatcher->addDeferredActiveObject(this);
+ m_hasRunAgain = true;
+ return true;
+ }
+}
+
+bool QActiveObject::maybeDeferSocketEvent()
+{
+ Q_ASSERT(!m_hasRunAgain);
+ Q_ASSERT(m_dispatcher);
+ if (!m_dispatcher->areSocketEventsBlocked()) {
+ return false;
+ }
+ m_hasRunAgain = true;
+ m_dispatcher->addDeferredSocketActiveObject(this);
+ return true;
+}
+
+void QActiveObject::reactivateAndComplete()
+{
+ TInt error = iStatus.Int();
+ iStatus = KRequestPending;
+ SetActive();
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, error);
+
+ m_hasRunAgain = false;
+ m_hasAlreadyRun = false;
+}
+
+QWakeUpActiveObject::QWakeUpActiveObject(QEventDispatcherSymbian *dispatcher)
+ : QActiveObject(WAKE_UP_PRIORITY, dispatcher)
+{
+ m_hostThreadId = RThread().Id();
+ CActiveScheduler::Add(this);
+ iStatus = KRequestPending;
+ SetActive();
+}
+
+QWakeUpActiveObject::~QWakeUpActiveObject()
+{
+ Cancel();
+}
+
+void QWakeUpActiveObject::DoCancel()
+{
+ if (iStatus.Int() == KRequestPending) {
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ } else if (IsActive() && m_hostThreadId != RThread().Id()) {
+ // This is being cancelled in the adopted monitor thread, which can happen if an adopted thread with
+ // an event loop has exited. The event loop creates an event dispatcher with this active object, which may be complete but not run on exit.
+ // We force a cancellation in this thread, because a) the object cannot be deleted while active and b) without a cancellation
+ // the thread semaphore will be one count down.
+ // It is possible for this problem to affect other active objects. They symptom would be that finished signals
+ // from adopted threads are not sent, or they arrive much later than they should.
+ TRequestStatus *status = &iStatus;
+ User::RequestComplete(status, KErrNone);
+ }
+}
+
+void QWakeUpActiveObject::RunL()
+{
+ if (maybeQueueForLater())
+ return;
+
+ iStatus = KRequestPending;
+ SetActive();
+ QT_TRYCATCH_LEAVING(m_dispatcher->wakeUpWasCalled());
+}
+
+QTimerActiveObject::QTimerActiveObject(QEventDispatcherSymbian *dispatcher, SymbianTimerInfo *timerInfo)
+ : QActiveObject((timerInfo->interval) ? TIMER_PRIORITY : NULLTIMER_PRIORITY , dispatcher),
+ m_timerInfo(timerInfo), m_expectedTimeSinceLastEvent(0)
+{
+ // start the timeout timer to ensure initialisation
+ m_timeoutTimer.start();
+}
+
+QTimerActiveObject::~QTimerActiveObject()
+{
+ Cancel();
+ m_rTimer.Close(); //close of null handle is safe
+}
+
+void QTimerActiveObject::DoCancel()
+{
+ if (m_timerInfo->interval > 0) {
+ m_rTimer.Cancel();
+ } else {
+ if (iStatus.Int() == KRequestPending) {
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+ }
+}
+
+void QTimerActiveObject::RunL()
+{
+ int error = KErrNone;
+ if (iStatus == KErrNone) {
+ QT_TRYCATCH_ERROR(error, Run());
+ } else {
+ error = iStatus.Int();
+ }
+ // All Symbian error codes are negative.
+ if (error < 0) {
+ CActiveScheduler::Current()->Error(error); // stop and report here, as this timer will be deleted on scope exit
+ }
+}
+
+#define MAX_SYMBIAN_TIMEOUT_MS 2000000
+void QTimerActiveObject::StartTimer()
+{
+ if (m_timerInfo->msLeft > MAX_SYMBIAN_TIMEOUT_MS) {
+ //There is loss of accuracy anyway due to needing to restart the timer every 33 minutes,
+ //so the 1/64s res of After() is acceptable for these very long timers.
+ m_rTimer.After(iStatus, MAX_SYMBIAN_TIMEOUT_MS * 1000);
+ m_timerInfo->msLeft -= MAX_SYMBIAN_TIMEOUT_MS;
+ } else {
+ // this algorithm implements drift correction for repeating timers
+ // calculate how late we are for this event
+ int timeSinceLastEvent = m_timeoutTimer.restart();
+ int overshoot = timeSinceLastEvent - m_expectedTimeSinceLastEvent;
+ if (overshoot > m_timerInfo->msLeft) {
+ // we skipped a whole timeout, restart from here
+ overshoot = 0;
+ }
+ // calculate when the next event should happen
+ int waitTime = m_timerInfo->msLeft - overshoot;
+ m_expectedTimeSinceLastEvent = waitTime;
+ // limit the actual ms wait time to avoid wild corrections
+ // this will cause the real event time to slowly drift back to the expected event time
+ // measurements show that Symbian timers always fire 1 or 2 ms late
+ const int limit = 4;
+ waitTime = qMax(m_timerInfo->msLeft - limit, waitTime);
+ m_rTimer.HighRes(iStatus, waitTime * 1000);
+ m_timerInfo->msLeft = 0;
+ }
+ SetActive();
+}
+
+void QTimerActiveObject::Run()
+{
+ //restart timer immediately, if the timeout has been split because it overflows max for platform.
+ if (m_timerInfo->msLeft > 0) {
+ StartTimer();
+ return;
+ }
+
+ if (maybeQueueForLater())
+ return;
+
+ if (m_timerInfo->interval > 0) {
+ // Start a new timer immediately so that we don't lose time.
+ m_timerInfo->msLeft = m_timerInfo->interval;
+ StartTimer();
+
+ m_timerInfo->dispatcher->timerFired(m_timerInfo->timerId);
+ } else {
+ // However, we only complete zero timers after the event has finished,
+ // in order to prevent busy looping when doing nested loops.
+
+ // Keep the refpointer around in order to avoid deletion until the end of this function.
+ SymbianTimerInfoPtr timerInfoPtr(m_timerInfo);
+
+ m_timerInfo->dispatcher->timerFired(m_timerInfo->timerId);
+
+ iStatus = KRequestPending;
+ SetActive();
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+}
+
+void QTimerActiveObject::Start()
+{
+ CActiveScheduler::Add(this);
+ m_timerInfo->msLeft = m_timerInfo->interval;
+ if (m_timerInfo->interval > 0) {
+ if (!m_rTimer.Handle()) {
+ qt_symbian_throwIfError(m_rTimer.CreateLocal());
+ }
+ m_timeoutTimer.start();
+ m_expectedTimeSinceLastEvent = 0;
+ StartTimer();
+ } else {
+ iStatus = KRequestPending;
+ SetActive();
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+}
+
+SymbianTimerInfo::SymbianTimerInfo()
+ : timerAO(0)
+{
+}
+
+SymbianTimerInfo::~SymbianTimerInfo()
+{
+ delete timerAO;
+}
+
+QCompleteDeferredAOs::QCompleteDeferredAOs(QEventDispatcherSymbian *dispatcher)
+ : CActive(COMPLETE_DEFERRED_ACTIVE_OBJECTS_PRIORITY),
+ m_dispatcher(dispatcher)
+{
+ CActiveScheduler::Add(this);
+ iStatus = KRequestPending;
+ SetActive();
+}
+
+QCompleteDeferredAOs::~QCompleteDeferredAOs()
+{
+ Cancel();
+}
+
+void QCompleteDeferredAOs::complete()
+{
+ if (iStatus.Int() == KRequestPending) {
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+}
+
+void QCompleteDeferredAOs::DoCancel()
+{
+ if (iStatus.Int() == KRequestPending) {
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+}
+
+void QCompleteDeferredAOs::RunL()
+{
+ iStatus = KRequestPending;
+ SetActive();
+
+ QT_TRYCATCH_LEAVING(m_dispatcher->reactivateDeferredActiveObjects());
+}
+
+QSelectThread::QSelectThread()
+ : m_quit(false)
+{
+ if (::pipe(m_pipeEnds) != 0) {
+ qWarning("Select thread was unable to open a pipe, errno: %i", errno);
+ } else {
+ int flags0 = qt_pipe_fcntl(m_pipeEnds[0], F_GETFL);
+ int flags1 = qt_pipe_fcntl(m_pipeEnds[1], F_GETFL);
+ // We should check the error code here, but Open C has a bug that returns
+ // failure even though the operation was successful.
+ qt_pipe2_fcntl(m_pipeEnds[0], F_SETFL, flags0 | O_NONBLOCK);
+ qt_pipe2_fcntl(m_pipeEnds[1], F_SETFL, flags1 | O_NONBLOCK);
+ }
+}
+
+QSelectThread::~QSelectThread()
+{
+ qt_pipe_close(m_pipeEnds[1]);
+ qt_pipe_close(m_pipeEnds[0]);
+}
+
+void QSelectThread::run()
+{
+ Q_D(QThread);
+
+ m_mutex.lock();
+
+ while (!m_quit) {
+ fd_set readfds;
+ fd_set writefds;
+ fd_set exceptionfds;
+
+ FD_ZERO(&readfds);
+ FD_ZERO(&writefds);
+ FD_ZERO(&exceptionfds);
+
+ int maxfd = 0;
+ maxfd = qMax(maxfd, updateSocketSet(QSocketNotifier::Read, &readfds));
+ maxfd = qMax(maxfd, updateSocketSet(QSocketNotifier::Write, &writefds));
+ maxfd = qMax(maxfd, updateSocketSet(QSocketNotifier::Exception, &exceptionfds));
+ maxfd = qMax(maxfd, m_pipeEnds[0]);
+ maxfd++;
+
+ FD_SET(m_pipeEnds[0], &readfds);
+
+ int ret;
+ int savedSelectErrno;
+ ret = qt_socket_select(maxfd, &readfds, &writefds, &exceptionfds, 0);
+ savedSelectErrno = errno;
+
+ if(ret == 0) {
+ // do nothing
+ } else if (ret < 0) {
+ switch (savedSelectErrno) {
+ case EBADF:
+ case EINVAL:
+ case ENOMEM:
+ case EFAULT:
+ qWarning("::select() returned an error: %i", savedSelectErrno);
+ break;
+ case ECONNREFUSED:
+ case EPIPE:
+ qWarning("::select() returned an error: %i (go through sockets)", savedSelectErrno);
+ // prepare to go through all sockets
+ // mark in fd sets both:
+ // good ones
+ // ones that return -1 in select
+ // after loop update notifiers for all of them
+
+ // as we don't have "exception" notifier type
+ // we should force monitoring fd_set of this
+ // type as well
+
+ // clean @ start
+ FD_ZERO(&readfds);
+ FD_ZERO(&writefds);
+ FD_ZERO(&exceptionfds);
+ for (QHash<QSocketNotifier *, TRequestStatus *>::const_iterator i = m_AOStatuses.begin();
+ i != m_AOStatuses.end(); ++i) {
+
+ fd_set onefds;
+ FD_ZERO(&onefds);
+ FD_SET(i.key()->socket(), &onefds);
+
+ fd_set excfds;
+ FD_ZERO(&excfds);
+ FD_SET(i.key()->socket(), &excfds);
+
+ maxfd = i.key()->socket() + 1;
+
+ struct timeval timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0;
+
+ ret = 0;
+
+ if(i.key()->type() == QSocketNotifier::Read) {
+ ret = ::select(maxfd, &onefds, 0, &excfds, &timeout);
+ if(ret != 0) FD_SET(i.key()->socket(), &readfds);
+ } else if(i.key()->type() == QSocketNotifier::Write) {
+ ret = ::select(maxfd, 0, &onefds, &excfds, &timeout);
+ if(ret != 0) FD_SET(i.key()->socket(), &writefds);
+ }
+
+ } // end for
+
+ // traversed all, so update
+ updateActivatedNotifiers(QSocketNotifier::Exception, &exceptionfds);
+ updateActivatedNotifiers(QSocketNotifier::Read, &readfds);
+ updateActivatedNotifiers(QSocketNotifier::Write, &writefds);
+
+ break;
+ case EINTR: // Should never occur on Symbian, but this is future proof!
+ default:
+ qWarning("::select() returned an unknown error: %i", savedSelectErrno);
+
+ break;
+ }
+ } else {
+ updateActivatedNotifiers(QSocketNotifier::Exception, &exceptionfds);
+ updateActivatedNotifiers(QSocketNotifier::Read, &readfds);
+ updateActivatedNotifiers(QSocketNotifier::Write, &writefds);
+ }
+
+ if (FD_ISSET(m_pipeEnds[0], &readfds))
+ m_waitCond.wait(&m_mutex);
+ }
+
+ m_mutex.unlock();
+}
+
+void QSelectThread::requestSocketEvents ( QSocketNotifier *notifier, TRequestStatus *status )
+{
+ Q_D(QThread);
+
+ if (!isRunning()) {
+ start();
+ }
+
+ Q_ASSERT(QThread::currentThread() == this->thread());
+
+ QSelectMutexGrabber lock(m_pipeEnds[1], m_pipeEnds[0], &m_mutex);
+
+ Q_ASSERT(!m_AOStatuses.contains(notifier));
+
+ m_AOStatuses.insert(notifier, status);
+
+ m_waitCond.wakeAll();
+}
+
+void QSelectThread::cancelSocketEvents ( QSocketNotifier *notifier )
+{
+ Q_ASSERT(QThread::currentThread() == this->thread());
+
+ QSelectMutexGrabber lock(m_pipeEnds[1], m_pipeEnds[0], &m_mutex);
+
+ m_AOStatuses.remove(notifier);
+
+ m_waitCond.wakeAll();
+}
+
+void QSelectThread::restart()
+{
+ Q_ASSERT(QThread::currentThread() == this->thread());
+
+ QSelectMutexGrabber lock(m_pipeEnds[1], m_pipeEnds[0], &m_mutex);
+
+ m_waitCond.wakeAll();
+}
+
+int QSelectThread::updateSocketSet(QSocketNotifier::Type type, fd_set *fds)
+{
+ int maxfd = 0;
+ if(m_AOStatuses.isEmpty()) {
+ /*
+ * Wonder if should return -1
+ * to signal that no descriptors
+ * added to fds
+ */
+ return maxfd;
+ }
+ for ( QHash<QSocketNotifier *, TRequestStatus *>::const_iterator i = m_AOStatuses.begin();
+ i != m_AOStatuses.end(); ++i) {
+ if (i.key()->type() == type) {
+ FD_SET(i.key()->socket(), fds);
+ maxfd = qMax(maxfd, i.key()->socket());
+ } else if(type == QSocketNotifier::Exception) {
+ /*
+ * We are registering existing sockets
+ * always to exception set
+ *
+ * Doing double FD_SET shouldn't
+ * matter
+ */
+ FD_SET(i.key()->socket(), fds);
+ maxfd = qMax(maxfd, i.key()->socket());
+ }
+ }
+
+ return maxfd;
+}
+
+void QSelectThread::updateActivatedNotifiers(QSocketNotifier::Type type, fd_set *fds)
+{
+ Q_D(QThread);
+ if(m_AOStatuses.isEmpty()) {
+ return;
+ }
+ QList<QSocketNotifier *> toRemove;
+ for (QHash<QSocketNotifier *, TRequestStatus *>::const_iterator i = m_AOStatuses.begin();
+ i != m_AOStatuses.end(); ++i) {
+ if (i.key()->type() == type && FD_ISSET(i.key()->socket(), fds)) {
+ toRemove.append(i.key());
+ TRequestStatus *status = i.value();
+ // Thread data is still owned by the main thread.
+ QEventDispatcherSymbian::RequestComplete(d->threadData->symbian_thread_handle, status, KErrNone);
+ } else if(type == QSocketNotifier::Exception && FD_ISSET(i.key()->socket(), fds)) {
+ /*
+ * check if socket is in exception set
+ * then signal RequestComplete for it
+ */
+ qWarning("exception on %d [will close the socket handle - hack]", i.key()->socket());
+ // quick fix; there is a bug
+ // when doing read on socket
+ // errors not preoperly mapped
+ // after offline-ing the device
+ // on some devices we do get exception
+ ::close(i.key()->socket());
+ toRemove.append(i.key());
+ TRequestStatus *status = i.value();
+ QEventDispatcherSymbian::RequestComplete(d->threadData->symbian_thread_handle, status, KErrNone);
+ }
+ }
+
+ for (int c = 0; c < toRemove.size(); ++c) {
+ m_AOStatuses.remove(toRemove[c]);
+ }
+}
+
+void QSelectThread::stop()
+{
+ m_quit = true;
+ restart();
+ wait();
+}
+
+QSocketActiveObject::QSocketActiveObject(QEventDispatcherSymbian *dispatcher, QSocketNotifier *notifier)
+ : QActiveObject(CActive::EPriorityStandard, dispatcher),
+ m_notifier(notifier),
+ m_inSocketEvent(false),
+ m_deleteLater(false)
+{
+ CActiveScheduler::Add(this);
+ iStatus = KRequestPending;
+ SetActive();
+}
+
+QSocketActiveObject::~QSocketActiveObject()
+{
+ Cancel();
+}
+
+void QSocketActiveObject::DoCancel()
+{
+ if (iStatus.Int() == KRequestPending) {
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+}
+
+void QSocketActiveObject::RunL()
+{
+ if (maybeDeferSocketEvent())
+ return;
+ if (maybeQueueForLater())
+ return;
+
+ QT_TRYCATCH_LEAVING(run());
+}
+
+void QSocketActiveObject::run()
+{
+ QEvent e(QEvent::SockAct);
+ m_inSocketEvent = true;
+ QCoreApplication::sendEvent(m_notifier, &e);
+ m_inSocketEvent = false;
+
+ if (m_deleteLater) {
+ delete this;
+ } else {
+ iStatus = KRequestPending;
+ SetActive();
+ m_dispatcher->reactivateSocketNotifier(m_notifier);
+ }
+}
+
+void QSocketActiveObject::deleteLater()
+{
+ if (m_inSocketEvent) {
+ m_deleteLater = true;
+ } else {
+ delete this;
+ }
+}
+
+#ifdef QT_SYMBIAN_PRIORITY_DROP
+class QIdleDetectorThread
+{
+public:
+ QIdleDetectorThread()
+ : m_state(STATE_RUN), m_stop(false), m_running(false)
+ {
+ start();
+ }
+
+ ~QIdleDetectorThread()
+ {
+ stop();
+ }
+
+ void start()
+ {
+ QMutexLocker lock(&m_mutex);
+ if (m_running)
+ return;
+ m_stop = false;
+ m_state = STATE_RUN;
+ TInt err = m_idleDetectorThread.Create(KNullDesC(), &idleDetectorThreadFunc, 1024, &User::Allocator(), this);
+ if (err != KErrNone)
+ return; // Fail silently on error. Next kick will try again. Exception might stop the event being processed
+ m_idleDetectorThread.SetPriority(EPriorityAbsoluteBackgroundNormal);
+ m_idleDetectorThread.Resume();
+ m_running = true;
+ // get a callback from QCoreApplication destruction to stop this thread
+ qAddPostRoutine(StopIdleDetectorThread);
+ }
+
+ void stop()
+ {
+ QMutexLocker lock(&m_mutex);
+ if (!m_running)
+ return;
+ // close down the idle thread because if corelib is loaded temporarily, this would leak threads into the host process
+ m_stop = true;
+ m_kick.release();
+ m_idleDetectorThread.SetPriority(EPriorityNormal);
+ TRequestStatus s;
+ m_idleDetectorThread.Logon(s);
+ User::WaitForRequest(s);
+ m_idleDetectorThread.Close();
+ m_running = false;
+ }
+
+ void kick()
+ {
+ start();
+ m_state = STATE_KICKED;
+ m_kick.release();
+ }
+
+ bool hasRun()
+ {
+ return m_state == STATE_RUN;
+ }
+
+private:
+ static TInt idleDetectorThreadFunc(TAny* self)
+ {
+ User::RenameThread(_L("IdleDetectorThread"));
+ static_cast<QIdleDetectorThread*>(self)->IdleLoop();
+ return KErrNone;
+ }
+
+ void IdleLoop()
+ {
+ while (!m_stop) {
+ m_kick.acquire();
+ m_state = STATE_RUN;
+ }
+ }
+
+ static void StopIdleDetectorThread();
+
+private:
+ enum IdleStates {STATE_KICKED, STATE_RUN} m_state;
+ bool m_stop;
+ bool m_running;
+ RThread m_idleDetectorThread;
+ QSemaphore m_kick;
+ QMutex m_mutex;
+};
+
+Q_GLOBAL_STATIC(QIdleDetectorThread, idleDetectorThread);
+
+void QIdleDetectorThread::StopIdleDetectorThread()
+{
+ idleDetectorThread()->stop();
+}
+
+const int maxBusyTime = 2000; // maximum time we allow idle detector to be blocked before worrying, in milliseconds
+const int baseDelay = 1000; // minimum delay time used when backing off to allow idling, in microseconds
+#endif
+
+QEventDispatcherSymbian::QEventDispatcherSymbian(QObject *parent)
+ : QAbstractEventDispatcher(parent),
+ m_selectThread(0),
+ m_activeScheduler(0),
+ m_wakeUpAO(0),
+ m_completeDeferredAOs(0),
+ m_interrupt(false),
+ m_wakeUpDone(0),
+ m_iterationCount(0),
+ m_insideTimerEvent(false),
+ m_noSocketEvents(false)
+{
+#ifdef QT_SYMBIAN_PRIORITY_DROP
+ m_delay = baseDelay;
+ m_avgEventTime = 0;
+ idleDetectorThread();
+#endif
+}
+
+QEventDispatcherSymbian::~QEventDispatcherSymbian()
+{
+}
+
+void QEventDispatcherSymbian::startingUp()
+{
+ if( !CActiveScheduler::Current() ) {
+ m_activeScheduler = q_check_ptr(new CQtActiveScheduler()); // CBase derived class needs to be checked on new
+ CActiveScheduler::Install(m_activeScheduler);
+ }
+ m_wakeUpAO = q_check_ptr(new QWakeUpActiveObject(this));
+ m_completeDeferredAOs = q_check_ptr(new QCompleteDeferredAOs(this));
+ // We already might have posted events, wakeup once to process them
+ wakeUp();
+}
+
+QSelectThread& QEventDispatcherSymbian::selectThread() {
+ if (!m_selectThread)
+ m_selectThread = new QSelectThread;
+ return *m_selectThread;
+}
+
+void QEventDispatcherSymbian::closingDown()
+{
+ if (m_selectThread && m_selectThread->isRunning()) {
+ m_selectThread->stop();
+ }
+ delete m_selectThread;
+ m_selectThread = 0;
+
+ delete m_completeDeferredAOs;
+ delete m_wakeUpAO;
+ if (m_activeScheduler) {
+ delete m_activeScheduler;
+ }
+}
+
+bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags flags )
+{
+ bool handledAnyEvent = false;
+ bool oldNoSocketEventsValue = m_noSocketEvents;
+ bool oldInsideTimerEventValue = m_insideTimerEvent;
+
+ m_insideTimerEvent = false;
+
+ QT_TRY {
+ Q_D(QAbstractEventDispatcher);
+
+ // It is safe if this counter overflows. The main importance is that each
+ // iteration count is different from the last.
+ m_iterationCount++;
+
+ RThread &thread = d->threadData->symbian_thread_handle;
+
+ bool block;
+ if (flags & QEventLoop::WaitForMoreEvents) {
+ block = true;
+ emit aboutToBlock();
+ } else {
+ block = false;
+ }
+
+ if (flags & QEventLoop::ExcludeSocketNotifiers) {
+ m_noSocketEvents = true;
+ } else {
+ m_noSocketEvents = false;
+ handledAnyEvent = sendDeferredSocketEvents();
+ }
+
+ bool handledSymbianEvent = false;
+ m_interrupt = false;
+
+#ifdef QT_SYMBIAN_PRIORITY_DROP
+ QElapsedTimer eventTimer;
+#endif
+
+ while (1) {
+ if (block) {
+ // This is where Qt will spend most of its time.
+ CActiveScheduler::Current()->WaitForAnyRequest();
+ } else {
+ if (thread.RequestCount() == 0) {
+#ifdef QT_SYMBIAN_PRIORITY_DROP
+ if (idleDetectorThread()->hasRun()) {
+ m_lastIdleRequestTimer.start();
+ idleDetectorThread()->kick();
+ } else if (m_lastIdleRequestTimer.elapsed() > maxBusyTime) {
+ User::AfterHighRes(m_delay);
+ }
+#endif
+ break;
+ }
+ // This one should return without delay.
+ CActiveScheduler::Current()->WaitForAnyRequest();
+ }
+
+#ifdef QT_SYMBIAN_PRIORITY_DROP
+ if (idleDetectorThread()->hasRun()) {
+ if (m_delay > baseDelay)
+ m_delay -= baseDelay;
+ m_lastIdleRequestTimer.start();
+ idleDetectorThread()->kick();
+ } else if (m_lastIdleRequestTimer.elapsed() > maxBusyTime) {
+ User::AfterHighRes(m_delay);
+ // allow delay to be up to 1/4 of execution time
+ if (!idleDetectorThread()->hasRun() && m_delay*3 < m_avgEventTime)
+ m_delay += baseDelay;
+ }
+ eventTimer.start();
+#endif
+
+ TInt error;
+ handledSymbianEvent = CActiveScheduler::RunIfReady(error, KMinTInt);
+ if (error) {
+ qWarning("CActiveScheduler::RunIfReady() returned error: %i\n", error);
+ CActiveScheduler::Current()->Error(error);
+ }
+
+#ifdef QT_SYMBIAN_PRIORITY_DROP
+ int eventDur = eventTimer.elapsed()*1000;
+ // average is calcualted as a 5% decaying exponential average
+ m_avgEventTime = (m_avgEventTime * 95 + eventDur * 5) / 100;
+#endif
+
+ if (!handledSymbianEvent) {
+ qFatal("QEventDispatcherSymbian::processEvents(): Caught Symbian stray signal");
+ }
+ handledAnyEvent = true;
+ if (m_interrupt) {
+ break;
+ }
+ block = false;
+ };
+
+ emit awake();
+ } QT_CATCH (const std::exception& ex) {
+#ifndef QT_NO_EXCEPTIONS
+ CActiveScheduler::Current()->Error(qt_symbian_exception2Error(ex));
+#endif
+ }
+
+ m_noSocketEvents = oldNoSocketEventsValue;
+ m_insideTimerEvent = oldInsideTimerEventValue;
+
+ return handledAnyEvent;
+}
+
+void QEventDispatcherSymbian::timerFired(int timerId)
+{
+ QHash<int, SymbianTimerInfoPtr>::iterator i = m_timerList.find(timerId);
+ if (i == m_timerList.end()) {
+ // The timer has been deleted. Ignore this event.
+ return;
+ }
+
+ SymbianTimerInfoPtr timerInfo = *i;
+
+ // Prevent infinite timer recursion.
+ if (timerInfo->inTimerEvent) {
+ return;
+ }
+
+ timerInfo->inTimerEvent = true;
+ bool oldInsideTimerEventValue = m_insideTimerEvent;
+ m_insideTimerEvent = true;
+
+ QTimerEvent event(timerInfo->timerId);
+ QCoreApplication::sendEvent(timerInfo->receiver, &event);
+
+ m_insideTimerEvent = oldInsideTimerEventValue;
+ timerInfo->inTimerEvent = false;
+
+ return;
+}
+
+void QEventDispatcherSymbian::wakeUpWasCalled()
+{
+ // The reactivation should happen in RunL, right before the call to this function.
+ // This is because m_wakeUpDone is the "signal" that the object can be completed
+ // once more.
+ // Also, by dispatching the posted events after resetting m_wakeUpDone, we guarantee
+ // that no posted event notification will be lost. If we did it the other way
+ // around, it would be possible for another thread to post an event right after
+ // the sendPostedEvents was done, but before the object was ready to be completed
+ // again. This could deadlock the application if there are no other posted events.
+ m_wakeUpDone.fetchAndStoreOrdered(0);
+ sendPostedEvents();
+}
+
+void QEventDispatcherSymbian::interrupt()
+{
+ m_interrupt = true;
+ wakeUp();
+}
+
+void QEventDispatcherSymbian::wakeUp()
+{
+ Q_D(QAbstractEventDispatcher);
+
+ if (m_wakeUpAO && m_wakeUpDone.testAndSetAcquire(0, 1)) {
+ TRequestStatus *status = &m_wakeUpAO->iStatus;
+ QEventDispatcherSymbian::RequestComplete(d->threadData->symbian_thread_handle, status, KErrNone);
+ }
+}
+
+bool QEventDispatcherSymbian::sendPostedEvents()
+{
+ Q_D(QAbstractEventDispatcher);
+
+ // moveToThread calls this and canWait == true -> Events will never get processed
+ // if we check for d->threadData->canWait
+ //
+ // QCoreApplication::postEvent sets canWait = false, but after the object and events
+ // are moved to a new thread, the canWait in new thread is true i.e. not changed to reflect
+ // the flag on old thread. That's why events in a new thread will not get processed.
+ // This migth be actually bug in moveToThread functionality, but because other platforms
+ // do not check canWait in wakeUp (where we essentially are now) - decided to remove it from
+ // here as well.
+
+ //if (!d->threadData->canWait) {
+ QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData);
+ return true;
+ //}
+ //return false;
+}
+
+inline void QEventDispatcherSymbian::addDeferredActiveObject(QActiveObject *object)
+{
+ queueDeferredActiveObjectsCompletion();
+ m_deferredActiveObjects.append(object);
+}
+
+inline void QEventDispatcherSymbian::removeDeferredActiveObject(QActiveObject *object)
+{
+ m_deferredActiveObjects.removeAll(object);
+ m_deferredSocketEvents.removeAll(object);
+}
+
+inline void QEventDispatcherSymbian::addDeferredSocketActiveObject(QActiveObject *object)
+{
+ m_deferredSocketEvents.append(object);
+}
+
+void QEventDispatcherSymbian::queueDeferredActiveObjectsCompletion()
+{
+ m_completeDeferredAOs->complete();
+}
+
+void QEventDispatcherSymbian::reactivateDeferredActiveObjects()
+{
+ while (!m_deferredActiveObjects.isEmpty()) {
+ QActiveObject *object = m_deferredActiveObjects.takeFirst();
+ object->reactivateAndComplete();
+ }
+
+ // We do this because we want to return from processEvents. This is because
+ // each invocation of processEvents should only run each active object once.
+ // The active scheduler should run them continously, however.
+ m_interrupt = true;
+}
+
+bool QEventDispatcherSymbian::sendDeferredSocketEvents()
+{
+ bool sentAnyEvents = false;
+ while (!m_deferredSocketEvents.isEmpty()) {
+ sentAnyEvents = true;
+ QActiveObject *object = m_deferredSocketEvents.takeFirst();
+ object->reactivateAndComplete();
+ }
+
+ return sentAnyEvents;
+}
+
+void QEventDispatcherSymbian::flush()
+{
+}
+
+bool QEventDispatcherSymbian::hasPendingEvents()
+{
+ Q_D(QAbstractEventDispatcher);
+ return (d->threadData->symbian_thread_handle.RequestCount() != 0
+ || !d->threadData->canWait || !m_deferredSocketEvents.isEmpty());
+}
+
+void QEventDispatcherSymbian::registerSocketNotifier ( QSocketNotifier * notifier )
+{
+ //note - this is only for "open C" file descriptors
+ //for native sockets, an active object in the symbian socket engine handles this
+ QSocketActiveObject *socketAO = new QSocketActiveObject(this, notifier);
+ Q_CHECK_PTR(socketAO);
+ m_notifiers.insert(notifier, socketAO);
+ selectThread().requestSocketEvents(notifier, &socketAO->iStatus);
+}
+
+void QEventDispatcherSymbian::unregisterSocketNotifier ( QSocketNotifier * notifier )
+{
+ //note - this is only for "open C" file descriptors
+ //for native sockets, an active object in the symbian socket engine handles this
+ if (m_selectThread)
+ m_selectThread->cancelSocketEvents(notifier);
+ if (m_notifiers.contains(notifier)) {
+ QSocketActiveObject *sockObj = *m_notifiers.find(notifier);
+ m_deferredSocketEvents.removeAll(sockObj);
+ sockObj->deleteLater();
+ m_notifiers.remove(notifier);
+ }
+}
+
+void QEventDispatcherSymbian::reactivateSocketNotifier(QSocketNotifier *notifier)
+{
+ selectThread().requestSocketEvents(notifier, &m_notifiers[notifier]->iStatus);
+}
+
+void QEventDispatcherSymbian::registerTimer ( int timerId, int interval, QObject * object )
+{
+ if (interval < 0) {
+ qWarning("Timer interval < 0");
+ interval = 0;
+ }
+
+ SymbianTimerInfoPtr timer(new SymbianTimerInfo);
+ timer->timerId = timerId;
+ timer->interval = interval;
+ timer->inTimerEvent = false;
+ timer->receiver = object;
+ timer->dispatcher = this;
+ timer->timerAO = q_check_ptr(new QTimerActiveObject(this, timer.data()));
+ m_timerList.insert(timerId, timer);
+
+ timer->timerAO->Start();
+
+ if (m_insideTimerEvent)
+ // If we are inside a timer event, we need to prevent event starvation
+ // by preventing newly created timers from running in the same event processing
+ // iteration. Do this by calling the maybeQueueForLater() function to "fake" that we have
+ // already run once. This will cause the next run to be added to the deferred
+ // queue instead.
+ timer->timerAO->maybeQueueForLater();
+}
+
+bool QEventDispatcherSymbian::unregisterTimer ( int timerId )
+{
+ if (!m_timerList.contains(timerId)) {
+ return false;
+ }
+
+ SymbianTimerInfoPtr timerInfo = m_timerList.take(timerId);
+
+ if (!QObjectPrivate::get(timerInfo->receiver)->inThreadChangeEvent)
+ QAbstractEventDispatcherPrivate::releaseTimerId(timerId);
+
+ return true;
+}
+
+bool QEventDispatcherSymbian::unregisterTimers ( QObject * object )
+{
+ if (m_timerList.isEmpty())
+ return false;
+
+ bool unregistered = false;
+ for (QHash<int, SymbianTimerInfoPtr>::iterator i = m_timerList.begin(); i != m_timerList.end(); ) {
+ if ((*i)->receiver == object) {
+ i = m_timerList.erase(i);
+ unregistered = true;
+ } else {
+ ++i;
+ }
+ }
+
+ return unregistered;
+}
+
+QList<QEventDispatcherSymbian::TimerInfo> QEventDispatcherSymbian::registeredTimers ( QObject * object ) const
+{
+ QList<TimerInfo> list;
+ for (QHash<int, SymbianTimerInfoPtr>::const_iterator i = m_timerList.begin(); i != m_timerList.end(); ++i) {
+ if ((*i)->receiver == object) {
+ list.push_back(TimerInfo((*i)->timerId, (*i)->interval));
+ }
+ }
+
+ return list;
+}
+
+/*
+ * This active scheduler class implements a simple report and continue policy, for Symbian OS leaves
+ * or exceptions from Qt that fall back to the scheduler.
+ * It will be used in cases where there is no existing active scheduler installed.
+ * Apps which link to qts60main.lib will have the UI active scheduler installed in the main thread
+ * instead of this one. But this would be used in other threads in the UI.
+ * An app could replace this behaviour by installing an alternative active scheduler.
+ */
+void CQtActiveScheduler::Error(TInt aError) const
+{
+ QT_TRY {
+ qWarning("Error from active scheduler %d", aError);
+ }
+ QT_CATCH (const std::bad_alloc&) {} // ignore alloc fails, nothing more can be done
+}
+
+bool QActiveObject::wait(CActive* ao, int ms)
+{
+ if (!ao->IsActive())
+ return true; //request already complete
+ bool timedout = false;
+ if (ms > 0) {
+ TRequestStatus tstat;
+ RTimer t;
+ if (KErrNone != t.CreateLocal())
+ return false;
+ t.HighRes(tstat, ms*1000);
+ User::WaitForRequest(tstat, ao->iStatus);
+ if (tstat != KRequestPending) {
+ timedout = true;
+ } else {
+ t.Cancel();
+ //balance thread semaphore
+ User::WaitForRequest(tstat);
+ }
+ t.Close();
+ } else {
+ User::WaitForRequest(ao->iStatus);
+ }
+ if (timedout)
+ return false;
+
+ //evil cast to allow calling of protected virtual
+ ((QActiveObject*)ao)->RunL();
+
+ //clear active & pending flags
+ ao->iStatus = TRequestStatus();
+
+ return true;
+}
+
+bool QActiveObject::wait(QList<CActive*> aos, int ms)
+{
+ QVector<TRequestStatus*> stati;
+ stati.reserve(aos.count() + 1);
+ foreach (CActive* ao, aos) {
+ if (!ao->IsActive())
+ return true; //request already complete
+ stati.append(&(ao->iStatus));
+ }
+ bool timedout = false;
+ TRequestStatus tstat;
+ RTimer t;
+ if (ms > 0) {
+ if (KErrNone != t.CreateLocal())
+ return false;
+ t.HighRes(tstat, ms*1000);
+ stati.append(&tstat);
+ }
+ User::WaitForNRequest(stati.data(), stati.count());
+ if (ms > 0) {
+ if (tstat != KRequestPending) {
+ timedout = true;
+ } else {
+ t.Cancel();
+ //balance thread semaphore
+ User::WaitForRequest(tstat);
+ }
+ t.Close();
+ }
+ if (timedout)
+ return false;
+
+ foreach (CActive* ao, aos) {
+ if (ao->iStatus != KRequestPending) {
+ //evil cast to allow calling of protected virtual
+ ((QActiveObject*)ao)->RunL();
+
+ //clear active & pending flags
+ ao->iStatus = TRequestStatus();
+ break; //only call one
+ }
+ }
+ return true;
+}
+
+QT_END_NAMESPACE
+
+#include "moc_qeventdispatcher_symbian_p.cpp"
diff --git a/src/corelib/kernel/qeventdispatcher_symbian_p.h b/src/corelib/kernel/qeventdispatcher_symbian_p.h
new file mode 100644
index 0000000000..6fdd4b248d
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_symbian_p.h
@@ -0,0 +1,327 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QEVENTDISPATCHER_SYMBIAN_P_H
+#define QEVENTDISPATCHER_SYMBIAN_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <qhash.h>
+#include <qset.h>
+#include <qshareddata.h>
+#include <qabstracteventdispatcher.h>
+#include <private/qabstracteventdispatcher_p.h>
+#include <qthread.h>
+#include <qmutex.h>
+#include <qwaitcondition.h>
+#include <qsocketnotifier.h>
+#include <qdatetime.h>
+#include <qelapsedtimer.h>
+
+#include <e32base.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/select.h>
+
+QT_BEGIN_NAMESPACE
+
+
+class QEventDispatcherSymbian;
+class QTimerActiveObject;
+
+class Q_CORE_EXPORT QActiveObject : public CActive
+{
+public:
+ QActiveObject(TInt priority, QEventDispatcherSymbian *dispatcher);
+ ~QActiveObject();
+
+ bool maybeQueueForLater();
+ bool maybeDeferSocketEvent();
+
+ void reactivateAndComplete();
+
+ static bool wait(CActive* ao, int ms);
+ static bool wait(QList<CActive*> aos, int ms);
+protected:
+ QEventDispatcherSymbian *m_dispatcher;
+
+private:
+ bool m_hasAlreadyRun : 1;
+ bool m_hasRunAgain : 1;
+ int m_iterationCount;
+};
+
+class QWakeUpActiveObject : public QActiveObject
+{
+public:
+ QWakeUpActiveObject(QEventDispatcherSymbian *dispatcher);
+ ~QWakeUpActiveObject();
+
+ void Complete();
+
+protected:
+ void DoCancel();
+ void RunL();
+
+private:
+ TThreadId m_hostThreadId;
+};
+
+struct SymbianTimerInfo : public QSharedData
+{
+ SymbianTimerInfo();
+ ~SymbianTimerInfo();
+
+ int timerId;
+ int interval;
+ int msLeft;
+ bool inTimerEvent;
+ QObject *receiver;
+ QTimerActiveObject *timerAO;
+ QEventDispatcherSymbian *dispatcher;
+};
+
+typedef QExplicitlySharedDataPointer<SymbianTimerInfo> SymbianTimerInfoPtr;
+
+// This is a bit of a proxy class. See comments in SetActive and Start for details.
+class QTimerActiveObject : public QActiveObject
+{
+public:
+ QTimerActiveObject(QEventDispatcherSymbian *dispatcher, SymbianTimerInfo *timerInfo);
+ ~QTimerActiveObject();
+
+ void Start();
+
+protected:
+ void DoCancel();
+ void RunL();
+
+private:
+ void Run();
+ void StartTimer();
+
+private:
+ SymbianTimerInfo *m_timerInfo;
+ QElapsedTimer m_timeoutTimer;
+ int m_expectedTimeSinceLastEvent;
+ RTimer m_rTimer;
+};
+
+class QCompleteDeferredAOs : public CActive
+{
+public:
+ QCompleteDeferredAOs(QEventDispatcherSymbian *dispatcher);
+ ~QCompleteDeferredAOs();
+
+ void complete();
+
+protected:
+ void DoCancel();
+ void RunL();
+
+private:
+ QEventDispatcherSymbian *m_dispatcher;
+};
+
+class QSocketActiveObject : public QActiveObject
+{
+public:
+ QSocketActiveObject(QEventDispatcherSymbian *dispatcher, QSocketNotifier *notifier);
+ ~QSocketActiveObject();
+
+ void deleteLater();
+
+protected:
+ void DoCancel();
+ void RunL();
+ void run();
+
+private:
+ QSocketNotifier *m_notifier;
+ bool m_inSocketEvent;
+ bool m_deleteLater;
+
+ friend class QEventDispatcherSymbian;
+};
+
+class QSelectThread : public QThread
+{
+ Q_DECLARE_PRIVATE(QThread)
+
+public:
+ QSelectThread();
+ ~QSelectThread();
+
+ void requestSocketEvents ( QSocketNotifier *notifier, TRequestStatus *status );
+ void cancelSocketEvents ( QSocketNotifier *notifier );
+ void restart();
+ void stop();
+
+protected:
+ void run();
+
+private:
+ int updateSocketSet(QSocketNotifier::Type type, fd_set *fds);
+ void updateActivatedNotifiers(QSocketNotifier::Type type, fd_set *fds);
+
+private:
+ int m_pipeEnds[2];
+ QHash<QSocketNotifier *, TRequestStatus *> m_AOStatuses;
+ QMutex m_mutex;
+ QWaitCondition m_waitCond;
+ bool m_quit;
+};
+
+class Q_CORE_EXPORT CQtActiveScheduler : public CActiveScheduler
+{
+public: // from CActiveScheduler
+ virtual void Error(TInt aError) const;
+};
+
+class Q_CORE_EXPORT QEventDispatcherSymbian : public QAbstractEventDispatcher
+{
+ Q_OBJECT
+ Q_DECLARE_PRIVATE(QAbstractEventDispatcher)
+
+public:
+ QEventDispatcherSymbian(QObject *parent = 0);
+ ~QEventDispatcherSymbian();
+
+ void flush();
+ bool hasPendingEvents();
+ void interrupt();
+ bool processEvents ( QEventLoop::ProcessEventsFlags flags );
+ void registerSocketNotifier ( QSocketNotifier * notifier );
+ void registerTimer ( int timerId, int interval, QObject * object );
+ QList<TimerInfo> registeredTimers ( QObject * object ) const;
+ void unregisterSocketNotifier ( QSocketNotifier * notifier );
+ bool unregisterTimer ( int timerId );
+ bool unregisterTimers ( QObject * object );
+ void wakeUp();
+
+ void startingUp();
+ void closingDown();
+
+ void timerFired(int timerId);
+ void wakeUpWasCalled();
+ void reactivateSocketNotifier(QSocketNotifier *notifier);
+
+ void addDeferredActiveObject(QActiveObject *object);
+ void removeDeferredActiveObject(QActiveObject *object);
+ void queueDeferredActiveObjectsCompletion();
+ // Can be overridden to activate local active objects too, but do call baseclass!
+ virtual void reactivateDeferredActiveObjects();
+
+ inline int iterationCount() const { return m_iterationCount; }
+
+ void addDeferredSocketActiveObject(QActiveObject *object);
+ inline bool areSocketEventsBlocked() const { return m_noSocketEvents; }
+
+ static void RequestComplete(TRequestStatus *&status, TInt reason);
+ static void RequestComplete(RThread &threadHandle, TRequestStatus *&status, TInt reason);
+
+private:
+ bool sendPostedEvents();
+ bool sendDeferredSocketEvents();
+
+ QSelectThread& selectThread();
+private:
+ QSelectThread *m_selectThread;
+
+ CQtActiveScheduler *m_activeScheduler;
+
+ QHash<int, SymbianTimerInfoPtr> m_timerList;
+ QHash<QSocketNotifier *, QSocketActiveObject *> m_notifiers;
+
+ QWakeUpActiveObject *m_wakeUpAO;
+ QCompleteDeferredAOs *m_completeDeferredAOs;
+
+ volatile bool m_interrupt;
+ QAtomicInt m_wakeUpDone;
+
+ unsigned char m_iterationCount;
+ bool m_insideTimerEvent;
+ bool m_noSocketEvents;
+ //deferred until socket events are enabled
+ QList<QActiveObject *> m_deferredSocketEvents;
+ //deferred until idle
+ QList<QActiveObject *> m_deferredActiveObjects;
+
+ int m_delay;
+ int m_avgEventTime;
+ QElapsedTimer m_lastIdleRequestTimer;
+};
+
+#ifdef QT_DEBUG
+# define VERIFY_PENDING_REQUEST_STATUS \
+ Q_ASSERT(status->Int() == KRequestPending);
+#else
+# define VERIFY_PENDING_REQUEST_STATUS
+#endif
+
+// Convenience functions for doing some sanity checking on our own complete code.
+// Unless QT_DEBUG is defined, it is exactly equivalent to the Symbian version.
+inline void QEventDispatcherSymbian::RequestComplete(TRequestStatus *&status, TInt reason)
+{
+ VERIFY_PENDING_REQUEST_STATUS
+ User::RequestComplete(status, reason);
+}
+inline void QEventDispatcherSymbian::RequestComplete(RThread &threadHandle, TRequestStatus *&status, TInt reason)
+{
+ VERIFY_PENDING_REQUEST_STATUS
+ threadHandle.RequestComplete(status, reason);
+}
+
+#undef VERIFY_PENDING_REQUEST_STATUS
+
+QT_END_NAMESPACE
+
+#endif // QEVENTDISPATCHER_SYMBIAN_P_H
diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp
new file mode 100644
index 0000000000..dceb51d181
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_unix.cpp
@@ -0,0 +1,979 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qplatformdefs.h"
+
+#include "qcoreapplication.h"
+#include "qpair.h"
+#include "qsocketnotifier.h"
+#include "qthread.h"
+#include "qelapsedtimer.h"
+
+#include "qeventdispatcher_unix_p.h"
+#include <private/qthread_p.h>
+#include <private/qcoreapplication_p.h>
+#include <private/qcore_unix_p.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+// VxWorks doesn't correctly set the _POSIX_... options
+#if defined(Q_OS_VXWORKS)
+# if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK <= 0)
+# undef _POSIX_MONOTONIC_CLOCK
+# define _POSIX_MONOTONIC_CLOCK 1
+# endif
+# include <pipeDrv.h>
+# include <selectLib.h>
+#endif
+
+#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) || defined(QT_BOOTSTRAPPED)
+# include <sys/times.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+Q_CORE_EXPORT bool qt_disable_lowpriority_timers=false;
+
+/*****************************************************************************
+ UNIX signal handling
+ *****************************************************************************/
+
+static sig_atomic_t signal_received;
+static sig_atomic_t signals_fired[NSIG];
+
+static void signalHandler(int sig)
+{
+ signals_fired[sig] = 1;
+ signal_received = 1;
+}
+
+
+#if defined(Q_OS_INTEGRITY) || defined(Q_OS_VXWORKS)
+static void initThreadPipeFD(int fd)
+{
+ int ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
+ if (ret == -1)
+ perror("QEventDispatcherUNIXPrivate: Unable to init thread pipe");
+
+ int flags = fcntl(fd, F_GETFL);
+ if (flags == -1)
+ perror("QEventDispatcherUNIXPrivate: Unable to get flags on thread pipe");
+
+ ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+ if (ret == -1)
+ perror("QEventDispatcherUNIXPrivate: Unable to set flags on thread pipe");
+}
+#endif
+
+QEventDispatcherUNIXPrivate::QEventDispatcherUNIXPrivate()
+{
+ extern Qt::HANDLE qt_application_thread_id;
+ mainThread = (QThread::currentThreadId() == qt_application_thread_id);
+ bool pipefail = false;
+
+ // initialize the common parts of the event loop
+#if defined(Q_OS_NACL)
+ // do nothing.
+#elif defined(Q_OS_INTEGRITY)
+ // INTEGRITY doesn't like a "select" on pipes, so use socketpair instead
+ if (socketpair(AF_INET, SOCK_STREAM, 0, thread_pipe) == -1) {
+ perror("QEventDispatcherUNIXPrivate(): Unable to create socket pair");
+ pipefail = true;
+ } else {
+ initThreadPipeFD(thread_pipe[0]);
+ initThreadPipeFD(thread_pipe[1]);
+ }
+#elif defined(Q_OS_VXWORKS)
+ char name[20];
+ qsnprintf(name, sizeof(name), "/pipe/qt_%08x", int(taskIdCurrent));
+
+ // make sure there is no pipe with this name
+ pipeDevDelete(name, true);
+ // create the pipe
+ if (pipeDevCreate(name, 128 /*maxMsg*/, 1 /*maxLength*/) != OK) {
+ perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe device");
+ pipefail = true;
+ } else {
+ if ((thread_pipe[0] = open(name, O_RDWR, 0)) < 0) {
+ perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe");
+ pipefail = true;
+ } else {
+ initThreadPipeFD(thread_pipe[0]);
+ thread_pipe[1] = thread_pipe[0];
+ }
+ }
+#else
+ if (qt_safe_pipe(thread_pipe, O_NONBLOCK) == -1) {
+ perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe");
+ pipefail = true;
+ }
+#endif
+
+ if (pipefail)
+ qFatal("QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe");
+
+ sn_highest = -1;
+
+ interrupt = false;
+}
+
+QEventDispatcherUNIXPrivate::~QEventDispatcherUNIXPrivate()
+{
+#if defined(Q_OS_NACL)
+ // do nothing.
+#elif defined(Q_OS_VXWORKS)
+ close(thread_pipe[0]);
+
+ char name[20];
+ qsnprintf(name, sizeof(name), "/pipe/qt_%08x", int(taskIdCurrent));
+
+ pipeDevDelete(name, true);
+#else
+ // cleanup the common parts of the event loop
+ close(thread_pipe[0]);
+ close(thread_pipe[1]);
+#endif
+
+ // cleanup timers
+ qDeleteAll(timerList);
+}
+
+int QEventDispatcherUNIXPrivate::doSelect(QEventLoop::ProcessEventsFlags flags, timeval *timeout)
+{
+ Q_Q(QEventDispatcherUNIX);
+
+ // needed in QEventDispatcherUNIX::select()
+ timerList.updateCurrentTime();
+
+ int nsel;
+ do {
+ if (mainThread) {
+ while (signal_received) {
+ signal_received = 0;
+ for (int i = 0; i < NSIG; ++i) {
+ if (signals_fired[i]) {
+ signals_fired[i] = 0;
+ emit QCoreApplication::instance()->unixSignal(i);
+ }
+ }
+ }
+ }
+
+ // Process timers and socket notifiers - the common UNIX stuff
+ int highest = 0;
+ if (! (flags & QEventLoop::ExcludeSocketNotifiers) && (sn_highest >= 0)) {
+ // return the highest fd we can wait for input on
+ sn_vec[0].select_fds = sn_vec[0].enabled_fds;
+ sn_vec[1].select_fds = sn_vec[1].enabled_fds;
+ sn_vec[2].select_fds = sn_vec[2].enabled_fds;
+ highest = sn_highest;
+ } else {
+ FD_ZERO(&sn_vec[0].select_fds);
+ FD_ZERO(&sn_vec[1].select_fds);
+ FD_ZERO(&sn_vec[2].select_fds);
+ }
+
+ FD_SET(thread_pipe[0], &sn_vec[0].select_fds);
+ highest = qMax(highest, thread_pipe[0]);
+
+ nsel = q->select(highest + 1,
+ &sn_vec[0].select_fds,
+ &sn_vec[1].select_fds,
+ &sn_vec[2].select_fds,
+ timeout);
+ } while (nsel == -1 && (errno == EINTR || errno == EAGAIN));
+
+ if (nsel == -1) {
+ if (errno == EBADF) {
+ // it seems a socket notifier has a bad fd... find out
+ // which one it is and disable it
+ fd_set fdset;
+ timeval tm;
+ tm.tv_sec = tm.tv_usec = 0l;
+
+ for (int type = 0; type < 3; ++type) {
+ QSockNotType::List &list = sn_vec[type].list;
+ if (list.size() == 0)
+ continue;
+
+ for (int i = 0; i < list.size(); ++i) {
+ QSockNot *sn = list[i];
+
+ FD_ZERO(&fdset);
+ FD_SET(sn->fd, &fdset);
+
+ int ret = -1;
+ do {
+ switch (type) {
+ case 0: // read
+ ret = select(sn->fd + 1, &fdset, 0, 0, &tm);
+ break;
+ case 1: // write
+ ret = select(sn->fd + 1, 0, &fdset, 0, &tm);
+ break;
+ case 2: // except
+ ret = select(sn->fd + 1, 0, 0, &fdset, &tm);
+ break;
+ }
+ } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+
+ if (ret == -1 && errno == EBADF) {
+ // disable the invalid socket notifier
+ static const char *t[] = { "Read", "Write", "Exception" };
+ qWarning("QSocketNotifier: Invalid socket %d and type '%s', disabling...",
+ sn->fd, t[type]);
+ sn->obj->setEnabled(false);
+ }
+ }
+ }
+ } else {
+ // EINVAL... shouldn't happen, so let's complain to stderr
+ // and hope someone sends us a bug report
+ perror("select");
+ }
+ }
+
+ // some other thread woke us up... consume the data on the thread pipe so that
+ // select doesn't immediately return next time
+ int nevents = 0;
+ if (nsel > 0 && FD_ISSET(thread_pipe[0], &sn_vec[0].select_fds)) {
+#if defined(Q_OS_VXWORKS)
+ char c[16];
+ ::read(thread_pipe[0], c, sizeof(c));
+ ::ioctl(thread_pipe[0], FIOFLUSH, 0);
+#else
+ char c[16];
+ while (::read(thread_pipe[0], c, sizeof(c)) > 0)
+ ;
+#endif
+ if (!wakeUps.testAndSetRelease(1, 0)) {
+ // hopefully, this is dead code
+ qWarning("QEventDispatcherUNIX: internal error, wakeUps.testAndSetRelease(1, 0) failed!");
+ }
+ ++nevents;
+ }
+
+ // activate socket notifiers
+ if (! (flags & QEventLoop::ExcludeSocketNotifiers) && nsel > 0 && sn_highest >= 0) {
+ // if select says data is ready on any socket, then set the socket notifier
+ // to pending
+ for (int i=0; i<3; i++) {
+ QSockNotType::List &list = sn_vec[i].list;
+ for (int j = 0; j < list.size(); ++j) {
+ QSockNot *sn = list[j];
+ if (FD_ISSET(sn->fd, &sn_vec[i].select_fds))
+ q->setSocketNotifierPending(sn->obj);
+ }
+ }
+ }
+ return (nevents + q->activateSocketNotifiers());
+}
+
+/*
+ * Internal functions for manipulating timer data structures. The
+ * timerBitVec array is used for keeping track of timer identifiers.
+ */
+
+QTimerInfoList::QTimerInfoList()
+{
+#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) && !defined(Q_OS_NACL)
+ if (!QElapsedTimer::isMonotonic()) {
+ // not using monotonic timers, initialize the timeChanged() machinery
+ previousTime = qt_gettime();
+
+ tms unused;
+ previousTicks = times(&unused);
+
+ ticksPerSecond = sysconf(_SC_CLK_TCK);
+ msPerTick = 1000/ticksPerSecond;
+ } else {
+ // detected monotonic timers
+ previousTime.tv_sec = previousTime.tv_usec = 0;
+ previousTicks = 0;
+ ticksPerSecond = 0;
+ msPerTick = 0;
+ }
+#endif
+
+ firstTimerInfo = 0;
+}
+
+timeval QTimerInfoList::updateCurrentTime()
+{
+ return (currentTime = qt_gettime());
+}
+
+#if ((_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) && !defined(Q_OS_INTEGRITY)) || defined(QT_BOOTSTRAPPED)
+
+template <>
+timeval qAbs(const timeval &t)
+{
+ timeval tmp = t;
+ if (tmp.tv_sec < 0) {
+ tmp.tv_sec = -tmp.tv_sec - 1;
+ tmp.tv_usec -= 1000000;
+ }
+ if (tmp.tv_sec == 0 && tmp.tv_usec < 0) {
+ tmp.tv_usec = -tmp.tv_usec;
+ }
+ return normalizedTimeval(tmp);
+}
+
+/*
+ Returns true if the real time clock has changed by more than 10%
+ relative to the processor time since the last time this function was
+ called. This presumably means that the system time has been changed.
+
+ If /a delta is nonzero, delta is set to our best guess at how much the system clock was changed.
+*/
+bool QTimerInfoList::timeChanged(timeval *delta)
+{
+#ifdef Q_OS_NACL
+ Q_UNUSED(delta)
+ return false; // Calling "times" crashes.
+#endif
+ struct tms unused;
+ clock_t currentTicks = times(&unused);
+
+ clock_t elapsedTicks = currentTicks - previousTicks;
+ timeval elapsedTime = currentTime - previousTime;
+
+ timeval elapsedTimeTicks;
+ elapsedTimeTicks.tv_sec = elapsedTicks / ticksPerSecond;
+ elapsedTimeTicks.tv_usec = (((elapsedTicks * 1000) / ticksPerSecond) % 1000) * 1000;
+
+ timeval dummy;
+ if (!delta)
+ delta = &dummy;
+ *delta = elapsedTime - elapsedTimeTicks;
+
+ previousTicks = currentTicks;
+ previousTime = currentTime;
+
+ // If tick drift is more than 10% off compared to realtime, we assume that the clock has
+ // been set. Of course, we have to allow for the tick granularity as well.
+ timeval tickGranularity;
+ tickGranularity.tv_sec = 0;
+ tickGranularity.tv_usec = msPerTick * 1000;
+ return elapsedTimeTicks < ((qAbs(*delta) - tickGranularity) * 10);
+}
+
+void QTimerInfoList::repairTimersIfNeeded()
+{
+ if (QElapsedTimer::isMonotonic())
+ return;
+ timeval delta;
+ if (timeChanged(&delta))
+ timerRepair(delta);
+}
+
+#else // !(_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(QT_BOOTSTRAPPED)
+
+void QTimerInfoList::repairTimersIfNeeded()
+{
+}
+
+#endif
+
+/*
+ insert timer info into list
+*/
+void QTimerInfoList::timerInsert(QTimerInfo *ti)
+{
+ int index = size();
+ while (index--) {
+ register const QTimerInfo * const t = at(index);
+ if (!(ti->timeout < t->timeout))
+ break;
+ }
+ insert(index+1, ti);
+}
+
+/*
+ repair broken timer
+*/
+void QTimerInfoList::timerRepair(const timeval &diff)
+{
+ // repair all timers
+ for (int i = 0; i < size(); ++i) {
+ register QTimerInfo *t = at(i);
+ t->timeout = t->timeout + diff;
+ }
+}
+
+/*
+ Returns the time to wait for the next timer, or null if no timers
+ are waiting.
+*/
+bool QTimerInfoList::timerWait(timeval &tm)
+{
+ timeval currentTime = updateCurrentTime();
+ repairTimersIfNeeded();
+
+ // Find first waiting timer not already active
+ QTimerInfo *t = 0;
+ for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
+ if (!(*it)->activateRef) {
+ t = *it;
+ break;
+ }
+ }
+
+ if (!t)
+ return false;
+
+ if (currentTime < t->timeout) {
+ // time to wait
+ tm = t->timeout - currentTime;
+ } else {
+ // no time to wait
+ tm.tv_sec = 0;
+ tm.tv_usec = 0;
+ }
+
+ return true;
+}
+
+void QTimerInfoList::registerTimer(int timerId, int interval, QObject *object)
+{
+ QTimerInfo *t = new QTimerInfo;
+ t->id = timerId;
+ t->interval.tv_sec = interval / 1000;
+ t->interval.tv_usec = (interval % 1000) * 1000;
+ t->timeout = updateCurrentTime() + t->interval;
+ t->obj = object;
+ t->activateRef = 0;
+
+ timerInsert(t);
+}
+
+bool QTimerInfoList::unregisterTimer(int timerId)
+{
+ // set timer inactive
+ for (int i = 0; i < count(); ++i) {
+ register QTimerInfo *t = at(i);
+ if (t->id == timerId) {
+ // found it
+ removeAt(i);
+ if (t == firstTimerInfo)
+ firstTimerInfo = 0;
+ if (t->activateRef)
+ *(t->activateRef) = 0;
+
+ // release the timer id
+ if (!QObjectPrivate::get(t->obj)->inThreadChangeEvent)
+ QAbstractEventDispatcherPrivate::releaseTimerId(timerId);
+
+ delete t;
+ return true;
+ }
+ }
+ // id not found
+ return false;
+}
+
+bool QTimerInfoList::unregisterTimers(QObject *object)
+{
+ if (isEmpty())
+ return false;
+ for (int i = 0; i < count(); ++i) {
+ register QTimerInfo *t = at(i);
+ if (t->obj == object) {
+ // object found
+ removeAt(i);
+ if (t == firstTimerInfo)
+ firstTimerInfo = 0;
+ if (t->activateRef)
+ *(t->activateRef) = 0;
+
+ // release the timer id
+ if (!QObjectPrivate::get(t->obj)->inThreadChangeEvent)
+ QAbstractEventDispatcherPrivate::releaseTimerId(t->id);
+
+ delete t;
+ // move back one so that we don't skip the new current item
+ --i;
+ }
+ }
+ return true;
+}
+
+QList<QPair<int, int> > QTimerInfoList::registeredTimers(QObject *object) const
+{
+ QList<QPair<int, int> > list;
+ for (int i = 0; i < count(); ++i) {
+ register const QTimerInfo * const t = at(i);
+ if (t->obj == object)
+ list << QPair<int, int>(t->id, t->interval.tv_sec * 1000 + t->interval.tv_usec / 1000);
+ }
+ return list;
+}
+
+/*
+ Activate pending timers, returning how many where activated.
+*/
+int QTimerInfoList::activateTimers()
+{
+ if (qt_disable_lowpriority_timers || isEmpty())
+ return 0; // nothing to do
+
+ int n_act = 0, maxCount = 0;
+ firstTimerInfo = 0;
+
+ timeval currentTime = updateCurrentTime();
+ repairTimersIfNeeded();
+
+
+ // Find out how many timer have expired
+ for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
+ if (currentTime < (*it)->timeout)
+ break;
+ maxCount++;
+ }
+
+ //fire the timers.
+ while (maxCount--) {
+ if (isEmpty())
+ break;
+
+ QTimerInfo *currentTimerInfo = first();
+ if (currentTime < currentTimerInfo->timeout)
+ break; // no timer has expired
+
+ if (!firstTimerInfo) {
+ firstTimerInfo = currentTimerInfo;
+ } else if (firstTimerInfo == currentTimerInfo) {
+ // avoid sending the same timer multiple times
+ break;
+ } else if (currentTimerInfo->interval < firstTimerInfo->interval
+ || currentTimerInfo->interval == firstTimerInfo->interval) {
+ firstTimerInfo = currentTimerInfo;
+ }
+
+ // remove from list
+ removeFirst();
+
+ // determine next timeout time
+ currentTimerInfo->timeout += currentTimerInfo->interval;
+ if (currentTimerInfo->timeout < currentTime)
+ currentTimerInfo->timeout = currentTime + currentTimerInfo->interval;
+
+ // reinsert timer
+ timerInsert(currentTimerInfo);
+ if (currentTimerInfo->interval.tv_usec > 0 || currentTimerInfo->interval.tv_sec > 0)
+ n_act++;
+
+ if (!currentTimerInfo->activateRef) {
+ // send event, but don't allow it to recurse
+ currentTimerInfo->activateRef = &currentTimerInfo;
+
+ QTimerEvent e(currentTimerInfo->id);
+ QCoreApplication::sendEvent(currentTimerInfo->obj, &e);
+
+ if (currentTimerInfo)
+ currentTimerInfo->activateRef = 0;
+ }
+ }
+
+ firstTimerInfo = 0;
+ return n_act;
+}
+
+QEventDispatcherUNIX::QEventDispatcherUNIX(QObject *parent)
+ : QAbstractEventDispatcher(*new QEventDispatcherUNIXPrivate, parent)
+{ }
+
+QEventDispatcherUNIX::QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent)
+ : QAbstractEventDispatcher(dd, parent)
+{ }
+
+QEventDispatcherUNIX::~QEventDispatcherUNIX()
+{
+ Q_D(QEventDispatcherUNIX);
+ d->threadData->eventDispatcher = 0;
+}
+
+int QEventDispatcherUNIX::select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
+ timeval *timeout)
+{
+ return qt_safe_select(nfds, readfds, writefds, exceptfds, timeout);
+}
+
+/*!
+ \internal
+*/
+void QEventDispatcherUNIX::registerTimer(int timerId, int interval, QObject *obj)
+{
+#ifndef QT_NO_DEBUG
+ if (timerId < 1 || interval < 0 || !obj) {
+ qWarning("QEventDispatcherUNIX::registerTimer: invalid arguments");
+ return;
+ } else if (obj->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QObject::startTimer: timers cannot be started from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherUNIX);
+ d->timerList.registerTimer(timerId, interval, obj);
+}
+
+/*!
+ \internal
+*/
+bool QEventDispatcherUNIX::unregisterTimer(int timerId)
+{
+#ifndef QT_NO_DEBUG
+ if (timerId < 1) {
+ qWarning("QEventDispatcherUNIX::unregisterTimer: invalid argument");
+ return false;
+ } else if (thread() != QThread::currentThread()) {
+ qWarning("QObject::killTimer: timers cannot be stopped from another thread");
+ return false;
+ }
+#endif
+
+ Q_D(QEventDispatcherUNIX);
+ return d->timerList.unregisterTimer(timerId);
+}
+
+/*!
+ \internal
+*/
+bool QEventDispatcherUNIX::unregisterTimers(QObject *object)
+{
+#ifndef QT_NO_DEBUG
+ if (!object) {
+ qWarning("QEventDispatcherUNIX::unregisterTimers: invalid argument");
+ return false;
+ } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QObject::killTimers: timers cannot be stopped from another thread");
+ return false;
+ }
+#endif
+
+ Q_D(QEventDispatcherUNIX);
+ return d->timerList.unregisterTimers(object);
+}
+
+QList<QEventDispatcherUNIX::TimerInfo>
+QEventDispatcherUNIX::registeredTimers(QObject *object) const
+{
+ if (!object) {
+ qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument");
+ return QList<TimerInfo>();
+ }
+
+ Q_D(const QEventDispatcherUNIX);
+ return d->timerList.registeredTimers(object);
+}
+
+/*****************************************************************************
+ Socket notifier type
+ *****************************************************************************/
+QSockNotType::QSockNotType()
+{
+ FD_ZERO(&select_fds);
+ FD_ZERO(&enabled_fds);
+ FD_ZERO(&pending_fds);
+}
+
+QSockNotType::~QSockNotType()
+{
+ for (int i = 0; i < list.size(); ++i)
+ delete list[i];
+}
+
+/*****************************************************************************
+ QEventDispatcher implementations for UNIX
+ *****************************************************************************/
+
+void QEventDispatcherUNIX::registerSocketNotifier(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+ int sockfd = notifier->socket();
+ int type = notifier->type();
+#ifndef QT_NO_DEBUG
+ if (sockfd < 0
+ || unsigned(sockfd) >= FD_SETSIZE) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread()
+ || thread() != QThread::currentThread()) {
+ qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherUNIX);
+ QSockNotType::List &list = d->sn_vec[type].list;
+ fd_set *fds = &d->sn_vec[type].enabled_fds;
+ QSockNot *sn;
+
+ sn = new QSockNot;
+ sn->obj = notifier;
+ sn->fd = sockfd;
+ sn->queue = &d->sn_vec[type].pending_fds;
+
+ int i;
+ for (i = 0; i < list.size(); ++i) {
+ QSockNot *p = list[i];
+ if (p->fd < sockfd)
+ break;
+ if (p->fd == sockfd) {
+ static const char *t[] = { "Read", "Write", "Exception" };
+ qWarning("QSocketNotifier: Multiple socket notifiers for "
+ "same socket %d and type %s", sockfd, t[type]);
+ }
+ }
+ list.insert(i, sn);
+
+ FD_SET(sockfd, fds);
+ d->sn_highest = qMax(d->sn_highest, sockfd);
+}
+
+void QEventDispatcherUNIX::unregisterSocketNotifier(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+ int sockfd = notifier->socket();
+ int type = notifier->type();
+#ifndef QT_NO_DEBUG
+ if (sockfd < 0
+ || unsigned(sockfd) >= FD_SETSIZE) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread()
+ || thread() != QThread::currentThread()) {
+ qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherUNIX);
+ QSockNotType::List &list = d->sn_vec[type].list;
+ fd_set *fds = &d->sn_vec[type].enabled_fds;
+ QSockNot *sn = 0;
+ int i;
+ for (i = 0; i < list.size(); ++i) {
+ sn = list[i];
+ if(sn->obj == notifier && sn->fd == sockfd)
+ break;
+ }
+ if (i == list.size()) // not found
+ return;
+
+ FD_CLR(sockfd, fds); // clear fd bit
+ FD_CLR(sockfd, sn->queue);
+ d->sn_pending_list.removeAll(sn); // remove from activation list
+ list.removeAt(i); // remove notifier found above
+ delete sn;
+
+ if (d->sn_highest == sockfd) { // find highest fd
+ d->sn_highest = -1;
+ for (int i=0; i<3; i++) {
+ if (!d->sn_vec[i].list.isEmpty())
+ d->sn_highest = qMax(d->sn_highest, // list is fd-sorted
+ d->sn_vec[i].list[0]->fd);
+ }
+ }
+}
+
+void QEventDispatcherUNIX::setSocketNotifierPending(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+ int sockfd = notifier->socket();
+ int type = notifier->type();
+#ifndef QT_NO_DEBUG
+ if (sockfd < 0
+ || unsigned(sockfd) >= FD_SETSIZE) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ }
+ Q_ASSERT(notifier->thread() == thread() && thread() == QThread::currentThread());
+#endif
+
+ Q_D(QEventDispatcherUNIX);
+ QSockNotType::List &list = d->sn_vec[type].list;
+ QSockNot *sn = 0;
+ int i;
+ for (i = 0; i < list.size(); ++i) {
+ sn = list[i];
+ if(sn->obj == notifier && sn->fd == sockfd)
+ break;
+ }
+ if (i == list.size()) // not found
+ return;
+
+ // We choose a random activation order to be more fair under high load.
+ // If a constant order is used and a peer early in the list can
+ // saturate the IO, it might grab our attention completely.
+ // Also, if we're using a straight list, the callback routines may
+ // delete other entries from the list before those other entries are
+ // processed.
+ if (! FD_ISSET(sn->fd, sn->queue)) {
+ if (d->sn_pending_list.isEmpty()) {
+ d->sn_pending_list.append(sn);
+ } else {
+ d->sn_pending_list.insert((qrand() & 0xff) %
+ (d->sn_pending_list.size()+1), sn);
+ }
+ FD_SET(sn->fd, sn->queue);
+ }
+}
+
+int QEventDispatcherUNIX::activateTimers()
+{
+ Q_ASSERT(thread() == QThread::currentThread());
+ Q_D(QEventDispatcherUNIX);
+ return d->timerList.activateTimers();
+}
+
+int QEventDispatcherUNIX::activateSocketNotifiers()
+{
+ Q_D(QEventDispatcherUNIX);
+ if (d->sn_pending_list.isEmpty())
+ return 0;
+
+ // activate entries
+ int n_act = 0;
+ QEvent event(QEvent::SockAct);
+ while (!d->sn_pending_list.isEmpty()) {
+ QSockNot *sn = d->sn_pending_list.takeFirst();
+ if (FD_ISSET(sn->fd, sn->queue)) {
+ FD_CLR(sn->fd, sn->queue);
+ QCoreApplication::sendEvent(sn->obj, &event);
+ ++n_act;
+ }
+ }
+ return n_act;
+}
+
+bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags)
+{
+ Q_D(QEventDispatcherUNIX);
+ d->interrupt = false;
+
+ // we are awake, broadcast it
+ emit awake();
+ QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData);
+
+ int nevents = 0;
+ const bool canWait = (d->threadData->canWait
+ && !d->interrupt
+ && (flags & QEventLoop::WaitForMoreEvents));
+
+ if (canWait)
+ emit aboutToBlock();
+
+ if (!d->interrupt) {
+ // return the maximum time we can wait for an event.
+ timeval *tm = 0;
+ timeval wait_tm = { 0l, 0l };
+ if (!(flags & QEventLoop::X11ExcludeTimers)) {
+ if (d->timerList.timerWait(wait_tm))
+ tm = &wait_tm;
+ }
+
+ if (!canWait) {
+ if (!tm)
+ tm = &wait_tm;
+
+ // no time to wait
+ tm->tv_sec = 0l;
+ tm->tv_usec = 0l;
+ }
+
+ nevents = d->doSelect(flags, tm);
+
+ // activate timers
+ if (! (flags & QEventLoop::X11ExcludeTimers)) {
+ nevents += activateTimers();
+ }
+ }
+ // return true if we handled events, false otherwise
+ return (nevents > 0);
+}
+
+bool QEventDispatcherUNIX::hasPendingEvents()
+{
+ extern uint qGlobalPostedEventsCount(); // from qapplication.cpp
+ return qGlobalPostedEventsCount();
+}
+
+void QEventDispatcherUNIX::wakeUp()
+{
+ Q_D(QEventDispatcherUNIX);
+ if (d->wakeUps.testAndSetAcquire(0, 1)) {
+ char c = 0;
+ qt_safe_write( d->thread_pipe[1], &c, 1 );
+ }
+}
+
+void QEventDispatcherUNIX::interrupt()
+{
+ Q_D(QEventDispatcherUNIX);
+ d->interrupt = true;
+ wakeUp();
+}
+
+void QEventDispatcherUNIX::flush()
+{ }
+
+
+
+
+void QCoreApplication::watchUnixSignal(int sig, bool watch)
+{
+ if (sig < NSIG) {
+ struct sigaction sa;
+ sigemptyset(&(sa.sa_mask));
+ sa.sa_flags = 0;
+ if (watch)
+ sa.sa_handler = signalHandler;
+ else
+ sa.sa_handler = SIG_DFL;
+ sigaction(sig, &sa, 0);
+ }
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qeventdispatcher_unix_p.h b/src/corelib/kernel/qeventdispatcher_unix_p.h
new file mode 100644
index 0000000000..96a3e4a276
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_unix_p.h
@@ -0,0 +1,208 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QEVENTDISPATCHER_UNIX_P_H
+#define QEVENTDISPATCHER_UNIX_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qabstracteventdispatcher.h"
+#include "QtCore/qlist.h"
+#include "private/qabstracteventdispatcher_p.h"
+#include "private/qcore_unix_p.h"
+#include "private/qpodlist_p.h"
+#include "QtCore/qvarlengtharray.h"
+
+#if defined(Q_OS_VXWORKS)
+# include <sys/times.h>
+#else
+# include <sys/time.h>
+# if (!defined(Q_OS_HPUX) || defined(__ia64)) && !defined(Q_OS_NACL)
+# include <sys/select.h>
+# endif
+#endif
+
+QT_BEGIN_NAMESPACE
+
+// internal timer info
+struct QTimerInfo {
+ int id; // - timer identifier
+ timeval interval; // - timer interval
+ timeval timeout; // - when to sent event
+ QObject *obj; // - object to receive event
+ QTimerInfo **activateRef; // - ref from activateTimers
+};
+
+class QTimerInfoList : public QList<QTimerInfo*>
+{
+#if ((_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC)) || defined(QT_BOOTSTRAPPED)
+ timeval previousTime;
+ clock_t previousTicks;
+ int ticksPerSecond;
+ int msPerTick;
+
+ bool timeChanged(timeval *delta);
+#endif
+
+ // state variables used by activateTimers()
+ QTimerInfo *firstTimerInfo;
+
+public:
+ QTimerInfoList();
+
+ timeval currentTime;
+ timeval updateCurrentTime();
+
+ // must call updateCurrentTime() first!
+ void repairTimersIfNeeded();
+
+ bool timerWait(timeval &);
+ void timerInsert(QTimerInfo *);
+ void timerRepair(const timeval &);
+
+ void registerTimer(int timerId, int interval, QObject *object);
+ bool unregisterTimer(int timerId);
+ bool unregisterTimers(QObject *object);
+ QList<QPair<int, int> > registeredTimers(QObject *object) const;
+
+ int activateTimers();
+};
+
+struct QSockNot
+{
+ QSocketNotifier *obj;
+ int fd;
+ fd_set *queue;
+};
+
+class QSockNotType
+{
+public:
+ QSockNotType();
+ ~QSockNotType();
+
+ typedef QPodList<QSockNot*, 32> List;
+
+ List list;
+ fd_set select_fds;
+ fd_set enabled_fds;
+ fd_set pending_fds;
+
+};
+
+class QEventDispatcherUNIXPrivate;
+
+class Q_CORE_EXPORT QEventDispatcherUNIX : public QAbstractEventDispatcher
+{
+ Q_OBJECT
+ Q_DECLARE_PRIVATE(QEventDispatcherUNIX)
+
+public:
+ explicit QEventDispatcherUNIX(QObject *parent = 0);
+ ~QEventDispatcherUNIX();
+
+ bool processEvents(QEventLoop::ProcessEventsFlags flags);
+ bool hasPendingEvents();
+
+ void registerSocketNotifier(QSocketNotifier *notifier);
+ void unregisterSocketNotifier(QSocketNotifier *notifier);
+
+ void registerTimer(int timerId, int interval, QObject *object);
+ bool unregisterTimer(int timerId);
+ bool unregisterTimers(QObject *object);
+ QList<TimerInfo> registeredTimers(QObject *object) const;
+
+ void wakeUp();
+ void interrupt();
+ void flush();
+
+protected:
+ QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent = 0);
+
+ void setSocketNotifierPending(QSocketNotifier *notifier);
+
+ int activateTimers();
+ int activateSocketNotifiers();
+
+ virtual int select(int nfds,
+ fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
+ timeval *timeout);
+};
+
+class Q_CORE_EXPORT QEventDispatcherUNIXPrivate : public QAbstractEventDispatcherPrivate
+{
+ Q_DECLARE_PUBLIC(QEventDispatcherUNIX)
+
+public:
+ QEventDispatcherUNIXPrivate();
+ ~QEventDispatcherUNIXPrivate();
+
+ int doSelect(QEventLoop::ProcessEventsFlags flags, timeval *timeout);
+
+ bool mainThread;
+ int thread_pipe[2];
+
+ // highest fd for all socket notifiers
+ int sn_highest;
+ // 3 socket notifier types - read, write and exception
+ QSockNotType sn_vec[3];
+
+ QTimerInfoList timerList;
+
+ // pending socket notifiers list
+ QSockNotType::List sn_pending_list;
+
+ QAtomicInt wakeUps;
+ bool interrupt;
+};
+
+QT_END_NAMESPACE
+
+#endif // QEVENTDISPATCHER_UNIX_P_H
diff --git a/src/corelib/kernel/qeventdispatcher_win.cpp b/src/corelib/kernel/qeventdispatcher_win.cpp
new file mode 100644
index 0000000000..6badb6a2f8
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_win.cpp
@@ -0,0 +1,1158 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qeventdispatcher_win_p.h"
+
+#include "qcoreapplication.h"
+#include "qhash.h"
+#include <private/qsystemlibrary_p.h>
+#include "qpair.h"
+#include "qset.h"
+#include "qsocketnotifier.h"
+#include "qvarlengtharray.h"
+#include "qwineventnotifier_p.h"
+
+#include "qabstracteventdispatcher_p.h"
+#include "qcoreapplication_p.h"
+#include <private/qthread_p.h>
+#include <private/qmutexpool_p.h>
+
+QT_BEGIN_NAMESPACE
+
+HINSTANCE qWinAppInst();
+extern uint qGlobalPostedEventsCount();
+
+#ifndef TIME_KILL_SYNCHRONOUS
+# define TIME_KILL_SYNCHRONOUS 0x0100
+#endif
+
+#ifndef QS_RAWINPUT
+# ifdef Q_OS_WINCE
+# define QS_RAWINPUT 0x0000
+# else
+# define QS_RAWINPUT 0x0400
+# endif
+#endif
+
+#ifndef WM_TOUCH
+# define WM_TOUCH 0x0240
+#endif
+#ifndef QT_NO_GESTURES
+#ifndef WM_GESTURE
+# define WM_GESTURE 0x0119
+#endif
+#ifndef WM_GESTURENOTIFY
+# define WM_GESTURENOTIFY 0x011A
+#endif
+#endif // QT_NO_GESTURES
+
+enum {
+ WM_QT_SOCKETNOTIFIER = WM_USER,
+ WM_QT_SENDPOSTEDEVENTS = WM_USER + 1,
+ SendPostedEventsWindowsTimerId = ~1u
+};
+
+#if defined(Q_OS_WINCE)
+QT_BEGIN_INCLUDE_NAMESPACE
+#include <winsock.h>
+// Asynchronous Winsocks ------------------------------------------
+#ifndef QT_NO_THREAD
+#include <qthread.h>
+#include <qmap.h>
+#include <qmutex.h>
+QT_END_INCLUDE_NAMESPACE
+
+//#define QCE_ASYNC_DEBUG
+
+namespace {
+ class SocketAsyncHandler;
+
+ class SocketAsyncHandler : public QThread
+ {
+ public:
+ SocketAsyncHandler();
+ ~SocketAsyncHandler();
+ void run();
+ void select(SOCKET sock, HWND handle, unsigned int msg, long ev);
+ void removeSelect(SOCKET sock);
+ void safeRemove(SOCKET sock);
+ private:
+ struct SockInfo {
+ HWND handle;
+ unsigned int msg;
+ long ev;
+ };
+ QMap<SOCKET, SockInfo> sockets;
+ QMutex mutex;
+ QWaitCondition cond;
+ bool supposedToDie;
+ };
+
+ SocketAsyncHandler::SocketAsyncHandler()
+ : supposedToDie(false)
+ {
+ }
+
+ SocketAsyncHandler::~SocketAsyncHandler()
+ {
+ mutex.lock();
+ supposedToDie = true;
+ mutex.unlock();
+ cond.wakeOne();
+ wait();
+ while (sockets.size() > 0)
+ removeSelect(sockets.begin().key());
+ }
+
+ void SocketAsyncHandler::removeSelect(SOCKET sock)
+ {
+ if (!sockets.contains(sock))
+ return;
+ sockets.remove(sock);
+ return;
+ }
+
+ void SocketAsyncHandler::safeRemove(SOCKET sock)
+ {
+ QMutexLocker locker(&mutex);
+ removeSelect(sock);
+ }
+
+ void SocketAsyncHandler::select(SOCKET sock, HWND handle, unsigned int msg, long ev)
+ {
+ QMutexLocker locker(&mutex);
+
+ if (sockets.contains(sock))
+ sockets.remove(sock);
+
+ SockInfo info;
+ info.handle = handle;
+ info.msg = msg;
+ info.ev = ev;
+ sockets.insert(sock, info);
+ cond.wakeOne();
+ }
+
+ void SocketAsyncHandler::run()
+ {
+ do {
+ mutex.lock();
+
+ while (!supposedToDie && sockets.isEmpty()) {
+ cond.wait(&mutex);
+ }
+
+ if (supposedToDie) {
+ mutex.unlock();
+ break;
+ }
+
+ // Copy current items to reduce lock time
+ // and to be able to use SendMessage
+ QMap<SOCKET, SockInfo> currentSockets = sockets;
+ mutex.unlock();
+
+ fd_set readS, writeS, exS;
+ FD_ZERO(&readS);
+ FD_ZERO(&writeS);
+ FD_ZERO(&exS);
+
+ int maxFd = 0;
+
+ for (QMap<SOCKET, SockInfo>::iterator it = currentSockets.begin(); it != currentSockets.end(); ++it) {
+ const SockInfo &info = it.value();
+ int socket = it.key();
+ maxFd = qMax(maxFd, socket);
+
+ if ((info.ev & FD_READ) || (info.ev & FD_CLOSE) || (info.ev & FD_ACCEPT))
+ FD_SET(socket, &readS);
+ if ((info.ev & FD_WRITE)|| (info.ev & FD_CONNECT))
+ FD_SET(socket, &writeS);
+ if (info.ev & FD_OOB)
+ FD_SET(socket, &exS);
+ }
+
+ timeval timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 50000;
+ int result = ::select(maxFd + 1, &readS, &writeS, &exS, &timeout);
+ if (result > 0) {
+ HWND handle;
+ unsigned int tmpMsg;
+ SOCKET sock;
+ HRESULT ret;
+ for (QMap<SOCKET, SockInfo>::const_iterator it = currentSockets.constBegin();
+ it != currentSockets.constEnd(); ++it) {
+ handle = (*it).handle;
+ tmpMsg = (*it).msg;
+ sock = it.key();
+ if (FD_ISSET(sock, &readS))
+ ret = SendMessage(handle, tmpMsg, sock, FD_READ);
+
+ if (FD_ISSET(sock, &writeS))
+ ret = SendMessage(handle, tmpMsg, sock, FD_WRITE);
+
+ if (FD_ISSET(sock, &exS))
+ ret = SendMessage(handle, tmpMsg, sock, FD_OOB);
+ }
+ }
+
+#ifdef QCE_ASYNC_DEBUG
+ else if (result == 0) { //timeout
+ qDebug(" WSAAsync select timeout");
+ } else if (result < 0) { // SocketError
+ // This might happen because of two reasons
+ // 1. We already closed a socket in between the copy and the select
+ // and thus select() returns an error
+ // 2. Something is really wrong, then
+ // ### Loop on all descriptors, try to select and remove the
+ // ### broken one.
+ qWarning("WSAAsync select error %d", WSAGetLastError());
+ }
+#endif
+ } while(true);
+ }
+} // namespace
+
+Q_GLOBAL_STATIC(SocketAsyncHandler, qt_async_handler)
+
+int WSAAsyncSelect(SOCKET sock, HWND handle, unsigned int msg, long ev)
+{
+ if (sock == 0 || handle == 0 || handle == INVALID_HANDLE_VALUE) {
+ WSASetLastError(WSAEINVAL);
+ return SOCKET_ERROR;
+ }
+
+ if (msg == 0 && ev == 0)
+ qt_async_handler()->safeRemove(sock);
+ else
+ qt_async_handler()->select(sock, handle, msg, ev);
+
+ qt_async_handler()->start(QThread::LowPriority);
+ WSASetLastError(0);
+ return 0;
+}
+#else // QT_NO_THREAD
+int WSAAsyncSelect(SOCKET, HWND, unsigned int, long)
+{
+ return SOCKET_ERROR;
+}
+#endif
+#endif // Q_OS_WINCE
+
+class QEventDispatcherWin32Private;
+
+struct QSockNot {
+ QSocketNotifier *obj;
+ int fd;
+};
+typedef QHash<int, QSockNot *> QSNDict;
+
+struct WinTimerInfo { // internal timer info
+ QObject *dispatcher;
+ int timerId;
+ int interval;
+ QObject *obj; // - object to receive events
+ bool inTimerEvent;
+ int fastTimerId;
+};
+
+class QZeroTimerEvent : public QTimerEvent
+{
+public:
+ inline QZeroTimerEvent(int timerId)
+ : QTimerEvent(timerId)
+ { t = QEvent::ZeroTimerEvent; }
+};
+
+typedef QList<WinTimerInfo*> WinTimerVec; // vector of TimerInfo structs
+typedef QHash<int, WinTimerInfo*> WinTimerDict; // fast dict of timers
+
+#if !defined(DWORD_PTR) && !defined(Q_WS_WIN64)
+#define DWORD_PTR DWORD
+#endif
+
+typedef MMRESULT(WINAPI *ptimeSetEvent)(UINT, UINT, LPTIMECALLBACK, DWORD_PTR, UINT);
+typedef MMRESULT(WINAPI *ptimeKillEvent)(UINT);
+
+static ptimeSetEvent qtimeSetEvent = 0;
+static ptimeKillEvent qtimeKillEvent = 0;
+
+LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
+
+static void resolveTimerAPI()
+{
+ static bool triedResolve = false;
+ if (!triedResolve) {
+#ifndef QT_NO_THREAD
+ QMutexLocker locker(QMutexPool::globalInstanceGet(&triedResolve));
+ if (triedResolve)
+ return;
+#endif
+ triedResolve = true;
+#if !defined(Q_OS_WINCE)
+ qtimeSetEvent = (ptimeSetEvent)QSystemLibrary::resolve(QLatin1String("winmm"), "timeSetEvent");
+ qtimeKillEvent = (ptimeKillEvent)QSystemLibrary::resolve(QLatin1String("winmm"), "timeKillEvent");
+#else
+ qtimeSetEvent = (ptimeSetEvent)QSystemLibrary::resolve(QLatin1String("Mmtimer"), "timeSetEvent");
+ qtimeKillEvent = (ptimeKillEvent)QSystemLibrary::resolve(QLatin1String("Mmtimer"), "timeKillEvent");
+#endif
+ }
+}
+
+
+class QEventDispatcherWin32Private : public QAbstractEventDispatcherPrivate
+{
+ Q_DECLARE_PUBLIC(QEventDispatcherWin32)
+public:
+ QEventDispatcherWin32Private();
+ ~QEventDispatcherWin32Private();
+
+ DWORD threadId;
+
+ bool interrupt;
+
+ // internal window handle used for socketnotifiers/timers/etc
+ HWND internalHwnd;
+ HHOOK getMessageHook;
+
+ // for controlling when to send posted events
+ QAtomicInt serialNumber;
+ int lastSerialNumber, sendPostedEventsWindowsTimerId;
+ QAtomicInt wakeUps;
+
+ // timers
+ WinTimerVec timerVec;
+ WinTimerDict timerDict;
+ void registerTimer(WinTimerInfo *t);
+ void unregisterTimer(WinTimerInfo *t, bool closingDown = false);
+ void sendTimerEvent(int timerId);
+
+ // socket notifiers
+ QSNDict sn_read;
+ QSNDict sn_write;
+ QSNDict sn_except;
+ void doWsaAsyncSelect(int socket);
+
+ QList<QWinEventNotifier *> winEventNotifierList;
+ void activateEventNotifier(QWinEventNotifier * wen);
+
+ QList<MSG> queuedUserInputEvents;
+ QList<MSG> queuedSocketEvents;
+};
+
+QEventDispatcherWin32Private::QEventDispatcherWin32Private()
+ : threadId(GetCurrentThreadId()), interrupt(false), internalHwnd(0), getMessageHook(0),
+ serialNumber(0), lastSerialNumber(0), sendPostedEventsWindowsTimerId(0), wakeUps(0)
+{
+ resolveTimerAPI();
+}
+
+QEventDispatcherWin32Private::~QEventDispatcherWin32Private()
+{
+ if (internalHwnd)
+ DestroyWindow(internalHwnd);
+ QString className = QLatin1String("QEventDispatcherWin32_Internal_Widget") + QString::number(quintptr(qt_internal_proc));
+ UnregisterClass((wchar_t*)className.utf16(), qWinAppInst());
+}
+
+void QEventDispatcherWin32Private::activateEventNotifier(QWinEventNotifier * wen)
+{
+ QEvent event(QEvent::WinEventAct);
+ QCoreApplication::sendEvent(wen, &event);
+}
+
+// ### Qt 5: remove
+Q_CORE_EXPORT bool winPeekMessage(MSG* msg, HWND hWnd, UINT wMsgFilterMin,
+ UINT wMsgFilterMax, UINT wRemoveMsg)
+{
+ return PeekMessage(msg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
+}
+
+// ### Qt 5: remove
+Q_CORE_EXPORT bool winPostMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ return PostMessage(hWnd, msg, wParam, lParam);
+}
+
+// ### Qt 5: remove
+Q_CORE_EXPORT bool winGetMessage(MSG* msg, HWND hWnd, UINT wMsgFilterMin,
+ UINT wMsgFilterMax)
+{
+ return GetMessage(msg, hWnd, wMsgFilterMin, wMsgFilterMax);
+}
+
+// This function is called by a workerthread
+void WINAPI QT_WIN_CALLBACK qt_fast_timer_proc(uint timerId, uint /*reserved*/, DWORD_PTR user, DWORD_PTR /*reserved*/, DWORD_PTR /*reserved*/)
+{
+ if (!timerId) // sanity check
+ return;
+ WinTimerInfo *t = (WinTimerInfo*)user;
+ Q_ASSERT(t);
+ QCoreApplication::postEvent(t->dispatcher, new QTimerEvent(t->timerId));
+}
+
+LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
+{
+ if (message == WM_NCCREATE)
+ return true;
+
+ MSG msg;
+ msg.hwnd = hwnd;
+ msg.message = message;
+ msg.wParam = wp;
+ msg.lParam = lp;
+ QCoreApplication *app = QCoreApplication::instance();
+ long result;
+ if (!app) {
+ if (message == WM_TIMER)
+ KillTimer(hwnd, wp);
+ return 0;
+ } else if (app->filterEvent(&msg, &result)) {
+ return result;
+ }
+
+#ifdef GWLP_USERDATA
+ QEventDispatcherWin32 *q = (QEventDispatcherWin32 *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
+#else
+ QEventDispatcherWin32 *q = (QEventDispatcherWin32 *) GetWindowLong(hwnd, GWL_USERDATA);
+#endif
+ QEventDispatcherWin32Private *d = 0;
+ if (q != 0)
+ d = q->d_func();
+
+ if (message == WM_QT_SOCKETNOTIFIER) {
+ // socket notifier message
+ int type = -1;
+ switch (WSAGETSELECTEVENT(lp)) {
+ case FD_READ:
+ case FD_CLOSE:
+ case FD_ACCEPT:
+ type = 0;
+ break;
+ case FD_WRITE:
+ case FD_CONNECT:
+ type = 1;
+ break;
+ case FD_OOB:
+ type = 2;
+ break;
+ }
+ if (type >= 0) {
+ Q_ASSERT(d != 0);
+ QSNDict *sn_vec[3] = { &d->sn_read, &d->sn_write, &d->sn_except };
+ QSNDict *dict = sn_vec[type];
+
+ QSockNot *sn = dict ? dict->value(wp) : 0;
+ if (sn) {
+ QEvent event(QEvent::SockAct);
+ QCoreApplication::sendEvent(sn->obj, &event);
+ }
+ }
+ return 0;
+ } else if (message == WM_QT_SENDPOSTEDEVENTS
+ // we also use a Windows timer to send posted events when the message queue is full
+ || (message == WM_TIMER
+ && d->sendPostedEventsWindowsTimerId != 0
+ && wp == (uint)d->sendPostedEventsWindowsTimerId)) {
+ int localSerialNumber = d->serialNumber;
+ if (localSerialNumber != d->lastSerialNumber) {
+ d->lastSerialNumber = localSerialNumber;
+ QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData);
+ }
+ return 0;
+ } else if (message == WM_TIMER) {
+ Q_ASSERT(d != 0);
+ d->sendTimerEvent(wp);
+ return 0;
+ }
+
+ return DefWindowProc(hwnd, message, wp, lp);
+}
+
+LRESULT QT_WIN_CALLBACK qt_GetMessageHook(int code, WPARAM wp, LPARAM lp)
+{
+ if (wp == PM_REMOVE) {
+ QEventDispatcherWin32 *q = qobject_cast<QEventDispatcherWin32 *>(QAbstractEventDispatcher::instance());
+ Q_ASSERT(q != 0);
+ if (q) {
+ MSG *msg = (MSG *) lp;
+ QEventDispatcherWin32Private *d = q->d_func();
+ int localSerialNumber = d->serialNumber;
+ if (HIWORD(GetQueueStatus(QS_TIMER | QS_INPUT | QS_RAWINPUT)) == 0) {
+ // no more input or timer events in the message queue, we can allow posted events to be sent normally now
+ if (d->sendPostedEventsWindowsTimerId != 0) {
+ // stop the timer to send posted events, since we now allow the WM_QT_SENDPOSTEDEVENTS message
+ KillTimer(d->internalHwnd, d->sendPostedEventsWindowsTimerId);
+ d->sendPostedEventsWindowsTimerId = 0;
+ }
+ (void) d->wakeUps.fetchAndStoreRelease(0);
+ if (localSerialNumber != d->lastSerialNumber
+ // if this message IS the one that triggers sendPostedEvents(), no need to post it again
+ && (msg->hwnd != d->internalHwnd
+ || msg->message != WM_QT_SENDPOSTEDEVENTS)) {
+ PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0);
+ }
+ } else if (d->sendPostedEventsWindowsTimerId == 0
+ && localSerialNumber != d->lastSerialNumber) {
+ // start a special timer to continue delivering posted events while
+ // there are still input and timer messages in the message queue
+ d->sendPostedEventsWindowsTimerId = SetTimer(d->internalHwnd,
+ SendPostedEventsWindowsTimerId,
+ 0, // we specify zero, but Windows uses USER_TIMER_MINIMUM
+ NULL);
+ // we don't check the return value of SetTimer()... if creating the timer failed, there's little
+ // we can do. we just have to accept that posted events will be starved
+ }
+ }
+ }
+#ifdef Q_OS_WINCE
+ return 0;
+#else
+ return CallNextHookEx(0, code, wp, lp);
+#endif
+}
+
+static HWND qt_create_internal_window(const QEventDispatcherWin32 *eventDispatcher)
+{
+ // make sure that multiple Qt's can coexist in the same process
+ QString className = QLatin1String("QEventDispatcherWin32_Internal_Widget") + QString::number(quintptr(qt_internal_proc));
+
+ WNDCLASS wc;
+ wc.style = 0;
+ wc.lpfnWndProc = qt_internal_proc;
+ wc.cbClsExtra = 0;
+ wc.cbWndExtra = 0;
+ wc.hInstance = qWinAppInst();
+ wc.hIcon = 0;
+ wc.hCursor = 0;
+ wc.hbrBackground = 0;
+ wc.lpszMenuName = NULL;
+ wc.lpszClassName = reinterpret_cast<const wchar_t *> (className.utf16());
+
+ RegisterClass(&wc);
+ HWND wnd = CreateWindow(wc.lpszClassName, // classname
+ wc.lpszClassName, // window name
+ 0, // style
+ 0, 0, 0, 0, // geometry
+ 0, // parent
+ 0, // menu handle
+ qWinAppInst(), // application
+ 0); // windows creation data.
+
+ if (!wnd) {
+ qWarning("QEventDispatcher: Failed to create QEventDispatcherWin32 internal window: %d\n", (int)GetLastError());
+ }
+
+#ifdef GWLP_USERDATA
+ SetWindowLongPtr(wnd, GWLP_USERDATA, (LONG_PTR)eventDispatcher);
+#else
+ SetWindowLong(wnd, GWL_USERDATA, (LONG)eventDispatcher);
+#endif
+
+ return wnd;
+}
+
+void QEventDispatcherWin32Private::registerTimer(WinTimerInfo *t)
+{
+ Q_ASSERT(internalHwnd);
+
+ Q_Q(QEventDispatcherWin32);
+
+ int ok = 0;
+ if (t->interval > 20 || !t->interval || !qtimeSetEvent) {
+ ok = 1;
+ if (!t->interval) // optimization for single-shot-zero-timer
+ QCoreApplication::postEvent(q, new QZeroTimerEvent(t->timerId));
+ else
+ ok = SetTimer(internalHwnd, t->timerId, (uint) t->interval, 0);
+ } else {
+ ok = t->fastTimerId = qtimeSetEvent(t->interval, 1, qt_fast_timer_proc, (DWORD_PTR)t,
+ TIME_CALLBACK_FUNCTION | TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
+ if (ok == 0) { // fall back to normal timer if no more multimedia timers available
+ ok = SetTimer(internalHwnd, t->timerId, (uint) t->interval, 0);
+ }
+ }
+
+ if (ok == 0)
+ qErrnoWarning("QEventDispatcherWin32::registerTimer: Failed to create a timer");
+}
+
+void QEventDispatcherWin32Private::unregisterTimer(WinTimerInfo *t, bool closingDown)
+{
+ // mark timer as unused
+ if (!QObjectPrivate::get(t->obj)->inThreadChangeEvent && !closingDown)
+ QAbstractEventDispatcherPrivate::releaseTimerId(t->timerId);
+
+ if (t->interval == 0) {
+ QCoreApplicationPrivate::removePostedTimerEvent(t->dispatcher, t->timerId);
+ } else if (t->fastTimerId != 0) {
+ qtimeKillEvent(t->fastTimerId);
+ QCoreApplicationPrivate::removePostedTimerEvent(t->dispatcher, t->timerId);
+ } else if (internalHwnd) {
+ KillTimer(internalHwnd, t->timerId);
+ }
+ delete t;
+}
+
+void QEventDispatcherWin32Private::sendTimerEvent(int timerId)
+{
+ WinTimerInfo *t = timerDict.value(timerId);
+ if (t && !t->inTimerEvent) {
+ // send event, but don't allow it to recurse
+ t->inTimerEvent = true;
+
+ QTimerEvent e(t->timerId);
+ QCoreApplication::sendEvent(t->obj, &e);
+
+ // timer could have been removed
+ t = timerDict.value(timerId);
+ if (t) {
+ t->inTimerEvent = false;
+ }
+ }
+}
+
+void QEventDispatcherWin32Private::doWsaAsyncSelect(int socket)
+{
+ Q_ASSERT(internalHwnd);
+ int sn_event = 0;
+ if (sn_read.contains(socket))
+ sn_event |= FD_READ | FD_CLOSE | FD_ACCEPT;
+ if (sn_write.contains(socket))
+ sn_event |= FD_WRITE | FD_CONNECT;
+ if (sn_except.contains(socket))
+ sn_event |= FD_OOB;
+ // BoundsChecker may emit a warning for WSAAsyncSelect when sn_event == 0
+ // This is a BoundsChecker bug and not a Qt bug
+ WSAAsyncSelect(socket, internalHwnd, sn_event ? WM_QT_SOCKETNOTIFIER : 0, sn_event);
+}
+
+void QEventDispatcherWin32::createInternalHwnd()
+{
+ Q_D(QEventDispatcherWin32);
+
+ Q_ASSERT(!d->internalHwnd);
+ if (d->internalHwnd)
+ return;
+ d->internalHwnd = qt_create_internal_window(this);
+
+#ifndef Q_OS_WINCE
+ // setup GetMessage hook needed to drive our posted events
+ d->getMessageHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) qt_GetMessageHook, NULL, GetCurrentThreadId());
+ if (!d->getMessageHook) {
+ qFatal("Qt: INTERNALL ERROR: failed to install GetMessage hook");
+ }
+#endif
+
+ // register all socket notifiers
+ QList<int> sockets = (d->sn_read.keys().toSet()
+ + d->sn_write.keys().toSet()
+ + d->sn_except.keys().toSet()).toList();
+ for (int i = 0; i < sockets.count(); ++i)
+ d->doWsaAsyncSelect(sockets.at(i));
+
+ // start all normal timers
+ for (int i = 0; i < d->timerVec.count(); ++i)
+ d->registerTimer(d->timerVec.at(i));
+
+ // trigger a call to sendPostedEvents()
+ wakeUp();
+}
+
+QEventDispatcherWin32::QEventDispatcherWin32(QObject *parent)
+ : QAbstractEventDispatcher(*new QEventDispatcherWin32Private, parent)
+{
+}
+
+QEventDispatcherWin32::~QEventDispatcherWin32()
+{
+}
+
+bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags)
+{
+ Q_D(QEventDispatcherWin32);
+
+ if (!d->internalHwnd)
+ createInternalHwnd();
+
+ d->interrupt = false;
+ emit awake();
+
+ bool canWait;
+ bool retVal = false;
+ bool seenWM_QT_SENDPOSTEDEVENTS = false;
+ bool needWM_QT_SENDPOSTEDEVENTS = false;
+ do {
+ DWORD waitRet = 0;
+ HANDLE pHandles[MAXIMUM_WAIT_OBJECTS - 1];
+ QVarLengthArray<MSG> processedTimers;
+ while (!d->interrupt) {
+ DWORD nCount = d->winEventNotifierList.count();
+ Q_ASSERT(nCount < MAXIMUM_WAIT_OBJECTS - 1);
+
+ MSG msg;
+ bool haveMessage;
+
+ if (!(flags & QEventLoop::ExcludeUserInputEvents) && !d->queuedUserInputEvents.isEmpty()) {
+ // process queued user input events
+ haveMessage = true;
+ msg = d->queuedUserInputEvents.takeFirst();
+ } else if(!(flags & QEventLoop::ExcludeSocketNotifiers) && !d->queuedSocketEvents.isEmpty()) {
+ // process queued socket events
+ haveMessage = true;
+ msg = d->queuedSocketEvents.takeFirst();
+ } else {
+ haveMessage = PeekMessage(&msg, 0, 0, 0, PM_REMOVE);
+ if (haveMessage && (flags & QEventLoop::ExcludeUserInputEvents)
+ && ((msg.message >= WM_KEYFIRST
+ && msg.message <= WM_KEYLAST)
+ || (msg.message >= WM_MOUSEFIRST
+ && msg.message <= WM_MOUSELAST)
+ || msg.message == WM_MOUSEWHEEL
+ || msg.message == WM_MOUSEHWHEEL
+ || msg.message == WM_TOUCH
+#ifndef QT_NO_GESTURES
+ || msg.message == WM_GESTURE
+ || msg.message == WM_GESTURENOTIFY
+#endif
+ || msg.message == WM_CLOSE)) {
+ // queue user input events for later processing
+ haveMessage = false;
+ d->queuedUserInputEvents.append(msg);
+ }
+ if (haveMessage && (flags & QEventLoop::ExcludeSocketNotifiers)
+ && (msg.message == WM_QT_SOCKETNOTIFIER && msg.hwnd == d->internalHwnd)) {
+ // queue socket events for later processing
+ haveMessage = false;
+ d->queuedSocketEvents.append(msg);
+ }
+ }
+ if (!haveMessage) {
+ // no message - check for signalled objects
+ for (int i=0; i<(int)nCount; i++)
+ pHandles[i] = d->winEventNotifierList.at(i)->handle();
+ waitRet = MsgWaitForMultipleObjectsEx(nCount, pHandles, 0, QS_ALLINPUT, MWMO_ALERTABLE);
+ if ((haveMessage = (waitRet == WAIT_OBJECT_0 + nCount))) {
+ // a new message has arrived, process it
+ continue;
+ }
+ }
+ if (haveMessage) {
+#ifdef Q_OS_WINCE
+ // WinCE doesn't support hooks at all, so we have to call this by hand :(
+ (void) qt_GetMessageHook(0, PM_REMOVE, (LPARAM) &msg);
+#endif
+
+ if (d->internalHwnd == msg.hwnd && msg.message == WM_QT_SENDPOSTEDEVENTS) {
+ if (seenWM_QT_SENDPOSTEDEVENTS) {
+ // when calling processEvents() "manually", we only want to send posted
+ // events once
+ needWM_QT_SENDPOSTEDEVENTS = true;
+ continue;
+ }
+ seenWM_QT_SENDPOSTEDEVENTS = true;
+ } else if (msg.message == WM_TIMER) {
+ // avoid live-lock by keeping track of the timers we've already sent
+ bool found = false;
+ for (int i = 0; !found && i < processedTimers.count(); ++i) {
+ const MSG processed = processedTimers.constData()[i];
+ found = (processed.wParam == msg.wParam && processed.hwnd == msg.hwnd && processed.lParam == msg.lParam);
+ }
+ if (found)
+ continue;
+ processedTimers.append(msg);
+ } else if (msg.message == WM_QUIT) {
+ if (QCoreApplication::instance())
+ QCoreApplication::instance()->quit();
+ return false;
+ }
+
+ if (!filterEvent(&msg)) {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ } else if (waitRet >= WAIT_OBJECT_0 && waitRet < WAIT_OBJECT_0 + nCount) {
+ d->activateEventNotifier(d->winEventNotifierList.at(waitRet - WAIT_OBJECT_0));
+ } else {
+ // nothing todo so break
+ break;
+ }
+ retVal = true;
+ }
+
+ // still nothing - wait for message or signalled objects
+ canWait = (!retVal
+ && !d->interrupt
+ && (flags & QEventLoop::WaitForMoreEvents));
+ if (canWait) {
+ DWORD nCount = d->winEventNotifierList.count();
+ Q_ASSERT(nCount < MAXIMUM_WAIT_OBJECTS - 1);
+ for (int i=0; i<(int)nCount; i++)
+ pHandles[i] = d->winEventNotifierList.at(i)->handle();
+
+ emit aboutToBlock();
+ waitRet = MsgWaitForMultipleObjectsEx(nCount, pHandles, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
+ emit awake();
+ if (waitRet >= WAIT_OBJECT_0 && waitRet < WAIT_OBJECT_0 + nCount) {
+ d->activateEventNotifier(d->winEventNotifierList.at(waitRet - WAIT_OBJECT_0));
+ retVal = true;
+ }
+ }
+ } while (canWait);
+
+ if (!seenWM_QT_SENDPOSTEDEVENTS && (flags & QEventLoop::EventLoopExec) == 0) {
+ // when called "manually", always send posted events
+ QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData);
+ }
+
+ if (needWM_QT_SENDPOSTEDEVENTS)
+ PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0);
+
+ return retVal;
+}
+
+bool QEventDispatcherWin32::hasPendingEvents()
+{
+ MSG msg;
+ return qGlobalPostedEventsCount() || PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
+}
+
+void QEventDispatcherWin32::registerSocketNotifier(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+ int sockfd = notifier->socket();
+ int type = notifier->type();
+#ifndef QT_NO_DEBUG
+ if (sockfd < 0) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherWin32);
+ QSNDict *sn_vec[3] = { &d->sn_read, &d->sn_write, &d->sn_except };
+ QSNDict *dict = sn_vec[type];
+
+ if (QCoreApplication::closingDown()) // ### d->exitloop?
+ return; // after sn_cleanup, don't reinitialize.
+
+ if (dict->contains(sockfd)) {
+ const char *t[] = { "Read", "Write", "Exception" };
+ /* Variable "socket" below is a function pointer. */
+ qWarning("QSocketNotifier: Multiple socket notifiers for "
+ "same socket %d and type %s", sockfd, t[type]);
+ }
+
+ QSockNot *sn = new QSockNot;
+ sn->obj = notifier;
+ sn->fd = sockfd;
+ dict->insert(sn->fd, sn);
+
+ if (d->internalHwnd)
+ d->doWsaAsyncSelect(sockfd);
+}
+
+void QEventDispatcherWin32::unregisterSocketNotifier(QSocketNotifier *notifier)
+{
+ Q_ASSERT(notifier);
+ int sockfd = notifier->socket();
+ int type = notifier->type();
+#ifndef QT_NO_DEBUG
+ if (sockfd < 0) {
+ qWarning("QSocketNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread");
+ return;
+ }
+#endif
+
+ Q_D(QEventDispatcherWin32);
+ QSNDict *sn_vec[3] = { &d->sn_read, &d->sn_write, &d->sn_except };
+ QSNDict *dict = sn_vec[type];
+ QSockNot *sn = dict->value(sockfd);
+ if (!sn)
+ return;
+
+ dict->remove(sockfd);
+ delete sn;
+
+ if (d->internalHwnd)
+ d->doWsaAsyncSelect(sockfd);
+}
+
+void QEventDispatcherWin32::registerTimer(int timerId, int interval, QObject *object)
+{
+ if (timerId < 1 || interval < 0 || !object) {
+ qWarning("QEventDispatcherWin32::registerTimer: invalid arguments");
+ return;
+ } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QObject::startTimer: timers cannot be started from another thread");
+ return;
+ }
+
+ Q_D(QEventDispatcherWin32);
+
+ register WinTimerInfo *t = new WinTimerInfo;
+ t->dispatcher = this;
+ t->timerId = timerId;
+ t->interval = interval;
+ t->obj = object;
+ t->inTimerEvent = false;
+ t->fastTimerId = 0;
+
+ if (d->internalHwnd)
+ d->registerTimer(t);
+
+ d->timerVec.append(t); // store in timer vector
+ d->timerDict.insert(t->timerId, t); // store timers in dict
+}
+
+bool QEventDispatcherWin32::unregisterTimer(int timerId)
+{
+ if (timerId < 1) {
+ qWarning("QEventDispatcherWin32::unregisterTimer: invalid argument");
+ return false;
+ }
+ QThread *currentThread = QThread::currentThread();
+ if (thread() != currentThread) {
+ qWarning("QObject::killTimer: timers cannot be stopped from another thread");
+ return false;
+ }
+
+ Q_D(QEventDispatcherWin32);
+ if (d->timerVec.isEmpty() || timerId <= 0)
+ return false;
+
+ WinTimerInfo *t = d->timerDict.value(timerId);
+ if (!t)
+ return false;
+
+ d->timerDict.remove(t->timerId);
+ d->timerVec.removeAll(t);
+ d->unregisterTimer(t);
+ return true;
+}
+
+bool QEventDispatcherWin32::unregisterTimers(QObject *object)
+{
+ if (!object) {
+ qWarning("QEventDispatcherWin32::unregisterTimers: invalid argument");
+ return false;
+ }
+ QThread *currentThread = QThread::currentThread();
+ if (object->thread() != thread() || thread() != currentThread) {
+ qWarning("QObject::killTimers: timers cannot be stopped from another thread");
+ return false;
+ }
+
+ Q_D(QEventDispatcherWin32);
+ if (d->timerVec.isEmpty())
+ return false;
+ register WinTimerInfo *t;
+ for (int i=0; i<d->timerVec.size(); i++) {
+ t = d->timerVec.at(i);
+ if (t && t->obj == object) { // object found
+ d->timerDict.remove(t->timerId);
+ d->timerVec.removeAt(i);
+ d->unregisterTimer(t);
+ --i;
+ }
+ }
+ return true;
+}
+
+QList<QEventDispatcherWin32::TimerInfo>
+QEventDispatcherWin32::registeredTimers(QObject *object) const
+{
+ if (!object) {
+ qWarning("QEventDispatcherWin32:registeredTimers: invalid argument");
+ return QList<TimerInfo>();
+ }
+
+ Q_D(const QEventDispatcherWin32);
+ QList<TimerInfo> list;
+ for (int i = 0; i < d->timerVec.size(); ++i) {
+ const WinTimerInfo *t = d->timerVec.at(i);
+ if (t && t->obj == object)
+ list << TimerInfo(t->timerId, t->interval);
+ }
+ return list;
+}
+
+bool QEventDispatcherWin32::registerEventNotifier(QWinEventNotifier *notifier)
+{
+ if (!notifier) {
+ qWarning("QWinEventNotifier: Internal error");
+ return false;
+ } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QWinEventNotifier: event notifiers cannot be enabled from another thread");
+ return false;
+ }
+
+ Q_D(QEventDispatcherWin32);
+
+ if (d->winEventNotifierList.contains(notifier))
+ return true;
+
+ if (d->winEventNotifierList.count() >= MAXIMUM_WAIT_OBJECTS - 2) {
+ qWarning("QWinEventNotifier: Cannot have more than %d enabled at one time", MAXIMUM_WAIT_OBJECTS - 2);
+ return false;
+ }
+ d->winEventNotifierList.append(notifier);
+ return true;
+}
+
+void QEventDispatcherWin32::unregisterEventNotifier(QWinEventNotifier *notifier)
+{
+ if (!notifier) {
+ qWarning("QWinEventNotifier: Internal error");
+ return;
+ } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QWinEventNotifier: event notifiers cannot be disabled from another thread");
+ return;
+ }
+
+ Q_D(QEventDispatcherWin32);
+
+ int i = d->winEventNotifierList.indexOf(notifier);
+ if (i != -1)
+ d->winEventNotifierList.takeAt(i);
+}
+
+void QEventDispatcherWin32::activateEventNotifiers()
+{
+ Q_D(QEventDispatcherWin32);
+ //### this could break if events are removed/added in the activation
+ for (int i=0; i<d->winEventNotifierList.count(); i++) {
+#if !defined(Q_OS_WINCE)
+ if (WaitForSingleObjectEx(d->winEventNotifierList.at(i)->handle(), 0, TRUE) == WAIT_OBJECT_0)
+ d->activateEventNotifier(d->winEventNotifierList.at(i));
+#else
+ if (WaitForSingleObject(d->winEventNotifierList.at(i)->handle(), 0) == WAIT_OBJECT_0)
+ d->activateEventNotifier(d->winEventNotifierList.at(i));
+#endif
+ }
+}
+
+void QEventDispatcherWin32::wakeUp()
+{
+ Q_D(QEventDispatcherWin32);
+ d->serialNumber.ref();
+ if (d->internalHwnd && d->wakeUps.testAndSetAcquire(0, 1)) {
+ // post a WM_QT_SENDPOSTEDEVENTS to this thread if there isn't one already pending
+ PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0);
+ }
+}
+
+void QEventDispatcherWin32::interrupt()
+{
+ Q_D(QEventDispatcherWin32);
+ d->interrupt = true;
+ wakeUp();
+}
+
+void QEventDispatcherWin32::flush()
+{ }
+
+void QEventDispatcherWin32::startingUp()
+{ }
+
+void QEventDispatcherWin32::closingDown()
+{
+ Q_D(QEventDispatcherWin32);
+
+ // clean up any socketnotifiers
+ while (!d->sn_read.isEmpty())
+ unregisterSocketNotifier((*(d->sn_read.begin()))->obj);
+ while (!d->sn_write.isEmpty())
+ unregisterSocketNotifier((*(d->sn_write.begin()))->obj);
+ while (!d->sn_except.isEmpty())
+ unregisterSocketNotifier((*(d->sn_except.begin()))->obj);
+
+ // clean up any timers
+ for (int i = 0; i < d->timerVec.count(); ++i)
+ d->unregisterTimer(d->timerVec.at(i), true);
+ d->timerVec.clear();
+ d->timerDict.clear();
+
+#ifndef Q_OS_WINCE
+ if (d->getMessageHook)
+ UnhookWindowsHookEx(d->getMessageHook);
+ d->getMessageHook = 0;
+#endif
+}
+
+bool QEventDispatcherWin32::event(QEvent *e)
+{
+ Q_D(QEventDispatcherWin32);
+ if (e->type() == QEvent::ZeroTimerEvent) {
+ QZeroTimerEvent *zte = static_cast<QZeroTimerEvent*>(e);
+ WinTimerInfo *t = d->timerDict.value(zte->timerId());
+ if (t) {
+ t->inTimerEvent = true;
+
+ QTimerEvent te(zte->timerId());
+ QCoreApplication::sendEvent(t->obj, &te);
+
+ t = d->timerDict.value(zte->timerId());
+ if (t) {
+ if (t->interval == 0 && t->inTimerEvent) {
+ // post the next zero timer event as long as the timer was not restarted
+ QCoreApplication::postEvent(this, new QZeroTimerEvent(zte->timerId()));
+ }
+
+ t->inTimerEvent = false;
+ }
+ }
+ return true;
+ } else if (e->type() == QEvent::Timer) {
+ QTimerEvent *te = static_cast<QTimerEvent*>(e);
+ d->sendTimerEvent(te->timerId());
+ }
+ return QAbstractEventDispatcher::event(e);
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qeventdispatcher_win_p.h b/src/corelib/kernel/qeventdispatcher_win_p.h
new file mode 100644
index 0000000000..2e3a5bca19
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_win_p.h
@@ -0,0 +1,110 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QEVENTDISPATCHER_WIN_P_H
+#define QEVENTDISPATCHER_WIN_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qabstracteventdispatcher.h"
+#include "QtCore/qt_windows.h"
+
+QT_BEGIN_NAMESPACE
+
+class QWinEventNotifier;
+class QEventDispatcherWin32Private;
+
+// forward declaration
+LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
+
+class Q_CORE_EXPORT QEventDispatcherWin32 : public QAbstractEventDispatcher
+{
+ Q_OBJECT
+ Q_DECLARE_PRIVATE(QEventDispatcherWin32)
+
+ void createInternalHwnd();
+ friend class QGuiEventDispatcherWin32;
+
+public:
+ explicit QEventDispatcherWin32(QObject *parent = 0);
+ ~QEventDispatcherWin32();
+
+ bool QT_ENSURE_STACK_ALIGNED_FOR_SSE processEvents(QEventLoop::ProcessEventsFlags flags);
+ bool hasPendingEvents();
+
+ void registerSocketNotifier(QSocketNotifier *notifier);
+ void unregisterSocketNotifier(QSocketNotifier *notifier);
+
+ void registerTimer(int timerId, int interval, QObject *object);
+ bool unregisterTimer(int timerId);
+ bool unregisterTimers(QObject *object);
+ QList<TimerInfo> registeredTimers(QObject *object) const;
+
+ bool registerEventNotifier(QWinEventNotifier *notifier);
+ void unregisterEventNotifier(QWinEventNotifier *notifier);
+ void activateEventNotifiers();
+
+ void wakeUp();
+ void interrupt();
+ void flush();
+
+ void startingUp();
+ void closingDown();
+
+ bool event(QEvent *e);
+
+private:
+ friend LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
+ friend LRESULT QT_WIN_CALLBACK qt_GetMessageHook(int, WPARAM, LPARAM);
+};
+
+QT_END_NAMESPACE
+
+#endif // QEVENTDISPATCHER_WIN_P_H
diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp
new file mode 100644
index 0000000000..d213b0e968
--- /dev/null
+++ b/src/corelib/kernel/qeventloop.cpp
@@ -0,0 +1,329 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qeventloop.h"
+
+#include "qabstracteventdispatcher.h"
+#include "qcoreapplication.h"
+#include "qelapsedtimer.h"
+
+#include "qobject_p.h"
+#include <private/qthread_p.h>
+
+QT_BEGIN_NAMESPACE
+
+class QEventLoopPrivate : public QObjectPrivate
+{
+ Q_DECLARE_PUBLIC(QEventLoop)
+public:
+ inline QEventLoopPrivate()
+ : exit(true), inExec(false), returnCode(-1)
+ { }
+ bool exit, inExec;
+ int returnCode;
+};
+
+/*!
+ \class QEventLoop
+ \brief The QEventLoop class provides a means of entering and leaving an event loop.
+
+ At any time, you can create a QEventLoop object and call exec()
+ on it to start a local event loop. From within the event loop,
+ calling exit() will force exec() to return.
+
+ \sa QAbstractEventDispatcher
+*/
+
+/*!
+ \enum QEventLoop::ProcessEventsFlag
+
+ This enum co