summaryrefslogtreecommitdiffstats
path: root/libksirtet/lib/keys.cpp
blob: 5d25068009862982f2176fd87f93d838ae109a1a (plain)
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
#include "keys.h"
#include "keys.moc"

#include <tqsignal.h>
#include <kkeydialog.h>
#include <tdelocale.h>


KeyData::KeyData(uint maxNb, uint nbActions, const ActionData *data,
                 TQObject *parent)
    : TQObject(parent), _maxNb(maxNb)
{
    _data.duplicate(data, nbActions);

    for (uint n=0; n<maxNb; n++)
        for (uint i=0; i<=n; i++)
            _keycodes[n][i].fill(0, nbActions);
}

void KeyData::setKeycodes(uint nb, uint index, const int *keycodes)
{
    Q_ASSERT( nb!=0 && nb-1<_maxNb && index<nb );
    for (uint n=nb-1; n<_maxNb; n++)
        for (uint k=0; k<_data.size(); k++)
            if ( n==nb-1 || _keycodes[n][index][k]==0 )
                _keycodes[n][index][k] = keycodes[k];
}

void KeyData::setCurrentNb(uint nb)
{
    Q_ASSERT( nb<_maxNb );
    clear();
    _cols.fill(0, nb);
}

void KeyData::clear()
{
    for (uint i=0; i<_cols.size(); i++)
        delete _cols[i];
    _cols.resize(0);
}

void KeyData::createActionCollection(uint index, TQWidget *receiver)
{
    Q_ASSERT( index<_cols.size() );
    _cols[index] = new TDEActionCollection(receiver, this);
    for (uint k=0; k<_data.size(); k++) {
        TQString label = i18n(_data[k].label);
        TQString name = TQString("%2 %3").arg(index+1).arg(_data[k].name);
        const char *slot = (_data[k].slotRelease ? 0 : _data[k].slot);
        TDEAction *a = new TDEAction(label, _keycodes[_cols.size()-1][index][k],
                                 TQT_TQOBJECT(receiver), slot, _cols[index], name.utf8());
        a->setEnabled(false);
        if ( slot==0 ) {
            SpecialData data;
            data.enabled = false;
            data.pressed = new TQSignal(this);
            data.pressed->connect(receiver, _data[k].slot);
            data.released = new TQSignal(this);
            data.released->connect(receiver, _data[k].slotRelease);
            _specActions[a] = data;
        }
    }
    _cols[index]->readShortcutSettings(group());
}

void KeyData::setEnabled(uint index, bool enabled)
{
    for (uint k=0; k<_cols[index]->count(); k++) {
        TQMap<TDEAction *, SpecialData>::Iterator it =
            _specActions.find(_cols[index]->action(k));
        if ( it==_specActions.end() )
            _cols[index]->action(k)->setEnabled(enabled);
        else (*it).enabled = enabled;
    }
}

void KeyData::addKeys(KKeyDialog &d)
{
    for (uint i=0; i<_cols.size(); i++)
        d.insert(_cols[i], i18n("Shortcuts for player #%1/%2").arg(i+1)
                 .arg(_cols.size()));
}

void KeyData::save()
{
    for (uint i=0; i<_cols.size(); i++)
        _cols[i]->writeShortcutSettings(group());
}

void KeyData::keyEvent(TQKeyEvent *e, bool pressed)
{
    if ( e->isAutoRepeat() ) return;
    
    KKey key(e);
    TQMap<TDEAction *, SpecialData>::Iterator it = _specActions.begin();
    for(; it!=_specActions.end(); ++it) {
        if ( !it.data().enabled ) continue;
        if ( !it.key()->shortcut().contains(key) ) continue;
        if (pressed) it.data().pressed->activate();
        else it.data().released->activate();
    }
    e->ignore();
}