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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/*
* This file is part of the KDE Libraries
* Copyright (C) 2007 Krzysztof Lichota ([email protected])
*
* 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 "kswitchlanguagedialog.h"
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqlabel.h>
#include <tqmap.h>
#include <klanguagebutton.h>
#include <kconfig.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kpushbutton.h>
struct LanguageRowData
{
TQLabel *label;
KLanguageButton *languageButton;
KPushButton *removeButton;
void setRowWidgets(
TQLabel *label,
KLanguageButton *languageButton,
KPushButton *removeButton
)
{
this->label = label;
this->languageButton = languageButton;
this->removeButton = removeButton;
}
};
class KSwitchLanguageDialogPrivate
{
public:
KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent);
KSwitchLanguageDialog *p; //parent class
/**
Fills language button with names of languages for which given application has translation.
*/
void fillApplicationLanguages(KLanguageButton *button);
/**
Adds one button with language to widget.
*/
void addLanguageButton(const TQString & languageCode, bool primaryLanguage);
/**
Returns list of languages chosen for application or default languages is they are not set.
*/
TQStringList applicationLanguageList();
TQMap<KPushButton*, LanguageRowData> languageRows;
TQPtrList<KLanguageButton> languageButtons;
TQGridLayout *languagesLayout;
TQWidget *page;
};
/*************************** KSwitchLanguageDialog **************************/
KSwitchLanguageDialog::KSwitchLanguageDialog(
TQWidget *parent,
const char *name,
bool modal
):
KDialogBase(parent, name, modal, i18n("Switch application language"), Ok|Cancel, Ok, true ),
d(new KSwitchLanguageDialogPrivate(this))
{
d->page = new TQWidget( this );
setMainWidget(d->page);
TQVBoxLayout *topLayout = new TQVBoxLayout( d->page, 0, spacingHint() );
TQLabel *label = new TQLabel( i18n("Please choose language which should be used for this application"), d->page, "label1" );
topLayout->addWidget( label );
TQHBoxLayout *languageHorizontalLayout = new TQHBoxLayout();
topLayout->addLayout(languageHorizontalLayout);
d->languagesLayout = new TQGridLayout(0 , 2);
languageHorizontalLayout->addLayout(d->languagesLayout);
languageHorizontalLayout->addStretch();
TQStringList defaultLanguages = d->applicationLanguageList();
for ( TQStringList::ConstIterator it = defaultLanguages.begin(); it != defaultLanguages.end(); ++it )
{
TQString language = *it;
bool primaryLanguage = (it == defaultLanguages.begin());
d->addLanguageButton(language, primaryLanguage);
}
if (defaultLanguages.count() == 0)
{
d->addLanguageButton(KGlobal::locale()->defaultLanguage(), true);
}
TQHBoxLayout *addButtonHorizontalLayout = new TQHBoxLayout();
topLayout->addLayout(addButtonHorizontalLayout);
KPushButton *addLangButton = new KPushButton(i18n("Add fallback language"), d->page, "addLangButton");
TQToolTip::add(addLangButton, i18n("Adds one more language which will be used if other translations do not contain proper translation"));
connect(addLangButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddLanguageButton()));
addButtonHorizontalLayout->addWidget(addLangButton);
addButtonHorizontalLayout->addStretch();
topLayout->addStretch(10);
}
KSwitchLanguageDialog::~KSwitchLanguageDialog()
{
delete this->d;
}
void KSwitchLanguageDialog::slotAddLanguageButton()
{
//adding new button with en_US as it should always be present
d->addLanguageButton("en_US", d->languageButtons.isEmpty() ? true : false);
}
void KSwitchLanguageDialog::removeButtonClicked()
{
TQObject const *signalSender = sender();
if (signalSender == NULL)
{
kdError() << "KSwitchLanguageDialog::removeButtonClicked() called directly, not using signal";
return;
}
KPushButton *removeButton = const_cast<KPushButton*>(::qt_cast<const KPushButton*>(signalSender));
if (removeButton == NULL)
{
kdError() << "KSwitchLanguageDialog::removeButtonClicked() called from something else than KPushButton";
return;
}
TQMap<KPushButton *, LanguageRowData>::iterator it = d->languageRows.find(removeButton);
if (it == d->languageRows.end())
{
kdError() << "KSwitchLanguageDialog::removeButtonClicked called from unknown KPushButton";
return;
}
LanguageRowData languageRowData = it.data();
d->languageButtons.removeRef(languageRowData.languageButton);
languageRowData.label->deleteLater();
languageRowData.languageButton->deleteLater();
languageRowData.removeButton->deleteLater();
d->languageRows.erase(it);
}
void KSwitchLanguageDialog::languageOnButtonChanged(const TQString & languageCode)
{
for ( TQPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it )
{
KLanguageButton *languageButton = *it;
if (languageButton->current() == languageCode)
{
//update all buttons which have matching id
//might update buttons which were not changed, but well...
languageButton->setText(KGlobal::locale()->twoAlphaToLanguageName(languageCode));
}
}
}
void KSwitchLanguageDialog::slotOk()
{
TQString languageString;
bool first = true;
for ( TQPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it )
{
KLanguageButton *languageButton = *it;
if (first == false)
{
languageString += ':';
}
languageString += languageButton->current();
first = false;
}
KConfig *config = KGlobal::config();
if (d->applicationLanguageList().join(":") != languageString)
{
//list is different from defaults or saved languages list
KConfigGroup group(config, "Locale");
group.writeEntry("Language", languageString);
config->sync();
KMessageBox::information(
this,
i18n("Language for this application has been changed. The change will take effect upon next start of application"), //text
i18n("Application language changed"), //caption
"ApplicationLanguageChangedWarning" //dontShowAgainName
);
}
emit okClicked();
accept();
}
/************************ KSwitchLanguageDialogPrivate ***********************/
KSwitchLanguageDialogPrivate::KSwitchLanguageDialogPrivate(
KSwitchLanguageDialog *parent
):
p(parent)
{
//NOTE: do NOT use "p" in constructor, it is not fully constructed
}
void KSwitchLanguageDialogPrivate::fillApplicationLanguages(KLanguageButton *button)
{
KLocale *locale = KGlobal::locale();
TQStringList allLanguages = locale->allLanguagesTwoAlpha();
for ( TQStringList::ConstIterator it = allLanguages.begin(); it != allLanguages.end(); ++it )
{
TQString languageCode = *it;
if (locale->isApplicationTranslatedInto(languageCode))
{
button->insertItem(
locale->twoAlphaToLanguageName(languageCode),
languageCode
);
}
}
}
TQStringList KSwitchLanguageDialogPrivate::applicationLanguageList()
{
KConfig *config = KGlobal::config();
TQStringList languagesList;
if (config->hasGroup("Locale"))
{
KConfigGroupSaver saver(config, "Locale");
if (config->hasKey("Language"))
{
languagesList = config->readListEntry("Language", ':');
}
}
if (languagesList.empty())
{
languagesList = KGlobal::locale()->languageList();
}
return languagesList;
}
void KSwitchLanguageDialogPrivate::addLanguageButton(const TQString & languageCode, bool primaryLanguage)
{
TQString labelText = primaryLanguage ? i18n("Primary language:") : i18n("Fallback language:");
KLanguageButton *languageButton = new KLanguageButton(page);
languageButton->setText(KGlobal::locale()->twoAlphaToLanguageName(languageCode));
fillApplicationLanguages(languageButton);
languageButton->setCurrentItem(languageCode);
TQObject::connect(
languageButton,
TQT_SIGNAL(activated( const TQString &)),
p,
TQT_SLOT(languageOnButtonChanged(const TQString &))
);
LanguageRowData languageRowData;
KPushButton *removeButton = NULL;
if (primaryLanguage == false)
{
removeButton = new KPushButton(i18n("Remove"), page);
TQObject::connect(
removeButton,
TQT_SIGNAL(clicked()),
p,
TQT_SLOT(removeButtonClicked())
);
}
if (primaryLanguage)
{
TQToolTip::add(languageButton, i18n("This is main application language which will be used first before any other languages"));
}
else
{
TQToolTip::add(languageButton, i18n("This is language which will be used if any previous languages does not contain proper translation"));
}
int numRows = languagesLayout->numRows();
TQLabel *languageLabel = new TQLabel(labelText, page);
languagesLayout->addWidget( languageLabel, numRows + 1, 1, Qt::AlignAuto );
languagesLayout->addWidget( languageButton, numRows + 1, 2, Qt::AlignAuto );
if (primaryLanguage == false)
{
languagesLayout->addWidget( removeButton, numRows + 1, 3, Qt::AlignAuto );
languageRowData.setRowWidgets(
languageLabel,
languageButton,
removeButton
);
removeButton->show();
}
languageRows.insert(removeButton, languageRowData);
languageButtons.append(languageButton);
languageButton->show();
languageLabel->show();
}
#include "kswitchlanguagedialog.moc"
|