Warning, file /education/cantor/src/lib/defaultvariablemodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2010 Miha Čančula <miha.cancula@gmail.com>
0004 */
0005 
0006 #include "defaultvariablemodel.h"
0007 #include <QDebug>
0008 #include <KLocalizedString>
0009 #include "extension.h"
0010 #include "backend.h"
0011 
0012 namespace Cantor
0013 {
0014 
0015 class DefaultVariableModelPrivate
0016 {
0017 public:
0018     QList<DefaultVariableModel::Variable> variables;
0019     QStringList functions;
0020 
0021     Session* session;
0022     VariableManagementExtension* extension;
0023 };
0024 
0025 DefaultVariableModel::DefaultVariableModel(Session* session): QAbstractTableModel(session),
0026 d_ptr(new DefaultVariableModelPrivate)
0027 {
0028     Q_D(DefaultVariableModel);
0029     d->session = session;
0030     if (session)
0031     {
0032         d->extension = dynamic_cast<Cantor::VariableManagementExtension*>(session->backend()->extension(QStringLiteral("VariableManagementExtension")));
0033     }
0034     qDebug() << d->session << d->extension;
0035 }
0036 
0037 DefaultVariableModel::~DefaultVariableModel()
0038 {
0039     Q_D(DefaultVariableModel);
0040     delete d;
0041 }
0042 
0043 int DefaultVariableModel::columnCount(const QModelIndex& parent) const
0044 {
0045     Q_UNUSED(parent);
0046     return ColumnCount;
0047 }
0048 
0049 int DefaultVariableModel::rowCount(const QModelIndex& parent) const
0050 {
0051     if (parent.isValid())
0052     {
0053         return 0;
0054     }
0055     else
0056     {
0057         Q_D(const DefaultVariableModel);
0058         return d->variables.size();
0059     }
0060 }
0061 
0062 QVariant DefaultVariableModel::headerData(int section, Qt::Orientation orientation, int role) const
0063 {
0064     if(role==Qt::DisplayRole && orientation==Qt::Horizontal) {
0065         switch(section) {
0066             case NameColumn:
0067                 return i18nc("@title:column", "Name");
0068 
0069             case ValueColumn:
0070                 return i18nc("@title:column", "Value");
0071                 break;
0072         }
0073     }
0074     return QVariant();
0075 }
0076 
0077 Qt::ItemFlags DefaultVariableModel::flags(const QModelIndex& index) const
0078 {
0079     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
0080 }
0081 
0082 QVariant DefaultVariableModel::data(const QModelIndex& index, int role) const
0083 {
0084     if ((role != Qt::DisplayRole && role != DataRole) || !index.isValid())
0085     {
0086         return QVariant();
0087     }
0088 
0089     Q_D(const DefaultVariableModel);
0090     switch (index.column())
0091     {
0092         case NameColumn:
0093             return QVariant(d->variables[index.row()].name);
0094         case ValueColumn:
0095         {
0096             const Variable& var = d->variables[index.row()];
0097             if (var.value.size() < 1000 || role == DefaultVariableModel::DataRole)
0098                 return QVariant(var.value);
0099             else
0100             {
0101                 if (var.size != 0)
0102                     return QVariant(i18n("<%1 bytes>", QString::number(var.size)));
0103                 else
0104                     return QVariant(i18n("<too big variable>"));
0105             }
0106         }
0107         default:
0108             return QVariant();
0109     }
0110 }
0111 
0112 bool DefaultVariableModel::setData(const QModelIndex& index, const QVariant& value, int role)
0113 {
0114     if(role!=Qt::EditRole || !value.isValid() || !index.isValid())
0115     {
0116         return false;
0117     }
0118 
0119     Q_D(const DefaultVariableModel);
0120     if(index.column() == ValueColumn)
0121     {
0122         // Changing values
0123         QString name = data(index.sibling(index.row(), NameColumn)).toString();
0124         d->session->evaluateExpression(d->extension->setValue(name, value.toString()), Expression::DeleteOnFinish);
0125         emit dataChanged(index, index);
0126         return true;
0127     }
0128     else if(index.column() == NameColumn)
0129     {
0130         // Renaming => copy it, then delete the old one
0131         QString oldName = data(index).toString();
0132         QString variableValue = data(index.sibling(index.row(), ValueColumn)).toString();
0133         d->session->evaluateExpression(d->extension->addVariable(value.toString(), variableValue), Expression::DeleteOnFinish);
0134         d->session->evaluateExpression(d->extension->removeVariable(oldName), Expression::DeleteOnFinish);
0135         emit dataChanged(index, index);
0136         return true;
0137     }
0138     return false;
0139 }
0140 
0141 void DefaultVariableModel::addVariable(const QString& name, const QString& value)
0142 {
0143     Variable v(name, value);
0144     addVariable(v);
0145 }
0146 
0147 void DefaultVariableModel::addVariable(const Cantor::DefaultVariableModel::Variable& variable)
0148 {
0149     Q_D(DefaultVariableModel);
0150     int index = d->variables.indexOf(variable);
0151     if (index != -1)
0152     {
0153         d->variables[index].value = variable.value;
0154         QModelIndex modelIdx = createIndex(index, ValueColumn);
0155         emit dataChanged(modelIdx, modelIdx);
0156     }
0157     else
0158     {
0159         beginInsertRows(QModelIndex(), d->variables.size(), d->variables.size());
0160         d->variables.append(variable);
0161         emit variablesAdded(QStringList(variable.name));
0162         endInsertRows();
0163     }
0164 }
0165 
0166 void DefaultVariableModel::removeVariable(const QString& name)
0167 {
0168     Variable v(name, QString());
0169     removeVariable(v);
0170 }
0171 
0172 void DefaultVariableModel::removeVariable(const Cantor::DefaultVariableModel::Variable& variable)
0173 {
0174     Q_D(DefaultVariableModel);
0175     int row = d->variables.indexOf(variable);
0176     if(row==-1)
0177         return;
0178     const QString& name = variable.name;
0179     beginRemoveRows(QModelIndex(), row, row);
0180     d->variables.removeAt(row);
0181     endRemoveRows();
0182     emit variablesRemoved(QStringList(name));
0183 }
0184 
0185 void DefaultVariableModel::clearVariables()
0186 {
0187     Q_D(DefaultVariableModel);
0188     beginResetModel();
0189 
0190     QStringList names;
0191     for (const Variable& var: d->variables)
0192         names.append(var.name);
0193 
0194     d->variables.clear();
0195     endResetModel();
0196 
0197     emit variablesRemoved(names);
0198 }
0199 
0200 void DefaultVariableModel::clearFunctions()
0201 {
0202     Q_D(DefaultVariableModel);
0203     QStringList names = d->functions;
0204     d->functions.clear();
0205     emit functionsRemoved(names);
0206 }
0207 
0208 void DefaultVariableModel::setVariables(const QList<DefaultVariableModel::Variable>& newVars)
0209 {
0210     Q_D(DefaultVariableModel);
0211 
0212     QStringList addedVars;
0213     QStringList removedVars;
0214 
0215     // Handle deleted vars
0216     int i = 0;
0217     while (i < d->variables.size())
0218     {
0219         Variable var = d->variables[i];
0220         bool found = false;
0221         for (const Variable& newvar : newVars)
0222             if(var.name == newvar.name)
0223             {
0224                 found=true;
0225                 break;
0226             }
0227 
0228         if (!found)
0229         {
0230             removedVars << var.name;
0231             beginRemoveRows(QModelIndex(), i, i);
0232             d->variables.removeAt(i);
0233             endRemoveRows();
0234         }
0235         else
0236             i++;
0237     }
0238 
0239     // Handle added vars
0240     const int size = d->variables.size();
0241     for (const Variable& newvar : newVars)
0242     {
0243         bool found = false;
0244         for (int i = 0; i < size; i++)
0245             if(d->variables[i].name == newvar.name)
0246             {
0247                 found=true;
0248                 if (d->variables[i].value != newvar.value)
0249                 {
0250                     QModelIndex index = createIndex(i, ValueColumn);
0251                     d->variables[i].value = newvar.value;
0252                     d->variables[i].size = newvar.size;
0253                     emit dataChanged(index, index);
0254                 }
0255                 break;
0256             }
0257 
0258         if (!found)
0259         {
0260             addedVars << newvar.name;
0261             beginInsertRows(QModelIndex(), d->variables.size(), d->variables.size());
0262             d->variables.append(newvar);
0263             endInsertRows();
0264         }
0265     }
0266 
0267     emit variablesAdded(addedVars);
0268     emit variablesRemoved(removedVars);
0269 }
0270 
0271 void DefaultVariableModel::setFunctions(const QStringList& newFuncs)
0272 {
0273     Q_D(DefaultVariableModel);
0274     QStringList addedFuncs;
0275     QStringList removedFuncs;
0276 
0277     //remove the old variables
0278     int i = 0;
0279     while (i < d->functions.size())
0280     {
0281         //check if this var is present in the new variables
0282         bool found=false;
0283         for (const QString& func : newFuncs)
0284             if(d->functions[i] == func)
0285             {
0286                 found=true;
0287                 break;
0288             }
0289 
0290         if(!found)
0291         {
0292             removedFuncs<<d->functions[i];
0293             d->functions.removeAt(i);
0294         }
0295         else
0296             i++;
0297     }
0298 
0299     for (const QString& func : newFuncs)
0300     {
0301         if (!d->functions.contains(func))
0302         {
0303             addedFuncs<<func;
0304             d->functions.append(func);
0305         }
0306     }
0307 
0308     emit functionsAdded(addedFuncs);
0309     emit functionsRemoved(removedFuncs);
0310 }
0311 
0312 Session* DefaultVariableModel::session() const
0313 {
0314     Q_D(const DefaultVariableModel);
0315     return d->session;
0316 }
0317 
0318 QList<DefaultVariableModel::Variable> DefaultVariableModel::variables() const
0319 {
0320     Q_D(const DefaultVariableModel);
0321     return d->variables;
0322 }
0323 
0324 QStringList DefaultVariableModel::variableNames() const
0325 {
0326     Q_D(const DefaultVariableModel);
0327     QStringList names;
0328     for (const Variable& var: d->variables)
0329         names << var.name;
0330     return names;
0331 }
0332 
0333 QStringList DefaultVariableModel::functions() const
0334 {
0335     Q_D(const DefaultVariableModel);
0336     return d->functions;
0337 }
0338 
0339 bool operator==(const Cantor::DefaultVariableModel::Variable& one, const Cantor::DefaultVariableModel::Variable& other)
0340 {
0341     return one.name == other.name;
0342 }
0343 
0344 }