QxOrm  1.3.2
C++ Object Relational Mapping library
IxModel.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _IX_MODEL_H_
00033 #define _IX_MODEL_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #ifdef _QX_NO_PRECOMPILED_HEADER
00047 #ifndef Q_MOC_RUN
00048 #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
00049 #endif // Q_MOC_RUN
00050 #endif // _QX_NO_PRECOMPILED_HEADER
00051 
00052 #include <QtCore/qabstractitemmodel.h>
00053 
00054 #ifndef Q_MOC_RUN
00055 #include <QxRegister/IxClass.h>
00056 #include <QxDataMember/IxDataMemberX.h>
00057 #include <QxCollection/QxCollection.h>
00058 #include <QxDao/IxSqlRelation.h>
00059 #include <QxDao/QxSqlQuery.h>
00060 #include <QxDao/QxDao.h>
00061 #include <QxValidator/QxInvalidValueX.h>
00062 #include <QxValidator/QxValidatorFct.h>
00063 #endif // Q_MOC_RUN
00064 
00065 namespace qx {
00066 
00150 class QX_DLL_EXPORT IxModel : public QAbstractItemModel
00151 {
00152 
00153    Q_OBJECT
00154 
00155 public:
00156 
00157    enum e_auto_update_database { e_no_auto_update, e_auto_update_on_field_change };
00158 
00159    typedef QHash<QString, IxModel *> type_relation_by_name;
00160    typedef QList<type_relation_by_name> type_lst_relation_by_name;
00161 
00162 protected:
00163 
00164    IxClass * m_pClass;                             
00165    IxDataMemberX * m_pDataMemberX;                 
00166    IxDataMember * m_pDataMemberId;                 
00167    IxCollection * m_pCollection;                   
00168    QHash<int, QByteArray> m_lstRoleNames;          
00169    QList<IxDataMember *> m_lstDataMember;          
00170    QHash<QString, int> m_lstDataMemberByKey;       
00171    QHash<QString, QString> m_lstHeaders;           
00172    QStringList m_lstColumns;                       
00173    QSqlDatabase m_database;                        
00174    QSqlError m_lastError;                          
00175    IxModel * m_pParent;                            
00176    type_lst_relation_by_name m_lstChild;           
00177    e_auto_update_database m_eAutoUpdateDatabase;   
00178 
00179 public:
00180 
00181    IxModel(QObject * parent = 0);
00182    virtual ~IxModel();
00183 
00184    IxClass * getClass() const;
00185    IxCollection * getCollection() const;
00186    QSqlDatabase getDatabase() const;
00187    QSqlError getLastError() const;
00188    Q_INVOKABLE QString getLastErrorAsString() const;
00189    Q_INVOKABLE QStringList getListOfColumns() const;
00190    QHash<QString, QString> getListOfHeaders() const;
00191    IxDataMember * getDataMember(int column) const;
00192    Q_INVOKABLE QString getDataMemberKey(int column) const;
00193    Q_INVOKABLE QVariant getModelValue(int row, const QString & column) const;
00194    Q_INVOKABLE int getColumnIndex(const QString & sColumnName) const;
00195    Q_INVOKABLE int getAutoUpdateDatabase_() const;
00196    e_auto_update_database getAutoUpdateDatabase() const;
00197 
00198    void setDatabase(const QSqlDatabase & db);
00199    Q_INVOKABLE void setListOfColumns(const QStringList & lst);
00200    void setListOfHeaders(const QHash<QString, QString> & lst);
00201    Q_INVOKABLE bool setModelValue(int row, const QString & column, const QVariant & value);
00202    void setParentModel(IxModel * pParent);
00203    Q_INVOKABLE void setAutoUpdateDatabase_(int i);
00204    void setAutoUpdateDatabase(e_auto_update_database e);
00205 
00206    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
00207    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
00208    virtual QSqlError qxFetchById(const QVariant & id, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00209    virtual QSqlError qxFetchAll(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00210    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00211    virtual QSqlError qxFetchRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00212    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00213    virtual QSqlError qxInsertRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00214    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00215    virtual QSqlError qxUpdateRow(int row, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00216    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00217    virtual QSqlError qxSaveRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00218    virtual QSqlError qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0;
00219    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0;
00220    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00221    virtual QSqlError qxDeleteRow(int row, QSqlDatabase * pDatabase = NULL) = 0;
00222    virtual QSqlError qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0;
00223    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0;
00224    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00225    virtual QSqlError qxDestroyRow(int row, QSqlDatabase * pDatabase = NULL) = 0;
00226    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00227    virtual qx_bool qxExist(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0;
00228    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0;
00229    virtual qx::QxInvalidValueX qxValidateRow(int row, const QStringList & groups = QStringList()) = 0;
00230 
00231    Q_INVOKABLE int qxCount_(const QString & sQuery);
00232    Q_INVOKABLE bool qxFetchById_(const QVariant & id, const QStringList & relation = QStringList());
00233    Q_INVOKABLE bool qxFetchAll_(const QStringList & relation = QStringList());
00234    Q_INVOKABLE bool qxFetchByQuery_(const QString & sQuery, const QStringList & relation = QStringList());
00235    Q_INVOKABLE bool qxFetchRow_(int row, const QStringList & relation = QStringList());
00236    Q_INVOKABLE bool qxInsert_(const QStringList & relation = QStringList());
00237    Q_INVOKABLE bool qxInsertRow_(int row, const QStringList & relation = QStringList());
00238    Q_INVOKABLE bool qxUpdate_(const QString & sQuery, const QStringList & relation = QStringList());
00239    Q_INVOKABLE bool qxUpdateRow_(int row, const QString & sQuery, const QStringList & relation = QStringList());
00240    Q_INVOKABLE bool qxSave_(const QStringList & relation = QStringList());
00241    Q_INVOKABLE bool qxSaveRow_(int row, const QStringList & relation = QStringList());
00242    Q_INVOKABLE bool qxDeleteById_(const QVariant & id);
00243    Q_INVOKABLE bool qxDeleteAll_();
00244    Q_INVOKABLE bool qxDeleteByQuery_(const QString & sQuery);
00245    Q_INVOKABLE bool qxDeleteRow_(int row);
00246    Q_INVOKABLE bool qxDestroyById_(const QVariant & id);
00247    Q_INVOKABLE bool qxDestroyAll_();
00248    Q_INVOKABLE bool qxDestroyByQuery_(const QString & sQuery);
00249    Q_INVOKABLE bool qxDestroyRow_(int row);
00250    Q_INVOKABLE bool qxExecuteQuery_(const QString & sQuery);
00251    Q_INVOKABLE bool qxExist_(const QVariant & id);
00252    Q_INVOKABLE QString qxValidate_(const QStringList & groups = QStringList());
00253    Q_INVOKABLE QString qxValidateRow_(int row, const QStringList & groups = QStringList());
00254 
00255 protected:
00256 
00257    void raiseEvent_headerDataChanged(Qt::Orientation orientation, int first, int last);
00258 #if (QT_VERSION >= 0x050000)
00259    void raiseEvent_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles = QVector<int>());
00260    void raiseEvent_layoutAboutToBeChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
00261    void raiseEvent_layoutChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
00262 #else // (QT_VERSION >= 0x050000)
00263    void raiseEvent_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
00264    void raiseEvent_layoutAboutToBeChanged();
00265    void raiseEvent_layoutChanged();
00266 #endif // (QT_VERSION >= 0x050000)
00267 
00268 public:
00269 
00270    Q_INVOKABLE void clear(bool bUpdateColumns = false);
00271 
00272    virtual int rowCount(const QModelIndex & parent = QModelIndex()) const;
00273    virtual int columnCount(const QModelIndex & parent = QModelIndex()) const;
00274    virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
00275    virtual QModelIndex parent(const QModelIndex & index) const;
00276    virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const;
00277    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
00278    virtual Qt::ItemFlags flags(const QModelIndex & index) const;
00279    virtual Qt::DropActions supportedDropActions() const;
00280    virtual bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
00281    virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole);
00282 
00283 #if (QT_VERSION >= 0x050000)
00284    virtual QHash<int, QByteArray> roleNames() const;
00285    virtual Qt::DropActions supportedDragActions() const;
00286 #endif // (QT_VERSION >= 0x050000)
00287 
00288 protected:
00289 
00290    virtual void syncNestedModel(int row, const QStringList & relation);
00291    virtual void syncAllNestedModel(const QStringList & relation);
00292    void syncNestedModelRecursive(IxModel * pNestedModel, const QStringList & relation);
00293 
00294    void generateRoleNames();
00295    QSqlDatabase * database(QSqlDatabase * other);
00296    IxModel * getChild(long row, const QString & relation);
00297    void insertChild(long row, const QString & relation, IxModel * pChild);
00298    void removeListOfChild(long row);
00299 
00300 };
00301 
00302 } // namespace qx
00303 
00304 #endif // _IX_MODEL_H_