qmldesigner: block local changes while initialization

- emit after the initialization
 - emit only if found languages are differrent

Change-Id: I4f426b7bf055ef1d97e5db86bba075a032210303
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Tim Jenssen
2020-06-15 22:20:51 +02:00
parent dd8709e554
commit 2ed7a7963e
2 changed files with 25 additions and 20 deletions

View File

@@ -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<QComboBox> 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<int>::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()

View File

@@ -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;
};