QxNestedModel.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_NESTED_MODEL_H_
  32. #define _QX_NESTED_MODEL_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxNestedModel.h
  38. * \author Lionel Marty
  39. * \ingroup QxModelView
  40. * \brief qx::model_view::create_nested_model is used by QxEntityEditor to manage complex data structure to work with relationships in QML views and Qt model/view architecture
  41. */
  42. #include <QxConvert/QxConvert.h>
  43. #include <QxCollection/QxCollection.h>
  44. #include <QxTraits/is_qx_registered.h>
  45. #include <QxTraits/is_container.h>
  46. #include <QxTraits/is_smart_ptr.h>
  47. #include <QxTraits/get_base_class.h>
  48. #include <QxTraits/get_class_name_primitive.h>
  49. #include <QxTraits/construct_ptr.h>
  50. #include <QxTraits/generic_container.h>
  51. #include <QxTraits/is_valid_primary_key.h>
  52. #include <QxSerialize/QxClone.h>
  53. #include <QxModelView/IxModel.h>
  54. #include <QxModelView/QxModel.h>
  55. namespace qx {
  56. namespace model_view {
  57. template <class T>
  58. qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t);
  59. template <class T, class M>
  60. qx::IxModel * create_nested_model_with_type(qx::IxModel * pParent, const QModelIndex & idxParent, T & t, M * pModelType);
  61. template <class T>
  62. void sync_nested_model(qx::IxModel * pModel, T & t);
  63. } // namespace model_view
  64. } // namespace qx
  65. namespace qx {
  66. namespace model_view {
  67. namespace detail {
  68. template <class T, class M /* = void */>
  69. struct QxNestedModel;
  70. template <class T, bool bIsQObject /* = false */>
  71. struct QxNestedModel_Helper
  72. {
  73. static std::shared_ptr<T> clone(T & t)
  74. { std::shared_ptr<T> p = std::make_shared<T>(); (* p) = t; return p; }
  75. static void synchronize(T & t1, T & t2)
  76. { t1 = t2; }
  77. };
  78. template <class T>
  79. struct QxNestedModel_Helper<T, true>
  80. {
  81. static std::shared_ptr<T> clone(T & t)
  82. { std::shared_ptr<T> p; p.reset(qx::clone_to_nude_ptr(t)); qAssert(p); return p; }
  83. static void synchronize(T & t1, T & t2)
  84. {
  85. qx::IxClass * pClass = qx::QxClass<T>::getSingleton();
  86. qx::IxDataMemberX * pDataMemberX = (pClass ? pClass->getDataMemberX() : NULL);
  87. for (long l = 0; (pDataMemberX && (l < pDataMemberX->count_WithDaoStrategy())); l++)
  88. {
  89. qx::IxDataMember * pDataMember = pDataMemberX->get_WithDaoStrategy(l); if (! pDataMember) { continue; }
  90. QVariant value = pDataMember->toVariant(& t2);
  91. pDataMember->fromVariant((& t1), value);
  92. }
  93. }
  94. };
  95. template <class T, class M, bool bIsTypeVoid /* = false */>
  96. struct QxNestedModel_Creator
  97. { static qx::IxModel * newModel(qx::IxModel * pParent) { return new M(pParent); } };
  98. template <class T, class M>
  99. struct QxNestedModel_Creator<T, M, true>
  100. { static qx::IxModel * newModel(qx::IxModel * pParent) { return new qx::QxModel<T>(pParent); } };
  101. template <class T, class M /* = void */>
  102. struct QxNestedModel_Generic
  103. {
  104. typedef typename qx::QxModel<T>::type_collection type_collection;
  105. typedef typename qx::QxModel<T>::type_primary_key type_primary_key;
  106. typedef typename qx::QxModel<T>::type_ptr type_ptr;
  107. enum { is_valid = qx::trait::is_qx_registered<T>::value };
  108. static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
  109. {
  110. static_assert(is_valid, "is_valid");
  111. qx::IxModel * pModel = qx::model_view::detail::QxNestedModel_Creator<T, M, std::is_same<M, void>::value>::newModel(pParent);
  112. pModel->setParentModel(pParent);
  113. type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
  114. long & idx = pModel->m_lManualInsertIndex;
  115. type_primary_key key;
  116. pModel->beginInsertRows(idxParent, 0, 0);
  117. type_ptr ptr = qx::model_view::detail::QxNestedModel_Helper<T, std::is_base_of<QObject, T>::value>::clone(t);
  118. qx::IxDataMember * pDataMemberId = pModel->m_pDataMemberId;
  119. if (! pDataMemberId) { qAssert(false); pModel->endInsertRows(); return pModel; }
  120. QVariant value = pDataMemberId->toVariant(& t);
  121. if (! qx::trait::is_valid_primary_key(value))
  122. { idx--; value = QVariant(static_cast<qlonglong>(idx)); }
  123. qx::cvt::from_variant(value, key);
  124. pListOfItems->insert(0, key, ptr);
  125. pModel->endInsertRows();
  126. return pModel;
  127. }
  128. static inline void synchronize(qx::IxModel * pModel, T & t)
  129. {
  130. if (! pModel) { qAssert(false); return; }
  131. type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
  132. if (! pListOfItems || (pListOfItems->count() <= 0)) { return; }
  133. type_ptr ptr = pListOfItems->getByIndex(0); if (! ptr) { return; }
  134. qx::model_view::detail::QxNestedModel_Helper<T, std::is_base_of<QObject, T>::value>::synchronize(t, (* ptr));
  135. }
  136. };
  137. template <class T, class M /* = void */>
  138. struct QxNestedModel_Container
  139. {
  140. typedef qx::trait::generic_container<T> type_generic_container;
  141. typedef typename type_generic_container::type_value_qx type_data;
  142. typedef typename type_generic_container::type_item type_item;
  143. typedef typename qx::QxModel<type_data>::type_collection type_collection;
  144. typedef typename qx::QxModel<type_data>::type_primary_key type_primary_key;
  145. typedef typename qx::QxModel<type_data>::type_ptr type_ptr;
  146. enum { is_valid = qx::trait::is_qx_registered<type_data>::value };
  147. static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
  148. {
  149. int iCurrRow = 0;
  150. static_assert(is_valid, "is_valid");
  151. qx::IxModel * pModel = qx::model_view::detail::QxNestedModel_Creator<type_data, M, std::is_same<M, void>::value>::newModel(pParent);
  152. pModel->setParentModel(pParent);
  153. long lCount = static_cast<long>(type_generic_container::size(t));
  154. if (lCount <= 0) { return pModel; }
  155. pModel->beginInsertRows(idxParent, 0, (lCount - 1));
  156. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  157. { insertItem(pModel, (* it), iCurrRow); iCurrRow++; }
  158. pModel->endInsertRows();
  159. return pModel;
  160. }
  161. template <typename U>
  162. static inline bool insert(qx::IxModel * pModel, U & item, int iRow)
  163. {
  164. if (! pModel) { qAssert(false); return false; }
  165. type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
  166. long & idx = pModel->m_lManualInsertIndex;
  167. type_primary_key key;
  168. type_ptr ptr = qx::model_view::detail::QxNestedModel_Helper<U, std::is_base_of<QObject, U>::value>::clone(item);
  169. qx::IxDataMember * pDataMemberId = pModel->m_pDataMemberId;
  170. if (! pDataMemberId) { qAssert(false); return false; }
  171. QVariant value = pDataMemberId->toVariant(& item);
  172. if (! qx::trait::is_valid_primary_key(value))
  173. { idx--; value = QVariant(static_cast<qlonglong>(idx)); }
  174. qx::cvt::from_variant(value, key);
  175. pListOfItems->insert(iRow, key, ptr);
  176. return true;
  177. }
  178. static inline void synchronize(qx::IxModel * pModel, T & t)
  179. {
  180. if (! pModel) { qAssert(false); return; }
  181. type_generic_container::clear(t);
  182. type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
  183. for (long l = 0; l < pListOfItems->count(); l++)
  184. {
  185. type_ptr ptr = pListOfItems->getByIndex(l); if (! ptr) { continue; }
  186. type_item item = type_generic_container::createItem();
  187. type_data & item_val = item.value_qx();
  188. qx::model_view::detail::QxNestedModel_Helper<type_data, std::is_base_of<QObject, type_data>::value>::synchronize(item_val, (* ptr));
  189. QVariant vKey = QVariant(static_cast<qlonglong>(l));
  190. qx::cvt::from_variant(vKey, item.key());
  191. type_generic_container::insertItem(t, item);
  192. }
  193. }
  194. private:
  195. template <typename U>
  196. static inline bool insertItem(qx::IxModel * pModel, U & item, int iRow)
  197. { return insertItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::insert(pModel, item, iRow); }
  198. template <typename U, bool bIsPointer /* = true */>
  199. struct insertItem_Helper
  200. {
  201. static inline bool insert(qx::IxModel * pModel, U & item, int iRow)
  202. { return (item ? qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, (* item), iRow) : true); }
  203. };
  204. template <typename U1, typename U2>
  205. struct insertItem_Helper<std::pair<U1, U2>, false>
  206. {
  207. static inline bool insert(qx::IxModel * pModel, std::pair<U1, U2> & item, int iRow)
  208. { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
  209. };
  210. template <typename U1, typename U2>
  211. struct insertItem_Helper<const std::pair<U1, U2>, false>
  212. {
  213. static inline bool insert(qx::IxModel * pModel, const std::pair<U1, U2> & item, int iRow)
  214. { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
  215. };
  216. template <typename U1, typename U2>
  217. struct insertItem_Helper<QPair<U1, U2>, false>
  218. {
  219. static inline bool insert(qx::IxModel * pModel, QPair<U1, U2> & item, int iRow)
  220. { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
  221. };
  222. template <typename U1, typename U2>
  223. struct insertItem_Helper<const QPair<U1, U2>, false>
  224. {
  225. static inline bool insert(qx::IxModel * pModel, const QPair<U1, U2> & item, int iRow)
  226. { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
  227. };
  228. template <typename U>
  229. struct insertItem_Helper<U, false>
  230. {
  231. enum { is_same_type = std::is_same<qx::model_view::detail::QxNestedModel_Container<T, M>::type_data, U>::value };
  232. static bool insert(qx::IxModel * pModel, U & item, int iRow)
  233. { static_assert(is_same_type, "is_same_type"); return qx::model_view::detail::QxNestedModel_Container<T, M>::insert(pModel, item, iRow); }
  234. };
  235. };
  236. template <class T, class M /* = void */>
  237. struct QxNestedModel_Ptr
  238. {
  239. static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
  240. { return (t ? create_Helper(pParent, idxParent, (* t)) : create_NullHelper(pParent, idxParent)); }
  241. static inline void synchronize(qx::IxModel * pModel, T & t)
  242. { if (! t) { qx::trait::construct_ptr<T>::get(t); }; if (t) { qx::model_view::sync_nested_model(pModel, (* t)); } }
  243. private:
  244. template <class U>
  245. static inline qx::IxModel * create_Helper(qx::IxModel * pParent, const QModelIndex & idxParent, U & u)
  246. { return qx::model_view::detail::QxNestedModel<U, M>::create(pParent, idxParent, u); }
  247. static inline qx::IxModel * create_NullHelper(qx::IxModel * pParent, const QModelIndex & idxParent)
  248. {
  249. M * pModelType = NULL; Q_UNUSED(pModelType);
  250. T t; qx::trait::construct_ptr<T>::get(t); if (! t) { qAssert(false); return NULL; }
  251. qx::IxModel * pModel = qx::model_view::create_nested_model_with_type(pParent, idxParent, (* t), pModelType);
  252. if (pModel) { pModel->clear(); } qAssert(pModel != NULL);
  253. return pModel;
  254. }
  255. };
  256. template <class T, class M /* = void */>
  257. struct QxNestedModel
  258. {
  259. typedef typename std::conditional< std::is_pointer<T>::value, qx::model_view::detail::QxNestedModel_Ptr<T, M>, qx::model_view::detail::QxNestedModel_Generic<T, M> >::type type_model_view_1;
  260. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::model_view::detail::QxNestedModel_Ptr<T, M>, type_model_view_1 >::type type_model_view_2;
  261. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::model_view::detail::QxNestedModel_Container<T, M>, type_model_view_2 >::type type_model_view_3;
  262. static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
  263. { return type_model_view_3::create(pParent, idxParent, t); }
  264. static inline void synchronize(qx::IxModel * pModel, T & t)
  265. { type_model_view_3::synchronize(pModel, t); }
  266. };
  267. } // namespace detail
  268. } // namespace model_view
  269. } // namespace qx
  270. namespace qx {
  271. namespace model_view {
  272. /*!
  273. * \ingroup QxModelView
  274. * \brief qx::model_view::create_nested_model is used by QxEntityEditor to manage complex data structure to work with relationships in QML views and Qt model/view architecture
  275. * \param pParent Parent model, qx::model_view::create_nested_model creates a child model associated to this parent model (a NULL value means that the created model is a root model)
  276. * \param idxParent Index parent model, qx::model_view::create_nested_model creates a child model indexed by this parent index model (an empty index means that the created model is a root model)
  277. * \param t Item which contain all values exposed by the model to the views, t item can be a simple type, a pointer, a smart pointer, or a collection of items
  278. */
  279. template <class T>
  280. qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
  281. { return qx::model_view::detail::QxNestedModel<T, void>::create(pParent, idxParent, t); }
  282. template <class T, class M>
  283. qx::IxModel * create_nested_model_with_type(qx::IxModel * pParent, const QModelIndex & idxParent, T & t, M * pModelType)
  284. { Q_UNUSED(pModelType); static_assert((std::is_base_of<qx::IxModel, M>::value || std::is_same<M, void>::value), "(std::is_base_of<qx::IxModel, M>::value || std::is_same<M, void>::value)"); return qx::model_view::detail::QxNestedModel<T, M>::create(pParent, idxParent, t); }
  285. template <class T>
  286. void sync_nested_model(qx::IxModel * pModel, T & t)
  287. { qx::model_view::detail::QxNestedModel<T, void>::synchronize(pModel, t); }
  288. } // namespace model_view
  289. } // namespace qx
  290. #endif // _QX_NESTED_MODEL_H_