2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2020-06-18 08:49:29 +02:00
|
|
|
|
|
|
|
|
#include "qmlmultilanguageaspect.h"
|
|
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <extensionsystem/pluginspec.h>
|
|
|
|
|
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2022-04-11 09:53:55 +02:00
|
|
|
#include <projectexplorer/runcontrol.h>
|
2020-07-21 13:47:14 +02:00
|
|
|
#include <projectexplorer/session.h>
|
2020-06-18 08:49:29 +02:00
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
|
|
|
|
static bool isMultilanguagePresent()
|
|
|
|
|
{
|
2020-11-18 12:25:51 +01:00
|
|
|
const QVector<ExtensionSystem::PluginSpec *> &specs = ExtensionSystem::PluginManager::plugins();
|
|
|
|
|
return std::find_if(specs.cbegin(), specs.cend(),
|
2020-06-18 08:49:29 +02:00
|
|
|
[](ExtensionSystem::PluginSpec *spec) {
|
|
|
|
|
return spec->name() == "MultiLanguage";
|
|
|
|
|
})
|
2020-11-18 12:25:51 +01:00
|
|
|
!= specs.cend();
|
2020-06-18 08:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Utils::FilePath getMultilanguageDatabaseFilePath(ProjectExplorer::Target *target)
|
|
|
|
|
{
|
|
|
|
|
if (target) {
|
2021-10-13 15:50:23 +03:00
|
|
|
auto filePath = target->project()->projectDirectory().pathAppended("translations.db");
|
2020-06-18 08:49:29 +02:00
|
|
|
if (filePath.exists())
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QObject *getPreviewPlugin()
|
|
|
|
|
{
|
2020-11-18 12:25:51 +01:00
|
|
|
const QVector<ExtensionSystem::PluginSpec *> &specs = ExtensionSystem::PluginManager::plugins();
|
|
|
|
|
const auto pluginIt = std::find_if(specs.cbegin(), specs.cend(),
|
2020-06-18 08:49:29 +02:00
|
|
|
[](const ExtensionSystem::PluginSpec *p) {
|
|
|
|
|
return p->name() == "QmlPreview";
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-18 12:25:51 +01:00
|
|
|
if (pluginIt != specs.cend())
|
2020-06-18 08:49:29 +02:00
|
|
|
return (*pluginIt)->plugin();
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace QmlProjectManager {
|
|
|
|
|
|
|
|
|
|
QmlMultiLanguageAspect::QmlMultiLanguageAspect(ProjectExplorer::Target *target)
|
|
|
|
|
: m_target(target)
|
|
|
|
|
{
|
|
|
|
|
setVisible(isMultilanguagePresent());
|
|
|
|
|
setSettingsKey(Constants::USE_MULTILANGUAGE_KEY);
|
2022-10-21 13:39:05 +02:00
|
|
|
setLabel(tr("Use MultiLanguage in 2D view"), BoolAspect::LabelPlacement::AtCheckBox);
|
2022-02-16 17:09:37 +01:00
|
|
|
setToolTip(tr("Reads translations from MultiLanguage plugin."));
|
2020-06-18 08:49:29 +02:00
|
|
|
|
|
|
|
|
setDefaultValue(!databaseFilePath().isEmpty());
|
|
|
|
|
QVariantMap getDefaultValues;
|
|
|
|
|
fromMap(getDefaultValues);
|
2022-04-11 09:53:55 +02:00
|
|
|
|
|
|
|
|
addDataExtractor(this, &QmlMultiLanguageAspect::origin, &Data::origin);
|
|
|
|
|
|
2022-05-11 10:42:16 +02:00
|
|
|
connect(this, &BoolAspect::changed, this, [this] {
|
2022-04-11 09:53:55 +02:00
|
|
|
for (ProjectExplorer::RunControl *runControl :
|
|
|
|
|
ProjectExplorer::ProjectExplorerPlugin::allRunControls()) {
|
|
|
|
|
if (runControl->aspect<QmlMultiLanguageAspect>()->origin == this)
|
|
|
|
|
runControl->initiateStop();
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-06-18 08:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlMultiLanguageAspect::~QmlMultiLanguageAspect()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 20:45:08 +02:00
|
|
|
void QmlMultiLanguageAspect::setCurrentLocale(const QString &locale)
|
2020-06-18 08:49:29 +02:00
|
|
|
{
|
2020-07-20 20:45:08 +02:00
|
|
|
if (m_currentLocale == locale)
|
|
|
|
|
return;
|
|
|
|
|
m_currentLocale = locale;
|
2020-06-18 08:49:29 +02:00
|
|
|
if (auto previewPlugin = getPreviewPlugin())
|
2020-10-14 14:02:49 +02:00
|
|
|
previewPlugin->setProperty("localeIsoCode", locale);
|
2020-06-18 08:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 20:45:08 +02:00
|
|
|
QString QmlMultiLanguageAspect::currentLocale() const
|
2020-06-18 08:49:29 +02:00
|
|
|
{
|
2020-07-20 20:45:08 +02:00
|
|
|
return m_currentLocale;
|
2020-06-18 08:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::FilePath QmlMultiLanguageAspect::databaseFilePath() const
|
|
|
|
|
{
|
|
|
|
|
if (m_databaseFilePath.isEmpty())
|
|
|
|
|
m_databaseFilePath = getMultilanguageDatabaseFilePath(m_target);
|
|
|
|
|
return m_databaseFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlMultiLanguageAspect::toMap(QVariantMap &map) const
|
|
|
|
|
{
|
2020-08-13 09:16:00 +02:00
|
|
|
BoolAspect::toMap(map);
|
2020-07-20 20:45:08 +02:00
|
|
|
if (!m_currentLocale.isEmpty())
|
|
|
|
|
map.insert(Constants::LAST_USED_LANGUAGE, m_currentLocale);
|
2020-06-18 08:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlMultiLanguageAspect::fromMap(const QVariantMap &map)
|
|
|
|
|
{
|
2020-08-13 09:16:00 +02:00
|
|
|
BoolAspect::fromMap(map);
|
2020-07-20 20:45:08 +02:00
|
|
|
setCurrentLocale(map.value(Constants::LAST_USED_LANGUAGE, "en").toString());
|
2020-06-18 08:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-21 13:47:14 +02:00
|
|
|
QmlMultiLanguageAspect *QmlMultiLanguageAspect::current()
|
|
|
|
|
{
|
|
|
|
|
if (auto project = ProjectExplorer::SessionManager::startupProject())
|
|
|
|
|
return current(project);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlMultiLanguageAspect *QmlMultiLanguageAspect::current(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
if (auto target = project->activeTarget())
|
|
|
|
|
return current(target);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlMultiLanguageAspect *QmlMultiLanguageAspect::current(ProjectExplorer::Target *target)
|
|
|
|
|
{
|
2020-08-20 13:19:45 +02:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
|
|
|
|
|
2020-07-21 13:47:14 +02:00
|
|
|
if (auto runConfiguration = target->activeRunConfiguration()) {
|
|
|
|
|
if (auto multiLanguageAspect = runConfiguration->aspect<QmlProjectManager::QmlMultiLanguageAspect>())
|
|
|
|
|
return multiLanguageAspect;
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 08:49:29 +02:00
|
|
|
} // namespace QmlProjectManager
|