| 1 |
/* This file is part of the KDE project |
| 2 |
SPDX-FileCopyrightText: 2001 David Faure <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2003 Nicolas GOUTTE <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
// clazy:excludeall=qstring-arg |
| 9 |
#include "KoGlobal.h" |
| 10 |
|
| 11 |
#include <config.h> |
| 12 |
#include <KoResourcePaths.h> |
| 13 |
|
| 14 |
#include <QPaintDevice> |
| 15 |
#include <QFont> |
| 16 |
#include <QFontInfo> |
| 17 |
#include <QFontDatabase> |
| 18 |
#include <QGlobalStatic> |
| 19 |
|
| 20 |
#include <WidgetsDebug.h> |
| 21 |
#include <KConfigGroup> |
| 22 |
#include <KLocalizedString> |
| 23 |
#include <KSharedConfig> |
| 24 |
#include <KConfig> |
| 25 |
|
| 26 |
Q_GLOBAL_STATIC(KoGlobal, s_instance) |
| 27 |
|
| 28 |
KoGlobal* KoGlobal::self() |
| 29 |
{ |
| 30 |
return s_instance; |
| 31 |
} |
| 32 |
|
| 33 |
KoGlobal::KoGlobal() |
| 34 |
: m_pointSize(-1) |
| 35 |
, m_planConfig(nullptr) |
| 36 |
{ |
| 37 |
// Fixes a bug where values from some config files are not picked up |
| 38 |
// due to KSharedConfig::openConfig() being initialized before paths have been set up above. |
| 39 |
// NOTE: Values set without a sync() call before KoGlobal has been initialized will not stick |
| 40 |
KSharedConfig::openConfig()->reparseConfiguration(); |
| 41 |
} |
| 42 |
|
| 43 |
KoGlobal::~KoGlobal() |
| 44 |
{ |
| 45 |
delete m_planConfig; |
| 46 |
} |
| 47 |
|
| 48 |
QFont KoGlobal::_defaultFont() |
| 49 |
{ |
| 50 |
QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont); |
| 51 |
// we have to use QFontInfo, in case the font was specified with a pixel size |
| 52 |
if (font.pointSize() == -1) { |
| 53 |
// cache size into m_pointSize, since QFontInfo loads the font -> slow |
| 54 |
if (m_pointSize == -1) |
| 55 |
m_pointSize = QFontInfo(font).pointSize(); |
| 56 |
Q_ASSERT(m_pointSize != -1); |
| 57 |
font.setPointSize(m_pointSize); |
| 58 |
} |
| 59 |
//debugWidgets<<"QFontInfo(font).pointSize() :"<<QFontInfo(font).pointSize(); |
| 60 |
//debugWidgets<<"font.name() :"<<font.family (); |
| 61 |
return font; |
| 62 |
} |
| 63 |
|
| 64 |
QStringList KoGlobal::_listOfLanguageTags() |
| 65 |
{ |
| 66 |
if (m_langMap.isEmpty()) |
| 67 |
createListOfLanguages(); |
| 68 |
return m_langMap.values(); |
| 69 |
} |
| 70 |
|
| 71 |
QStringList KoGlobal::_listOfLanguages() |
| 72 |
{ |
| 73 |
if (m_langMap.empty()) |
| 74 |
createListOfLanguages(); |
| 75 |
return m_langMap.keys(); |
| 76 |
} |
| 77 |
|
| 78 |
void KoGlobal::createListOfLanguages() |
| 79 |
{ |
| 80 |
KConfig config(QStringLiteral("all_languages"), KConfig::NoGlobals); |
| 81 |
|
| 82 |
QMap<QString, bool> seenLanguages; |
| 83 |
const QStringList langlist = config.groupList(); |
| 84 |
for (QStringList::ConstIterator itall = langlist.begin(); |
| 85 |
itall != langlist.end(); ++itall) { |
| 86 |
const QString tag = *itall; |
| 87 |
const QString name = config.group(tag).readEntry("Name", tag); |
| 88 |
// e.g. name is "French" and tag is "fr" |
| 89 |
|
| 90 |
// The QMap does the sorting on the display-name, so that |
| 91 |
// comboboxes are sorted. |
| 92 |
m_langMap.insert(name, tag); |
| 93 |
|
| 94 |
seenLanguages.insert(tag, true); |
| 95 |
} |
| 96 |
|
| 97 |
// Also take a look at the installed translations. |
| 98 |
// Many of them are already in all_languages but all_languages doesn't |
| 99 |
// currently have en_US (etc?). |
| 100 |
|
| 101 |
const QStringList translationList = KoResourcePaths::findAllResources("locale", |
| 102 |
QString::fromLatin1("*/kf5_entry.desktop")); |
| 103 |
for (QStringList::ConstIterator it = translationList.begin(); |
| 104 |
it != translationList.end(); ++it) { |
| 105 |
// Extract the language tag from the directory name |
| 106 |
QString tag = *it; |
| 107 |
int index = tag.lastIndexOf(QLatin1Char('/')); |
| 108 |
tag = tag.left(index); |
| 109 |
index = tag.lastIndexOf(QLatin1Char('/')); |
| 110 |
tag = tag.mid(index + 1); |
| 111 |
|
| 112 |
if (seenLanguages.find(tag) == seenLanguages.end()) { |
| 113 |
KConfig entry(*it, KConfig::SimpleConfig); |
| 114 |
|
| 115 |
const QString name = entry.group(QStringLiteral("KCM Locale")).readEntry("Name", tag); |
| 116 |
// e.g. name is "US English" and tag is "en_US" |
| 117 |
m_langMap.insert(name, tag); |
| 118 |
|
| 119 |
// enable this if writing a third way of finding languages below |
| 120 |
//seenLanguages.insert(tag, true); |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
// #### We also might not have an entry for a language where spellchecking is supported, |
| 126 |
// but no KDE translation is available, like fr_CA. |
| 127 |
// How to add them? |
| 128 |
} |
| 129 |
|
| 130 |
QString KoGlobal::tagOfLanguage(const QString & _lang) |
| 131 |
{ |
| 132 |
const LanguageMap& map = self()->m_langMap; |
| 133 |
QMap<QString, QString>::ConstIterator it = map.find(_lang); |
| 134 |
if (it != map.end()) |
| 135 |
return *it; |
| 136 |
return QString(); |
| 137 |
} |
| 138 |
|
| 139 |
QString KoGlobal::languageFromTag(const QString &langTag) |
| 140 |
{ |
| 141 |
const LanguageMap& map = self()->m_langMap; |
| 142 |
QMap<QString, QString>::ConstIterator it = map.begin(); |
| 143 |
const QMap<QString, QString>::ConstIterator end = map.end(); |
| 144 |
for (; it != end; ++it) |
| 145 |
if (it.value() == langTag) |
| 146 |
return it.key(); |
| 147 |
|
| 148 |
// Language code not found. Better return the code (tag) than nothing. |
| 149 |
return langTag; |
| 150 |
} |
| 151 |
|
| 152 |
KConfig* KoGlobal::_planConfig() |
| 153 |
{ |
| 154 |
if (!m_planConfig) { |
| 155 |
m_planConfig = new KConfig(QStringLiteral("calligraplanrc")); |
| 156 |
} |
| 157 |
return m_planConfig; |
| 158 |
} |