File: pKeySequenceInput.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (114 lines) | stat: -rw-r--r-- 3,561 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    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 St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "pKeySequenceInput.h"

#include <QKeySequence>
#include <QKeyEvent>

/*!
    \details Create a new pKeySequenceInput object
    \param parent The parent widget
*/
pKeySequenceInput::pKeySequenceInput( QWidget* parent )
    : QLineEdit( parent )
{}

void pKeySequenceInput::keyPressEvent( QKeyEvent* event )
{   
    // return if auto repeat
    if ( event->isAutoRepeat() )
        return;
    
    // if user press something, sequence is not finished
    setProperty( "Finished", false );
    
    // show current sequence
    setText( checkKeyEvent( event ) );
}

void pKeySequenceInput::keyReleaseEvent( QKeyEvent* event )
{
    // return if auto repeat
    if ( event->isAutoRepeat() )
        return;
    
    // check if sequence is finished or not
    if ( property( "Finished" ).toBool() )
        return;
    
    // show current sequence
    setText( checkKeyEvent( event ) );
}

/*!
    \details Check a QKeyEvent event
    \param event The QKeyEvent to check
    \return The QString shortcut generate from the QKeyEvent
*/
QString pKeySequenceInput::checkKeyEvent( QKeyEvent* event )
{
// is key pressed or key released ?
    const bool keyPressed = event->type() == QEvent::KeyPress;
    
    // or-ed keys
    int mKeys = 0;
    
    // check modifiers pressed
    if ( event->modifiers() & Qt::ControlModifier )
        mKeys |= Qt::ControlModifier;
    if ( event->modifiers() & Qt::AltModifier )
        mKeys |= Qt::AltModifier;
    if ( event->modifiers() & Qt::ShiftModifier )
        mKeys |= Qt::ShiftModifier;
    if ( event->modifiers() & Qt::MetaModifier )
        mKeys |= Qt::MetaModifier;
    
    if ( keyPressed )
    {
        // get press key
        switch( event->key() )
        {
            // this keys can't be used
            case Qt::Key_Shift:
            case Qt::Key_Control:
            case Qt::Key_Meta:
            case Qt::Key_Alt:
            case Qt::Key_AltGr:
            case Qt::Key_Super_L:
            case Qt::Key_Super_R:
            case Qt::Key_Menu:
            case Qt::Key_Hyper_L:
            case Qt::Key_Hyper_R:
            case Qt::Key_Help:
            case Qt::Key_Direction_L:
            case Qt::Key_Direction_R:
                break;
            default:
                // add pressed key
                mKeys |= event->key();
                
                // set sequence finished
                setProperty( "Finished", true );
                break;
        }
    }
    
    // return human readable key sequence
    return QKeySequence( mKeys ).toString();
}