Commit 192d4fd8 authored by Alexander Semke's avatar Alexander Semke
Browse files

In b45af5e4 the NullBackend class was removed.

However, there is still some logic in the code making use of this "backend"
which crashes Cantor when opening of a project file at the moment. This patch
refactors the logic a bit and removes the remaining usage of the null backend completely.

Differential Revision: https://phabricator.kde.org/D24006
parent cc1f1ec6
......@@ -97,7 +97,7 @@ void CantorShell::load(const QUrl &url)
{
if (!m_part||!m_part->url().isEmpty() || m_part->isModified() )
{
addWorksheet(QLatin1String("null"));
addWorksheet(QString());
m_tabWidget->setCurrentIndex(m_parts.size()-1);
}
if (!m_part->openUrl( url ))
......@@ -106,18 +106,6 @@ void CantorShell::load(const QUrl &url)
m_recentProjectsAction->addUrl(url);
}
bool CantorShell::hasAvailableBackend()
{
bool hasBackend=false;
foreach(Cantor::Backend* b, Cantor::Backend::availableBackends())
{
if(b->isEnabled())
hasBackend=true;
}
return hasBackend;
}
void CantorShell::setupActions()
{
QAction* openNew = KStandardAction::openNew(this, SLOT(fileNew()), actionCollection());
......@@ -190,18 +178,22 @@ void CantorShell::readProperties(const KConfigGroup & /*config*/)
// in 'saveProperties'
}
/*!
* called when one of the "new backend" action or the "New" action are called
* adds a new worksheet with the backend assossiated with the called action
* or opens the "Choose Backend" dialog, respectively.
*/
void CantorShell::fileNew()
{
if (sender()->inherits("QAction"))
QAction* a = static_cast<QAction*>(sender());
const QString& backendName = a->data().toString();
if (!backendName.isEmpty())
{
QAction * a = qobject_cast<QAction*>(sender());
QString backendName = a->data().toString();
if (!backendName.isEmpty())
{
addWorksheet(backendName);
return;
}
addWorksheet(backendName);
return;
}
//"New" action was called -> open the "Choose Backend" dialog.
addWorksheet();
}
......@@ -251,7 +243,17 @@ void CantorShell::fileOpen()
void CantorShell::addWorksheet()
{
if(hasAvailableBackend()) //There is no point in asking for the backend, if no one is available
bool hasBackend = false;
for (auto* b : Cantor::Backend::availableBackends())
{
if(b->isEnabled())
{
hasBackend = true;
break;
}
}
if(hasBackend) //There is no point in asking for the backend, if no one is available
{
QString backend = Settings::self()->defaultBackend();
if (backend.isEmpty())
......@@ -318,38 +320,42 @@ void CantorShell::addWorksheet(const QString& backendName)
KPluginFactory* factory = loader.factory();
if (factory)
{
Cantor::Backend* backend = Cantor::Backend::getBackend(backendName);
if (backend)
if (!backendName.isEmpty())
{
if (backend->isEnabled() || backendName == QLatin1String("null"))
Cantor::Backend* backend = Cantor::Backend::getBackend(backendName);
if (!backend)
{
// now that the Part is loaded, we cast it to a Part to get our hands on it
KParts::ReadWritePart* part = factory->create<KParts::ReadWritePart>(m_tabWidget, QVariantList()<<backendName);
if (part)
{
connect(part, SIGNAL(setCaption(QString,QIcon)), this, SLOT(setTabCaption(QString,QIcon)));
connect(part, SIGNAL(worksheetSave(QUrl)), this, SLOT(onWorksheetSave(QUrl)));
m_parts.append(part);
int tab = m_tabWidget->addTab(part->widget(), QIcon::fromTheme(backend->icon()), i18n("Session %1", sessionCount++));
m_tabWidget->setCurrentIndex(tab);
// Setting focus on worksheet view, because Qt clear focus of added widget inside addTab
// This fix https://bugs.kde.org/show_bug.cgi?id=395976
part->widget()->findChild<QGraphicsView*>()->setFocus();
}
else
{
qDebug()<<"error creating part ";
}
KMessageBox::error(this, i18n("Backend %1 is not installed", backendName), i18n("Cantor"));
return;
}
else
{
KMessageBox::error(this, i18n("%1 backend installed, but inactive. Please check installation and Cantor settings", backendName), i18n("Cantor"));
if (!backend->isEnabled())
{
KMessageBox::error(this, i18n("%1 backend installed, but inactive. Please check installation and Cantor settings", backendName), i18n("Cantor"));
return;
}
}
}
// now that the Part is loaded, we cast it to a Part to get our hands on it
KParts::ReadWritePart* part = factory->create<KParts::ReadWritePart>(m_tabWidget, QVariantList()<<backendName);
if (part)
{
connect(part, SIGNAL(setCaption(QString,QIcon)), this, SLOT(setTabCaption(QString,QIcon)));
connect(part, SIGNAL(worksheetSave(QUrl)), this, SLOT(onWorksheetSave(QUrl)));
m_parts.append(part);
int tab = m_tabWidget->addTab(part->widget(), i18n("Session %1", sessionCount++));
m_tabWidget->setCurrentIndex(tab);
// Setting focus on worksheet view, because Qt clear focus of added widget inside addTab
// This fix https://bugs.kde.org/show_bug.cgi?id=395976
part->widget()->findChild<QGraphicsView*>()->setFocus();
}
else
KMessageBox::error(this, i18n("Backend %1 is not installed", backendName), i18n("Cantor"));
{
qDebug()<<"error creating part ";
}
}
else
{
......
......@@ -58,12 +58,9 @@ public:
*/
~CantorShell() override;
/**
* checks if at least one usable Backend is installed
*/
bool hasAvailableBackend();
Cantor::WorksheetAccessInterface* currentWorksheetAccessInterface();
void addWorksheet();
protected:
/**
* This method is called when it is time for the app to save its
......@@ -77,8 +74,8 @@ protected:
* with @ref saveProperties
*/
void readProperties(const KConfigGroup &) override;
public Q_SLOTS:
void addWorksheet();
void addWorksheet(const QString& backendName);
/// Use this method/slot to load whatever file/URL you have
void load(const QUrl& url);
......@@ -103,6 +100,7 @@ private Q_SLOTS:
void updateNewSubmenu();
void pluginVisibilityRequested();
private:
void setupActions();
void closeEvent(QCloseEvent*) override;
......
......@@ -115,9 +115,7 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantLi
connect(m_panelHandler, &Cantor::PanelPluginHandler::pluginsChanged, this, &CantorPart::pluginsChanged);
QString backendName;
if(args.isEmpty())
backendName = QLatin1String("null");
else
if(!args.isEmpty())
backendName = args.first().toString();
for (const QVariant& arg : args)
......@@ -129,8 +127,12 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantLi
}
}
Cantor::Backend* b = Cantor::Backend::getBackend(backendName);
qDebug()<<"Backend "<<b->name()<<" offers extensions: "<<b->extensions();
Cantor::Backend* b = nullptr;
if (!backendName.isEmpty())
{
b = Cantor::Backend::getBackend(backendName);
qDebug()<<"Backend "<<b->name()<<" offers extensions: "<<b->extensions();
}
//central widget
QWidget* widget = new QWidget(parentWidget);
......@@ -312,7 +314,7 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantLi
connect(removeCurrent, &QAction::triggered, m_worksheet, &Worksheet::removeCurrentEntry);
m_editActions.push_back(removeCurrent);
m_showBackendHelp = new QAction(i18n("Show %1 Help", b->name()) , collection);
m_showBackendHelp = new QAction(i18n("Show Help") , collection);
m_showBackendHelp->setIcon(QIcon::fromTheme(QLatin1String("help-contents")));
collection->addAction(QLatin1String("backend_help"), m_showBackendHelp);
connect(m_showBackendHelp, &QAction::triggered, this, &CantorPart::showBackendHelp);
......@@ -331,7 +333,6 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantLi
showEditor->setChecked(false);
collection->addAction(QLatin1String("show_editor"), showEditor);
connect(showEditor, &KToggleAction::toggled, this, &CantorPart::showScriptEditor);
showEditor->setEnabled(b->extensions().contains(QLatin1String("ScriptExtension")));
QAction* showCompletion = new QAction(i18n("Show Completion"), collection);
collection->addAction(QLatin1String("show_completion"), showCompletion);
......@@ -350,7 +351,12 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantLi
// we are not modified since we haven't done anything yet
setModified(false);
initialized();
if (b)
{
m_showBackendHelp->setText(i18n("Show %1 Help", b->name()));
showEditor->setEnabled(b->extensions().contains(QLatin1String("ScriptExtension")));
initialized();
}
}
CantorPart::~CantorPart()
......
......@@ -143,7 +143,6 @@ int main(int argc, char **argv)
KMessageBox::error(widget, i18n("Couldn't open the file %1", filename), i18n("Cantor"));
else
{
widget->addWorksheet(QLatin1String("null"));
widget->load(url);
}
}
......
......@@ -27,11 +27,9 @@
#include <KColorScheme>
#include <KProcess>
#include <QScopedPointer>
#include <QMutex>
#include <QApplication>
#include <QDebug>
#include "lib/renderer.h"
#include "lib/latexrenderer.h"
......@@ -62,7 +60,12 @@ MathRenderTask::MathRenderTask(
double scale,
bool highResolution
): m_jobId(jobId), m_code(code), m_type(type), m_scale(scale), m_highResolution(highResolution)
{}
{
KColorScheme scheme(QPalette::Active);
m_backgroundColor = scheme.background().color();
m_foregroundColor = scheme.foreground().color();
}
void MathRenderTask::setHandler(const QObject* receiver, const char* resultHandler)
{
......@@ -71,6 +74,7 @@ void MathRenderTask::setHandler(const QObject* receiver, const char* resultHandl
void MathRenderTask::run()
{
qDebug()<<"MathRenderTask::run " << m_jobId;
QSharedPointer<MathRenderResult> result(new MathRenderResult());
const QString& tempDir=QStandardPaths::writableLocation(QStandardPaths::TempLocation);
......@@ -98,14 +102,9 @@ void MathRenderTask::run()
}
QString expressionTex=mathTex;
KColorScheme scheme(QPalette::Active);
const QColor &backgroundColor=scheme.background().color();
const QColor &foregroundColor=scheme.foreground().color();
expressionTex=expressionTex
.arg(backgroundColor.redF()).arg(backgroundColor.greenF()).arg(backgroundColor.blueF())
.arg(foregroundColor.redF()).arg(foregroundColor.greenF()).arg(foregroundColor.blueF());
.arg(m_backgroundColor.redF()).arg(m_backgroundColor.greenF()).arg(m_backgroundColor.blueF())
.arg(m_foregroundColor.redF()).arg(m_foregroundColor.greenF()).arg(m_foregroundColor.blueF());
int fontPointSize = QApplication::font().pointSize();
expressionTex=expressionTex.arg(fontPointSize);
......
......@@ -85,6 +85,9 @@ class MathRenderTask : public QObject, public QRunnable
Cantor::LatexRenderer::EquationType m_type;
double m_scale;
bool m_highResolution;
QColor m_backgroundColor;
QColor m_foregroundColor;
};
#endif /* MATHRENDERTASK_H */
......@@ -66,7 +66,8 @@ const double Worksheet::EntryCursorWidth = 2;
Worksheet::Worksheet(Cantor::Backend* backend, QWidget* parent)
: QGraphicsScene(parent)
{
m_session = backend->createSession();
m_session = nullptr;
m_highlighter = nullptr;
m_firstEntry = nullptr;
......@@ -99,11 +100,8 @@ Worksheet::Worksheet(Cantor::Backend* backend, QWidget* parent)
m_jupyterMetadata = nullptr;
enableHighlighting(Settings::self()->highlightDefault());
enableCompletion(Settings::self()->completionDefault());
enableExpressionNumbering(Settings::self()->expressionNumberingDefault());
enableAnimations(Settings::self()->animationDefault());
enableEmbeddedMath(Settings::self()->embeddedMathDefault());
if (backend)
initSession(backend);
}
Worksheet::~Worksheet()
......@@ -1221,7 +1219,7 @@ bool Worksheet::loadCantorWorksheet(const KZip& archive)
m_maxWidth = 0;
if (!m_readOnly)
m_session=b->createSession();
initSession(b);
qDebug()<<"loading entries";
QDomElement expressionChild = root.firstChildElement();
......@@ -1279,6 +1277,16 @@ bool Worksheet::loadCantorWorksheet(const KZip& archive)
return true;
}
void Worksheet::initSession(Cantor::Backend* backend)
{
m_session = backend->createSession();
enableHighlighting(Settings::self()->highlightDefault());
enableCompletion(Settings::self()->completionDefault());
enableExpressionNumbering(Settings::self()->expressionNumberingDefault());
enableAnimations(Settings::self()->animationDefault());
enableEmbeddedMath(Settings::self()->embeddedMathDefault());
}
bool Worksheet::loadJupyterNotebook(const QJsonDocument& doc)
{
m_type = Type::JupyterNotebook;
......@@ -1376,7 +1384,7 @@ bool Worksheet::loadJupyterNotebook(const QJsonDocument& doc)
m_maxWidth = 0;
if (!m_readOnly)
m_session=backend->createSession();
initSession(backend);
qDebug() << "loading jupyter entries";
......
......@@ -277,6 +277,7 @@ class Worksheet : public QGraphicsScene
bool loadCantorWorksheet(const KZip& archive);
bool loadJupyterNotebook(const QJsonDocument& doc);
void showInvalidNotebookSchemeError(QString additionalInfo = QString());
void initSession(Cantor::Backend*);
private:
static const double LeftMargin;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment