QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxNestedModel.h
Go to the documentation of this file.
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
32#ifndef _QX_NESTED_MODEL_H_
33#define _QX_NESTED_MODEL_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <QxConvert/QxConvert.h>
47
49
58
59#include <QxSerialize/QxClone.h>
60
61#include <QxModelView/IxModel.h>
62#include <QxModelView/QxModel.h>
63
64namespace qx {
65namespace model_view {
66
67template <class T>
68qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t);
69
70template <class T, class M>
71qx::IxModel * create_nested_model_with_type(qx::IxModel * pParent, const QModelIndex & idxParent, T & t, M * pModelType);
72
73template <class T>
74void sync_nested_model(qx::IxModel * pModel, T & t);
75
76} // namespace model_view
77} // namespace qx
78
79namespace qx {
80namespace model_view {
81namespace detail {
82
83template <class T, class M /* = void */>
84struct QxNestedModel;
85
86template <class T, bool bIsQObject /* = false */>
88{
89
90 static std::shared_ptr<T> clone(T & t)
91 { std::shared_ptr<T> p = std::make_shared<T>(); (* p) = t; return p; }
92
93 static void synchronize(T & t1, T & t2)
94 { t1 = t2; }
95
96};
97
98template <class T>
99struct QxNestedModel_Helper<T, true>
100{
101
102 static std::shared_ptr<T> clone(T & t)
103 { std::shared_ptr<T> p; p.reset(qx::clone_to_nude_ptr(t)); qAssert(p); return p; }
104
105 static void synchronize(T & t1, T & t2)
106 {
108 qx::IxDataMemberX * pDataMemberX = (pClass ? pClass->getDataMemberX() : NULL);
109 for (long l = 0; (pDataMemberX && (l < pDataMemberX->count_WithDaoStrategy())); l++)
110 {
111 qx::IxDataMember * pDataMember = pDataMemberX->get_WithDaoStrategy(l); if (! pDataMember) { continue; }
112 QVariant value = pDataMember->toVariant(& t2);
113 pDataMember->fromVariant((& t1), value);
114 }
115 }
116
117};
118
119template <class T, class M, bool bIsTypeVoid /* = false */>
121{ static qx::IxModel * newModel(qx::IxModel * pParent) { return new M(pParent); } };
122
123template <class T, class M>
124struct QxNestedModel_Creator<T, M, true>
125{ static qx::IxModel * newModel(qx::IxModel * pParent) { return new qx::QxModel<T>(pParent); } };
126
127template <class T, class M /* = void */>
129{
130
134
136
137 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
138 {
139 static_assert(is_valid, "is_valid");
141 pModel->setParentModel(pParent);
142 type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
143 long & idx = pModel->m_lManualInsertIndex;
145
146 pModel->beginInsertRows(idxParent, 0, 0);
148 qx::IxDataMember * pDataMemberId = pModel->m_pDataMemberId;
149 if (! pDataMemberId) { qAssert(false); pModel->endInsertRows(); return pModel; }
150 QVariant value = pDataMemberId->toVariant(& t);
152 { idx--; value = QVariant(static_cast<qlonglong>(idx)); }
153 qx::cvt::from_variant(value, key);
154 pListOfItems->insert(0, key, ptr);
155 pModel->endInsertRows();
156 return pModel;
157 }
158
159 static inline void synchronize(qx::IxModel * pModel, T & t)
160 {
161 if (! pModel) { qAssert(false); return; }
162 type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
163 if (! pListOfItems || (pListOfItems->count() <= 0)) { return; }
164 type_ptr ptr = pListOfItems->getByIndex(0); if (! ptr) { return; }
166 }
167
168};
169
170template <class T, class M /* = void */>
172{
173
180
182
183 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
184 {
185 int iCurrRow = 0;
186 static_assert(is_valid, "is_valid");
188 pModel->setParentModel(pParent);
189 long lCount = static_cast<long>(type_generic_container::size(t));
190 if (lCount <= 0) { return pModel; }
191
192 pModel->beginInsertRows(idxParent, 0, (lCount - 1));
193 for (typename T::iterator it = t.begin(); it != t.end(); ++it)
194 { insertItem(pModel, (* it), iCurrRow); iCurrRow++; }
195 pModel->endInsertRows();
196 return pModel;
197 }
198
199 template <typename U>
200 static inline bool insert(qx::IxModel * pModel, U & item, int iRow)
201 {
202 if (! pModel) { qAssert(false); return false; }
203 type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
204 long & idx = pModel->m_lManualInsertIndex;
206
208 qx::IxDataMember * pDataMemberId = pModel->m_pDataMemberId;
209 if (! pDataMemberId) { qAssert(false); return false; }
210 QVariant value = pDataMemberId->toVariant(& item);
212 { idx--; value = QVariant(static_cast<qlonglong>(idx)); }
213 qx::cvt::from_variant(value, key);
214 pListOfItems->insert(iRow, key, ptr);
215 return true;
216 }
217
218 static inline void synchronize(qx::IxModel * pModel, T & t)
219 {
220 if (! pModel) { qAssert(false); return; }
221 type_generic_container::clear(t);
222 type_collection * pListOfItems = static_cast<type_collection *>(pModel->getCollection());
223
224 for (long l = 0; l < pListOfItems->count(); l++)
225 {
226 type_ptr ptr = pListOfItems->getByIndex(l); if (! ptr) { continue; }
227 type_item item = type_generic_container::createItem();
228 type_data & item_val = item.value_qx();
230 QVariant vKey = QVariant(static_cast<qlonglong>(l));
231 qx::cvt::from_variant(vKey, item.key());
232 type_generic_container::insertItem(t, item);
233 }
234 }
235
236private:
237
238 template <typename U>
239 static inline bool insertItem(qx::IxModel * pModel, U & item, int iRow)
241
242 template <typename U, bool bIsPointer /* = true */>
244 {
245 static inline bool insert(qx::IxModel * pModel, U & item, int iRow)
246 { return (item ? qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, (* item), iRow) : true); }
247 };
248
249 template <typename U1, typename U2>
250 struct insertItem_Helper<std::pair<U1, U2>, false>
251 {
252 static inline bool insert(qx::IxModel * pModel, std::pair<U1, U2> & item, int iRow)
253 { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
254 };
255
256 template <typename U1, typename U2>
257 struct insertItem_Helper<const std::pair<U1, U2>, false>
258 {
259 static inline bool insert(qx::IxModel * pModel, const std::pair<U1, U2> & item, int iRow)
260 { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
261 };
262
263#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
264 template <typename U1, typename U2>
265 struct insertItem_Helper<QPair<U1, U2>, false>
266 {
267 static inline bool insert(qx::IxModel * pModel, QPair<U1, U2> & item, int iRow)
268 { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
269 };
270
271 template <typename U1, typename U2>
272 struct insertItem_Helper<const QPair<U1, U2>, false>
273 {
274 static inline bool insert(qx::IxModel * pModel, const QPair<U1, U2> & item, int iRow)
275 { return qx::model_view::detail::QxNestedModel_Container<T, M>::insertItem(pModel, item.second, iRow); }
276 };
277#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
278
279 template <typename U>
280 struct insertItem_Helper<U, false>
281 {
282 enum { is_same_type = std::is_same<qx::model_view::detail::QxNestedModel_Container<T, M>::type_data, U>::value };
283 static bool insert(qx::IxModel * pModel, U & item, int iRow)
284 { static_assert(is_same_type, "is_same_type"); return qx::model_view::detail::QxNestedModel_Container<T, M>::insert(pModel, item, iRow); }
285 };
286
287};
288
289template <class T, class M /* = void */>
291{
292
293 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
294 { return (t ? create_Helper(pParent, idxParent, (* t)) : create_NullHelper(pParent, idxParent)); }
295
296 static inline void synchronize(qx::IxModel * pModel, T & t)
297 { if (! t) { qx::trait::construct_ptr<T>::get(t); }; if (t) { qx::model_view::sync_nested_model(pModel, (* t)); } }
298
299private:
300
301 template <class U>
302 static inline qx::IxModel * create_Helper(qx::IxModel * pParent, const QModelIndex & idxParent, U & u)
303 { return qx::model_view::detail::QxNestedModel<U, M>::create(pParent, idxParent, u); }
304
305 static inline qx::IxModel * create_NullHelper(qx::IxModel * pParent, const QModelIndex & idxParent)
306 {
307 M * pModelType = NULL; Q_UNUSED(pModelType);
308 T t; qx::trait::construct_ptr<T>::get(t); if (! t) { qAssert(false); return NULL; }
309 qx::IxModel * pModel = qx::model_view::create_nested_model_with_type(pParent, idxParent, (* t), pModelType);
310 if (pModel) { pModel->clear(); } qAssert(pModel != NULL);
311 return pModel;
312 }
313
314};
315
316template <class T, class M /* = void */>
318{
319
321 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;
322 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;
323
324 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
325 { return type_model_view_3::create(pParent, idxParent, t); }
326
327 static inline void synchronize(qx::IxModel * pModel, T & t)
328 { type_model_view_3::synchronize(pModel, t); }
329
330};
331
332} // namespace detail
333} // namespace model_view
334} // namespace qx
335
336namespace qx {
337namespace model_view {
338
346template <class T>
347qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t)
348{ return qx::model_view::detail::QxNestedModel<T, void>::create(pParent, idxParent, t); }
349
350template <class T, class M>
351qx::IxModel * create_nested_model_with_type(qx::IxModel * pParent, const QModelIndex & idxParent, T & t, M * pModelType)
352{ 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); }
353
354template <class T>
357
358} // namespace model_view
359} // namespace qx
360
361#endif // _QX_NESTED_MODEL_H_
Interface to manage Qt model/view architecture with classes registered into QxOrm context (Qt widgets...
Clone all classes registered into QxOrm context using QxOrm library serialization engine.
QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
qx::cvt : namespace to provide global functions to convert any kind of objects to/from QString and QV...
#define qAssert(x)
Definition QxMacro.h:52
All classes registered into QxOrm context can be used with Qt model/view architecture (Qt widgets and...
qx::IxClass : common interface for all classes registered into QxOrm context
Definition IxClass.h:69
IxDataMemberX * getDataMemberX() const
qx::IxDataMember : common interface for all class properties registered into QxOrm context
virtual qx_bool fromVariant(void *pOwner, const QVariant &v, const QString &sFormat, int iIndexName=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)=0
virtual QVariant toVariant(const void *pOwner, const QString &sFormat, int iIndexName=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context) const =0
qx::IxDataMemberX : common interface for a list of IxDataMember class properties registered into QxOr...
virtual long count_WithDaoStrategy() const =0
virtual IxDataMember * get_WithDaoStrategy(long lIndex) const =0
qx::IxModel : interface to manage Qt model/view architecture with classes registered into QxOrm conte...
Definition IxModel.h:163
Q_INVOKABLE void clear(bool bUpdateColumns=false)
IxCollection * getCollection() const
long m_lManualInsertIndex
Index to insert manually items to the collection.
Definition IxModel.h:198
void setParentModel(IxModel *pParent)
IxDataMember * m_pDataMemberId
Primary key (property id) defined into QxOrm context.
Definition IxModel.h:184
bool insert(const Key &key, const Value &value)
Add element 'value' at the end of the list indexed by 'key'.
long count() const
Return the number of items in the list (same as 'size()')
const_reference_value getByIndex(long index) const
Return the item at index position 'index'.
qx::QxModel<T, B> : all classes registered into QxOrm context can be used with Qt model/view architec...
Definition QxModel.h:164
qx::trait::get_primary_key< T >::type type_primary_key
Definition QxModel.h:173
std::shared_ptr< T > type_ptr
Definition QxModel.h:172
qx::trait::construct_ptr<T>::get(T & t, bool bReset = false) : instantiate (or reset) a new pointer,...
qx::trait::generic_container<T> : provide some tools to manage all containers without knowing its typ...
qx::trait::get_base_class<T>::type : retrieve base class of type T registered into QxOrm context and ...
Register all primitive and useful types of stl, boost and Qt libraries using QX_REGISTER_CLASS_NAME(T...
qx::IxModel * create_nested_model(qx::IxModel *pParent, const QModelIndex &idxParent, T &t)
qx::model_view::create_nested_model is used by QxEntityEditor to manage complex data structure to wor...
T * clone_to_nude_ptr(const T &obj)
qx::clone_to_nude_ptr(const T & obj) : return a nude pointer (be careful with memory leak) of a new i...
Definition QxClone.h:78
std::shared_ptr< T > clone(const T &obj)
qx::clone(const T & obj) : return a boost smart-pointer (std::shared_ptr<T>) of a new instance of typ...
Definition QxClone.h:128
qx::trait::is_container<T>::value : return true if T is a container from stl, boost,...
qx::trait::is_qx_registered<T>::value : return true if T is registered into QxOrm context to provide ...
qx::trait::is_smart_ptr<T>::value : return true if T is a smart-pointer of boost, Qt or QxOrm librari...
qx::trait::is_valid_primary_key<T>(const T & t) : return true if t can be a valid primary key to be i...
qx_bool from_variant(const QVariant &v, T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:80
void sync_nested_model(qx::IxModel *pModel, T &t)
qx::IxModel * create_nested_model_with_type(qx::IxModel *pParent, const QModelIndex &idxParent, T &t, M *pModelType)
bool is_valid_primary_key(const T &t)
Root namespace for all QxOrm library features.
static bool insert(qx::IxModel *pModel, const std::pair< U1, U2 > &item, int iRow)
static bool insert(qx::IxModel *pModel, std::pair< U1, U2 > &item, int iRow)
static bool insert(qx::IxModel *pModel, U &item, int iRow)
qx::QxModel< type_data >::type_ptr type_ptr
static qx::IxModel * create(qx::IxModel *pParent, const QModelIndex &idxParent, T &t)
static bool insertItem(qx::IxModel *pModel, U &item, int iRow)
qx::trait::generic_container< T > type_generic_container
qx::QxModel< type_data >::type_primary_key type_primary_key
type_generic_container::type_value_qx type_data
type_generic_container::type_item type_item
static void synchronize(qx::IxModel *pModel, T &t)
static bool insert(qx::IxModel *pModel, U &item, int iRow)
qx::QxModel< type_data >::type_collection type_collection
static qx::IxModel * newModel(qx::IxModel *pParent)
static qx::IxModel * newModel(qx::IxModel *pParent)
qx::QxModel< T >::type_collection type_collection
static qx::IxModel * create(qx::IxModel *pParent, const QModelIndex &idxParent, T &t)
qx::QxModel< T >::type_primary_key type_primary_key
static void synchronize(qx::IxModel *pModel, T &t)
static std::shared_ptr< T > clone(T &t)
static qx::IxModel * create_Helper(qx::IxModel *pParent, const QModelIndex &idxParent, U &u)
static qx::IxModel * create_NullHelper(qx::IxModel *pParent, const QModelIndex &idxParent)
static qx::IxModel * create(qx::IxModel *pParent, const QModelIndex &idxParent, T &t)
static void synchronize(qx::IxModel *pModel, T &t)
static void synchronize(qx::IxModel *pModel, T &t)
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
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
static qx::IxModel * create(qx::IxModel *pParent, const QModelIndex &idxParent, T &t)
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
static void get(T &t, bool bReset=false)
qx::trait::generic_container<T> : provide some tools to manage all containers without knowing its typ...
qx::trait::is_qx_registered<T>::value : return true if T is registered into QxOrm context to provide ...
qx::trait::is_smart_ptr<T>::value : return true if T is a smart-pointer of boost, Qt or QxOrm librari...