diff options
author | Kai Koehne <kai.koehne@theqtcompany.com> | 2014-12-08 17:34:12 +0100 |
---|---|---|
committer | Kai Koehne <kai.koehne@theqtcompany.com> | 2014-12-10 09:44:27 +0100 |
commit | 71c248e2704225dc4e361699d08fe3b422a4da4f (patch) | |
tree | a9f12e4f3b7f3ec2315f833cfa6e4ac13b7b4285 | |
parent | cb57245ea95d2c1209bcc14f6d119d5ed912a0bf (diff) |
Expose findChild, findChildren methods to JS
Re-add the findChild, findChildren methods known from Qt Script.
Change-Id: I3db6be53ccd89a09b2c7de14ba7f96ebb26dbdbb
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@theqtcompany.com>
Reviewed-by: Niels Weber <niels.weber@theqtcompany.com>
-rw-r--r-- | src/libs/installer/packagemanagergui.cpp | 20 | ||||
-rw-r--r-- | src/libs/installer/packagemanagergui.h | 3 | ||||
-rw-r--r-- | src/libs/installer/scriptengine.cpp | 31 | ||||
-rw-r--r-- | src/libs/installer/scriptengine.h | 2 | ||||
-rw-r--r-- | tests/auto/installer/scriptengine/data/auto-install.qs | 8 | ||||
-rw-r--r-- | tests/auto/installer/scriptengine/tst_scriptengine.cpp | 7 |
6 files changed, 68 insertions, 3 deletions
diff --git a/src/libs/installer/packagemanagergui.cpp b/src/libs/installer/packagemanagergui.cpp index 4653c7ff6..5b9e7493a 100644 --- a/src/libs/installer/packagemanagergui.cpp +++ b/src/libs/installer/packagemanagergui.cpp @@ -698,6 +698,26 @@ void PackageManagerGui::setSettingsButtonEnabled(bool enabled) btn->setEnabled(enabled); } +/** + Returns the first descendant of \a parent that has \a objectName as name. + + This method is meant to be invoked from script. + */ +QObject *PackageManagerGui::findChild(QObject *parent, const QString &objectName) +{ + return parent->findChild<QObject*>(objectName); +} + +/** + Returns all descendants of \a parent that have \a objectName as name. + + This method is meant to be invoked from script. + */ +QList<QObject *> PackageManagerGui::findChildren(QObject *parent, const QString &objectName) +{ + return parent->findChildren<QObject*>(objectName); +} + void PackageManagerGui::customButtonClicked(int which) { if (QWizard::WizardButton(which) == QWizard::CustomButton1 && d->m_showSettingsButton) diff --git a/src/libs/installer/packagemanagergui.h b/src/libs/installer/packagemanagergui.h index 2830b1b3d..f99d65593 100644 --- a/src/libs/installer/packagemanagergui.h +++ b/src/libs/installer/packagemanagergui.h @@ -90,6 +90,9 @@ public: Q_INVOKABLE void showSettingsButton(bool show); Q_INVOKABLE void setSettingsButtonEnabled(bool enable); + Q_INVOKABLE QObject *findChild(QObject *parent, const QString &objectName); + Q_INVOKABLE QList<QObject*> findChildren(QObject *parent, const QString &objectName); + void updateButtonLayout(); static QWizard::WizardStyle getStyle(const QString &name); diff --git a/src/libs/installer/scriptengine.cpp b/src/libs/installer/scriptengine.cpp index c67295f57..d15357e0d 100644 --- a/src/libs/installer/scriptengine.cpp +++ b/src/libs/installer/scriptengine.cpp @@ -225,6 +225,22 @@ namespace QInstaller { */ /*! + \qmlmethod object gui::findChild(object parent, string objectName) + + Returns the first descendant of \a parent that has \a objectName as name. + + \sa QObject::findChild +*/ + +/*! + \qmlmethod object[] gui::findChildren(object parent, string objectName) + + Returns all descendants of \a parent that have \a objectName as name. + + \sa QObject::findChildren +*/ + +/*! \qmlsignal gui::interrupted() */ @@ -293,6 +309,21 @@ ScriptEngine::ScriptEngine(PackageManagerCore *core) proxy.property(QLatin1String("componentByName"))); } +QJSValue ScriptEngine::newQObject(QObject *object) +{ + QJSValue obj = m_engine.newQObject(object); + + // add findChild(), findChildren() methods known from QtScript + QJSValue findChild = m_engine.evaluate( + QLatin1String("(function() { return gui.findChild(this, arguments[0]); })")); + QJSValue findChildren = m_engine.evaluate( + QLatin1String("(function() { return gui.findChildren(this, arguments[0]); })")); + obj.setProperty(QLatin1String("findChild"), findChild); + obj.setProperty(QLatin1String("findChildren"), findChildren); + + return obj; +} + QJSValue ScriptEngine::evaluate(const QString &program, const QString &fileName, int lineNumber) { return m_engine.evaluate(program, fileName, lineNumber); diff --git a/src/libs/installer/scriptengine.h b/src/libs/installer/scriptengine.h index 5046bd14f..c4352d4ca 100644 --- a/src/libs/installer/scriptengine.h +++ b/src/libs/installer/scriptengine.h @@ -53,7 +53,7 @@ public: explicit ScriptEngine(PackageManagerCore *core = 0); QJSValue globalObject() const { return m_engine.globalObject(); } - QJSValue newQObject(QObject *object) { return m_engine.newQObject(object); } + QJSValue newQObject(QObject *object); QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1); diff --git a/tests/auto/installer/scriptengine/data/auto-install.qs b/tests/auto/installer/scriptengine/data/auto-install.qs index 26937fc05..8504cd08a 100644 --- a/tests/auto/installer/scriptengine/data/auto-install.qs +++ b/tests/auto/installer/scriptengine/data/auto-install.qs @@ -4,6 +4,14 @@ function Controller() installer.setValue("GuiTestValue", "hello"); } +Controller.prototype.IntroductionPageCallback = function() +{ + var widget = gui.currentPageWidget(); + var updaterRadioButton = widget.findChild("UpdaterRadioButton"); + updaterRadioButton.checked = true; + gui.clickButton(buttons.NextButton); +} + Controller.prototype.ComponentSelectionPageCallback = function() { var notExistingButtonId = 9999999; diff --git a/tests/auto/installer/scriptengine/tst_scriptengine.cpp b/tests/auto/installer/scriptengine/tst_scriptengine.cpp index 23cb56dd0..91013d028 100644 --- a/tests/auto/installer/scriptengine/tst_scriptengine.cpp +++ b/tests/auto/installer/scriptengine/tst_scriptengine.cpp @@ -56,6 +56,11 @@ public: setPage(PackageManagerCore::Introduction, new IntroductionPage(core)); setPage(PackageManagerCore::ComponentSelection, new ComponentSelectionPage(core)); setPage(PackageManagerCore::InstallationFinished, new FinishedPage(core)); + + foreach (const int id, pageIds()) { + packageManagerCore()->controlScriptEngine()->addQObjectChildren(page(id)); + packageManagerCore()->componentScriptEngine()->addQObjectChildren(page(id)); + } } virtual void init() {} @@ -306,8 +311,6 @@ private slots: testGui.loadControlScript(":///data/auto-install.qs"); QCOMPARE(m_core.value("GuiTestValue"), QString("hello")); - // show event calls automatically the first callback which does not exist - setExpectedScriptOutput("Control script callback \"IntroductionPageCallback\" does not exist. "); testGui.show(); // inside the auto-install script we are clicking the next button, with a not existing button |