File indexing completed on 2024-04-28 11:20:45

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2010 Miha Čančula <miha.cancula@gmail.com>
0004     SPDX-FileCopyrightText: 2018-2022 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #ifndef CANTOR_DEFAULTVARIABLEMODEL_H
0008 #define CANTOR_DEFAULTVARIABLEMODEL_H
0009 
0010 #include <QAbstractTableModel>
0011 #include "session.h"
0012 #include "expression.h"
0013 
0014 namespace Cantor {
0015 
0016 class DefaultVariableModelPrivate;
0017 
0018 /**
0019  * @brief
0020  * This DefaultVariableModel class is an implementation of QAbstractItemModel
0021  * that can be used with the Variable Manager plugin.
0022  *
0023  * For most uses the addVariable(), removeVariable() and clearVariables() methods are sufficient.
0024  * They can be used from session (directly or by connecting signals to them), or called from
0025  * a subclass.
0026  *
0027  * DefaultVariableModel uses the session to run expressions for changing variables, and it
0028  * gets the commands from the backend's VariableManagementExtension.
0029  * If you do not want this behavior, you can subclass it and reimplement data() and/or setData().
0030  *
0031  * @see Session::variableModel()
0032  */
0033 class CANTOR_EXPORT DefaultVariableModel : public QAbstractTableModel
0034 {
0035     Q_OBJECT
0036     Q_PROPERTY(Session* session READ session)
0037 
0038 public:
0039 
0040     enum {DataRole = Qt::UserRole + 1};
0041 
0042     /**
0043      * A structure representing a variable.
0044      */
0045     struct Variable
0046     {
0047         Variable(): size(0) {}
0048         Variable(QString name, QString value, size_t size = 0, QString type = QString(), QString dimension = QString()) :
0049             name(name), value(value), size(size), type(type), dimension(dimension) {}
0050 
0051         QString name;
0052         QString value;
0053         size_t size;
0054         QString type;
0055         QString dimension;
0056     };
0057 
0058     /**
0059      * Default constructor
0060      * If you are constructing a DefaultVariableModel without subclassing, the @p session must be valid
0061      * and its backends must support a VariableManagementExtension.
0062      *
0063      * This requirement can be avoided by reimplementing setData() in a subclass.
0064      *
0065      * @param session the session this Model belongs to, also becomes the Model's parent.
0066      */
0067     explicit DefaultVariableModel(Session* session);
0068     ~DefaultVariableModel() override;
0069 
0070     /**
0071      * Get the session which created this Model and whose variables it contains
0072      * @return the session
0073      */
0074     Session* session() const;
0075 
0076     /**
0077      * Returns variables, stored in this model, as @see Variable.
0078      */
0079     QList<Variable> variables() const;
0080 
0081     /**
0082      * Returns names of stored variables
0083      */
0084     QStringList variableNames() const;
0085 
0086     /**
0087      * Return functions, stored in this model
0088      */
0089     QStringList functions() const;
0090 
0091     //TODO: improve the description?
0092     /**
0093      * Starts updating variable model (variable lists, etc.). Usually executed after finished all user's commands
0094      */
0095     virtual void update() {};
0096 
0097 public Q_SLOTS:
0098     /**
0099      * Adds a variable to the model.
0100      * If a variable with the same name already exists, it will be overwritten.
0101      * @param name the name of the variable
0102      * @param value the value of the variable
0103      */
0104     void addVariable(const QString& name, const QString& value);
0105     /**
0106      * Convenience method, equivalent to addVariable(variable.name, variable.value)
0107      * @param variable the variable to add
0108      */
0109     void addVariable(const Cantor::DefaultVariableModel::Variable& variable);
0110     /**
0111      * Remove the variable @p name from the model.
0112      * If a variable with the specified @p name doesn't exists, this method does nothing.
0113      * @param name the name of the variable to remove
0114      */
0115     void removeVariable(const QString& name);
0116     /**
0117      * Convenience method, equivalent to removeVariable(variable.name)
0118      * @param variable the variable to remove
0119      */
0120     void removeVariable(const Cantor::DefaultVariableModel::Variable& variable);
0121     /**
0122      * Clears all variables from the model
0123      */
0124     void clearVariables();
0125 
0126     /**
0127      * Clears all functions
0128      */
0129     void clearFunctions();
0130 
0131 
0132 Q_SIGNALS:
0133     /**
0134      * Emitted after adding new variables
0135      * @param variables list of new variables
0136      */
0137     void variablesAdded(const QStringList& variables);
0138 
0139     /**
0140      * Emitted after variables removing
0141      * @param variables list of removed variables
0142      */
0143     void variablesRemoved(const QStringList& variables);
0144 
0145     /**
0146      * Similar to @c variablesAdded
0147      */
0148     void functionsAdded(const QStringList& names);
0149 
0150     /**
0151      * Similar to @c variablesRemoved
0152      */
0153     void functionsRemoved(const QStringList funcs);
0154 
0155 protected:
0156     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0157     bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
0158 
0159     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0160     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0161     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0162     Qt::ItemFlags flags(const QModelIndex& index) const override;
0163 
0164     void setVariables(const QList<DefaultVariableModel::Variable>& newVars);
0165     void setFunctions(const QStringList& newFuns);
0166 
0167     enum Column
0168     {
0169         NameColumn = 0,
0170         ValueColumn = 1,
0171         TypeColumn = 2,
0172         SizeColumn = 3,
0173         DimensionColumn = 4,
0174         ColumnCount = 5
0175     };
0176 
0177 private:
0178 
0179     DefaultVariableModelPrivate* const d_ptr;
0180     Q_DECLARE_PRIVATE(DefaultVariableModel)
0181 };
0182 
0183 bool operator==(const Cantor::DefaultVariableModel::Variable& one, const Cantor::DefaultVariableModel::Variable& other);
0184 
0185 }
0186 
0187 #endif // CANTOR_DEFAULTVARIABLEMODEL_H