Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
connyhald
Posts: 102
Registered: ‎06-03-2012
My Device: Playbook
My Carrier: *
Accepted Solution

Qt and BlackBerry Platform Services (BPS)

Hi,

 

in my Qt5 game I'm trying to get events from the Navigator through BPS. Basically I need to know the window state (fullscreen, thumbnail) to pause the game when not running fullscreen.

 

The problem is that I only receive a couple of events during startup. Later while the game is running no events are available from the event queue anymore. I know that Qt4 and Qt5 contain code to integrate BPS events into the Qt eventloop.

 

Still, after reading most of that code I'm still unsure what happens. Does Qt take the events out of the queue before I do? What happens to those events? Are they delivered through Qt somehow?

 

If anyone knows something about that I'd be grateful for all hints and pointer.

 

Thanks!

Conny

 

-----------
http://kodira.de
BB Apps: Pattern :: Plonk
Please use plain text.
Contributor
mcguire
Posts: 24
Registered: ‎09-10-2012
My Device: Blackberry Dev Alpha
My Carrier: -

Re: Qt and BlackBerry Platform Services (BPS)

Right, BPS events are integrated into the Qt eventloop. When Qt is processing events, it also reads all BPS events and processes them. This also mean no one else should read BPS events (i.e. bps_get_event()), because otherwise these events are not available for Qt itself, and it might miss vital events needed to work properly.

 

Qt provides hooks to retrieve system events (BPS events in our case), see QAbstractEventDispatcher::installNativeEventFilter(). See https://qt.gitorious.org/qt/qtmultimedia/blobs/master/src/plugins/blackberry/bbmediaplayercontrol.cp... for an example on how it is used.

 

In your specfic case, you don't even need to use BPS events in any way, since you'll get QEvent::ApplicationActivate and QEvent::ApplicationDeactivate events from Qt when the application goes into thumbnail state, IIRC.

Please use plain text.
Developer
connyhald
Posts: 102
Registered: ‎06-03-2012
My Device: Playbook
My Carrier: *

Re: Qt and BlackBerry Platform Services (BPS)

Thanks a lot for the reply and sorry for the delay. Now I finally have some time...

 

I have now checked for events that are normally emitted. Unfortunately I only get QEvent::FocusIn and QEvent::FocusOut. I could use FocusIn to resume, but FocusOut is only called once I activate another application and not as soon as the task switcher is shown.

I'll now look at the QAbstractEventDispatcher. I'll report back once I know more.

 

-----------
http://kodira.de
BB Apps: Pattern :: Plonk
Please use plain text.
Contributor
mcguire
Posts: 24
Registered: ‎09-10-2012
My Device: Blackberry Dev Alpha
My Carrier: -

Re: Qt and BlackBerry Platform Services (BPS)

In the Blackberry plugin for Qt, we use the navigator event NAVIGATOR_WINDOW_INACTIVE and NAVIGATOR_WINDOW_ACTIVE for activating/deactivating the application (which also translates into FocusOut/FocusIn events).

So I guess it is the navigator who doesn't send NAVIGATOR_WINDOW_INACTIVE when the app goes to thumbnail state, but only when the user switches to another application.

 

I suppose you can listen to NAVIGATOR_WINDOW_STATE events, that looks like it makes a difference between thumbnail and fullscreen.

Please use plain text.
Developer
connyhald
Posts: 102
Registered: ‎06-03-2012
My Device: Playbook
My Carrier: *

Re: Qt and BlackBerry Platform Services (BPS)

Yes, it's exactly like that. Here is the code I'm using now. So far it seems to work great. Thanks again for the help!

 

#include <QDebug>
#include <bps/navigator.h>

#include "blackberryeventfilter.h"

BlackberryEventFilter::BlackberryEventFilter(QObject *parent) :
    QObject(parent)
{
}

bool BlackberryEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(eventType);
    Q_UNUSED(result);

    bps_event_t * const event = static_cast<bps_event_t *>(message);

    // Check if it is a navigator event
    if (!event || (bps_event_get_domain(event) != navigator_get_domain())) {
        return false;
    }

    unsigned int code = bps_event_get_code(event);

    switch (code) {

    case NAVIGATOR_WINDOW_INACTIVE:
        qDebug() << "INFO: Window inactive";
        break;

    case NAVIGATOR_WINDOW_ACTIVE:
        qDebug() << "INFO: Window active";
        break;

    case NAVIGATOR_WINDOW_STATE:
        navigator_window_state_t state = navigator_event_get_window_state(event);
        if (state == NAVIGATOR_WINDOW_FULLSCREEN) {
            qDebug() << "INFO: Resume game";
        } else {
            qDebug() << "INFO: Pause game";
        }
        break;
    }

    // Don't stop event from beeing processed further
    return false;
}

 

-----------
http://kodira.de
BB Apps: Pattern :: Plonk
Please use plain text.
Developer
Shwarup
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: Qt and BlackBerry Platform Services (BPS)

Hi Connyhald,

 

Thanks for sharing your code. I have a question. your "nativeEventFilter()" method require a set of parameter how to get those parameter?

Thanks,

Shwarup Biswas
Please use plain text.
Developer
connyhald
Posts: 102
Registered: ‎06-03-2012
My Device: Playbook
My Carrier: *

Re: Qt and BlackBerry Platform Services (BPS)

[ Edited ]

Simply register the filter with QGuiApplication like that:

 

BlackberryEventFilter *filter = new BlackberryEventFilter();
QGuiApplication::instance()->installNativeEventFilter(filter);

Hope that helps :smileyhappy:

 

-----------
http://kodira.de
BB Apps: Pattern :: Plonk
Please use plain text.
Developer
nguafack
Posts: 44
Registered: ‎05-22-2013
My Device: Blackberry 10
My Carrier: 02

Re: Qt and BlackBerry Platform Services (BPS)

https://qt.gitorious.org/qt/qtmultimedia/blobs/master/src/plugins/blackberry/bbmediaplayercontrol.cp...

 

This link is no longer available, Anyone knows where I can find it, I wan to learn how to use QAbstractEventDispatcher::installNativeEventFilter(), to handle BPS events in Qt.

Please use plain text.
Developer
connyhald
Posts: 102
Registered: ‎06-03-2012
My Device: Playbook
My Carrier: *

Re: Qt and BlackBerry Platform Services (BPS)

Please use plain text.
Developer
nguafack
Posts: 44
Registered: ‎05-22-2013
My Device: Blackberry 10
My Carrier: 02

Re: Qt and BlackBerry Platform Services (BPS)

I am developing a BB10 graphics app using QGraphicsView framework of Qt.  I ahve implement the graphics view and can display my tiles. 

 

Now I want to implement gestures like Pinch Pan and Swipe. I am using the Qt's QGestureEvent, my app crashes and I only see a black screen.

 

Here is the code for getting the gesture events and my constructor.

 

MapView::MapView(QGraphicsScene *scene, QWidget *parent) :
    //QWidget(parent),
    QGraphicsView(scene, parent),
    mScene(scene),
    rotationAngle(0),
    scaleFactor(1),
    currentStepScaleFactor(1)
{

    //Accept touch events.
    viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
    // receive gesture events
    viewport()->grabGesture(Qt::PanGesture);
    viewport()->grabGesture(Qt::PinchGesture);
    viewport()->grabGesture(Qt::SwipeGesture);

    //construct the graphics view.
    graphicsView = new View(this);
    graphicsView->setScene(mScene);
    //graphicsView->mapFromScene(mScene->sceneRect());
    graphicsView->ensureVisible ( mScene->sceneRect()  );

    graphicsView->setRenderHint(QPainter::Antialiasing, false);
    //graphicsView->setBackgroundBrush(QColor(Qt::lightGray));
    graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    //graphicsView->ensureVisible(QRectF(0, 0, 0, 0));

    setupMatrix();
    populateScene();
}

void MapView::populateScene()
{

    //Populate Scene
    int xx = 0;
    for (int i = -384; i < 384; i += 256) { //348
        ++xx;
        int yy = 0;
        for (int j = -640; j < 640; j += 256) {//640
            ++yy;
            qreal x = (i + 384) / 768.0;
            qreal y = (j + 640) / 1280.0;

            QImage image(":/images/test.png");
            image.scaled(x, y, Qt::KeepAspectRatio);

            QGraphicsObject *tile = new Tile(image, i, j);
            //tile->setPos(QPointF(i, j));
            mScene->addItem(tile);
        }
    }
    graphicsView->fitInView(mScene->itemsBoundingRect(),
                            Qt::KeepAspectRatio);
}

void MapView::setupMatrix()
{
    qreal scale = scaleFactor;

    QMatrix matrix;
    matrix.scale(scale, scale);
    matrix.rotate(rotationAngle);
    graphicsView->setMatrix(matrix);
}

void MapView::resetView()
{
    setupMatrix();
}

bool MapView::viewportEvent(QEvent *event)
{
    if (event->type() == QEvent::Gesture) {
        return gestureEvent(static_cast<QGestureEvent*>(event));
    }
   return QGraphicsView::viewportEvent(event);;
}

bool MapView::gestureEvent(QGestureEvent *event)
{
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
        swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    else if (QGesture *pan = event->gesture(Qt::PanGesture))
        panTriggered(static_cast<QPanGesture *>(pan));
    if (QGesture *pinch = event->gesture(Qt::PinchGesture))
        pinchTriggered(static_cast<QPinchGesture *>(pinch));
    return true;
}

void MapView::swipeTriggered(QSwipeGesture *gesture)
{
    if (gesture->state() == Qt::GestureFinished) {
        if (gesture->horizontalDirection() == QSwipeGesture::Left)
        {

        }
        else if (gesture->horizontalDirection() == QSwipeGesture::Right)
        {

        }
        else if (gesture->verticalDirection() == QSwipeGesture::Up)
        {

        }
        else if (gesture->verticalDirection() == QSwipeGesture::Down)
        {

        }

        update();
    }
}

void MapView::panTriggered(QPanGesture *gesture)
{
    switch (gesture->state()) {
        case Qt::GestureStarted:
        case Qt::GestureUpdated:

            break;
    default:
            return;

    }

    update();
}

void MapView::pinchTriggered(QPinchGesture *gesture)
{
    QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
    if (changeFlags & QPinchGesture::RotationAngleChanged) {
        qreal value = gesture->property("rotationAngle").toReal();
        qreal lastValue = gesture->property("lastRotationAngle").toReal();
        rotationAngle += value - lastValue;
    }
    if (changeFlags & QPinchGesture::ScaleFactorChanged) {
        qreal value = gesture->property("scaleFactor").toReal();
        currentStepScaleFactor = value;
    }
    if (gesture->state() == Qt::GestureFinished) {
        scaleFactor *= currentStepScaleFactor;
        currentStepScaleFactor = 1;
    }
    update();
}

 Any advice on how to do this will be much appreciated.  Also if I can only archeive this by bps native event filter a guide for gestures will do as well.

 

Thanks

 

Please use plain text.