kalgebramobile.cpp 4.51 KB
Newer Older
1
/*************************************************************************************
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
2
 *  Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org>                               *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "kalgebramobile.h"

21
#include <analitzaplot/plotsmodel.h>
22
#include <analitza/variables.h>
23
#include <analitza/expression.h>
24

25 26
#include "../src/consolemodel.h"

27
#include <QSortFilterProxyModel>
28
#include <qqml.h>
29
#include "pluginsmodel.h"
30
#include "clipboard.h"
31

32 33
using namespace Analitza;

34 35 36
KAlgebraMobile* KAlgebraMobile::s_self=0;
KAlgebraMobile* KAlgebraMobile::self() { return s_self; }

37 38
Q_DECLARE_METATYPE(QSharedPointer<Analitza::Variables>)

39
KAlgebraMobile::KAlgebraMobile(QObject* parent)
40
    : QObject(parent), m_functionsModel(0), m_vars(new Analitza::Variables)
41
{
42 43 44
    Q_ASSERT(s_self==0);
    s_self=this;
    
45
    const auto uri = "org.kde.kalgebra.mobile";
46
    qmlRegisterType<PluginsModel>("org.kde.kalgebra.mobile", 1, 0, "PluginsModel");
47
    qmlRegisterType<ConsoleModel>("org.kde.kalgebra.mobile", 1, 0, "ConsoleModel");
48
    qmlRegisterType<QSortFilterProxyModel>("org.kde.kalgebra.mobile", 1, 0, "QSortFilterProxyModel");
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
49
    qmlRegisterUncreatableType<QAbstractItemModel>("org.kde.kalgebra.mobile", 1, 0, "QAbstractItemModel", "no");
50
    qmlRegisterType<Clipboard>(uri, 1, 0, "Clipboard");
51 52 53 54 55 56

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
    qmlRegisterType<QStandardItemModel>();
#else
    qmlRegisterAnonymousType<QStandardItemModel>("Kalgebra", 1);
#endif
57
    qmlRegisterUncreatableType<Analitza::Expression>("org.kde.kalgebra.mobile", 1, 0, "Expression", QStringLiteral("because"));
58
    qRegisterMetaType<QSharedPointer<Analitza::Variables>>("QSharedPointer<Analitza::Variables>");
59 60
}

61
PlotsModel* KAlgebraMobile::functionsModel()
62
{
63 64
    if(!m_functionsModel) {
        m_functionsModel = new Analitza::PlotsModel(this);
65 66 67
        connect(m_functionsModel, &QAbstractItemModel::rowsRemoved, this, &KAlgebraMobile::functionRemoved);
        connect(m_functionsModel, &QAbstractItemModel::rowsInserted, this, &KAlgebraMobile::functionInserted);
        connect(m_functionsModel, &QAbstractItemModel::dataChanged, this, &KAlgebraMobile::functionModified);
68 69 70
    }
    
    return m_functionsModel;
71 72
}

73
void KAlgebraMobile::functionInserted(const QModelIndex& parent, int start, int end)
74
{
75 76 77 78 79
    Q_ASSERT(!parent.isValid());
    for(int i=start; i<=end; i++) {
        QModelIndex idx = functionsModel()->index(i, 1);
        m_vars->modify(idx.sibling(i,0).data().toString(), Analitza::Expression(idx.data().toString()));
    }
80 81 82 83
}

void KAlgebraMobile::functionRemoved(const QModelIndex& parent, int start, int end)
{
84 85 86 87 88
    Q_ASSERT(!parent.isValid());
    for(int i=start; i<=end; i++) {
        QModelIndex idx = functionsModel()->index(i);
        m_vars->remove(functionsModel()->data(idx, Qt::DisplayRole).toString());
    }
89 90
}

91
void KAlgebraMobile::functionModified(const QModelIndex& idxA, const QModelIndex& idxB)
92
{
93 94 95 96 97 98
    if(idxB.column()==1) {
        for(int i=idxA.row(); i<idxB.row(); ++i) {
            QModelIndex idx = functionsModel()->index(i, 1);
            m_vars->modify(idx.sibling(i,0).data().toString(), Analitza::Expression(idx.data().toString()));
        }
    } //else TODO: figure out how to control a "rename"
99 100
}

101
QSharedPointer<Analitza::Variables> KAlgebraMobile::variables() const { return m_vars; }