File indexing completed on 2024-04-28 11:20:35

0001 /*
0002     SPDX-FileCopyrightText: 2010 Miha Čančula <miha.cancula@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "octavesyntaxhelpobject.h"
0008 #include "session.h"
0009 #include "result.h"
0010 
0011 #include <QDebug>
0012 
0013 OctaveSyntaxHelpObject::OctaveSyntaxHelpObject(const QString& command, Cantor::Session* session): SyntaxHelpObject(command, session),
0014 m_expression(nullptr)
0015 {
0016 
0017 }
0018 
0019 void OctaveSyntaxHelpObject::fetchInformation()
0020 {
0021     if (session()->status() != Cantor::Session::Disable)
0022     {
0023         qDebug() << "Fetching syntax help for" << command();
0024         QString expr = QString::fromLatin1("help('%1')").arg(command());
0025         m_expression = session()->evaluateExpression(expr, Cantor::Expression::FinishingBehavior::DoNotDelete, true);
0026         connect(m_expression, &Cantor::Expression::statusChanged, this, &OctaveSyntaxHelpObject::fetchingDone);
0027     }
0028     else
0029         emit done();
0030 }
0031 
0032 void OctaveSyntaxHelpObject::fetchingDone(Cantor::Expression::Status status)
0033 {
0034     switch(status)
0035     {
0036         case Cantor::Expression::Done:
0037         {
0038             Cantor::Result* result = m_expression->result();
0039             if (result)
0040             {
0041                 QString res = result->toHtml();
0042                 res.remove(QLatin1String("<br/>"));
0043                 res.remove(0, res.indexOf(QLatin1String("--")));
0044                 setHtml(QLatin1Char(' ') + res.trimmed());
0045             }
0046             break;
0047         }
0048 
0049         case Cantor::Expression::Interrupted:
0050         case Cantor::Expression::Error:
0051         {
0052             qDebug() << "fetching expression finished with status" << (status == Cantor::Expression::Error? "Error" : "Interrupted");
0053             break;
0054         }
0055 
0056         default:
0057             return;
0058     }
0059     m_expression->deleteLater();
0060     m_expression = nullptr;
0061     emit done();
0062 }