rust-qt-binding-generator/tests/test_list_rust.cpp

217 lines
5.6 KiB
C++
Raw Normal View History

2017-08-20 07:11:58 -07:00
/* generated by rust_qt_binding_generator */
#include "test_list_rust.h"
namespace {
template <typename T>
struct option {
public:
2017-08-20 07:11:58 -07:00
T value;
bool some;
operator QVariant() const {
if (some) {
return QVariant(value);
}
return QVariant();
}
};
struct qbytearray_t {
private:
const char* data;
int len;
public:
qbytearray_t(const QByteArray& v):
data(v.data()),
len(v.size()) {
}
operator QByteArray() const {
return QByteArray(data, len);
}
};
struct qstring_t {
private:
const void* data;
int len;
public:
qstring_t(const QString& v):
data(static_cast<const void*>(v.utf16())),
len(v.size()) {
}
operator QString() const {
return QString::fromUtf8(static_cast<const char*>(data), len);
}
};
struct qmodelindex_t {
int row;
quintptr id;
};
2017-08-26 10:10:18 -07:00
2017-08-20 07:11:58 -07:00
}
typedef void (*qstring_set)(QString*, qstring_t*);
void set_qstring(QString* v, qstring_t* val) {
*v = *val;
}
typedef void (*qbytearray_set)(QByteArray*, qbytearray_t*);
void set_qbytearray(QByteArray* v, qbytearray_t* val) {
*v = *val;
}
extern "C" {
void persons_data_user_name(const Persons::Private*, int, QString*, qstring_set);
bool persons_set_data_user_name(Persons::Private*, int, qstring_t);
2017-08-22 04:45:34 -07:00
void persons_sort(Persons::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);
2017-08-20 07:11:58 -07:00
int persons_row_count(const Persons::Private*);
bool persons_can_fetch_more(const Persons::Private*);
void persons_fetch_more(Persons::Private*);
}
int Persons::columnCount(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : 1;
}
bool Persons::hasChildren(const QModelIndex &parent) const
{
return rowCount(parent) > 0;
}
int Persons::rowCount(const QModelIndex &parent) const
{
2017-08-26 10:10:18 -07:00
return (parent.isValid()) ? 0 : persons_row_count(m_d);
2017-08-20 07:11:58 -07:00
}
QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const
{
2017-08-21 15:51:48 -07:00
if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) {
return createIndex(row, column, (quintptr)row);
2017-08-20 07:11:58 -07:00
}
return QModelIndex();
}
QModelIndex Persons::parent(const QModelIndex &) const
{
return QModelIndex();
}
bool Persons::canFetchMore(const QModelIndex &parent) const
{
2017-08-26 10:10:18 -07:00
return (parent.isValid()) ? 0 : persons_can_fetch_more(m_d);
2017-08-20 07:11:58 -07:00
}
void Persons::fetchMore(const QModelIndex &parent)
{
if (!parent.isValid()) {
2017-08-26 10:10:18 -07:00
persons_fetch_more(m_d);
2017-08-20 07:11:58 -07:00
}
}
void Persons::sort(int column, Qt::SortOrder order)
{
2017-08-26 10:10:18 -07:00
persons_sort(m_d, column, order);
2017-08-20 07:11:58 -07:00
}
Qt::ItemFlags Persons::flags(const QModelIndex &i) const
{
auto flags = QAbstractItemModel::flags(i);
if (i.column() == 0) {
flags |= Qt::ItemIsEditable;
}
return flags;
}
QVariant Persons::data(const QModelIndex &index, int role) const
{
QVariant v;
Q_ASSERT(rowCount(index.parent()) > index.row());
2017-08-20 07:11:58 -07:00
QString s;
QByteArray b;
switch (index.column()) {
case 0:
switch (role) {
2017-08-22 04:45:34 -07:00
case Qt::DisplayRole:
case Qt::EditRole:
case Qt::UserRole + 0:
2017-08-26 10:10:18 -07:00
persons_data_user_name(m_d, index.row(), &s, set_qstring);
2017-08-20 07:11:58 -07:00
if (!s.isNull()) v.setValue<QString>(s);
break;
}
break;
}
return v;
}
QHash<int, QByteArray> Persons::roleNames() const {
2017-08-21 15:51:48 -07:00
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
2017-08-22 04:45:34 -07:00
names.insert(Qt::UserRole + 0, "userName");
2017-08-20 07:11:58 -07:00
return names;
}
bool Persons::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool set = false;
if (index.column() == 0) {
2017-08-22 04:45:34 -07:00
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) {
2017-08-26 10:10:18 -07:00
set = persons_set_data_user_name(m_d, index.row(), value.value<QString>());
2017-08-20 07:11:58 -07:00
}
}
if (set) {
emit dataChanged(index, index, QVector<int>() << role);
}
return set;
}
2017-08-22 04:45:34 -07:00
extern "C" {
Persons::Private* persons_new(Persons*,
void (*)(const Persons*),
void (*)(Persons*, quintptr, quintptr),
2017-08-22 04:45:34 -07:00
void (*)(Persons*),
void (*)(Persons*),
void (*)(Persons*, int, int),
void (*)(Persons*),
void (*)(Persons*, int, int),
void (*)(Persons*));
void persons_free(Persons::Private*);
};
2017-08-26 10:10:18 -07:00
Persons::Persons(bool /*owned*/, QObject *parent):
QAbstractItemModel(parent),
m_d(0),
m_ownsPrivate(false)
{
}
2017-08-22 04:45:34 -07:00
Persons::Persons(QObject *parent):
QAbstractItemModel(parent),
2017-08-26 10:10:18 -07:00
m_d(persons_new(this,
2017-08-22 04:45:34 -07:00
[](const Persons* o) {
emit o->newDataReady(QModelIndex());
},
[](Persons* o, quintptr first, quintptr last) {
o->dataChanged(o->createIndex(first, 0, first),
o->createIndex(last, 0, last));
},
2017-08-22 04:45:34 -07:00
[](Persons* o) {
o->beginResetModel();
},
[](Persons* o) {
o->endResetModel();
},
[](Persons* o, int first, int last) {
o->beginInsertRows(QModelIndex(), first, last);
},
[](Persons* o) {
o->endInsertRows();
},
[](Persons* o, int first, int last) {
o->beginRemoveRows(QModelIndex(), first, last);
},
[](Persons* o) {
o->endRemoveRows();
}
2017-08-26 10:10:18 -07:00
)),
m_ownsPrivate(true)
{
2017-08-22 04:45:34 -07:00
connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) {
fetchMore(i);
}, Qt::QueuedConnection);
}
Persons::~Persons() {
2017-08-26 10:10:18 -07:00
if (m_ownsPrivate) {
persons_free(m_d);
}
2017-08-22 04:45:34 -07:00
}