qwt_picker_machine.cpp 13 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10
/* -*- 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
 *****************************************************************************/

#include "qwt_picker_machine.h"
Bryant's avatar
Bryant committed
11 12
#include "qwt_event_pattern.h"
#include <qevent.h>
pixhawk's avatar
pixhawk committed
13 14

//! Constructor
Bryant's avatar
Bryant committed
15 16 17
QwtPickerMachine::QwtPickerMachine( SelectionType type ):
    d_selectionType( type ),
    d_state( 0 )
pixhawk's avatar
pixhawk committed
18 19 20 21 22 23 24 25
{
}

//! Destructor
QwtPickerMachine::~QwtPickerMachine()
{
}

Bryant's avatar
Bryant committed
26 27 28 29 30 31
//! Return the selection type
QwtPickerMachine::SelectionType QwtPickerMachine::selectionType() const
{
    return d_selectionType;
}

pixhawk's avatar
pixhawk committed
32 33 34 35 36 37 38
//! Return the current state
int QwtPickerMachine::state() const
{
    return d_state;
}

//! Change the current state
Bryant's avatar
Bryant committed
39
void QwtPickerMachine::setState( int state )
pixhawk's avatar
pixhawk committed
40 41 42 43 44
{
    d_state = state;
}

//! Set the current state to 0.
45
void QwtPickerMachine::reset()
pixhawk's avatar
pixhawk committed
46
{
Bryant's avatar
Bryant committed
47 48 49 50 51 52 53
    setState( 0 );
}

//! Constructor
QwtPickerTrackerMachine::QwtPickerTrackerMachine():
    QwtPickerMachine( NoSelection )
{
pixhawk's avatar
pixhawk committed
54 55 56
}

//! Transition
Bryant's avatar
Bryant committed
57 58
QList<QwtPickerMachine::Command> QwtPickerTrackerMachine::transition(
    const QwtEventPattern &, const QEvent *e )
59
{
Bryant's avatar
Bryant committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    QList<QwtPickerMachine::Command> cmdList;

    switch ( e->type() )
    {
        case QEvent::Enter:
        case QEvent::MouseMove:
        {
            if ( state() == 0 )
            {
                cmdList += Begin;
                cmdList += Append;
                setState( 1 );
            }
            else
            {
                cmdList += Move;
            }
            break;
pixhawk's avatar
pixhawk committed
78
        }
Bryant's avatar
Bryant committed
79 80 81
        case QEvent::Leave:
        {
            cmdList += Remove;
82
            cmdList += End;
Bryant's avatar
Bryant committed
83
            setState( 0 );
pixhawk's avatar
pixhawk committed
84
        }
Bryant's avatar
Bryant committed
85 86
        default:
            break;
pixhawk's avatar
pixhawk committed
87 88 89 90 91
    }

    return cmdList;
}

Bryant's avatar
Bryant committed
92 93 94 95 96 97
//! Constructor
QwtPickerClickPointMachine::QwtPickerClickPointMachine():
    QwtPickerMachine( PointSelection )
{
}

pixhawk's avatar
pixhawk committed
98
//! Transition
Bryant's avatar
Bryant committed
99 100
QList<QwtPickerMachine::Command> QwtPickerClickPointMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *event )
101
{
Bryant's avatar
Bryant committed
102
    QList<QwtPickerMachine::Command> cmdList;
pixhawk's avatar
pixhawk committed
103

Bryant's avatar
Bryant committed
104 105 106 107 108 109 110
    switch ( event->type() )
    {
        case QEvent::MouseButtonPress:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
111 112
                cmdList += Begin;
                cmdList += Append;
Bryant's avatar
Bryant committed
113
                cmdList += End;
pixhawk's avatar
pixhawk committed
114
            }
Bryant's avatar
Bryant committed
115
            break;
pixhawk's avatar
pixhawk committed
116
        }
Bryant's avatar
Bryant committed
117 118 119 120 121
        case QEvent::KeyPress:
        {
            if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, 
                static_cast<const QKeyEvent *> ( event ) ) )
            {
122 123
                cmdList += Begin;
                cmdList += Append;
pixhawk's avatar
pixhawk committed
124 125
                cmdList += End;
            }
Bryant's avatar
Bryant committed
126
            break;
pixhawk's avatar
pixhawk committed
127
        }
Bryant's avatar
Bryant committed
128 129
        default:
            break;
pixhawk's avatar
pixhawk committed
130 131 132 133 134
    }

    return cmdList;
}

Bryant's avatar
Bryant committed
135 136 137 138 139 140
//! Constructor
QwtPickerDragPointMachine::QwtPickerDragPointMachine():
    QwtPickerMachine( PointSelection )
{
}

pixhawk's avatar
pixhawk committed
141
//! Transition
Bryant's avatar
Bryant committed
142 143
QList<QwtPickerMachine::Command> QwtPickerDragPointMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *event )
144
{
Bryant's avatar
Bryant committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    QList<QwtPickerMachine::Command> cmdList;

    switch ( event->type() )
    {
        case QEvent::MouseButtonPress:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    setState( 1 );
                }
160
            }
Bryant's avatar
Bryant committed
161 162 163 164 165 166 167 168 169 170 171 172 173
            break;
        }
        case QEvent::MouseMove:
        case QEvent::Wheel:
        {
            if ( state() != 0 )
                cmdList += Move;
            break;
        }
        case QEvent::MouseButtonRelease:
        {
            if ( state() != 0 )
            {
174
                cmdList += End;
Bryant's avatar
Bryant committed
175
                setState( 0 );
176
            }
Bryant's avatar
Bryant committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
            break;
        }
        case QEvent::KeyPress:
        {
            if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, 
                static_cast<const QKeyEvent *>( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    setState( 1 );
                }
                else
                {
                    cmdList += End;
                    setState( 0 );
                }
pixhawk's avatar
pixhawk committed
195
            }
Bryant's avatar
Bryant committed
196
            break;
pixhawk's avatar
pixhawk committed
197
        }
Bryant's avatar
Bryant committed
198 199
        default:
            break;
200
    }
Bryant's avatar
Bryant committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    return cmdList;
}

//! Constructor
QwtPickerClickRectMachine::QwtPickerClickRectMachine():
    QwtPickerMachine( RectSelection )
{
}

//! Transition
QList<QwtPickerMachine::Command> QwtPickerClickRectMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *event )
{
    QList<QwtPickerMachine::Command> cmdList;

    switch ( event->type() )
    {
        case QEvent::MouseButtonPress:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
                switch ( state() )
                {
                    case 0:
                    {
                        cmdList += Begin;
                        cmdList += Append;
                        setState( 1 );
                        break;
                    }
                    case 1:
                    {
                        // Uh, strange we missed the MouseButtonRelease
                        break;
                    }
                    default:
                    {
                        cmdList += End;
                        setState( 0 );
                    }
                }
244
            }
Bryant's avatar
Bryant committed
245
            break;
pixhawk's avatar
pixhawk committed
246
        }
Bryant's avatar
Bryant committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260
        case QEvent::MouseMove:
        case QEvent::Wheel:
        {
            if ( state() != 0 )
                cmdList += Move;
            break;
        }
        case QEvent::MouseButtonRelease:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
                if ( state() == 1 )
                {
pixhawk's avatar
pixhawk committed
261
                    cmdList += Append;
Bryant's avatar
Bryant committed
262
                    setState( 2 );
pixhawk's avatar
pixhawk committed
263 264
                }
            }
Bryant's avatar
Bryant committed
265
            break;
pixhawk's avatar
pixhawk committed
266
        }
Bryant's avatar
Bryant committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        case QEvent::KeyPress:
        {
            if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, 
                static_cast<const QKeyEvent *> ( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    setState( 1 );
                }
                else
                {
                    if ( state() == 1 )
                    {
                        cmdList += Append;
                        setState( 2 );
                    }
                    else if ( state() == 2 )
                    {
                        cmdList += End;
                        setState( 0 );
                    }
                }
            }
            break;
        }
        default:
            break;
pixhawk's avatar
pixhawk committed
296 297 298 299 300
    }

    return cmdList;
}

Bryant's avatar
Bryant committed
301 302 303 304 305 306
//! Constructor
QwtPickerDragRectMachine::QwtPickerDragRectMachine():
    QwtPickerMachine( RectSelection )
{
}

pixhawk's avatar
pixhawk committed
307
//! Transition
Bryant's avatar
Bryant committed
308 309
QList<QwtPickerMachine::Command> QwtPickerDragRectMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *event )
310
{
Bryant's avatar
Bryant committed
311
    QList<QwtPickerMachine::Command> cmdList;
pixhawk's avatar
pixhawk committed
312

Bryant's avatar
Bryant committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326
    switch ( event->type() )
    {
        case QEvent::MouseButtonPress:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    cmdList += Append;
                    setState( 2 );
                }
pixhawk's avatar
pixhawk committed
327
            }
Bryant's avatar
Bryant committed
328
            break;
pixhawk's avatar
pixhawk committed
329
        }
Bryant's avatar
Bryant committed
330 331 332 333 334 335
        case QEvent::MouseMove:
        case QEvent::Wheel:
        {
            if ( state() != 0 )
                cmdList += Move;
            break;
pixhawk's avatar
pixhawk committed
336
        }
Bryant's avatar
Bryant committed
337 338 339 340
        case QEvent::MouseButtonRelease:
        {
            if ( state() == 2 )
            {
pixhawk's avatar
pixhawk committed
341
                cmdList += End;
Bryant's avatar
Bryant committed
342
                setState( 0 );
pixhawk's avatar
pixhawk committed
343
            }
Bryant's avatar
Bryant committed
344
            break;
pixhawk's avatar
pixhawk committed
345
        }
Bryant's avatar
Bryant committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        case QEvent::KeyPress:
        {
            if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, 
                static_cast<const QKeyEvent *> ( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    cmdList += Append;
                    setState( 2 );
                }
                else
                {
                    cmdList += End;
                    setState( 0 );
                }
            }
            break;
        }
        default:
            break;
pixhawk's avatar
pixhawk committed
368 369 370 371 372
    }

    return cmdList;
}

Bryant's avatar
Bryant committed
373 374 375 376 377 378
//! Constructor
QwtPickerPolygonMachine::QwtPickerPolygonMachine():
    QwtPickerMachine( PolygonSelection )
{
}

pixhawk's avatar
pixhawk committed
379
//! Transition
Bryant's avatar
Bryant committed
380 381
QList<QwtPickerMachine::Command> QwtPickerPolygonMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *event )
pixhawk's avatar
pixhawk committed
382
{
Bryant's avatar
Bryant committed
383
    QList<QwtPickerMachine::Command> cmdList;
pixhawk's avatar
pixhawk committed
384

Bryant's avatar
Bryant committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    switch ( event->type() )
    {
        case QEvent::MouseButtonPress:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    cmdList += Append;
                    setState( 1 );
                }
                else
                {
                    cmdList += Append;
                }
            }
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect2, 
                static_cast<const QMouseEvent *>( event ) ) )
            {
                if ( state() == 1 )
                {
                    cmdList += End;
                    setState( 0 );
                }
pixhawk's avatar
pixhawk committed
412
            }
Bryant's avatar
Bryant committed
413
            break;
pixhawk's avatar
pixhawk committed
414
        }
Bryant's avatar
Bryant committed
415 416 417 418 419 420
        case QEvent::MouseMove:
        case QEvent::Wheel:
        {
            if ( state() != 0 )
                cmdList += Move;
            break;
pixhawk's avatar
pixhawk committed
421
        }
Bryant's avatar
Bryant committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        case QEvent::KeyPress:
        {
            if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, 
                static_cast<const QKeyEvent *> ( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    cmdList += Append;
                    setState( 1 );
                }
                else
                {
                    cmdList += Append;
                }
            }
            else if ( eventPattern.keyMatch( QwtEventPattern::KeySelect2, 
                static_cast<const QKeyEvent *> ( event ) ) )
            {
                if ( state() == 1 )
                {
                    cmdList += End;
                    setState( 0 );
                }
            }
            break;
        }
        default:
            break;
452
    }
Bryant's avatar
Bryant committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

    return cmdList;
}

//! Constructor
QwtPickerDragLineMachine::QwtPickerDragLineMachine():
    QwtPickerMachine( PolygonSelection )
{
}

//! Transition
QList<QwtPickerMachine::Command> QwtPickerDragLineMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *event )
{
    QList<QwtPickerMachine::Command> cmdList;

    switch( event->type() )
    {
        case QEvent::MouseButtonPress:
        {
            if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
                static_cast<const QMouseEvent *>( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    cmdList += Append;
                    setState( 1 );
                }
            }
            break;
        }
        case QEvent::KeyPress:
        {
            if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1,
                static_cast<const QKeyEvent *> ( event ) ) )
            {
                if ( state() == 0 )
                {
                    cmdList += Begin;
                    cmdList += Append;
                    cmdList += Append;
                    setState( 1 );
                }
                else
                {
                    cmdList += End;
                    setState( 0 );
                }
            }
            break;
        }
        case QEvent::MouseMove:
        case QEvent::Wheel:
        {
            if ( state() != 0 )
                cmdList += Move;

            break;
        }
        case QEvent::MouseButtonRelease:
        {
            if ( state() != 0 )
            {
518
                cmdList += End;
Bryant's avatar
Bryant committed
519
                setState( 0 );
pixhawk's avatar
pixhawk committed
520 521
            }
        }
Bryant's avatar
Bryant committed
522 523
        default:
            break;
pixhawk's avatar
pixhawk committed
524 525 526 527
    }

    return cmdList;
}