# qwt_event_pattern.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
    00019 QwtEventPattern::QwtEventPattern():
    00020     d_mousePattern(MousePatternCount),
    00021     d_keyPattern(KeyPatternCount)
    00022 {
    00023     initKeyPattern();
    00024     initMousePattern(3);
    00025 }
    00026
    00028 QwtEventPattern::~QwtEventPattern()
    00029 {
    00030 }
    00031
    00038 void QwtEventPattern::initMousePattern(int numButtons)
    00039 {
    00040 #if QT_VERSION < 0x040000
    00041     const int altButton = Qt::AltButton;
    00042     const int controlButton = Qt::ControlButton;
    00043     const int shiftButton = Qt::ShiftButton;
    00044 #else
    00045     const int altButton = Qt::AltModifier;
    00046     const int controlButton = Qt::ControlModifier;
    00047     const int shiftButton = Qt::ShiftModifier;
    00048 #endif
    00049
    00050     d_mousePattern.resize(MousePatternCount);
    00051
    00052     switch(numButtons)
    00053     {
    00054         case 1:
    00055         {
    00056             setMousePattern(MouseSelect1, Qt::LeftButton);
    00057             setMousePattern(MouseSelect2, Qt::LeftButton, controlButton);
    00058             setMousePattern(MouseSelect3, Qt::LeftButton, altButton);
    00059             break;
    00060         }
    00061         case 2:
    00062         {
    00063             setMousePattern(MouseSelect1, Qt::LeftButton);
    00064             setMousePattern(MouseSelect2, Qt::RightButton);
    00065             setMousePattern(MouseSelect3, Qt::LeftButton, altButton);
    00066             break;
    00067         }
    00068         default:
    00069         {
    00070             setMousePattern(MouseSelect1, Qt::LeftButton);
    00071             setMousePattern(MouseSelect2, Qt::RightButton);
    00072             setMousePattern(MouseSelect3, Qt::MidButton);
    00073         }
    00074     }
    00075     for ( int i = 0; i < 3; i++ )
    00076     {
    00077         setMousePattern(MouseSelect4 + i,
    00078             d_mousePattern[MouseSelect1 + i].button,
    00079             d_mousePattern[MouseSelect1 + i].state | shiftButton);
    00080     }
    00081 }
    00082
    00088 void QwtEventPattern::initKeyPattern()
    00089 {
    00090     d_keyPattern.resize(KeyPatternCount);
    00091
    00092     setKeyPattern(KeySelect1, Qt::Key_Return);
    00093     setKeyPattern(KeySelect2, Qt::Key_Space);
    00094     setKeyPattern(KeyAbort, Qt::Key_Escape);
    00095
    00096     setKeyPattern(KeyLeft, Qt::Key_Left);
    00097     setKeyPattern(KeyRight, Qt::Key_Right);
    00098     setKeyPattern(KeyUp, Qt::Key_Up);
    00099     setKeyPattern(KeyDown, Qt::Key_Down);
    00100
    00101     setKeyPattern(KeyRedo, Qt::Key_Plus);
    00102     setKeyPattern(KeyUndo, Qt::Key_Minus);
    00103     setKeyPattern(KeyHome, Qt::Key_Escape);
    00104 }
    00105
    00115 void QwtEventPattern::setMousePattern(uint pattern, int button, int state)
    00116 {
    00117     if ( pattern < (uint)d_mousePattern.count() )
    00118     {
    00119         d_mousePattern[int(pattern)].button = button;
    00120         d_mousePattern[int(pattern)].state = state;
    00121     }
    00122 }
    00123
    00133 void QwtEventPattern::setKeyPattern(uint pattern, int key, int state)
    00134 {
    00135     if ( pattern < (uint)d_keyPattern.count() )
    00136     {
    00137         d_keyPattern[int(pattern)].key = key;
    00138         d_keyPattern[int(pattern)].state = state;
    00139     }
    00140 }
    00141
    00143 void QwtEventPattern::setMousePattern(const QwtArray<MousePattern> &pattern)
    00144 {
    00145     d_mousePattern = pattern;
    00146 }
    00147
    00149 void QwtEventPattern::setKeyPattern(const QwtArray<KeyPattern> &pattern)
    00150 {
    00151     d_keyPattern = pattern;
    00152 }
    00153
    00155 const QwtArray<QwtEventPattern::MousePattern> &
    00156 QwtEventPattern::mousePattern() const
    00157 {
    00158     return d_mousePattern;
    00159 }
    00160
    00162 const QwtArray<QwtEventPattern::KeyPattern> &
    00163 QwtEventPattern::keyPattern() const
    00164 {
    00165     return d_keyPattern;
    00166 }
    00167
    00169 QwtArray<QwtEventPattern::MousePattern> &QwtEventPattern::mousePattern()
    00170 {
    00171     return d_mousePattern;
    00172 }
    00173
    00175 QwtArray<QwtEventPattern::KeyPattern> &QwtEventPattern::keyPattern()
    00176 {
    00177     return d_keyPattern;
    00178 }
    00179
    00193 bool QwtEventPattern::mouseMatch(uint pattern, const QMouseEvent *e) const
    00194 {
    00195     bool ok = false;
    00196
    00197     if ( e && pattern < (uint)d_mousePattern.count() )
    00198         ok = mouseMatch(d_mousePattern[int(pattern)], e);
    00199
    00200     return ok;
    00201 }
    00202
    00217 bool QwtEventPattern::mouseMatch(const MousePattern &pattern,
    00218     const QMouseEvent *e) const
    00219 {
    00220     if ( e->button() != pattern.button )
    00221         return false;
    00222
    00223     const bool matched =
    00224 #if QT_VERSION < 0x040000
    00225         (e->state() & Qt::KeyButtonMask) ==
    00226             (pattern.state & Qt::KeyButtonMask);
    00227 #else
    00228         (e->modifiers() & Qt::KeyboardModifierMask) ==
    00229             (int)(pattern.state & Qt::KeyboardModifierMask);
    00230 #endif
    00231
    00232     return matched;
    00233 }
    00234
    00248 bool QwtEventPattern::keyMatch(uint pattern, const QKeyEvent *e) const
    00249 {
    00250     bool ok = false;
    00251
    00252     if ( e && pattern < (uint)d_keyPattern.count() )
    00253         ok = keyMatch(d_keyPattern[int(pattern)], e);
    00254
    00255     return ok;
    00256 }
    00257
    00272 bool QwtEventPattern::keyMatch(
    00273     const KeyPattern &pattern, const QKeyEvent *e) const
    00274 {
    00275     if ( e->key() != pattern.key)
    00276         return false;
    00277
    00278     const bool matched =
    00279 #if QT_VERSION < 0x040000
    00280         (e->state() & Qt::KeyButtonMask) ==
    00281             (pattern.state & Qt::KeyButtonMask);
    00282 #else
    00283         (e->modifiers() & Qt::KeyboardModifierMask) ==
    00284             (int)(pattern.state & Qt::KeyboardModifierMask);
    00285 #endif
    00286
    00287     return matched;
    00288 }