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