File indexing completed on 2022-11-29 18:09:59

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 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 "variablesmodel.h"
0020 #include <analitza/expression.h>
0021 #include <analitza/value.h>
0022 #include <analitza/analitzautils.h>
0023 #include <QDebug>
0024 #include <QCoreApplication>
0025 
0026 using namespace Analitza;
0027 
0028 VariablesModel::VariablesModel(const QSharedPointer<Variables>& v, QObject *parent)
0029     : QAbstractTableModel(parent), m_vars(v), m_editable(true)
0030 {}
0031 
0032 VariablesModel::VariablesModel(QObject* parent)
0033     : QAbstractTableModel(parent), m_vars(nullptr), m_editable(true)
0034 {}
0035 
0036 void VariablesModel::setVariables(const QSharedPointer<Variables> &v)
0037 {
0038     m_vars = v;
0039 }
0040 
0041 QVariant VariablesModel::data(const QModelIndex & index, int role) const
0042 {
0043     QVariant ret;
0044     if(role==Qt::DisplayRole) {
0045         switch(index.column()) {
0046             case 0:
0047                 ret=m_vars->keys()[index.row()];
0048                 break;
0049             case 1:
0050                 return data(index.sibling(index.row(), 0), Qt::ToolTipRole);
0051         }
0052     } else if(role==Qt::ToolTipRole && index.column()==0) {
0053         QString key = m_vars->keys()[index.row()];
0054         if(m_vars->value(key)->type()==Object::value) {
0055             Cn* v=static_cast<Cn*>(m_vars->value(key));
0056             ret=v->value();
0057         } else
0058             ret=m_vars->value(key)->toString();
0059     } else if(role==Qt::WhatsThisRole && index.column()==0) {
0060         ret = QStringLiteral("%1 := %2").arg(index.sibling(index.row(), 0).data(Qt::DisplayRole).toString(),
0061                                              index.sibling(index.row(), 1).data(Qt::DisplayRole).toString());
0062     }
0063     return ret;
0064 }
0065 
0066 bool VariablesModel::setData(const QModelIndex& index, const QVariant& value, int role)
0067 {
0068     if(role!=Qt::EditRole || !value.isValid())
0069         return false;
0070     
0071     if(index.column()==1) { //Changing values
0072         QString name=data(index.sibling(index.row(), 0)).toString();
0073         m_vars->modify(name, AnalitzaUtils::variantToExpression(value));
0074         emit dataChanged(index, index);
0075         return true;
0076     } else if(index.column()==0) {
0077         QString name=data(index.sibling(index.row(), 0)).toString();
0078         m_vars->rename(name, value.toString());
0079         emit dataChanged(index, index);
0080         return true;
0081     }
0082     return false;
0083 }
0084 
0085 QVariant VariablesModel::headerData(int section, Qt::Orientation orientation, int role) const
0086 {
0087     QVariant ret;
0088     if(role==Qt::DisplayRole && orientation==Qt::Horizontal) {
0089         switch(section) {
0090             case 0:
0091                 ret=QCoreApplication::translate("@title:column", "Name");
0092                 break;
0093             case 1:
0094                 ret=QCoreApplication::translate("@title:column", "Value");
0095                 break;
0096         }
0097     }
0098     return ret;
0099 }
0100 
0101 int VariablesModel::rowCount(const QModelIndex &idx) const
0102 {
0103     if(!m_vars || idx.isValid())
0104         return 0;
0105     else
0106         return m_vars->count();
0107 }
0108 
0109 QFlags< Qt::ItemFlag > VariablesModel::flags(const QModelIndex& index) const
0110 {
0111     QFlags< Qt::ItemFlag > ret = QAbstractItemModel::flags(index);
0112     if(index.column()==1 && m_editable)
0113         ret |= Qt::ItemIsEditable;
0114     return ret;
0115 }
0116 
0117 void VariablesModel::updateInformation()
0118 {
0119     beginResetModel();
0120     endResetModel();
0121 }
0122 
0123 void VariablesModel::insertVariable(const QString& name, const Expression& value)
0124 {
0125     beginResetModel();
0126     m_vars->modify(name, value);
0127     endResetModel();
0128 }
0129 
0130