qwt_picker_machine.cpp 7.67 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* -*- 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 <qevent.h>
#include "qwt_event_pattern.h"
#include "qwt_picker_machine.h"

//! Constructor
QwtPickerMachine::QwtPickerMachine():
    d_state(0)
{
}

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

//! Return the current state
int QwtPickerMachine::state() const
{
    return d_state;
}

//! Change the current state
void QwtPickerMachine::setState(int state)
{
    d_state = state;
}

//! Set the current state to 0.
38
void QwtPickerMachine::reset()
pixhawk's avatar
pixhawk committed
39 40 41 42 43 44 45
{
    setState(0);
}

//! Transition
QwtPickerMachine::CommandList QwtPickerClickPointMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *e)
46
{
pixhawk's avatar
pixhawk committed
47 48
    QwtPickerMachine::CommandList cmdList;

49 50 51 52 53 54 55
    switch(e->type()) {
    case QEvent::MouseButtonPress: {
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) ) {
            cmdList += Begin;
            cmdList += Append;
            cmdList += End;
pixhawk's avatar
pixhawk committed
56
        }
57 58 59 60 61 62 63 64
        break;
    }
    case QEvent::KeyPress: {
        if ( eventPattern.keyMatch(
                    QwtEventPattern::KeySelect1, (const QKeyEvent *)e) ) {
            cmdList += Begin;
            cmdList += Append;
            cmdList += End;
pixhawk's avatar
pixhawk committed
65
        }
66 67 68 69
        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
70 71 72 73 74 75 76 77
    }

    return cmdList;
}

//! Transition
QwtPickerMachine::CommandList QwtPickerDragPointMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *e)
78
{
pixhawk's avatar
pixhawk committed
79 80
    QwtPickerMachine::CommandList cmdList;

81 82 83 84 85 86 87 88
    switch(e->type()) {
    case QEvent::MouseButtonPress: {
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) ) {
            if ( state() == 0 ) {
                cmdList += Begin;
                cmdList += Append;
                setState(1);
pixhawk's avatar
pixhawk committed
89 90
            }
        }
91 92 93 94 95 96 97 98 99 100 101 102
        break;
    }
    case QEvent::MouseMove:
    case QEvent::Wheel: {
        if ( state() != 0 )
            cmdList += Move;
        break;
    }
    case QEvent::MouseButtonRelease: {
        if ( state() != 0 ) {
            cmdList += End;
            setState(0);
pixhawk's avatar
pixhawk committed
103
        }
104 105 106 107 108 109 110 111 112 113
        break;
    }
    case QEvent::KeyPress: {
        if ( eventPattern.keyMatch(
                    QwtEventPattern::KeySelect1, (const QKeyEvent *)e) ) {
            if ( state() == 0 ) {
                cmdList += Begin;
                cmdList += Append;
                setState(1);
            } else {
pixhawk's avatar
pixhawk committed
114 115 116 117
                cmdList += End;
                setState(0);
            }
        }
118 119 120 121
        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
122 123 124 125 126 127 128 129
    }

    return cmdList;
}

//! Transition
QwtPickerMachine::CommandList QwtPickerClickRectMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *e)
130
{
pixhawk's avatar
pixhawk committed
131 132
    QwtPickerMachine::CommandList cmdList;

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    switch(e->type()) {
    case QEvent::MouseButtonPress: {
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) ) {
            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);
            }
pixhawk's avatar
pixhawk committed
152 153
            }
        }
154 155 156 157 158 159 160 161 162 163 164 165 166 167
    }
    case QEvent::MouseMove:
    case QEvent::Wheel: {
        if ( state() != 0 )
            cmdList += Move;
        break;
    }
    case QEvent::MouseButtonRelease: {
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) ) {
            if ( state() == 1 ) {
                cmdList += Append;
                setState(2);
            }
pixhawk's avatar
pixhawk committed
168
        }
169 170 171 172 173 174 175 176 177 178 179
        break;
    }
    case QEvent::KeyPress: {
        if ( eventPattern.keyMatch(
                    QwtEventPattern::KeySelect1, (const QKeyEvent *)e) ) {
            if ( state() == 0 ) {
                cmdList += Begin;
                cmdList += Append;
                setState(1);
            } else {
                if ( state() == 1 ) {
pixhawk's avatar
pixhawk committed
180 181
                    cmdList += Append;
                    setState(2);
182 183 184
                } else if ( state() == 2 ) {
                    cmdList += End;
                    setState(0);
pixhawk's avatar
pixhawk committed
185 186 187
                }
            }
        }
188 189 190 191
        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
192 193 194 195 196 197 198 199
    }

    return cmdList;
}

//! Transition
QwtPickerMachine::CommandList QwtPickerDragRectMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *e)
200
{
pixhawk's avatar
pixhawk committed
201 202
    QwtPickerMachine::CommandList cmdList;

203 204 205 206 207 208 209 210 211
    switch(e->type()) {
    case QEvent::MouseButtonPress: {
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) ) {
            if ( state() == 0 ) {
                cmdList += Begin;
                cmdList += Append;
                cmdList += Append;
                setState(2);
pixhawk's avatar
pixhawk committed
212 213
            }
        }
214 215 216 217 218 219 220 221 222 223 224 225
        break;
    }
    case QEvent::MouseMove:
    case QEvent::Wheel: {
        if ( state() != 0 )
            cmdList += Move;
        break;
    }
    case QEvent::MouseButtonRelease: {
        if ( state() == 2 ) {
            cmdList += End;
            setState(0);
pixhawk's avatar
pixhawk committed
226
        }
227 228 229 230 231 232 233 234 235 236 237
        break;
    }
    case QEvent::KeyPress: {
        if ( eventPattern.keyMatch(
                    QwtEventPattern::KeySelect1, (const QKeyEvent *)e) ) {
            if ( state() == 0 ) {
                cmdList += Begin;
                cmdList += Append;
                cmdList += Append;
                setState(2);
            } else {
pixhawk's avatar
pixhawk committed
238 239 240 241
                cmdList += End;
                setState(0);
            }
        }
242 243 244 245
        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
246 247 248 249 250 251 252 253 254 255 256
    }

    return cmdList;
}

//! Transition
QwtPickerMachine::CommandList QwtPickerPolygonMachine::transition(
    const QwtEventPattern &eventPattern, const QEvent *e)
{
    QwtPickerMachine::CommandList cmdList;

257 258 259 260 261 262 263 264 265 266 267 268
    switch(e->type()) {
    case QEvent::MouseButtonPress: {
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect1, (const QMouseEvent *)e) ) {
            if (state() == 0) {
                cmdList += Begin;
                cmdList += Append;
                cmdList += Append;
                setState(1);
            } else {
                cmdList += End;
                setState(0);
pixhawk's avatar
pixhawk committed
269 270
            }
        }
271 272 273 274
        if ( eventPattern.mouseMatch(
                    QwtEventPattern::MouseSelect2, (const QMouseEvent *)e) ) {
            if (state() == 1)
                cmdList += Append;
pixhawk's avatar
pixhawk committed
275
        }
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        break;
    }
    case QEvent::MouseMove:
    case QEvent::Wheel: {
        if ( state() != 0 )
            cmdList += Move;
        break;
    }
    case QEvent::KeyPress: {
        if ( eventPattern.keyMatch(
                    QwtEventPattern::KeySelect1, (const QKeyEvent *)e) ) {
            if ( state() == 0 ) {
                cmdList += Begin;
                cmdList += Append;
                cmdList += Append;
                setState(1);
            } else {
                cmdList += End;
                setState(0);
pixhawk's avatar
pixhawk committed
295
            }
296 297 298 299
        } else if ( eventPattern.keyMatch(
                        QwtEventPattern::KeySelect2, (const QKeyEvent *)e) ) {
            if ( state() == 1 )
                cmdList += Append;
pixhawk's avatar
pixhawk committed
300
        }
301 302 303 304
        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
305 306 307 308
    }

    return cmdList;
}