PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libs/LeDocumentObject/DocumentObjectTreeModel.cpp

https://gitlab.com/chgans/LibreEDA
C++ | 327 lines | 254 code | 71 blank | 2 comment | 45 complexity | bcdb1875052c283f6194b98b793b1958 MD5 | raw file
  1. #include "DocumentObjectTreeModel.h"
  2. #include "Document.h"
  3. namespace LDO
  4. {
  5. class DocumentObjectTreeModelPrivate
  6. {
  7. Q_DISABLE_COPY(DocumentObjectTreeModelPrivate)
  8. Q_DECLARE_PUBLIC(DocumentObjectTreeModel)
  9. DocumentObjectTreeModel * const q_ptr;
  10. public:
  11. explicit DocumentObjectTreeModelPrivate(DocumentObjectTreeModel *model):
  12. q_ptr(model)
  13. {
  14. }
  15. ~DocumentObjectTreeModelPrivate()
  16. {
  17. }
  18. inline IDocumentObject *object(const QModelIndex &index) const
  19. {
  20. return static_cast<IDocumentObject*>(index.internalPointer());
  21. }
  22. inline IDocumentObject *objectOrRoot(const QModelIndex &index) const
  23. {
  24. if (!index.isValid())
  25. return rootObject;
  26. else
  27. return object(index);
  28. }
  29. Document *document = nullptr;
  30. IDocumentObject *rootObject = nullptr;
  31. };
  32. }
  33. using namespace LDO;
  34. DocumentObjectTreeModel::DocumentObjectTreeModel(QObject *parent)
  35. : QAbstractItemModel(parent)
  36. , IDocumentObjectListener()
  37. , d_ptr(new DocumentObjectTreeModelPrivate(this))
  38. {
  39. }
  40. DocumentObjectTreeModel::~DocumentObjectTreeModel()
  41. {
  42. }
  43. void DocumentObjectTreeModel::setRootObject(Document *document, IDocumentObject *object)
  44. {
  45. Q_D(DocumentObjectTreeModel);
  46. if (d->rootObject != nullptr)
  47. stopListeningRecursive(d->rootObject);
  48. beginResetModel();
  49. d->document = document;
  50. d->rootObject = object;
  51. endResetModel();
  52. if (d->rootObject != nullptr)
  53. startListeningRecursive(d->rootObject);
  54. }
  55. IDocumentObject *DocumentObjectTreeModel::rootObject() const
  56. {
  57. Q_D(const DocumentObjectTreeModel);
  58. return d->rootObject;
  59. }
  60. IDocumentObject *DocumentObjectTreeModel::documentObject(const QModelIndex &index) const
  61. {
  62. Q_D(const DocumentObjectTreeModel);
  63. return d->object(index);
  64. }
  65. QModelIndex DocumentObjectTreeModel::index(const IDocumentObject *object) const
  66. {
  67. Q_D(const DocumentObjectTreeModel);
  68. QModelIndex modelIndex;
  69. if (object != d->rootObject)
  70. modelIndex = createIndex(object->parentObjectIndex(), 0, const_cast<IDocumentObject*>(object));
  71. return modelIndex;
  72. }
  73. QModelIndex DocumentObjectTreeModel::index(int row, int column, const QModelIndex &parent) const
  74. {
  75. Q_D(const DocumentObjectTreeModel);
  76. if (!hasIndex(row, column, parent))
  77. return QModelIndex();
  78. if (d->rootObject == nullptr)
  79. return QModelIndex();
  80. const IDocumentObject *parentItem = d->objectOrRoot(parent);
  81. IDocumentObject *childItem = parentItem->childObject(row);
  82. if (childItem != nullptr)
  83. return createIndex(row, column, childItem);
  84. else
  85. return QModelIndex();
  86. }
  87. QModelIndex DocumentObjectTreeModel::parent(const QModelIndex &index) const
  88. {
  89. Q_D(const DocumentObjectTreeModel);
  90. if (!index.isValid())
  91. return QModelIndex();
  92. IDocumentObject *childItem = d->object(index);
  93. IDocumentObject *parentItem = childItem->parentObject();
  94. if (parentItem == d->rootObject)
  95. return QModelIndex();
  96. return createIndex(parentItem->parentObjectIndex(), 0, parentItem);
  97. }
  98. int DocumentObjectTreeModel::rowCount(const QModelIndex &parent) const
  99. {
  100. Q_D(const DocumentObjectTreeModel);
  101. if (parent.column() > 0)
  102. return 0;
  103. if (d->rootObject == nullptr)
  104. return 0;
  105. const IDocumentObject *parentItem = d->objectOrRoot(parent);
  106. return parentItem->childObjectCount();
  107. }
  108. int DocumentObjectTreeModel::columnCount(const QModelIndex &parent) const
  109. {
  110. Q_UNUSED(parent);
  111. return ColumnCount;
  112. }
  113. QVariant DocumentObjectTreeModel::data(const QModelIndex &index, int role) const
  114. {
  115. Q_D(const DocumentObjectTreeModel);
  116. if (!index.isValid())
  117. return QVariant();
  118. const IDocumentObject *item = d->object(index);
  119. switch (role) {
  120. case Qt::DisplayRole:
  121. case Qt::EditRole:
  122. switch (index.column())
  123. {
  124. case NameColumn:
  125. return item->objectUserName();
  126. default:
  127. return QVariant();
  128. }
  129. break;
  130. case Qt::DecorationRole:
  131. switch (index.column())
  132. {
  133. case NameColumn:
  134. {
  135. // const QString name = d->rootObject->iconName(item);
  136. // return icon(name);
  137. return QVariant();
  138. }
  139. default:
  140. return QVariant();
  141. }
  142. default:
  143. break;
  144. }
  145. return QVariant();
  146. }
  147. QVariant DocumentObjectTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
  148. {
  149. if (orientation != Qt::Horizontal)
  150. return QVariant();
  151. if (role != Qt::DisplayRole)
  152. return QVariant();
  153. switch (section)
  154. {
  155. case NameColumn:
  156. return "Name";
  157. default:
  158. return QVariant();
  159. }
  160. }
  161. Qt::ItemFlags DocumentObjectTreeModel::flags(const QModelIndex &index) const
  162. {
  163. return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
  164. }
  165. bool DocumentObjectTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
  166. {
  167. Q_D(const DocumentObjectTreeModel);
  168. if (!index.isValid())
  169. return false;
  170. if (role != Qt::EditRole)
  171. return false;
  172. if (index.column() != NameColumn)
  173. return false;
  174. if (!value.canConvert<QString>())
  175. return false;
  176. d->object(index)->setObjectUserName(value.toString());
  177. return true;
  178. }
  179. void DocumentObjectTreeModel::documentObjectAboutToBeInserted(IDocumentObject *parent,
  180. IDocumentObject *child,
  181. int index)
  182. {
  183. Q_UNUSED(child)
  184. Q_D(const DocumentObjectTreeModel);
  185. QModelIndex modelIndex;
  186. if (parent != d->rootObject)
  187. modelIndex = createIndex(parent->parentObjectIndex(), 0, const_cast<IDocumentObject*>(parent));
  188. emit beginInsertRows(modelIndex, index, index);
  189. }
  190. void DocumentObjectTreeModel::documentObjectInserted(IDocumentObject *parent,
  191. IDocumentObject *child,
  192. int index)
  193. {
  194. Q_UNUSED(parent)
  195. Q_UNUSED(child)
  196. Q_UNUSED(index)
  197. emit endInsertRows();
  198. startListeningRecursive(child);
  199. }
  200. void DocumentObjectTreeModel::documentObjectAboutToBeRemoved(IDocumentObject *parent,
  201. IDocumentObject *child,
  202. int index)
  203. {
  204. Q_UNUSED(child)
  205. Q_D(const DocumentObjectTreeModel);
  206. QModelIndex modelIndex;
  207. if (parent != d->rootObject)
  208. modelIndex = createIndex(parent->parentObjectIndex(), 0, const_cast<IDocumentObject*>(parent));
  209. emit beginRemoveRows(modelIndex, index, index);
  210. }
  211. void DocumentObjectTreeModel::documentObjectRemoved(IDocumentObject *parent,
  212. IDocumentObject *child,
  213. int index)
  214. {
  215. Q_UNUSED(parent)
  216. Q_UNUSED(child)
  217. Q_UNUSED(index)
  218. emit endRemoveRows();
  219. stopListeningRecursive(child);
  220. }
  221. void DocumentObjectTreeModel::documentObjectAboutToChangeProperty(const IDocumentObject *object,
  222. const QString &name,
  223. const QVariant &oldValue)
  224. {
  225. Q_UNUSED(object)
  226. Q_UNUSED(name)
  227. Q_UNUSED(oldValue)
  228. }
  229. void DocumentObjectTreeModel::documentObjectPropertyChanged(const IDocumentObject *object,
  230. const QString &name, const
  231. QVariant &newValue)
  232. {
  233. Q_UNUSED(name)
  234. Q_UNUSED(newValue)
  235. Q_D(const DocumentObjectTreeModel);
  236. QModelIndex modelIndex;
  237. if (object != d->rootObject)
  238. modelIndex = createIndex(object->parentObjectIndex(), 0, const_cast<IDocumentObject*>(object));
  239. emit dataChanged(modelIndex, modelIndex);
  240. }
  241. void DocumentObjectTreeModel::startListeningRecursive(IDocumentObject *object)
  242. {
  243. if (object == nullptr)
  244. return;
  245. beginListeningToDocumentObject(object);
  246. for (int i = 0; i < object->childObjectCount(); i++)
  247. startListeningRecursive(object->childObject(i));
  248. }
  249. void DocumentObjectTreeModel::stopListeningRecursive(IDocumentObject *object)
  250. {
  251. if (object == nullptr)
  252. return;
  253. for (int i = 0; i < object->childObjectCount(); i++)
  254. stopListeningRecursive(object->childObject(i));
  255. endListeningToDocumentObject(object);
  256. }