Skip to content

Commit 2cc8160

Browse files
committed
ENH: Re-factor WebChannelTransport initialization
Since only one web channel can be installed per page, this commit introduces initializeWebChannelTransport() function allowing both base and derived class to customize the WebChannelTransport initialization callback code. Instead of systematically re-installing a QWebChannel, this commit also ensure that the object `window.slicerPython` is set. List of DataStore changes: $ git shortlog e6278c076..8053b0283 --no-merges Jean-Christophe Fillion-Robin (1): ENH: Refactor WebChannelTransport initialization git-svn-id: http://svn.slicer.org/Slicer4/trunk@27941 3bd1e089-480b-0410-8dfb-8563597acbee
1 parent 3fd5f46 commit 2cc8160

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

Applications/SlicerApp/Testing/Python/WebEngine.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ def test_WebEngine1(self):
152152
webWidget.show()
153153
self.delayDisplay('Showing widget')
154154

155-
webWidget.connect("evalResult(QString,QString)", self.onEvalResult)
156-
157155
webWidget.evalJS("""
158156
const paragraph = document.createElement('p');
159157
paragraph.innerText = 'Hello from Slicer!';
160158
document.body.appendChild(paragraph);
161159
""")
162-
163160
self.delayDisplay('Slicer should be saying hello!')
164161

162+
#
163+
# Test javascript evaluation + use of "evalResult()" signal
164+
#
165+
webWidget.connect("evalResult(QString,QString)", self.onEvalResult)
166+
165167
self.delayDisplay('Slicer setting a javascript value')
166168

167169
webWidget.evalJS("const valueFromSlicer = 42;")
@@ -189,16 +191,12 @@ def test_WebEngine1(self):
189191
slicer.app.settings().setValue("WebEngine/AllowPythonExecution", ctk.ctkMessageBox.AcceptRole)
190192

191193
webWidget.evalJS(r"""
192-
new QWebChannel(qt.webChannelTransport, channel => {
193-
const slicerPython = channel.objects.slicerPython;
194-
195-
let pythonCode = "dialog = qt.QInputDialog(slicer.util.mainWindow())\n";
196-
pythonCode += "dialog.setLabelText('hello')\n";
197-
pythonCode += "dialog.open()\n";
198-
pythonCode += "qt.QTimer.singleShot(1000, dialog.close)\n";
194+
let pythonCode = "dialog = qt.QInputDialog(slicer.util.mainWindow())\n";
195+
pythonCode += "dialog.setLabelText('hello')\n";
196+
pythonCode += "dialog.open()\n";
197+
pythonCode += "qt.QTimer.singleShot(1000, dialog.close)\n";
199198
200-
slicerPython.evalPython(pythonCode);
201-
});
199+
window.slicerPython.evalPython(pythonCode);
202200
""")
203201

204202
self.delayDisplay('Test access to python via js', msec=500)
@@ -207,9 +205,7 @@ def test_WebEngine1(self):
207205
del slicer.modules.slicerPythonValueFromJS
208206

209207
webWidget.evalJS("""
210-
new QWebChannel(qt.webChannelTransport, channel => {
211-
out = channel.objects.slicerPython.evalPython("slicer.modules.slicerPythonValueFromJS = 42");
212-
});
208+
window.slicerPython.evalPython("slicer.modules.slicerPythonValueFromJS = 42");
213209
""")
214210

215211
iteration = 0

Base/QTGUI/qSlicerExtensionsInstallWidget.cxx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,16 @@ void qSlicerExtensionsInstallWidgetPrivate::setFailurePage(const QStringList& er
152152
}
153153

154154
// --------------------------------------------------------------------------
155-
void qSlicerExtensionsInstallWidgetPrivate::updateWebChannelScript(QByteArray& webChannelScript)
155+
void qSlicerExtensionsInstallWidgetPrivate::initializeWebChannelTransport(QByteArray& webChannelScript)
156156
{
157157
#if (QT_VERSION < QT_VERSION_CHECK(5, 6, 0))
158158
Q_UNUSED(webChannelScript);
159159
#else
160+
this->Superclass::initializeWebChannelTransport(webChannelScript);
160161
webChannelScript.append(
161-
"\n"
162-
"new QWebChannel(qt.webChannelTransport, function(channel) {"
163-
" window.extensions_manager_model = channel.objects.extensions_manager_model;"
162+
" window.extensions_manager_model = channel.objects.extensions_manager_model;\n"
164163
// See ExtensionInstallWidgetWebChannelProxy
165-
" window.extensions_install_widget = channel.objects.extensions_install_widget;"
166-
"});"
164+
" window.extensions_install_widget = channel.objects.extensions_install_widget;\n"
167165
);
168166
#endif
169167
}

Base/QTGUI/qSlicerExtensionsInstallWidget_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class qSlicerExtensionsInstallWidgetPrivate : public qSlicerWebWidgetPrivate
6666

6767
void setFailurePage(const QStringList &errors);
6868

69-
virtual void updateWebChannelScript(QByteArray& webChannelScript);
7069
virtual void initializeWebChannel(QWebChannel* webChannel);
70+
virtual void initializeWebChannelTransport(QByteArray& webChannelScript);
7171
void registerExtensionsManagerModel(qSlicerExtensionsManagerModel* oldModel, qSlicerExtensionsManagerModel* newModel);
7272

7373
qSlicerExtensionsManagerModel * ExtensionsManagerModel;

Base/QTGUI/qSlicerWebWidget.cxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,29 @@ void qSlicerWebWidgetPrivate::onAppAboutToQuit()
212212
#endif
213213
}
214214

215+
// --------------------------------------------------------------------------
216+
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
217+
void qSlicerWebWidgetPrivate::updateWebChannelScript(QByteArray& webChannelScript)
218+
{
219+
webChannelScript.append(
220+
"\n"
221+
"new QWebChannel(qt.webChannelTransport, function(channel) {\n"
222+
);
223+
this->initializeWebChannelTransport(webChannelScript);
224+
webChannelScript.append(
225+
"});\n"
226+
);
227+
}
228+
#endif
229+
230+
// --------------------------------------------------------------------------
231+
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
232+
void qSlicerWebWidgetPrivate::initializeWebChannelTransport(QByteArray& webChannelScript)
233+
{
234+
webChannelScript.append(" window.slicerPython = channel.objects.slicerPython;\n");
235+
}
236+
#endif
237+
215238
// --------------------------------------------------------------------------
216239
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
217240
void qSlicerWebWidgetPrivate::initializeWebEngineProfile(QWebEngineProfile* profile)

Base/QTGUI/qSlicerWebWidget_p.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,21 @@ class Q_SLICER_BASE_QTGUI_EXPORT qSlicerWebWidgetPrivate: public QObject, Ui_qSl
114114
/// script content.
115115
virtual void initializeWebEngineProfile(QWebEngineProfile* profile);
116116

117+
/// \brief Append additional script content to ``qwebchannel_appended.js``.
118+
///
119+
/// The default implementation instantiates a ``QWebChannel`` JS object and
120+
/// call initializeWebChannelTransport() to provide derived classes with an
121+
/// opportunity to further customize the WebChannelTransport initialization
122+
/// callback code.
123+
///
117124
/// \sa initializeWebEngineProfile(QWebEngineProfile*)
118-
virtual void updateWebChannelScript(QByteArray& /* webChannelScript */){}
125+
virtual void updateWebChannelScript(QByteArray& /* webChannelScript */);
126+
127+
/// \brief Append additional script content to the WebChannelTransport initialization
128+
/// callback associated with the default QWebChannel.
129+
///
130+
/// \sa updateWebChannelScript()
131+
virtual void initializeWebChannelTransport(QByteArray& /* webChannelScript */);
119132

120133
virtual void initializeWebChannel(QWebChannel* /* webChannel */);
121134

SuperBuild.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ list_conditional_append(Slicer_BUILD_OtsuThresholdImageFilter Slicer_REMOTE_DEPE
301301

302302
Slicer_Remote_Add(DataStore
303303
GIT_REPOSITORY "${EP_GIT_PROTOCOL}://github.com/Slicer/Slicer-DataStore"
304-
GIT_TAG "e6278c07610d0a8719bfa65b24f2aa7c28332d68"
304+
GIT_TAG "8053b0283583d93b223c43bbf4678cb9adf7b0c7"
305305
OPTION_NAME Slicer_BUILD_DataStore
306306
LABELS REMOTE_MODULE
307307
)

0 commit comments

Comments
 (0)