예제 #1
0
/*!
  Handle a wheel event for the observed widget.

  Move the last point of the selection in case of isActive() == true

  \param wheelEvent Wheel event

  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
void QwtPicker::widgetWheelEvent( QWheelEvent *wheelEvent )
{
    if ( pickArea().contains( wheelEvent->pos() ) )
        d_data->trackerPosition = wheelEvent->pos();
    else
        d_data->trackerPosition = QPoint( -1, -1 );

    updateDisplay();

    transition( wheelEvent );
}
예제 #2
0
/*!
  Handle a mouse move event for the observed widget.

  \param mouseEvent Mouse event

  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
void QwtPicker::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
{
    if ( pickArea().contains( mouseEvent->pos() ) )
        d_data->trackerPosition = mouseEvent->pos();
    else
        d_data->trackerPosition = QPoint( -1, -1 );

    if ( !isActive() )
        updateDisplay();

    transition( mouseEvent );
}
예제 #3
0
/*!
  Handle a key press event for the observed widget.

  Selections can be completely done by the keyboard. The arrow keys
  move the cursor, the abort key aborts a selection. All other keys
  are handled by the current state machine.

  \param keyEvent Key event

  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetWheelEvent(), widgetKeyReleaseEvent(), stateMachine(),
      QwtEventPattern::KeyPatternCode
*/
void QwtPicker::widgetKeyPressEvent( QKeyEvent *keyEvent )
{
    int dx = 0;
    int dy = 0;

    int offset = 1;
    if ( keyEvent->isAutoRepeat() )
        offset = 5;

    if ( keyMatch( KeyLeft, keyEvent ) )
        dx = -offset;
    else if ( keyMatch( KeyRight, keyEvent ) )
        dx = offset;
    else if ( keyMatch( KeyUp, keyEvent ) )
        dy = -offset;
    else if ( keyMatch( KeyDown, keyEvent ) )
        dy = offset;
    else if ( keyMatch( KeyAbort, keyEvent ) )
    {
        reset();
    }
    else
        transition( keyEvent );

    if ( dx != 0 || dy != 0 )
    {
        const QRect rect = pickArea().boundingRect().toRect();
        const QPoint pos = parentWidget()->mapFromGlobal( QCursor::pos() );

        int x = pos.x() + dx;
        x = qMax( rect.left(), x );
        x = qMin( rect.right(), x );

        int y = pos.y() + dy;
        y = qMax( rect.top(), y );
        y = qMin( rect.bottom(), y );

        QCursor::setPos( parentWidget()->mapToGlobal( QPoint( x, y ) ) );
    }
}
예제 #4
0
void AdvPlotZoomer::drawRubberBand( QPainter *painter ) const
{
    if ( !isActive() || rubberBandPen().style() == Qt::NoPen )
        return;

    QwtPickerMachine::SelectionType selectionType =
        QwtPickerMachine::NoSelection;
    if ( stateMachine() )
        selectionType = stateMachine()->selectionType();

    if ( d_zoomConstrain == NoConstrain || rubberBand() != RectRubberBand ) {
        QwtPlotZoomer::drawRubberBand(painter);
        return;
    }
    const QRect &pRect = pickArea().boundingRect().toRect();
    const QPolygon &pa = pickedPoints();
    const int npa      = pa.count();

    switch ( d_zoomConstrain ) {
    case HZoomOnly:
        if ( npa >= 1 )
            QwtPainter::drawLine(painter, pa[0].x(), pRect.top(), pa[0].x(),
                pRect.bottom());
        if ( npa >= 2 )
            QwtPainter::drawLine(painter, pa[1].x(), pRect.top(), pa[1].x(),
                pRect.bottom());
        break;
    case VZoomOnly:
        if ( npa >= 1 )
            QwtPainter::drawLine(painter, pRect.left(), pa[0].y(),
                pRect.right(), pa[0].y());
        if ( npa >= 2 )
            QwtPainter::drawLine(painter, pRect.left(), pa[1].y(),
                pRect.right(), pa[1].y());
        break;
    default:
        // Nothing to be done here
        break;
    }
}
예제 #5
0
/*!
   Calculate the bounding rectangle for the tracker text
   from the current position of the tracker

   \param font Font of the tracker text
   \return Bounding rectangle of the tracker text

   \sa trackerPosition()
*/
QRect QwtPicker::trackerRect( const QFont &font ) const
{
    if ( trackerMode() == AlwaysOff ||
        ( trackerMode() == ActiveOnly && !isActive() ) )
    {
        return QRect();
    }

    if ( d_data->trackerPosition.x() < 0 || d_data->trackerPosition.y() < 0 )
        return QRect();

    QwtText text = trackerText( d_data->trackerPosition );
    if ( text.isEmpty() )
        return QRect();

    const QSizeF textSize = text.textSize( font );
    QRect textRect( 0, 0, qCeil( textSize.width() ), qCeil( textSize.height() ) );

    const QPoint &pos = d_data->trackerPosition;

    int alignment = 0;
    if ( isActive() && d_data->pickedPoints.count() > 1
        && rubberBand() != NoRubberBand )
    {
        const QPoint last =
            d_data->pickedPoints[int( d_data->pickedPoints.count() ) - 2];

        alignment |= ( pos.x() >= last.x() ) ? Qt::AlignRight : Qt::AlignLeft;
        alignment |= ( pos.y() > last.y() ) ? Qt::AlignBottom : Qt::AlignTop;
    }
    else
        alignment = Qt::AlignTop | Qt::AlignRight;

    const int margin = 5;

    int x = pos.x();
    if ( alignment & Qt::AlignLeft )
        x -= textRect.width() + margin;
    else if ( alignment & Qt::AlignRight )
        x += margin;

    int y = pos.y();
    if ( alignment & Qt::AlignBottom )
        y += margin;
    else if ( alignment & Qt::AlignTop )
        y -= textRect.height() + margin;

    textRect.moveTopLeft( QPoint( x, y ) );

    const QRect pickRect = pickArea().boundingRect().toRect();

    int right = qMin( textRect.right(), pickRect.right() - margin );
    int bottom = qMin( textRect.bottom(), pickRect.bottom() - margin );
    textRect.moveBottomRight( QPoint( right, bottom ) );

    int left = qMax( textRect.left(), pickRect.left() + margin );
    int top = qMax( textRect.top(), pickRect.top() + margin );
    textRect.moveTopLeft( QPoint( left, top ) );

    return textRect;
}
예제 #6
0
void QwtPicker::drawRubberBand( QPainter *painter ) const
{
    if ( !isActive() || rubberBand() == NoRubberBand ||
        rubberBandPen().style() == Qt::NoPen )
    {
        return;
    }

    const QPolygon pa = adjustedPoints( d_data->pickedPoints );

    QwtPickerMachine::SelectionType selectionType =
        QwtPickerMachine::NoSelection;

    if ( d_data->stateMachine )
        selectionType = d_data->stateMachine->selectionType();

    switch ( selectionType )
    {
        case QwtPickerMachine::NoSelection:
        case QwtPickerMachine::PointSelection:
        {
            if ( pa.count() < 1 )
                return;

            const QPoint pos = pa[0];

            const QRect pRect = pickArea().boundingRect().toRect();
            switch ( rubberBand() )
            {
                case VLineRubberBand:
                {
                    QwtPainter::drawLine( painter, pos.x(),
                        pRect.top(), pos.x(), pRect.bottom() );
                    break;
                }
                case HLineRubberBand:
                {
                    QwtPainter::drawLine( painter, pRect.left(),
                        pos.y(), pRect.right(), pos.y() );
                    break;
                }
                case CrossRubberBand:
                {
                    QwtPainter::drawLine( painter, pos.x(),
                        pRect.top(), pos.x(), pRect.bottom() );
                    QwtPainter::drawLine( painter, pRect.left(),
                        pos.y(), pRect.right(), pos.y() );
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case QwtPickerMachine::RectSelection:
        {
            if ( pa.count() < 2 )
                return;

            const QRect rect = QRect( pa.first(), pa.last() ).normalized();
            switch ( rubberBand() )
            {
                case EllipseRubberBand:
                {
                    QwtPainter::drawEllipse( painter, rect );
                    break;
                }
                case RectRubberBand:
                {
                    QwtPainter::drawRect( painter, rect );
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case QwtPickerMachine::PolygonSelection:
        {
            if ( rubberBand() == PolygonRubberBand )
                painter->drawPolyline( pa );
            break;
        }
        default:
            break;
    }
}
예제 #7
0
/*!
  Calculate the mask for the rubber band overlay

  \return Region for the mask
  \sa QWidget::setMask()
 */
QRegion QwtPicker::rubberBandMask() const
{
    QRegion mask;

    if ( !isActive() || rubberBand() == NoRubberBand ||
        rubberBandPen().style() == Qt::NoPen )
    {
        return mask;
    }

    const QPolygon pa = adjustedPoints( d_data->pickedPoints );

    QwtPickerMachine::SelectionType selectionType =
        QwtPickerMachine::NoSelection;

    if ( d_data->stateMachine )
        selectionType = d_data->stateMachine->selectionType();

    switch ( selectionType )
    {
        case QwtPickerMachine::NoSelection:
        case QwtPickerMachine::PointSelection:
        {
            if ( pa.count() < 1 )
                return mask;

            const QPoint pos = pa[0];
            const int pw = rubberBandPen().width();

            const QRect pRect = pickArea().boundingRect().toRect();
            switch ( rubberBand() )
            {
                case VLineRubberBand:
                {
                    mask += qwtMaskRegion( QLine( pos.x(), pRect.top(),
                        pos.x(), pRect.bottom() ), pw );
                    break;
                }
                case HLineRubberBand:
                {
                    mask += qwtMaskRegion( QLine( pRect.left(), pos.y(),
                        pRect.right(), pos.y() ), pw );
                    break;
                }
                case CrossRubberBand:
                {
                    mask += qwtMaskRegion( QLine( pos.x(), pRect.top(),
                        pos.x(), pRect.bottom() ), pw );
                    mask += qwtMaskRegion( QLine( pRect.left(), pos.y(),
                        pRect.right(), pos.y() ), pw );
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case QwtPickerMachine::RectSelection:
        {
            if ( pa.count() < 2 )
                return mask;

            const int pw = rubberBandPen().width();

            switch ( rubberBand() )
            {
                case RectRubberBand:
                {
                    const QRect r = QRect( pa.first(), pa.last() );
                    mask = qwtMaskRegion( r.normalized(), pw );
                    break;
                }
                case EllipseRubberBand:
                {
                    const QRect r = QRect( pa.first(), pa.last() );
                    mask += r.adjusted( -pw, -pw, pw, pw );
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case QwtPickerMachine::PolygonSelection:
        {
            const int pw = rubberBandPen().width();
            if ( pw <= 1 )
            {
                // because of the join style we better
                // return a mask for a pen width <= 1 only

                const int off = 2 * pw;
                const QRect r = pa.boundingRect();
                mask += r.adjusted( -off, -off, off, off );
            }
            break;
        }
        default:
            break;
    }

    return mask;
}