• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kswitchlanguagedialog.cpp
1/*
2 * This file is part of the KDE Libraries
3 * Copyright (C) 2007 Krzysztof Lichota (lichota@mimuw.edu.pl)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "kswitchlanguagedialog.h"
23
24#include <tqlayout.h>
25#include <tqtooltip.h>
26#include <tqlabel.h>
27#include <tqmap.h>
28
29#include <klanguagebutton.h>
30#include <tdeconfig.h>
31#include <tdelocale.h>
32#include <tdemessagebox.h>
33#include <kdebug.h>
34#include <kpushbutton.h>
35
36struct LanguageRowData
37{
38 TQLabel *label;
39 KLanguageButton *languageButton;
40 KPushButton *removeButton;
41
42 void setRowWidgets(
43 TQLabel *label,
44 KLanguageButton *languageButton,
45 KPushButton *removeButton
46 )
47 {
48 this->label = label;
49 this->languageButton = languageButton;
50 this->removeButton = removeButton;
51 }
52
53};
54
55class KSwitchLanguageDialogPrivate
56{
57public:
58 KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent);
59
60 KSwitchLanguageDialog *p; //parent class
61
65 void fillApplicationLanguages(KLanguageButton *button);
66
70 void addLanguageButton(const TQString & languageCode, bool primaryLanguage);
71
75 TQStringList applicationLanguageList();
76
77 TQMap<KPushButton*, LanguageRowData> languageRows;
78 TQPtrList<KLanguageButton> languageButtons;
79 TQGridLayout *languagesLayout;
80 TQWidget *page;
81};
82
83/*************************** KSwitchLanguageDialog **************************/
84
85KSwitchLanguageDialog::KSwitchLanguageDialog(
86 TQWidget *parent,
87 const char *name,
88 bool modal
89 ):
90 KDialogBase(parent, name, modal, i18n("Switch application language"), Ok|Cancel, Ok, true ),
91 d(new KSwitchLanguageDialogPrivate(this))
92{
93 d->page = new TQWidget( this );
94 setMainWidget(d->page);
95 TQVBoxLayout *topLayout = new TQVBoxLayout( d->page, 0, spacingHint() );
96 TQLabel *label = new TQLabel( i18n("Please choose language which should be used for this application"), d->page, "label1" );
97 topLayout->addWidget( label );
98
99 TQHBoxLayout *languageHorizontalLayout = new TQHBoxLayout();
100 topLayout->addLayout(languageHorizontalLayout);
101
102 d->languagesLayout = new TQGridLayout(0 , 2);
103 languageHorizontalLayout->addLayout(d->languagesLayout);
104 languageHorizontalLayout->addStretch();
105
106 TQStringList defaultLanguages = d->applicationLanguageList();
107
108 for ( TQStringList::ConstIterator it = defaultLanguages.begin(); it != defaultLanguages.end(); ++it )
109 {
110 TQString language = *it;
111 bool primaryLanguage = (it == defaultLanguages.begin());
112 d->addLanguageButton(language, primaryLanguage);
113 }
114
115 if (defaultLanguages.count() == 0)
116 {
117 d->addLanguageButton(TDEGlobal::locale()->defaultLanguage(), true);
118 }
119
120 TQHBoxLayout *addButtonHorizontalLayout = new TQHBoxLayout();
121 topLayout->addLayout(addButtonHorizontalLayout);
122
123 KPushButton *addLangButton = new KPushButton(i18n("Add fallback language"), d->page, "addLangButton");
124 TQToolTip::add(addLangButton, i18n("Adds one more language which will be used if other translations do not contain proper translation"));
125 connect(addLangButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotAddLanguageButton()));
126 addButtonHorizontalLayout->addWidget(addLangButton);
127 addButtonHorizontalLayout->addStretch();
128
129 topLayout->addStretch(10);
130}
131
132KSwitchLanguageDialog::~KSwitchLanguageDialog()
133{
134 delete this->d;
135}
136
137void KSwitchLanguageDialog::slotAddLanguageButton()
138{
139 //adding new button with en_US as it should always be present
140 d->addLanguageButton("en_US", d->languageButtons.isEmpty() ? true : false);
141}
142
143void KSwitchLanguageDialog::removeButtonClicked()
144{
145 TQObject const *signalSender = sender();
146
147 if (signalSender == NULL)
148 {
149 kdError() << "KSwitchLanguageDialog::removeButtonClicked() called directly, not using signal";
150 return;
151 }
152
153 KPushButton *removeButton = const_cast<KPushButton*>(::tqt_cast<const KPushButton*>(signalSender));
154
155 if (removeButton == NULL)
156 {
157 kdError() << "KSwitchLanguageDialog::removeButtonClicked() called from something else than KPushButton";
158 return;
159 }
160
161 TQMap<KPushButton *, LanguageRowData>::iterator it = d->languageRows.find(removeButton);
162
163 if (it == d->languageRows.end())
164 {
165 kdError() << "KSwitchLanguageDialog::removeButtonClicked called from unknown KPushButton";
166 return;
167 }
168
169 LanguageRowData languageRowData = it.data();
170
171 d->languageButtons.removeRef(languageRowData.languageButton);
172
173 languageRowData.label->deleteLater();
174 languageRowData.languageButton->deleteLater();
175 languageRowData.removeButton->deleteLater();
176 d->languageRows.erase(it);
177}
178
179void KSwitchLanguageDialog::languageOnButtonChanged(const TQString & languageCode)
180{
181 for ( TQPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it )
182 {
183 KLanguageButton *languageButton = *it;
184 if (languageButton->current() == languageCode)
185 {
186 //update all buttons which have matching id
187 //might update buttons which were not changed, but well...
188 languageButton->setText(TDEGlobal::locale()->twoAlphaToLanguageName(languageCode));
189 }
190 }
191}
192
193void KSwitchLanguageDialog::slotOk()
194{
195 TQString languageString;
196 bool first = true;
197
198 for ( TQPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it )
199 {
200 KLanguageButton *languageButton = *it;
201
202 if (first == false)
203 {
204 languageString += ':';
205 }
206 languageString += languageButton->current();
207 first = false;
208 }
209
210 TDEConfig *config = TDEGlobal::config();
211
212 if (d->applicationLanguageList().join(":") != languageString)
213 {
214 //list is different from defaults or saved languages list
215 TDEConfigGroup group(config, "Locale");
216
217 group.writeEntry("Language", languageString);
218 config->sync();
219
220 KMessageBox::information(
221 this,
222 i18n("Language for this application has been changed. The change will take effect upon next start of application"), //text
223 i18n("Application language changed"), //caption
224 "ApplicationLanguageChangedWarning" //dontShowAgainName
225 );
226 }
227
228 emit okClicked();
229 accept();
230}
231
232/************************ KSwitchLanguageDialogPrivate ***********************/
233
234KSwitchLanguageDialogPrivate::KSwitchLanguageDialogPrivate(
235 KSwitchLanguageDialog *parent
236 ):
237 p(parent)
238{
239 //NOTE: do NOT use "p" in constructor, it is not fully constructed
240}
241
242void KSwitchLanguageDialogPrivate::fillApplicationLanguages(KLanguageButton *button)
243{
244 TDELocale *locale = TDEGlobal::locale();
245 TQStringList allLanguages = locale->allLanguagesTwoAlpha();
246 for ( TQStringList::ConstIterator it = allLanguages.begin(); it != allLanguages.end(); ++it )
247 {
248 TQString languageCode = *it;
249 if (locale->isApplicationTranslatedInto(languageCode))
250 {
251 button->insertItem(
252 locale->twoAlphaToLanguageName(languageCode),
253 languageCode
254 );
255 }
256 }
257}
258
259TQStringList KSwitchLanguageDialogPrivate::applicationLanguageList()
260{
261 TDEConfig *config = TDEGlobal::config();
262 TQStringList languagesList;
263
264 if (config->hasGroup("Locale"))
265 {
266 TDEConfigGroupSaver saver(config, "Locale");
267
268 if (config->hasKey("Language"))
269 {
270 languagesList = config->readListEntry("Language", ':');
271 }
272 }
273 if (languagesList.empty())
274 {
275 languagesList = TDEGlobal::locale()->languageList();
276 }
277 return languagesList;
278}
279
280void KSwitchLanguageDialogPrivate::addLanguageButton(const TQString & languageCode, bool primaryLanguage)
281{
282 TQString labelText = primaryLanguage ? i18n("Primary language:") : i18n("Fallback language:");
283
284 KLanguageButton *languageButton = new KLanguageButton(page);
285
286 languageButton->setText(TDEGlobal::locale()->twoAlphaToLanguageName(languageCode));
287
288 fillApplicationLanguages(languageButton);
289
290 languageButton->setCurrentItem(languageCode);
291
292 TQObject::connect(
293 languageButton,
294 TQ_SIGNAL(activated( const TQString &)),
295 p,
296 TQ_SLOT(languageOnButtonChanged(const TQString &))
297 );
298
299 LanguageRowData languageRowData;
300 KPushButton *removeButton = NULL;
301
302 if (primaryLanguage == false)
303 {
304 removeButton = new KPushButton(i18n("Remove"), page);
305
306 TQObject::connect(
307 removeButton,
308 TQ_SIGNAL(clicked()),
309 p,
310 TQ_SLOT(removeButtonClicked())
311 );
312 }
313
314 if (primaryLanguage)
315 {
316 TQToolTip::add(languageButton, i18n("This is main application language which will be used first before any other languages"));
317 }
318 else
319 {
320 TQToolTip::add(languageButton, i18n("This is language which will be used if any previous languages does not contain proper translation"));
321 }
322
323 int numRows = languagesLayout->numRows();
324
325 TQLabel *languageLabel = new TQLabel(labelText, page);
326 languagesLayout->addWidget( languageLabel, numRows + 1, 1, (TQt::AlignmentFlags)TQt::AlignAuto );
327 languagesLayout->addWidget( languageButton, numRows + 1, 2, (TQt::AlignmentFlags)TQt::AlignAuto );
328
329 if (primaryLanguage == false)
330 {
331 languagesLayout->addWidget( removeButton, numRows + 1, 3, (TQt::AlignmentFlags)TQt::AlignAuto );
332
333 languageRowData.setRowWidgets(
334 languageLabel,
335 languageButton,
336 removeButton
337 );
338 removeButton->show();
339 }
340
341 languageRows.insert(removeButton, languageRowData);
342
343 languageButtons.append(languageButton);
344 languageButton->show();
345 languageLabel->show();
346}
347
348#include "kswitchlanguagedialog.moc"
349
KDialogBase
A dialog base class with standard buttons and predefined layouts.
Definition: kdialogbase.h:192
KDialogBase::setMainWidget
void setMainWidget(TQWidget *widget)
Sets the main user definable widget.
Definition: kdialogbase.cpp:1431
KDialogBase::okClicked
void okClicked()
The OK button was pressed.
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition: kdialog.cpp:110
KLanguageButton
KLanguageButton provides a combobox with a 2-D dataset.
Definition: klanguagebutton.h:48
KLanguageButton::setText
void setText(const TQString &text)
Changes the current text item of the combobox, and makes the text static.
Definition: klanguagebutton.cpp:98
KLanguageButton::current
TQString current() const
Returns the id of the combobox's current item.
Definition: klanguagebutton.cpp:238
KLanguageButton::setCurrentItem
void setCurrentItem(const TQString &id)
Sets id as current item.
Definition: klanguagebutton.cpp:277
KLanguageButton::insertItem
void insertItem(const TQIconSet &icon, const TQString &text, const TQString &id, const TQString &submenu=TQString::null, int index=-1)
Inserts an item into the combo box.
Definition: klanguagebutton.cpp:144
KMessageBox::information
static void information(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const TQString &dontShowAgainName=TQString::null, int options=Notify)
Display an "Information" dialog.
Definition: tdemessagebox.cpp:883
KPushButton
This is nothing but a TQPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:38
KSwitchLanguageDialog
Standard "switch application language" dialog box.
Definition: kswitchlanguagedialog.h:39
KSwitchLanguageDialog::slotOk
virtual void slotOk()
Activated when the Ok button has been clicked.
Definition: kswitchlanguagedialog.cpp:193
KSwitchLanguageDialog::KSwitchLanguageDialog
KSwitchLanguageDialog(TQWidget *parent=0, const char *name=0, bool modal=true)
Constructor.
Definition: kswitchlanguagedialog.cpp:85
KSwitchLanguageDialog::slotAddLanguageButton
virtual void slotAddLanguageButton()
Called to add one language button to dialog.
Definition: kswitchlanguagedialog.cpp:137
KSwitchLanguageDialog::removeButtonClicked
virtual void removeButtonClicked()
Called when "Remove" language button is clicked.
Definition: kswitchlanguagedialog.cpp:143
KSwitchLanguageDialog::languageOnButtonChanged
virtual void languageOnButtonChanged(const TQString &)
Called when one of language buttons changes state.
Definition: kswitchlanguagedialog.cpp:179
TDEConfigBase::hasGroup
bool hasGroup(const TQString &group) const
TDEConfigBase::readListEntry
int readListEntry(const TQString &pKey, TQStrList &list, char sep=',') const
TDEConfigBase::sync
virtual void sync()
TDEConfigBase::hasKey
bool hasKey(const TQString &key) const
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigGroupSaver
TDEConfigGroup
TDEConfig
TDEGlobal::config
static TDEConfig * config()
TDEGlobal::locale
static TDELocale * locale()
TDELocale
TDELocale::languageList
TQStringList languageList() const
TDELocale::defaultLanguage
static TQString defaultLanguage()
TDELocale::allLanguagesTwoAlpha
TQStringList allLanguagesTwoAlpha() const
TDELocale::twoAlphaToLanguageName
TQString twoAlphaToLanguageName(const TQString &code) const
kdError
kdbgstream kdError(int area=0)
TDEStdAccel::label
TQString label(StdAccel id)
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.