forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionarybar.cc
219 lines (167 loc) · 5.55 KB
/
dictionarybar.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "dictionarybar.hh"
#include <QAction>
#include <QApplication>
#include <QMenu>
#include <QContextMenuEvent>
#include "dprintf.hh"
using std::vector;
DictionaryBar::DictionaryBar( QWidget * parent,
Config::MutedDictionaries & mutedDictionaries_,
Config::Events & events ):
QToolBar( tr( "Dictionary Bar" ), parent ),
mutedDictionaries( mutedDictionaries_ ),
configEvents( events )
{
setObjectName( "dictionaryBar" );
connect( &events, SIGNAL( mutedDictionariesChanged() ),
this, SLOT( mutedDictionariesChanged() ) );
connect( this, SIGNAL(actionTriggered(QAction*)),
this, SLOT(actionWasTriggered(QAction*)) );
}
static QString elideDictName( QString const & name )
{
// Some names are way too long -- we insert an ellipsis in the middle of those
int const maxSize = 33;
if ( name.size() <= maxSize )
return name;
int const pieceSize = maxSize / 2 - 1;
return name.left( pieceSize ) + QChar( 0x2026 ) + name.right( pieceSize );
}
void DictionaryBar::setDictionaries( vector< sptr< Dictionary::Class > >
const & dictionaries )
{
setUpdatesEnabled( false );
clear();
dictActions.clear();
bool use14x21 = false;
for( unsigned x = 0; x < dictionaries.size(); ++x )
{
QIcon icon = dictionaries[ x ]->getNativeIcon();
QString dictName = QString::fromUtf8( dictionaries[ x ]->
getName().c_str() );
QAction * action = addAction( icon, elideDictName( dictName ) );
action->setToolTip( dictName ); // Tooltip need not be shortened
QString id = QString::fromStdString( dictionaries[ x ]->getId() );
action->setData( id );
action->setCheckable( true );
action->setChecked( !mutedDictionaries.contains( id ) );
QList< QSize > sizes = icon.availableSizes();
for( QList< QSize >::iterator i = sizes.begin(); i != sizes.end();
++i )
if ( i->width() == 14 && i->height() == 21 )
use14x21 = true;
dictActions.append( action );
}
setIconSize( use14x21 ? QSize( 14, 21 ) : QSize( 21, 21 ) );
setUpdatesEnabled( true );
}
void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
{
QMenu menu( this );
for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
{
// We need new action, since the one we have has text elided
QAction * action = menu.addAction( (*i)->icon(), (*i)->toolTip() );
action->setCheckable( true );
action->setChecked( (*i)->isChecked() );
action->setData( QVariant::fromValue( (void *)*i ) );
// Force "icon in menu" on all platforms, for
// usability reasons.
action->setIconVisibleInMenu( true );
}
if ( !menu.isEmpty() )
menu.addSeparator();
QAction * editAction =
menu.addAction( QIcon( ":/icons/bookcase.png" ), tr( "Edit this group" ) );
QAction * result = menu.exec( event->globalPos() );
if ( result == editAction )
emit editGroupRequested();
else
if ( result && result->data().value< void * >() )
( ( QAction * )( result->data().value< void * >() ) )->trigger();
event->accept();
}
void DictionaryBar::mutedDictionariesChanged()
{
//DPRINTF( "Muted dictionaries changed\n" );
// Update actions
setUpdatesEnabled( false );
for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
{
bool isUnmuted = !mutedDictionaries.contains( (*i)->data().toString() );
if ( isUnmuted != (*i)->isChecked() )
(*i)->setChecked( isUnmuted );
}
setUpdatesEnabled( true );
}
void DictionaryBar::actionWasTriggered( QAction * action )
{
QString id = action->data().toString();
if ( id.isEmpty() )
return; // Some weird action, not our button
if ( QApplication::keyboardModifiers() &
( Qt::ControlModifier | Qt::ShiftModifier ) )
{
// Solo mode -- either use the dictionary exclusively, or toggle
// back all dictionaries if we do that already.
// Are we solo already?
bool isSolo = true;
// For solo, all dictionaries must be unchecked, since we're handling
// the result of the dictionary being (un)checked, and in case we were
// in solo, now we would end up with no dictionaries being checked at all.
for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
{
if ( (*i)->isChecked() )
{
isSolo = false;
break;
}
}
if ( isSolo )
{
// Toggle back all the dictionaries
for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
mutedDictionaries.remove( (*i)->data().toString() );
}
else
{
// Make dictionary solo
for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
{
QString dictId = (*i)->data().toString();
if ( dictId == id )
mutedDictionaries.remove( dictId );
else
mutedDictionaries.insert( dictId );
}
}
configEvents.signalMutedDictionariesChanged();
}
else
{
// Normal mode
if ( action->isChecked() )
{
// Unmute the dictionary
if ( mutedDictionaries.contains( id ) )
{
mutedDictionaries.remove( id );
configEvents.signalMutedDictionariesChanged();
}
}
else
{
// Mute the dictionary
if ( !mutedDictionaries.contains( id ) )
{
mutedDictionaries.insert( id );
configEvents.signalMutedDictionariesChanged();
}
}
}
}