forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 1
/
editdictionaries.cc
234 lines (185 loc) · 7.35 KB
/
editdictionaries.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "editdictionaries.hh"
#include "loaddictionaries.hh"
#include <QMessageBox>
using std::vector;
EditDictionaries::EditDictionaries( QWidget * parent, Config::Class & cfg_,
vector< sptr< Dictionary::Class > > & dictionaries_,
Instances::Groups & groupInstances_,
QNetworkAccessManager & dictNetMgr_ ):
QDialog( parent ), cfg( cfg_ ), dictionaries( dictionaries_ ),
groupInstances( groupInstances_ ),
dictNetMgr( dictNetMgr_ ),
origCfg( cfg ),
sources( this, cfg.paths, cfg.soundDirs, cfg.hunspell, cfg.transliteration,
cfg.forvo, cfg.mediawikis, cfg.webSites, cfg.programs ),
orderAndProps( new OrderAndProps( this, cfg.dictionaryOrder, cfg.inactiveDictionaries,
dictionaries ) ),
groups( new Groups( this, dictionaries, cfg.groups, orderAndProps->getCurrentDictionaryOrder() ) ),
dictionariesChanged( false ),
groupsChanged( false ),
lastCurrentTab( 0 )
{
// Some groups may have contained links to non-existnent dictionaries. We
// would like to preserve them if no edits were done. To that end, we save
// the initial group readings so that if no edits were really done, we won't
// be changing groups.
origCfg.groups = groups->getGroups();
origCfg.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
ui.setupUi( this );
setWindowIcon( QIcon(":/icons/book.png") );
ui.tabs->clear();
ui.tabs->addTab( &sources, QIcon(":/icons/reload.png"), tr( "&Sources" ) );
ui.tabs->addTab( orderAndProps.get(), QIcon(":/icons/book.png"), tr( "&Dictionaries" ) );
ui.tabs->addTab( groups.get(), QIcon(":/icons/bookcase.png"), tr( "&Groups" ) );
connect( ui.buttons, SIGNAL( clicked( QAbstractButton * ) ),
this, SLOT( buttonBoxClicked( QAbstractButton * ) ) );
connect( &sources, SIGNAL( rescan() ), this, SLOT( rescanSources() ) );
}
void EditDictionaries::editGroup( unsigned id )
{
if ( id == Instances::Group::AllGroupId )
ui.tabs->setCurrentIndex( 1 );
else
{
ui.tabs->setCurrentIndex( 2 );
groups->editGroup( id );
}
}
void EditDictionaries::save()
{
Config::Groups newGroups = groups->getGroups();
Config::Group newOrder = orderAndProps->getCurrentDictionaryOrder();
Config::Group newInactive = orderAndProps->getCurrentInactiveDictionaries();
if ( isSourcesChanged() )
acceptChangedSources( false );
if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder ||
origCfg.inactiveDictionaries != newInactive )
{
groupsChanged = true;
cfg.groups = newGroups;
cfg.dictionaryOrder = newOrder;
cfg.inactiveDictionaries = newInactive;
}
}
void EditDictionaries::accept()
{
save();
QDialog::accept();
}
void EditDictionaries::on_tabs_currentChanged( int index )
{
if ( index == -1 || !isVisible() )
return; // Sent upon the construction/destruction
if ( !lastCurrentTab && index )
{
// We're switching away from the Sources tab -- if its contents were
// changed, we need to either apply or reject now.
if ( isSourcesChanged() )
{
ui.tabs->setCurrentIndex( 0 );
QMessageBox question( QMessageBox::Question, tr( "Sources changed" ),
tr( "Some sources were changed. Would you like to accept the changes?" ),
QMessageBox::NoButton, this );
QPushButton * accept = question.addButton( tr( "Accept" ), QMessageBox::AcceptRole );
question.addButton( tr( "Cancel" ), QMessageBox::RejectRole );
question.exec();
if ( question.clickedButton() == accept )
{
acceptChangedSources( true );
lastCurrentTab = index;
ui.tabs->setCurrentIndex( index );
}
else
{
// Prevent tab from switching
lastCurrentTab = 0;
return;
}
}
}
else
if ( lastCurrentTab == 1 && index != 1 )
{
// When switching from the dictionary order, we need to propagate any
// changes to the groups.
groups->updateDictionaryOrder( orderAndProps->getCurrentDictionaryOrder() );
}
lastCurrentTab = index;
}
void EditDictionaries::rescanSources()
{
acceptChangedSources( true );
}
void EditDictionaries::buttonBoxClicked( QAbstractButton * button )
{
if (ui.buttons->buttonRole(button) == QDialogButtonBox::ApplyRole) {
if ( isSourcesChanged() ) {
acceptChangedSources( true );
}
save();
}
}
bool EditDictionaries::isSourcesChanged() const
{
return sources.getPaths() != cfg.paths ||
sources.getSoundDirs() != cfg.soundDirs ||
sources.getHunspell() != cfg.hunspell ||
sources.getTransliteration() != cfg.transliteration ||
sources.getForvo() != cfg.forvo ||
sources.getMediaWikis() != cfg.mediawikis ||
sources.getWebSites() != cfg.webSites ||
sources.getPrograms() != cfg.programs;
}
void EditDictionaries::acceptChangedSources( bool rebuildGroups )
{
dictionariesChanged = true;
Config::Groups savedGroups = groups->getGroups();
Config::Group savedOrder = orderAndProps->getCurrentDictionaryOrder();
Config::Group savedInactive = orderAndProps->getCurrentInactiveDictionaries();
cfg.paths = sources.getPaths();
cfg.soundDirs = sources.getSoundDirs();
cfg.hunspell = sources.getHunspell();
cfg.transliteration = sources.getTransliteration();
cfg.forvo = sources.getForvo();
cfg.mediawikis = sources.getMediaWikis();
cfg.webSites = sources.getWebSites();
cfg.programs = sources.getPrograms();
groupInstances.clear(); // Those hold pointers to dictionaries, we need to
// free them.
ui.tabs->setUpdatesEnabled( false );
ui.tabs->removeTab( 1 );
ui.tabs->removeTab( 1 );
groups.reset();
orderAndProps.reset();
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
// If no changes to groups were made, update the original data
bool noGroupEdits = ( origCfg.groups == savedGroups );
if ( noGroupEdits )
savedGroups = cfg.groups;
Instances::updateNames( savedGroups, dictionaries );
bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder );
if ( noOrderEdits )
savedOrder = cfg.dictionaryOrder;
Instances::updateNames( savedOrder, dictionaries );
bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive );
if ( noInactiveEdits )
savedInactive = cfg.inactiveDictionaries;
Instances::updateNames( savedInactive, dictionaries );
if ( rebuildGroups )
{
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
ui.tabs->insertTab( 1, orderAndProps.get(), QIcon(":/icons/book.png"), tr( "&Dictionaries" ) );
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
ui.tabs->insertTab( 2, groups.get(), QIcon(":/icons/bookcase.png"), tr( "&Groups" ) );
ui.tabs->setUpdatesEnabled( true );
if ( noGroupEdits )
origCfg.groups = groups->getGroups();
if ( noOrderEdits )
origCfg.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
if ( noInactiveEdits )
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
}
}