cutter/src/widgets/JupyterWidget.cpp

181 lines
4.8 KiB
C++
Raw Normal View History

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>
#include <QPushButton>
2017-12-13 17:36:00 +00:00
2018-03-02 13:15:53 +00:00
#ifdef CUTTER_ENABLE_QTWEBENGINE
#include <QWebEngineSettings>
#endif
JupyterWidget::JupyterWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
2017-12-13 17:36:00 +00:00
ui(new Ui::JupyterWidget)
{
ui->setupUi(this);
ui->tabWidget->setTabsClosable(true);
ui->tabWidget->setMovable(true);
QWidget *cornerWidget = new QWidget(ui->tabWidget);
QHBoxLayout *cornerWidgetLayout = new QHBoxLayout(cornerWidget);
cornerWidget->setLayout(cornerWidgetLayout);
2018-03-03 17:40:06 +00:00
cornerWidgetLayout->setContentsMargins(4, 4, 4, 4);
homeButton = new QPushButton(cornerWidget);
2018-03-03 17:40:06 +00:00
homeButton->setStyleSheet("QPushButton { padding: 2px; background-color: palette(light); border-radius: 4px; }"
"QPushButton:pressed { background-color: palette(dark); }");
homeButton->setIcon(QIcon(":/img/icons/home.svg"));
homeButton->setEnabled(false);
cornerWidgetLayout->addWidget(homeButton);
ui->tabWidget->setCornerWidget(cornerWidget);
connect(homeButton, &QAbstractButton::clicked, this, &JupyterWidget::openHomeTab);
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &JupyterWidget::tabCloseRequested);
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);
int index = ui->tabWidget->addTab(webView, "Tab");
2018-03-02 16:56:59 +00:00
webView->setTabWidget(ui->tabWidget);
ui->tabWidget->setCurrentIndex(index);
2017-12-13 17:36:00 +00:00
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
Q_UNUSED(url);
openHomeTab();
homeButton->setEnabled(true);
2018-03-02 13:15:53 +00:00
#else
clearTabs();
QWidget *failPage = new QWidget(this);
QLabel *label = new QLabel(failPage);
2018-03-21 20:32:32 +00:00
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"));
homeButton->setEnabled(false);
ui->tabWidget->setTabsClosable(false);
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()
{
clearTabs();
2018-02-22 18:39:20 +00:00
QWidget *failPage = new QWidget(this);
QLabel *label = new QLabel(failPage);
2018-03-21 20:32:32 +00:00
label->setText(
tr("An error occurred while opening jupyter. Make sure Jupyter is installed system-wide."));
2018-02-22 18:39:20 +00:00
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(label);
layout->setAlignment(label, Qt::AlignCenter);
failPage->setLayout(layout);
ui->tabWidget->addTab(failPage, tr("Error"));
homeButton->setEnabled(false);
ui->tabWidget->setTabsClosable(false);
}
void JupyterWidget::openHomeTab()
{
#ifdef CUTTER_ENABLE_QTWEBENGINE
QString url = Jupyter()->getUrl();
2018-03-21 20:32:32 +00:00
if (!url.isNull()) {
createNewTab()->load(QUrl(url));
}
#endif
}
void JupyterWidget::tabCloseRequested(int index)
{
removeTab(index);
2018-03-21 20:32:32 +00:00
if (ui->tabWidget->count() == 0) {
openHomeTab();
}
2018-02-22 18:39:20 +00:00
}
2017-12-13 17:36:00 +00:00
void JupyterWidget::removeTab(int index)
{
QWidget *widget = ui->tabWidget->widget(index);
ui->tabWidget->removeTab(index);
delete widget;
}
void JupyterWidget::clearTabs()
{
2018-03-21 20:32:32 +00:00
while (ui->tabWidget->count() > 0) {
removeTab(0);
}
}
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;
2018-03-02 16:56:59 +00:00
this->tabWidget = nullptr;
connect(this, &QWebEngineView::titleChanged, this, &JupyterWebView::onTitleChanged);
}
void JupyterWebView::setTabWidget(QTabWidget *tabWidget)
{
this->tabWidget = tabWidget;
updateTitle();
2017-12-13 17:36:00 +00:00
}
QWebEngineView *JupyterWebView::createWindow(QWebEnginePage::WebWindowType type)
{
2018-03-21 20:32:32 +00:00
switch (type) {
case QWebEnginePage::WebBrowserTab:
return mainWidget->createNewTab();
default:
return nullptr;
2017-12-13 17:36:00 +00:00
}
}
2018-03-02 16:56:59 +00:00
void JupyterWebView::onTitleChanged(const QString &)
{
updateTitle();
}
void JupyterWebView::updateTitle()
{
2018-03-21 20:32:32 +00:00
if (!tabWidget) {
2018-03-02 16:56:59 +00:00
return;
}
QString title = this->title();
2018-03-21 20:32:32 +00:00
if (title.isEmpty()) {
2018-03-02 16:56:59 +00:00
title = tr("Jupyter");
}
tabWidget->setTabText(tabWidget->indexOf(this), title);
}
2018-03-02 13:15:53 +00:00
#endif
#endif