File indexing completed on 2024-04-21 03:41:33

0001 // SPDX-FileCopyrightText: 2010 by Aleix Pol <aleixpol@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "kalgebramobile.h"
0005 
0006 #include <analitza/expression.h>
0007 #include <analitzaplot/plotsmodel.h>
0008 
0009 #include "../src/consolemodel.h"
0010 
0011 #include "clipboard.h"
0012 #include <QSortFilterProxyModel>
0013 #include <QStandardItemModel>
0014 #include <qqml.h>
0015 
0016 using namespace Analitza;
0017 
0018 KAlgebraMobile *KAlgebraMobile::s_self = nullptr;
0019 KAlgebraMobile *KAlgebraMobile::self()
0020 {
0021     return s_self;
0022 }
0023 
0024 Q_DECLARE_METATYPE(QSharedPointer<Analitza::Variables>)
0025 
0026 KAlgebraMobile::KAlgebraMobile(QObject *parent)
0027     : QObject(parent)
0028     , m_functionsModel(nullptr)
0029     , m_vars(new Analitza::Variables)
0030 {
0031     Q_ASSERT(s_self == nullptr);
0032     s_self = this;
0033 
0034     const auto uri = "org.kde.kalgebra.mobile";
0035     qmlRegisterType<ConsoleModel>("org.kde.kalgebra.mobile", 1, 0, "ConsoleModel");
0036     qmlRegisterType<QSortFilterProxyModel>("org.kde.kalgebra.mobile", 1, 0, "QSortFilterProxyModel");
0037     qmlRegisterUncreatableType<QAbstractItemModel>("org.kde.kalgebra.mobile", 1, 0, "QAbstractItemModel", QStringLiteral("no"));
0038     qmlRegisterType<Clipboard>(uri, 1, 0, "Clipboard");
0039 
0040     qmlRegisterAnonymousType<QStandardItemModel>("Kalgebra", 1);
0041     qmlRegisterUncreatableType<Analitza::Expression>("org.kde.kalgebra.mobile", 1, 0, "Expression", QStringLiteral("because"));
0042     qRegisterMetaType<QSharedPointer<Analitza::Variables>>("QSharedPointer<Analitza::Variables>");
0043 }
0044 
0045 PlotsModel *KAlgebraMobile::functionsModel()
0046 {
0047     if (!m_functionsModel) {
0048         m_functionsModel = new Analitza::PlotsModel(this);
0049         connect(m_functionsModel, &QAbstractItemModel::rowsRemoved, this, &KAlgebraMobile::functionRemoved);
0050         connect(m_functionsModel, &QAbstractItemModel::rowsInserted, this, &KAlgebraMobile::functionInserted);
0051         connect(m_functionsModel, &QAbstractItemModel::dataChanged, this, &KAlgebraMobile::functionModified);
0052     }
0053 
0054     return m_functionsModel;
0055 }
0056 
0057 void KAlgebraMobile::functionInserted(const QModelIndex &parent, int start, int end)
0058 {
0059     Q_ASSERT(!parent.isValid());
0060     for (int i = start; i <= end; i++) {
0061         QModelIndex idx = functionsModel()->index(i, 1);
0062         m_vars->modify(idx.sibling(i, 0).data().toString(), Analitza::Expression(idx.data().toString()));
0063     }
0064 }
0065 
0066 void KAlgebraMobile::functionRemoved(const QModelIndex &parent, int start, int end)
0067 {
0068     Q_ASSERT(!parent.isValid());
0069     for (int i = start; i <= end; i++) {
0070         QModelIndex idx = functionsModel()->index(i);
0071         m_vars->remove(functionsModel()->data(idx, Qt::DisplayRole).toString());
0072     }
0073 }
0074 
0075 void KAlgebraMobile::functionModified(const QModelIndex &idxA, const QModelIndex &idxB)
0076 {
0077     if (idxB.column() == 1) {
0078         for (int i = idxA.row(); i < idxB.row(); ++i) {
0079             QModelIndex idx = functionsModel()->index(i, 1);
0080             m_vars->modify(idx.sibling(i, 0).data().toString(), Analitza::Expression(idx.data().toString()));
0081         }
0082     } // else TODO: figure out how to control a "rename"
0083 }
0084 
0085 QSharedPointer<Analitza::Variables> KAlgebraMobile::variables() const
0086 {
0087     return m_vars;
0088 }