qwt_picker.cpp 38.3 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
10 11 12 13 14
#include "qwt_picker.h"
#include "qwt_picker_machine.h"
#include "qwt_painter.h"
#include "qwt_math.h"
#include "qwt_widget_overlay.h"
pixhawk's avatar
pixhawk committed
15 16 17 18 19 20 21 22
#include <qapplication.h>
#include <qevent.h>
#include <qpainter.h>
#include <qframe.h>
#include <qcursor.h>
#include <qbitmap.h>
#include <qpointer.h>
#include <qpaintengine.h>
Bryant's avatar
Bryant committed
23
#include <qmath.h>
pixhawk's avatar
pixhawk committed
24

Bryant's avatar
Bryant committed
25
static inline QRegion qwtMaskRegion( const QRect &r, int penWidth )
pixhawk's avatar
pixhawk committed
26
{
Bryant's avatar
Bryant committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    const int pw = qMax( penWidth, 1 );
    const int pw2 = penWidth / 2;

    int x1 = r.left() - pw2;
    int x2 = r.right() + 1 + pw2 + ( pw % 2 );

    int y1 = r.top() - pw2;
    int y2 = r.bottom() + 1 + pw2 + ( pw % 2 );

    QRegion region;

    region += QRect( x1, y1, x2 - x1, pw );
    region += QRect( x1, y1, pw, y2 - y1 );
    region += QRect( x1, y2 - pw, x2 - x1, pw );
    region += QRect( x2 - pw, y1, pw, y2 - y1 );

    return region;
}

static inline QRegion qwtMaskRegion( const QLine &l, int penWidth )
{
    const int pw = qMax( penWidth, 1 );
    const int pw2 = penWidth / 2;

    QRegion region;

    if ( l.x1() == l.x2() )
    {
        region += QRect( l.x1() - pw2, l.y1(), 
            pw, l.y2() ).normalized();
    }
    else if ( l.y1() == l.y2() )
    {
        region += QRect( l.x1(), l.y1() - pw2, 
            l.x2(), pw ).normalized();
    }
pixhawk's avatar
pixhawk committed
63

Bryant's avatar
Bryant committed
64 65
    return region;
}
pixhawk's avatar
pixhawk committed
66

Bryant's avatar
Bryant committed
67 68 69 70
class QwtPickerRubberband: public QwtWidgetOverlay
{
public:
    QwtPickerRubberband( QwtPicker *, QWidget * );
pixhawk's avatar
pixhawk committed
71 72

protected:
Bryant's avatar
Bryant committed
73 74
    virtual void drawOverlay( QPainter * ) const;
    virtual QRegion maskHint() const;
pixhawk's avatar
pixhawk committed
75 76 77 78

    QwtPicker *d_picker;
};

Bryant's avatar
Bryant committed
79 80 81 82 83 84 85 86 87 88 89 90 91
class QwtPickerTracker: public QwtWidgetOverlay
{                                  
public:
    QwtPickerTracker( QwtPicker *, QWidget * );
    
protected:
    virtual void drawOverlay( QPainter * ) const;
    virtual QRegion maskHint() const;
    
    QwtPicker *d_picker;
};  


pixhawk's avatar
pixhawk committed
92 93 94
class QwtPicker::PrivateData
{
public:
Bryant's avatar
Bryant committed
95 96 97 98 99 100 101 102 103 104 105 106 107
    PrivateData():
        enabled( false ),
        stateMachine( NULL ),
        resizeMode( QwtPicker::Stretch ),
        rubberBand( QwtPicker::NoRubberBand ),
        trackerMode( QwtPicker::AlwaysOff ),
        isActive( false ),
        trackerPosition( -1, -1 ),
        mouseTracking( false ),
        openGL( false )
    {
    }
        
pixhawk's avatar
pixhawk committed
108 109 110 111 112 113 114 115 116 117 118 119 120
    bool enabled;

    QwtPickerMachine *stateMachine;

    QwtPicker::ResizeMode resizeMode;

    QwtPicker::RubberBand rubberBand;
    QPen rubberBandPen;

    QwtPicker::DisplayMode trackerMode;
    QPen trackerPen;
    QFont trackerFont;

Bryant's avatar
Bryant committed
121
    QPolygon pickedPoints;
pixhawk's avatar
pixhawk committed
122 123 124 125 126
    bool isActive;
    QPoint trackerPosition;

    bool mouseTracking; // used to save previous value

Bryant's avatar
Bryant committed
127 128 129 130
    QPointer< QwtPickerRubberband > rubberBandOverlay;
    QPointer< QwtPickerTracker> trackerOverlay;

    bool openGL;
pixhawk's avatar
pixhawk committed
131 132
};

Bryant's avatar
Bryant committed
133 134 135 136
QwtPickerRubberband::QwtPickerRubberband(
        QwtPicker *picker, QWidget *parent ):
    QwtWidgetOverlay( parent ),
    d_picker( picker )
pixhawk's avatar
pixhawk committed
137
{
Bryant's avatar
Bryant committed
138
    setMaskMode( QwtWidgetOverlay::MaskHint );
pixhawk's avatar
pixhawk committed
139 140
}

Bryant's avatar
Bryant committed
141
QRegion QwtPickerRubberband::maskHint() const
pixhawk's avatar
pixhawk committed
142
{
Bryant's avatar
Bryant committed
143 144
    return d_picker->rubberBandMask();
}
pixhawk's avatar
pixhawk committed
145

Bryant's avatar
Bryant committed
146 147 148 149
void QwtPickerRubberband::drawOverlay( QPainter *painter ) const
{
    painter->setPen( d_picker->rubberBandPen() );
    d_picker->drawRubberBand( painter );
pixhawk's avatar
pixhawk committed
150 151
}

Bryant's avatar
Bryant committed
152 153 154 155
QwtPickerTracker::QwtPickerTracker(
        QwtPicker *picker, QWidget *parent ):
    QwtWidgetOverlay( parent ),
    d_picker( picker )
pixhawk's avatar
pixhawk committed
156
{
Bryant's avatar
Bryant committed
157 158
    setMaskMode( QwtWidgetOverlay::MaskHint );
}
pixhawk's avatar
pixhawk committed
159

Bryant's avatar
Bryant committed
160 161 162 163
QRegion QwtPickerTracker::maskHint() const
{
    return d_picker->trackerRect( font() );
}
pixhawk's avatar
pixhawk committed
164

Bryant's avatar
Bryant committed
165 166 167 168
void QwtPickerTracker::drawOverlay( QPainter *painter ) const
{
    painter->setPen( d_picker->trackerPen() );
    d_picker->drawTracker( painter );
pixhawk's avatar
pixhawk committed
169 170 171 172 173
}

/*!
  Constructor

Bryant's avatar
Bryant committed
174 175
  Creates an picker that is enabled, but without a state machine.
  rubber band and tracker are disabled.
176

pixhawk's avatar
pixhawk committed
177 178 179
  \param parent Parent widget, that will be observed
 */

Bryant's avatar
Bryant committed
180 181
QwtPicker::QwtPicker( QWidget *parent ):
    QObject( parent )
pixhawk's avatar
pixhawk committed
182
{
Bryant's avatar
Bryant committed
183
    init( parent, NoRubberBand, AlwaysOff );
pixhawk's avatar
pixhawk committed
184 185 186 187 188
}

/*!
  Constructor

Bryant's avatar
Bryant committed
189
  \param rubberBand Rubber band style
pixhawk's avatar
pixhawk committed
190 191 192
  \param trackerMode Tracker mode
  \param parent Parent widget, that will be observed
 */
Bryant's avatar
Bryant committed
193 194 195
QwtPicker::QwtPicker( RubberBand rubberBand,
        DisplayMode trackerMode, QWidget *parent ):
    QObject( parent )
pixhawk's avatar
pixhawk committed
196
{
Bryant's avatar
Bryant committed
197
    init( parent, rubberBand, trackerMode );
pixhawk's avatar
pixhawk committed
198 199 200 201 202
}

//! Destructor
QwtPicker::~QwtPicker()
{
Bryant's avatar
Bryant committed
203 204
    setMouseTracking( false );

pixhawk's avatar
pixhawk committed
205
    delete d_data->stateMachine;
Bryant's avatar
Bryant committed
206 207 208
    delete d_data->rubberBandOverlay;
    delete d_data->trackerOverlay;

pixhawk's avatar
pixhawk committed
209 210 211
    delete d_data;
}

Bryant's avatar
Bryant committed
212 213 214
//! Initialize the picker - used by the constructors
void QwtPicker::init( QWidget *parent,
    RubberBand rubberBand, DisplayMode trackerMode )
pixhawk's avatar
pixhawk committed
215 216 217 218
{
    d_data = new PrivateData;

    d_data->rubberBand = rubberBand;
Bryant's avatar
Bryant committed
219 220 221

    if ( parent )
    {
pixhawk's avatar
pixhawk committed
222
        if ( parent->focusPolicy() == Qt::NoFocus )
Bryant's avatar
Bryant committed
223
            parent->setFocusPolicy( Qt::WheelFocus );
pixhawk's avatar
pixhawk committed
224

Bryant's avatar
Bryant committed
225
        d_data->openGL = parent->inherits( "QGLWidget" );
pixhawk's avatar
pixhawk committed
226 227
        d_data->trackerFont = parent->font();
        d_data->mouseTracking = parent->hasMouseTracking();
Bryant's avatar
Bryant committed
228 229

        setEnabled( true );
pixhawk's avatar
pixhawk committed
230
    }
Bryant's avatar
Bryant committed
231 232

    setTrackerMode( trackerMode );
pixhawk's avatar
pixhawk committed
233 234 235
}

/*!
Bryant's avatar
Bryant committed
236 237 238 239
  Set a state machine and delete the previous one

  \param stateMachine State machine
  \sa stateMachine()
pixhawk's avatar
pixhawk committed
240
*/
Bryant's avatar
Bryant committed
241
void QwtPicker::setStateMachine( QwtPickerMachine *stateMachine )
pixhawk's avatar
pixhawk committed
242
{
Bryant's avatar
Bryant committed
243 244
    if ( d_data->stateMachine != stateMachine )
    {
pixhawk's avatar
pixhawk committed
245 246 247 248 249 250 251 252 253 254 255
        reset();

        delete d_data->stateMachine;
        d_data->stateMachine = stateMachine;

        if ( d_data->stateMachine )
            d_data->stateMachine->reset();
    }
}

/*!
Bryant's avatar
Bryant committed
256 257
  \return Assigned state machine
  \sa setStateMachine()
pixhawk's avatar
pixhawk committed
258
*/
Bryant's avatar
Bryant committed
259
QwtPickerMachine *QwtPicker::stateMachine()
pixhawk's avatar
pixhawk committed
260
{
Bryant's avatar
Bryant committed
261 262 263 264 265 266 267 268 269 270
    return d_data->stateMachine;
}

/*!
  \return Assigned state machine
  \sa setStateMachine()
*/
const QwtPickerMachine *QwtPicker::stateMachine() const
{
    return d_data->stateMachine;
pixhawk's avatar
pixhawk committed
271 272 273 274 275 276 277
}

//! Return the parent widget, where the selection happens
QWidget *QwtPicker::parentWidget()
{
    QObject *obj = parent();
    if ( obj && obj->isWidgetType() )
Bryant's avatar
Bryant committed
278
        return static_cast<QWidget *>( obj );
pixhawk's avatar
pixhawk committed
279 280 281 282 283 284 285 286 287

    return NULL;
}

//! Return the parent widget, where the selection happens
const QWidget *QwtPicker::parentWidget() const
{
    QObject *obj = parent();
    if ( obj && obj->isWidgetType() )
Bryant's avatar
Bryant committed
288
        return static_cast< const QWidget *>( obj );
pixhawk's avatar
pixhawk committed
289 290 291 292 293

    return NULL;
}

/*!
Bryant's avatar
Bryant committed
294
  Set the rubber band style
pixhawk's avatar
pixhawk committed
295

Bryant's avatar
Bryant committed
296
  \param rubberBand Rubber band style
pixhawk's avatar
pixhawk committed
297 298 299 300
         The default value is NoRubberBand.

  \sa rubberBand(), RubberBand, setRubberBandPen()
*/
Bryant's avatar
Bryant committed
301
void QwtPicker::setRubberBand( RubberBand rubberBand )
pixhawk's avatar
pixhawk committed
302 303 304 305 306
{
    d_data->rubberBand = rubberBand;
}

/*!
Bryant's avatar
Bryant committed
307
  \return Rubber band style
pixhawk's avatar
pixhawk committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  \sa setRubberBand(), RubberBand, rubberBandPen()
*/
QwtPicker::RubberBand QwtPicker::rubberBand() const
{
    return d_data->rubberBand;
}

/*!
  \brief Set the display mode of the tracker.

  A tracker displays information about current position of
  the cursor as a string. The display mode controls
  if the tracker has to be displayed whenever the observed
  widget has focus and cursor (AlwaysOn), never (AlwaysOff), or
  only when the selection is active (ActiveOnly).
323

pixhawk's avatar
pixhawk committed
324 325 326 327 328 329 330
  \param mode Tracker display mode

  \warning In case of AlwaysOn, mouseTracking will be enabled
           for the observed widget.
  \sa trackerMode(), DisplayMode
*/

Bryant's avatar
Bryant committed
331
void QwtPicker::setTrackerMode( DisplayMode mode )
332
{
Bryant's avatar
Bryant committed
333 334
    if ( d_data->trackerMode != mode )
    {
pixhawk's avatar
pixhawk committed
335
        d_data->trackerMode = mode;
Bryant's avatar
Bryant committed
336
        setMouseTracking( d_data->trackerMode == AlwaysOn );
pixhawk's avatar
pixhawk committed
337
    }
338
}
pixhawk's avatar
pixhawk committed
339 340 341 342 343 344

/*!
  \return Tracker display mode
  \sa setTrackerMode(), DisplayMode
*/
QwtPicker::DisplayMode QwtPicker::trackerMode() const
345
{
pixhawk's avatar
pixhawk committed
346
    return d_data->trackerMode;
347
}
pixhawk's avatar
pixhawk committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

/*!
  \brief Set the resize mode.

  The resize mode controls what to do with the selected points of an active
  selection when the observed widget is resized.

  Stretch means the points are scaled according to the new
  size, KeepSize means the points remain unchanged.

  The default mode is Stretch.

  \param mode Resize mode
  \sa resizeMode(), ResizeMode
*/
Bryant's avatar
Bryant committed
363
void QwtPicker::setResizeMode( ResizeMode mode )
pixhawk's avatar
pixhawk committed
364 365
{
    d_data->resizeMode = mode;
366
}
pixhawk's avatar
pixhawk committed
367 368 369 370 371 372 373

/*!
  \return Resize mode
  \sa setResizeMode(), ResizeMode
*/

QwtPicker::ResizeMode QwtPicker::resizeMode() const
374
{
pixhawk's avatar
pixhawk committed
375 376 377 378 379 380 381 382 383 384 385 386
    return d_data->resizeMode;
}

/*!
  \brief En/disable the picker

  When enabled is true an event filter is installed for
  the observed widget, otherwise the event filter is removed.

  \param enabled true or false
  \sa isEnabled(), eventFilter()
*/
Bryant's avatar
Bryant committed
387
void QwtPicker::setEnabled( bool enabled )
pixhawk's avatar
pixhawk committed
388
{
Bryant's avatar
Bryant committed
389 390
    if ( d_data->enabled != enabled )
    {
pixhawk's avatar
pixhawk committed
391 392 393
        d_data->enabled = enabled;

        QWidget *w = parentWidget();
Bryant's avatar
Bryant committed
394 395
        if ( w )
        {
pixhawk's avatar
pixhawk committed
396
            if ( enabled )
Bryant's avatar
Bryant committed
397
                w->installEventFilter( this );
pixhawk's avatar
pixhawk committed
398
            else
Bryant's avatar
Bryant committed
399
                w->removeEventFilter( this );
pixhawk's avatar
pixhawk committed
400 401 402 403 404 405 406 407
        }

        updateDisplay();
    }
}

/*!
  \return true when enabled, false otherwise
Bryant's avatar
Bryant committed
408
  \sa setEnabled(), eventFilter()
pixhawk's avatar
pixhawk committed
409 410 411 412 413 414 415 416 417 418 419 420 421
*/

bool QwtPicker::isEnabled() const
{
    return d_data->enabled;
}

/*!
  Set the font for the tracker

  \param font Tracker font
  \sa trackerFont(), setTrackerMode(), setTrackerPen()
*/
Bryant's avatar
Bryant committed
422
void QwtPicker::setTrackerFont( const QFont &font )
pixhawk's avatar
pixhawk committed
423
{
Bryant's avatar
Bryant committed
424 425
    if ( font != d_data->trackerFont )
    {
pixhawk's avatar
pixhawk committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        d_data->trackerFont = font;
        updateDisplay();
    }
}

/*!
  \return Tracker font
  \sa setTrackerFont(), trackerMode(), trackerPen()
*/

QFont QwtPicker::trackerFont() const
{
    return d_data->trackerFont;
}

/*!
  Set the pen for the tracker

  \param pen Tracker pen
  \sa trackerPen(), setTrackerMode(), setTrackerFont()
*/
Bryant's avatar
Bryant committed
447
void QwtPicker::setTrackerPen( const QPen &pen )
pixhawk's avatar
pixhawk committed
448
{
Bryant's avatar
Bryant committed
449 450
    if ( pen != d_data->trackerPen )
    {
pixhawk's avatar
pixhawk committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        d_data->trackerPen = pen;
        updateDisplay();
    }
}

/*!
  \return Tracker pen
  \sa setTrackerPen(), trackerMode(), trackerFont()
*/
QPen QwtPicker::trackerPen() const
{
    return d_data->trackerPen;
}

/*!
  Set the pen for the rubberband

Bryant's avatar
Bryant committed
468
  \param pen Rubber band pen
pixhawk's avatar
pixhawk committed
469 470
  \sa rubberBandPen(), setRubberBand()
*/
Bryant's avatar
Bryant committed
471
void QwtPicker::setRubberBandPen( const QPen &pen )
pixhawk's avatar
pixhawk committed
472
{
Bryant's avatar
Bryant committed
473 474
    if ( pen != d_data->rubberBandPen )
    {
pixhawk's avatar
pixhawk committed
475 476 477 478 479 480
        d_data->rubberBandPen = pen;
        updateDisplay();
    }
}

/*!
Bryant's avatar
Bryant committed
481
  \return Rubber band pen
pixhawk's avatar
pixhawk committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  \sa setRubberBandPen(), rubberBand()
*/
QPen QwtPicker::rubberBandPen() const
{
    return d_data->rubberBandPen;
}

/*!
   \brief Return the label for a position

   In case of HLineRubberBand the label is the value of the
   y position, in case of VLineRubberBand the value of the x position.
   Otherwise the label contains x and y position separated by a ',' .

   The format for the string conversion is "%d".

   \param pos Position
   \return Converted position as string
*/

Bryant's avatar
Bryant committed
502
QwtText QwtPicker::trackerText( const QPoint &pos ) const
pixhawk's avatar
pixhawk committed
503 504 505
{
    QString label;

Bryant's avatar
Bryant committed
506 507 508 509 510 511 512 513 514 515
    switch ( rubberBand() )
    {
        case HLineRubberBand:
            label.sprintf( "%d", pos.y() );
            break;
        case VLineRubberBand:
            label.sprintf( "%d", pos.x() );
            break;
        default:
            label.sprintf( "%d, %d", pos.x(), pos.y() );
pixhawk's avatar
pixhawk committed
516 517 518 519 520
    }
    return label;
}

/*!
Bryant's avatar
Bryant committed
521
  Calculate the mask for the rubber band overlay
pixhawk's avatar
pixhawk committed
522

Bryant's avatar
Bryant committed
523 524 525 526
  \return Region for the mask
  \sa QWidget::setMask()
 */
QRegion QwtPicker::rubberBandMask() const
pixhawk's avatar
pixhawk committed
527
{
Bryant's avatar
Bryant committed
528 529
    QRegion mask;

530
    if ( !isActive() || rubberBand() == NoRubberBand ||
Bryant's avatar
Bryant committed
531 532 533
        rubberBandPen().style() == Qt::NoPen )
    {
        return mask;
pixhawk's avatar
pixhawk committed
534 535
    }

Bryant's avatar
Bryant committed
536
    const QPolygon pa = adjustedPoints( d_data->pickedPoints );
pixhawk's avatar
pixhawk committed
537

Bryant's avatar
Bryant committed
538 539
    QwtPickerMachine::SelectionType selectionType =
        QwtPickerMachine::NoSelection;
pixhawk's avatar
pixhawk committed
540

Bryant's avatar
Bryant committed
541 542
    if ( d_data->stateMachine )
        selectionType = d_data->stateMachine->selectionType();
pixhawk's avatar
pixhawk committed
543

Bryant's avatar
Bryant committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    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;
            }
581
            break;
Bryant's avatar
Bryant committed
582 583 584 585 586
        }
        case QwtPickerMachine::RectSelection:
        {
            if ( pa.count() < 2 )
                return mask;
587

Bryant's avatar
Bryant committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
            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;
            }
607
            break;
Bryant's avatar
Bryant committed
608 609 610 611 612 613 614 615
        }
        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
616

Bryant's avatar
Bryant committed
617 618 619 620
                const int off = 2 * pw;
                const QRect r = pa.boundingRect();
                mask += r.adjusted( -off, -off, off, off );
            }
621
            break;
Bryant's avatar
Bryant committed
622
        }
623 624
        default:
            break;
pixhawk's avatar
pixhawk committed
625 626
    }

Bryant's avatar
Bryant committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    return mask;
}

/*!
   Draw a rubber band, depending on rubberBand()

   \param painter Painter, initialized with a clip region

   \sa rubberBand(), RubberBand
*/

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;
pixhawk's avatar
pixhawk committed
691
        }
Bryant's avatar
Bryant committed
692 693 694 695
        case QwtPickerMachine::RectSelection:
        {
            if ( pa.count() < 2 )
                return;
pixhawk's avatar
pixhawk committed
696

Bryant's avatar
Bryant committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
            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;
            }
713
            break;
Bryant's avatar
Bryant committed
714 715 716 717 718
        }
        case QwtPickerMachine::PolygonSelection:
        {
            if ( rubberBand() == PolygonRubberBand )
                painter->drawPolyline( pa );
719
            break;
Bryant's avatar
Bryant committed
720
        }
721 722
        default:
            break;
pixhawk's avatar
pixhawk committed
723 724 725 726 727 728 729 730 731 732
    }
}

/*!
   Draw the tracker

   \param painter Painter
   \sa trackerRect(), trackerText()
*/

Bryant's avatar
Bryant committed
733 734 735 736 737 738 739 740
void QwtPicker::drawTracker( QPainter *painter ) const
{
    const QRect textRect = trackerRect( painter->font() );
    if ( !textRect.isEmpty() )
    {
        const QwtText label = trackerText( d_data->trackerPosition );
        if ( !label.isEmpty() )
            label.draw( painter, textRect );
pixhawk's avatar
pixhawk committed
741 742 743
    }
}

Bryant's avatar
Bryant committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
/*!
   \brief Map the pickedPoints() into a selection()

   adjustedPoints() maps the points, that have been collected on
   the parentWidget() into a selection(). The default implementation
   simply returns the points unmodified.

   The reason, why a selection() differs from the picked points
   depends on the application requirements. F.e. :

   - A rectangular selection might need to have a specific aspect ratio only.\n
   - A selection could accept non intersecting polygons only.\n
   - ...\n

   The example below is for a rectangular selection, where the first
   point is the center of the selected rectangle.
  \par Example
  \verbatim QPolygon MyPicker::adjustedPoints(const QPolygon &points) const
{
    QPolygon adjusted;
    if ( points.size() == 2 )
    {
        const int width = qAbs(points[1].x() - points[0].x());
        const int height = qAbs(points[1].y() - points[0].y());

        QRect rect(0, 0, 2 * width, 2 * height);
        rect.moveCenter(points[0]);

        adjusted += rect.topLeft();
        adjusted += rect.bottomRight();
    }
    return adjusted;
}\endverbatim\n

  \param points Selected points
  \return Selected points unmodified
*/
QPolygon QwtPicker::adjustedPoints( const QPolygon &points ) const
{
    return points;
}

/*!
  \return Selected points
  \sa pickedPoints(), adjustedPoints()
*/
QPolygon QwtPicker::selection() const
{
    return adjustedPoints( d_data->pickedPoints );
}

//! \return Current position of the tracker
796
QPoint QwtPicker::trackerPosition() const
pixhawk's avatar
pixhawk committed
797 798 799 800
{
    return d_data->trackerPosition;
}

Bryant's avatar
Bryant committed
801 802 803 804 805 806 807 808 809 810
/*!
   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
pixhawk's avatar
pixhawk committed
811
{
812
    if ( trackerMode() == AlwaysOff ||
Bryant's avatar
Bryant committed
813 814
        ( trackerMode() == ActiveOnly && !isActive() ) )
    {
pixhawk's avatar
pixhawk committed
815 816 817 818 819 820
        return QRect();
    }

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

Bryant's avatar
Bryant committed
821
    QwtText text = trackerText( d_data->trackerPosition );
pixhawk's avatar
pixhawk committed
822 823 824
    if ( text.isEmpty() )
        return QRect();

Bryant's avatar
Bryant committed
825 826
    const QSizeF textSize = text.textSize( font );
    QRect textRect( 0, 0, qCeil( textSize.width() ), qCeil( textSize.height() ) );
pixhawk's avatar
pixhawk committed
827 828 829 830

    const QPoint &pos = d_data->trackerPosition;

    int alignment = 0;
Bryant's avatar
Bryant committed
831 832 833
    if ( isActive() && d_data->pickedPoints.count() > 1
        && rubberBand() != NoRubberBand )
    {
834
        const QPoint last =
Bryant's avatar
Bryant committed
835
            d_data->pickedPoints[int( d_data->pickedPoints.count() ) - 2];
pixhawk's avatar
pixhawk committed
836

Bryant's avatar
Bryant committed
837 838 839 840
        alignment |= ( pos.x() >= last.x() ) ? Qt::AlignRight : Qt::AlignLeft;
        alignment |= ( pos.y() > last.y() ) ? Qt::AlignBottom : Qt::AlignTop;
    }
    else
pixhawk's avatar
pixhawk committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        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;
856

Bryant's avatar
Bryant committed
857
    textRect.moveTopLeft( QPoint( x, y ) );
pixhawk's avatar
pixhawk committed
858

Bryant's avatar
Bryant committed
859
    const QRect pickRect = pickArea().boundingRect().toRect();
pixhawk's avatar
pixhawk committed
860

Bryant's avatar
Bryant committed
861 862 863 864 865 866 867
    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 ) );
pixhawk's avatar
pixhawk committed
868 869 870 871 872 873 874

    return textRect;
}

/*!
  \brief Event filter

Bryant's avatar
Bryant committed
875
  When isEnabled() is true all events of the observed widget are filtered.
pixhawk's avatar
pixhawk committed
876
  Mouse and keyboard events are translated into widgetMouse- and widgetKey-
877
  and widgetWheel-events. Paint and Resize events are handled to keep
Bryant's avatar
Bryant committed
878 879 880 881
  rubber band and tracker up to date.

  \param object Object to be filtered
  \param event Event
pixhawk's avatar
pixhawk committed
882

Bryant's avatar
Bryant committed
883 884 885 886
  \return Always false.

  \sa widgetEnterEvent(), widgetLeaveEvent(),
      widgetMousePressEvent(), widgetMouseReleaseEvent(),
pixhawk's avatar
pixhawk committed
887
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
Bryant's avatar
Bryant committed
888 889
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent(),
      QObject::installEventFilter(), QObject::event()
pixhawk's avatar
pixhawk committed
890
*/
Bryant's avatar
Bryant committed
891
bool QwtPicker::eventFilter( QObject *object, QEvent *event )
pixhawk's avatar
pixhawk committed
892
{
Bryant's avatar
Bryant committed
893 894 895 896 897 898 899 900 901
    if ( object && object == parentWidget() )
    {
        switch ( event->type() )
        {
            case QEvent::Resize:
            {
                const QResizeEvent *re = static_cast<QResizeEvent *>( event );
                if ( d_data->resizeMode == Stretch )
                    stretchSelection( re->oldSize(), re->size() );
902

Bryant's avatar
Bryant committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
                break;
            }
            case QEvent::Enter:
            {
                widgetEnterEvent( event );
                break;
            }
            case QEvent::Leave:
            {
                widgetLeaveEvent( event );
                break;
            }
            case QEvent::MouseButtonPress:
            {
                widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::MouseButtonRelease:
            {
                widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::MouseButtonDblClick:
            {
                widgetMouseDoubleClickEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::MouseMove:
            {
                widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::KeyPress:
            {
                widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
                break;
            }
            case QEvent::KeyRelease:
            {
                widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
                break;
            }
            case QEvent::Wheel:
            {
                widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
                break;
            }
            default:
                break;
pixhawk's avatar
pixhawk committed
952 953 954 955 956 957 958 959
        }
    }
    return false;
}

/*!
  Handle a mouse press event for the observed widget.

Bryant's avatar
Bryant committed
960
  \param mouseEvent Mouse event
pixhawk's avatar
pixhawk committed
961 962 963 964 965

  \sa eventFilter(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
966
void QwtPicker::widgetMousePressEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
967
{
Bryant's avatar
Bryant committed
968
    transition( mouseEvent );
pixhawk's avatar
pixhawk committed
969 970 971 972 973
}

/*!
  Handle a mouse move event for the observed widget.

Bryant's avatar
Bryant committed
974
  \param mouseEvent Mouse event
pixhawk's avatar
pixhawk committed
975 976 977 978 979

  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
980
void QwtPicker::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
981
{
Bryant's avatar
Bryant committed
982 983
    if ( pickArea().contains( mouseEvent->pos() ) )
        d_data->trackerPosition = mouseEvent->pos();
pixhawk's avatar
pixhawk committed
984
    else
Bryant's avatar
Bryant committed
985
        d_data->trackerPosition = QPoint( -1, -1 );
pixhawk's avatar
pixhawk committed
986 987 988 989

    if ( !isActive() )
        updateDisplay();

Bryant's avatar
Bryant committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    transition( mouseEvent );
}

/*!
  Handle a enter event for the observed widget.

  \param event Qt event

  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
void QwtPicker::widgetEnterEvent( QEvent *event )
{
    transition( event );
pixhawk's avatar
pixhawk committed
1005 1006 1007 1008 1009
}

/*!
  Handle a leave event for the observed widget.

Bryant's avatar
Bryant committed
1010 1011
  \param event Qt event

pixhawk's avatar
pixhawk committed
1012 1013 1014 1015
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
1016
void QwtPicker::widgetLeaveEvent( QEvent *event )
pixhawk's avatar
pixhawk committed
1017
{
Bryant's avatar
Bryant committed
1018 1019 1020
    transition( event );

    d_data->trackerPosition = QPoint( -1, -1 );
pixhawk's avatar
pixhawk committed
1021 1022 1023 1024 1025
    if ( !isActive() )
        updateDisplay();
}

/*!
Bryant's avatar
Bryant committed
1026
  Handle a mouse release event for the observed widget.
pixhawk's avatar
pixhawk committed
1027

Bryant's avatar
Bryant committed
1028
  \param mouseEvent Mouse event
pixhawk's avatar
pixhawk committed
1029

1030
  \sa eventFilter(), widgetMousePressEvent(),
pixhawk's avatar
pixhawk committed
1031 1032 1033
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
1034
void QwtPicker::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
1035
{
Bryant's avatar
Bryant committed
1036
    transition( mouseEvent );
pixhawk's avatar
pixhawk committed
1037 1038 1039 1040 1041
}

/*!
  Handle mouse double click event for the observed widget.

Bryant's avatar
Bryant committed
1042
  \param mouseEvent Mouse event
pixhawk's avatar
pixhawk committed
1043 1044 1045 1046 1047

  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseMoveEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
1048
void QwtPicker::widgetMouseDoubleClickEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
1049
{
Bryant's avatar
Bryant committed
1050
    transition( mouseEvent );
pixhawk's avatar
pixhawk committed
1051
}
1052

pixhawk's avatar
pixhawk committed
1053 1054 1055 1056 1057 1058

/*!
  Handle a wheel event for the observed widget.

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

Bryant's avatar
Bryant committed
1059 1060
  \param wheelEvent Wheel event

pixhawk's avatar
pixhawk committed
1061 1062 1063 1064
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
1065
void QwtPicker::widgetWheelEvent( QWheelEvent *wheelEvent )
pixhawk's avatar
pixhawk committed
1066
{
Bryant's avatar
Bryant committed
1067 1068
    if ( pickArea().contains( wheelEvent->pos() ) )
        d_data->trackerPosition = wheelEvent->pos();
pixhawk's avatar
pixhawk committed
1069
    else
Bryant's avatar
Bryant committed
1070
        d_data->trackerPosition = QPoint( -1, -1 );
pixhawk's avatar
pixhawk committed
1071 1072 1073

    updateDisplay();

Bryant's avatar
Bryant committed
1074
    transition( wheelEvent );
pixhawk's avatar
pixhawk committed
1075 1076 1077 1078 1079 1080 1081 1082 1083
}

/*!
  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.

Bryant's avatar
Bryant committed
1084 1085
  \param keyEvent Key event

pixhawk's avatar
pixhawk committed
1086 1087 1088 1089 1090
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetWheelEvent(), widgetKeyReleaseEvent(), stateMachine(),
      QwtEventPattern::KeyPatternCode
*/
Bryant's avatar
Bryant committed
1091
void QwtPicker::widgetKeyPressEvent( QKeyEvent *keyEvent )
pixhawk's avatar
pixhawk committed
1092 1093 1094 1095 1096
{
    int dx = 0;
    int dy = 0;

    int offset = 1;
Bryant's avatar
Bryant committed
1097
    if ( keyEvent->isAutoRepeat() )
pixhawk's avatar
pixhawk committed
1098 1099
        offset = 5;

Bryant's avatar
Bryant committed
1100
    if ( keyMatch( KeyLeft, keyEvent ) )
pixhawk's avatar
pixhawk committed
1101
        dx = -offset;
Bryant's avatar
Bryant committed
1102
    else if ( keyMatch( KeyRight, keyEvent ) )
pixhawk's avatar
pixhawk committed
1103
        dx = offset;
Bryant's avatar
Bryant committed
1104
    else if ( keyMatch( KeyUp, keyEvent ) )
pixhawk's avatar
pixhawk committed
1105
        dy = -offset;
Bryant's avatar
Bryant committed
1106
    else if ( keyMatch( KeyDown, keyEvent ) )
pixhawk's avatar
pixhawk committed
1107
        dy = offset;
Bryant's avatar
Bryant committed
1108 1109
    else if ( keyMatch( KeyAbort, keyEvent ) )
    {
pixhawk's avatar
pixhawk committed
1110
        reset();
Bryant's avatar
Bryant committed
1111 1112 1113
    }
    else
        transition( keyEvent );
pixhawk's avatar
pixhawk committed
1114

Bryant's avatar
Bryant committed
1115 1116 1117 1118
    if ( dx != 0 || dy != 0 )
    {
        const QRect rect = pickArea().boundingRect().toRect();
        const QPoint pos = parentWidget()->mapFromGlobal( QCursor::pos() );
pixhawk's avatar
pixhawk committed
1119 1120

        int x = pos.x() + dx;
Bryant's avatar
Bryant committed
1121 1122
        x = qMax( rect.left(), x );
        x = qMin( rect.right(), x );
pixhawk's avatar
pixhawk committed
1123 1124

        int y = pos.y() + dy;
Bryant's avatar
Bryant committed
1125 1126
        y = qMax( rect.top(), y );
        y = qMin( rect.bottom(), y );
pixhawk's avatar
pixhawk committed
1127

Bryant's avatar
Bryant committed
1128
        QCursor::setPos( parentWidget()->mapToGlobal( QPoint( x, y ) ) );
pixhawk's avatar
pixhawk committed
1129 1130
    }
}
1131

pixhawk's avatar
pixhawk committed
1132 1133 1134 1135 1136
/*!
  Handle a key release event for the observed widget.

  Passes the event to the state machine.

Bryant's avatar
Bryant committed
1137 1138
  \param keyEvent Key event

pixhawk's avatar
pixhawk committed
1139 1140 1141 1142
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
      widgetWheelEvent(), widgetKeyPressEvent(), stateMachine()
*/
Bryant's avatar
Bryant committed
1143
void QwtPicker::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
pixhawk's avatar
pixhawk committed
1144
{
Bryant's avatar
Bryant committed
1145
    transition( keyEvent );
pixhawk's avatar
pixhawk committed
1146 1147 1148
}

/*!
1149
  Passes an event to the state machine and executes the resulting
pixhawk's avatar
pixhawk committed
1150
  commands. Append and Move commands use the current position
Bryant's avatar
Bryant committed
1151
  of the cursor ( QCursor::pos() ).
pixhawk's avatar
pixhawk committed
1152

Bryant's avatar
Bryant committed
1153
  \param event Event
pixhawk's avatar
pixhawk committed
1154
*/
Bryant's avatar
Bryant committed
1155
void QwtPicker::transition( const QEvent *event )
pixhawk's avatar
pixhawk committed
1156 1157 1158 1159
{
    if ( !d_data->stateMachine )
        return;

Bryant's avatar
Bryant committed
1160 1161
    const QList<QwtPickerMachine::Command> commandList =
        d_data->stateMachine->transition( *this, event );
pixhawk's avatar
pixhawk committed
1162 1163

    QPoint pos;
Bryant's avatar
Bryant committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    switch ( event->type() )
    {
        case QEvent::MouseButtonDblClick:
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
        case QEvent::MouseMove:
        {
            const QMouseEvent *me = 
                static_cast< const QMouseEvent * >( event );
            pos = me->pos();
1174 1175
            break;
        }
Bryant's avatar
Bryant committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
        default:
            pos = parentWidget()->mapFromGlobal( QCursor::pos() );
    }

    for ( int i = 0; i < commandList.count(); i++ )
    {
        switch ( commandList[i] )
        {
            case QwtPickerMachine::Begin:
            {
                begin();
                break;
            }
            case QwtPickerMachine::Append:
            {
                append( pos );
                break;
            }
            case QwtPickerMachine::Move:
            {
                move( pos );
                break;
            }
            case QwtPickerMachine::Remove:
            {
                remove();
                break;
            }
            case QwtPickerMachine::End:
            {
                end();
                break;
            }
pixhawk's avatar
pixhawk committed
1209 1210 1211 1212 1213 1214 1215
        }
    }
}

/*!
  Open a selection setting the state to active

Bryant's avatar
Bryant committed
1216
  \sa isActive(), end(), append(), move()
pixhawk's avatar
pixhawk committed
1217 1218 1219 1220 1221 1222
*/
void QwtPicker::begin()
{
    if ( d_data->isActive )
        return;

Bryant's avatar
Bryant committed
1223
    d_data->pickedPoints.resize( 0 );
pixhawk's avatar
pixhawk committed
1224
    d_data->isActive = true;
Bryant's avatar
Bryant committed
1225
    Q_EMIT activated( true );
pixhawk's avatar
pixhawk committed
1226

Bryant's avatar
Bryant committed
1227 1228 1229 1230
    if ( trackerMode() != AlwaysOff )
    {
        if ( d_data->trackerPosition.x() < 0 || d_data->trackerPosition.y() < 0 )
        {
pixhawk's avatar
pixhawk committed
1231 1232
            QWidget *w = parentWidget();
            if ( w )
Bryant's avatar
Bryant committed
1233
                d_data->trackerPosition = w->mapFromGlobal( QCursor::pos() );
pixhawk's avatar
pixhawk committed
1234 1235 1236 1237
        }
    }

    updateDisplay();
Bryant's avatar
Bryant committed
1238
    setMouseTracking( true );
pixhawk's avatar
pixhawk committed
1239 1240 1241 1242 1243
}

/*!
  \brief Close a selection setting the state to inactive.

Bryant's avatar
Bryant committed
1244
  The selection is validated and maybe fixed by accept().
pixhawk's avatar
pixhawk committed
1245 1246 1247 1248

  \param ok If true, complete the selection and emit a selected signal
            otherwise discard the selection.
  \return true if the selection is accepted, false otherwise
Bryant's avatar
Bryant committed
1249
  \sa isActive(), begin(), append(), move(), selected(), accept()
pixhawk's avatar
pixhawk committed
1250
*/
Bryant's avatar
Bryant committed
1251
bool QwtPicker::end( bool ok )
pixhawk's avatar
pixhawk committed
1252
{
Bryant's avatar
Bryant committed
1253 1254 1255
    if ( d_data->isActive )
    {
        setMouseTracking( false );
pixhawk's avatar
pixhawk committed
1256 1257

        d_data->isActive = false;
Bryant's avatar
Bryant committed
1258
        Q_EMIT activated( false );
pixhawk's avatar
pixhawk committed
1259 1260

        if ( trackerMode() == ActiveOnly )
Bryant's avatar
Bryant committed
1261
            d_data->trackerPosition = QPoint( -1, -1 );
pixhawk's avatar
pixhawk committed
1262 1263

        if ( ok )
Bryant's avatar
Bryant committed
1264
            ok = accept( d_data->pickedPoints );
pixhawk's avatar
pixhawk committed
1265 1266

        if ( ok )
Bryant's avatar
Bryant committed
1267
            Q_EMIT selected( d_data->pickedPoints );
pixhawk's avatar
pixhawk committed
1268
        else
Bryant's avatar
Bryant committed
1269
            d_data->pickedPoints.resize( 0 );
pixhawk's avatar
pixhawk committed
1270 1271

        updateDisplay();
Bryant's avatar
Bryant committed
1272 1273
    }
    else
pixhawk's avatar
pixhawk committed
1274 1275 1276 1277 1278 1279
        ok = false;

    return ok;
}

/*!
Bryant's avatar
Bryant committed
1280
   Reset the state machine and terminate ( end(false) ) the selection
pixhawk's avatar
pixhawk committed
1281 1282 1283 1284 1285 1286
*/
void QwtPicker::reset()
{
    if ( d_data->stateMachine )
        d_data->stateMachine->reset();

Bryant's avatar
Bryant committed
1287 1288
    if ( isActive() )
        end( false );
pixhawk's avatar
pixhawk committed
1289 1290 1291
}

/*!
Bryant's avatar
Bryant committed
1292
  Append a point to the selection and update rubber band and tracker.
pixhawk's avatar
pixhawk committed
1293 1294 1295 1296
  The appended() signal is emitted.

  \param pos Additional point

Bryant's avatar
Bryant committed
1297
  \sa isActive(), begin(), end(), move(), appended()
pixhawk's avatar
pixhawk committed
1298
*/
Bryant's avatar
Bryant committed
1299
void QwtPicker::append( const QPoint &pos )
pixhawk's avatar
pixhawk committed
1300
{
Bryant's avatar
Bryant committed
1301 1302 1303 1304 1305
    if ( d_data->isActive )
    {
        const int idx = d_data->pickedPoints.count();
        d_data->pickedPoints.resize( idx + 1 );
        d_data->pickedPoints[idx] = pos;
pixhawk's avatar
pixhawk committed
1306 1307

        updateDisplay();
Bryant's avatar
Bryant committed
1308
        Q_EMIT appended( pos );
pixhawk's avatar
pixhawk committed
1309 1310 1311 1312 1313 1314 1315 1316
    }
}

/*!
  Move the last point of the selection
  The moved() signal is emitted.

  \param pos New position
Bryant's avatar
Bryant committed
1317
  \sa isActive(), begin(), end(), append()
pixhawk's avatar
pixhawk committed
1318
*/
Bryant's avatar
Bryant committed
1319
void QwtPicker::move( const QPoint &pos )
pixhawk's avatar
pixhawk committed
1320
{
Bryant's avatar
Bryant committed
1321 1322 1323 1324 1325 1326 1327 1328
    if ( d_data->isActive )
    {
        const int idx = d_data->pickedPoints.count() - 1;
        if ( idx >= 0 )
        {
            if ( d_data->pickedPoints[idx] != pos )
            {
                d_data->pickedPoints[idx] = pos;
pixhawk's avatar
pixhawk committed
1329 1330

                updateDisplay();
Bryant's avatar
Bryant committed
1331
                Q_EMIT moved( pos );
pixhawk's avatar
pixhawk committed
1332 1333 1334 1335 1336
            }
        }
    }
}

Bryant's avatar
Bryant committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
/*!
  Remove the last point of the selection
  The removed() signal is emitted.

  \sa isActive(), begin(), end(), append(), move()
*/
void QwtPicker::remove()
{
    if ( d_data->isActive )
    {
        const int idx = d_data->pickedPoints.count() - 1;
        if ( idx > 0 )
        {
            const int idx = d_data->pickedPoints.count();

            const QPoint pos = d_data->pickedPoints[idx - 1];
            d_data->pickedPoints.resize( idx - 1 );

            updateDisplay();
            Q_EMIT removed( pos );
        }
    }
}

/*!
  \brief Validate and fix up the selection

  Accepts all selections unmodified

  \param selection Selection to validate and fix up
  \return true, when accepted, false otherwise
*/
bool QwtPicker::accept( QPolygon &selection ) const
pixhawk's avatar
pixhawk committed
1370
{
Bryant's avatar
Bryant committed
1371
    Q_UNUSED( selection );
pixhawk's avatar
pixhawk committed
1372 1373 1374 1375 1376 1377 1378
    return true;
}

/*!
  A picker is active between begin() and end().
  \return true if the selection is active.
*/
1379
bool QwtPicker::isActive() const
pixhawk's avatar
pixhawk committed
1380 1381 1382 1383
{
    return d_data->isActive;
}

Bryant's avatar
Bryant committed
1384 1385 1386 1387 1388 1389
/*!
  Return the points, that have been collected so far. The selection()
  is calculated from the pickedPoints() in adjustedPoints().
  \return Picked points
*/
const QPolygon &QwtPicker::pickedPoints() const
pixhawk's avatar
pixhawk committed
1390
{
Bryant's avatar
Bryant committed
1391
    return d_data->pickedPoints;
pixhawk's avatar
pixhawk committed
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
}

/*!
  Scale the selection by the ratios of oldSize and newSize
  The changed() signal is emitted.

  \param oldSize Previous size
  \param newSize Current size

  \sa ResizeMode, setResizeMode(), resizeMode()
*/
Bryant's avatar
Bryant committed
1403
void QwtPicker::stretchSelection( const QSize &oldSize, const QSize &newSize )
pixhawk's avatar
pixhawk committed
1404
{
Bryant's avatar
Bryant committed
1405 1406
    if ( oldSize.isEmpty() )
    {
1407
        // avoid division by zero. But scaling for small sizes also
pixhawk's avatar
pixhawk committed
1408 1409 1410 1411 1412
        // doesn't make much sense, because of rounding losses. TODO ...
        return;
    }

    const double xRatio =
Bryant's avatar
Bryant committed
1413
        double( newSize.width() ) / double( oldSize.width() );
pixhawk's avatar
pixhawk committed
1414
    const double yRatio =
Bryant's avatar
Bryant committed
1415
        double( newSize.height() ) / double( oldSize.height() );
pixhawk's avatar
pixhawk committed
1416

Bryant's avatar
Bryant committed
1417 1418 1419 1420 1421
    for ( int i = 0; i < int( d_data->pickedPoints.count() ); i++ )
    {
        QPoint &p = d_data->pickedPoints[i];
        p.setX( qRound( p.x() * xRatio ) );
        p.setY( qRound( p.y() * yRatio ) );
pixhawk's avatar
pixhawk committed
1422

Bryant's avatar
Bryant committed
1423
        Q_EMIT changed( d_data->pickedPoints );
pixhawk's avatar
pixhawk committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
    }
}

/*!
  Set mouse tracking for the observed widget.

  In case of enable is true, the previous value
  is saved, that is restored when enable is false.

  \warning Even when enable is false, mouse tracking might be restored
           to true. When mouseTracking for the observed widget
           has been changed directly by QWidget::setMouseTracking
           while mouse tracking has been set to true, this value can't
           be restored.
*/

Bryant's avatar
Bryant committed
1440
void QwtPicker::setMouseTracking( bool enable )
pixhawk's avatar
pixhawk committed
1441 1442 1443 1444 1445
{
    QWidget *widget = parentWidget();
    if ( !widget )
        return;

Bryant's avatar
Bryant committed
1446 1447
    if ( enable )
    {
pixhawk's avatar
pixhawk committed
1448
        d_data->mouseTracking = widget->hasMouseTracking();
Bryant's avatar
Bryant committed
1449 1450 1451 1452 1453
        widget->setMouseTracking( true );
    }
    else
    {
        widget->setMouseTracking( d_data->mouseTracking );
pixhawk's avatar
pixhawk committed
1454 1455 1456 1457 1458 1459
    }
}

/*!
  Find the area of the observed widget, where selection might happen.

Bryant's avatar
Bryant committed
1460
  \return parentWidget()->contentsRect() 
pixhawk's avatar
pixhawk committed
1461
*/
Bryant's avatar
Bryant committed
1462
QPainterPath QwtPicker::pickArea() const
pixhawk's avatar
pixhawk committed
1463
{
Bryant's avatar
Bryant committed
1464
    QPainterPath path;
pixhawk's avatar
pixhawk committed
1465 1466

    const QWidget *widget = parentWidget();
Bryant's avatar
Bryant committed
1467 1468
    if ( widget )
        path.addRect( widget->contentsRect() );
pixhawk's avatar
pixhawk committed
1469

Bryant's avatar
Bryant committed
1470
    return path;
pixhawk's avatar
pixhawk committed
1471 1472
}

Bryant's avatar
Bryant committed
1473
//! Update the state of rubber band and tracker label
pixhawk's avatar
pixhawk committed
1474 1475 1476 1477 1478 1479
void QwtPicker::updateDisplay()
{
    QWidget *w = parentWidget();

    bool showRubberband = false;
    bool showTracker = false;
Bryant's avatar
Bryant committed
1480 1481 1482

    if ( w && w->isVisible() && d_data->enabled )
    {
pixhawk's avatar
pixhawk committed
1483
        if ( rubberBand() != NoRubberBand && isActive() &&
Bryant's avatar
Bryant committed
1484 1485
            rubberBandPen().style() != Qt::NoPen )
        {
pixhawk's avatar
pixhawk committed
1486 1487 1488 1489
            showRubberband = true;
        }

        if ( trackerMode() == AlwaysOn ||
Bryant's avatar
Bryant committed
1490 1491 1492 1493 1494
            ( trackerMode() == ActiveOnly && isActive() ) )
        {
            if ( trackerPen() != Qt::NoPen 
                && !trackerRect( QFont() ).isEmpty() )
            {
pixhawk's avatar
pixhawk committed
1495
                showTracker = true;
Bryant's avatar
Bryant committed
1496
            }
pixhawk's avatar
pixhawk committed
1497 1498 1499
        }
    }

Bryant's avatar
Bryant committed
1500 1501 1502 1503 1504 1505 1506 1507
    QPointer< QwtPickerRubberband > &rw = d_data->rubberBandOverlay;
    if ( showRubberband )
    {
        if ( rw.isNull() )
        {
            rw = new QwtPickerRubberband( this, w );
            rw->setObjectName( "PickerRubberBand" );
            rw->resize( w->size() );
pixhawk's avatar
pixhawk committed
1508
        }
Bryant's avatar
Bryant committed
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531

        if ( d_data->rubberBand <= RectRubberBand )
            rw->setMaskMode( QwtWidgetOverlay::MaskHint );
        else
            rw->setMaskMode( QwtWidgetOverlay::AlphaMask );

        rw->updateOverlay();
    }
    else
    {
        if ( d_data->openGL )
        {
            // Qt 4.8 crashes for a delete
            if ( !rw.isNull() )
            {
                rw->hide();
                rw->deleteLater();
                rw = NULL;
            }
        }
        else
        {
            delete rw;
pixhawk's avatar
pixhawk committed
1532
        }
Bryant's avatar
Bryant committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    }

    QPointer< QwtPickerTracker > &tw = d_data->trackerOverlay;
    if ( showTracker )
    {
        if ( tw.isNull() )
        {
            tw = new QwtPickerTracker( this, w );
            tw->setObjectName( "PickerTracker" );
            tw->resize( w->size() );
        }
        tw->setFont( d_data->trackerFont );
        tw->updateOverlay();
    }
    else
    {
        if ( d_data->openGL )
        {
            // Qt 4.8 crashes for a delete
            if ( !tw.isNull() )
            {
                tw->hide();
                tw->deleteLater();
                tw = NULL;
            }
        }
        else
        {
            delete tw;
        }
    }
pixhawk's avatar
pixhawk committed
1564 1565
}

Bryant's avatar
Bryant committed
1566 1567
//! \return Overlay displaying the rubber band
const QwtWidgetOverlay *QwtPicker::rubberBandOverlay() const
pixhawk's avatar
pixhawk committed
1568
{
Bryant's avatar
Bryant committed
1569
    return d_data->rubberBandOverlay;
pixhawk's avatar
pixhawk committed
1570 1571
}

Bryant's avatar
Bryant committed
1572 1573
//! \return Overlay displaying the tracker text
const QwtWidgetOverlay *QwtPicker::trackerOverlay() const
pixhawk's avatar
pixhawk committed
1574
{
Bryant's avatar
Bryant committed
1575
    return d_data->trackerOverlay;
pixhawk's avatar
pixhawk committed
1576 1577
}