File indexing completed on 2023-05-30 10:41:17

0001 /*************************************************************************************
0002  *  Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "kalgebramobile.h"
0020 
0021 #include <analitzaplot/plotsmodel.h>
0022 #include <analitza/expression.h>
0023 
0024 #include "../src/consolemodel.h"
0025 
0026 #include <QSortFilterProxyModel>
0027 #include <QStandardItemModel>
0028 #include <qqml.h>
0029 #include "clipboard.h"
0030 
0031 using namespace Analitza;
0032 
0033 KAlgebraMobile* KAlgebraMobile::s_self=0;
0034 KAlgebraMobile* KAlgebraMobile::self() { return s_self; }
0035 
0036 Q_DECLARE_METATYPE(QSharedPointer<Analitza::Variables>)
0037 
0038 KAlgebraMobile::KAlgebraMobile(QObject* parent)
0039     : QObject(parent), m_functionsModel(0), m_vars(new Analitza::Variables)
0040 {
0041     Q_ASSERT(s_self==0);
0042     s_self=this;
0043     
0044     const auto uri = "org.kde.kalgebra.mobile";
0045     qmlRegisterType<ConsoleModel>("org.kde.kalgebra.mobile", 1, 0, "ConsoleModel");
0046     qmlRegisterType<QSortFilterProxyModel>("org.kde.kalgebra.mobile", 1, 0, "QSortFilterProxyModel");
0047     qmlRegisterUncreatableType<QAbstractItemModel>("org.kde.kalgebra.mobile", 1, 0, "QAbstractItemModel", "no");
0048     qmlRegisterType<Clipboard>(uri, 1, 0, "Clipboard");
0049 
0050 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
0051     qmlRegisterType<QStandardItemModel>();
0052 #else
0053     qmlRegisterAnonymousType<QStandardItemModel>("Kalgebra", 1);
0054 #endif
0055     qmlRegisterUncreatableType<Analitza::Expression>("org.kde.kalgebra.mobile", 1, 0, "Expression", QStringLiteral("because"));
0056     qRegisterMetaType<QSharedPointer<Analitza::Variables>>("QSharedPointer<Analitza::Variables>");
0057 }
0058 
0059 PlotsModel* KAlgebraMobile::functionsModel()
0060 {
0061     if(!m_functionsModel) {
0062         m_functionsModel = new Analitza::PlotsModel(this);
0063         connect(m_functionsModel, &QAbstractItemModel::rowsRemoved, this, &KAlgebraMobile::functionRemoved);
0064         connect(m_functionsModel, &QAbstractItemModel::rowsInserted, this, &KAlgebraMobile::functionInserted);
0065         connect(m_functionsModel, &QAbstractItemModel::dataChanged, this, &KAlgebraMobile::functionModified);
0066     }
0067     
0068     return m_functionsModel;
0069 }
0070 
0071 void KAlgebraMobile::functionInserted(const QModelIndex& parent, int start, int end)
0072 {
0073     Q_ASSERT(!parent.isValid());
0074     for(int i=start; i<=end; i++) {
0075         QModelIndex idx = functionsModel()->index(i, 1);
0076         m_vars->modify(idx.sibling(i,0).data().toString(), Analitza::Expression(idx.data().toString()));
0077     }
0078 }
0079 
0080 void KAlgebraMobile::functionRemoved(const QModelIndex& parent, int start, int end)
0081 {
0082     Q_ASSERT(!parent.isValid());
0083     for(int i=start; i<=end; i++) {
0084         QModelIndex idx = functionsModel()->index(i);
0085         m_vars->remove(functionsModel()->data(idx, Qt::DisplayRole).toString());
0086     }
0087 }
0088 
0089 void KAlgebraMobile::functionModified(const QModelIndex& idxA, const QModelIndex& idxB)
0090 {
0091     if(idxB.column()==1) {
0092         for(int i=idxA.row(); i<idxB.row(); ++i) {
0093             QModelIndex idx = functionsModel()->index(i, 1);
0094             m_vars->modify(idx.sibling(i,0).data().toString(), Analitza::Expression(idx.data().toString()));
0095         }
0096     } //else TODO: figure out how to control a "rename"
0097 }
0098 
0099 QSharedPointer<Analitza::Variables> KAlgebraMobile::variables() const { return m_vars; }