gibi70
26th January 2013, 21:58
I need to put a number of always-visible pickers on a canvas. This is the code I have devised so far:
class QwtPickerStayMachine : public QwtPickerMachine
{
public:
QwtPickerStayMachine():
QwtPickerMachine( PointSelection )
{
// 0 = button not pressed
// 1 = button pressed
setState(0);
}
QList<QwtPickerMachine::Command> transition(
const QwtEventPattern &eventPattern, const QEvent *e )
{
QList<QwtPickerMachine::Command> cmdList;
switch ( e->type() )
{
case QEvent::MouseButtonPress:
{
if ( eventPattern.mouseMatch(
QwtEventPattern::MouseSelect1, ( const QMouseEvent * )e ) )
{
if ( state() == 0 )
{
cmdList += End;
cmdList += Begin;
cmdList += Append;
setState(1);
}
else
{
//cmdList += Move;
}
//qDebug() << "machine mousebuttonpress state" << state() ;
}
break;
}
case QEvent::MouseMove:
case QEvent::Wheel:
{
if ( state() == 0 )
{
}
else
{
cmdList += Move;
}
//qDebug() << "machine mousemove state" << state();
break;
}
case QEvent::MouseButtonRelease:
{
//setState(0);
if ( state() == 0 )
{
}
else
{
//cmdList += End;
setState(0);
}
//qDebug() << "machine mousebuttonrelease state" << state();
break;
}
case QEvent::Leave:
{
//setState(0);
//qDebug() << "machine leave state" << state();
}
case QEvent::KeyPress:
{
if ( eventPattern.keyMatch(
QwtEventPattern::KeySelect1, ( const QKeyEvent * )e ) )
{
if ( state() == 0 )
{
cmdList += Begin;
cmdList += Append;
setState( 1 );
}
else
{
//cmdList += End;
//setState( 0 );
}
}
break;
}
default:
break;
}
return cmdList;
}
};
It seems to work and the rubberband stays visible. I instantiate the picker in its constructor like in the following code
Picker(QwtPlotCanvas *canvas, QColor col)
: QwtPicker(canvas)
{
//setStateMachine( new QwtPickerDragPointMachine() );
setStateMachine( new QwtPickerStayMachine() );
setRubberBandPen(QPen(QBrush(col), 3));
//setRubberBand( QwtPicker::NoRubberBand );
setRubberBand( QwtPicker::VLineRubberBand );
setTrackerMode(QwtPicker::AlwaysOn);
setTrackerPen( QColor( Qt::blue ) );
connect(this, SIGNAL(moved(QPoint)), this, SLOT(onPickerDragged(QPoint)));
}
The first type of problem I am facing is that the signal moved() does not seem to be emitted, as the code on the connected slot is never called.
The second problem is that when I associate two pickers to the canvas, they move synchronously one over another, when actually I would like to move just one of them (the one nearest to the point where I click).
Thanks for any advice.
class QwtPickerStayMachine : public QwtPickerMachine
{
public:
QwtPickerStayMachine():
QwtPickerMachine( PointSelection )
{
// 0 = button not pressed
// 1 = button pressed
setState(0);
}
QList<QwtPickerMachine::Command> transition(
const QwtEventPattern &eventPattern, const QEvent *e )
{
QList<QwtPickerMachine::Command> cmdList;
switch ( e->type() )
{
case QEvent::MouseButtonPress:
{
if ( eventPattern.mouseMatch(
QwtEventPattern::MouseSelect1, ( const QMouseEvent * )e ) )
{
if ( state() == 0 )
{
cmdList += End;
cmdList += Begin;
cmdList += Append;
setState(1);
}
else
{
//cmdList += Move;
}
//qDebug() << "machine mousebuttonpress state" << state() ;
}
break;
}
case QEvent::MouseMove:
case QEvent::Wheel:
{
if ( state() == 0 )
{
}
else
{
cmdList += Move;
}
//qDebug() << "machine mousemove state" << state();
break;
}
case QEvent::MouseButtonRelease:
{
//setState(0);
if ( state() == 0 )
{
}
else
{
//cmdList += End;
setState(0);
}
//qDebug() << "machine mousebuttonrelease state" << state();
break;
}
case QEvent::Leave:
{
//setState(0);
//qDebug() << "machine leave state" << state();
}
case QEvent::KeyPress:
{
if ( eventPattern.keyMatch(
QwtEventPattern::KeySelect1, ( const QKeyEvent * )e ) )
{
if ( state() == 0 )
{
cmdList += Begin;
cmdList += Append;
setState( 1 );
}
else
{
//cmdList += End;
//setState( 0 );
}
}
break;
}
default:
break;
}
return cmdList;
}
};
It seems to work and the rubberband stays visible. I instantiate the picker in its constructor like in the following code
Picker(QwtPlotCanvas *canvas, QColor col)
: QwtPicker(canvas)
{
//setStateMachine( new QwtPickerDragPointMachine() );
setStateMachine( new QwtPickerStayMachine() );
setRubberBandPen(QPen(QBrush(col), 3));
//setRubberBand( QwtPicker::NoRubberBand );
setRubberBand( QwtPicker::VLineRubberBand );
setTrackerMode(QwtPicker::AlwaysOn);
setTrackerPen( QColor( Qt::blue ) );
connect(this, SIGNAL(moved(QPoint)), this, SLOT(onPickerDragged(QPoint)));
}
The first type of problem I am facing is that the signal moved() does not seem to be emitted, as the code on the connected slot is never called.
The second problem is that when I associate two pickers to the canvas, they move synchronously one over another, when actually I would like to move just one of them (the one nearest to the point where I click).
Thanks for any advice.