qmlprieview: fix that init language is the chosen one

* removes language feature from preview service
* it is handled by an extra DebugTranslationService until now
* Qt versions without that extra service can not preview
  languages anymore
* Introduce the use of QmlPreviewRunnerSetting. It makes it
  easier to add more properties in the future
* Language needs an extra handling because it needs
  the loadUrl to find existing languages. So we save
  the language to a m_scheduledInitLanguage and emit
  this after the loadFile call happend.

Change-Id: I7f4aec97f7b61d6b290f225282169da594eb9160
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Tim Jenssen
2020-09-28 08:20:23 +02:00
parent ac0ae2f1eb
commit 9dda8af0b0
16 changed files with 91 additions and 130 deletions

View File

@@ -217,6 +217,8 @@ SwitchLanguageComboboxAction::SwitchLanguageComboboxAction(QObject *parent)
QWidget *SwitchLanguageComboboxAction::createWidget(QWidget *parent)
{
QPointer<QComboBox> comboBox = new QComboBox(parent);
// FIXME: this combobox does not work at the moment
comboBox->setDisabled(true);
const QString toolTip(tr("Switch the language used by preview."));
comboBox->setToolTip(toolTip);
comboBox->addItem(tr("Default"));

View File

@@ -36,24 +36,24 @@ QmlDebugTranslationClient::QmlDebugTranslationClient(QmlDebug::QmlDebugConnectio
{
}
void QmlDebugTranslationClient::changeLanguage(const QUrl &url, const QString &locale)
void QmlDebugTranslationClient::changeLanguage(const QUrl &url, const QString &localeIsoCode)
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(ChangeLanguage) << url << locale;
packet << static_cast<qint8>(Command::ChangeLanguage) << url << localeIsoCode;
sendMessage(packet.data());
}
void QmlDebugTranslationClient::changeWarningColor(const QColor &warningColor)
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(ChangeWarningColor) << warningColor;
packet << static_cast<qint8>(Command::ChangeWarningColor) << warningColor;
sendMessage(packet.data());
}
void QmlDebugTranslationClient::changeElidedTextWarningString(const QString &warningString)
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(ChangeElidedTextWarningString) << warningString;
packet << static_cast<qint8>(Command::ChangeElidedTextWarningString) << warningString;
sendMessage(packet.data());
}
@@ -68,21 +68,21 @@ void QmlDebugTranslationClient::changeElideWarning(bool elideWarning)
void QmlDebugTranslationClient::setDebugTranslationServiceLogFile(const QString &logFilePath)
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(SetDebugTranslationServiceLogFile) << logFilePath;
packet << static_cast<qint8>(Command::SetDebugTranslationServiceLogFile) << logFilePath;
sendMessage(packet.data());
}
void QmlDebugTranslationClient::enableElidedTextWarning()
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(EnableElidedTextWarning);
packet << static_cast<qint8>(Command::EnableElidedTextWarning);
sendMessage(packet.data());
}
void QmlDebugTranslationClient::disableElidedTextWarning()
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(DisableElidedTextWarning);
packet << static_cast<qint8>(Command::DisableElidedTextWarning);
sendMessage(packet.data());
}
@@ -91,7 +91,7 @@ void QmlDebugTranslationClient::messageReceived(const QByteArray &data)
QmlDebug::QPacket packet(dataStreamVersion(), data);
qint8 command;
packet >> command;
qDebug() << "invalid command" << command;
qDebug() << Q_FUNC_INFO << "invalid command" << command;
}
void QmlDebugTranslationClient::stateChanged(QmlDebug::QmlDebugClient::State state)

View File

@@ -35,19 +35,20 @@ class QMLPREVIEW_EXPORT QmlDebugTranslationClient : public QmlDebug::QmlDebugCli
Q_OBJECT
public:
//needs to be in sync with QQmlDebugTranslationClient in qtdeclarative/src/plugins/qmltooling/qmldbg_preview/qqmldebugtranslationservice.h
enum Command {
enum class Command {
ChangeLanguage,
MissingTranslationsChecked,
EnableElidedTextWarning,
DisableElidedTextWarning,
ChangeWarningColor,
ChangeElidedTextWarningString,
SetDebugTranslationServiceLogFile,
EnableElidedTextWarning,
DisableElidedTextWarning,
TestAllLanguages
};
explicit QmlDebugTranslationClient(QmlDebug::QmlDebugConnection *connection);
void changeLanguage(const QUrl &url, const QString &locale);
void changeLanguage(const QUrl &url, const QString &localeIsoCode);
void changeWarningColor(const QColor &warningColor);
void changeElidedTextWarningString(const QString &warningString); //is QByteArray better here?
void changeElideWarning(bool elideWarning);

View File

@@ -124,6 +124,7 @@ QmlDebugTranslationWidget::QmlDebugTranslationWidget(QWidget *parent)
layout()->addWidget(elideWarningCheckBox);
connect(elideWarningCheckBox, &QCheckBox::stateChanged, [this] (int state) {
m_elideWarning = (state == Qt::Checked);
});
auto controlLayout = new QHBoxLayout;
@@ -298,14 +299,16 @@ void QmlDebugTranslationWidget::runTest()
int timerCounter = 1;
const auto testLanguageList = m_testLanguages;
if (m_elideWarning)
previewPlugin->changeElideWarning(true);
auto testLanguages = [previewPlugin, runControl, testLanguageList](int timerCounter, const QString &previewedFile) {
qDebug() << "testLanguages" << previewedFile;
for (auto language : testLanguageList) {
QTimer::singleShot(timerCounter * 1000, previewPlugin, [previewPlugin, runControl, language, previewedFile]() {
if (runControl && runControl->isRunning()) {
if (!previewedFile.isEmpty())
previewPlugin->setPreviewedFile(previewedFile);
previewPlugin->setLocale(language);
previewPlugin->setLocaleIsoCode(language);
}
});
}
@@ -319,7 +322,7 @@ void QmlDebugTranslationWidget::runTest()
//delete m_currentRunControl; // who deletes the runcontrol?
m_currentRunControl = nullptr;
if (auto previewPlugin = qobject_cast<Internal::QmlPreviewPlugin*>(getPreviewPlugin()))
previewPlugin->setLocale(m_lastUsedLanguageBeforeTest);
previewPlugin->setLocaleIsoCode(m_lastUsedLanguageBeforeTest);
});
connect(runControl, &ProjectExplorer::RunControl::appendMessage,
@@ -332,7 +335,7 @@ void QmlDebugTranslationWidget::runTest()
if (auto runConfiguration = target->activeRunConfiguration()) {
runControl->setRunConfiguration(runConfiguration);
if (runControl->createMainWorker()) {
previewPlugin->setLocale(QString());
previewPlugin->setLocaleIsoCode(QString());
runControl->initiateStart();
}
}
@@ -399,7 +402,7 @@ void QmlDebugTranslationWidget::appendMessage(const QString &message, Utils::Out
return;
}
const QString serviceSeperator = ": QQmlDebugTranslationService: ";
if (!message.contains(serviceSeperator) || message.contains("DebugTranslation service - language changed"))
if (!message.contains(serviceSeperator))
return;
QString locationString = message;
locationString = locationString.split(serviceSeperator).first();

View File

@@ -56,13 +56,6 @@ void QmlPreviewClient::zoom(float zoomFactor)
sendMessage(packet.data());
}
void QmlPreviewClient::language(const QUrl &context, const QString &locale)
{
QmlDebug::QPacket packet(dataStreamVersion());
packet << static_cast<qint8>(Language) << context << locale;
sendMessage(packet.data());
}
void QmlPreviewClient::announceFile(const QString &path, const QByteArray &contents)
{
QmlDebug::QPacket packet(dataStreamVersion());

View File

@@ -43,8 +43,7 @@ public:
Directory,
ClearCache,
Zoom,
Fps,
Language
Fps
};
struct FpsInfo {
@@ -64,7 +63,6 @@ public:
void loadUrl(const QUrl &url);
void rerun();
void zoom(float zoomFactor);
void language(const QUrl &context, const QString &locale);
void announceFile(const QString &path, const QByteArray &contents);
void announceDirectory(const QString &path, const QStringList &entries);
void announceError(const QString &path);

View File

@@ -36,16 +36,14 @@
namespace QmlPreview {
namespace Internal {
QmlPreviewConnectionManager::~QmlPreviewConnectionManager()
{
}
QmlPreviewConnectionManager::QmlPreviewConnectionManager(QObject *parent) :
QmlDebug::QmlDebugConnectionManager(parent)
{
setTarget(nullptr);
}
QmlPreviewConnectionManager::~QmlPreviewConnectionManager() = default;
void QmlPreviewConnectionManager::setTarget(ProjectExplorer::Target *target)
{
QtSupport::BaseQtVersion::populateQmlFileFinder(&m_projectFileFinder, target);
@@ -117,13 +115,11 @@ void QmlPreviewConnectionManager::createDebugTranslationClient()
{
m_qmlDebugTranslationClient = new QmlDebugTranslationClient(connection());
connect(this, &QmlPreviewConnectionManager::language,
m_qmlDebugTranslationClient.data(), [this](const QString &locale) {
if (m_lastLoadedUrl.isEmpty()) {
// findValidI18nDirectoryAsUrl does not work if we didn't load any file
m_initLocale = locale;
} else {
// service expects a context URL.
m_qmlDebugTranslationClient, [this](const QString &locale) {
m_lastUsedLanguage = locale;
// findValidI18nDirectoryAsUrl does not work if we didn't load any file
// service expects a context URL.
if (!m_lastLoadedUrl.isEmpty()) {
// Search the parent directories of the last loaded URL for i18n files.
m_qmlDebugTranslationClient->changeLanguage(findValidI18nDirectoryAsUrl(locale), locale);
}
@@ -159,10 +155,9 @@ void QmlPreviewConnectionManager::createPreviewClient()
m_lastLoadedUrl = m_targetFileFinder.findUrl(filename);
m_qmlPreviewClient->loadUrl(m_lastLoadedUrl);
if (!m_initLocale.isEmpty()) {
emit language(m_initLocale);
m_initLocale.clear();
}
// emit language after a file was loaded and do it every time,
// because this also triggers the check for missing translations
emit language(m_lastUsedLanguage);
});
connect(this, &QmlPreviewConnectionManager::rerun,
@@ -171,19 +166,6 @@ void QmlPreviewConnectionManager::createPreviewClient()
connect(this, &QmlPreviewConnectionManager::zoom,
m_qmlPreviewClient.data(), &QmlPreviewClient::zoom);
connect(this, &QmlPreviewConnectionManager::language,
m_qmlPreviewClient.data(), [this](const QString &locale) {
if (m_lastLoadedUrl.isEmpty()) {
// findValidI18nDirectoryAsUrl does not work if we didn't load any file
m_initLocale = locale;
} else {
// service expects a context URL.
// Search the parent directories of the last loaded URL for i18n files.
m_qmlPreviewClient->language(findValidI18nDirectoryAsUrl(locale), locale);
}
});
connect(m_qmlPreviewClient.data(), &QmlPreviewClient::pathRequested,
this, [this](const QString &path) {
const bool found = m_projectFileFinder.findFileOrDirectory(

View File

@@ -76,10 +76,10 @@ private:
QPointer<QmlDebugTranslationClient> m_qmlDebugTranslationClient;
Utils::FileSystemWatcher m_fileSystemWatcher;
QUrl m_lastLoadedUrl;
QString m_lastUsedLanguage;
QmlPreviewFileLoader m_fileLoader = nullptr;
QmlPreviewFileClassifier m_fileClassifier = nullptr;
QmlPreviewFpsHandler m_fpsHandler = nullptr;
QString m_initLocale;
};
} // namespace Internal

View File

@@ -150,8 +150,8 @@ public:
QmlPreview::QmlPreviewFileClassifier m_fileClassifer = nullptr;
float m_zoomFactor = -1.0;
QmlPreview::QmlPreviewFpsHandler m_fpsHandler = nullptr;
QString m_locale;
bool elideWarning = false;
QString m_localeIsoCode;
bool m_translationElideWarning = false;
QPointer<QmlDebugTranslationWidget> m_qmlDebugTranslationWidget;
RunWorkerFactory localRunWorkerFactory{
@@ -163,8 +163,15 @@ public:
RunWorkerFactory runWorkerFactory{
[this](RunControl *runControl) {
QmlPreviewRunner *runner = new QmlPreviewRunner(runControl, m_fileLoader, m_fileClassifer,
m_fpsHandler, m_zoomFactor);
QmlPreviewRunner *runner = new QmlPreviewRunner(QmlPreviewRunnerSetting{
runControl,
m_fileLoader,
m_fileClassifer,
m_fpsHandler,
m_zoomFactor,
m_localeIsoCode,
m_translationElideWarning
});
connect(q, &QmlPreviewPlugin::updatePreviews,
runner, &QmlPreviewRunner::loadFile);
connect(q, &QmlPreviewPlugin::rerunPreviews,
@@ -173,7 +180,7 @@ public:
this, &QmlPreviewPluginPrivate::previewCurrentFile);
connect(q, &QmlPreviewPlugin::zoomFactorChanged,
runner, &QmlPreviewRunner::zoom);
connect(q, &QmlPreviewPlugin::localeChanged,
connect(q, &QmlPreviewPlugin::localeIsoCodeChanged,
runner, &QmlPreviewRunner::language);
connect(q, &QmlPreviewPlugin::elideWarningChanged,
runner, &QmlPreviewRunner::changeElideWarning);
@@ -207,7 +214,7 @@ QmlPreviewPluginPrivate::QmlPreviewPluginPrivate(QmlPreviewPlugin *parent)
&QAction::setEnabled);
connect(action, &QAction::triggered, this, [this]() {
if (auto multiLanguageAspect = QmlProjectManager::QmlMultiLanguageAspect::current())
m_locale = multiLanguageAspect->currentLocale();
m_localeIsoCode = multiLanguageAspect->currentLocale();
ProjectExplorerPlugin::runStartupProject(Constants::QML_PREVIEW_RUN_MODE);
});
@@ -387,30 +394,31 @@ void QmlPreviewPlugin::setFpsHandler(QmlPreviewFpsHandler fpsHandler)
emit fpsHandlerChanged(d->m_fpsHandler);
}
QString QmlPreviewPlugin::locale() const
QString QmlPreviewPlugin::localeIsoCode() const
{
return d->m_locale;
return d->m_localeIsoCode;
}
void QmlPreviewPlugin::setLocale(const QString &locale)
void QmlPreviewPlugin::setLocaleIsoCode(const QString &localeIsoCode)
{
if (auto multiLanguageAspect = QmlProjectManager::QmlMultiLanguageAspect::current())
multiLanguageAspect->setCurrentLocale(locale);
if (d->m_locale == locale)
multiLanguageAspect->setCurrentLocale(localeIsoCode);
if (d->m_localeIsoCode == localeIsoCode)
return;
d->m_locale = locale;
emit localeChanged(d->m_locale);
d->m_localeIsoCode = localeIsoCode;
emit localeIsoCodeChanged(d->m_localeIsoCode);
}
bool QmlPreviewPlugin::elideWarning() const
{
return d->elideWarning;
return d->m_translationElideWarning;
}
void QmlPreviewPlugin::changeElideWarning(bool elideWarning)
{
d->elideWarning = elideWarning;
d->m_translationElideWarning = elideWarning;
emit elideWarningChanged(elideWarning);
}
void QmlPreviewPlugin::setFileLoader(QmlPreviewFileLoader fileLoader)

View File

@@ -58,7 +58,7 @@ class QmlPreviewPlugin : public ExtensionSystem::IPlugin
Q_PROPERTY(QmlPreview::QmlPreviewFpsHandler fpsHandler READ fpsHandler
WRITE setFpsHandler NOTIFY fpsHandlerChanged)
Q_PROPERTY(float zoomFactor READ zoomFactor WRITE setZoomFactor NOTIFY zoomFactorChanged)
Q_PROPERTY(QString locale READ locale WRITE setLocale NOTIFY localeChanged)
Q_PROPERTY(QString localeIsoCode READ localeIsoCode WRITE setLocaleIsoCode NOTIFY localeIsoCodeChanged)
Q_PROPERTY(bool elideWarning READ elideWarning WRITE changeElideWarning NOTIFY elideWarningChanged)
public:
@@ -84,8 +84,8 @@ public:
QmlPreview::QmlPreviewFpsHandler fpsHandler() const;
void setFpsHandler(QmlPreview::QmlPreviewFpsHandler fpsHandler);
QString locale() const;
void setLocale(const QString &locale);
QString localeIsoCode() const;
void setLocaleIsoCode(const QString &localeIsoCode);
bool elideWarning() const;
void changeElideWarning(bool elideWarning);
@@ -103,7 +103,7 @@ signals:
void fpsHandlerChanged(QmlPreview::QmlPreviewFpsHandler fpsHandler);
void zoomFactorChanged(float zoomFactor);
void localeChanged(const QString &locale);
void localeIsoCodeChanged(const QString &localeIsoCode);
void elideWarningChanged(bool elideWarning);
private:

View File

@@ -45,17 +45,13 @@ namespace QmlPreview {
static const QString QmlServerUrl = "QmlServerUrl";
QmlPreviewRunner::QmlPreviewRunner(ProjectExplorer::RunControl *runControl,
QmlPreviewFileLoader fileLoader,
QmlPreviewFileClassifier fileClassifier,
QmlPreviewFpsHandler fpsHandler,
float initialZoom)
: RunWorker(runControl)
QmlPreviewRunner::QmlPreviewRunner(const QmlPreviewRunnerSetting &settings)
: RunWorker(settings.runControl)
{
setId("QmlPreviewRunner");
m_connectionManager.setFileLoader(fileLoader);
m_connectionManager.setFileClassifier(fileClassifier);
m_connectionManager.setFpsHandler(fpsHandler);
m_connectionManager.setFileLoader(settings.fileLoader);
m_connectionManager.setFileClassifier(settings.fileClassifier);
m_connectionManager.setFpsHandler(settings.fpsHandler);
connect(this, &QmlPreviewRunner::loadFile,
&m_connectionManager, &Internal::QmlPreviewConnectionManager::loadFile);
@@ -70,24 +66,29 @@ QmlPreviewRunner::QmlPreviewRunner(ProjectExplorer::RunControl *runControl,
&m_connectionManager, &Internal::QmlPreviewConnectionManager::changeElideWarning);
connect(&m_connectionManager, &Internal::QmlPreviewConnectionManager::connectionOpened,
this, [this, initialZoom]() {
if (initialZoom > 0)
emit zoom(initialZoom);
this, [this, settings]() {
if (settings.zoom > 0)
emit zoom(settings.zoom);
if (!settings.language.isEmpty())
emit language(settings.language);
if (settings.translationElideWarning)
emit changeElideWarning(true);
emit ready();
});
connect(&m_connectionManager, &Internal::QmlPreviewConnectionManager::restart,
runControl, [this, runControl]() {
if (!runControl->isRunning())
runControl(), [this]() {
if (!runControl()->isRunning())
return;
this->connect(runControl, &ProjectExplorer::RunControl::stopped, runControl, [runControl]() {
this->connect(runControl(), &ProjectExplorer::RunControl::stopped, [this]() {
ProjectExplorer::ProjectExplorerPlugin::runRunConfiguration(
runControl->runConfiguration(),
runControl()->runConfiguration(),
ProjectExplorer::Constants::QML_PREVIEW_RUN_MODE, true);
});
runControl->initiateStop();
runControl()->initiateStop();
});
}

View File

@@ -32,14 +32,22 @@
namespace QmlPreview {
struct QmlPreviewRunnerSetting {
ProjectExplorer::RunControl *runControl = nullptr;
QmlPreviewFileLoader fileLoader;
QmlPreviewFileClassifier fileClassifier;
QmlPreviewFpsHandler fpsHandler;
float zoom = 1.0;
QString language;
bool translationElideWarning = false;
};
class QmlPreviewRunner : public ProjectExplorer::RunWorker
{
Q_OBJECT
public:
QmlPreviewRunner(ProjectExplorer::RunControl *runControl, QmlPreviewFileLoader fileLoader,
QmlPreviewFileClassifier fileClassifier, QmlPreviewFpsHandler fpsHandler,
float initialZoom);
QmlPreviewRunner(const QmlPreviewRunnerSetting &settings);
void setServerUrl(const QUrl &serverUrl);
QUrl serverUrl() const;

View File

@@ -91,24 +91,6 @@ void QmlPreviewClientTest::testZoom()
QVERIFY(packet.atEnd());
}
void QmlPreviewClientTest::testLanguate()
{
TestableQmlPreviewClient client;
QUrl url("file:///some/file.qml");
QString locale("qt_QT");
client.language(url, locale);
QCOMPARE(client.messages.count(), 1);
QmlDebug::QPacket packet(client.dataStreamVersion(), client.messages.takeFirst());
qint8 command;
QUrl resultUrl;
QString resultLocale;
packet >> command >> resultUrl >> resultLocale;
QCOMPARE(static_cast<QmlPreviewClient::Command>(command), QmlPreviewClient::Language);
QCOMPARE(resultUrl, url);
QCOMPARE(resultLocale, locale);
QVERIFY(packet.atEnd());
}
void QmlPreviewClientTest::testMessageReceived()
{
TestableQmlPreviewClient client;

View File

@@ -36,7 +36,6 @@ private slots:
void testLoadFile();
void testAnnounceFile();
void testZoom();
void testLanguate();
void testMessageReceived();
};

View File

@@ -78,21 +78,6 @@ void QmlPreviewPluginTest::testZoomFactorProperty()
QCOMPARE(spy.count(), 2);
}
void QmlPreviewPluginTest::testLocaleProperty()
{
ExtensionSystem::IPlugin *plugin = getPlugin();
QVERIFY(plugin);
QSignalSpy spy(plugin, SIGNAL(localeChanged(QString)));
QCOMPARE(plugin->property("locale").toString(), QString());
plugin->setProperty("locale", "de_DE");
QCOMPARE(plugin->property("locale").toString(), QString("de_DE"));
plugin->setProperty("locale", "qt_QT");
QCOMPARE(plugin->property("locale").toString(), QString("qt_QT"));
QCOMPARE(spy.count(), 2);
}
void QmlPreviewPluginTest::testFpsHandlerProperty()
{
ExtensionSystem::IPlugin *plugin = getPlugin();

View File

@@ -42,7 +42,6 @@ public:
private slots:
void testFileLoaderProperty();
void testZoomFactorProperty();
void testLocaleProperty();
void testFpsHandlerProperty();
};