tests/auto/macnativeevents/qnativeevents.cpp
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qnativeevents.h"
       
    43 
       
    44 QNativeInput::QNativeInput(bool subscribe)
       
    45 {
       
    46     if (subscribe)
       
    47         subscribeForNativeEvents();
       
    48 }
       
    49 
       
    50 QNativeInput::~QNativeInput()
       
    51 {
       
    52     unsubscribeForNativeEvents();
       
    53 }
       
    54 
       
    55 void QNativeInput::notify(QNativeEvent *event)
       
    56 {
       
    57     nativeEvent(event);
       
    58 }
       
    59 
       
    60 void QNativeInput::nativeEvent(QNativeEvent *event)
       
    61 {
       
    62     switch (event->id()){
       
    63         case QNativeMouseButtonEvent::eventId:{
       
    64             QNativeMouseButtonEvent *e = static_cast<QNativeMouseButtonEvent *>(event);
       
    65             (e->clickCount > 0) ? nativeMousePressEvent(e) : nativeMouseReleaseEvent(e);
       
    66             break; }
       
    67         case QNativeMouseMoveEvent::eventId:
       
    68             nativeMouseMoveEvent(static_cast<QNativeMouseMoveEvent *>(event));
       
    69             break;
       
    70         case QNativeMouseDragEvent::eventId:
       
    71             nativeMouseDragEvent(static_cast<QNativeMouseDragEvent *>(event));
       
    72             break;
       
    73         case QNativeMouseWheelEvent::eventId:
       
    74             nativeMouseWheelEvent(static_cast<QNativeMouseWheelEvent *>(event));
       
    75             break;
       
    76         case QNativeKeyEvent::eventId:{
       
    77             QNativeKeyEvent *e = static_cast<QNativeKeyEvent *>(event);
       
    78             e->press ? nativeKeyPressEvent(e) : nativeKeyReleaseEvent(e);
       
    79             break; }
       
    80         case QNativeModifierEvent::eventId:
       
    81             nativeModifierEvent(static_cast<QNativeModifierEvent *>(event));
       
    82             break;
       
    83         default:
       
    84             break;
       
    85     }
       
    86 }
       
    87 
       
    88 Qt::Native::Status QNativeInput::sendNativeEvent(const QNativeEvent &event, int pid)
       
    89 {
       
    90     switch (event.id()){
       
    91         case QNativeMouseMoveEvent::eventId:
       
    92             return sendNativeMouseMoveEvent(static_cast<const QNativeMouseMoveEvent &>(event));
       
    93         case QNativeMouseButtonEvent::eventId:
       
    94             return sendNativeMouseButtonEvent(static_cast<const QNativeMouseButtonEvent &>(event));
       
    95         case QNativeMouseDragEvent::eventId:
       
    96             return sendNativeMouseDragEvent(static_cast<const QNativeMouseDragEvent &>(event));
       
    97         case QNativeMouseWheelEvent::eventId:
       
    98             return sendNativeMouseWheelEvent(static_cast<const QNativeMouseWheelEvent &>(event));
       
    99         case QNativeKeyEvent::eventId:
       
   100             return sendNativeKeyEvent(static_cast<const QNativeKeyEvent &>(event), pid);
       
   101         case QNativeModifierEvent::eventId:
       
   102             return sendNativeModifierEvent(static_cast<const QNativeModifierEvent &>(event));
       
   103         case QNativeEvent::eventId:
       
   104             qWarning() << "Warning: Cannot send a pure native event. Use a sub class.";
       
   105         default:
       
   106             return Qt::Native::Failure;
       
   107     }
       
   108 }
       
   109 
       
   110 QNativeEvent::QNativeEvent(Qt::KeyboardModifiers modifiers)
       
   111     : modifiers(modifiers){}
       
   112 
       
   113 QNativeMouseEvent::QNativeMouseEvent(QPoint pos, Qt::KeyboardModifiers modifiers)
       
   114     : QNativeEvent(modifiers), globalPos(pos){}
       
   115 
       
   116 QNativeMouseMoveEvent::QNativeMouseMoveEvent(QPoint pos, Qt::KeyboardModifiers modifiers)
       
   117     : QNativeMouseEvent(pos, modifiers){}
       
   118 
       
   119 QNativeMouseButtonEvent::QNativeMouseButtonEvent(QPoint globalPos, Qt::MouseButton button, int clickCount, Qt::KeyboardModifiers modifiers)
       
   120     : QNativeMouseEvent(globalPos, modifiers), button(button), clickCount(clickCount){}
       
   121 
       
   122 QNativeMouseDragEvent::QNativeMouseDragEvent(QPoint globalPos, Qt::MouseButton button, Qt::KeyboardModifiers modifiers)
       
   123     : QNativeMouseButtonEvent(globalPos, button, true, modifiers){}
       
   124 
       
   125 QNativeMouseWheelEvent::QNativeMouseWheelEvent(QPoint globalPos, int delta, Qt::KeyboardModifiers modifiers)
       
   126     : QNativeMouseEvent(globalPos, modifiers), delta(delta){}
       
   127 
       
   128 QNativeKeyEvent::QNativeKeyEvent(int nativeKeyCode, bool press, Qt::KeyboardModifiers modifiers)
       
   129     : QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode), press(press), character(QChar()){}
       
   130 
       
   131 QNativeModifierEvent::QNativeModifierEvent(Qt::KeyboardModifiers modifiers, int nativeKeyCode)
       
   132     : QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode){}
       
   133 
       
   134 QNativeKeyEvent::QNativeKeyEvent(int nativeKeyCode, bool press, QChar character, Qt::KeyboardModifiers modifiers)
       
   135     : QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode), press(press), character(character){}
       
   136 
       
   137 static QString getButtonAsString(const QNativeMouseButtonEvent *e)
       
   138 {
       
   139     switch (e->button){
       
   140         case Qt::LeftButton:
       
   141             return "button = LeftButton";
       
   142             break;
       
   143         case Qt::RightButton:
       
   144             return "button = RightButton";
       
   145             break;
       
   146         case Qt::MidButton:
       
   147             return "button = MidButton";
       
   148             break;
       
   149         default:
       
   150             return "button = Other";
       
   151             break;
       
   152     }
       
   153 }
       
   154 
       
   155 static QString getModifiersAsString(const QNativeEvent *e)
       
   156 {
       
   157     if (e->modifiers == 0)
       
   158         return "modifiers = none";
       
   159 
       
   160     QString tmp = "modifiers = ";
       
   161     if (e->modifiers.testFlag(Qt::ShiftModifier))
       
   162         tmp += "Shift";
       
   163     if (e->modifiers.testFlag(Qt::ControlModifier))
       
   164         tmp += "Control";
       
   165     if (e->modifiers.testFlag(Qt::AltModifier))
       
   166         tmp += "Alt";
       
   167     if (e->modifiers.testFlag(Qt::MetaModifier))
       
   168         tmp += "Meta";
       
   169     return tmp;
       
   170 }
       
   171 
       
   172 static QString getPosAsString(QPoint pos)
       
   173 {
       
   174     return QString("QPoint(%1, %2)").arg(pos.x()).arg(pos.y());
       
   175 }
       
   176 
       
   177 static QString getBoolAsString(bool b)
       
   178 {
       
   179     return b ? QString("true") : QString("false");
       
   180 }
       
   181 
       
   182 QString QNativeMouseMoveEvent::toString() const
       
   183 {
       
   184     return QString("QNativeMouseMoveEvent(globalPos = %1 %2)").arg(getPosAsString(globalPos))
       
   185         .arg(getModifiersAsString(this));
       
   186 }
       
   187 
       
   188 QString QNativeMouseButtonEvent::toString() const
       
   189 {
       
   190     return QString("QNativeMouseButtonEvent(globalPos = %1, %2, clickCount = %3, %4)").arg(getPosAsString(globalPos))
       
   191         .arg(getButtonAsString(this)).arg(clickCount).arg(getModifiersAsString(this));
       
   192 }
       
   193 
       
   194 QString QNativeMouseDragEvent::toString() const
       
   195 {
       
   196     return QString("QNativeMouseDragEvent(globalPos = %1, %2, clickCount = %3, %4)").arg(getPosAsString(globalPos))
       
   197     .arg(getButtonAsString(this)).arg(clickCount).arg(getModifiersAsString(this));
       
   198 }
       
   199 
       
   200 QString QNativeMouseWheelEvent::toString() const
       
   201 {
       
   202     return QString("QNativeMouseWheelEvent(globalPos = %1, delta = %2, %3)").arg(getPosAsString(globalPos))
       
   203         .arg(delta).arg(getModifiersAsString(this));
       
   204 }
       
   205 
       
   206 QString QNativeKeyEvent::toString() const
       
   207 {
       
   208     return QString("QNativeKeyEvent(press = %1, native key code = %2, character = %3, %4)").arg(getBoolAsString(press))
       
   209         .arg(nativeKeyCode).arg(character.isPrint() ? character : QString("<no char>"))
       
   210         .arg(getModifiersAsString(this));
       
   211 }
       
   212 
       
   213 QString QNativeModifierEvent::toString() const
       
   214 {
       
   215     return QString("QNativeModifierEvent(%1, native key code = %2)").arg(getModifiersAsString(this))
       
   216         .arg(nativeKeyCode);
       
   217 }
       
   218 
       
   219 QDebug operator<<(QDebug d, QNativeEvent *e)
       
   220 {
       
   221     Q_UNUSED(e);
       
   222     return d << e->toString();
       
   223 }
       
   224 
       
   225 QDebug operator<<(QDebug d, const QNativeEvent &e)
       
   226 {
       
   227     Q_UNUSED(e);
       
   228     return d << e.toString();
       
   229 }
       
   230 
       
   231 QTextStream &operator<<(QTextStream &s, QNativeEvent *e)
       
   232 {
       
   233     return s << e->eventId << " " << e->modifiers << " QNativeEvent";
       
   234 }
       
   235 
       
   236 QTextStream &operator<<(QTextStream &s, QNativeMouseEvent *e)
       
   237 {
       
   238     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->modifiers << " " << e->toString();
       
   239 }
       
   240 
       
   241 QTextStream &operator<<(QTextStream &s, QNativeMouseMoveEvent *e)
       
   242 {
       
   243     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->modifiers << " " << e->toString();
       
   244 }
       
   245 
       
   246 QTextStream &operator<<(QTextStream &s, QNativeMouseButtonEvent *e)
       
   247 {
       
   248     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->button
       
   249         << " " << e->clickCount << " " << e->modifiers << " " << e->toString();
       
   250 }
       
   251 
       
   252 QTextStream &operator<<(QTextStream &s, QNativeMouseDragEvent *e)
       
   253 {
       
   254     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->button << " " << e->clickCount
       
   255         << " " << e->modifiers << " " << e->toString();
       
   256 }
       
   257 
       
   258 QTextStream &operator<<(QTextStream &s, QNativeMouseWheelEvent *e)
       
   259 {
       
   260     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->delta
       
   261         << " " << e->modifiers << " " << e->toString();
       
   262 }
       
   263 
       
   264 QTextStream &operator<<(QTextStream &s, QNativeKeyEvent *e)
       
   265 {
       
   266     return s << e->eventId << " " << e->press << " " << e->nativeKeyCode << " " << e->character
       
   267         << " " << e->modifiers << " " << e->toString();
       
   268 }
       
   269 
       
   270 QTextStream &operator<<(QTextStream &s, QNativeModifierEvent *e)
       
   271 {
       
   272     return s << e->eventId << " " << e->modifiers << " " << e->nativeKeyCode << " " << e->toString();
       
   273 }
       
   274 
       
   275 
       
   276 
       
   277 
       
   278 QTextStream &operator>>(QTextStream &s, QNativeMouseMoveEvent *e)
       
   279 {
       
   280     // Skip reading eventId.
       
   281     QString humanReadable;
       
   282     int x, y, modifiers;
       
   283     s >> x >> y >> modifiers >> humanReadable;
       
   284     e->globalPos.setX(x);
       
   285     e->globalPos.setY(y);
       
   286     e->modifiers = Qt::KeyboardModifiers(modifiers);
       
   287     return s;
       
   288 }
       
   289 
       
   290 QTextStream &operator>>(QTextStream &s, QNativeMouseButtonEvent *e)
       
   291 {
       
   292     // Skip reading eventId.
       
   293     QString humanReadable;
       
   294     int x, y, button, clickCount, modifiers;
       
   295     s >> x >> y >> button >> clickCount >> modifiers >> humanReadable;
       
   296     e->globalPos.setX(x);
       
   297     e->globalPos.setY(y);
       
   298     e->clickCount = clickCount;
       
   299     e->modifiers = Qt::KeyboardModifiers(modifiers);
       
   300     switch (button){
       
   301         case 1:
       
   302             e->button = Qt::LeftButton;
       
   303             break;
       
   304         case 2:
       
   305             e->button = Qt::RightButton;
       
   306             break;
       
   307         case 3:
       
   308             e->button = Qt::MidButton;
       
   309             break;
       
   310         default:
       
   311             e->button = Qt::NoButton;
       
   312             break;
       
   313     }
       
   314     return s;
       
   315 }
       
   316 
       
   317 QTextStream &operator>>(QTextStream &s, QNativeMouseDragEvent *e)
       
   318 {
       
   319     // Skip reading eventId.
       
   320     QString humanReadable;
       
   321     int x, y, button, clickCount, modifiers;
       
   322     s >> x >> y >> button >> clickCount >> modifiers >> humanReadable;
       
   323     e->globalPos.setX(x);
       
   324     e->globalPos.setY(y);
       
   325     e->clickCount = clickCount;
       
   326     e->modifiers = Qt::KeyboardModifiers(modifiers);
       
   327     switch (button){
       
   328         case 1:
       
   329             e->button = Qt::LeftButton;
       
   330             break;
       
   331         case 2:
       
   332             e->button = Qt::RightButton;
       
   333             break;
       
   334         case 3:
       
   335             e->button = Qt::MidButton;
       
   336             break;
       
   337         default:
       
   338             e->button = Qt::NoButton;
       
   339             break;
       
   340     }
       
   341     return s;
       
   342 }
       
   343 
       
   344 QTextStream &operator>>(QTextStream &s, QNativeMouseWheelEvent *e)
       
   345 {
       
   346     // Skip reading eventId.
       
   347     QString humanReadable;
       
   348     int x, y, modifiers;
       
   349     s >> x >> y >> e->delta >> modifiers >> humanReadable;
       
   350     e->globalPos.setX(x);
       
   351     e->globalPos.setY(y);
       
   352     e->modifiers = Qt::KeyboardModifiers(modifiers);
       
   353     return s;
       
   354 }
       
   355 
       
   356 QTextStream &operator>>(QTextStream &s, QNativeKeyEvent *e)
       
   357 {
       
   358     // Skip reading eventId.
       
   359     QString humanReadable;
       
   360     int press, modifiers;
       
   361     QString character;
       
   362     s >> press >> e->nativeKeyCode >> character >> modifiers >> humanReadable;
       
   363     e->press = bool(press);
       
   364     e->character = character[0];
       
   365     e->modifiers = Qt::KeyboardModifiers(modifiers);
       
   366     return s;
       
   367 }
       
   368 
       
   369 QTextStream &operator>>(QTextStream &s, QNativeModifierEvent *e)
       
   370 {
       
   371     // Skip reading eventId.
       
   372     QString humanReadable;
       
   373     int modifiers;
       
   374     s >> modifiers >> e->nativeKeyCode >> humanReadable;
       
   375     e->modifiers = Qt::KeyboardModifiers(modifiers);
       
   376     return s;
       
   377 }
       
   378