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

rocs/VisualEditor

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • VisualEditor
  • Interface
model_GraphProperties.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2008-2011 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2010 Wagner Reck <wagner.reck@gmail.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "model_GraphProperties.h"
21 #include <KLocale>
22 #include <KDebug>
23 #include "Pointer.h"
24 #include "Data.h"
25 #include "DataStructure.h"
26 
27 GraphPropertiesModel::GraphPropertiesModel(QObject *parent) : QAbstractTableModel(parent)
28 {
29  // start all pointers to zero, so we don't crash things.
30  _dataSource = 0;
31  _metaObject = 0;
32 }
33 
34 int GraphPropertiesModel::rowCount(const QModelIndex &parent) const
35 {
36  Q_UNUSED(parent);
37  // if there's no dataSource, there's no data. return zero.
38  // else return the size of properties of the object.
39  if (_dataSource == 0) {
40  return 0;
41  }
42  return _dataSource->dynamicPropertyNames().size();
43 }
44 
45 int GraphPropertiesModel::columnCount(const QModelIndex & parent) const
46 {
47  Q_UNUSED(parent);
48  return 2;
49 }
50 
51 QVariant GraphPropertiesModel::data(const QModelIndex &index, int role) const
52 {
53  // error conditions, return a default value constructed QVariant().
54  if (!index.isValid()) {
55  return QVariant();
56  }
57  if (index.row() >= _dataSource->dynamicPropertyNames().size()) {
58  return QVariant();
59  }
60 
61  if (role == Qt::DisplayRole) {
62  const char* propertyName = _dataSource->dynamicPropertyNames()[index.row()];
63  return (index.column() == 0) ? propertyName
64  : (index.column() == 1) ? _dataSource->property(propertyName)
65  : QVariant();
66  }
67 
68  // only edit value
69  if (role == Qt::EditRole) {
70  const char* propertyName = _dataSource->dynamicPropertyNames()[index.row()];
71  return (index.column() == 1) ? _dataSource->property(propertyName)
72  : QVariant();
73  }
74 
75  return QVariant();
76 
77 }
78 
79 QVariant GraphPropertiesModel::headerData(int section, Qt::Orientation orientation, int role) const
80 {
81  // if the role is not for displaying anything, return a default empty value.
82  if (role != Qt::DisplayRole) {
83  return QVariant();
84  }
85 
86  if (orientation == Qt::Horizontal) {
87  switch (section) {
88  case 0: return i18n("Property");
89  case 1: return i18n("Value");
90  }
91  }
92  return QVariant();
93 }
94 
95 void GraphPropertiesModel::setDataSource(QObject *dataSource)
96 {
97  // if there isn't any datasource being send, just remove everything from the model and exit.
98  if (dataSource == 0) {
99  int count = rowCount();
100  if (count == 0) {
101  return;
102  }
103  beginRemoveRows(QModelIndex(), 0, count - 1);
104  endRemoveRows();
105  return;
106  }
107 
108  // clear the model for the new data.
109  if (_dataSource) {
110  beginRemoveRows(QModelIndex(), 0, _dataSource->dynamicPropertyNames().size() - 1);
111  endRemoveRows();
112  }
113 
114  // set some needed variables.
115  _dataSource = dataSource;
116  _metaObject = _dataSource -> metaObject();
117 
118  // insert the information.
119 
120  if (dataSource->dynamicPropertyNames().size() > 0) {
121  beginInsertRows(QModelIndex(), 0, dataSource->dynamicPropertyNames().size() - 1);
122  endInsertRows();
123  }
124 }
125 
126 Qt::ItemFlags GraphPropertiesModel::flags(const QModelIndex &index) const
127 {
128  if (index.isValid()) {
129  if (index.column() == 1) { // only the value can be edited
130  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
131  } else {
132  return QAbstractItemModel::flags(index);
133  }
134  }
135  return Qt::ItemIsEnabled;
136 }
137 
138 bool GraphPropertiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
139 {
140  if (index.isValid() && role == Qt::EditRole) {
141  const char* propertyName = _dataSource->dynamicPropertyNames()[index.row()];
142  if (index.column() == 0 && value.toString() == QString(propertyName) ){
143  return false;
144  }
145 
146  switch (index.column()) {
147  case 0: {
148  if (!Document::isValidIdentifier(value.toString())) {
149  kWarning() << "Property identifier is not valid: aborting";
150  return false;
151  }
152  QByteArray oldName = QByteArray(_dataSource->dynamicPropertyNames()[index.row()]);
153  QByteArray newName = value.toByteArray();
154  _dataSource->setProperty(newName, property(oldName));
155  _dataSource->setProperty(oldName, QVariant::Invalid);
156  break;
157  }
158  case 1: _dataSource->setProperty(_dataSource->dynamicPropertyNames()[index.row()], value); break; /* just change the values */
159  default: kDebug() << "shoudn't enter here"; return false;
160  }
161 
162  emit dataChanged(index, index);
163  return true;
164 
165  }
166  return false;
167 
168 }
169 
170 void GraphPropertiesModel::addDynamicProperty(QString name, QVariant value, QObject *obj)
171 {
172  // Need check if the property already exists
173  bool insertingRow = false;
174  if (name.isEmpty()) {
175  kWarning() << "Cannot add an empty property";
176  return;
177  }
178 
179  if (! obj->dynamicPropertyNames().contains(name.toUtf8())) {
180  beginInsertRows(QModelIndex(), rowCount(), rowCount());
181  insertingRow = true;
182  }
183 
184  if (Pointer * pointer = qobject_cast<Pointer*> (obj)) {
185  pointer->addDynamicProperty(name, value);
186  }
187  if (Data * datum = qobject_cast<Data*> (obj)) {
188  datum->addDynamicProperty(name, value);
189  }
190  if (DataStructure * dataStructure = qobject_cast<DataStructure*> (obj)) {
191  dataStructure->addDynamicProperty(name, value);
192  }
193 
194  if (insertingRow) { /* if inserting, need finish*/
195  endInsertRows();
196  }
197 }
QModelIndex
QVariant::toByteArray
QByteArray toByteArray() const
GraphPropertiesModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: model_GraphProperties.cpp:138
QByteArray
GraphPropertiesModel::GraphPropertiesModel
GraphPropertiesModel(QObject *parent=0)
Definition: model_GraphProperties.cpp:27
QAbstractTableModel
QObject::metaObject
virtual const QMetaObject * metaObject() const
GraphPropertiesModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: model_GraphProperties.cpp:34
QModelIndex::isValid
bool isValid() const
QObject::property
QVariant property(const char *name) const
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QAbstractItemModel::endInsertRows
void endInsertRows()
QObject
QString::isEmpty
bool isEmpty() const
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
GraphPropertiesModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: model_GraphProperties.cpp:79
QString
model_GraphProperties.h
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
GraphPropertiesModel::setDataSource
void setDataSource(QObject *dataSource)
Definition: model_GraphProperties.cpp:95
GraphPropertiesModel::addDynamicProperty
void addDynamicProperty(QString name, QVariant value, QObject *obj)
Add properti to data source and insert a new row,.
Definition: model_GraphProperties.cpp:170
GraphPropertiesModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: model_GraphProperties.cpp:51
QModelIndex::column
int column() const
QAbstractItemModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
QObject::setProperty
bool setProperty(const char *name, const QVariant &value)
GraphPropertiesModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: model_GraphProperties.cpp:126
QObject::dynamicPropertyNames
QList< QByteArray > dynamicPropertyNames() const
QAbstractItemModel::endRemoveRows
void endRemoveRows()
QVariant::toString
QString toString() const
GraphPropertiesModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: model_GraphProperties.cpp:45
QVariant
Qt::ItemFlags
typedef ItemFlags
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/VisualEditor

Skip menu "rocs/VisualEditor"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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