summaryrefslogtreecommitdiffstats
path: root/tdecore/tdeshortcutmenu.cpp
blob: 91061a7342d212c0bb8428392d587713c058cac8 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
    Copyright (c) 2002 Ellis Whitehead <ellis@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqkeysequence.h>
#include <tqlabel.h>
#include <tqpopupmenu.h>

#include "tdeaccelaction.h"
#include <kdebug.h>
#include <kglobalsettings.h>
#include "tdeshortcutmenu.h"
//#include <kkeynative.h>

TDEShortcutMenu::TDEShortcutMenu( TQWidget* pParent, TDEAccelActions* pActions, KKeySequence seq )
:	TQPopupMenu( pParent ),
	m_pActions( pActions ),
	m_seq( seq )
{
	kdDebug() << seq.toStringInternal() << endl;
	
	TQFont fontTitle = TDEGlobalSettings::menuFont();
	fontTitle.setBold( true );
	
	pTitle = new TQLabel( "", (TQWidget*)0 );
	pTitle->setFont( fontTitle );
	pTitle->setFrameShape( TQFrame::Panel );	
	
	insertItem( pTitle );
}

bool TDEShortcutMenu::insertAction( uint iAction, KKeySequence seq )
{
	TDEAccelAction* pAction = m_pActions->actionPtr( iAction );
	
	if( pAction ) {
		insertItem( "", iAction );
		m_seqs[indexOf(iAction)] = seq;
		return true;
	} else
		return false;
}


void TDEShortcutMenu::updateShortcuts()
{
	pTitle->setText( m_seq.toString() + ",..." );
	
	for( uint iItem = 1; iItem < count(); iItem++ ) {
		int iAction = idAt( iItem );
		if( iAction >= 0 ) {
			TDEAccelAction* pAction = m_pActions->actionPtr( iAction );
			if( pAction ) {
				KKeySequence seq = m_seqs[iItem];
				TQString sSeq = seq.key(m_seq.count()).toString();
				for( uint iKey = m_seq.count() + 1; iKey < seq.count(); iKey++ )
					sSeq += TQString(",") + seq.key(iKey).toString();

				kdDebug(125) << "seq = " << seq.toStringInternal() << " sSeq = " << sSeq << endl;
				changeItem( iAction, pAction->label() + "\t" + sSeq );
			}
		}
	}
}

void TDEShortcutMenu::keyPressEvent( TQKeyEvent* pEvent )
{
	kdDebug() << "keypress; " << pEvent->key() << endl;
	KKey key( pEvent );
	
	switch( pEvent->key() ) {
	 case TQ_Key_Shift:
	 case TQ_Key_Control:
	 case TQ_Key_Alt:
	 case TQ_Key_Meta:
	 case TQ_Key_Super_L:
	 case TQ_Key_Super_R:
	 case TQ_Key_Hyper_L:
	 case TQ_Key_Hyper_R:
		break;
	 default:
		int iItem = searchForKey( key );
		// If key not found, look for unmodified version.
		if( iItem == -1 ) {
			key = pEvent->key();
			iItem = searchForKey( key );
		}
			
		if( iItem == -1 ) {
			// Let Up and Down keys navigate menu,
			// And permit Enter, Return to select the item.
			if( pEvent->key() == Qt::Key_Up    || pEvent->key() == Qt::Key_Down ||
			    pEvent->key() == Qt::Key_Enter || pEvent->key() == Qt::Key_Return )
				TQPopupMenu::keyPressEvent( pEvent );
			else
				close();
		}
		else if( iItem == 0 )
			keepItemsMatching( key );
		else
			activateItemAt( iItem );
	}
}

int TDEShortcutMenu::searchForKey( KKey key )
{
	int iItemFound = -1; // -1 indicates no match
	uint iKey = m_seq.count();
	
	for( uint iItem = 1; iItem < count(); iItem++ ) {
		if( m_seqs.contains( iItem ) ) {
			KKey keyItem = m_seqs[iItem].key( iKey );
			//kdDebug(125) << "iItem = " << iItem << " key = " << key.toStringInternal() << " keyItem = " << keyItem.toStringInternal() << endl;
			if( key == keyItem ) {
				if( iItemFound == -1 )
					iItemFound = iItem;
				else
					return 0; // 0 indicates duplicate matches
			}
		}
	}
	
	return iItemFound;
}

void TDEShortcutMenu::keepItemsMatching( KKey key )
{
	kdDebug(125) << "MyAccel::keepItemsMatching( " << key.toStringInternal() << " )" << endl;
	
	uint iKey = m_seq.count();
	m_seq.setKey( iKey, key );
	
	for( uint iItem = 1; iItem < count(); iItem++ ) {
		if( m_seqs.contains( iItem ) ) {
			KKey keyItem = m_seqs[iItem].key( iKey );
			if( key != keyItem ) {
				m_seqs.remove( iItem );
				removeItemAt( iItem-- );
			}
		}
	}
	
	updateShortcuts();
}

#include "tdeshortcutmenu.moc"