mirror of
https://github.com/rizinorg/cutter.git
synced 2025-01-31 16:47:26 +00:00
Make Jupyter Tabs closable, Add Home Button
This commit is contained in:
parent
17f7c0b593
commit
09a34d265e
@ -8,6 +8,7 @@
|
|||||||
#include <QTabWidget>
|
#include <QTabWidget>
|
||||||
#include <QHBoxLayout>
|
#include <QHBoxLayout>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QPushButton>
|
||||||
|
|
||||||
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
||||||
#include <QWebEngineSettings>
|
#include <QWebEngineSettings>
|
||||||
@ -19,6 +20,21 @@ JupyterWidget::JupyterWidget(QWidget *parent, Qt::WindowFlags flags) :
|
|||||||
{
|
{
|
||||||
ui->setupUi(this);
|
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);
|
||||||
|
homeButton = new QPushButton(cornerWidget);
|
||||||
|
homeButton->setText(tr("Home"));
|
||||||
|
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);
|
||||||
|
|
||||||
connect(Jupyter(), &JupyterConnection::urlReceived, this, &JupyterWidget::urlReceived);
|
connect(Jupyter(), &JupyterConnection::urlReceived, this, &JupyterWidget::urlReceived);
|
||||||
connect(Jupyter(), &JupyterConnection::creationFailed, this, &JupyterWidget::creationFailed);
|
connect(Jupyter(), &JupyterConnection::creationFailed, this, &JupyterWidget::creationFailed);
|
||||||
Jupyter()->start();
|
Jupyter()->start();
|
||||||
@ -32,8 +48,9 @@ JupyterWidget::~JupyterWidget()
|
|||||||
JupyterWebView *JupyterWidget::createNewTab()
|
JupyterWebView *JupyterWidget::createNewTab()
|
||||||
{
|
{
|
||||||
auto webView = new JupyterWebView(this);
|
auto webView = new JupyterWebView(this);
|
||||||
ui->tabWidget->addTab(webView, "Tab");
|
int index = ui->tabWidget->addTab(webView, "Tab");
|
||||||
webView->setTabWidget(ui->tabWidget);
|
webView->setTabWidget(ui->tabWidget);
|
||||||
|
ui->tabWidget->setCurrentIndex(index);
|
||||||
return webView;
|
return webView;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -41,8 +58,11 @@ JupyterWebView *JupyterWidget::createNewTab()
|
|||||||
void JupyterWidget::urlReceived(const QString &url)
|
void JupyterWidget::urlReceived(const QString &url)
|
||||||
{
|
{
|
||||||
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
||||||
createNewTab()->load(QUrl(url));
|
Q_UNUSED(url);
|
||||||
|
openHomeTab();
|
||||||
|
homeButton->setEnabled(true);
|
||||||
#else
|
#else
|
||||||
|
clearTabs();
|
||||||
QWidget *failPage = new QWidget(this);
|
QWidget *failPage = new QWidget(this);
|
||||||
QLabel *label = new QLabel(failPage);
|
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->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));
|
||||||
@ -54,11 +74,14 @@ void JupyterWidget::urlReceived(const QString &url)
|
|||||||
layout->setAlignment(label, Qt::AlignCenter);
|
layout->setAlignment(label, Qt::AlignCenter);
|
||||||
failPage->setLayout(layout);
|
failPage->setLayout(layout);
|
||||||
ui->tabWidget->addTab(failPage, tr("Jupyter"));
|
ui->tabWidget->addTab(failPage, tr("Jupyter"));
|
||||||
|
homeButton->setEnabled(false);
|
||||||
|
ui->tabWidget->setTabsClosable(false);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void JupyterWidget::creationFailed()
|
void JupyterWidget::creationFailed()
|
||||||
{
|
{
|
||||||
|
clearTabs();
|
||||||
QWidget *failPage = new QWidget(this);
|
QWidget *failPage = new QWidget(this);
|
||||||
QLabel *label = new QLabel(failPage);
|
QLabel *label = new QLabel(failPage);
|
||||||
label->setText(tr("An error occurred while opening jupyter. Make sure Jupyter is installed system-wide."));
|
label->setText(tr("An error occurred while opening jupyter. Make sure Jupyter is installed system-wide."));
|
||||||
@ -67,8 +90,44 @@ void JupyterWidget::creationFailed()
|
|||||||
layout->setAlignment(label, Qt::AlignCenter);
|
layout->setAlignment(label, Qt::AlignCenter);
|
||||||
failPage->setLayout(layout);
|
failPage->setLayout(layout);
|
||||||
ui->tabWidget->addTab(failPage, tr("Error"));
|
ui->tabWidget->addTab(failPage, tr("Error"));
|
||||||
|
homeButton->setEnabled(false);
|
||||||
|
ui->tabWidget->setTabsClosable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JupyterWidget::openHomeTab()
|
||||||
|
{
|
||||||
|
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
||||||
|
QString url = Jupyter()->getUrl();
|
||||||
|
if (!url.isNull())
|
||||||
|
{
|
||||||
|
createNewTab()->load(QUrl(url));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void JupyterWidget::tabCloseRequested(int index)
|
||||||
|
{
|
||||||
|
removeTab(index);
|
||||||
|
if (ui->tabWidget->count() == 0)
|
||||||
|
{
|
||||||
|
openHomeTab();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JupyterWidget::removeTab(int index)
|
||||||
|
{
|
||||||
|
QWidget *widget = ui->tabWidget->widget(index);
|
||||||
|
ui->tabWidget->removeTab(index);
|
||||||
|
delete widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JupyterWidget::clearTabs()
|
||||||
|
{
|
||||||
|
while (ui->tabWidget->count() > 0)
|
||||||
|
{
|
||||||
|
removeTab(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
||||||
JupyterWebView::JupyterWebView(JupyterWidget *mainWidget, QWidget *parent) : QWebEngineView(parent)
|
JupyterWebView::JupyterWebView(JupyterWidget *mainWidget, QWidget *parent) : QWebEngineView(parent)
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <QDockWidget>
|
#include <QDockWidget>
|
||||||
|
#include <QAbstractButton>
|
||||||
|
|
||||||
#include "utils/JupyterConnection.h"
|
#include "utils/JupyterConnection.h"
|
||||||
|
|
||||||
@ -35,8 +36,16 @@ private slots:
|
|||||||
void urlReceived(const QString &url);
|
void urlReceived(const QString &url);
|
||||||
void creationFailed();
|
void creationFailed();
|
||||||
|
|
||||||
|
void openHomeTab();
|
||||||
|
void tabCloseRequested(int index);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Ui::JupyterWidget> ui;
|
std::unique_ptr<Ui::JupyterWidget> ui;
|
||||||
|
|
||||||
|
QAbstractButton *homeButton;
|
||||||
|
|
||||||
|
void removeTab(int index);
|
||||||
|
void clearTabs();
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
#ifdef CUTTER_ENABLE_QTWEBENGINE
|
||||||
|
Loading…
Reference in New Issue
Block a user