×

Discussion Board

Results 1 to 4 of 4

Thread: Jerky Swipe

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Mumbai, India
    Posts
    32

    Jerky Swipe

    i,

    I have implemented a swipe class to implement swiping on a widget. However the resulting scroll is jerky and not smooth. Sometimes while swiping up/down the widget would scroll in the direction opposite to what it should.

    The code is as follows:

    SwipeWidget.
    ===========
    #ifndef SWIPEWIDGET_H
    #define SWIPEWIDGET_H

    #include <QWidget>
    #include <QGesture>
    #include <QGestureEvent>
    #include <QScrollBar>

    /*#include "flickable.h"

    class SwipeWidgetDlg : public QWidget, public Flickable
    {
    Q_OBJECT

    public:
    explicit SwipeWidgetDlg(QWidget *parent = 0);
    void setInitialPosition(int topPos, int botPos);
    void setWidgetHeight(int widgetHeight);

    signals:
    void widgetClicked(QWidget *);

    protected:
    // reimplement for SwipeWidgetDlg
    virtual QPoint scrollOffset() const;

    // reimplement for SwipeWidgetDlg
    virtual bool setScrollOffset(const QPoint &offset);

    protected:

    void keyReleaseEvent(QKeyEvent *event);

    void mousePressEvent(QMouseEvent *event);

    void mouseMoveEvent(QMouseEvent *event);

    void mouseReleaseEvent(QMouseEvent *event);

    private:
    int iTy;
    int iBy;
    int m_initTopPos;
    int m_initBotPos;
    int m_offset;
    int m_height;
    int m_highlight;
    int m_selected;
    QPoint m_mouseDown;
    };*/


    class SwipeWidgetDlg : public QWidget
    {
    Q_OBJECT
    private:
    QPoint m_mouseDown;
    QPoint m_mouseMove;
    int iTy;
    int iBy;
    int m_initTopPos;
    int m_initBotPos;
    int m_totalHeight;
    int m_height;
    bool m_clickStatus;
    int iM;

    public:
    explicit SwipeWidgetDlg(QWidget *parent = 0);
    void setInitialPosition(int topPos, int botPos);
    void setWidgetHeight(int widgetHeight);
    void setTotalHeight(int totalHeight);
    void mousePressEvent (QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent (QMouseEvent *);
    void setClickStatus(bool aClickStatus);
    void setMultFact(int m)
    {
    iM = m;
    }

    protected:
    bool eventFilter(QObject *obj, QEvent *event);

    signals:
    void widgetClicked(QWidget *);
    };

    #endif // SWIPEWIDGET_H

    SwipWidget.cpp
    =============
    #include "SwipeWidget.h"
    #include <QGestureEvent>
    #include <QGesture>
    #include <QLabel>
    #include <QVBoxLayout>
    #include <QScrollBar>
    #include <QHBoxLayout>
    #include <QApplication>
    #include <QtGlobal>
    #include <QSettings>
    #include <QPainter>
    #include <QTimer>

    /*SwipeWidgetDlg::SwipeWidgetDlg(QWidget *parent)
    : QWidget(parent) {
    iTy = 0;
    iBy = 0;
    m_height = 0;
    m_offset = 0;
    m_highlight = -1;
    m_selected = -1;
    setMouseTracking(true);
    Flickable::setAcceptMouseClick(this);
    }

    void SwipeWidgetDlg::keyReleaseEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Down) {
    qWarning("KEY DOWN");
    m_offset += 20;
    event->accept();
    update();
    return;
    }
    if (event->key() == Qt::Key_Up) {
    qWarning("KEY UP");
    m_offset -= 20;
    event->accept();
    update();
    return;
    }
    }

    void SwipeWidgetDlg::mousePressEvent(QMouseEvent *event) {
    qWarning("In SwipeWidgetDlg::mousePressEvent");
    Flickable::handleMousePress(event);
    if (event->isAccepted())
    return;

    if (event->button() == Qt::LeftButton) {
    m_mouseDown = event->pos();
    int y = event->pos().y() + m_offset;
    int i = y / m_height;
    if (i != m_highlight) {
    m_highlight = i;
    m_selected = -1;
    update();
    }
    event->accept();
    }
    }

    void SwipeWidgetDlg::mouseMoveEvent(QMouseEvent *event) {
    Flickable::handleMouseMove(event);
    }

    void SwipeWidgetDlg::mouseReleaseEvent(QMouseEvent *event) {
    int delta = m_mouseDown.y() - event->pos().y();
    qWarning("In mouserelease: delta = %d", delta);
    if (abs(delta) < 10)
    {
    int X = event->x();
    int Y = event->y();

    QWidget *elem = childAt(X,Y);
    if(elem)
    {
    iTy = m_initTopPos;
    iBy = m_initBotPos;
    emit widgetClicked(elem);
    }
    }
    else
    {
    Flickable::handleMouseRelease(event);
    if (event->isAccepted())
    return;

    if (event->button() == Qt::LeftButton) {
    m_selected = m_highlight;
    event->accept();
    update();
    }
    }
    }

    QPoint SwipeWidgetDlg::scrollOffset() const {
    return QPoint(0, m_offset);
    }

    bool SwipeWidgetDlg::setScrollOffset(const QPoint &offset) {
    int yy = offset.y();
    qWarning("In SwipeWidgetDlg::setScrollOffset");
    if (iTy + yy >= 0 && iBy + yy <= geometry().height()) {
    iTy += yy;
    iBy += yy;
    qWarning("Scroll by %d", -yy);
    scroll(0, -yy);
    return true;
    }
    return false;
    }*/

    SwipeWidgetDlg::SwipeWidgetDlg(QWidget *parent) :
    QWidget(parent)
    {
    iTy = 0;
    iBy = 0;
    m_initTopPos = 0;
    m_initBotPos = 0;
    m_height = 0;
    m_totalHeight = 0;
    m_mouseDown = QPoint(0, 0);
    m_mouseMove = QPoint(0, 0);
    m_clickStatus = true;
    iM = 2;
    }

    bool SwipeWidgetDlg::eventFilter(QObject *obj, QEvent *event)
    {
    QMouseEvent *e;

    if (event->type() == QEvent::MouseMove)
    {
    e = static_cast<QMouseEvent*>(event);
    this->mouseMoveEvent(e);
    }
    else if(event->type() == QEvent::MouseButtonPress)
    {
    e = static_cast<QMouseEvent*>(event);
    this->mousePressEvent(e);
    }
    else if(event->type() == QEvent::MouseButtonRelease)
    {
    e = static_cast<QMouseEvent*>(event);
    this->mouseReleaseEvent(e);
    }

    return QObject::eventFilter(obj, event);
    }

    void SwipeWidgetDlg::mousePressEvent(QMouseEvent *e)
    {
    qWarning("Mouse Press Event\n");
    m_mouseDown = e->pos();
    m_mouseMove = e->pos();
    }

    void SwipeWidgetDlg::mouseMoveEvent (QMouseEvent *e)
    {
    QPointF mouseMove = e->pos();
    int delta = (m_mouseMove.y() - mouseMove.y())*iM;
    qWarning("Mouse Move(%d, %d): iTy = %d, iBy = %d, delta = %d, total height = %d", e->pos().x(), e->pos().y(), iTy, iBy, delta, m_totalHeight);
    if (iTy + delta >= 0 && iBy + delta <= m_totalHeight)
    {
    m_mouseMove.setX(mouseMove.x());
    m_mouseMove.setY(mouseMove.y());
    qWarning("About scroll\n");
    scroll(0, -delta);
    iTy += delta;
    iBy += delta;
    }
    }

    void SwipeWidgetDlg::mouseReleaseEvent(QMouseEvent *e)
    {
    qWarning("Mouse Release Event\n");
    if(m_clickStatus)
    {
    int delta = m_mouseDown.y() - e->pos().y();
    if (abs(delta) < 10)
    {
    int X = e->x();
    int Y = e->y();

    QWidget *elem = childAt(X,Y);
    if(elem)
    {
    //iTy = m_initTopPos;
    //iBy = m_initBotPos;
    emit widgetClicked(elem);
    }
    }
    }
    }

    void SwipeWidgetDlg::setInitialPosition(int topPos, int botPos)
    {
    iTy = topPos;
    m_initTopPos = topPos;
    iBy = botPos;
    m_initBotPos = botPos;
    }

    void SwipeWidgetDlg::setWidgetHeight(int widgetHeight)
    {
    qWarning("setWidgetHeight: %d\n", widgetHeight);
    m_height = widgetHeight;
    }

    void SwipeWidgetDlg::setTotalHeight(int totalHeight)
    {
    qWarning("setTotalHeight: %d\n", totalHeight);
    m_totalHeight = totalHeight;
    }

    void SwipeWidgetDlg::setClickStatus(bool aClickStatus)
    {
    m_clickStatus = aClickStatus;
    }



    I have installed the SwipeWidgetDlg object "widget" as filter for all the controls that use mouse events (buttons, etc). so that the SwipeWidgetDlg gets the mouse events.

    Kindly help as to what is wrong with code and what should I do?



    thanx and regards


    Zia

  2. #2
    Registered User
    Join Date
    Mar 2010
    Location
    Mumbai, India
    Posts
    32

    Re: Jerky Swipe

    The qWarnings emitted by SwipeWidgetDlg when the mouse is moved is:

    Mouse Press Event

    Mouse Move(88, 15): iTy = 0, iBy = 374, delta = 1, total height = 630
    Mouse Move(91, 15): iTy = 1, iBy = 375, delta = 0, total height = 630
    Mouse Move(96, 7): iTy = 1, iBy = 375, delta = 8, total height = 630
    Mouse Move(97, 12): iTy = 9, iBy = 383, delta = -5, total height = 630 <---------------
    Mouse Move(102, -7): iTy = 4, iBy = 378, delta = 19, total height = 630
    Mouse Move(104, 4): iTy = 23, iBy = 397, delta = -11, total height = 630<---------------
    Mouse Move(104, 1): iTy = 12, iBy = 386, delta = 3, total height = 630
    Mouse Move(104, -10): iTy = 15, iBy = 389, delta = 11, total height = 630
    Mouse Move(104, -20): iTy = 26, iBy = 400, delta = 10, total height = 630
    Mouse Move(104, 256): iTy = 36, iBy = 410, delta = -276, total height = 630<--------------------
    Mouse Move(104, -29): iTy = 36, iBy = 410, delta = 9, total height = 630
    Mouse Move(104, 238): iTy = 45, iBy = 419, delta = -267, total height = 630<-------------------
    Mouse Move(104, -3): iTy = 45, iBy = 419, delta = -26, total height = 630
    Mouse Move(104, 290): iTy = 19, iBy = 393, delta = -293, total height = 630<--------------------
    Mouse Move(104, -12): iTy = 19, iBy = 393, delta = 9, total height = 630
    Mouse Move(104, 272): iTy = 28, iBy = 402, delta = -284, total height = 630<-----------------------
    Mouse Move(104, -16): iTy = 28, iBy = 402, delta = 4, total height = 630
    Mouse Move(104, 264): iTy = 32, iBy = 406, delta = -280, total height = 630<---------------------
    Mouse Move(105, -39): iTy = 32, iBy = 406, delta = 23, total height = 630
    Mouse Move(105, 218): iTy = 55, iBy = 429, delta = -257, total height = 630<-----------------------

    In the above output i feel the line marked "<--------------" are the ones causing problem. But why should the events(lines) marked "<--------------" should happen? for eg, from the above:

    Mouse Move(88, 15): iTy = 0, iBy = 374, delta = 1, total height = 630
    Mouse Move(91, 15): iTy = 1, iBy = 375, delta = 0, total height = 630
    Mouse Move(96, 7): iTy = 1, iBy = 375, delta = 8, total height = 630
    Mouse Move(97, 12): iTy = 9, iBy = 383, delta = -5, total height = 630 <--------- this would cause scroll in opposite direction.
    Mouse Move(102, -7): iTy = 4, iBy = 378, delta = 19, total height = 630

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Jerky Swipe

    This is pretty much as good as it gets. You can play with the algorithm to smooth it out, but you're not going to get it to work like an iPhone.

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Rome
    Posts
    268

    Re: Jerky Swipe

    have a good debug ... after you fix it try playing with QTimeLine animation to add some cool kinetic effect
    Keep it simple, stupid
    borg - http://store.ovi.com/content/116105

Similar Threads

  1. detecting left-right swipe in WRT?
    By InYourDreamz in forum Symbian
    Replies: 2
    Last Post: 2010-01-20, 14:51
  2. Playing mp4 jerky
    By bhaskarmallu in forum Open C/C++
    Replies: 1
    Last Post: 2008-10-27, 08:59
  3. N70 3gp playback jerky
    By vpcg in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2008-02-12, 11:40
  4. 6630 Jerky Games
    By davepoo in forum Mobile Java General
    Replies: 16
    Last Post: 2005-12-14, 17:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×