diff --git a/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.cpp b/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.cpp index 32d524f6909..c0da2f8b8c6 100644 --- a/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.cpp +++ b/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.cpp @@ -217,42 +217,46 @@ SwitchLanguageComboboxAction::SwitchLanguageComboboxAction(QObject *parent) connect(ProjectExplorer::SessionManager::instance(), &ProjectExplorer::SessionManager::startupProjectChanged, this, - &SwitchLanguageComboboxAction::refreshProjectLocales); + &SwitchLanguageComboboxAction::updateProjectLocales); } QWidget *SwitchLanguageComboboxAction::createWidget(QWidget *parent) { QPointer comboBox = new QComboBox(parent); comboBox->setToolTip(tr("Switch the language used by preview.")); + comboBox->addItem(tr("Default")); + + auto refreshComboBoxFunction = [this, comboBox] (ProjectExplorer::Project *project) { + if (comboBox) { + if (updateProjectLocales(project)) { + comboBox->clear(); + comboBox->addItem(tr("Default")); + comboBox->addItems(m_localeStrings); + } + } + }; + connect(ProjectExplorer::SessionManager::instance(), &ProjectExplorer::SessionManager::startupProjectChanged, + comboBox, refreshComboBoxFunction); + + if (auto project = SessionManager::startupProject()) + refreshComboBoxFunction(project); + + // do this after refreshComboBoxFunction so we do not get currentLocaleChanged signals at initialization connect(comboBox, QOverload::of(&QComboBox::currentIndexChanged), [this, comboBox](int index) { - if (index == 0) + if (index == 0) // == Default emit currentLocaleChanged(""); else emit currentLocaleChanged(comboBox->currentText()); }); - auto refreshComboBoxFunction = [this, comboBox] (ProjectExplorer::Project *project) { - if (comboBox) { - refreshProjectLocales(project); - comboBox->clear(); - comboBox->addItem(tr("Default")); - comboBox->addItems(m_localeStrings); - } - }; - connect(ProjectExplorer::SessionManager::instance(), - &ProjectExplorer::SessionManager::startupProjectChanged, - refreshComboBoxFunction); - - if (auto project = SessionManager::startupProject()) - refreshComboBoxFunction(project); - return comboBox; } -void SwitchLanguageComboboxAction::refreshProjectLocales(Project *project) +bool SwitchLanguageComboboxAction::updateProjectLocales(Project *project) { if (!project) - return; + return false; + auto previousLocales = m_localeStrings; m_localeStrings.clear(); const auto projectDirectory = project->rootProjectDirectory().toFileInfo().absoluteFilePath(); const QDir languageDirectory(projectDirectory + "/i18n"); @@ -263,6 +267,7 @@ void SwitchLanguageComboboxAction::refreshProjectLocales(Project *project) const QString locale = qmFile.left(localeEndPosition).mid(localeStartPosition); return locale; }); + return previousLocales != m_localeStrings; } SwitchLanguageAction::SwitchLanguageAction() diff --git a/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.h b/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.h index 8a71ee07c29..a23f125ca14 100644 --- a/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.h +++ b/src/plugins/qmldesigner/qmlpreviewplugin/qmlpreviewactions.h @@ -113,7 +113,7 @@ signals: protected: QWidget *createWidget(QWidget *parent) override; private: - void refreshProjectLocales(ProjectExplorer::Project *project); + bool updateProjectLocales(ProjectExplorer::Project *project); QStringList m_localeStrings; };