Warning, file /office/calligra/libs/widgets/KoGlobal.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2001 David Faure <faure@kde.org>
0003    Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KoGlobal.h"
0022 
0023 #include <KoConfig.h>
0024 #include <KoResourcePaths.h>
0025 
0026 #include <QPaintDevice>
0027 #include <QFont>
0028 #include <QFontInfo>
0029 #include <QFontDatabase>
0030 #include <QGlobalStatic>
0031 
0032 #include <WidgetsDebug.h>
0033 #include <kconfiggroup.h>
0034 #include <klocalizedstring.h>
0035 #include <ksharedconfig.h>
0036 #include <kconfig.h>
0037 
0038 Q_GLOBAL_STATIC(KoGlobal, s_instance)
0039 
0040 KoGlobal* KoGlobal::self()
0041 {
0042     return s_instance;
0043 }
0044 
0045 KoGlobal::KoGlobal()
0046     : m_pointSize(-1)
0047     , m_calligraConfig(0)
0048 {
0049     // Fixes a bug where values from some config files are not picked up
0050     // due to  KSharedConfig::openConfig() being initialized before paths have been set up above.
0051     // NOTE: Values set without a sync() call before KoGlobal has been initialized will not stick
0052      KSharedConfig::openConfig()->reparseConfiguration();
0053 }
0054 
0055 KoGlobal::~KoGlobal()
0056 {
0057     delete m_calligraConfig;
0058 }
0059 
0060 QFont KoGlobal::_defaultFont()
0061 {
0062     QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0063     // we have to use QFontInfo, in case the font was specified with a pixel size
0064     if (font.pointSize() == -1) {
0065         // cache size into m_pointSize, since QFontInfo loads the font -> slow
0066         if (m_pointSize == -1)
0067             m_pointSize = QFontInfo(font).pointSize();
0068         Q_ASSERT(m_pointSize != -1);
0069         font.setPointSize(m_pointSize);
0070     }
0071     //debugWidgets<<"QFontInfo(font).pointSize() :"<<QFontInfo(font).pointSize();
0072     //debugWidgets<<"font.name() :"<<font.family ();
0073     return font;
0074 }
0075 
0076 QStringList KoGlobal::_listOfLanguageTags()
0077 {
0078     if (m_langMap.isEmpty())
0079         createListOfLanguages();
0080     return m_langMap.values();
0081 }
0082 
0083 QStringList KoGlobal::_listOfLanguages()
0084 {
0085     if (m_langMap.empty())
0086         createListOfLanguages();
0087     return m_langMap.keys();
0088 }
0089 
0090 void KoGlobal::createListOfLanguages()
0091 {
0092     KConfig config("all_languages", KConfig::NoGlobals);
0093     // Note that we could also use KLocale::allLanguagesTwoAlpha
0094 
0095     QMap<QString, bool> seenLanguages;
0096     const QStringList langlist = config.groupList();
0097     for (QStringList::ConstIterator itall = langlist.begin();
0098             itall != langlist.end(); ++itall) {
0099         const QString tag = *itall;
0100         const QString name = config.group(tag).readEntry("Name", tag);
0101         // e.g. name is "French" and tag is "fr"
0102 
0103         // The QMap does the sorting on the display-name, so that
0104         // comboboxes are sorted.
0105         m_langMap.insert(name, tag);
0106 
0107         seenLanguages.insert(tag, true);
0108     }
0109 
0110     // Also take a look at the installed translations.
0111     // Many of them are already in all_languages but all_languages doesn't
0112     // currently have en_GB or en_US etc.
0113 
0114     const QStringList translationList = KoResourcePaths::findAllResources("locale",
0115                                         QString::fromLatin1("*/kf5_entry.desktop"));
0116     for (QStringList::ConstIterator it = translationList.begin();
0117             it != translationList.end(); ++it) {
0118         // Extract the language tag from the directory name
0119         QString tag = *it;
0120         int index = tag.lastIndexOf('/');
0121         tag = tag.left(index);
0122         index = tag.lastIndexOf('/');
0123         tag = tag.mid(index + 1);
0124 
0125         if (seenLanguages.find(tag) == seenLanguages.end()) {
0126             KConfig entry(*it, KConfig::SimpleConfig);
0127 
0128             const QString name = entry.group("KCM Locale").readEntry("Name", tag);
0129             // e.g. name is "US English" and tag is "en_US"
0130             m_langMap.insert(name, tag);
0131 
0132             // enable this if writing a third way of finding languages below
0133             //seenLanguages.insert( tag, true );
0134         }
0135 
0136     }
0137 
0138     // #### We also might not have an entry for a language where spellchecking is supported,
0139     //      but no KDE translation is available, like fr_CA.
0140     // How to add them?
0141 }
0142 
0143 QString KoGlobal::tagOfLanguage(const QString & _lang)
0144 {
0145     const LanguageMap& map = self()->m_langMap;
0146     QMap<QString, QString>::ConstIterator it = map.find(_lang);
0147     if (it != map.end())
0148         return *it;
0149     return QString();
0150 }
0151 
0152 QString KoGlobal::languageFromTag(const QString &langTag)
0153 {
0154     const LanguageMap& map = self()->m_langMap;
0155     QMap<QString, QString>::ConstIterator it = map.begin();
0156     const QMap<QString, QString>::ConstIterator end = map.end();
0157     for (; it != end; ++it)
0158         if (it.value() == langTag)
0159             return it.key();
0160 
0161     // Language code not found. Better return the code (tag) than nothing.
0162     return langTag;
0163 }
0164 
0165 KConfig* KoGlobal::_calligraConfig()
0166 {
0167     if (!m_calligraConfig) {
0168         m_calligraConfig = new KConfig("calligrarc");
0169     }
0170     return m_calligraConfig;
0171 }