PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/rocs-4.8.97/src/Models/model_GraphProperties.cpp

#
C++ | 194 lines | 138 code | 27 blank | 29 comment | 44 complexity | 1aaa65b4bd3ae745d9cfa1aaaf827565 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, LGPL-2.1
  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. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "model_GraphProperties.h"
  17. #include <KLocale>
  18. #include <KDebug>
  19. #include <DynamicPropertiesList.h>
  20. #include "Pointer.h"
  21. #include "Data.h"
  22. #include "DataStructure.h"
  23. GraphPropertiesModel::GraphPropertiesModel(QObject *parent) : QAbstractTableModel(parent)
  24. {
  25. // start all pointers to zero, so we don't crash things.
  26. _dataSource = 0;
  27. _metaObject = 0;
  28. }
  29. int GraphPropertiesModel::rowCount(const QModelIndex &parent) const
  30. {
  31. Q_UNUSED(parent);
  32. // if there's no dataSource, there's no data. return zero.
  33. // else return the size of properties of the object.
  34. if (_dataSource == 0) {
  35. return 0;
  36. }
  37. kDebug() << _dataSource->dynamicPropertyNames().size();
  38. return _dataSource->dynamicPropertyNames().size();
  39. }
  40. int GraphPropertiesModel::columnCount(const QModelIndex & parent) const
  41. {
  42. Q_UNUSED(parent);
  43. return 3; //Name - Value - Type
  44. }
  45. QVariant GraphPropertiesModel::data(const QModelIndex &index, int role) const
  46. {
  47. // error conditions, return a default value constructed QVariant().
  48. if (!index.isValid()) {
  49. return QVariant();
  50. }
  51. if (index.row() >= _dataSource->dynamicPropertyNames().size()) {
  52. return QVariant();
  53. }
  54. if (role != Qt::DisplayRole && role != Qt::EditRole) {
  55. return QVariant();
  56. }
  57. const char* propertyName = _dataSource->dynamicPropertyNames()[index.row()];
  58. return (index.column() == 0) ? propertyName
  59. : (index.column() == 1) ? _dataSource->property(propertyName)
  60. : (index.column() == 2) ? DynamicPropertiesList::New()->typeInText(_dataSource, propertyName)
  61. : QVariant();
  62. }
  63. QVariant GraphPropertiesModel::headerData(int section, Qt::Orientation orientation, int role) const
  64. {
  65. // if the role is not for displaying anything, return a default empty value.
  66. if (role != Qt::DisplayRole) {
  67. return QVariant();
  68. }
  69. if (orientation == Qt::Horizontal) {
  70. switch (section) {
  71. case 0: return i18n("Property");
  72. case 1: return i18n("Value");
  73. case 2: return i18n("Type");
  74. }
  75. }
  76. return QVariant();
  77. }
  78. void GraphPropertiesModel::setDataSource(QObject *dataSource)
  79. {
  80. // if there isn't any datasource being send, just remove everything from the model and exit.
  81. if (dataSource == 0) {
  82. int count = rowCount();
  83. if (count == 0) return;
  84. beginRemoveRows(QModelIndex(), 0, count - 1);
  85. endRemoveRows();
  86. return;
  87. }
  88. // clear the model for the new data.
  89. if (_dataSource) {
  90. beginRemoveRows(QModelIndex(), 0, _dataSource->dynamicPropertyNames().size() - 1);
  91. endRemoveRows();
  92. }
  93. // set some needed variables.
  94. _dataSource = dataSource;
  95. _metaObject = _dataSource -> metaObject();
  96. // insert the information.
  97. if (dataSource->dynamicPropertyNames().size() > 0) {
  98. beginInsertRows(QModelIndex(), 0, dataSource->dynamicPropertyNames().size() - 1);
  99. endInsertRows();
  100. }
  101. }
  102. Qt::ItemFlags GraphPropertiesModel::flags(const QModelIndex &index) const
  103. {
  104. if (index.isValid()) {
  105. if (index.column() != 2) {//Can't change type for now
  106. return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
  107. } else {
  108. return QAbstractItemModel::flags(index);
  109. }
  110. }
  111. return Qt::ItemIsEnabled;
  112. }
  113. bool GraphPropertiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
  114. {
  115. if (index.isValid() && role == Qt::EditRole) {
  116. const char* propertyName = _dataSource->dynamicPropertyNames()[index.row()];
  117. if (index.column() == 0 && value.toString() == QString(propertyName) ){
  118. return false;
  119. }
  120. switch (index.column()) {
  121. /* Change name. DinamicPropertiesList take part" name new name object */
  122. case 0: DynamicPropertiesList::New()->changePropertyName(QString(_dataSource->dynamicPropertyNames()[index.row()]), value.toString(), _dataSource); break;
  123. case 1: _dataSource->setProperty(_dataSource->dynamicPropertyNames()[index.row()], value); break; /* just change the values */
  124. default: kDebug() << "shoudn't enter here Â?Â?"; return false;
  125. }
  126. emit dataChanged(index, index);
  127. return true;
  128. }
  129. return false;
  130. }
  131. void GraphPropertiesModel::addDynamicProperty(QString name, QVariant value, QObject *obj, bool isGlobal)
  132. {
  133. // Need check if the propertie already exists
  134. bool insertingRow = false;
  135. if (name.isEmpty()) {
  136. kWarning() << "Cannot add am empty property";
  137. return;
  138. }
  139. if (! obj->dynamicPropertyNames().contains(name.toUtf8())) {
  140. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  141. insertingRow = true;
  142. }
  143. if (isGlobal) {
  144. if (Pointer * pointer = qobject_cast<Pointer*> (obj)) {
  145. pointer->dataStructure()->addPointersDynamicProperty(name, value);
  146. }
  147. if (Data * datum = qobject_cast<Data*> (obj)) {
  148. datum->dataStructure()->addDataDynamicProperty(name, value);
  149. }
  150. } else {
  151. if (Pointer * pointer = qobject_cast<Pointer*> (obj)) {
  152. pointer->addDynamicProperty(name, value);
  153. }
  154. if (Data * datum = qobject_cast<Data*> (obj)) {
  155. datum->addDynamicProperty(name, value);
  156. }
  157. if (DataStructure * dataStructure = qobject_cast<DataStructure*> (obj)) {
  158. dataStructure->addDynamicProperty(name, value);
  159. }
  160. }
  161. if (insertingRow) { /* if inserting, need finish*/
  162. endInsertRows();
  163. }
  164. }