Skip to content
qwt_event_pattern.cpp 6.58 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- 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
#include <qevent.h>
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
  Constructor

  \sa MousePatternCode, KeyPatternCode
*/

QwtEventPattern::QwtEventPattern():
Bryant's avatar
Bryant committed
    d_mousePattern( MousePatternCount ),
    d_keyPattern( KeyPatternCount )
pixhawk's avatar
pixhawk committed
{
    initKeyPattern();
Bryant's avatar
Bryant committed
    initMousePattern( 3 );
pixhawk's avatar
pixhawk committed
}

//! 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
void QwtEventPattern::initMousePattern( int numButtons )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    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
    }
Bryant's avatar
Bryant committed

    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
}

/*!
  Set default mouse patterns.

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

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

Bryant's avatar
Bryant committed
    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

Bryant's avatar
Bryant committed
    setKeyPattern( KeyRedo, Qt::Key_Plus );
    setKeyPattern( KeyUndo, Qt::Key_Minus );
    setKeyPattern( KeyHome, Qt::Key_Escape );
pixhawk's avatar
pixhawk committed
}

/*!
  Change one mouse pattern

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

  \sa QMouseEvent
*/
Bryant's avatar
Bryant committed
void QwtEventPattern::setMousePattern( MousePatternCode pattern, 
    Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( pattern >= 0 && pattern < MousePatternCount )
    {
        d_mousePattern[ pattern ].button = button;
        d_mousePattern[ pattern ].modifiers = modifiers;
pixhawk's avatar
pixhawk committed
    }
}

/*!
  Change one key pattern

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

  \sa QKeyEvent
*/
Bryant's avatar
Bryant committed
void QwtEventPattern::setKeyPattern( KeyPatternCode pattern, 
    int key, Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( pattern >= 0 && pattern < KeyPatternCount )
    {
        d_keyPattern[ pattern ].key = key;
        d_keyPattern[ pattern ].modifiers = modifiers;
pixhawk's avatar
pixhawk committed
    }
}

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

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

Bryant's avatar
Bryant committed
//! \return Mouse pattern
const QVector<QwtEventPattern::MousePattern> &
pixhawk's avatar
pixhawk committed
QwtEventPattern::mousePattern() const
{
    return d_mousePattern;
}

Bryant's avatar
Bryant committed
//! \return Key pattern
const QVector<QwtEventPattern::KeyPattern> &
pixhawk's avatar
pixhawk committed
QwtEventPattern::keyPattern() const
{
    return d_keyPattern;
}

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

Bryant's avatar
Bryant committed
//! \return Key pattern
QVector<QwtEventPattern::KeyPattern> &QwtEventPattern::keyPattern()
pixhawk's avatar
pixhawk committed
{
    return d_keyPattern;
}

/*!
  \brief Compare a mouse event with an event pattern.
pixhawk's avatar
pixhawk committed

  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.
Bryant's avatar
Bryant committed
  \param code Index of the event pattern
  \param event Mouse event
pixhawk's avatar
pixhawk committed
  \return true if matches

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

Bryant's avatar
Bryant committed
    return false;
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Compare a mouse event with an event pattern.
pixhawk's avatar
pixhawk committed

  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.
pixhawk's avatar
pixhawk committed
  \param pattern Mouse event pattern
Bryant's avatar
Bryant committed
  \param event Mouse event
pixhawk's avatar
pixhawk committed
  \return true if matches

  \sa keyMatch()
*/

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

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

/*!
  \brief Compare a key event with an event pattern.
pixhawk's avatar
pixhawk committed

  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.
Bryant's avatar
Bryant committed
  \param code Index of the event pattern
  \param event Key event
pixhawk's avatar
pixhawk committed
  \return true if matches

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

Bryant's avatar
Bryant committed
    return false;
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Compare a key event with an event pattern.
pixhawk's avatar
pixhawk committed

  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.
pixhawk's avatar
pixhawk committed
  \param pattern Key event pattern
Bryant's avatar
Bryant committed
  \param event Key event
pixhawk's avatar
pixhawk committed
  \return true if matches

  \sa mouseMatch()
*/

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

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