• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kdeui

kswitchlanguagedialog.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of the KDE Libraries
00003  * Copyright (C) 2007 Krzysztof Lichota (lichota@mimuw.edu.pl)
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  *
00020  */
00021 
00022 #include "kswitchlanguagedialog.h"
00023 
00024 #include <qlayout.h>
00025 #include <qtooltip.h>
00026 #include <qlabel.h>
00027 #include <qmap.h>
00028 
00029 #include <klanguagebutton.h>
00030 #include <kconfig.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <kdebug.h>
00034 #include <kpushbutton.h>
00035 
00036 struct LanguageRowData
00037 {
00038     QLabel *label;
00039     KLanguageButton *languageButton;
00040     KPushButton *removeButton;
00041     
00042     void setRowWidgets(
00043         QLabel *label,
00044         KLanguageButton *languageButton,
00045         KPushButton *removeButton
00046         )
00047     {
00048         this->label = label;
00049         this->languageButton = languageButton;
00050         this->removeButton = removeButton;
00051     }
00052     
00053 };
00054 
00055 class KSwitchLanguageDialogPrivate
00056 {
00057 public:
00058     KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent);
00059     
00060     KSwitchLanguageDialog *p; //parent class
00061     
00065     void fillApplicationLanguages(KLanguageButton *button);
00066     
00070     void addLanguageButton(const QString & languageCode, bool primaryLanguage);
00071     
00075     QStringList applicationLanguageList();
00076     
00077     QMap<KPushButton*, LanguageRowData> languageRows;
00078     QPtrList<KLanguageButton> languageButtons;
00079     QGridLayout *languagesLayout;
00080     QWidget *page;
00081 };
00082 
00083 /*************************** KSwitchLanguageDialog **************************/
00084 
00085 KSwitchLanguageDialog::KSwitchLanguageDialog(
00086     QWidget *parent, 
00087     const char *name, 
00088     bool modal
00089     ):
00090     KDialogBase(parent, name, modal, i18n("Switch application language"), Ok|Cancel, Ok, true ),
00091     d(new KSwitchLanguageDialogPrivate(this))
00092 {
00093     d->page = new QWidget( this );
00094     setMainWidget(d->page);
00095     QVBoxLayout *topLayout = new QVBoxLayout( d->page, 0, spacingHint() );
00096     QLabel *label = new QLabel( i18n("Please choose language which should be used for this application"), d->page, "label1" );
00097     topLayout->addWidget( label );
00098     
00099     QHBoxLayout *languageHorizontalLayout = new QHBoxLayout();
00100     topLayout->addLayout(languageHorizontalLayout);
00101     
00102     d->languagesLayout = new QGridLayout(0 , 2);
00103     languageHorizontalLayout->addLayout(d->languagesLayout);
00104     languageHorizontalLayout->addStretch();
00105     
00106     QStringList defaultLanguages = d->applicationLanguageList();
00107     
00108     for ( QStringList::ConstIterator it = defaultLanguages.begin(); it != defaultLanguages.end(); ++it )
00109     {
00110         QString language = *it;
00111         bool primaryLanguage = (it == defaultLanguages.begin());
00112         d->addLanguageButton(language, primaryLanguage);
00113     }
00114     
00115     if (defaultLanguages.count() == 0)
00116     {
00117         d->addLanguageButton(KGlobal::locale()->defaultLanguage(), true);
00118     }
00119     
00120     QHBoxLayout *addButtonHorizontalLayout = new QHBoxLayout();
00121     topLayout->addLayout(addButtonHorizontalLayout);
00122     
00123     KPushButton *addLangButton = new KPushButton(i18n("Add fallback language"), d->page, "addLangButton");
00124     QToolTip::add(addLangButton, i18n("Adds one more language which will be used if other translations do not contain proper translation"));
00125     connect(addLangButton, SIGNAL(clicked()), this, SLOT(slotAddLanguageButton()));
00126     addButtonHorizontalLayout->addWidget(addLangButton);
00127     addButtonHorizontalLayout->addStretch();
00128     
00129     topLayout->addStretch(10);
00130 }
00131 
00132 KSwitchLanguageDialog::~KSwitchLanguageDialog()
00133 {
00134     delete this->d;
00135 }
00136 
00137 void KSwitchLanguageDialog::slotAddLanguageButton()
00138 {
00139     //adding new button with en_US as it should always be present
00140     d->addLanguageButton("en_US", d->languageButtons.isEmpty() ? true : false);
00141 }
00142 
00143 void KSwitchLanguageDialog::removeButtonClicked()
00144 {
00145     QObject const *signalSender = sender();
00146     
00147     if (signalSender == NULL)
00148     {
00149         kdError() << "KSwitchLanguageDialog::removeButtonClicked() called directly, not using signal";
00150         return;
00151     }
00152     
00153     KPushButton *removeButton = const_cast<KPushButton*>(::qt_cast<const KPushButton*>(signalSender));
00154     
00155     if (removeButton == NULL)
00156     {
00157         kdError() << "KSwitchLanguageDialog::removeButtonClicked() called from something else than KPushButton";
00158         return;
00159     }
00160     
00161     QMap<KPushButton *, LanguageRowData>::iterator it = d->languageRows.find(removeButton);
00162     
00163     if (it == d->languageRows.end())
00164     {
00165         kdError() << "KSwitchLanguageDialog::removeButtonClicked called from unknown KPushButton";
00166         return;
00167     }
00168 
00169     LanguageRowData languageRowData = it.data();
00170     
00171     d->languageButtons.removeRef(languageRowData.languageButton);
00172     
00173     languageRowData.label->deleteLater();
00174     languageRowData.languageButton->deleteLater();
00175     languageRowData.removeButton->deleteLater();
00176     d->languageRows.erase(it);
00177 }
00178 
00179 void KSwitchLanguageDialog::languageOnButtonChanged(const QString & languageCode)
00180 {
00181     for ( QPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it )
00182     {
00183         KLanguageButton *languageButton = *it;
00184         if (languageButton->current() == languageCode)
00185         {
00186             //update all buttons which have matching id
00187             //might update buttons which were not changed, but well...
00188             languageButton->setText(KGlobal::locale()->twoAlphaToLanguageName(languageCode));
00189         }
00190     }
00191 }
00192 
00193 void KSwitchLanguageDialog::slotOk()
00194 {
00195     QString languageString;
00196     bool first = true;
00197     
00198     for ( QPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it )
00199     {
00200         KLanguageButton *languageButton = *it;
00201         
00202         if (first == false)
00203         {
00204             languageString += ':';
00205         }
00206         languageString += languageButton->current();
00207         first = false;
00208     }
00209     
00210     KConfig *config = KGlobal::config();
00211     
00212     if (d->applicationLanguageList().join(":") != languageString)
00213     {
00214         //list is different from defaults or saved languages list
00215         KConfigGroup group(config, "Locale");
00216     
00217         group.writeEntry("Language", languageString);
00218         config->sync();
00219         
00220         KMessageBox::information(
00221             this,
00222             i18n("Language for this application has been changed. The change will take effect upon next start of application"), //text
00223             i18n("Application language changed"), //caption
00224             "ApplicationLanguageChangedWarning" //dontShowAgainName
00225             );
00226     }
00227 
00228     emit okClicked();
00229     accept();
00230 }
00231 
00232 /************************ KSwitchLanguageDialogPrivate ***********************/
00233 
00234 KSwitchLanguageDialogPrivate::KSwitchLanguageDialogPrivate(
00235     KSwitchLanguageDialog *parent
00236     ):
00237     p(parent)
00238 {
00239     //NOTE: do NOT use "p" in constructor, it is not fully constructed
00240 }
00241 
00242 void KSwitchLanguageDialogPrivate::fillApplicationLanguages(KLanguageButton *button)
00243 {
00244     KLocale *locale = KGlobal::locale();
00245     QStringList allLanguages = locale->allLanguagesTwoAlpha();
00246     for ( QStringList::ConstIterator it = allLanguages.begin(); it != allLanguages.end(); ++it )
00247     {
00248         QString languageCode = *it;
00249         if (locale->isApplicationTranslatedInto(languageCode))
00250         {
00251             button->insertItem(
00252                 locale->twoAlphaToLanguageName(languageCode),
00253                 languageCode
00254                 );
00255         }
00256     }
00257 }
00258 
00259 QStringList KSwitchLanguageDialogPrivate::applicationLanguageList()
00260 {
00261     KConfig *config = KGlobal::config();
00262     QStringList languagesList;
00263     
00264     if (config->hasGroup("Locale"))
00265     {
00266         KConfigGroupSaver saver(config, "Locale");
00267         
00268         if (config->hasKey("Language"))
00269         {
00270             languagesList = config->readListEntry("Language", ':');
00271         }
00272     }
00273     if (languagesList.empty())
00274     {
00275         languagesList = KGlobal::locale()->languageList();
00276     }
00277     return languagesList;
00278 }
00279 
00280 void KSwitchLanguageDialogPrivate::addLanguageButton(const QString & languageCode, bool primaryLanguage)
00281 {
00282     QString labelText = primaryLanguage ? i18n("Primary language:") : i18n("Fallback language:");
00283     
00284     KLanguageButton *languageButton = new KLanguageButton(page);
00285     
00286     languageButton->setText(KGlobal::locale()->twoAlphaToLanguageName(languageCode));
00287     
00288     fillApplicationLanguages(languageButton);
00289     
00290     languageButton->setCurrentItem(languageCode);
00291     
00292     QObject::connect(
00293         languageButton, 
00294         SIGNAL(activated( const QString &)), 
00295         p,
00296         SLOT(languageOnButtonChanged(const QString &))
00297         );
00298     
00299     LanguageRowData languageRowData;
00300     KPushButton *removeButton = NULL;
00301     
00302     if (primaryLanguage == false)
00303     {
00304         removeButton = new KPushButton(i18n("Remove"), page);
00305         
00306         QObject::connect(
00307             removeButton, 
00308             SIGNAL(clicked()),
00309             p,
00310             SLOT(removeButtonClicked())
00311             );
00312     }
00313         
00314     if (primaryLanguage)
00315     {
00316         QToolTip::add(languageButton, i18n("This is main application language which will be used first before any other languages"));
00317     }
00318     else
00319     {
00320         QToolTip::add(languageButton, i18n("This is language which will be used if any previous languages does not contain proper translation"));
00321     }
00322     
00323     int numRows = languagesLayout->numRows();
00324     
00325     QLabel *languageLabel = new QLabel(labelText, page);
00326     languagesLayout->addWidget( languageLabel, numRows + 1, 1, Qt::AlignAuto );
00327     languagesLayout->addWidget( languageButton, numRows + 1, 2, Qt::AlignAuto );
00328     
00329     if (primaryLanguage == false)
00330     {
00331         languagesLayout->addWidget( removeButton, numRows + 1, 3, Qt::AlignAuto );
00332         
00333         languageRowData.setRowWidgets(
00334             languageLabel,
00335             languageButton,
00336             removeButton
00337             );
00338         removeButton->show();
00339     }
00340     
00341     languageRows.insert(removeButton, languageRowData);
00342     
00343     languageButtons.append(languageButton);
00344     languageButton->show();
00345     languageLabel->show();
00346 }
00347 
00348 #include "kswitchlanguagedialog.moc"
00349 

kdeui

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal