Skip to content

Commit

Permalink
improved mouse handling
Browse files Browse the repository at this point in the history
git-svn-id: https://lmms.svn.sf.net/svnroot/lmms/trunk/lmms@55 0778d3d1-df1d-0410-868b-ea421aaaa00d
  • Loading branch information
brandmaier committed Jan 22, 2006
1 parent a0b7588 commit 7e6538d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
90 changes: 61 additions & 29 deletions plugins/bit_invader/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <qfontmetrics.h>
#include <qpainter.h>
#include <qcursor.h>

#endif

Expand All @@ -46,30 +47,35 @@

using namespace std;



graph::graph( const QString & _text, QWidget * _parent) :
QWidget( _parent )
{

m_background = NULL;
m_mouseDown = false;

setFixedSize( 132, 104 );




#ifndef QT4
setBackgroundMode( NoBackground );
#endif

}




graph::~graph()
{
// delete m_background;
}

void graph::setBackground( const QPixmap &_pixmap )
{
m_background = _pixmap;
setErasePixmap ( m_background );
// setErasePixmap ( m_background );

}

Expand All @@ -82,53 +88,77 @@ void graph::setSamplePointer( float * _pointer, int _length )
void graph::mouseMoveEvent ( QMouseEvent * _me )
{


// get position
int x = _me->x();
if (x < 0) { return; }
if (x > sampleLength) { return; }

int y = _me->y();
if (y < 0) { return; }
if (y >= 100) { return; }

y = 100 - y;

samplePointer[x] = (y-50.0)/50.0;
// avoid mouse leaps
int diff = x - m_lastCursorX;

if (diff >= 1) {
x = m_lastCursorX + 1;
} else if (diff <= 1) {
x = m_lastCursorX - 1;
} else {
x = m_lastCursorX;
}
// QCursor::setPos( 1, 1 );

emit sampleChanged();



changeSampleAt( x, y );

// update mouse
m_lastCursorX = x;

}

void graph::mousePressEvent( QMouseEvent * _me )
{
/* if( _me->button() == Qt::LeftButton )
{
toggle();
}*/

// toggle mouse state
m_mouseDown = true;

// get position
int x = _me->x();
if (x < 0) { return; }
if (x > sampleLength) { return; }

int y = _me->y();
if (y < 0) { return; }
if (y >= 100) { return; }

y = 100 - y;
changeSampleAt( x,y );

samplePointer[x] = (y-50.0)/50.0;
// toggle mouse state
m_mouseDown = true;
// setCursor( QCursor::BlankCursor );
m_lastCursorX = x;
}

void graph::changeSampleAt(int _x, int _y)
{
// consider border of background image
_x -= 2;
_y -= 2;

// boundary check
if (_x < 0) { return; }
if (_x > sampleLength) { return; }
if (_y < 0) { return; }
if (_y >= 100) { return; }
_y = 100 - _y;

// change sample shape
samplePointer[_x] = (_y-50.0)/50.0;
emit sampleChanged();


}

void graph::mouseReleaseEvent( QMouseEvent * _me )
{
// toggle mouse state
m_mouseDown = false;
setCursor( QCursor::ArrowCursor );
}


void sampleChanged() {}


void graph::paintEvent( QPaintEvent * )
Expand All @@ -138,7 +168,7 @@ void graph::paintEvent( QPaintEvent * )
QPainter p( this );
#else
QPixmap draw_pm( rect().size() );
draw_pm.fill( this, rect().topLeft() );
// draw_pm.fill( this, rect().topLeft() );

QPainter p( &draw_pm, this );
#endif
Expand All @@ -163,6 +193,8 @@ void graph::paintEvent( QPaintEvent * )
);
}

// draw Pointer
// mapFromGlobal( QCursor::pos() );

#ifndef QT4
// and blit all the drawn stuff on the screen...
Expand Down
9 changes: 9 additions & 0 deletions plugins/bit_invader/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@

#include <QWidget>
#include <QPixmap>
#include <QCursor>

#else

#include <qwidget.h>
#include <qpixmap.h>
#include <qcursor.h>
#endif


Expand Down Expand Up @@ -70,13 +72,20 @@ class graph : public QWidget
virtual void paintEvent( QPaintEvent * _pe );
virtual void mousePressEvent( QMouseEvent * _me );
virtual void mouseMoveEvent( QMouseEvent * _me );
virtual void mouseReleaseEvent( QMouseEvent * _me );

private:

void changeSampleAt(int _x, int _y);

QPixmap m_background;


float *samplePointer;
int sampleLength;

bool m_mouseDown;
int m_lastCursorX;

//signals:
// void toggled( bool );
Expand Down

0 comments on commit 7e6538d

Please sign in to comment.