dotherside/lib/include/DOtherSide/BaseQAbstractListModel.h

204 lines
6.4 KiB
C
Raw Normal View History

#pragma once
// Qt
#include <QAbstractListModel>
// DOtherSide
#include "DOtherSide/DOtherSideTypes.h"
#include "DOtherSide/DynamicQObject.h"
/// This class act as a base class for D and Nim QAbstractListModel
class BaseQAbstractListModel final : public DynamicQObject<QAbstractListModel>
{
2015-01-31 17:01:03 +01:00
public:
/// Constructor
BaseQAbstractListModel(void* modelObject,
RowCountCallback rowCountCallback,
ColumnCountCallback columnCountCallback,
2015-01-31 17:01:03 +01:00
DataCallback dataCallback,
SetDataCallback setDataCallback,
RoleNamesCallback roleNamesCallback,
FlagsCallback flagsCallback,
HeaderDataCallback headerDataCallback);
2015-01-31 17:01:03 +01:00
/// Return the model's row count
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
/// Return the model's column count
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
2015-01-31 17:01:03 +01:00
/// Return the QVariant at the given index
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
/// Sets the QVariant value at the given index and role
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
/// Return the item flags for the given index
Qt::ItemFlags flags(const QModelIndex& index) const override;
/// Return the data for the given role and section in the header with the specified orientation
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
2015-01-31 17:01:03 +01:00
/// Return the dModelPointer
void* modelObject();
/// Return the roleNames
QHash<int, QByteArray> roleNames() const override;
2015-01-31 17:01:03 +01:00
/// Expose beginInsertRows
void publicBeginInsertRows(const QModelIndex& index, int first, int last);
2015-02-04 23:02:26 +01:00
/// Expose endInsertRows
void publicEndInsertRows();
2015-02-04 23:02:26 +01:00
/// Expose beginRemoveRows
void publicBeginRemoveRows(const QModelIndex& index, int first, int last);
2015-02-04 23:02:26 +01:00
/// Expose endInsertRows
void publicEndRemoveRows();
2015-02-04 23:02:26 +01:00
/// Expose beginResetModel
void publicBeginResetModel();
2015-02-04 23:02:26 +01:00
/// Expose endResetModel
void publicEndResetModel();
2015-02-04 23:02:26 +01:00
/// Expose dataChanged
2015-02-04 23:02:26 +01:00
void publicDataChanged(const QModelIndex& topLeft,
const QModelIndex& bottomRight,
const QVector<int>& roles = QVector<int>());
2015-02-04 23:02:26 +01:00
2015-01-31 17:01:03 +01:00
private:
void* m_modelObject;
RowCountCallback m_rowCountCallback;
ColumnCountCallback m_columnCountCallback;
2015-01-31 17:01:03 +01:00
DataCallback m_dataCallback;
SetDataCallback m_setDataCallback;
2015-01-31 17:01:03 +01:00
RoleNamesCallback m_roleNamesCallback;
FlagsCallback m_flagsCallback;
HeaderDataCallback m_headerDataCallback;
};
2015-02-04 23:02:26 +01:00
BaseQAbstractListModel::BaseQAbstractListModel(void* modelObject,
RowCountCallback rowCountCallback,
ColumnCountCallback columnCountCallback,
DataCallback dataCallback,
SetDataCallback setDataCallback,
RoleNamesCallback roleNamesCallback,
FlagsCallback flagsCallback,
HeaderDataCallback headerDataCallback)
2015-02-04 23:02:26 +01:00
: m_modelObject(modelObject)
, m_rowCountCallback(rowCountCallback)
, m_columnCountCallback(columnCountCallback)
2015-02-04 23:02:26 +01:00
, m_dataCallback(dataCallback)
, m_setDataCallback(setDataCallback)
2015-02-04 23:02:26 +01:00
, m_roleNamesCallback(roleNamesCallback)
, m_flagsCallback(flagsCallback)
, m_headerDataCallback(headerDataCallback)
2015-02-04 23:02:26 +01:00
{
}
int BaseQAbstractListModel::rowCount(const QModelIndex& parent) const
2015-02-04 23:02:26 +01:00
{
auto parentIndex = new QModelIndex();
*parentIndex = parent;
int result;
m_rowCountCallback(m_modelObject, parentIndex, &result);
return result;
}
int BaseQAbstractListModel::columnCount(const QModelIndex& parent) const
{
auto parentIndex = new QModelIndex();
*parentIndex = parent;
2015-02-04 23:02:26 +01:00
int result;
m_columnCountCallback(m_modelObject, parentIndex, &result);
2015-02-04 23:02:26 +01:00
return result;
}
QVariant BaseQAbstractListModel::data(const QModelIndex& index, int role) const
{
auto newIndex = new QModelIndex();
*newIndex = index;
QVariant result;
m_dataCallback(m_modelObject, newIndex, role, &result);
return result;
}
bool BaseQAbstractListModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
auto newIndex = new QModelIndex(index);
*newIndex = index;
auto newValue = new QVariant();
*newValue = value;
bool result = false;
m_setDataCallback(m_modelObject, newIndex, newValue, role, &result);
return result;
}
Qt::ItemFlags BaseQAbstractListModel::flags(const QModelIndex& index) const
{
auto newIndex = new QModelIndex(index);
*newIndex = index;
int result;
m_flagsCallback(m_modelObject, newIndex, &result);
return Qt::ItemFlags(result);
}
QVariant BaseQAbstractListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant result;
m_headerDataCallback(m_modelObject, section, orientation, role, &result);
return result;
}
2015-02-04 23:02:26 +01:00
void* BaseQAbstractListModel::modelObject()
{
return m_modelObject;
}
QHash<int, QByteArray> BaseQAbstractListModel::roleNames() const
{
QHash<int, QByteArray> result;
m_roleNamesCallback(m_modelObject, &result);
return result;
}
void BaseQAbstractListModel::publicBeginInsertRows(const QModelIndex& index, int first, int last)
{
beginInsertRows(index, first, last);
}
void BaseQAbstractListModel::publicEndInsertRows()
{
return endInsertRows();
}
void BaseQAbstractListModel::publicBeginRemoveRows(const QModelIndex& index, int first, int last)
{
beginRemoveRows(index, first, last);
}
void BaseQAbstractListModel::publicEndRemoveRows()
{
return endRemoveRows();
}
void BaseQAbstractListModel::publicBeginResetModel()
{
beginResetModel();
}
void BaseQAbstractListModel::publicEndResetModel()
{
endResetModel();
}
void BaseQAbstractListModel::publicDataChanged(const QModelIndex& topLeft,
const QModelIndex& bottomRight,
const QVector<int>& roles)
2015-02-04 23:02:26 +01:00
{
emit dataChanged(topLeft, bottomRight, roles);
}