| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #include "qcoreevent.h" |
| 6 | #include "qcoreevent_p.h" |
| 7 | #include "qcoreapplication.h" |
| 8 | #include "qcoreapplication_p.h" |
| 9 | |
| 10 | #include "qbasicatomic.h" |
| 11 | |
| 12 | #include <qtcore_tracepoints_p.h> |
| 13 | |
| 14 | #include <limits> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | Q_TRACE_POINT(qtcore, QEvent_ctor, QEvent *event, QEvent::Type type); |
| 19 | Q_TRACE_POINT(qtcore, QEvent_dtor, QEvent *event, QEvent::Type type); |
| 20 | |
| 21 | /*! |
| 22 | \class QEvent |
| 23 | \inmodule QtCore |
| 24 | \brief The QEvent class is the base class of all |
| 25 | event classes. Event objects contain event parameters. |
| 26 | |
| 27 | \ingroup events |
| 28 | |
| 29 | Qt's main event loop (QCoreApplication::exec()) fetches native |
| 30 | window system events from the event queue, translates them into |
| 31 | QEvents, and sends the translated events to \l{QObject}s. |
| 32 | |
| 33 | In general, events come from the underlying window system |
| 34 | (spontaneous() returns \c true), but it is also possible to manually |
| 35 | send events using QCoreApplication::sendEvent() and |
| 36 | QCoreApplication::postEvent() (spontaneous() returns \c false). |
| 37 | |
| 38 | \l {QObject}{QObjects} receive events by having their QObject::event() function |
| 39 | called. The function can be reimplemented in subclasses to |
| 40 | customize event handling and add additional event types; |
| 41 | QWidget::event() is a notable example. By default, events are |
| 42 | dispatched to event handlers like QObject::timerEvent() and |
| 43 | QWidget::mouseMoveEvent(). QObject::installEventFilter() allows an |
| 44 | object to intercept events destined for another object. |
| 45 | |
| 46 | The basic QEvent contains only an event type parameter and an |
| 47 | "accept" flag. The accept flag set with accept(), and cleared |
| 48 | with ignore(). It is set by default, but don't rely on this as |
| 49 | subclasses may choose to clear it in their constructor. |
| 50 | |
| 51 | Subclasses of QEvent contain additional parameters that describe |
| 52 | the particular event. |
| 53 | |
| 54 | \sa QObject::event(), QObject::installEventFilter(), |
| 55 | QCoreApplication::sendEvent(), |
| 56 | QCoreApplication::postEvent(), QCoreApplication::processEvents() |
| 57 | */ |
| 58 | |
| 59 | |
| 60 | /*! |
| 61 | \enum QEvent::Type |
| 62 | |
| 63 | This enum type defines the valid event types in Qt. The event |
| 64 | types and the specialized classes for each type are as follows: |
| 65 | |
| 66 | \value None Not an event. |
| 67 | \value ActionAdded A new action has been added (QActionEvent). |
| 68 | \value ActionChanged An action has been changed (QActionEvent). |
| 69 | \value ActionRemoved An action has been removed (QActionEvent). |
| 70 | \value ActivationChange A widget's top-level window activation state has changed. |
| 71 | \value ApplicationActivate This enum has been deprecated. Use ApplicationStateChange instead. |
| 72 | \value ApplicationActivated This enum has been deprecated. Use ApplicationStateChange instead. |
| 73 | \value ApplicationDeactivate This enum has been deprecated. Use ApplicationStateChange instead. |
| 74 | \value ApplicationFontChange The default application font has changed. |
| 75 | \value ApplicationLayoutDirectionChange The default application layout direction has changed. |
| 76 | \value ApplicationPaletteChange The default application palette has changed. |
| 77 | \value ApplicationStateChange The state of the application has changed. |
| 78 | \value ApplicationWindowIconChange The application's icon has changed. |
| 79 | \value ChildAdded An object gets a child (QChildEvent). |
| 80 | \value ChildPolished A widget child gets polished (QChildEvent). |
| 81 | \value ChildRemoved An object loses a child (QChildEvent). |
| 82 | \value [since 6.7] ChildWindowAdded A child window was added to the window. |
| 83 | \value [since 6.7] ChildWindowRemoved A child window was removed from the window. |
| 84 | \value Clipboard The clipboard contents have changed. |
| 85 | \value Close Widget was closed (QCloseEvent). |
| 86 | \value CloseSoftwareInputPanel A widget wants to close the software input panel (SIP). |
| 87 | \value ContentsRectChange The margins of the widget's content rect changed. |
| 88 | \value ContextMenu Context popup menu (QContextMenuEvent). |
| 89 | \value CursorChange The widget's cursor has changed. |
| 90 | \value DeferredDelete The object will be deleted after it has cleaned up (QDeferredDeleteEvent) |
| 91 | \value [since 6.6] DevicePixelRatioChange |
| 92 | The devicePixelRatio has changed for this widget's or window's underlying backing store. |
| 93 | \value DragEnter The cursor enters a widget during a drag and drop operation (QDragEnterEvent). |
| 94 | \value DragLeave The cursor leaves a widget during a drag and drop operation (QDragLeaveEvent). |
| 95 | \value DragMove A drag and drop operation is in progress (QDragMoveEvent). |
| 96 | \value Drop A drag and drop operation is completed (QDropEvent). |
| 97 | \value DynamicPropertyChange A dynamic property was added, changed, or removed from the object. |
| 98 | \value EnabledChange Widget's enabled state has changed. |
| 99 | \value Enter Mouse enters widget's boundaries (QEnterEvent). |
| 100 | \value EnterEditFocus An editor widget gains focus for editing. \c QT_KEYPAD_NAVIGATION must be defined. |
| 101 | \value EnterWhatsThisMode Send to toplevel widgets when the application enters "What's This?" mode. |
| 102 | \value Expose Sent to a window when its on-screen contents are invalidated and need to be flushed from the backing store. |
| 103 | \value FileOpen File open request (QFileOpenEvent). |
| 104 | \value FocusIn Widget or Window gains keyboard focus (QFocusEvent). |
| 105 | \value FocusOut Widget or Window loses keyboard focus (QFocusEvent). |
| 106 | \value FocusAboutToChange Widget or Window focus is about to change (QFocusEvent) |
| 107 | \value FontChange Widget's font has changed. |
| 108 | \value Gesture A gesture was triggered (QGestureEvent). |
| 109 | \value GestureOverride A gesture override was triggered (QGestureEvent). |
| 110 | \value GrabKeyboard Item gains keyboard grab (QGraphicsItem only). |
| 111 | \value GrabMouse Item gains mouse grab (QGraphicsItem only). |
| 112 | \value GraphicsSceneContextMenu Context popup menu over a graphics scene (QGraphicsSceneContextMenuEvent). |
| 113 | \value GraphicsSceneDragEnter The cursor enters a graphics scene during a drag and drop operation (QGraphicsSceneDragDropEvent). |
| 114 | \value GraphicsSceneDragLeave The cursor leaves a graphics scene during a drag and drop operation (QGraphicsSceneDragDropEvent). |
| 115 | \value GraphicsSceneDragMove A drag and drop operation is in progress over a scene (QGraphicsSceneDragDropEvent). |
| 116 | \value GraphicsSceneDrop A drag and drop operation is completed over a scene (QGraphicsSceneDragDropEvent). |
| 117 | \value GraphicsSceneHelp The user requests help for a graphics scene (QHelpEvent). |
| 118 | \value GraphicsSceneHoverEnter The mouse cursor enters a hover item in a graphics scene (QGraphicsSceneHoverEvent). |
| 119 | \value GraphicsSceneHoverLeave The mouse cursor leaves a hover item in a graphics scene (QGraphicsSceneHoverEvent). |
| 120 | \value GraphicsSceneHoverMove The mouse cursor moves inside a hover item in a graphics scene (QGraphicsSceneHoverEvent). |
| 121 | \value GraphicsSceneMouseDoubleClick Mouse press again (double click) in a graphics scene (QGraphicsSceneMouseEvent). |
| 122 | \value GraphicsSceneMouseMove Move mouse in a graphics scene (QGraphicsSceneMouseEvent). |
| 123 | \value GraphicsSceneMousePress Mouse press in a graphics scene (QGraphicsSceneMouseEvent). |
| 124 | \value GraphicsSceneMouseRelease Mouse release in a graphics scene (QGraphicsSceneMouseEvent). |
| 125 | \value GraphicsSceneMove Widget was moved (QGraphicsSceneMoveEvent). |
| 126 | \value GraphicsSceneResize Widget was resized (QGraphicsSceneResizeEvent). |
| 127 | \value GraphicsSceneWheel Mouse wheel rolled in a graphics scene (QGraphicsSceneWheelEvent). |
| 128 | \value GraphicsSceneLeave The cursor leaves a graphics scene (QGraphicsSceneWheelEvent). |
| 129 | \value Hide Widget was hidden (QHideEvent). |
| 130 | \value HideToParent A child widget has been hidden. |
| 131 | \value HoverEnter The mouse cursor enters a hover widget (QHoverEvent). |
| 132 | \value HoverLeave The mouse cursor leaves a hover widget (QHoverEvent). |
| 133 | \value HoverMove The mouse cursor moves inside a hover widget (QHoverEvent). |
| 134 | \value IconDrag The main icon of a window has been dragged away (QIconDragEvent). |
| 135 | \value IconTextChange Widget's icon text has been changed. (Deprecated) |
| 136 | \value InputMethod An input method is being used (QInputMethodEvent). |
| 137 | \value InputMethodQuery A input method query event (QInputMethodQueryEvent) |
| 138 | \value KeyboardLayoutChange The keyboard layout has changed. |
| 139 | \value KeyPress Key press (QKeyEvent). |
| 140 | \value KeyRelease Key release (QKeyEvent). |
| 141 | \value LanguageChange The application translation changed. |
| 142 | \value LayoutDirectionChange The direction of layouts changed. |
| 143 | \value LayoutRequest Widget layout needs to be redone. |
| 144 | \value Leave Mouse leaves widget's boundaries. |
| 145 | \value LeaveEditFocus An editor widget loses focus for editing. QT_KEYPAD_NAVIGATION must be defined. |
| 146 | \value LeaveWhatsThisMode Send to toplevel widgets when the application leaves "What's This?" mode. |
| 147 | \value LocaleChange The system locale has changed. |
| 148 | \value NonClientAreaMouseButtonDblClick A mouse double click occurred outside the client area (QMouseEvent). |
| 149 | \value NonClientAreaMouseButtonPress A mouse button press occurred outside the client area (QMouseEvent). |
| 150 | \value NonClientAreaMouseButtonRelease A mouse button release occurred outside the client area (QMouseEvent). |
| 151 | \value NonClientAreaMouseMove A mouse move occurred outside the client area (QMouseEvent). |
| 152 | \value MacSizeChange The user changed his widget sizes (\macos only). |
| 153 | \value MetaCall An asynchronous method invocation via QMetaObject::invokeMethod(). |
| 154 | \value ModifiedChange Widgets modification state has been changed. |
| 155 | \value MouseButtonDblClick Mouse press again (QMouseEvent). |
| 156 | \value MouseButtonPress Mouse press (QMouseEvent). |
| 157 | \value MouseButtonRelease Mouse release (QMouseEvent). |
| 158 | \value MouseMove Mouse move (QMouseEvent). |
| 159 | \value MouseTrackingChange The mouse tracking state has changed. |
| 160 | \value Move Widget's position changed (QMoveEvent). |
| 161 | \value NativeGesture The system has detected a gesture (QNativeGestureEvent). |
| 162 | \value OrientationChange The screens orientation has changes (QScreenOrientationChangeEvent). |
| 163 | \value Paint Screen update necessary (QPaintEvent). |
| 164 | \value PaletteChange Palette of the widget changed. |
| 165 | \value ParentAboutToChange The object parent is about to change. |
| 166 | Only sent to some object types, such as QWidget. |
| 167 | \value ParentChange The object parent has changed. |
| 168 | Only sent to some object types, such as QWidget. |
| 169 | \value [since 6.7] ParentWindowAboutToChange The parent window is about to change. |
| 170 | \value [since 6.7] ParentWindowChange The parent window has changed. |
| 171 | \value PlatformPanel A platform specific panel has been requested. |
| 172 | \value PlatformSurface A native platform surface has been created or is about to be destroyed (QPlatformSurfaceEvent). |
| 173 | \omitvalue Pointer |
| 174 | \value Polish The widget is polished. |
| 175 | \value PolishRequest The widget should be polished. |
| 176 | \value QueryWhatsThis The widget should accept the event if it has "What's This?" help (QHelpEvent). |
| 177 | \value Quit The application has exited. |
| 178 | \value [since 5.4] ReadOnlyChange Widget's read-only state has changed. |
| 179 | \value RequestSoftwareInputPanel A widget wants to open a software input panel (SIP). |
| 180 | \value Resize Widget's size changed (QResizeEvent). |
| 181 | \value ScrollPrepare The object needs to fill in its geometry information (QScrollPrepareEvent). |
| 182 | \value Scroll The object needs to scroll to the supplied position (QScrollEvent). |
| 183 | \value Shortcut Key press in child for shortcut key handling (QShortcutEvent). |
| 184 | \value ShortcutOverride Key press in child, for overriding shortcut key handling (QKeyEvent). |
| 185 | When a shortcut is about to trigger, \c ShortcutOverride |
| 186 | is sent to the active window. This allows clients (e.g. widgets) |
| 187 | to signal that they will handle the shortcut themselves, by |
| 188 | accepting the event. If the shortcut override is accepted, the |
| 189 | event is delivered as a normal key press to the focus widget. |
| 190 | Otherwise, it triggers the shortcut action, if one exists. |
| 191 | \value Show Widget was shown on screen (QShowEvent). |
| 192 | \value ShowToParent A child widget has been shown. |
| 193 | \value SockAct Socket activated, used to implement QSocketNotifier. |
| 194 | \omitvalue SockClose |
| 195 | \value StateMachineSignal A signal delivered to a state machine (QStateMachine::SignalEvent). |
| 196 | \value StateMachineWrapped The event is a wrapper for, i.e., contains, another event (QStateMachine::WrappedEvent). |
| 197 | \value StatusTip A status tip is requested (QStatusTipEvent). |
| 198 | \value StyleChange Widget's style has been changed. |
| 199 | \value TabletMove Wacom tablet move (QTabletEvent). |
| 200 | \value TabletPress Wacom tablet press (QTabletEvent). |
| 201 | \value TabletRelease Wacom tablet release (QTabletEvent). |
| 202 | \omitvalue OkRequest |
| 203 | \value TabletEnterProximity Wacom tablet enter proximity event (QTabletEvent), sent to QApplication. |
| 204 | \value TabletLeaveProximity Wacom tablet leave proximity event (QTabletEvent), sent to QApplication. |
| 205 | \value [since 5.9] TabletTrackingChange The Wacom tablet tracking state has changed. |
| 206 | \omitvalue ThemeChange |
| 207 | \value ThreadChange The object is moved to another thread. This is the last event sent to this object in the previous thread. See QObject::moveToThread(). |
| 208 | \value Timer Regular timer events (QTimerEvent). |
| 209 | \value ToolBarChange The toolbar button is toggled on \macos. |
| 210 | \value ToolTip A tooltip was requested (QHelpEvent). |
| 211 | \value ToolTipChange The widget's tooltip has changed. |
| 212 | \value TouchBegin Beginning of a sequence of touch-screen or track-pad events (QTouchEvent). |
| 213 | \value TouchCancel Cancellation of touch-event sequence (QTouchEvent). |
| 214 | \value TouchEnd End of touch-event sequence (QTouchEvent). |
| 215 | \value TouchUpdate Touch-screen event (QTouchEvent). |
| 216 | \value UngrabKeyboard Item loses keyboard grab (QGraphicsItem only). |
| 217 | \value UngrabMouse Item loses mouse grab (QGraphicsItem, QQuickItem). |
| 218 | \value UpdateLater The widget should be queued to be repainted at a later time. |
| 219 | \value UpdateRequest The widget should be repainted. |
| 220 | \value WhatsThis The widget should reveal "What's This?" help (QHelpEvent). |
| 221 | \value WhatsThisClicked A link in a widget's "What's This?" help was clicked. |
| 222 | \value Wheel Mouse wheel rolled (QWheelEvent). |
| 223 | \value WinEventAct A Windows-specific activation event has occurred. |
| 224 | \value WindowActivate Window was activated. |
| 225 | \value WindowBlocked The window is blocked by a modal dialog. |
| 226 | \value WindowDeactivate Window was deactivated. |
| 227 | \value WindowIconChange The window's icon has changed. |
| 228 | \value WindowStateChange The \l{QWindow::windowState()}{window's state} (minimized, maximized or full-screen) has changed (QWindowStateChangeEvent). |
| 229 | \value WindowTitleChange The window title has changed. |
| 230 | \value WindowUnblocked The window is unblocked after a modal dialog exited. |
| 231 | \value WinIdChange The window system identifier for this native widget has changed. |
| 232 | \value ZOrderChange The widget's z-order has changed. This event is never sent to top level windows. |
| 233 | |
| 234 | User events should have values between \c User and \c{MaxUser}: |
| 235 | |
| 236 | \value User User-defined event. |
| 237 | \value MaxUser Last user event ID. |
| 238 | |
| 239 | For convenience, you can use the registerEventType() function to |
| 240 | register and reserve a custom event type for your |
| 241 | application. Doing so will allow you to avoid accidentally |
| 242 | re-using a custom event type already in use elsewhere in your |
| 243 | application. |
| 244 | |
| 245 | \omitvalue AcceptDropsChange |
| 246 | \omitvalue ActivateControl |
| 247 | \omitvalue Create |
| 248 | \omitvalue DeactivateControl |
| 249 | \omitvalue Destroy |
| 250 | \omitvalue DragResponse |
| 251 | \omitvalue EmbeddingControl |
| 252 | \omitvalue HelpRequest |
| 253 | \omitvalue Quit |
| 254 | \omitvalue ShowWindowRequest |
| 255 | \omitvalue Speech |
| 256 | \omitvalue Style |
| 257 | \omitvalue StyleAnimationUpdate |
| 258 | \omitvalue ZeroTimerEvent |
| 259 | \omitvalue ApplicationActivate |
| 260 | \omitvalue ApplicationActivated |
| 261 | \omitvalue ApplicationDeactivate |
| 262 | \omitvalue ApplicationDeactivated |
| 263 | \omitvalue MacGLWindowChange |
| 264 | \omitvalue NetworkReplyUpdated |
| 265 | \omitvalue FutureCallOut |
| 266 | \omitvalue NativeGesture |
| 267 | \omitvalue WindowChangeInternal |
| 268 | \omitvalue ScreenChangeInternal |
| 269 | \omitvalue WindowAboutToChangeInternal |
| 270 | */ |
| 271 | |
| 272 | /*! |
| 273 | Constructs an event object of type \a type. |
| 274 | */ |
| 275 | QEvent::QEvent(Type type) |
| 276 | : t(type), m_reserved(0), |
| 277 | m_inputEvent(false), m_pointerEvent(false), m_singlePointEvent(false) |
| 278 | { |
| 279 | Q_TRACE(QEvent_ctor, this, type); |
| 280 | } |
| 281 | |
| 282 | /*! |
| 283 | \fn QEvent::QEvent(const QEvent &other) |
| 284 | \internal |
| 285 | Copies the \a other event. |
| 286 | */ |
| 287 | |
| 288 | /*! |
| 289 | \internal |
| 290 | \since 6.0 |
| 291 | \fn QEvent::QEvent(Type type, QEvent::InputEventTag) |
| 292 | |
| 293 | Constructs an event object of type \a type, setting the inputEvent flag to \c true. |
| 294 | */ |
| 295 | |
| 296 | /*! |
| 297 | \internal |
| 298 | \since 6.0 |
| 299 | \fn QEvent::QEvent(Type type, QEvent::PointerEventTag) |
| 300 | |
| 301 | Constructs an event object of type \a type, setting the pointerEvent and |
| 302 | inputEvent flags to \c true. |
| 303 | */ |
| 304 | |
| 305 | /*! |
| 306 | \internal |
| 307 | \since 6.0 |
| 308 | \fn QEvent::QEvent(Type type, QEvent::SinglePointEventTag) |
| 309 | |
| 310 | Constructs an event object of type \a type, setting the singlePointEvent, |
| 311 | pointerEvent and inputEvent flags to \c true. |
| 312 | */ |
| 313 | |
| 314 | /*! |
| 315 | \fn QEvent &QEvent::operator=(const QEvent &other) |
| 316 | \internal |
| 317 | Attempts to copy the \a other event. |
| 318 | |
| 319 | Copying events is a bad idea, yet some Qt 4 code does it (notably, |
| 320 | QApplication and the state machine). |
| 321 | */ |
| 322 | |
| 323 | /*! |
| 324 | Destroys the event. If it was \l{QCoreApplication::postEvent()}{posted}, |
| 325 | it will be removed from the list of events to be posted. |
| 326 | */ |
| 327 | |
| 328 | QEvent::~QEvent() |
| 329 | { |
| 330 | if (m_posted && QCoreApplication::instance()) |
| 331 | QCoreApplicationPrivate::removePostedEvent(this); |
| 332 | } |
| 333 | |
| 334 | /*! |
| 335 | Creates and returns an identical copy of this event. |
| 336 | \since 6.0 |
| 337 | */ |
| 338 | QEvent *QEvent::clone() const |
| 339 | { return new QEvent(*this); } |
| 340 | |
| 341 | /*! |
| 342 | \property QEvent::accepted |
| 343 | \brief the accept flag of the event object. |
| 344 | |
| 345 | Setting the accept parameter indicates that the event receiver |
| 346 | wants the event. Unwanted events might be propagated to the parent |
| 347 | widget. By default, isAccepted() is set to true, but don't rely on |
| 348 | this as subclasses may choose to clear it in their constructor. |
| 349 | |
| 350 | For convenience, the accept flag can also be set with accept(), |
| 351 | and cleared with ignore(). |
| 352 | |
| 353 | \note Accepting a QPointerEvent implicitly |
| 354 | \l {QEventPoint::setAccepted()}{accepts} all the |
| 355 | \l {QPointerEvent::points()}{points} that the event carries. |
| 356 | */ |
| 357 | |
| 358 | /*! |
| 359 | \fn void QEvent::accept() |
| 360 | |
| 361 | Sets the accept flag of the event object, the equivalent of |
| 362 | calling setAccepted(true). |
| 363 | |
| 364 | Setting the accept parameter indicates that the event receiver |
| 365 | wants the event. Unwanted events might be propagated to the parent |
| 366 | widget. |
| 367 | |
| 368 | \sa ignore() |
| 369 | */ |
| 370 | |
| 371 | |
| 372 | /*! |
| 373 | \fn void QEvent::ignore() |
| 374 | |
| 375 | Clears the accept flag parameter of the event object, the |
| 376 | equivalent of calling setAccepted(false). |
| 377 | |
| 378 | Clearing the accept parameter indicates that the event receiver |
| 379 | does not want the event. Unwanted events might be propagated to the |
| 380 | parent widget. |
| 381 | |
| 382 | \sa accept() |
| 383 | */ |
| 384 | |
| 385 | |
| 386 | /*! |
| 387 | \fn QEvent::Type QEvent::type() const |
| 388 | |
| 389 | Returns the event type. |
| 390 | */ |
| 391 | |
| 392 | /*! |
| 393 | \fn bool QEvent::spontaneous() const |
| 394 | |
| 395 | Returns \c true if the event originated outside the application (a |
| 396 | system event); otherwise returns \c false. |
| 397 | */ |
| 398 | |
| 399 | /*! |
| 400 | \fn bool QEvent::isInputEvent() const |
| 401 | \since 6.0 |
| 402 | |
| 403 | Returns \c true if the event object is a QInputEvent or one of its |
| 404 | subclasses. |
| 405 | */ |
| 406 | |
| 407 | /*! |
| 408 | \fn bool QEvent::isPointerEvent() const |
| 409 | \since 6.0 |
| 410 | |
| 411 | Returns \c true if the event object is a QPointerEvent or one of its |
| 412 | subclasses. |
| 413 | */ |
| 414 | |
| 415 | /*! |
| 416 | \fn bool QEvent::isSinglePointEvent() const |
| 417 | \since 6.0 |
| 418 | |
| 419 | Returns \c true if the event object is a subclass of QSinglePointEvent. |
| 420 | */ |
| 421 | |
| 422 | namespace { |
| 423 | template <size_t N> |
| 424 | struct QBasicAtomicBitField { |
| 425 | enum { |
| 426 | BitsPerInt = std::numeric_limits<uint>::digits, |
| 427 | NumInts = (N + BitsPerInt - 1) / BitsPerInt, |
| 428 | NumBits = N |
| 429 | }; |
| 430 | |
| 431 | // This atomic int points to the next (possibly) free ID saving |
| 432 | // the otherwise necessary scan through 'data': |
| 433 | QBasicAtomicInteger<uint> next; |
| 434 | QBasicAtomicInteger<uint> data[NumInts]; |
| 435 | |
| 436 | constexpr QBasicAtomicBitField() = default; |
| 437 | |
| 438 | bool allocateSpecific(int which) noexcept |
| 439 | { |
| 440 | QBasicAtomicInteger<uint> &entry = data[which / BitsPerInt]; |
| 441 | const uint old = entry.loadRelaxed(); |
| 442 | const uint bit = 1U << (which % BitsPerInt); |
| 443 | if (old & bit) |
| 444 | return false; // already taken |
| 445 | return (entry.fetchAndOrRelaxed(valueToAdd: bit) & bit) == 0; |
| 446 | |
| 447 | // don't update 'next' here - it's unlikely that it will need |
| 448 | // to be updated, in the general case, and having 'next' |
| 449 | // trailing a bit is not a problem, as it is just a starting |
| 450 | // hint for allocateNext(), which, when wrong, will just |
| 451 | // result in a few more rounds through the allocateNext() |
| 452 | // loop. |
| 453 | } |
| 454 | |
| 455 | int allocateNext() noexcept |
| 456 | { |
| 457 | // Unroll loop to iterate over ints, then bits? Would save |
| 458 | // potentially a lot of cmpxchgs, because we can scan the |
| 459 | // whole int before having to load it again. |
| 460 | |
| 461 | // Then again, this should never execute many iterations, so |
| 462 | // leave like this for now: |
| 463 | for (uint i = next.loadRelaxed(); i < NumBits; ++i) { |
| 464 | if (allocateSpecific(which: i)) { |
| 465 | // remember next (possibly) free id: |
| 466 | const uint oldNext = next.loadRelaxed(); |
| 467 | next.testAndSetRelaxed(expectedValue: oldNext, newValue: qMax(a: i + 1, b: oldNext)); |
| 468 | return i; |
| 469 | } |
| 470 | } |
| 471 | return -1; |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | } // unnamed namespace |
| 476 | |
| 477 | typedef QBasicAtomicBitField<QEvent::MaxUser - QEvent::User + 1> UserEventTypeRegistry; |
| 478 | |
| 479 | Q_CONSTINIT static UserEventTypeRegistry userEventTypeRegistry {}; |
| 480 | |
| 481 | static inline int registerEventTypeZeroBased(int id) noexcept |
| 482 | { |
| 483 | // if the type hint hasn't been registered yet, take it: |
| 484 | if (id < UserEventTypeRegistry::NumBits && id >= 0 && userEventTypeRegistry.allocateSpecific(which: id)) |
| 485 | return id; |
| 486 | |
| 487 | // otherwise, ignore hint: |
| 488 | return userEventTypeRegistry.allocateNext(); |
| 489 | } |
| 490 | |
| 491 | /*! |
| 492 | \since 4.4 |
| 493 | \threadsafe |
| 494 | |
| 495 | Registers and returns a custom event type. The \a hint provided |
| 496 | will be used if it is available, otherwise it will return a value |
| 497 | between QEvent::User and QEvent::MaxUser that has not yet been |
| 498 | registered. The \a hint is ignored if its value is not between |
| 499 | QEvent::User and QEvent::MaxUser. |
| 500 | |
| 501 | Returns -1 if all available values are already taken or the |
| 502 | program is shutting down. |
| 503 | */ |
| 504 | int QEvent::registerEventType(int hint) noexcept |
| 505 | { |
| 506 | const int result = registerEventTypeZeroBased(id: QEvent::MaxUser - hint); |
| 507 | return result < 0 ? -1 : QEvent::MaxUser - result ; |
| 508 | } |
| 509 | |
| 510 | /*! |
| 511 | \class QTimerEvent |
| 512 | \inmodule QtCore |
| 513 | \brief The QTimerEvent class contains parameters that describe a |
| 514 | timer event. |
| 515 | |
| 516 | \ingroup events |
| 517 | |
| 518 | Timer events are sent at regular intervals to objects that have |
| 519 | started one or more timers. Each timer has a unique identifier. A |
| 520 | timer is started with QObject::startTimer(). |
| 521 | |
| 522 | The QChronoTimer class provides a high-level programming interface that |
| 523 | uses signals instead of events. |
| 524 | |
| 525 | The event handler QObject::timerEvent() receives timer events. |
| 526 | |
| 527 | \sa QChronoTimer, QObject::timerEvent(), QObject::startTimer(), |
| 528 | QObject::killTimer() |
| 529 | */ |
| 530 | |
| 531 | /*! |
| 532 | Constructs a timer event object with the timer identifier set to |
| 533 | \a timerId. |
| 534 | */ |
| 535 | QTimerEvent::QTimerEvent(int timerId) |
| 536 | : QTimerEvent(Qt::TimerId{timerId}) |
| 537 | {} |
| 538 | |
| 539 | /*! |
| 540 | \since 6.8 |
| 541 | |
| 542 | Constructs a timer event object with the timer identifier set to |
| 543 | \a timerId. |
| 544 | */ |
| 545 | QTimerEvent::QTimerEvent(Qt::TimerId timerId) |
| 546 | : QEvent(Timer), m_id(timerId) |
| 547 | { |
| 548 | static_assert(sizeof(Qt::TimerId) == sizeof(int)); |
| 549 | } |
| 550 | |
| 551 | Q_IMPL_EVENT_COMMON(QTimerEvent) |
| 552 | |
| 553 | /*! |
| 554 | \fn int QTimerEvent::timerId() const |
| 555 | |
| 556 | Returns the unique timer identifier, which is the same identifier |
| 557 | as returned from QObject::startTimer(). |
| 558 | */ |
| 559 | |
| 560 | /*! |
| 561 | \fn Qt::TimerId QTimerEvent::id() const |
| 562 | \since 6.8 |
| 563 | |
| 564 | Returns the Qt::TimerId of the timer associated with this event, which |
| 565 | is the same identifier returned by QObject::startTimer() cast to |
| 566 | Qt::TimerId. |
| 567 | */ |
| 568 | |
| 569 | /*! |
| 570 | \class QChildEvent |
| 571 | \inmodule QtCore |
| 572 | \brief The QChildEvent class contains event parameters for child object |
| 573 | events. |
| 574 | |
| 575 | \ingroup events |
| 576 | |
| 577 | Child events are sent immediately to objects when children are |
| 578 | added or removed. |
| 579 | |
| 580 | In both cases you can only rely on the child being a QObject (or, |
| 581 | if QObject::isWidgetType() returns \c true, a QWidget). This is |
| 582 | because in the QEvent::ChildAdded case the child is not yet fully |
| 583 | constructed; in the QEvent::ChildRemoved case it might have |
| 584 | already been destructed. |
| 585 | |
| 586 | The handler for these events is QObject::childEvent(). |
| 587 | */ |
| 588 | |
| 589 | /*! |
| 590 | Constructs a child event object of a particular \a type for the |
| 591 | \a child. |
| 592 | |
| 593 | \a type can be QEvent::ChildAdded, QEvent::ChildRemoved, |
| 594 | or QEvent::ChildPolished. |
| 595 | |
| 596 | \sa child() |
| 597 | */ |
| 598 | QChildEvent::QChildEvent(Type type, QObject *child) |
| 599 | : QEvent(type), c(child) |
| 600 | {} |
| 601 | |
| 602 | Q_IMPL_EVENT_COMMON(QChildEvent) |
| 603 | |
| 604 | /*! |
| 605 | \fn QObject *QChildEvent::child() const |
| 606 | |
| 607 | Returns the child object that was added or removed. |
| 608 | */ |
| 609 | |
| 610 | /*! |
| 611 | \fn bool QChildEvent::added() const |
| 612 | |
| 613 | Returns \c true if type() is QEvent::ChildAdded; otherwise returns |
| 614 | false. |
| 615 | */ |
| 616 | |
| 617 | /*! |
| 618 | \fn bool QChildEvent::removed() const |
| 619 | |
| 620 | Returns \c true if type() is QEvent::ChildRemoved; otherwise returns |
| 621 | false. |
| 622 | */ |
| 623 | |
| 624 | /*! |
| 625 | \fn bool QChildEvent::polished() const |
| 626 | |
| 627 | Returns \c true if type() is QEvent::ChildPolished; otherwise returns |
| 628 | false. |
| 629 | */ |
| 630 | |
| 631 | |
| 632 | /*! |
| 633 | \class QDynamicPropertyChangeEvent |
| 634 | \inmodule QtCore |
| 635 | \since 4.2 |
| 636 | \brief The QDynamicPropertyChangeEvent class contains event parameters for dynamic |
| 637 | property change events. |
| 638 | |
| 639 | \ingroup events |
| 640 | |
| 641 | Dynamic property change events are sent to objects when properties are |
| 642 | dynamically added, changed or removed using QObject::setProperty(). |
| 643 | */ |
| 644 | |
| 645 | /*! |
| 646 | Constructs a dynamic property change event object with the property name set to |
| 647 | \a name. |
| 648 | */ |
| 649 | QDynamicPropertyChangeEvent::QDynamicPropertyChangeEvent(const QByteArray &name) |
| 650 | : QEvent(QEvent::DynamicPropertyChange), n(name) |
| 651 | { |
| 652 | } |
| 653 | |
| 654 | Q_IMPL_EVENT_COMMON(QDynamicPropertyChangeEvent) |
| 655 | |
| 656 | /*! |
| 657 | \fn QByteArray QDynamicPropertyChangeEvent::propertyName() const |
| 658 | |
| 659 | Returns the name of the dynamic property that was added, changed or |
| 660 | removed. |
| 661 | |
| 662 | \sa QObject::setProperty(), QObject::dynamicPropertyNames() |
| 663 | */ |
| 664 | |
| 665 | /*! |
| 666 | Constructs a deferred delete event with the given loop and scope level. |
| 667 | */ |
| 668 | QDeferredDeleteEvent::QDeferredDeleteEvent(int loopLevel, int scopeLevel) |
| 669 | : QEvent(QEvent::DeferredDelete), m_loopLevel(loopLevel), m_scopeLevel(scopeLevel) |
| 670 | { } |
| 671 | |
| 672 | Q_IMPL_EVENT_COMMON(QDeferredDeleteEvent) |
| 673 | |
| 674 | QT_END_NAMESPACE |
| 675 | |
| 676 | #include "moc_qcoreevent.cpp" |
| 677 | |