File indexing completed on 2024-05-19 15:27:50

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 /* This file was part of the KDE project
0020    Copyright (C) 2005 Jarosław Staniek <staniek@kde.org>
0021 
0022    This program is free software; you can redistribute it and/or
0023    modify it under the terms of the GNU Library General Public
0024    License as published by the Free Software Foundation; either
0025    version 2 of the License, or (at your option) any later version.
0026  */
0027 
0028 #include "kgraphviewerlib_debug.h"
0029 #include <KSharedConfig>
0030 #include <KgvGlobal.h>
0031 #include <QApplication>
0032 #include <QDebug>
0033 #include <QPaintDevice>
0034 #include <QStandardPaths>
0035 #include <qfont.h>
0036 #include <qfontinfo.h>
0037 // #include <k3staticdeleter.h>
0038 #include <QIcon>
0039 #include <QImage>
0040 
0041 KgvGlobal *KgvGlobal::s_global = nullptr;
0042 // static K3StaticDeleter<KgvGlobal> sdg;
0043 
0044 KgvGlobal *KgvGlobal::self()
0045 {
0046     if (!s_global)
0047         s_global = new KgvGlobal;
0048     //         sdg.setObject( s_global, new KgvGlobal );
0049     return s_global;
0050 }
0051 
0052 KgvGlobal::KgvGlobal()
0053     : m_pointSize(-1)
0054     , m_kofficeConfig(nullptr)
0055 {
0056     // Install the libkoffice* translations
0057     //     KGlobal::locale()->insertCatalogue("koffice");
0058 
0059     // Tell the iconloader about share/apps/koffice/icons
0060     //     KGlobal::iconLoader()->addAppDir("koffice");
0061 
0062     // Another way to get the DPI of the display would be QPaintDeviceMetrics,
0063     // but we have no widget here (and moving this to KgvView wouldn't allow
0064     // using this from the document easily).
0065 }
0066 
0067 KgvGlobal::~KgvGlobal()
0068 {
0069     delete m_kofficeConfig;
0070 }
0071 
0072 QFont KgvGlobal::_defaultFont()
0073 {
0074     QFont font = qApp->font();
0075     // we have to use QFontInfo, in case the font was specified with a pixel size
0076     if (font.pointSize() == -1) {
0077         // cache size into m_pointSize, since QFontInfo loads the font -> slow
0078         if (m_pointSize == -1)
0079             m_pointSize = QFontInfo(font).pointSize();
0080         Q_ASSERT(m_pointSize != -1);
0081         font.setPointSize(m_pointSize);
0082     }
0083     // qCDebug(KGRAPHVIEWERLIB_LOG)<<"QFontInfo(font).pointSize() :"<<QFontInfo(font).pointSize();
0084     // qCDebug(KGRAPHVIEWERLIB_LOG)<<"font.name() :"<<font.family ();
0085     return font;
0086 }
0087 
0088 // QStringList KgvGlobal::_listOfLanguageTags()
0089 // {
0090 //     if ( m_langMap.isEmpty() )
0091 //         createListOfLanguages();
0092 //     return m_langMap.values();
0093 // }
0094 
0095 // QStringList KgvGlobal::_listOfLanguages()
0096 // {
0097 //     if ( m_langMap.empty() )
0098 //         createListOfLanguages();
0099 //     return m_langMap.keys();
0100 // }
0101 
0102 // void KgvGlobal::createListOfLanguages()
0103 // {
0104 // //     KConfig config( "all_languages", true, false, "locale" );
0105 //     KConfig config( "locale" );
0106 //     // Note that we could also use KLocale::allLanguagesTwoAlpha
0107 //
0108 //     QMap<QString, bool> seenLanguages;
0109 //     const QStringList langlist = config.groupList();
0110 //     for ( QStringList::ConstIterator itall = langlist.begin();
0111 //           itall != langlist.end(); ++itall )
0112 //     {
0113 //         const QString tag = *itall;
0114 //         config.setGroup( tag );
0115 //         const QString name = config.readEntry("Name", tag);
0116 //         // e.g. name is "French" and tag is "fr"
0117 //
0118 //         // The QMap does the sorting on the display-name, so that
0119 //         // comboboxes are sorted.
0120 //         m_langMap.insert( name, tag );
0121 //
0122 //         seenLanguages.insert( tag, true );
0123 //     }
0124 //
0125 //     // Also take a look at the installed translations.
0126 //     // Many of them are already in all_languages but all_languages doesn't
0127 //     // currently have en_GB or en_US etc.
0128 //
0129 //     const QStringList translationList = KGlobal::dirs()->findAllResources("locale",
0130 //                                                             QString::fromLatin1("*/entry.desktop"));
0131 //     for ( QStringList::ConstIterator it = translationList.begin();
0132 //           it != translationList.end(); ++it )
0133 //     {
0134 //         // Extract the language tag from the directory name
0135 //         QString tag = *it;
0136 //         int index = tag.findRev('/');
0137 //         tag = tag.left(index);
0138 //         index = tag.findRev('/');
0139 //         tag = tag.mid(index+1);
0140 //
0141 //         if ( seenLanguages.find( tag ) == seenLanguages.end() ) {
0142 // //             KSimpleConfig entry(*it);
0143 // //             entry.setGroup("KCM Locale");
0144 //
0145 // //             const QString name = entry.readEntry("Name", tag);
0146 //             // e.g. name is "US English" and tag is "en_US"
0147 // //             m_langMap.insert( name, tag );
0148 //
0149 //             // enable this if writing a third way of finding languages below
0150 //             //seenLanguages.insert( tag, true );
0151 //         }
0152 //
0153 //     }
0154 //
0155 //     // #### We also might not have an entry for a language where spellchecking is supported,
0156 //     //      but no KDE translation is available, like fr_CA.
0157 //     // How to add them?
0158 // }
0159 
0160 QString KgvGlobal::tagOfLanguage(const QString &_lang)
0161 {
0162     const LanguageMap &map = self()->m_langMap;
0163     QMap<QString, QString>::ConstIterator it = map.find(_lang);
0164     if (it != map.end())
0165         return *it;
0166     return QString();
0167 }
0168 
0169 QString KgvGlobal::languageFromTag(const QString &langTag)
0170 {
0171     const LanguageMap &map = self()->m_langMap;
0172     QMap<QString, QString>::ConstIterator it = map.begin();
0173     const QMap<QString, QString>::ConstIterator end = map.end();
0174     for (; it != end; ++it)
0175         if (it.value() == langTag)
0176             return it.key();
0177 
0178     // Language code not found. Better return the code (tag) than nothing.
0179     return langTag;
0180 }
0181 
0182 KConfig *KgvGlobal::_kofficeConfig()
0183 {
0184     if (!m_kofficeConfig) {
0185         m_kofficeConfig = new KConfig("kofficerc");
0186     }
0187     return m_kofficeConfig;
0188 }