diff --git a/core/autotablemodel.h b/core/autotablemodel.h new file mode 100644 index 0000000..d399f7c --- /dev/null +++ b/core/autotablemodel.h @@ -0,0 +1,101 @@ +#ifndef ODBTABLEMODEL_H +#define ODBTABLEMODEL_H + +#include +#include +#include +#include + +template +class AutoTableModel : public QAbstractTableModel +{ + +public: + explicit AutoTableModel(QObject *parent = NULL) + :QAbstractTableModel(parent) + { + } + + virtual ~AutoTableModel() {} + + // QAbstractItemModel interface +public: + int rowCount(const QModelIndex &parent = QModelIndex()) const + { + return m_list.size(); + } + + int columnCount(const QModelIndex &parent = QModelIndex()) const + { + if (m_list.isEmpty()) + { + return 0; + } + + QSharedPointer entity = m_list.at(0); + QObject *rawEntity = (QObject*)entity.data(); + + return rawEntity->metaObject()->propertyCount() - 1; + } + + QVariant data(const QModelIndex &index, int role) const + { + if (role == Qt::DisplayRole) + { + QSharedPointer entity = m_list.at(index.row()); + QObject *rawEntity = (QObject*)entity.data(); + + return rawEntity->property(rawEntity->metaObject()->property(index.column() + 1).name()); + } + + return QVariant::Invalid; + } + + QList > list() + { + return m_list; + } + + // QAbstractItemModel interface +public: + QSharedPointer itemFromIndex(const QModelIndex &index) const + { + return m_list.at(index.row()); + } + + void setItemToIndex(const QModelIndex &index, QSharedPointer data) + { + m_list.removeAt(index.row()); + m_list.insert(index.row(), data); + + emit dataChanged(index, index); + } + + void addRow(QSharedPointer data) + { + beginInsertRows(QModelIndex(), rowCount() - 1, rowCount() - 1); + insertRow(rowCount()); + m_list.append(data); + endInsertRows(); + } + + void removeRowAt(const QModelIndex &index) + { + beginRemoveRows(QModelIndex(), index.row(), index.row()); + removeRow(index.row()); + m_list.removeAt(index.row()); + endRemoveRows(); + } + + void setData(QList > list) + { + m_list = list; + } + +private: + + QList > m_list; +}; + +#endif // ODBTABLEMODEL_H + diff --git a/core/core.pro b/core/core.pro index 890d0c4..514611c 100644 --- a/core/core.pro +++ b/core/core.pro @@ -22,7 +22,8 @@ HEADERS += core.h\ service.h \ data/user.h \ context.h \ - imetadataplugin.h + imetadataplugin.h \ + autotablemodel.h unix { target.path = /usr/lib diff --git a/core/service.h b/core/service.h index 1cbcf29..6e22098 100644 --- a/core/service.h +++ b/core/service.h @@ -10,6 +10,7 @@ #include #include "core_global.h" +#include "context.h" template class Service @@ -19,13 +20,15 @@ public: QList > all() { odb::database *db = Context::instance().db(); + + Q_ASSERT(db); + odb::transaction tx(db->begin()); odb::result res = db->template query(); QList > ret; for (typename odb::result::iterator it = res.begin(); it != res.end(); it++) { - QSharedPointer entity = db->template load(((T)*it).getId()); - ret.append(entity); + ret.append(it.load()); } tx.commit(); diff --git a/odb.pri b/odb.pri index 8f6d8fd..afcea2e 100755 --- a/odb.pri +++ b/odb.pri @@ -42,6 +42,7 @@ DEFINES += DATABASE_SQLITE # ODB_FLAGS += -I $$[QT_INSTALL_HEADERS] ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore +ODB_FLAGS += -D __PIC__ win32 { ODB_FLAGS += -I d:/prac/odb/libodb-2.4.0