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

13
/*!
pixhawk's avatar
pixhawk committed
14 15 16 17 18 19 20 21 22 23 24 25 26 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
  Constructor

  \sa MousePatternCode, KeyPatternCode
*/

QwtEventPattern::QwtEventPattern():
    d_mousePattern(MousePatternCount),
    d_keyPattern(KeyPatternCount)
{
    initKeyPattern();
    initMousePattern(3);
}

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

/*!
  Set default mouse patterns, depending on the number of mouse buttons

  \param numButtons Number of mouse buttons ( <= 3 )
  \sa MousePatternCode
*/
void QwtEventPattern::initMousePattern(int numButtons)
{
#if QT_VERSION < 0x040000
    const int altButton = Qt::AltButton;
    const int controlButton = Qt::ControlButton;
    const int shiftButton = Qt::ShiftButton;
#else
    const int altButton = Qt::AltModifier;
    const int controlButton = Qt::ControlModifier;
    const int shiftButton = Qt::ShiftModifier;
#endif

    d_mousePattern.resize(MousePatternCount);

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    switch(numButtons) {
    case 1: {
        setMousePattern(MouseSelect1, Qt::LeftButton);
        setMousePattern(MouseSelect2, Qt::LeftButton, controlButton);
        setMousePattern(MouseSelect3, Qt::LeftButton, altButton);
        break;
    }
    case 2: {
        setMousePattern(MouseSelect1, Qt::LeftButton);
        setMousePattern(MouseSelect2, Qt::RightButton);
        setMousePattern(MouseSelect3, Qt::LeftButton, altButton);
        break;
    }
    default: {
        setMousePattern(MouseSelect1, Qt::LeftButton);
        setMousePattern(MouseSelect2, Qt::RightButton);
        setMousePattern(MouseSelect3, Qt::MidButton);
    }
pixhawk's avatar
pixhawk committed
70
    }
71 72 73 74
    for ( int i = 0; i < 3; i++ ) {
        setMousePattern(MouseSelect4 + i,
                        d_mousePattern[MouseSelect1 + i].button,
                        d_mousePattern[MouseSelect1 + i].state | shiftButton);
pixhawk's avatar
pixhawk committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }
}

/*!
  Set default mouse patterns.

  \sa KeyPatternCode
*/
void QwtEventPattern::initKeyPattern()
{
    d_keyPattern.resize(KeyPatternCount);

    setKeyPattern(KeySelect1, Qt::Key_Return);
    setKeyPattern(KeySelect2, Qt::Key_Space);
    setKeyPattern(KeyAbort, Qt::Key_Escape);

    setKeyPattern(KeyLeft, Qt::Key_Left);
    setKeyPattern(KeyRight, Qt::Key_Right);
    setKeyPattern(KeyUp, Qt::Key_Up);
    setKeyPattern(KeyDown, Qt::Key_Down);

    setKeyPattern(KeyRedo, Qt::Key_Plus);
    setKeyPattern(KeyUndo, Qt::Key_Minus);
    setKeyPattern(KeyHome, Qt::Key_Escape);
}

/*!
  Change one mouse pattern

  \param pattern Index of the pattern
  \param button Button
  \param state State

  \sa QMouseEvent
*/
void QwtEventPattern::setMousePattern(uint pattern, int button, int state)
{
112
    if ( pattern < (uint)d_mousePattern.count() ) {
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        d_mousePattern[int(pattern)].button = button;
        d_mousePattern[int(pattern)].state = state;
    }
}

/*!
  Change one key pattern

  \param pattern Index of the pattern
  \param key Key
  \param state State

  \sa QKeyEvent
*/
void QwtEventPattern::setKeyPattern(uint pattern, int key, int state)
{
129
    if ( pattern < (uint)d_keyPattern.count() ) {
pixhawk's avatar
pixhawk committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        d_keyPattern[int(pattern)].key = key;
        d_keyPattern[int(pattern)].state = state;
    }
}

//! Change the mouse event patterns
void QwtEventPattern::setMousePattern(const QwtArray<MousePattern> &pattern)
{
    d_mousePattern = pattern;
}

//! Change the key event patterns
void QwtEventPattern::setKeyPattern(const QwtArray<KeyPattern> &pattern)
{
    d_keyPattern = pattern;
}

//! Return mouse patterns
const QwtArray<QwtEventPattern::MousePattern> &
QwtEventPattern::mousePattern() const
{
    return d_mousePattern;
}

//! Return key patterns
const QwtArray<QwtEventPattern::KeyPattern> &
QwtEventPattern::keyPattern() const
{
    return d_keyPattern;
}

//! Return ,ouse patterns
162
QwtArray<QwtEventPattern::MousePattern> &QwtEventPattern::mousePattern()
pixhawk's avatar
pixhawk committed
163 164 165 166 167
{
    return d_mousePattern;
}

//! Return Key patterns
168
QwtArray<QwtEventPattern::KeyPattern> &QwtEventPattern::keyPattern()
pixhawk's avatar
pixhawk committed
169 170 171 172 173
{
    return d_keyPattern;
}

/*!
174
  \brief Compare a mouse event with an event pattern.
pixhawk's avatar
pixhawk committed
175 176 177 178

  A mouse event matches the pattern when both have the same button
  value and in the state value the same key flags(Qt::KeyButtonMask)
  are set.
179

pixhawk's avatar
pixhawk committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  \param pattern Index of the event pattern
  \param e Mouse event
  \return true if matches

  \sa keyMatch()
*/
bool QwtEventPattern::mouseMatch(uint pattern, const QMouseEvent *e) const
{
    bool ok = false;

    if ( e && pattern < (uint)d_mousePattern.count() )
        ok = mouseMatch(d_mousePattern[int(pattern)], e);

    return ok;
}

/*!
197
  \brief Compare a mouse event with an event pattern.
pixhawk's avatar
pixhawk committed
198 199 200 201

  A mouse event matches the pattern when both have the same button
  value and in the state value the same key flags(Qt::KeyButtonMask)
  are set.
202

pixhawk's avatar
pixhawk committed
203 204 205 206 207 208 209 210
  \param pattern Mouse event pattern
  \param e Mouse event
  \return true if matches

  \sa keyMatch()
*/

bool QwtEventPattern::mouseMatch(const MousePattern &pattern,
211
                                 const QMouseEvent *e) const
pixhawk's avatar
pixhawk committed
212 213 214 215 216 217
{
    if ( e->button() != pattern.button )
        return false;

    const bool matched =
#if QT_VERSION < 0x040000
218 219
        (e->state() & Qt::KeyButtonMask) ==
        (pattern.state & Qt::KeyButtonMask);
pixhawk's avatar
pixhawk committed
220
#else
221 222
        (e->modifiers() & Qt::KeyboardModifierMask) ==
        (int)(pattern.state & Qt::KeyboardModifierMask);
pixhawk's avatar
pixhawk committed
223 224 225 226 227 228
#endif

    return matched;
}

/*!
229
  \brief Compare a key event with an event pattern.
pixhawk's avatar
pixhawk committed
230 231 232 233

  A key event matches the pattern when both have the same key
  value and in the state value the same key flags (Qt::KeyButtonMask)
  are set.
234

pixhawk's avatar
pixhawk committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  \param pattern Index of the event pattern
  \param e Key event
  \return true if matches

  \sa mouseMatch()
*/
bool QwtEventPattern::keyMatch(uint pattern, const QKeyEvent *e) const
{
    bool ok = false;

    if ( e && pattern < (uint)d_keyPattern.count() )
        ok = keyMatch(d_keyPattern[int(pattern)], e);

    return ok;
}

/*!
252
  \brief Compare a key event with an event pattern.
pixhawk's avatar
pixhawk committed
253 254 255 256

  A key event matches the pattern when both have the same key
  value and in the state value the same key flags (Qt::KeyButtonMask)
  are set.
257

pixhawk's avatar
pixhawk committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  \param pattern Key event pattern
  \param e Key event
  \return true if matches

  \sa mouseMatch()
*/

bool QwtEventPattern::keyMatch(
    const KeyPattern &pattern, const QKeyEvent *e) const
{
    if ( e->key() != pattern.key)
        return false;

    const bool matched =
#if QT_VERSION < 0x040000
273 274
        (e->state() & Qt::KeyButtonMask) ==
        (pattern.state & Qt::KeyButtonMask);
pixhawk's avatar
pixhawk committed
275
#else
276 277
        (e->modifiers() & Qt::KeyboardModifierMask) ==
        (int)(pattern.state & Qt::KeyboardModifierMask);
pixhawk's avatar
pixhawk committed
278 279 280 281
#endif

    return matched;
}