Added template class AutoTableModel for automatic table models based on

given list of QObject derived objects.
print
Josef Rokos 9 years ago
parent 7755432484
commit b7cfb8c39c

@ -0,0 +1,101 @@
#ifndef ODBTABLEMODEL_H
#define ODBTABLEMODEL_H
#include <QAbstractTableModel>
#include <QSharedPointer>
#include <QMetaProperty>
#include <QModelIndex>
template<class T>
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<T> 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<T> 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<QSharedPointer<T> > list()
{
return m_list;
}
// QAbstractItemModel interface
public:
QSharedPointer<T> itemFromIndex(const QModelIndex &index) const
{
return m_list.at(index.row());
}
void setItemToIndex(const QModelIndex &index, QSharedPointer<T> data)
{
m_list.removeAt(index.row());
m_list.insert(index.row(), data);
emit dataChanged(index, index);
}
void addRow(QSharedPointer<T> 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<QSharedPointer<T> > list)
{
m_list = list;
}
private:
QList<QSharedPointer<T> > m_list;
};
#endif // ODBTABLEMODEL_H

@ -22,7 +22,8 @@ HEADERS += core.h\
service.h \ service.h \
data/user.h \ data/user.h \
context.h \ context.h \
imetadataplugin.h imetadataplugin.h \
autotablemodel.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib

@ -10,6 +10,7 @@
#include <odb/result.hxx> #include <odb/result.hxx>
#include "core_global.h" #include "core_global.h"
#include "context.h"
template<class T> template<class T>
class Service class Service
@ -19,13 +20,15 @@ public:
QList<QSharedPointer<T> > all() { QList<QSharedPointer<T> > all() {
odb::database *db = Context::instance().db(); odb::database *db = Context::instance().db();
Q_ASSERT(db);
odb::transaction tx(db->begin()); odb::transaction tx(db->begin());
odb::result<T> res = db->template query<T>(); odb::result<T> res = db->template query<T>();
QList<QSharedPointer<T> > ret; QList<QSharedPointer<T> > ret;
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) { for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
QSharedPointer<T> entity = db->template load<T>(((T)*it).getId()); ret.append(it.load());
ret.append(entity);
} }
tx.commit(); tx.commit();

@ -42,6 +42,7 @@ DEFINES += DATABASE_SQLITE
# #
ODB_FLAGS += -I $$[QT_INSTALL_HEADERS] ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]
ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore
ODB_FLAGS += -D __PIC__
win32 { win32 {
ODB_FLAGS += -I d:/prac/odb/libodb-2.4.0 ODB_FLAGS += -I d:/prac/odb/libodb-2.4.0

Loading…
Cancel
Save