Added support for automatic sorting.
This commit is contained in:
+71
-25
@@ -6,6 +6,8 @@
|
||||
#include <QMetaProperty>
|
||||
#include <QModelIndex>
|
||||
|
||||
#include "core_global.h"
|
||||
|
||||
template<class T>
|
||||
class AutoTableModel : public QAbstractTableModel
|
||||
{
|
||||
@@ -18,15 +20,46 @@ public:
|
||||
|
||||
virtual ~AutoTableModel() {}
|
||||
|
||||
private:
|
||||
class Comparator
|
||||
{
|
||||
public:
|
||||
Comparator(const QString &property, Qt::SortOrder order)
|
||||
{
|
||||
m_property = property;
|
||||
m_order = order;
|
||||
}
|
||||
|
||||
bool operator()(QSharedPointer<T> entA, QSharedPointer<T> entB) {
|
||||
QObject *rawEntityA = (QObject*)entA.data();
|
||||
QObject *rawEntityB = (QObject*)entB.data();
|
||||
|
||||
if (m_order == Qt::AscendingOrder) {
|
||||
return rawEntityA->property(m_property.toStdString().c_str()) < rawEntityB->property(m_property.toStdString().c_str());
|
||||
} else {
|
||||
return rawEntityB->property(m_property.toStdString().c_str()) < rawEntityA->property(m_property.toStdString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
QString m_property;
|
||||
Qt::SortOrder m_order;
|
||||
};
|
||||
|
||||
|
||||
// QAbstractItemModel interface
|
||||
public:
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
|
||||
return m_list.size();
|
||||
}
|
||||
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
|
||||
if (m_list.isEmpty())
|
||||
{
|
||||
return 0;
|
||||
@@ -56,8 +89,43 @@ public:
|
||||
return m_list;
|
||||
}
|
||||
|
||||
// QAbstractItemModel interface
|
||||
public:
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role != Qt::DisplayRole) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if (orientation == Qt::Horizontal) {
|
||||
QObject *entity = (QObject*)new T();
|
||||
|
||||
for (int i = 0; i < entity->metaObject()->propertyCount(); i++) {
|
||||
if (i == section) {
|
||||
QString colName(entity->metaObject()->property(i + 1).name());
|
||||
return tr(colName.toStdString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
delete entity;
|
||||
}
|
||||
|
||||
return QVariant(section + 1);
|
||||
}
|
||||
|
||||
virtual void sort(int column, Qt::SortOrder order) {
|
||||
if (m_list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginResetModel();
|
||||
QObject *rawEntity = (QObject*)m_list.at(0).data();
|
||||
Comparator c(rawEntity->metaObject()->property(column + 1).name(), order);
|
||||
|
||||
std::sort(m_list.begin(), m_list.end(), c);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
/*///////////////////////*/
|
||||
|
||||
QSharedPointer<T> itemFromIndex(const QModelIndex &index) const
|
||||
{
|
||||
return m_list.at(index.row());
|
||||
@@ -93,32 +161,10 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
friend class Comparator;
|
||||
|
||||
QList<QSharedPointer<T> > m_list;
|
||||
|
||||
// QAbstractItemModel interface
|
||||
public:
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role != Qt::DisplayRole) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if (orientation == Qt::Horizontal) {
|
||||
QObject *entity = (QObject*)new T();
|
||||
|
||||
for (int i = 0; i < entity->metaObject()->propertyCount(); i++) {
|
||||
if (i == section) {
|
||||
QString colName(entity->metaObject()->property(i + 1).name());
|
||||
return tr(colName.toStdString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
delete entity;
|
||||
}
|
||||
|
||||
return QVariant(section + 1);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ODBTABLEMODEL_H
|
||||
|
||||
Reference in New Issue
Block a user