2017-12-13 17:36:00 +00:00
2018-03-02 13:15:53 +00:00
# ifdef CUTTER_ENABLE_JUPYTER
2017-12-13 17:36:00 +00:00
# include "ui_JupyterWidget.h"
# include "JupyterWidget.h"
2018-02-22 18:39:20 +00:00
# include <QTabWidget>
# include <QHBoxLayout>
# include <QLabel>
2017-12-13 17:36:00 +00:00
2018-03-02 13:15:53 +00:00
# ifdef CUTTER_ENABLE_QTWEBENGINE
# include <QWebEngineSettings>
# endif
2017-12-13 17:36:00 +00:00
JupyterWidget : : JupyterWidget ( QWidget * parent , Qt : : WindowFlags flags ) :
QDockWidget ( parent , flags ) ,
ui ( new Ui : : JupyterWidget )
{
ui - > setupUi ( this ) ;
2018-02-22 19:56:15 +00:00
connect ( Jupyter ( ) , & JupyterConnection : : urlReceived , this , & JupyterWidget : : urlReceived ) ;
connect ( Jupyter ( ) , & JupyterConnection : : creationFailed , this , & JupyterWidget : : creationFailed ) ;
Jupyter ( ) - > start ( ) ;
2017-12-13 17:36:00 +00:00
}
JupyterWidget : : ~ JupyterWidget ( )
{
}
2018-03-02 13:15:53 +00:00
# ifdef CUTTER_ENABLE_QTWEBENGINE
2017-12-13 17:36:00 +00:00
JupyterWebView * JupyterWidget : : createNewTab ( )
{
auto webView = new JupyterWebView ( this ) ;
ui - > tabWidget - > addTab ( webView , " Tab " ) ;
return webView ;
}
2018-03-02 13:15:53 +00:00
# endif
2017-12-13 17:36:00 +00:00
void JupyterWidget : : urlReceived ( const QString & url )
{
2018-03-02 13:15:53 +00:00
# ifdef CUTTER_ENABLE_QTWEBENGINE
2017-12-13 17:36:00 +00:00
createNewTab ( ) - > load ( QUrl ( url ) ) ;
2018-03-02 13:15:53 +00:00
# else
2018-03-02 14:06:08 +00:00
QWidget * failPage = new QWidget ( this ) ;
QLabel * label = new QLabel ( failPage ) ;
label - > setText ( tr ( " Cutter has been built without QtWebEngine.<br />Open the following URL in your Browser to use Jupyter:<br /><a href= \" %1 \" >%1</a> " ) . arg ( url ) ) ;
label - > setTextFormat ( Qt : : RichText ) ;
label - > setTextInteractionFlags ( Qt : : TextBrowserInteraction ) ;
label - > setOpenExternalLinks ( true ) ;
QHBoxLayout * layout = new QHBoxLayout ( ) ;
layout - > addWidget ( label ) ;
layout - > setAlignment ( label , Qt : : AlignCenter ) ;
failPage - > setLayout ( layout ) ;
ui - > tabWidget - > addTab ( failPage , tr ( " Jupyter " ) ) ;
2018-03-02 13:15:53 +00:00
# endif
2017-12-13 17:36:00 +00:00
}
2018-02-22 18:39:20 +00:00
void JupyterWidget : : creationFailed ( )
{
QWidget * failPage = new QWidget ( this ) ;
QLabel * label = new QLabel ( failPage ) ;
label - > setText ( tr ( " An error occurred while opening jupyter. Make sure Jupyter is installed system-wide. " ) ) ;
QHBoxLayout * layout = new QHBoxLayout ( ) ;
layout - > addWidget ( label ) ;
layout - > setAlignment ( label , Qt : : AlignCenter ) ;
failPage - > setLayout ( layout ) ;
ui - > tabWidget - > addTab ( failPage , tr ( " Error " ) ) ;
}
2017-12-13 17:36:00 +00:00
2018-02-22 19:56:15 +00:00
2018-03-02 13:15:53 +00:00
# ifdef CUTTER_ENABLE_QTWEBENGINE
2017-12-13 17:36:00 +00:00
JupyterWebView : : JupyterWebView ( JupyterWidget * mainWidget , QWidget * parent ) : QWebEngineView ( parent )
{
this - > mainWidget = mainWidget ;
}
QWebEngineView * JupyterWebView : : createWindow ( QWebEnginePage : : WebWindowType type )
{
switch ( type )
{
case QWebEnginePage : : WebBrowserTab :
return mainWidget - > createNewTab ( ) ;
default :
return nullptr ;
}
}
2018-03-02 13:15:53 +00:00
# endif
# endif