# qwt_picker_machine.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    00008  *****************************************************************************/
    00009
    00010 #include <qevent.h>
    00011 #include "qwt_event_pattern.h"
    00012 #include "qwt_picker_machine.h"
    00013
    00015 QwtPickerMachine::QwtPickerMachine():
    00016     d_state(0)
    00017 {
    00018 }
    00019
    00021 QwtPickerMachine::~QwtPickerMachine()
    00022 {
    00023 }
    00024
    00026 int QwtPickerMachine::state() const
    00027 {
    00028     return d_state;
    00029 }
    00030
    00032 void QwtPickerMachine::setState(int state)
    00033 {
    00034     d_state = state;
    00035 }
    00036
    00038 void QwtPickerMachine::reset()
    00039 {
    00040     setState(0);
    00041 }
    00042
    00044 QwtPickerMachine::CommandList QwtPickerClickPointMachine::transition(
    00045     const QwtEventPattern &eventPattern, const QEvent *e)
    00046 {
    00047     QwtPickerMachine::CommandList cmdList;
    00048
    00049     switch(e->type())
    00050     {
    00051         case QEvent::MouseButtonPress:
    00052         {
    00053             if ( eventPattern.mouseMatch(
    00054                 QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) )
    00055             {
    00056                 cmdList += Begin;
    00057                 cmdList += Append;
    00058                 cmdList += End;
    00059             }
    00060             break;
    00061         }
    00062         case QEvent::KeyPress:
    00063         {
    00064             if ( eventPattern.keyMatch(
    00065                 QwtEventPattern::KeySelect1, (const QKeyEvent *)e) )
    00066             {
    00067                 cmdList += Begin;
    00068                 cmdList += Append;
    00069                 cmdList += End;
    00070             }
    00071             break;
    00072         }
    00073         default:
    00074             break;
    00075     }
    00076
    00077     return cmdList;
    00078 }
    00079
    00081 QwtPickerMachine::CommandList QwtPickerDragPointMachine::transition(
    00082     const QwtEventPattern &eventPattern, const QEvent *e)
    00083 {
    00084     QwtPickerMachine::CommandList cmdList;
    00085
    00086     switch(e->type())
    00087     {
    00088         case QEvent::MouseButtonPress:
    00089         {
    00090             if ( eventPattern.mouseMatch(
    00091                 QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) )
    00092             {
    00093                 if ( state() == 0 )
    00094                 {
    00095                     cmdList += Begin;
    00096                     cmdList += Append;
    00097                     setState(1);
    00098                 }
    00099             }
    00100             break;
    00101         }
    00102         case QEvent::MouseMove:
    00103         case QEvent::Wheel:
    00104         {
    00105             if ( state() != 0 )
    00106                 cmdList += Move;
    00107             break;
    00108         }
    00109         case QEvent::MouseButtonRelease:
    00110         {
    00111             if ( state() != 0 )
    00112             {
    00113                 cmdList += End;
    00114                 setState(0);
    00115             }
    00116             break;
    00117         }
    00118         case QEvent::KeyPress:
    00119         {
    00120             if ( eventPattern.keyMatch(
    00121                 QwtEventPattern::KeySelect1, (const QKeyEvent *)e) )
    00122             {
    00123                 if ( state() == 0 )
    00124                 {
    00125                     cmdList += Begin;
    00126                     cmdList += Append;
    00127                     setState(1);
    00128                 }
    00129                 else
    00130                 {
    00131                     cmdList += End;
    00132                     setState(0);
    00133                 }
    00134             }
    00135             break;
    00136         }
    00137         default:
    00138             break;
    00139     }
    00140
    00141     return cmdList;
    00142 }
    00143
    00145 QwtPickerMachine::CommandList QwtPickerClickRectMachine::transition(
    00146     const QwtEventPattern &eventPattern, const QEvent *e)
    00147 {
    00148     QwtPickerMachine::CommandList cmdList;
    00149
    00150     switch(e->type())
    00151     {
    00152         case QEvent::MouseButtonPress:
    00153         {
    00154             if ( eventPattern.mouseMatch(
    00155                 QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) )
    00156             {
    00157                 switch(state())
    00158                 {
    00159                     case 0:
    00160                     {
    00161                         cmdList += Begin;
    00162                         cmdList += Append;
    00163                         setState(1);
    00164                         break;
    00165                     }
    00166                     case 1:
    00167                     {
    00168                         // Uh, strange we missed the MouseButtonRelease
    00169                         break;
    00170                     }
    00171                     default:
    00172                     {
    00173                         cmdList += End;
    00174                         setState(0);
    00175                     }
    00176                 }
    00177             }
    00178         }
    00179         case QEvent::MouseMove:
    00180         case QEvent::Wheel:
    00181         {
    00182             if ( state() != 0 )
    00183                 cmdList += Move;
    00184             break;
    00185         }
    00186         case QEvent::MouseButtonRelease:
    00187         {
    00188             if ( eventPattern.mouseMatch(
    00189                 QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) )
    00190             {
    00191                 if ( state() == 1 )
    00192                 {
    00193                     cmdList += Append;
    00194                     setState(2);
    00195                 }
    00196             }
    00197             break;
    00198         }
    00199         case QEvent::KeyPress:
    00200         {
    00201             if ( eventPattern.keyMatch(
    00202                 QwtEventPattern::KeySelect1, (const QKeyEvent *)e) )
    00203             {
    00204                 if ( state() == 0 )
    00205                 {
    00206                     cmdList += Begin;
    00207                     cmdList += Append;
    00208                     setState(1);
    00209                 }
    00210                 else
    00211                 {
    00212                     if ( state() == 1 )
    00213                     {
    00214                         cmdList += Append;
    00215                         setState(2);
    00216                     }
    00217                     else if ( state() == 2 )
    00218                     {
    00219                         cmdList += End;
    00220                         setState(0);
    00221                     }
    00222                 }
    00223             }
    00224             break;
    00225         }
    00226         default:
    00227             break;
    00228     }
    00229
    00230     return cmdList;
    00231 }
    00232
    00234 QwtPickerMachine::CommandList QwtPickerDragRectMachine::transition(
    00235     const QwtEventPattern &eventPattern, const QEvent *e)
    00236 {
    00237     QwtPickerMachine::CommandList cmdList;
    00238
    00239     switch(e->type())
    00240     {
    00241         case QEvent::MouseButtonPress:
    00242         {
    00243             if ( eventPattern.mouseMatch(
    00244                 QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) )
    00245             {
    00246                 if ( state() == 0 )
    00247                 {
    00248                     cmdList += Begin;
    00249                     cmdList += Append;
    00250                     cmdList += Append;
    00251                     setState(2);
    00252                 }
    00253             }
    00254             break;
    00255         }
    00256         case QEvent::MouseMove:
    00257         case QEvent::Wheel:
    00258         {
    00259             if ( state() != 0 )
    00260                 cmdList += Move;
    00261             break;
    00262         }
    00263         case QEvent::MouseButtonRelease:
    00264         {
    00265             if ( state() == 2 )
    00266             {
    00267                 cmdList += End;
    00268                 setState(0);
    00269             }
    00270             break;
    00271         }
    00272         case QEvent::KeyPress:
    00273         {
    00274             if ( eventPattern.keyMatch(
    00275                 QwtEventPattern::KeySelect1, (const QKeyEvent *)e) )
    00276             {
    00277                 if ( state() == 0 )
    00278                 {
    00279                     cmdList += Begin;
    00280                     cmdList += Append;
    00281                     cmdList += Append;
    00282                     setState(2);
    00283                 }
    00284                 else
    00285                 {
    00286                     cmdList += End;
    00287                     setState(0);
    00288                 }
    00289             }
    00290             break;
    00291         }
    00292         default:
    00293             break;
    00294     }
    00295
    00296     return cmdList;
    00297 }
    00298
    00300 QwtPickerMachine::CommandList QwtPickerPolygonMachine::transition(
    00301     const QwtEventPattern &eventPattern, const QEvent *e)
    00302 {
    00303     QwtPickerMachine::CommandList cmdList;
    00304
    00305     switch(e->type())
    00306     {
    00307         case QEvent::MouseButtonPress:
    00308         {
    00309             if ( eventPattern.mouseMatch(
    00310                 QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) )
    00311             {
    00312                 if (state() == 0)
    00313                 {
    00314                     cmdList += Begin;
    00315                     cmdList += Append;
    00316                     cmdList += Append;
    00317                     setState(1);
    00318                 }
    00319                 else
    00320                 {
    00321                     cmdList += End;
    00322                     setState(0);
    00323                 }
    00324             }
    00325             if ( eventPattern.mouseMatch(
    00326                 QwtEventPattern::MouseSelect2, (const QMouseEvent *)e) )
    00327             {
    00328                 if (state() == 1)
    00329                     cmdList += Append;
    00330             }
    00331             break;
    00332         }
    00333         case QEvent::MouseMove:
    00334         case QEvent::Wheel:
    00335         {
    00336             if ( state() != 0 )
    00337                 cmdList += Move;
    00338             break;
    00339         }
    00340         case QEvent::KeyPress:
    00341         {
    00342             if ( eventPattern.keyMatch(
    00343                 QwtEventPattern::KeySelect1, (const QKeyEvent *)e) )
    00344             {
    00345                 if ( state() == 0 )
    00346                 {
    00347                     cmdList += Begin;
    00348                     cmdList += Append;
    00349                     cmdList += Append;
    00350                     setState(1);
    00351                 }
    00352                 else
    00353                 {
    00354                     cmdList += End;
    00355                     setState(0);
    00356                 }
    00357             }
    00358             else if ( eventPattern.keyMatch(
    00359                 QwtEventPattern::KeySelect2, (const QKeyEvent *)e) )
    00360             {
    00361                 if ( state() == 1 )
    00362                     cmdList += Append;
    00363             }
    00364             break;
    00365         }
    00366         default:
    00367             break;
    00368     }
    00369
    00370     return cmdList;
    00371 }