qwt_event_pattern.cpp 6.58 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_event_pattern.h"
Bryant's avatar
Bryant committed
11
#include <qevent.h>
pixhawk's avatar
pixhawk committed
12

13
/*!
pixhawk's avatar
pixhawk committed
14 15 16 17 18 19
  Constructor

  \sa MousePatternCode, KeyPatternCode
*/

QwtEventPattern::QwtEventPattern():
Bryant's avatar
Bryant committed
20 21
    d_mousePattern( MousePatternCount ),
    d_keyPattern( KeyPatternCount )
pixhawk's avatar
pixhawk committed
22 23
{
    initKeyPattern();
Bryant's avatar
Bryant committed
24
    initMousePattern( 3 );
pixhawk's avatar
pixhawk committed
25 26 27 28 29 30 31 32 33 34 35 36 37
}

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

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

  \param numButtons Number of mouse buttons ( <= 3 )
  \sa MousePatternCode
*/
Bryant's avatar
Bryant committed
38
void QwtEventPattern::initMousePattern( int numButtons )
pixhawk's avatar
pixhawk committed
39
{
Bryant's avatar
Bryant committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    d_mousePattern.resize( MousePatternCount );

    switch ( numButtons )
    {
        case 1:
        {
            setMousePattern( MouseSelect1, Qt::LeftButton );
            setMousePattern( MouseSelect2, Qt::LeftButton, Qt::ControlModifier );
            setMousePattern( MouseSelect3, Qt::LeftButton, Qt::AltModifier );
            break;
        }
        case 2:
        {
            setMousePattern( MouseSelect1, Qt::LeftButton );
            setMousePattern( MouseSelect2, Qt::RightButton );
            setMousePattern( MouseSelect3, Qt::LeftButton, Qt::AltModifier );
            break;
        }
        default:
        {
            setMousePattern( MouseSelect1, Qt::LeftButton );
            setMousePattern( MouseSelect2, Qt::RightButton );
            setMousePattern( MouseSelect3, Qt::MidButton );
        }
pixhawk's avatar
pixhawk committed
64
    }
Bryant's avatar
Bryant committed
65 66 67 68 69 70 71 72 73

    setMousePattern( MouseSelect4, d_mousePattern[MouseSelect1].button,
        d_mousePattern[MouseSelect1].modifiers | Qt::ShiftModifier );

    setMousePattern( MouseSelect5, d_mousePattern[MouseSelect2].button,
        d_mousePattern[MouseSelect2].modifiers | Qt::ShiftModifier );

    setMousePattern( MouseSelect6, d_mousePattern[MouseSelect3].button,
        d_mousePattern[MouseSelect3].modifiers | Qt::ShiftModifier );
pixhawk's avatar
pixhawk committed
74 75 76 77 78 79 80 81 82
}

/*!
  Set default mouse patterns.

  \sa KeyPatternCode
*/
void QwtEventPattern::initKeyPattern()
{
Bryant's avatar
Bryant committed
83
    d_keyPattern.resize( KeyPatternCount );
pixhawk's avatar
pixhawk committed
84

Bryant's avatar
Bryant committed
85 86 87
    setKeyPattern( KeySelect1, Qt::Key_Return );
    setKeyPattern( KeySelect2, Qt::Key_Space );
    setKeyPattern( KeyAbort, Qt::Key_Escape );
pixhawk's avatar
pixhawk committed
88

Bryant's avatar
Bryant committed
89 90 91 92
    setKeyPattern( KeyLeft, Qt::Key_Left );
    setKeyPattern( KeyRight, Qt::Key_Right );
    setKeyPattern( KeyUp, Qt::Key_Up );
    setKeyPattern( KeyDown, Qt::Key_Down );
pixhawk's avatar
pixhawk committed
93

Bryant's avatar
Bryant committed
94 95 96
    setKeyPattern( KeyRedo, Qt::Key_Plus );
    setKeyPattern( KeyUndo, Qt::Key_Minus );
    setKeyPattern( KeyHome, Qt::Key_Escape );
pixhawk's avatar
pixhawk committed
97 98 99 100 101 102 103
}

/*!
  Change one mouse pattern

  \param pattern Index of the pattern
  \param button Button
Bryant's avatar
Bryant committed
104
  \param modifiers Keyboard modifiers
pixhawk's avatar
pixhawk committed
105 106 107

  \sa QMouseEvent
*/
Bryant's avatar
Bryant committed
108 109
void QwtEventPattern::setMousePattern( MousePatternCode pattern, 
    Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
110
{
Bryant's avatar
Bryant committed
111 112 113 114
    if ( pattern >= 0 && pattern < MousePatternCount )
    {
        d_mousePattern[ pattern ].button = button;
        d_mousePattern[ pattern ].modifiers = modifiers;
pixhawk's avatar
pixhawk committed
115 116 117 118 119 120 121 122
    }
}

/*!
  Change one key pattern

  \param pattern Index of the pattern
  \param key Key
Bryant's avatar
Bryant committed
123
  \param modifiers Keyboard modifiers
pixhawk's avatar
pixhawk committed
124 125 126

  \sa QKeyEvent
*/
Bryant's avatar
Bryant committed
127 128
void QwtEventPattern::setKeyPattern( KeyPatternCode pattern, 
    int key, Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
129
{
Bryant's avatar
Bryant committed
130 131 132 133
    if ( pattern >= 0 && pattern < KeyPatternCount )
    {
        d_keyPattern[ pattern ].key = key;
        d_keyPattern[ pattern ].modifiers = modifiers;
pixhawk's avatar
pixhawk committed
134 135 136 137
    }
}

//! Change the mouse event patterns
Bryant's avatar
Bryant committed
138
void QwtEventPattern::setMousePattern( const QVector<MousePattern> &pattern )
pixhawk's avatar
pixhawk committed
139 140 141 142 143
{
    d_mousePattern = pattern;
}

//! Change the key event patterns
Bryant's avatar
Bryant committed
144
void QwtEventPattern::setKeyPattern( const QVector<KeyPattern> &pattern )
pixhawk's avatar
pixhawk committed
145 146 147 148
{
    d_keyPattern = pattern;
}

Bryant's avatar
Bryant committed
149 150
//! \return Mouse pattern
const QVector<QwtEventPattern::MousePattern> &
pixhawk's avatar
pixhawk committed
151 152 153 154 155
QwtEventPattern::mousePattern() const
{
    return d_mousePattern;
}

Bryant's avatar
Bryant committed
156 157
//! \return Key pattern
const QVector<QwtEventPattern::KeyPattern> &
pixhawk's avatar
pixhawk committed
158 159 160 161 162
QwtEventPattern::keyPattern() const
{
    return d_keyPattern;
}

Bryant's avatar
Bryant committed
163 164
//! \return Mouse pattern
QVector<QwtEventPattern::MousePattern> &QwtEventPattern::mousePattern()
pixhawk's avatar
pixhawk committed
165 166 167 168
{
    return d_mousePattern;
}

Bryant's avatar
Bryant committed
169 170
//! \return Key pattern
QVector<QwtEventPattern::KeyPattern> &QwtEventPattern::keyPattern()
pixhawk's avatar
pixhawk committed
171 172 173 174 175
{
    return d_keyPattern;
}

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

  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.
181

Bryant's avatar
Bryant committed
182 183
  \param code Index of the event pattern
  \param event Mouse event
pixhawk's avatar
pixhawk committed
184 185 186 187
  \return true if matches

  \sa keyMatch()
*/
Bryant's avatar
Bryant committed
188 189
bool QwtEventPattern::mouseMatch( MousePatternCode code, 
    const QMouseEvent *event ) const
pixhawk's avatar
pixhawk committed
190
{
Bryant's avatar
Bryant committed
191 192
    if ( code >= 0 && code < MousePatternCount )
        return mouseMatch( d_mousePattern[ code ], event );
pixhawk's avatar
pixhawk committed
193

Bryant's avatar
Bryant committed
194
    return false;
pixhawk's avatar
pixhawk committed
195 196 197
}

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

  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.
203

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

  \sa keyMatch()
*/

Bryant's avatar
Bryant committed
211 212
bool QwtEventPattern::mouseMatch( const MousePattern &pattern,
    const QMouseEvent *event ) const
pixhawk's avatar
pixhawk committed
213
{
Bryant's avatar
Bryant committed
214
    if ( event == NULL )
pixhawk's avatar
pixhawk committed
215 216
        return false;

Bryant's avatar
Bryant committed
217 218
    const MousePattern mousePattern( event->button(), event->modifiers() );
    return mousePattern == pattern;
pixhawk's avatar
pixhawk committed
219 220 221
}

/*!
222
  \brief Compare a key event with an event pattern.
pixhawk's avatar
pixhawk committed
223 224 225 226

  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.
227

Bryant's avatar
Bryant committed
228 229
  \param code Index of the event pattern
  \param event Key event
pixhawk's avatar
pixhawk committed
230 231 232 233
  \return true if matches

  \sa mouseMatch()
*/
Bryant's avatar
Bryant committed
234 235
bool QwtEventPattern::keyMatch( KeyPatternCode code, 
    const QKeyEvent *event ) const
pixhawk's avatar
pixhawk committed
236
{
Bryant's avatar
Bryant committed
237 238
    if ( code >= 0 && code < KeyPatternCount )
        return keyMatch( d_keyPattern[ code ], event );
pixhawk's avatar
pixhawk committed
239

Bryant's avatar
Bryant committed
240
    return false;
pixhawk's avatar
pixhawk committed
241 242 243
}

/*!
244
  \brief Compare a key event with an event pattern.
pixhawk's avatar
pixhawk committed
245 246 247 248

  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.
249

pixhawk's avatar
pixhawk committed
250
  \param pattern Key event pattern
Bryant's avatar
Bryant committed
251
  \param event Key event
pixhawk's avatar
pixhawk committed
252 253 254 255 256 257
  \return true if matches

  \sa mouseMatch()
*/

bool QwtEventPattern::keyMatch(
Bryant's avatar
Bryant committed
258
    const KeyPattern &pattern, const QKeyEvent *event ) const
pixhawk's avatar
pixhawk committed
259
{
Bryant's avatar
Bryant committed
260
    if ( event == NULL )
pixhawk's avatar
pixhawk committed
261 262
        return false;

Bryant's avatar
Bryant committed
263 264
    const KeyPattern keyPattern( event->key(), event->modifiers() );
    return keyPattern == pattern;
pixhawk's avatar
pixhawk committed
265
}