• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

cantor/src/lib

  • sources
  • kde-4.14
  • kdeedu
  • cantor
  • src
  • lib
defaultvariablemodel.cpp
Go to the documentation of this file.
1 /*
2  This program is free software; you can redistribute it and/or
3  modify it under the terms of the GNU General Public License
4  as published by the Free Software Foundation; either version 2
5  of the License, or (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor,
15  Boston, MA 02110-1301, USA.
16 
17  ---
18  Copyright (C) 2010 Miha Čančula <miha.cancula@gmail.com>
19  */
20 
21 #include "defaultvariablemodel.h"
22 #include <KDebug>
23 #include <KLocale>
24 #include "extension.h"
25 #include "backend.h"
26 
27 namespace Cantor
28 {
29 
30 class DefaultVariableModelPrivate
31 {
32 public:
33  QList<DefaultVariableModel::Variable> variables;
34 
35  Session* session;
36  VariableManagementExtension* extension;
37 };
38 
39 DefaultVariableModel::DefaultVariableModel(Session* session): QAbstractTableModel(session),
40 d_ptr(new DefaultVariableModelPrivate)
41 {
42  Q_D(DefaultVariableModel);
43  d->session = session;
44  if (session)
45  {
46  d->extension = dynamic_cast<Cantor::VariableManagementExtension*>(session->backend()->extension("VariableManagementExtension"));
47  }
48  kDebug() << d->session << d->extension;
49 }
50 
51 DefaultVariableModel::~DefaultVariableModel()
52 {
53 
54 }
55 
56 int DefaultVariableModel::columnCount(const QModelIndex& parent) const
57 {
58  Q_UNUSED(parent);
59  return ColumnCount;
60 }
61 
62 int DefaultVariableModel::rowCount(const QModelIndex& parent) const
63 {
64  if (parent.isValid())
65  {
66  return 0;
67  }
68  else
69  {
70  Q_D(const DefaultVariableModel);
71  return d->variables.size();
72  }
73 }
74 QVariant DefaultVariableModel::headerData(int section, Qt::Orientation orientation, int role) const
75 {
76  if(role==Qt::DisplayRole && orientation==Qt::Horizontal) {
77  switch(section) {
78  case NameColumn:
79  return i18nc("@title:column", "Name");
80 
81  case ValueColumn:
82  return i18nc("@title:column", "Value");
83  break;
84  }
85  }
86  return QVariant();
87 }
88 
89 Qt::ItemFlags DefaultVariableModel::flags(const QModelIndex& index) const
90 {
91  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
92 }
93 
94 
95 QVariant DefaultVariableModel::data(const QModelIndex& index, int role) const
96 {
97  if (role != Qt::DisplayRole || !index.isValid())
98  {
99  return QVariant();
100  }
101 
102  Q_D(const DefaultVariableModel);
103  switch (index.column())
104  {
105  case NameColumn:
106  return QVariant(d->variables[index.row()].name);
107  case ValueColumn:
108  return QVariant(d->variables[index.row()].value);
109  default:
110  return QVariant();
111  }
112 }
113 
114 bool DefaultVariableModel::setData(const QModelIndex& index, const QVariant& value, int role)
115 {
116  if(role!=Qt::EditRole || !value.isValid() || !index.isValid())
117  {
118  return false;
119  }
120 
121  Q_D(const DefaultVariableModel);
122  if(index.column() == ValueColumn)
123  {
124  // Changing values
125  QString name = data(index.sibling(index.row(), NameColumn)).toString();
126  d->session->evaluateExpression(d->extension->setValue(name, value.toString()), Expression::DeleteOnFinish);
127  return true;
128  }
129  else if(index.column() == NameColumn)
130  {
131  // Renaming => copy it, then delete the old one
132  QString oldName = data(index).toString();
133  QString variableValue = data(index.sibling(index.row(), ValueColumn)).toString();
134  d->session->evaluateExpression(d->extension->addVariable(value.toString(), variableValue), Expression::DeleteOnFinish);
135  d->session->evaluateExpression(d->extension->removeVariable(oldName), Expression::DeleteOnFinish);
136  return true;
137  }
138  return false;
139 }
140 
141 
142 void DefaultVariableModel::addVariable(const QString& name, const QString& value)
143 {
144  Variable v;
145  v.name = name;
146  v.value = value;
147  addVariable(v);
148 }
149 
150 void DefaultVariableModel::addVariable(const Cantor::DefaultVariableModel::Variable& variable)
151 {
152  Q_D(DefaultVariableModel);
153  if ( d->variables.contains(variable) )
154  {
155  removeVariable(variable);
156  }
157  beginInsertRows(QModelIndex(), d->variables.size(), d->variables.size());
158  d->variables.append(variable);
159  endInsertRows();
160 }
161 
162 void DefaultVariableModel::removeVariable(const QString& name)
163 {
164  Variable v;
165  v.name = name;
166  removeVariable(v);
167 }
168 
169 void DefaultVariableModel::removeVariable(const Cantor::DefaultVariableModel::Variable& variable)
170 {
171  Q_D(DefaultVariableModel);
172  int row = d->variables.indexOf(variable);
173  if(row==-1)
174  return;
175  beginRemoveRows(QModelIndex(), row, row);
176  d->variables.removeAt(row);
177  endRemoveRows();
178 }
179 
180 void DefaultVariableModel::clearVariables()
181 {
182  Q_D(DefaultVariableModel);
183  beginResetModel();
184  d->variables.clear();
185  endResetModel();
186 }
187 
188 Session* DefaultVariableModel::session() const
189 {
190  Q_D(const DefaultVariableModel);
191  return d->session;
192 }
193 
194 bool operator==(const Cantor::DefaultVariableModel::Variable& one, const Cantor::DefaultVariableModel::Variable& other)
195 {
196  return one.name == other.name;
197 }
198 
199 }
200 
201 #include "defaultvariablemodel.moc"
202 
QModelIndex
Cantor::DefaultVariableModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: defaultvariablemodel.cpp:95
Cantor::DefaultVariableModel
This DefaultVariableModel class is an implementation of QAbstractItemModel that can be used with the ...
Definition: defaultvariablemodel.h:47
QAbstractTableModel
Cantor::DefaultVariableModel::clearVariables
void clearVariables()
Clears all variables from the model.
Definition: defaultvariablemodel.cpp:180
Cantor::DefaultVariableModel::Variable
A structure representing a variable.
Definition: defaultvariablemodel.h:57
Cantor::Session::backend
Backend * backend()
Returns the Backend, this Session is for.
Definition: session.cpp:58
Cantor::DefaultVariableModel::session
Session * session() const
Get the session which created this Model and whose variables it contains.
Cantor::Backend::extension
Extension * extension(const QString &name) const
Returns an Extension of this backend for the given name, or null if the Backend doesn't have an exten...
Definition: backend.cpp:176
Cantor::DefaultVariableModel::removeVariable
void removeVariable(const QString &name)
Remove the variable name from the model.
Definition: defaultvariablemodel.cpp:162
QAbstractItemModel::beginResetModel
void beginResetModel()
Cantor::DefaultVariableModel::ColumnCount
Definition: defaultvariablemodel.h:130
QObject::name
const char * name() const
QModelIndex::isValid
bool isValid() const
Cantor::Expression::DeleteOnFinish
< The Object will delete itself when finished.
Definition: expression.h:64
defaultvariablemodel.h
QAbstractItemModel::endInsertRows
void endInsertRows()
Cantor::DefaultVariableModel::NameColumn
Definition: defaultvariablemodel.h:128
Cantor::operator==
bool operator==(const Cantor::DefaultVariableModel::Variable &one, const Cantor::DefaultVariableModel::Variable &other)
Definition: defaultvariablemodel.cpp:194
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
Cantor::DefaultVariableModel::columnCount
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: defaultvariablemodel.cpp:56
QString
QList
Cantor::DefaultVariableModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Definition: defaultvariablemodel.cpp:89
extension.h
Cantor::DefaultVariableModel::DefaultVariableModel
DefaultVariableModel(Session *session)
Default constructor If you are constructing a DefaultVariableModel without subclassing, the session must be valid and its backends must support a VariableManagementExtension.
Definition: defaultvariablemodel.cpp:39
Cantor::DefaultVariableModel::ValueColumn
Definition: defaultvariablemodel.h:129
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
Cantor::DefaultVariableModel::addVariable
void addVariable(const QString &name, const QString &value)
Adds a variable to the model.
Definition: defaultvariablemodel.cpp:142
Cantor::DefaultVariableModel::Variable::name
QString name
The variable's name.
Definition: defaultvariablemodel.h:62
Cantor::DefaultVariableModel::~DefaultVariableModel
virtual ~DefaultVariableModel()
Definition: defaultvariablemodel.cpp:51
QModelIndex::sibling
QModelIndex sibling(int row, int column) const
Cantor::DefaultVariableModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: defaultvariablemodel.cpp:114
QModelIndex::column
int column() const
QVariant::isValid
bool isValid() const
QAbstractItemModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
QAbstractItemModel::endRemoveRows
void endRemoveRows()
backend.h
QAbstractItemModel::endResetModel
void endResetModel()
Cantor::DefaultVariableModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: defaultvariablemodel.cpp:62
Cantor::VariableManagementExtension
Definition: extension.h:448
QVariant::toString
QString toString() const
Cantor::DefaultVariableModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: defaultvariablemodel.cpp:74
QVariant
Qt::ItemFlags
typedef ItemFlags
Cantor::DefaultVariableModel::Variable::value
QString value
The variable's value, represented as a string.
Definition: defaultvariablemodel.h:66
Cantor::Session
The Session object is the main class used to interact with a Backend.
Definition: session.h:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

cantor/src/lib

Skip menu "cantor/src/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal