Index: src/cantor.h =================================================================== --- src/cantor.h +++ src/cantor.h @@ -58,12 +58,9 @@ */ ~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 @@ * 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 @@ void updateNewSubmenu(); void pluginVisibilityRequested(); + private: void setupActions(); void closeEvent(QCloseEvent*) override; Index: src/cantor.cpp =================================================================== --- src/cantor.cpp +++ src/cantor.cpp @@ -97,7 +97,7 @@ { 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 @@ 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 @@ // 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(sender()); + const QString& backendName = a->data().toString(); + if (!backendName.isEmpty()) { - QAction * a = qobject_cast(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::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 @@ 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(m_tabWidget, QVariantList()<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()->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(m_tabWidget, QVariantList()<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()->setFocus(); + } else - KMessageBox::error(this, i18n("Backend %1 is not installed", backendName), i18n("Cantor")); + { + qDebug()<<"error creating part "; + } } else { Index: src/cantor_part.cpp =================================================================== --- src/cantor_part.cpp +++ src/cantor_part.cpp @@ -115,9 +115,7 @@ 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 @@ } } - Cantor::Backend* b = Cantor::Backend::getBackend(backendName); - qDebug()<<"Backend "<name()<<" offers extensions: "<extensions(); + Cantor::Backend* b = nullptr; + if (!backendName.isEmpty()) + { + b = Cantor::Backend::getBackend(backendName); + qDebug()<<"Backend "<name()<<" offers extensions: "<extensions(); + } //central widget QWidget* widget = new QWidget(parentWidget); @@ -312,7 +314,7 @@ 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 @@ 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 @@ // 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() Index: src/main.cpp =================================================================== --- src/main.cpp +++ src/main.cpp @@ -143,7 +143,6 @@ KMessageBox::error(widget, i18n("Couldn't open the file %1", filename), i18n("Cantor")); else { - widget->addWorksheet(QLatin1String("null")); widget->load(url); } } Index: src/mathrendertask.h =================================================================== --- src/mathrendertask.h +++ src/mathrendertask.h @@ -85,6 +85,9 @@ Cantor::LatexRenderer::EquationType m_type; double m_scale; bool m_highResolution; + QColor m_backgroundColor; + QColor m_foregroundColor; + }; #endif /* MATHRENDERTASK_H */ Index: src/mathrendertask.cpp =================================================================== --- src/mathrendertask.cpp +++ src/mathrendertask.cpp @@ -27,11 +27,9 @@ #include #include #include -#include #include #include - #include "lib/renderer.h" #include "lib/latexrenderer.h" @@ -62,7 +60,12 @@ 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::run() { + qDebug()<<"MathRenderTask::run " << m_jobId; QSharedPointer result(new MathRenderResult()); const QString& tempDir=QStandardPaths::writableLocation(QStandardPaths::TempLocation); @@ -98,14 +102,9 @@ } 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); Index: src/worksheet.h =================================================================== --- src/worksheet.h +++ src/worksheet.h @@ -277,6 +277,7 @@ bool loadCantorWorksheet(const KZip& archive); bool loadJupyterNotebook(const QJsonDocument& doc); void showInvalidNotebookSchemeError(QString additionalInfo = QString()); + void initSession(Cantor::Backend*); private: static const double LeftMargin; Index: src/worksheet.cpp =================================================================== --- src/worksheet.cpp +++ src/worksheet.cpp @@ -66,7 +66,8 @@ 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 @@ 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 @@ m_maxWidth = 0; if (!m_readOnly) - m_session=b->createSession(); + initSession(b); qDebug()<<"loading entries"; QDomElement expressionChild = root.firstChildElement(); @@ -1279,6 +1277,16 @@ 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 @@ m_maxWidth = 0; if (!m_readOnly) - m_session=backend->createSession(); + initSession(backend); qDebug() << "loading jupyter entries";