bool MouseEventFilter::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseMove)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent)
        {
            ApplicationManager::singleton()->setMouseX(mouseEvent->pos().x());
            ApplicationManager::singleton()->setMouseY(mouseEvent->pos().y());

            if(mouseEvent->buttons() && Qt::LeftButton && ApplicationManager::singleton()->grabbingWindowMoveHandle() &&
                    ApplicationManager::singleton()->windowControlButtonsEnabled())
            {
                QPoint diffPosition = mouseEvent->pos() - mousePos.toPoint();
                QPoint newPosition = ApplicationManager::singleton()->window()->position() + diffPosition;

                int oldWindowWidth = ApplicationManager::singleton()->window()->width();
                if(ApplicationManager::singleton()->showNormal())
                {
                    int windowWidth = ApplicationManager::singleton()->window()->width();
                    ApplicationManager::singleton()->window()->setPosition(newPosition.x() + (oldWindowWidth - windowWidth) * (mousePos.x() / oldWindowWidth), newPosition.y());
                    mousePos.setX(mousePos.x() - (oldWindowWidth - windowWidth) * (mousePos.x() / oldWindowWidth));
                }
                else
                {
                    ApplicationManager::singleton()->window()->setPosition(newPosition);
                }
            }
            else if(mouseEvent->buttons() && Qt::LeftButton && ApplicationManager::singleton()->grabbingWindowResizeHandle() &&
                    ApplicationManager::singleton()->windowControlButtonsEnabled())
            {
                QPoint diffPosition = mouseEvent->pos() - mousePos.toPoint();
                mousePos = mouseEvent->pos();

                QWindow *window = ApplicationManager::singleton()->window();
                int newWidth = window->width() + diffPosition.x();
                int newHeight = window->height() + diffPosition.y();

                if(newWidth < 1024) newWidth = 1024;
                if(newHeight < 600) newHeight = 600;

                window->setGeometry(window->position().x(), window->position().y(), newWidth, newHeight);
            }
        }
    }
    else if(event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent)
        {
            mousePos = mouseEvent->pos();
        }
    }
    return QObject::eventFilter(obj, event);
}
 static void mouseUp(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/, jint x, jint y)
 {
     QPoint globalPos(x,y);
     QWindow *tlw = m_mouseGrabber.data();
     if (!tlw)
         tlw = topLevelWindowAt(globalPos);
     QPoint localPos = tlw ? (globalPos -tlw->position()) : globalPos;
     QWindowSystemInterface::handleMouseEvent(tlw, localPos, globalPos
                                             , Qt::MouseButtons(Qt::NoButton));
     m_ignoreMouseEvents = false;
     m_mouseGrabber = 0;
 }
    static void mouseDown(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/, jint x, jint y)
    {
        if (m_ignoreMouseEvents)
            return;

        QPoint globalPos(x,y);
        QWindow *tlw = topLevelWindowAt(globalPos);
        m_mouseGrabber = tlw;
        QPoint localPos = tlw ? (globalPos - tlw->position()) : globalPos;
        QWindowSystemInterface::handleMouseEvent(tlw,
                                                 localPos,
                                                 globalPos,
                                                 Qt::MouseButtons(Qt::LeftButton));
    }
    static void longPress(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/, jint x, jint y)
    {
        //### TODO: add proper API for Qt 5.2
        static bool rightMouseFromLongPress = qgetenv("QT_NECESSITAS_COMPATIBILITY_LONG_PRESS").toInt();
        if (!rightMouseFromLongPress)
            return;
        m_ignoreMouseEvents = true;
        QPoint globalPos(x,y);
        QWindow *tlw = topLevelWindowAt(globalPos);
        QPoint localPos = tlw ? (globalPos-tlw->position()) : globalPos;

        // Release left button
        QWindowSystemInterface::handleMouseEvent(tlw,
                                                 localPos,
                                                 globalPos,
                                                 Qt::MouseButtons(Qt::NoButton));

        // Press right button
        QWindowSystemInterface::handleMouseEvent(tlw,
                                                 localPos,
                                                 globalPos,
                                                 Qt::MouseButtons(Qt::RightButton));
    }