File indexing completed on 2024-04-21 11:13:54

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aleix Pol <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kalgebrasession.h"
0008 
0009 #include "settings.h"
0010 
0011 #include "kalgebraexpression.h"
0012 #include "kalgebracompletionobject.h"
0013 #include <analitzagui/algebrahighlighter.h>
0014 #include <analitza/analyzer.h>
0015 #include <QTextEdit>
0016 
0017 #include <QDebug>
0018 #include "kalgebrasyntaxhelpobject.h"
0019 #include <analitzagui/operatorsmodel.h>
0020 #include <analitzagui/variablesmodel.h>
0021 
0022 KAlgebraSession::KAlgebraSession( Cantor::Backend* backend)
0023     : Session(backend)
0024 {
0025     m_analyzer = new Analitza::Analyzer;
0026     m_operatorsModel = new OperatorsModel;
0027     m_variablesModel = new Analitza::VariablesModel(m_analyzer->variables());
0028     m_operatorsModel->setVariables(m_analyzer->variables());
0029 }
0030 
0031 KAlgebraSession::~KAlgebraSession()
0032 {
0033     delete m_analyzer;
0034 }
0035 
0036 void KAlgebraSession::login()
0037 {
0038     emit loginStarted();
0039     if(!KAlgebraSettings::autorunScripts().isEmpty()){
0040         QString autorunScripts = KAlgebraSettings::self()->autorunScripts().join(QLatin1String("\n"));
0041 
0042         evaluateExpression(autorunScripts, KAlgebraExpression::DeleteOnFinish, true);
0043     }
0044 
0045     changeStatus(Cantor::Session::Done);
0046     emit loginDone();
0047 }
0048 
0049 void KAlgebraSession::logout()
0050 {
0051     Session::logout();
0052 }
0053 
0054 void KAlgebraSession::interrupt()
0055 {
0056     changeStatus(Cantor::Session::Done);
0057 }
0058 
0059 Cantor::Expression* KAlgebraSession::evaluateExpression(const QString& cmd,
0060                                                         Cantor::Expression::FinishingBehavior behave,
0061                                                         bool internal)
0062 {
0063     KAlgebraExpression* expr=new KAlgebraExpression(this, internal);
0064     expr->setFinishingBehavior(behave);
0065 
0066     changeStatus(Cantor::Session::Running);
0067     expr->setCommand(cmd);
0068     expr->evaluate();
0069     changeStatus(Cantor::Session::Done);
0070 
0071     m_operatorsModel->setVariables(m_analyzer->variables());
0072     m_variablesModel->updateInformation();
0073     return expr;
0074 }
0075 
0076 Cantor::CompletionObject* KAlgebraSession::completionFor(const QString& command, int index)
0077 {
0078     return new KAlgebraCompletionObject(command, index, this);
0079 }
0080 
0081 Cantor::SyntaxHelpObject* KAlgebraSession::syntaxHelpFor(const QString& cmd)
0082 {
0083     return new KAlgebraSyntaxHelpObject(cmd, this);
0084 }
0085 
0086 OperatorsModel* KAlgebraSession::operatorsModel()
0087 {
0088     return m_operatorsModel;
0089 }
0090 
0091 QSyntaxHighlighter* KAlgebraSession::syntaxHighlighter(QObject* parent)
0092 {
0093     Q_UNUSED(parent);
0094     //return new AlgebraHighlighter(parent->document());
0095     // TODO: Think of something better here.
0096     return new AlgebraHighlighter(nullptr);
0097 }
0098 
0099 QAbstractItemModel* KAlgebraSession::variableDataModel() const
0100 {
0101     return m_variablesModel;
0102 }