Beispiel #1
0
    static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons,
                                Qt::KeyboardModifiers stateKey,
                                QPointF _pos, int xDelta, int yDelta, int delay = -1)
    {
        QTEST_ASSERT(window);
        QTEST_ASSERT(item);
        if (delay == -1 || delay < QTest::defaultMouseDelay())
            delay = QTest::defaultMouseDelay();
        if (delay > 0)
            QTest::qWait(delay);

        QPoint pos;
        QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
        if (sgitem)
            pos = sgitem->mapToScene(_pos).toPoint();

        QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
        QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);

        stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
        QWheelEvent we(pos, window->mapToGlobal(pos), QPoint(0, 0), QPoint(xDelta, yDelta), 0, Qt::Vertical, buttons, stateKey);

        QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
        if (!qApp->notify(window, &we))
            QTest::qWarn("Wheel event not accepted by receiving window");
    }
static void mouseEvent(MouseAction _action, QWindow* _window, QObject* _item, Qt::MouseButton _button, Qt::KeyboardModifiers _stateKey, QPointF _pos, int _delay = -1)
{
    if (_delay == -1 || _delay < 30)
        _delay = 30;
    if (_delay > 0)
        QTest::qWait(_delay);

    if (_action == MouseClick)
    {
        mouseEvent(MousePress, _window, _item, _button, _stateKey, _pos);
        mouseEvent(MouseRelease, _window, _item, _button, _stateKey, _pos);
        return;
    }

    QPoint pos = _pos.toPoint();
    QQuickItem* sgitem = qobject_cast<QQuickItem*>(_item);
    if (sgitem)
        pos = sgitem->mapToScene(_pos).toPoint();

    _stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);

    QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, _button, _stateKey);
    switch (_action)
    {
    case MousePress:
        me = QMouseEvent(QEvent::MouseButtonPress, pos, _window->mapToGlobal(pos), _button, _button, _stateKey);
        break;
    case MouseRelease:
        me = QMouseEvent(QEvent::MouseButtonRelease, pos, _window->mapToGlobal(pos), _button, 0, _stateKey);
        break;
    case MouseDoubleClick:
        me = QMouseEvent(QEvent::MouseButtonDblClick, pos, _window->mapToGlobal(pos), _button, _button, _stateKey);
        break;
    case MouseMove:
        // with move event the _button is NoButton, but 'buttons' holds the currently pressed buttons
        me = QMouseEvent(QEvent::MouseMove, pos, _window->mapToGlobal(pos), Qt::NoButton, _button, _stateKey);
        break;
    default:
        break;
    }
    QSpontaneKeyEvent::setSpontaneous(&me);
    if (!qApp->notify(_window, &me))
    {
        static const char* mouseActionNames[] = { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" };
        QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window");
        QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(_action)])).toLatin1().data());
    }
}
/*!
  Traverse QObject based items.
*/
void SceneGraphTraverse::traverseObject(TasObject* objectInfo, QObject* object, TasCommand* command)
{
    Q_UNUSED(command);

    QQuickItem* item = qobject_cast<QQuickItem*>(object);

    if (item) {
        QQmlContext* context = QQmlEngine::contextForObject(object);

        if (context) {
            QString name = context->nameForObject(object);
            objectInfo->addAttribute("QML_ID", name);
        }

#ifdef USE_QTQML_PRIVATE_HEADERS
        addTypeInfo(item, objectInfo);
#endif

        mTraverseUtils->addObjectDetails(objectInfo, object);

        objectInfo->addAttribute("objectType", TYPE_QSCENEGRAPH);

        QPointF point = item->mapToScene(QPoint());

        // needed for visualizer
        objectInfo->addAttribute("x", (int)point.x());
        objectInfo->addAttribute("y", (int)point.y());
        objectInfo->addAttribute("x_absolute", (int)point.x());
        objectInfo->addAttribute("y_absolute", (int)point.y());


        objectInfo->addAttribute("x_relative", item->x());
        objectInfo->addAttribute("y_relative", item->y());

        // TODO already included?
        objectInfo->addAttribute("width", item->width());
        objectInfo->addAttribute("height", item->height());
    }
}
Beispiel #4
0
    static void mouseEvent(MouseAction action, QWindow *window,
                           QObject *item, Qt::MouseButton button,
                           Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
    {
        QTEST_ASSERT(window);
        QTEST_ASSERT(item);

        if (delay == -1 || delay < QTest::defaultMouseDelay())
            delay = QTest::defaultMouseDelay();
        if (delay > 0)
            QTest::qWait(delay);

        if (action == MouseClick) {
            mouseEvent(MousePress, window, item, button, stateKey, _pos);
            mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
            return;
        }

        if (action == MouseDoubleClickSequence) {
            mouseEvent(MousePress, window, item, button, stateKey, _pos);
            mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
            mouseEvent(MousePress, window, item, button, stateKey, _pos);
            mouseEvent(MouseDoubleClick, window, item, button, stateKey, _pos);
            mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
            return;
        }

        QPoint pos;
        QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
        if (sgitem)
            pos = sgitem->mapToScene(_pos).toPoint();
        QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
        QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);

        stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);

        QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
        switch (action)
        {
            case MousePress:
                me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
                break;
            case MouseRelease:
                me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
                break;
            case MouseDoubleClick:
                me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
                break;
            case MouseMove:
                // with move event the button is NoButton, but 'buttons' holds the currently pressed buttons
                me = QMouseEvent(QEvent::MouseMove, pos, window->mapToGlobal(pos), Qt::NoButton, button, stateKey);
                break;
            default:
                QTEST_ASSERT(false);
        }
        QSpontaneKeyEvent::setSpontaneous(&me);
        if (!qApp->notify(window, &me)) {
            static const char *mouseActionNames[] =
                { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove", "MouseDoubleClickSequence" };
            QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window");
            QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toLatin1().data());
        }
    }
void ScreenshotService::getScreenshot(TasCommandModel& model, TasResponse& response)
{
    QListIterator<TasTarget*> i(model.targetList());
    QString errorMsg = PARSE_ERROR;
    QImage screenshot;
    QString pictureFormat = "PNG";
    while (i.hasNext()) {
        TasTarget* commandTarget = i.next();
        QString targetId = commandTarget->id();
        QString targetType = commandTarget->type();
        TasCommand* command = commandTarget->findCommand("Screenshot");

        // are required for command completion
        if (targetId.isEmpty() || targetType.isEmpty() || !command) {
            continue;
        }

        if (!command->parameter("format").isEmpty()) {
            pictureFormat = command->parameter("format");
        }

        if (!isFormatSupported(pictureFormat)) {
            errorMsg = "Given format " + pictureFormat + "is not supported. Supported formats are: PNG, JPEG and BMP.";
            break;
        }

        bool draw = (command->parameter("draw") == "true");

        QWidget* widget = 0;
        QQuickWindow* qtQuickWindow = 0;
        WId winId = 0;
        QRect rect(0,0,-1,-1);

        errorMsg = "Taking screenshot failed!";

        if (targetType == TYPE_GRAPHICS_VIEW) {
            //TasLogger::logger()->debug("TYPE_GRAPHICS_VIEW Target id:" + targetId);
            QGraphicsItem* item = findGraphicsItem(targetId);

            if (item) {
                QGraphicsView* view = getViewForItem(item);
                if(view) {
                    ItemLocationDetails locationDetails = TestabilityUtils::getItemLocationDetails(item);
                    rect = QRect(locationDetails.windowPoint.x(),
                                 locationDetails.windowPoint.y(),
                                 locationDetails.width,
                                 locationDetails.height);

                    if (draw) {
                        widget = view->window();
                    } else {
                        winId = view->window()->winId();
                    }
                } else {
                    errorMsg = "Could not find a GraphicsView for the GraphicsItem!";
                }
            } else {
                errorMsg = "Could not find the GraphicsItem!";
            }
        } else if (targetType == TYPE_STANDARD_VIEW) {
            //TasLogger::logger()->debug("TYPE_STANDARD_VIEW about to find widget Target id:" + targetId);
            widget = findWidget(targetId);

            if (widget) {
                if ((widget->isWindow() && !draw) || widget->inherits("QDesktopWidget")) {
                    winId = widget->winId();
                    widget = 0;
                } else if (!draw) {
                    QPoint point = widget->mapToGlobal(QPoint(0,0));
                    QPoint windowPoint = widget->window()->mapFromGlobal(point);

                    rect = QRect(windowPoint.x(),
                                 windowPoint.y(),
                                 widget->rect().width(),
                                 widget->rect().width());
                    winId = widget->window()->winId();
                    widget = 0;
                }
            } else {
                TasLogger::logger()->debug("ScreenshotService::executeService application has no visible ui!");
                errorMsg = "Application has no visible ui!";
            }
        } else if (targetType == TYPE_QSCENEGRAPH) {
            QQuickItem* item = TestabilityUtils::findQuickItem(targetId);

            if (item) {
                QPointF offset = item->mapToScene(QPointF(0,0));
                rect = QRect(-offset.x(), -offset.y(), item->width(), item->height());
                qtQuickWindow = item->window();
            }
        } else {
            //TasLogger::logger()->debug("TYPE_APPLICATION_VIEW about to find application window Target id:" + targetId);
            widget = getApplicationWidget();

            if (!widget) {
                QWindow *window = getApplicationWindow();
                //in case no window false, return the desktop
                qtQuickWindow = qobject_cast<QQuickWindow *>(window);

                if (!window) {
                    widget = qApp->desktop();
                }
            }
        }

        if (widget) {
            screenshot = widget->grab(rect).toImage();

            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", objectId(widget));
            }
        } else if (qtQuickWindow) {
            screenshot = qtQuickWindow->grabWindow();
            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", objectId(qtQuickWindow));
            }
        } else if (winId) {
            screenshot = QPixmap::grabWindow(winId, rect.x(), rect.y(), rect.width(), rect.height()).toImage();

            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", QString::number(winId));
            }
        }

        break;
    }

    if (!screenshot.isNull()) {
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        screenshot.save(&buffer, pictureFormat.toLatin1());
        response.setData(bytes);
        buffer.close();
    } else {
        response.setErrorMessage(errorMsg);
    }

}