[4271] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 2 | * Qwt Widget Library
|
---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
| 8 | *****************************************************************************/
|
---|
| 9 |
|
---|
| 10 | #include "qwt_event_pattern.h"
|
---|
| 11 | #include <qevent.h>
|
---|
| 12 |
|
---|
| 13 | /*!
|
---|
| 14 | Constructor
|
---|
| 15 |
|
---|
| 16 | \sa MousePatternCode, KeyPatternCode
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | QwtEventPattern::QwtEventPattern():
|
---|
| 20 | d_mousePattern( MousePatternCount ),
|
---|
| 21 | d_keyPattern( KeyPatternCount )
|
---|
| 22 | {
|
---|
| 23 | initKeyPattern();
|
---|
| 24 | initMousePattern( 3 );
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | //! Destructor
|
---|
| 28 | QwtEventPattern::~QwtEventPattern()
|
---|
| 29 | {
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /*!
|
---|
| 33 | Set default mouse patterns, depending on the number of mouse buttons
|
---|
| 34 |
|
---|
| 35 | \param numButtons Number of mouse buttons ( <= 3 )
|
---|
| 36 | \sa MousePatternCode
|
---|
| 37 | */
|
---|
| 38 | void QwtEventPattern::initMousePattern( int numButtons )
|
---|
| 39 | {
|
---|
| 40 | const int altButton = Qt::AltModifier;
|
---|
| 41 | const int controlButton = Qt::ControlModifier;
|
---|
| 42 | const int shiftButton = Qt::ShiftModifier;
|
---|
| 43 |
|
---|
| 44 | d_mousePattern.resize( MousePatternCount );
|
---|
| 45 |
|
---|
| 46 | switch ( numButtons )
|
---|
| 47 | {
|
---|
| 48 | case 1:
|
---|
| 49 | {
|
---|
| 50 | setMousePattern( MouseSelect1, Qt::LeftButton );
|
---|
| 51 | setMousePattern( MouseSelect2, Qt::LeftButton, controlButton );
|
---|
| 52 | setMousePattern( MouseSelect3, Qt::LeftButton, altButton );
|
---|
| 53 | break;
|
---|
| 54 | }
|
---|
| 55 | case 2:
|
---|
| 56 | {
|
---|
| 57 | setMousePattern( MouseSelect1, Qt::LeftButton );
|
---|
| 58 | setMousePattern( MouseSelect2, Qt::RightButton );
|
---|
| 59 | setMousePattern( MouseSelect3, Qt::LeftButton, altButton );
|
---|
| 60 | break;
|
---|
| 61 | }
|
---|
| 62 | default:
|
---|
| 63 | {
|
---|
| 64 | setMousePattern( MouseSelect1, Qt::LeftButton );
|
---|
| 65 | setMousePattern( MouseSelect2, Qt::RightButton );
|
---|
| 66 | setMousePattern( MouseSelect3, Qt::MidButton );
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | for ( int i = 0; i < 3; i++ )
|
---|
| 70 | {
|
---|
| 71 | setMousePattern( MouseSelect4 + i,
|
---|
| 72 | d_mousePattern[MouseSelect1 + i].button,
|
---|
| 73 | d_mousePattern[MouseSelect1 + i].state | shiftButton );
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /*!
|
---|
| 78 | Set default mouse patterns.
|
---|
| 79 |
|
---|
| 80 | \sa KeyPatternCode
|
---|
| 81 | */
|
---|
| 82 | void QwtEventPattern::initKeyPattern()
|
---|
| 83 | {
|
---|
| 84 | d_keyPattern.resize( KeyPatternCount );
|
---|
| 85 |
|
---|
| 86 | setKeyPattern( KeySelect1, Qt::Key_Return );
|
---|
| 87 | setKeyPattern( KeySelect2, Qt::Key_Space );
|
---|
| 88 | setKeyPattern( KeyAbort, Qt::Key_Escape );
|
---|
| 89 |
|
---|
| 90 | setKeyPattern( KeyLeft, Qt::Key_Left );
|
---|
| 91 | setKeyPattern( KeyRight, Qt::Key_Right );
|
---|
| 92 | setKeyPattern( KeyUp, Qt::Key_Up );
|
---|
| 93 | setKeyPattern( KeyDown, Qt::Key_Down );
|
---|
| 94 |
|
---|
| 95 | setKeyPattern( KeyRedo, Qt::Key_Plus );
|
---|
| 96 | setKeyPattern( KeyUndo, Qt::Key_Minus );
|
---|
| 97 | setKeyPattern( KeyHome, Qt::Key_Escape );
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /*!
|
---|
| 101 | Change one mouse pattern
|
---|
| 102 |
|
---|
| 103 | \param pattern Index of the pattern
|
---|
| 104 | \param button Button
|
---|
| 105 | \param state State
|
---|
| 106 |
|
---|
| 107 | \sa QMouseEvent
|
---|
| 108 | */
|
---|
| 109 | void QwtEventPattern::setMousePattern( uint pattern, int button, int state )
|
---|
| 110 | {
|
---|
| 111 | if ( pattern < ( uint )d_mousePattern.count() )
|
---|
| 112 | {
|
---|
| 113 | d_mousePattern[int( pattern )].button = button;
|
---|
| 114 | d_mousePattern[int( pattern )].state = state;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /*!
|
---|
| 119 | Change one key pattern
|
---|
| 120 |
|
---|
| 121 | \param pattern Index of the pattern
|
---|
| 122 | \param key Key
|
---|
| 123 | \param state State
|
---|
| 124 |
|
---|
| 125 | \sa QKeyEvent
|
---|
| 126 | */
|
---|
| 127 | void QwtEventPattern::setKeyPattern( uint pattern, int key, int state )
|
---|
| 128 | {
|
---|
| 129 | if ( pattern < ( uint )d_keyPattern.count() )
|
---|
| 130 | {
|
---|
| 131 | d_keyPattern[int( pattern )].key = key;
|
---|
| 132 | d_keyPattern[int( pattern )].state = state;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | //! Change the mouse event patterns
|
---|
| 137 | void QwtEventPattern::setMousePattern( const QVector<MousePattern> &pattern )
|
---|
| 138 | {
|
---|
| 139 | d_mousePattern = pattern;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | //! Change the key event patterns
|
---|
| 143 | void QwtEventPattern::setKeyPattern( const QVector<KeyPattern> &pattern )
|
---|
| 144 | {
|
---|
| 145 | d_keyPattern = pattern;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | //! Return mouse patterns
|
---|
| 149 | const QVector<QwtEventPattern::MousePattern> &
|
---|
| 150 | QwtEventPattern::mousePattern() const
|
---|
| 151 | {
|
---|
| 152 | return d_mousePattern;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | //! Return key patterns
|
---|
| 156 | const QVector<QwtEventPattern::KeyPattern> &
|
---|
| 157 | QwtEventPattern::keyPattern() const
|
---|
| 158 | {
|
---|
| 159 | return d_keyPattern;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | //! Return ,ouse patterns
|
---|
| 163 | QVector<QwtEventPattern::MousePattern> &QwtEventPattern::mousePattern()
|
---|
| 164 | {
|
---|
| 165 | return d_mousePattern;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | //! Return Key patterns
|
---|
| 169 | QVector<QwtEventPattern::KeyPattern> &QwtEventPattern::keyPattern()
|
---|
| 170 | {
|
---|
| 171 | return d_keyPattern;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /*!
|
---|
| 175 | \brief Compare a mouse event with an event pattern.
|
---|
| 176 |
|
---|
| 177 | A mouse event matches the pattern when both have the same button
|
---|
| 178 | value and in the state value the same key flags(Qt::KeyButtonMask)
|
---|
| 179 | are set.
|
---|
| 180 |
|
---|
| 181 | \param pattern Index of the event pattern
|
---|
| 182 | \param event Mouse event
|
---|
| 183 | \return true if matches
|
---|
| 184 |
|
---|
| 185 | \sa keyMatch()
|
---|
| 186 | */
|
---|
| 187 | bool QwtEventPattern::mouseMatch( uint pattern,
|
---|
| 188 | const QMouseEvent *event ) const
|
---|
| 189 | {
|
---|
| 190 | bool ok = false;
|
---|
| 191 |
|
---|
| 192 | if ( event && pattern < ( uint )d_mousePattern.count() )
|
---|
| 193 | ok = mouseMatch( d_mousePattern[int( pattern )], event );
|
---|
| 194 |
|
---|
| 195 | return ok;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /*!
|
---|
| 199 | \brief Compare a mouse event with an event pattern.
|
---|
| 200 |
|
---|
| 201 | A mouse event matches the pattern when both have the same button
|
---|
| 202 | value and in the state value the same key flags(Qt::KeyButtonMask)
|
---|
| 203 | are set.
|
---|
| 204 |
|
---|
| 205 | \param pattern Mouse event pattern
|
---|
| 206 | \param event Mouse event
|
---|
| 207 | \return true if matches
|
---|
| 208 |
|
---|
| 209 | \sa keyMatch()
|
---|
| 210 | */
|
---|
| 211 |
|
---|
| 212 | bool QwtEventPattern::mouseMatch( const MousePattern &pattern,
|
---|
| 213 | const QMouseEvent *event ) const
|
---|
| 214 | {
|
---|
| 215 | if ( event->button() != pattern.button )
|
---|
| 216 | return false;
|
---|
| 217 |
|
---|
| 218 | const bool matched =
|
---|
| 219 | ( event->modifiers() & Qt::KeyboardModifierMask ) ==
|
---|
| 220 | ( int )( pattern.state & Qt::KeyboardModifierMask );
|
---|
| 221 |
|
---|
| 222 | return matched;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /*!
|
---|
| 226 | \brief Compare a key event with an event pattern.
|
---|
| 227 |
|
---|
| 228 | A key event matches the pattern when both have the same key
|
---|
| 229 | value and in the state value the same key flags (Qt::KeyButtonMask)
|
---|
| 230 | are set.
|
---|
| 231 |
|
---|
| 232 | \param pattern Index of the event pattern
|
---|
| 233 | \param event Key event
|
---|
| 234 | \return true if matches
|
---|
| 235 |
|
---|
| 236 | \sa mouseMatch()
|
---|
| 237 | */
|
---|
| 238 | bool QwtEventPattern::keyMatch( uint pattern,
|
---|
| 239 | const QKeyEvent *event ) const
|
---|
| 240 | {
|
---|
| 241 | bool ok = false;
|
---|
| 242 |
|
---|
| 243 | if ( event && pattern < ( uint )d_keyPattern.count() )
|
---|
| 244 | ok = keyMatch( d_keyPattern[int( pattern )], event );
|
---|
| 245 |
|
---|
| 246 | return ok;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /*!
|
---|
| 250 | \brief Compare a key event with an event pattern.
|
---|
| 251 |
|
---|
| 252 | A key event matches the pattern when both have the same key
|
---|
| 253 | value and in the state value the same key flags (Qt::KeyButtonMask)
|
---|
| 254 | are set.
|
---|
| 255 |
|
---|
| 256 | \param pattern Key event pattern
|
---|
| 257 | \param event Key event
|
---|
| 258 | \return true if matches
|
---|
| 259 |
|
---|
| 260 | \sa mouseMatch()
|
---|
| 261 | */
|
---|
| 262 |
|
---|
| 263 | bool QwtEventPattern::keyMatch(
|
---|
| 264 | const KeyPattern &pattern, const QKeyEvent *event ) const
|
---|
| 265 | {
|
---|
| 266 | if ( event->key() != pattern.key )
|
---|
| 267 | return false;
|
---|
| 268 |
|
---|
| 269 | const bool matched =
|
---|
| 270 | ( event->modifiers() & Qt::KeyboardModifierMask ) ==
|
---|
| 271 | ( int )( pattern.state & Qt::KeyboardModifierMask );
|
---|
| 272 |
|
---|
| 273 | return matched;
|
---|
| 274 | }
|
---|