forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupcombobox.cc
131 lines (103 loc) · 2.81 KB
/
groupcombobox.cc
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
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "groupcombobox.hh"
#include <QEvent>
#include <QShortcutEvent>
GroupComboBox::GroupComboBox( QWidget * parent ): QComboBox( parent ),
popupAction( this ),
selectNextAction( this ),
selectPreviousAction( this )
{
setSizeAdjustPolicy( AdjustToContents );
setToolTip( tr( "Choose a Group (Alt+G)" ) );
popupAction.setShortcut( QKeySequence( "Alt+G" ) );
connect( &popupAction, SIGNAL( triggered() ),
this, SLOT( popupGroups() ) );
addAction( &popupAction );
selectNextAction.setShortcut( QKeySequence( "Alt+PgDown" ) );
connect( &selectNextAction, SIGNAL( triggered() ),
this, SLOT( selectNextGroup() ) );
addAction( &selectNextAction );
selectPreviousAction.setShortcut( QKeySequence( "Alt+PgUp" ) );
connect( &selectPreviousAction, SIGNAL( triggered() ),
this, SLOT( selectPreviousGroup() ) );
addAction( &selectPreviousAction );
}
void GroupComboBox::fill( Instances::Groups const & groups )
{
unsigned prevId = 0;
if ( count() )
prevId = itemData( currentIndex() ).toUInt();
clear();
for( QMap< int, int >::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i )
releaseShortcut( i.key() );
shortcuts.clear();
for( unsigned x = 0; x < groups.size(); ++x )
{
addItem( groups[ x ].makeIcon(),
groups[ x ].name, groups[ x ].id );
if ( prevId == groups[ x ].id )
setCurrentIndex( x );
// Create a shortcut
if ( !groups[ x ].shortcut.isEmpty() )
{
int id = grabShortcut( groups[ x ].shortcut );
setShortcutEnabled( id );
shortcuts.insert( id, x );
}
}
}
bool GroupComboBox::event( QEvent * event )
{
if ( event->type() == QEvent::Shortcut )
{
QShortcutEvent * ev = ( QShortcutEvent * ) event;
QMap< int, int >::const_iterator i = shortcuts.find( ev->shortcutId() );
if ( i != shortcuts.end() )
{
ev->accept();
setCurrentIndex( i.value() );
return true;
}
}
return QComboBox::event( event );
}
void GroupComboBox::setCurrentGroup( unsigned id )
{
for( int x = 0; x < count(); ++x )
{
if ( itemData( x ).toUInt() == id )
{
setCurrentIndex( x );
break;
}
}
}
unsigned GroupComboBox::getCurrentGroup() const
{
if ( !count() )
return 0;
return itemData( currentIndex() ).toUInt();
}
void GroupComboBox::popupGroups()
{
showPopup();
}
void GroupComboBox::selectNextGroup()
{
if( count() <= 1 )
return;
int n = currentIndex() + 1;
if( n >= count() )
n = 0;
setCurrentIndex( n );
}
void GroupComboBox::selectPreviousGroup()
{
if( count() <= 1 )
return;
int n = currentIndex() - 1;
if( n < 0 )
n = count() - 1;
setCurrentIndex( n );
}