KeyboardShortcutInput.cpp 3.46 KB
Newer Older
1 2 3
/*****************************************************************************
 * KeyboardShortcutInput.cpp: Input widget for keybard shortcut
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24
#include <QKeyEvent>
#include <QKeySequence>
25
#include <QTimer>
26

27
#include "SettingsManager.h"
28 29
#include "KeyboardShortcutInput.h"

30 31
KeyboardShortcutInput::KeyboardShortcutInput( const QString& name,
                                              const QString& initialValue, QWidget* parent ) :
32
        QPushButton( parent ),
33
        m_name( name ),
34 35 36 37 38 39
        m_capturing( false ),
        m_current( 0 )
{
    m_timer = new QTimer( this );
    m_timer->setSingleShot( true );
    connect( m_timer, SIGNAL( timeout() ), this, SLOT( timeout() ) );
40
    setText( initialValue );
41 42 43
    QKeySequence    ks( initialValue );
    for ( int i = 0; i < 4; ++i )
        m_shortcuts[i] = ks[i];
44
    setMaximumWidth( 300 );
45 46
}

47 48
void
KeyboardShortcutInput::mousePressEvent( QMouseEvent* )
49 50 51
{
    m_capturing = true;
    setDown( true );
52
    memcpy( m_initialValue, m_shortcuts, 4 * sizeof(int) );
53 54 55 56 57
    memset( m_shortcuts, 0, sizeof( m_shortcuts ) );
    m_current = 0;
    setText( "" );
}

58 59
void
KeyboardShortcutInput::mouseReleaseEvent( QMouseEvent* )
60 61 62
{
}

63 64 65 66 67 68 69
QString
KeyboardShortcutInput::shortcut() const
{
    QKeySequence    seq( m_shortcuts[0], m_shortcuts[1], m_shortcuts[2], m_shortcuts[3] );
    return seq.toString();
}

70 71
void
KeyboardShortcutInput::release()
72
{
73 74
    QKeySequence    seq( m_shortcuts[0], m_shortcuts[1], m_shortcuts[2], m_shortcuts[3] );

75 76
    m_capturing = false;
    setDown( false );
77
    setText( seq.toString() );
78 79
}

80 81
void
KeyboardShortcutInput::cancel()
82 83 84 85 86
{
    memcpy( m_shortcuts, m_initialValue, 4 * sizeof(int) );
    release();
}

87 88
void
KeyboardShortcutInput::keyPressEvent( QKeyEvent* e )
89 90 91 92 93 94 95 96
{
    if ( m_capturing == false )
    {
        QPushButton::keyPressEvent( e );
        return ;
    }
    if ( e->modifiers() == Qt::NoModifier && e->key() == Qt::Key_Escape )
    {
97
        cancel();
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }
    else
    {
        m_timer->stop();
        int key = e->key();
        if ( key == Qt::Key_Control || key == Qt::Key_Meta ||
             key == Qt::Key_Shift || key == Qt::Key_Alt ||
             key == Qt::Key_AltGr )
            key = 0;
        int res = key | e->modifiers();
        m_shortcuts[m_current] = res;
        QKeySequence    seq( m_shortcuts[0], m_shortcuts[1], m_shortcuts[2], m_shortcuts[3] );
        setText( seq.toString() );
        if ( key != 0 )
        {
            m_timer->start( 500 );
            ++m_current;
        }
    }
}

119 120
void
KeyboardShortcutInput::timeout()
121 122 123
{
    release();
}