File indexing completed on 2024-05-12 04:44:35
0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com> 0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT 0003 0004 // Own headers 0005 // First the interface, which forces the header to be self-contained. 0006 #include "settranslation.h" 0007 0008 #include "initializetranslation.h" 0009 #include <optional> 0010 #include <qcoreapplication.h> 0011 #include <qdebug.h> 0012 #include <qglobal.h> 0013 #include <qthread.h> 0014 0015 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0016 #include <qlist.h> 0017 #endif 0018 0019 namespace PerceptualColor 0020 { 0021 0022 /** @brief Set the translation for the whole library. 0023 * 0024 * After calling this function, all objects of this library that are created 0025 * from now on are translated according to translation that was set. 0026 * 0027 * Objects that were yet existing when calling are <em>not always</em> 0028 * automatically updated: When calling this function, Qt sends 0029 * a <tt>QEvent::LanguageChange</tt> event only to top-level widgets, 0030 * and these will get updated then. You can send the event yourself 0031 * to non-top-level widgets to update those widgets also. Note that 0032 * also @ref RgbColorSpaceFactory generates objects that might have 0033 * localized properties; these objects do not support translation 0034 * updates. 0035 * 0036 * If you create objects that use translations <em>before</em> a translation 0037 * has been set explicitly, than automatically an environment-dependant 0038 * translation is loaded. 0039 * 0040 * You might call this function again after a change of 0041 * <tt>QLocale()</tt> to change the translation. Also, call this 0042 * function again after destroying the <tt>QCoreApplication</tt> 0043 * object and creating a new one. 0044 * 0045 * It is safe to call this function multiple times. 0046 * 0047 * @pre There exists exactly <em>one</em> instance of <tt>QCoreApplication</tt> 0048 * to which the parameter points. This function is called from the same thread 0049 * in which the <tt>QCoreApplication</tt> instance lives. 0050 * 0051 * @param instance A pointer to the <tt>QCoreApplication</tt> instance for 0052 * which the initialization will be done. 0053 * @param newUiLanguages List of translations, ordered by priority, 0054 * most important ones first, like in <tt>QLocale::uiLanguages()</tt>. */ 0055 void setTranslation(QCoreApplication *instance, const QStringList &newUiLanguages) 0056 { 0057 // Check of pre-conditions 0058 // The mutex lowers the risk when using QCoreApplication::instance() 0059 // and QThread::currentThread(), which are not explicitly documented 0060 // as thread-safe. 0061 if (instance == nullptr) { 0062 qWarning() // 0063 << __func__ // 0064 << "must not be called without a QCoreApplication object."; 0065 return; 0066 } 0067 if (QThread::currentThread() != QCoreApplication::instance()->thread()) { 0068 qWarning() // 0069 << __func__ // 0070 << "must not be called by any other thread " 0071 "except the QCoreApplication thread."; 0072 return; 0073 } 0074 0075 initializeTranslation(instance, std::optional<QStringList>(newUiLanguages)); 0076 } 0077 0078 } // namespace PerceptualColor