Пример #1
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 ) );

    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;
}
Пример #2
0
/*!
  Handle a wheel event for the observed widget.

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

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

    updateDisplay();

    transition(e);
}
Пример #3
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 ( pickRect().contains( wheelEvent->pos() ) )
        d_data->trackerPosition = wheelEvent->pos();
    else
        d_data->trackerPosition = QPoint( -1, -1 );

    updateDisplay();

    transition( wheelEvent );
}
Пример #4
0
/*!
  Handle a mouse move event for the observed widget.

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

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

    if ( !isActive() )
        updateDisplay();

    transition(e);
}
Пример #5
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 ( pickRect().contains( mouseEvent->pos() ) )
        d_data->trackerPosition = mouseEvent->pos();
    else
        d_data->trackerPosition = QPoint( -1, -1 );

    if ( !isActive() )
        updateDisplay();

    transition( mouseEvent );
}
Пример #6
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.

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

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

    if ( keyMatch(KeyLeft, ke) )
        dx = -offset;
    else if ( keyMatch(KeyRight, ke) )
        dx = offset;
    else if ( keyMatch(KeyUp, ke) )
        dy = -offset;
    else if ( keyMatch(KeyDown, ke) )
        dy = offset;
    else if ( keyMatch(KeyAbort, ke) )
    {
        if ( d_data->stateMachine )
            d_data->stateMachine->reset();

        if (isActive())
            end(false);
    }
    else
        transition(ke);

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

        int x = pos.x() + dx;
        x = qwtMax(rect.left(), x);
        x = qwtMin(rect.right(), x);

        int y = pos.y() + dy;
        y = qwtMax(rect.top(), y);
        y = qwtMin(rect.bottom(), y);

        QCursor::setPos(parentWidget()->mapToGlobal(QPoint(x, y)));
    }
}
Пример #7
0
void QwtPicker::drawRubberBand(QPainter *painter) const
{
    if ( !isActive() || rubberBand() == NoRubberBand || 
        rubberBandPen().style() == Qt::NoPen )
    {
        return;
    }

    const QRect &pRect = pickRect();
    const SelectedPoints &pa = d_data->selection;

    if ( selectionFlags() & PointSelection )
    {
        if ( pa.count() < 1 )
            return;

        const QPoint pos = pa[0];

        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;
        }
    }

    else if ( selectionFlags() & RectSelection )
    {
        if ( pa.count() < 2 )
            return;

        QPoint p1 = pa[0];
        QPoint p2 = pa[int(pa.count() - 1)];

        if ( selectionFlags() & CenterToCorner )
        {
            p1.setX(p1.x() - (p2.x() - p1.x()));
            p1.setY(p1.y() - (p2.y() - p1.y()));
        }
        else if ( selectionFlags() & CenterToRadius )
        {
            const int radius = qwtMax(qwtAbs(p2.x() - p1.x()), 
                qwtAbs(p2.y() - p1.y()));
            p2.setX(p1.x() + radius);
            p2.setY(p1.y() + radius);
            p1.setX(p1.x() - radius);
            p1.setY(p1.y() - radius);
        }

#if QT_VERSION < 0x040000
        const QRect rect = QRect(p1, p2).normalize();
#else
        const QRect rect = QRect(p1, p2).normalized();
#endif
        switch(rubberBand())
        {
            case EllipseRubberBand:
                QwtPainter::drawEllipse(painter, rect);
                break;
            case RectRubberBand:
                QwtPainter::drawRect(painter, rect);
                break;
            default:
                break;
        }
    }
    else if ( selectionFlags() & PolygonSelection )
    {
        if ( rubberBand() == PolygonRubberBand )
            painter->drawPolyline(pa);
    }
}
Пример #8
0
void QwtPicker::drawRubberBand( QPainter *painter ) const
{
    if ( !isActive() || rubberBand() == NoRubberBand ||
        rubberBandPen().style() == Qt::NoPen )
    {
        return;
    }

    const QRect &pRect = pickRect();
    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];

            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 QPoint p1 = pa[0];
            const QPoint p2 = pa[int( pa.count() - 1 )];

            const QRect rect = QRect( p1, p2 ).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;
    }
}