diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 562ff65..6ff92fd 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -1,1754 +1,1767 @@ /* generated by rust_qt_binding_generator */ #include "Bindings.h" namespace { struct option_quintptr { public: quintptr value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_quintptr must be a POD type."); struct option_quint64 { public: quint64 value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_quint64 must be a POD type."); typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes); void set_qstring(QString* val, const char* utf8, int nbytes) { *val = QString::fromUtf8(utf8, nbytes); } typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { if (v->isNull() && nbytes == 0) { *v = QByteArray(bytes, nbytes); } else { v->truncate(0); v->append(bytes, nbytes); } } struct qmodelindex_t { int row; quintptr id; }; inline QVariant cleanNullQVariant(const QVariant& v) { return (v.isNull()) ?QVariant() :v; } inline void fibonacciInputChanged(Fibonacci* o) { emit o->inputChanged(); } inline void fibonacciResultChanged(Fibonacci* o) { emit o->resultChanged(); } inline void fileSystemTreePathChanged(FileSystemTree* o) { emit o->pathChanged(); } inline void processesActiveChanged(Processes* o) { emit o->activeChanged(); } } extern "C" { Demo::Private* demo_new(Demo*, Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*), FibonacciList*, void (*)(const FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*, quintptr, quintptr), void (*)(FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int, int), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int), void (*)(FibonacciList*), FileSystemTree*, void (*)(FileSystemTree*), void (*)(const FileSystemTree*, option_quintptr), void (*)(FileSystemTree*), void (*)(FileSystemTree*), void (*)(FileSystemTree*, quintptr, quintptr), void (*)(FileSystemTree*), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int, option_quintptr, int), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int), void (*)(FileSystemTree*), Processes*, void (*)(Processes*), void (*)(const Processes*, option_quintptr), void (*)(Processes*), void (*)(Processes*), void (*)(Processes*, quintptr, quintptr), void (*)(Processes*), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int, option_quintptr, int), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int), void (*)(Processes*), TimeSeries*, void (*)(const TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*, quintptr, quintptr), void (*)(TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int, int), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int), void (*)(TimeSeries*)); void demo_free(Demo::Private*); Fibonacci::Private* demo_fibonacci_get(const Demo::Private*); FibonacciList::Private* demo_fibonacci_list_get(const Demo::Private*); FileSystemTree::Private* demo_file_system_tree_get(const Demo::Private*); Processes::Private* demo_processes_get(const Demo::Private*); TimeSeries::Private* demo_time_series_get(const Demo::Private*); }; extern "C" { Fibonacci::Private* fibonacci_new(Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*)); void fibonacci_free(Fibonacci::Private*); quint32 fibonacci_input_get(const Fibonacci::Private*); void fibonacci_input_set(Fibonacci::Private*, quint32); quint64 fibonacci_result_get(const Fibonacci::Private*); }; extern "C" { quint64 fibonacci_list_data_fibonacci_number(const FibonacciList::Private*, int); quint64 fibonacci_list_data_row(const FibonacciList::Private*, int); void fibonacci_list_sort(FibonacciList::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int fibonacci_list_row_count(const FibonacciList::Private*); bool fibonacci_list_insert_rows(FibonacciList::Private*, int, int); bool fibonacci_list_remove_rows(FibonacciList::Private*, int, int); bool fibonacci_list_can_fetch_more(const FibonacciList::Private*); void fibonacci_list_fetch_more(FibonacciList::Private*); } int FibonacciList::columnCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : 2; } bool FibonacciList::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int FibonacciList::rowCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : fibonacci_list_row_count(m_d); } bool FibonacciList::insertRows(int row, int count, const QModelIndex &) { return fibonacci_list_insert_rows(m_d, row, count); } bool FibonacciList::removeRows(int row, int count, const QModelIndex &) { return fibonacci_list_remove_rows(m_d, row, count); } QModelIndex FibonacciList::index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 2) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); } QModelIndex FibonacciList::parent(const QModelIndex &) const { return QModelIndex(); } bool FibonacciList::canFetchMore(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : fibonacci_list_can_fetch_more(m_d); } void FibonacciList::fetchMore(const QModelIndex &parent) { if (!parent.isValid()) { fibonacci_list_fetch_more(m_d); } } void FibonacciList::updatePersistentIndexes() {} void FibonacciList::sort(int column, Qt::SortOrder order) { fibonacci_list_sort(m_d, column, order); } Qt::ItemFlags FibonacciList::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); return flags; } quint64 FibonacciList::fibonacciNumber(int row) const { return fibonacci_list_data_fibonacci_number(m_d, row); } quint64 FibonacciList::row(int row) const { return fibonacci_list_data_row(m_d, row); } QVariant FibonacciList::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::UserRole + 0: return QVariant::fromValue(fibonacciNumber(index.row())); case Qt::DisplayRole: case Qt::UserRole + 1: return QVariant::fromValue(row(index.row())); } + break; case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 0: return QVariant::fromValue(fibonacciNumber(index.row())); } + break; } return QVariant(); } int FibonacciList::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash FibonacciList::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "fibonacciNumber"); names.insert(Qt::UserRole + 1, "row"); return names; } QVariant FibonacciList::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool FibonacciList::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } extern "C" { FibonacciList::Private* fibonacci_list_new(FibonacciList*, void (*)(const FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*, quintptr, quintptr), void (*)(FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int, int), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int), void (*)(FibonacciList*)); void fibonacci_list_free(FibonacciList::Private*); }; extern "C" { void file_system_tree_data_file_icon(const FileSystemTree::Private*, quintptr, QByteArray*, qbytearray_set); void file_system_tree_data_file_name(const FileSystemTree::Private*, quintptr, QString*, qstring_set); void file_system_tree_data_file_path(const FileSystemTree::Private*, quintptr, QString*, qstring_set); qint32 file_system_tree_data_file_permissions(const FileSystemTree::Private*, quintptr); option_quint64 file_system_tree_data_file_size(const FileSystemTree::Private*, quintptr); qint32 file_system_tree_data_file_type(const FileSystemTree::Private*, quintptr); void file_system_tree_sort(FileSystemTree::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int file_system_tree_row_count(const FileSystemTree::Private*, option_quintptr); bool file_system_tree_can_fetch_more(const FileSystemTree::Private*, option_quintptr); void file_system_tree_fetch_more(FileSystemTree::Private*, option_quintptr); quintptr file_system_tree_index(const FileSystemTree::Private*, option_quintptr, int); qmodelindex_t file_system_tree_parent(const FileSystemTree::Private*, quintptr); int file_system_tree_row(const FileSystemTree::Private*, quintptr); option_quintptr file_system_tree_check_row(const FileSystemTree::Private*, quintptr, int); } int FileSystemTree::columnCount(const QModelIndex &) const { return 5; } bool FileSystemTree::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int FileSystemTree::rowCount(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return 0; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return file_system_tree_row_count(m_d, rust_parent); } bool FileSystemTree::insertRows(int, int, const QModelIndex &) { return false; // not supported yet } bool FileSystemTree::removeRows(int, int, const QModelIndex &) { return false; // not supported yet } QModelIndex FileSystemTree::index(int row, int column, const QModelIndex &parent) const { if (row < 0 || column < 0 || column >= 5) { return QModelIndex(); } if (parent.isValid() && parent.column() != 0) { return QModelIndex(); } if (row >= rowCount(parent)) { return QModelIndex(); } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; const quintptr id = file_system_tree_index(m_d, rust_parent, row); return createIndex(row, column, id); } QModelIndex FileSystemTree::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } const qmodelindex_t parent = file_system_tree_parent(m_d, index.internalId()); return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); } bool FileSystemTree::canFetchMore(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return false; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return file_system_tree_can_fetch_more(m_d, rust_parent); } void FileSystemTree::fetchMore(const QModelIndex &parent) { const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; file_system_tree_fetch_more(m_d, rust_parent); } void FileSystemTree::updatePersistentIndexes() { const auto from = persistentIndexList(); auto to = from; auto len = to.size(); for (int i = 0; i < len; ++i) { auto index = to.at(i); auto row = file_system_tree_check_row(m_d, index.internalId(), index.row()); if (row.some) { to[i] = createIndex(row.value, index.column(), index.internalId()); } else { to[i] = QModelIndex(); } } changePersistentIndexList(from, to); } void FileSystemTree::sort(int column, Qt::SortOrder order) { file_system_tree_sort(m_d, column, order); } Qt::ItemFlags FileSystemTree::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); return flags; } QByteArray FileSystemTree::fileIcon(const QModelIndex& index) const { QByteArray b; file_system_tree_data_file_icon(m_d, index.internalId(), &b, set_qbytearray); return b; } QString FileSystemTree::fileName(const QModelIndex& index) const { QString s; file_system_tree_data_file_name(m_d, index.internalId(), &s, set_qstring); return s; } QString FileSystemTree::filePath(const QModelIndex& index) const { QString s; file_system_tree_data_file_path(m_d, index.internalId(), &s, set_qstring); return s; } qint32 FileSystemTree::filePermissions(const QModelIndex& index) const { return file_system_tree_data_file_permissions(m_d, index.internalId()); } QVariant FileSystemTree::fileSize(const QModelIndex& index) const { QVariant v; v = file_system_tree_data_file_size(m_d, index.internalId()); return v; } qint32 FileSystemTree::fileType(const QModelIndex& index) const { return file_system_tree_data_file_type(m_d, index.internalId()); } QVariant FileSystemTree::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::DecorationRole: case Qt::UserRole + 0: return QVariant::fromValue(fileIcon(index)); case Qt::DisplayRole: case Qt::UserRole + 1: return QVariant::fromValue(fileName(index)); case Qt::UserRole + 2: return cleanNullQVariant(QVariant::fromValue(filePath(index))); case Qt::UserRole + 3: return QVariant::fromValue(filePermissions(index)); case Qt::UserRole + 4: return fileSize(index); case Qt::UserRole + 5: return QVariant::fromValue(fileType(index)); } + break; case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 4: return fileSize(index); } + break; case 2: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 2: return cleanNullQVariant(QVariant::fromValue(filePath(index))); } + break; case 3: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 3: return QVariant::fromValue(filePermissions(index)); } + break; case 4: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 5: return QVariant::fromValue(fileType(index)); } + break; } return QVariant(); } int FileSystemTree::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash FileSystemTree::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "fileIcon"); names.insert(Qt::UserRole + 1, "fileName"); names.insert(Qt::UserRole + 2, "filePath"); names.insert(Qt::UserRole + 3, "filePermissions"); names.insert(Qt::UserRole + 4, "fileSize"); names.insert(Qt::UserRole + 5, "fileType"); return names; } QVariant FileSystemTree::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool FileSystemTree::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } extern "C" { FileSystemTree::Private* file_system_tree_new(FileSystemTree*, void (*)(FileSystemTree*), void (*)(const FileSystemTree*, option_quintptr), void (*)(FileSystemTree*), void (*)(FileSystemTree*), void (*)(FileSystemTree*, quintptr, quintptr), void (*)(FileSystemTree*), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int, option_quintptr, int), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int), void (*)(FileSystemTree*)); void file_system_tree_free(FileSystemTree::Private*); void file_system_tree_path_get(const FileSystemTree::Private*, QString*, qstring_set); void file_system_tree_path_set(FileSystemTree::Private*, const ushort *str, int len); void file_system_tree_path_set_none(FileSystemTree::Private*); }; extern "C" { void processes_data_cmd(const Processes::Private*, quintptr, QString*, qstring_set); quint8 processes_data_cpu_percentage(const Processes::Private*, quintptr); float processes_data_cpu_usage(const Processes::Private*, quintptr); quint64 processes_data_memory(const Processes::Private*, quintptr); void processes_data_name(const Processes::Private*, quintptr, QString*, qstring_set); quint32 processes_data_pid(const Processes::Private*, quintptr); quint32 processes_data_uid(const Processes::Private*, quintptr); void processes_sort(Processes::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int processes_row_count(const Processes::Private*, option_quintptr); bool processes_can_fetch_more(const Processes::Private*, option_quintptr); void processes_fetch_more(Processes::Private*, option_quintptr); quintptr processes_index(const Processes::Private*, option_quintptr, int); qmodelindex_t processes_parent(const Processes::Private*, quintptr); int processes_row(const Processes::Private*, quintptr); option_quintptr processes_check_row(const Processes::Private*, quintptr, int); } int Processes::columnCount(const QModelIndex &) const { return 3; } bool Processes::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int Processes::rowCount(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return 0; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return processes_row_count(m_d, rust_parent); } bool Processes::insertRows(int, int, const QModelIndex &) { return false; // not supported yet } bool Processes::removeRows(int, int, const QModelIndex &) { return false; // not supported yet } QModelIndex Processes::index(int row, int column, const QModelIndex &parent) const { if (row < 0 || column < 0 || column >= 3) { return QModelIndex(); } if (parent.isValid() && parent.column() != 0) { return QModelIndex(); } if (row >= rowCount(parent)) { return QModelIndex(); } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; const quintptr id = processes_index(m_d, rust_parent, row); return createIndex(row, column, id); } QModelIndex Processes::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } const qmodelindex_t parent = processes_parent(m_d, index.internalId()); return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); } bool Processes::canFetchMore(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return false; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return processes_can_fetch_more(m_d, rust_parent); } void Processes::fetchMore(const QModelIndex &parent) { const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; processes_fetch_more(m_d, rust_parent); } void Processes::updatePersistentIndexes() { const auto from = persistentIndexList(); auto to = from; auto len = to.size(); for (int i = 0; i < len; ++i) { auto index = to.at(i); auto row = processes_check_row(m_d, index.internalId(), index.row()); if (row.some) { to[i] = createIndex(row.value, index.column(), index.internalId()); } else { to[i] = QModelIndex(); } } changePersistentIndexList(from, to); } void Processes::sort(int column, Qt::SortOrder order) { processes_sort(m_d, column, order); } Qt::ItemFlags Processes::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); return flags; } QString Processes::cmd(const QModelIndex& index) const { QString s; processes_data_cmd(m_d, index.internalId(), &s, set_qstring); return s; } quint8 Processes::cpuPercentage(const QModelIndex& index) const { return processes_data_cpu_percentage(m_d, index.internalId()); } float Processes::cpuUsage(const QModelIndex& index) const { return processes_data_cpu_usage(m_d, index.internalId()); } quint64 Processes::memory(const QModelIndex& index) const { return processes_data_memory(m_d, index.internalId()); } QString Processes::name(const QModelIndex& index) const { QString s; processes_data_name(m_d, index.internalId(), &s, set_qstring); return s; } quint32 Processes::pid(const QModelIndex& index) const { return processes_data_pid(m_d, index.internalId()); } quint32 Processes::uid(const QModelIndex& index) const { return processes_data_uid(m_d, index.internalId()); } QVariant Processes::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::UserRole + 0: return QVariant::fromValue(cmd(index)); case Qt::UserRole + 1: return QVariant::fromValue(cpuPercentage(index)); case Qt::UserRole + 2: return QVariant::fromValue(cpuUsage(index)); case Qt::UserRole + 3: return QVariant::fromValue(memory(index)); case Qt::DisplayRole: case Qt::UserRole + 4: return QVariant::fromValue(name(index)); case Qt::ToolTipRole: case Qt::UserRole + 5: return QVariant::fromValue(pid(index)); case Qt::UserRole + 6: return QVariant::fromValue(uid(index)); } + break; case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 2: return QVariant::fromValue(cpuUsage(index)); } + break; case 2: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 3: return QVariant::fromValue(memory(index)); } + break; } return QVariant(); } int Processes::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash Processes::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "cmd"); names.insert(Qt::UserRole + 1, "cpuPercentage"); names.insert(Qt::UserRole + 2, "cpuUsage"); names.insert(Qt::UserRole + 3, "memory"); names.insert(Qt::UserRole + 4, "name"); names.insert(Qt::UserRole + 5, "pid"); names.insert(Qt::UserRole + 6, "uid"); return names; } QVariant Processes::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool Processes::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } extern "C" { Processes::Private* processes_new(Processes*, void (*)(Processes*), void (*)(const Processes*, option_quintptr), void (*)(Processes*), void (*)(Processes*), void (*)(Processes*, quintptr, quintptr), void (*)(Processes*), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int, option_quintptr, int), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int), void (*)(Processes*)); void processes_free(Processes::Private*); bool processes_active_get(const Processes::Private*); void processes_active_set(Processes::Private*, bool); }; extern "C" { float time_series_data_cos(const TimeSeries::Private*, int); bool time_series_set_data_cos(TimeSeries::Private*, int, float); float time_series_data_sin(const TimeSeries::Private*, int); bool time_series_set_data_sin(TimeSeries::Private*, int, float); float time_series_data_time(const TimeSeries::Private*, int); bool time_series_set_data_time(TimeSeries::Private*, int, float); void time_series_sort(TimeSeries::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int time_series_row_count(const TimeSeries::Private*); bool time_series_insert_rows(TimeSeries::Private*, int, int); bool time_series_remove_rows(TimeSeries::Private*, int, int); bool time_series_can_fetch_more(const TimeSeries::Private*); void time_series_fetch_more(TimeSeries::Private*); } int TimeSeries::columnCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : 3; } bool TimeSeries::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int TimeSeries::rowCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : time_series_row_count(m_d); } bool TimeSeries::insertRows(int row, int count, const QModelIndex &) { return time_series_insert_rows(m_d, row, count); } bool TimeSeries::removeRows(int row, int count, const QModelIndex &) { return time_series_remove_rows(m_d, row, count); } QModelIndex TimeSeries::index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 3) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); } QModelIndex TimeSeries::parent(const QModelIndex &) const { return QModelIndex(); } bool TimeSeries::canFetchMore(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : time_series_can_fetch_more(m_d); } void TimeSeries::fetchMore(const QModelIndex &parent) { if (!parent.isValid()) { time_series_fetch_more(m_d); } } void TimeSeries::updatePersistentIndexes() {} void TimeSeries::sort(int column, Qt::SortOrder order) { time_series_sort(m_d, column, order); } Qt::ItemFlags TimeSeries::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); if (i.column() == 0) { flags |= Qt::ItemIsEditable; } if (i.column() == 1) { flags |= Qt::ItemIsEditable; } if (i.column() == 2) { flags |= Qt::ItemIsEditable; } return flags; } float TimeSeries::cos(int row) const { return time_series_data_cos(m_d, row); } bool TimeSeries::setCos(int row, float value) { bool set = false; set = time_series_set_data_cos(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } float TimeSeries::sin(int row) const { return time_series_data_sin(m_d, row); } bool TimeSeries::setSin(int row, float value) { bool set = false; set = time_series_set_data_sin(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } float TimeSeries::time(int row) const { return time_series_data_time(m_d, row); } bool TimeSeries::setTime(int row, float value) { bool set = false; set = time_series_set_data_time(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant TimeSeries::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::UserRole + 0: return QVariant::fromValue(cos(index.row())); case Qt::UserRole + 1: return QVariant::fromValue(sin(index.row())); case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 2: return QVariant::fromValue(time(index.row())); } + break; case 1: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 1: return QVariant::fromValue(sin(index.row())); } + break; case 2: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 0: return QVariant::fromValue(cos(index.row())); } + break; } return QVariant(); } int TimeSeries::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash TimeSeries::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "cos"); names.insert(Qt::UserRole + 1, "sin"); names.insert(Qt::UserRole + 2, "time"); return names; } QVariant TimeSeries::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool TimeSeries::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } bool TimeSeries::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.column() == 0) { if (role == Qt::UserRole + 0) { if (value.canConvert(qMetaTypeId())) { return setCos(index.row(), value.value()); } } if (role == Qt::UserRole + 1) { if (value.canConvert(qMetaTypeId())) { return setSin(index.row(), value.value()); } } if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 2) { if (value.canConvert(qMetaTypeId())) { return setTime(index.row(), value.value()); } } } if (index.column() == 1) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 1) { if (value.canConvert(qMetaTypeId())) { return setSin(index.row(), value.value()); } } } if (index.column() == 2) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { if (value.canConvert(qMetaTypeId())) { return setCos(index.row(), value.value()); } } } return false; } extern "C" { TimeSeries::Private* time_series_new(TimeSeries*, void (*)(const TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*, quintptr, quintptr), void (*)(TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int, int), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int), void (*)(TimeSeries*)); void time_series_free(TimeSeries::Private*); }; Demo::Demo(bool /*owned*/, QObject *parent): QObject(parent), m_fibonacci(new Fibonacci(false, this)), m_fibonacciList(new FibonacciList(false, this)), m_fileSystemTree(new FileSystemTree(false, this)), m_processes(new Processes(false, this)), m_timeSeries(new TimeSeries(false, this)), m_d(0), m_ownsPrivate(false) { } Demo::Demo(QObject *parent): QObject(parent), m_fibonacci(new Fibonacci(false, this)), m_fibonacciList(new FibonacciList(false, this)), m_fileSystemTree(new FileSystemTree(false, this)), m_processes(new Processes(false, this)), m_timeSeries(new TimeSeries(false, this)), m_d(demo_new(this, m_fibonacci, fibonacciInputChanged, fibonacciResultChanged, m_fibonacciList, [](const FibonacciList* o) { emit o->newDataReady(QModelIndex()); }, [](FibonacciList* o) { emit o->layoutAboutToBeChanged(); }, [](FibonacciList* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](FibonacciList* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 1, last)); }, [](FibonacciList* o) { o->beginResetModel(); }, [](FibonacciList* o) { o->endResetModel(); }, [](FibonacciList* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](FibonacciList* o) { o->endInsertRows(); }, [](FibonacciList* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](FibonacciList* o) { o->endMoveRows(); }, [](FibonacciList* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](FibonacciList* o) { o->endRemoveRows(); } , m_fileSystemTree, fileSystemTreePathChanged, [](const FileSystemTree* o, option_quintptr id) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); emit o->newDataReady(o->createIndex(row, 0, id.value)); } else { emit o->newDataReady(QModelIndex()); } }, [](FileSystemTree* o) { emit o->layoutAboutToBeChanged(); }, [](FileSystemTree* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](FileSystemTree* o, quintptr first, quintptr last) { quintptr frow = file_system_tree_row(o->m_d, first); quintptr lrow = file_system_tree_row(o->m_d, first); o->dataChanged(o->createIndex(frow, 0, first), o->createIndex(lrow, 4, last)); }, [](FileSystemTree* o) { o->beginResetModel(); }, [](FileSystemTree* o) { o->endResetModel(); }, [](FileSystemTree* o, option_quintptr id, int first, int last) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginInsertRows(QModelIndex(), first, last); } }, [](FileSystemTree* o) { o->endInsertRows(); }, [](FileSystemTree* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) { QModelIndex s; if (sourceParent.some) { int row = file_system_tree_row(o->m_d, sourceParent.value); s = o->createIndex(row, 0, sourceParent.value); } QModelIndex d; if (destinationParent.some) { int row = file_system_tree_row(o->m_d, destinationParent.value); d = o->createIndex(row, 0, destinationParent.value); } o->beginMoveRows(s, first, last, d, destination); }, [](FileSystemTree* o) { o->endMoveRows(); }, [](FileSystemTree* o, option_quintptr id, int first, int last) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](FileSystemTree* o) { o->endRemoveRows(); } , m_processes, processesActiveChanged, [](const Processes* o, option_quintptr id) { if (id.some) { int row = processes_row(o->m_d, id.value); emit o->newDataReady(o->createIndex(row, 0, id.value)); } else { emit o->newDataReady(QModelIndex()); } }, [](Processes* o) { emit o->layoutAboutToBeChanged(); }, [](Processes* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](Processes* o, quintptr first, quintptr last) { quintptr frow = processes_row(o->m_d, first); quintptr lrow = processes_row(o->m_d, first); o->dataChanged(o->createIndex(frow, 0, first), o->createIndex(lrow, 2, last)); }, [](Processes* o) { o->beginResetModel(); }, [](Processes* o) { o->endResetModel(); }, [](Processes* o, option_quintptr id, int first, int last) { if (id.some) { int row = processes_row(o->m_d, id.value); o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginInsertRows(QModelIndex(), first, last); } }, [](Processes* o) { o->endInsertRows(); }, [](Processes* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) { QModelIndex s; if (sourceParent.some) { int row = processes_row(o->m_d, sourceParent.value); s = o->createIndex(row, 0, sourceParent.value); } QModelIndex d; if (destinationParent.some) { int row = processes_row(o->m_d, destinationParent.value); d = o->createIndex(row, 0, destinationParent.value); } o->beginMoveRows(s, first, last, d, destination); }, [](Processes* o) { o->endMoveRows(); }, [](Processes* o, option_quintptr id, int first, int last) { if (id.some) { int row = processes_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](Processes* o) { o->endRemoveRows(); } , m_timeSeries, [](const TimeSeries* o) { emit o->newDataReady(QModelIndex()); }, [](TimeSeries* o) { emit o->layoutAboutToBeChanged(); }, [](TimeSeries* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](TimeSeries* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 2, last)); }, [](TimeSeries* o) { o->beginResetModel(); }, [](TimeSeries* o) { o->endResetModel(); }, [](TimeSeries* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](TimeSeries* o) { o->endInsertRows(); }, [](TimeSeries* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](TimeSeries* o) { o->endMoveRows(); }, [](TimeSeries* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](TimeSeries* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { m_fibonacci->m_d = demo_fibonacci_get(m_d); m_fibonacciList->m_d = demo_fibonacci_list_get(m_d); m_fileSystemTree->m_d = demo_file_system_tree_get(m_d); m_processes->m_d = demo_processes_get(m_d); m_timeSeries->m_d = demo_time_series_get(m_d); connect(this->m_fibonacciList, &FibonacciList::newDataReady, this->m_fibonacciList, [this](const QModelIndex& i) { this->m_fibonacciList->fetchMore(i); }, Qt::QueuedConnection); connect(this->m_fileSystemTree, &FileSystemTree::newDataReady, this->m_fileSystemTree, [this](const QModelIndex& i) { this->m_fileSystemTree->fetchMore(i); }, Qt::QueuedConnection); connect(this->m_processes, &Processes::newDataReady, this->m_processes, [this](const QModelIndex& i) { this->m_processes->fetchMore(i); }, Qt::QueuedConnection); connect(this->m_timeSeries, &TimeSeries::newDataReady, this->m_timeSeries, [this](const QModelIndex& i) { this->m_timeSeries->fetchMore(i); }, Qt::QueuedConnection); } Demo::~Demo() { if (m_ownsPrivate) { demo_free(m_d); } } const Fibonacci* Demo::fibonacci() const { return m_fibonacci; } Fibonacci* Demo::fibonacci() { return m_fibonacci; } const FibonacciList* Demo::fibonacciList() const { return m_fibonacciList; } FibonacciList* Demo::fibonacciList() { return m_fibonacciList; } const FileSystemTree* Demo::fileSystemTree() const { return m_fileSystemTree; } FileSystemTree* Demo::fileSystemTree() { return m_fileSystemTree; } const Processes* Demo::processes() const { return m_processes; } Processes* Demo::processes() { return m_processes; } const TimeSeries* Demo::timeSeries() const { return m_timeSeries; } TimeSeries* Demo::timeSeries() { return m_timeSeries; } Fibonacci::Fibonacci(bool /*owned*/, QObject *parent): QObject(parent), m_d(0), m_ownsPrivate(false) { } Fibonacci::Fibonacci(QObject *parent): QObject(parent), m_d(fibonacci_new(this, fibonacciInputChanged, fibonacciResultChanged)), m_ownsPrivate(true) { } Fibonacci::~Fibonacci() { if (m_ownsPrivate) { fibonacci_free(m_d); } } quint32 Fibonacci::input() const { return fibonacci_input_get(m_d); } void Fibonacci::setInput(quint32 v) { fibonacci_input_set(m_d, v); } quint64 Fibonacci::result() const { return fibonacci_result_get(m_d); } FibonacciList::FibonacciList(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } FibonacciList::FibonacciList(QObject *parent): QAbstractItemModel(parent), m_d(fibonacci_list_new(this, [](const FibonacciList* o) { emit o->newDataReady(QModelIndex()); }, [](FibonacciList* o) { emit o->layoutAboutToBeChanged(); }, [](FibonacciList* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](FibonacciList* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 1, last)); }, [](FibonacciList* o) { o->beginResetModel(); }, [](FibonacciList* o) { o->endResetModel(); }, [](FibonacciList* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](FibonacciList* o) { o->endInsertRows(); }, [](FibonacciList* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](FibonacciList* o) { o->endMoveRows(); }, [](FibonacciList* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](FibonacciList* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &FibonacciList::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } FibonacciList::~FibonacciList() { if (m_ownsPrivate) { fibonacci_list_free(m_d); } } void FibonacciList::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("row")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("fibonacciNumber")); } FileSystemTree::FileSystemTree(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } FileSystemTree::FileSystemTree(QObject *parent): QAbstractItemModel(parent), m_d(file_system_tree_new(this, fileSystemTreePathChanged, [](const FileSystemTree* o, option_quintptr id) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); emit o->newDataReady(o->createIndex(row, 0, id.value)); } else { emit o->newDataReady(QModelIndex()); } }, [](FileSystemTree* o) { emit o->layoutAboutToBeChanged(); }, [](FileSystemTree* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](FileSystemTree* o, quintptr first, quintptr last) { quintptr frow = file_system_tree_row(o->m_d, first); quintptr lrow = file_system_tree_row(o->m_d, first); o->dataChanged(o->createIndex(frow, 0, first), o->createIndex(lrow, 4, last)); }, [](FileSystemTree* o) { o->beginResetModel(); }, [](FileSystemTree* o) { o->endResetModel(); }, [](FileSystemTree* o, option_quintptr id, int first, int last) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginInsertRows(QModelIndex(), first, last); } }, [](FileSystemTree* o) { o->endInsertRows(); }, [](FileSystemTree* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) { QModelIndex s; if (sourceParent.some) { int row = file_system_tree_row(o->m_d, sourceParent.value); s = o->createIndex(row, 0, sourceParent.value); } QModelIndex d; if (destinationParent.some) { int row = file_system_tree_row(o->m_d, destinationParent.value); d = o->createIndex(row, 0, destinationParent.value); } o->beginMoveRows(s, first, last, d, destination); }, [](FileSystemTree* o) { o->endMoveRows(); }, [](FileSystemTree* o, option_quintptr id, int first, int last) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](FileSystemTree* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &FileSystemTree::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } FileSystemTree::~FileSystemTree() { if (m_ownsPrivate) { file_system_tree_free(m_d); } } void FileSystemTree::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("fileName")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("fileSize")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("filePath")); m_headerData.insert(qMakePair(3, Qt::DisplayRole), QVariant("filePermissions")); m_headerData.insert(qMakePair(4, Qt::DisplayRole), QVariant("fileType")); } QString FileSystemTree::path() const { QString v; file_system_tree_path_get(m_d, &v, set_qstring); return v; } void FileSystemTree::setPath(const QString& v) { if (v.isNull()) { file_system_tree_path_set_none(m_d); } else { file_system_tree_path_set(m_d, reinterpret_cast(v.data()), v.size()); } } Processes::Processes(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } Processes::Processes(QObject *parent): QAbstractItemModel(parent), m_d(processes_new(this, processesActiveChanged, [](const Processes* o, option_quintptr id) { if (id.some) { int row = processes_row(o->m_d, id.value); emit o->newDataReady(o->createIndex(row, 0, id.value)); } else { emit o->newDataReady(QModelIndex()); } }, [](Processes* o) { emit o->layoutAboutToBeChanged(); }, [](Processes* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](Processes* o, quintptr first, quintptr last) { quintptr frow = processes_row(o->m_d, first); quintptr lrow = processes_row(o->m_d, first); o->dataChanged(o->createIndex(frow, 0, first), o->createIndex(lrow, 2, last)); }, [](Processes* o) { o->beginResetModel(); }, [](Processes* o) { o->endResetModel(); }, [](Processes* o, option_quintptr id, int first, int last) { if (id.some) { int row = processes_row(o->m_d, id.value); o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginInsertRows(QModelIndex(), first, last); } }, [](Processes* o) { o->endInsertRows(); }, [](Processes* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) { QModelIndex s; if (sourceParent.some) { int row = processes_row(o->m_d, sourceParent.value); s = o->createIndex(row, 0, sourceParent.value); } QModelIndex d; if (destinationParent.some) { int row = processes_row(o->m_d, destinationParent.value); d = o->createIndex(row, 0, destinationParent.value); } o->beginMoveRows(s, first, last, d, destination); }, [](Processes* o) { o->endMoveRows(); }, [](Processes* o, option_quintptr id, int first, int last) { if (id.some) { int row = processes_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](Processes* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &Processes::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } Processes::~Processes() { if (m_ownsPrivate) { processes_free(m_d); } } void Processes::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("name")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("cpuUsage")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("memory")); } bool Processes::active() const { return processes_active_get(m_d); } void Processes::setActive(bool v) { processes_active_set(m_d, v); } TimeSeries::TimeSeries(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } TimeSeries::TimeSeries(QObject *parent): QAbstractItemModel(parent), m_d(time_series_new(this, [](const TimeSeries* o) { emit o->newDataReady(QModelIndex()); }, [](TimeSeries* o) { emit o->layoutAboutToBeChanged(); }, [](TimeSeries* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](TimeSeries* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 2, last)); }, [](TimeSeries* o) { o->beginResetModel(); }, [](TimeSeries* o) { o->endResetModel(); }, [](TimeSeries* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](TimeSeries* o) { o->endInsertRows(); }, [](TimeSeries* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](TimeSeries* o) { o->endMoveRows(); }, [](TimeSeries* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](TimeSeries* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &TimeSeries::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } TimeSeries::~TimeSeries() { if (m_ownsPrivate) { time_series_free(m_d); } } void TimeSeries::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("time")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("sin")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("cos")); } diff --git a/src/cpp.cpp b/src/cpp.cpp index 34f660d..3e09321 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -1,1194 +1,1194 @@ /* * Copyright 2017 Jos van den Oever * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "structs.h" #include "cpp.h" #include "helper.h" #include #include template QString cppSetType(const T& p) { if (p.optional) { return "option_" + p.type.cppSetType; } return p.type.cppSetType; } template QString propertyType(const T& p) { return (p.optional && !p.type.isComplex()) ?"QVariant" :p.type.name; } QString upperInitial(const QString& name) { return name.left(1).toUpper() + name.mid(1); } QString lowerInitial(const QString& name) { return name.left(1).toLower() + name.mid(1); } QString writeProperty(const QString& name) { return "WRITE set" + upperInitial(name) + " "; } QString baseType(const Object& o) { if (o.type != ObjectType::Object) { return "QAbstractItemModel"; } return "QObject"; } QString cGetType(const BindingTypeProperties& type) { return type.name + "*, " + type.name.toLower() + "_set"; } bool modelIsWritable(const Object& o) { bool write = false; for (auto ip: o.itemProperties) { write |= ip.write; } return write; } void writeHeaderItemModel(QTextStream& h, const Object& o) { h << QString(R"( int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; Qt::ItemFlags flags(const QModelIndex &index) const override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; int role(const char* name) const; QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; )"); if (modelIsWritable(o)) { h << " bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;\n"; } for (auto ip: o.itemProperties) { auto r = propertyType(ip); auto rw = r; if (r == "QVariant" || ip.type.isComplex()) { rw = "const " + r + "&"; } if (o.type == ObjectType::List) { h << QString(" Q_INVOKABLE %2 %1(int row) const;\n").arg(ip.name, r); if (ip.write) { h << QString(" Q_INVOKABLE bool set%1(int row, %2 value);\n").arg(upperInitial(ip.name), rw); } } else { h << QString(" Q_INVOKABLE %2 %1(const QModelIndex& index) const;\n").arg(ip.name, r); if (ip.write) { h << QString(" Q_INVOKABLE bool set%1(const QModelIndex& index, %2 value);\n").arg(upperInitial(ip.name), rw); } } } h << R"( signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); void updatePersistentIndexes(); )"; } bool isColumnWrite(const Object& o, int col) { for (auto ip: o.itemProperties) { if (ip.write && (col == 0 || (ip.roles.size() > col && ip.roles[col].size() > 0))) { return true; } } return false; } void writeModelGetterSetter(QTextStream& cpp, const QString& index, const ItemProperty& ip, const Object& o) { const QString lcname(snakeCase(o.name)); QString idx = index; // getter auto r = propertyType(ip); if (o.type == ObjectType::List) { idx = ", row"; cpp << QString("%3 %1::%2(int row) const\n{\n") .arg(o.name, ip.name, r); } else { cpp << QString("%3 %1::%2(const QModelIndex& index) const\n{\n") .arg(o.name, ip.name, r); } if (ip.type.name == "QString") { cpp << " QString s;\n"; cpp << QString(" %1_data_%2(m_d%4, &s, set_%3);\n") .arg(lcname, snakeCase(ip.name), ip.type.name.toLower(), idx); cpp << " return s;\n"; } else if (ip.type.name == "QByteArray") { cpp << " QByteArray b;\n"; cpp << QString(" %1_data_%2(m_d%4, &b, set_%3);\n") .arg(lcname, snakeCase(ip.name), ip.type.name.toLower(), idx); cpp << " return b;\n"; } else if (ip.optional) { cpp << " QVariant v;\n"; cpp << QString(" v = %1_data_%2(m_d%3);\n") .arg(lcname, snakeCase(ip.name), idx); cpp << " return v;\n"; } else { cpp << QString(" return %1_data_%2(m_d%3);\n") .arg(lcname, snakeCase(ip.name), idx); } cpp << "}\n\n"; if (!ip.write) { return; } // setter if (r == "QVariant" || ip.type.isComplex()) { r = "const " + r + "&"; } if (o.type == ObjectType::List) { idx = ", row"; cpp << QString("bool %1::set%2(int row, %3 value)\n{\n") .arg(o.name, upperInitial(ip.name), r); } else { cpp << QString("bool %1::set%2(const QModelIndex& index, %3 value)\n{\n") .arg(o.name, upperInitial(ip.name), r); } cpp << " bool set = false;\n"; if (ip.optional) { QString test = "value.isNull()"; if (!ip.type.isComplex()) { test += " || !value.isValid()"; } cpp << " if (" << test << ") {\n"; cpp << QString(" set = %1_set_data_%2_none(m_d%3);") .arg(lcname, snakeCase(ip.name), idx) << endl; cpp << " } else {\n"; } if (ip.optional && !ip.type.isComplex()) { cpp << QString(" if (!value.canConvert(qMetaTypeId<%1>())) {\n return false;\n }\n").arg(ip.type.name); cpp << QString(" set = %1_set_data_%2(m_d%3, value.value<%4>());") .arg(lcname, snakeCase(ip.name), idx, ip.type.name) << endl; } else { QString val = "value"; if (ip.type.isComplex()) { if (ip.type.name == "QString") { val = "value.utf16(), value.length()"; } else { val = "value.data(), value.length()"; } } cpp << QString(" set = %1_set_data_%2(m_d%3, %4);") .arg(lcname, snakeCase(ip.name), idx, val) << endl; } if (ip.optional) { cpp << " }\n"; } if (o.type == ObjectType::List) { cpp << R"( if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } )"; } else { cpp << R"( if (set) { emit dataChanged(index, index); } return set; } )"; } } void writeCppModel(QTextStream& cpp, const Object& o) { const QString lcname(snakeCase(o.name)); QString indexDecl = ", int"; QString index = ", index.row()"; if (o.type == ObjectType::Tree) { indexDecl = ", quintptr"; index = ", index.internalId()"; } cpp << "extern \"C\" {\n"; for (auto ip: o.itemProperties) { if (ip.type.isComplex()) { cpp << QString(" void %2_data_%3(const %1::Private*%5, %4);\n") .arg(o.name, lcname, snakeCase(ip.name), cGetType(ip.type), indexDecl); } else { cpp << QString(" %4 %2_data_%3(const %1::Private*%5);\n") .arg(o.name, lcname, snakeCase(ip.name), cppSetType(ip), indexDecl); } if (ip.write) { if (ip.type.name == "QString") { cpp << QString(" bool %2_set_data_%3(%1::Private*%4, const ushort* s, int len);") .arg(o.name, lcname, snakeCase(ip.name), indexDecl) << endl; } else if (ip.type.name == "QByteArray") { cpp << QString(" bool %2_set_data_%3(%1::Private*%4, const char* s, int len);") .arg(o.name, lcname, snakeCase(ip.name), indexDecl) << endl; } else { cpp << QString(" bool %2_set_data_%3(%1::Private*%5, %4);") .arg(o.name, lcname, snakeCase(ip.name), ip.type.cSetType, indexDecl) << endl; } if (ip.optional) { cpp << QString(" bool %2_set_data_%3_none(%1::Private*%4);") .arg(o.name, lcname, snakeCase(ip.name), indexDecl) << endl; } } } cpp << QString(" void %2_sort(%1::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);\n").arg(o.name, lcname); if (o.type == ObjectType::List) { cpp << QString(R"( int %2_row_count(const %1::Private*); bool %2_insert_rows(%1::Private*, int, int); bool %2_remove_rows(%1::Private*, int, int); bool %2_can_fetch_more(const %1::Private*); void %2_fetch_more(%1::Private*); } int %1::columnCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : %3; } bool %1::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int %1::rowCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : %2_row_count(m_d); } bool %1::insertRows(int row, int count, const QModelIndex &) { return %2_insert_rows(m_d, row, count); } bool %1::removeRows(int row, int count, const QModelIndex &) { return %2_remove_rows(m_d, row, count); } QModelIndex %1::index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < %3) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); } QModelIndex %1::parent(const QModelIndex &) const { return QModelIndex(); } bool %1::canFetchMore(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : %2_can_fetch_more(m_d); } void %1::fetchMore(const QModelIndex &parent) { if (!parent.isValid()) { %2_fetch_more(m_d); } } void %1::updatePersistentIndexes() {} )").arg(o.name, lcname, QString::number(o.columnCount)); } else { cpp << QString(R"( int %2_row_count(const %1::Private*, option_quintptr); bool %2_can_fetch_more(const %1::Private*, option_quintptr); void %2_fetch_more(%1::Private*, option_quintptr); quintptr %2_index(const %1::Private*, option_quintptr, int); qmodelindex_t %2_parent(const %1::Private*, quintptr); int %2_row(const %1::Private*, quintptr); option_quintptr %2_check_row(const %1::Private*, quintptr, int); } int %1::columnCount(const QModelIndex &) const { return %3; } bool %1::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int %1::rowCount(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return 0; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return %2_row_count(m_d, rust_parent); } bool %1::insertRows(int, int, const QModelIndex &) { return false; // not supported yet } bool %1::removeRows(int, int, const QModelIndex &) { return false; // not supported yet } QModelIndex %1::index(int row, int column, const QModelIndex &parent) const { if (row < 0 || column < 0 || column >= %3) { return QModelIndex(); } if (parent.isValid() && parent.column() != 0) { return QModelIndex(); } if (row >= rowCount(parent)) { return QModelIndex(); } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; const quintptr id = %2_index(m_d, rust_parent, row); return createIndex(row, column, id); } QModelIndex %1::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } const qmodelindex_t parent = %2_parent(m_d, index.internalId()); return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); } bool %1::canFetchMore(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return false; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return %2_can_fetch_more(m_d, rust_parent); } void %1::fetchMore(const QModelIndex &parent) { const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; %2_fetch_more(m_d, rust_parent); } void %1::updatePersistentIndexes() { const auto from = persistentIndexList(); auto to = from; auto len = to.size(); for (int i = 0; i < len; ++i) { auto index = to.at(i); auto row = %2_check_row(m_d, index.internalId(), index.row()); if (row.some) { to[i] = createIndex(row.value, index.column(), index.internalId()); } else { to[i] = QModelIndex(); } } changePersistentIndexList(from, to); } )").arg(o.name, lcname, QString::number(o.columnCount)); } cpp << QString(R"( void %1::sort(int column, Qt::SortOrder order) { %2_sort(m_d, column, order); } Qt::ItemFlags %1::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); )").arg(o.name, lcname); for (int col = 0; col < o.columnCount; ++col) { if (isColumnWrite(o, col)) { cpp << " if (i.column() == " << col << ") {\n"; cpp << " flags |= Qt::ItemIsEditable;\n }\n"; } } cpp << " return flags;\n}\n\n"; for (auto ip: o.itemProperties) { writeModelGetterSetter(cpp, index, ip, o); } cpp << QString(R"(QVariant %1::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { )").arg(o.name); auto metaRoles = QMetaEnum::fromType(); for (int col = 0; col < o.columnCount; ++col) { cpp << QString(" case %1:\n").arg(col); cpp << QString(" switch (role) {\n"); for (int i = 0; i < o.itemProperties.size(); ++i) { auto ip = o.itemProperties[i]; auto roles = ip.roles.value(col); if (col > 0 && roles.size() == 0) { continue; } for (auto role: roles) { cpp << QString(" case Qt::%1:\n").arg(metaRoles.valueToKey(role)); } cpp << QString(" case Qt::UserRole + %1:\n").arg(i); auto ii = (o.type == ObjectType::List) ?".row()" :""; if (ip.optional && !ip.type.isComplex()) { cpp << QString(" return %1(index%2);\n").arg(ip.name, ii); } else if (ip.optional) { cpp << QString(" return cleanNullQVariant(QVariant::fromValue(%1(index%2)));\n").arg(ip.name, ii); } else { cpp << QString(" return QVariant::fromValue(%1(index%2));\n").arg(ip.name, ii); } } - cpp << " }\n"; + cpp << " }\n break;\n"; } cpp << " }\n return QVariant();\n}\n\n"; cpp << "int " << o.name << "::role(const char* name) const {\n"; cpp << " auto names = roleNames();\n"; cpp << " auto i = names.constBegin();\n"; cpp << " while (i != names.constEnd()) {\n"; cpp << " if (i.value() == name) {\n"; cpp << " return i.key();\n"; cpp << " }\n"; cpp << " ++i;\n"; cpp << " }\n"; cpp << " return -1;\n"; cpp << "}\n"; cpp << "QHash " << o.name << "::roleNames() const {\n"; cpp << " QHash names = QAbstractItemModel::roleNames();\n"; for (int i = 0; i < o.itemProperties.size(); ++i) { auto ip = o.itemProperties[i]; cpp << " names.insert(Qt::UserRole + " << i << ", \"" << ip.name << "\");\n"; } cpp << " return names;\n"; cpp << QString(R"(} QVariant %1::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool %1::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } )").arg(o.name); if (modelIsWritable(o)) { cpp << QString("bool %1::setData(const QModelIndex &index, const QVariant &value, int role)\n{\n").arg(o.name); for (int col = 0; col < o.columnCount; ++col) { if (!isColumnWrite(o, col)) { continue; } cpp << " if (index.column() == " << col << ") {\n"; for (int i = 0; i < o.itemProperties.size(); ++i) { auto ip = o.itemProperties[i]; if (!ip.write) { continue; } auto roles = ip.roles.value(col); if (col > 0 && roles.size() == 0) { continue; } cpp << " if ("; for (auto role: roles) { cpp << QString("role == Qt::%1 || ").arg(metaRoles.valueToKey(role)); } cpp << "role == Qt::UserRole + " << i << ") {\n"; auto ii = (o.type == ObjectType::List) ?".row()" :""; if (ip.optional && !ip.type.isComplex()) { cpp << QString(" return set%1(index%2, value);\n") .arg(upperInitial(ip.name), ii); } else { QString pre = ""; if (ip.optional) { pre = "!value.isValid() || value.isNull() ||"; } cpp << QString(" if (%2value.canConvert(qMetaTypeId<%1>())) {\n").arg(ip.type.name, pre); cpp << QString(" return set%1(index%2, value.value<%3>());\n").arg(upperInitial(ip.name), ii, ip.type.name); cpp << QString(" }\n"); } cpp << " }\n"; } cpp << " }\n"; } cpp << " return false;\n}\n\n"; } } void writeHeaderObject(QTextStream& h, const Object& o, const Configuration& conf) { h << QString(R"( class %1 : public %3 { Q_OBJEC%2 )").arg(o.name, "T", baseType(o)); for (auto object: conf.objects) { if (object.containsObject() && o.name != object.name) { h << " friend class " << object.name << ";\n"; } } h << R"(public: class Private; private: )"; for (auto p: o.properties) { if (p.type.type == BindingType::Object) { h << " " << p.type.name << "* const m_" << p.name << ";\n"; } } h << R"( Private * m_d; bool m_ownsPrivate; )"; for (auto p: o.properties) { bool obj = p.type.type == BindingType::Object; auto t = p.type.name; if (p.optional && !p.type.isComplex()) { t = "QVariant"; } h << QString(" Q_PROPERTY(%1 %2 READ %2 %3NOTIFY %2Changed FINAL)") .arg(t + (obj ?"*" :""), p.name, p.write ? writeProperty(p.name) :"") << endl; } h << QString(R"( explicit %1(bool owned, QObject *parent); public: explicit %1(QObject *parent = nullptr); ~%1(); )").arg(o.name); for (auto p: o.properties) { if (p.type.type == BindingType::Object) { h << " const " << p.type.name << "* " << p.name << "() const;" << endl; h << " " << p.type.name << "* " << p.name << "();" << endl; } else { auto t = p.type.name; auto t2 = p.type.cppSetType; if (p.optional && !p.type.isComplex()) { t = "QVariant"; t2 = "const QVariant&"; } h << " " << t << " " << p.name << "() const;" << endl; if (p.write) { h << " void set" << upperInitial(p.name) << "(" << t2 << " v);" << endl; } } } for (auto f: o.functions) { h << " Q_INVOKABLE " << f.type.name << " " << f.name << "("; for (auto a = f.args.begin(); a < f.args.end(); a++) { if (a != f.args.begin()) { h << ", "; } h << QString("%1 %2").arg(a->type.cppSetType, a->name); } h << QString(")%1;").arg(f.mut ? "" : " const"); h << endl; } if (baseType(o) == "QAbstractItemModel") { writeHeaderItemModel(h, o); } h << "signals:" << endl; for (auto p: o.properties) { h << " void " << p.name << "Changed();" << endl; } h << "};" << endl; } void constructorArgsDecl(QTextStream& cpp, const Object& o, const Configuration& conf) { cpp << o.name << "*"; for (auto p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(", "); constructorArgsDecl(cpp, conf.findObject(p.type.name), conf); } else { cpp << QString(", void (*)(%1*)").arg(o.name); } } if (o.type == ObjectType::List) { cpp << QString(R"(, void (*)(const %1*), void (*)(%1*), void (*)(%1*), void (*)(%1*, quintptr, quintptr), void (*)(%1*), void (*)(%1*), void (*)(%1*, int, int), void (*)(%1*), void (*)(%1*, int, int, int), void (*)(%1*), void (*)(%1*, int, int), void (*)(%1*))").arg(o.name); } if (o.type == ObjectType::Tree) { cpp << QString(R"(, void (*)(const %1*, option_quintptr), void (*)(%1*), void (*)(%1*), void (*)(%1*, quintptr, quintptr), void (*)(%1*), void (*)(%1*), void (*)(%1*, option_quintptr, int, int), void (*)(%1*), void (*)(%1*, option_quintptr, int, int, option_quintptr, int), void (*)(%1*), void (*)(%1*, option_quintptr, int, int), void (*)(%1*))").arg(o.name); } } QString changedF(const Object& o, const Property& p) { return lowerInitial(o.name) + upperInitial(p.name) + "Changed"; } void constructorArgs(QTextStream& cpp, const QString& prefix, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << ", " << prefix << "m_" << p.name; constructorArgs(cpp, "m_" + p.name + "->", conf.findObject(p.type.name), conf); } else { cpp << ",\n " << changedF(o, p); } } if (o.type == ObjectType::List) { cpp << QString(R"(, [](const %1* o) { emit o->newDataReady(QModelIndex()); }, [](%1* o) { emit o->layoutAboutToBeChanged(); }, [](%1* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](%1* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, %2, last)); }, [](%1* o) { o->beginResetModel(); }, [](%1* o) { o->endResetModel(); }, [](%1* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](%1* o) { o->endInsertRows(); }, [](%1* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](%1* o) { o->endMoveRows(); }, [](%1* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](%1* o) { o->endRemoveRows(); } )").arg(o.name, QString::number(o.columnCount - 1)); } if (o.type == ObjectType::Tree) { cpp << QString(R"(, [](const %1* o, option_quintptr id) { if (id.some) { int row = %2_row(o->m_d, id.value); emit o->newDataReady(o->createIndex(row, 0, id.value)); } else { emit o->newDataReady(QModelIndex()); } }, [](%1* o) { emit o->layoutAboutToBeChanged(); }, [](%1* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](%1* o, quintptr first, quintptr last) { quintptr frow = %2_row(o->m_d, first); quintptr lrow = %2_row(o->m_d, first); o->dataChanged(o->createIndex(frow, 0, first), o->createIndex(lrow, %3, last)); }, [](%1* o) { o->beginResetModel(); }, [](%1* o) { o->endResetModel(); }, [](%1* o, option_quintptr id, int first, int last) { if (id.some) { int row = %2_row(o->m_d, id.value); o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginInsertRows(QModelIndex(), first, last); } }, [](%1* o) { o->endInsertRows(); }, [](%1* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) { QModelIndex s; if (sourceParent.some) { int row = %2_row(o->m_d, sourceParent.value); s = o->createIndex(row, 0, sourceParent.value); } QModelIndex d; if (destinationParent.some) { int row = %2_row(o->m_d, destinationParent.value); d = o->createIndex(row, 0, destinationParent.value); } o->beginMoveRows(s, first, last, d, destination); }, [](%1* o) { o->endMoveRows(); }, [](%1* o, option_quintptr id, int first, int last) { if (id.some) { int row = %2_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](%1* o) { o->endRemoveRows(); } )").arg(o.name, lcname, QString::number(o.columnCount - 1)); } } void writeFunctionCDecl(QTextStream& cpp, const Function& f, const QString& lcname, const Object& o) { const QString lc(snakeCase(f.name)); cpp << " "; if (f.type.isComplex()) { cpp << "void"; } else { cpp << f.type.name; } const QString name = QString("%1_%2").arg(lcname, lc); cpp << QString(" %1(%3%2::Private*").arg(name, o.name, f.mut ? "" : "const "); // write all the input arguments, for QString and QByteArray, write // pointers to their content and the length for (auto a = f.args.begin(); a < f.args.end(); a++) { if (a->type.name == "QString") { cpp << ", const ushort*, int"; } else if (a->type.name == "QByteArray") { cpp << ", const char*, int"; } else { cpp << ", " << a->type.name; } } // If the return type is QString or QByteArray, append a pointer to the // variable that will be set to the argument list. Also add a setter // function. if (f.type.name == "QString") { cpp << ", QString*, qstring_set"; } else if (f.type.name == "QByteArray") { cpp << ", QByteArray*, qbytearray_set"; } cpp << ");\n"; } void writeObjectCDecl(QTextStream& cpp, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); cpp << QString(" %1::Private* %2_new(").arg(o.name, lcname); constructorArgsDecl(cpp, o, conf); cpp << ");" << endl; cpp << QString(" void %2_free(%1::Private*);").arg(o.name, lcname) << endl; for (const Property& p: o.properties) { const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name)); if (p.type.type == BindingType::Object) { cpp << QString(" %3::Private* %2_get(const %1::Private*);") .arg(o.name, base, p.type.name) << endl; } else if (p.type.isComplex()) { cpp << QString(" void %2_get(const %1::Private*, %3);") .arg(o.name, base, cGetType(p.type)) << endl; } else if (p.optional) { cpp << QString(" option_%3 %2_get(const %1::Private*);") .arg(o.name, base, p.type.name) << endl; } else { cpp << QString(" %3 %2_get(const %1::Private*);") .arg(o.name, base, p.type.name) << endl; } if (p.write) { QString t = p.type.cSetType; if (t == "qstring_t") { t = "const ushort *str, int len"; } else if (t == "qbytearray_t") { t = "const char* bytes, int len"; } cpp << QString(" void %2_set(%1::Private*, %3);") .arg(o.name, base, t) << endl; if (p.optional) { cpp << QString(" void %2_set_none(%1::Private*);") .arg(o.name, base) << endl; } } } for (const Function& f: o.functions) { writeFunctionCDecl(cpp, f, lcname, o); } } void initializeMembersEmpty(QTextStream& cpp, const Object& o, const Configuration& conf) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { initializeMembersEmpty(cpp, conf.findObject(p.type.name), conf); cpp << QString(" %1m_%2(new %3(false, this)),\n") .arg(p.name, p.type.name); } } } void initializeMembersZero(QTextStream& cpp, const Object& o) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(" m_%1(new %2(false, this)),\n") .arg(p.name, p.type.name); } } } void initializeMembers(QTextStream& cpp, const QString& prefix, const Object& o, const Configuration& conf) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(" %1m_%2->m_d = %3_%4_get(%1m_d);\n") .arg(prefix, p.name, snakeCase(o.name), snakeCase(p.name)); initializeMembers(cpp, "m_" + p.name + "->", conf.findObject(p.type.name), conf); } } } void connect(QTextStream& cpp, const QString& d, const Object& o, const Configuration& conf) { for (auto p: o.properties) { if (p.type.type == BindingType::Object) { connect(cpp, d + "->m_" + p.name, conf.findObject(p.type.name), conf); } } if (o.type != ObjectType::Object) { cpp << QString(R"( connect(%2, &%1::newDataReady, %2, [this](const QModelIndex& i) { %2->fetchMore(i); }, Qt::QueuedConnection); )").arg(o.name, d); } } void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); cpp << QString("%1::%1(bool /*owned*/, QObject *parent):\n %2(parent),") .arg(o.name, baseType(o)) << endl; for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(" m_%1(new %2(false, this)),\n") .arg(p.name, p.type.name); } } cpp << " m_d(0),\n m_ownsPrivate(false)\n{\n"; if (o.type != ObjectType::Object) { cpp << " initHeaderData();\n"; } cpp << QString("}\n\n%1::%1(QObject *parent):\n %2(parent),") .arg(o.name, baseType(o)) << endl; initializeMembersZero(cpp, o); cpp << QString(" m_d(%1_new(this").arg(lcname); constructorArgs(cpp, "", o, conf); cpp << ")),\n m_ownsPrivate(true)\n{\n"; initializeMembers(cpp, "", o, conf); connect(cpp, "this", o, conf); if (o.type != ObjectType::Object) { cpp << " initHeaderData();\n"; } cpp << QString(R"(} %1::~%1() { if (m_ownsPrivate) { %2_free(m_d); } } )").arg(o.name, lcname); if (o.type != ObjectType::Object) { cpp << QString("void %1::initHeaderData() {\n").arg(o.name); for (int col = 0; col < o.columnCount; ++col) { for (auto ip: o.itemProperties) { auto roles = ip.roles.value(col); if (roles.contains(Qt::DisplayRole)) { cpp << QString(" m_headerData.insert(qMakePair(%1, Qt::DisplayRole), QVariant(\"%2\"));\n").arg(QString::number(col), ip.name); } } } cpp << "}\n"; } for (const Property& p: o.properties) { const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name)); if (p.type.type == BindingType::Object) { cpp << QString(R"(const %3* %1::%2() const { return m_%2; } %3* %1::%2() { return m_%2; } )").arg(o.name, p.name, p.type.name); } else if (p.type.isComplex()) { cpp << QString("%3 %1::%2() const\n{\n").arg(o.name, p.name, p.type.name); cpp << " " << p.type.name << " v;\n"; cpp << " " << base << "_get(m_d, &v, set_" << p.type.name.toLower() << ");\n"; cpp << " return v;\n}\n"; } else if (p.optional) { cpp << QString("QVariant %1::%2() const\n{\n").arg(o.name, p.name); cpp << " QVariant v;\n"; cpp << QString(" auto r = %1_get(m_d);\n").arg(base); cpp << " if (r.some) {\n"; cpp << " v.setValue(r.value);\n"; cpp << " }\n"; cpp << " return r;\n"; cpp << "}\n"; } else { cpp << QString("%3 %1::%2() const\n{\n").arg(o.name, p.name, p.type.name); cpp << QString(" return %1_get(m_d);\n}\n").arg(base); } if (p.write) { auto t = p.type.cppSetType; if (p.optional && !p.type.isComplex()) { t = "const QVariant&"; } cpp << "void " << o.name << "::set" << upperInitial(p.name) << "(" << t << " v) {" << endl; if (p.optional) { if (p.type.isComplex()) { cpp << " if (v.isNull()) {" << endl; } else { cpp << QString(" if (v.isNull() || !v.canConvert<%1>()) {").arg(p.type.name) << endl; } cpp << QString(" %1_set_none(m_d);").arg(base) << endl; cpp << QString(" } else {") << endl; if (p.type.name == "QString") { cpp << QString(" %1_set(m_d, reinterpret_cast(v.data()), v.size());").arg(base) << endl; } else if (p.type.name == "QByteArray") { cpp << QString(" %1_set(m_d, v.data(), v.size());").arg(base) << endl; } else if (p.optional) { cpp << QString(" %1_set(m_d, v.value<%2>());").arg(base, p.type.name) << endl; } else { cpp << QString(" %1_set(m_d, v);").arg(base) << endl; } cpp << QString(" }") << endl; } else if (p.type.name == "QString") { cpp << QString(" %1_set(m_d, reinterpret_cast(v.data()), v.size());").arg(base) << endl; } else if (p.type.name == "QByteArray") { cpp << QString(" %1_set(m_d, v.data(), v.size());").arg(base) << endl; } else { cpp << QString(" %1_set(m_d, v);").arg(base) << endl; } cpp << "}" << endl; } } for (const Function& f: o.functions) { const QString base = QString("%1_%2") .arg(lcname, snakeCase(f.name)); cpp << QString("%1 %2::%3(").arg(f.type.name, o.name, f.name); for (auto a = f.args.begin(); a < f.args.end(); a++) { cpp << QString("%1 %2%3").arg(a->type.cppSetType, a->name, a + 1 < f.args.end() ? ", " : ""); } cpp << QString(")%1\n{\n").arg(f.mut ? "" : " const"); QString argList; for (auto a = f.args.begin(); a < f.args.end(); a++) { if (a->type.name == "QString") { argList.append(QString(", %1.utf16(), %1.size()").arg(a->name)); } else if (a->type.name == "QByteArray") { argList.append(QString(", %1.data(), %1.size()").arg(a->name)); } else { argList.append(QString(", %1").arg(a->name)); } } if (f.type.name == "QString") { cpp << QString(" %1 s;").arg(f.type.name) << endl; cpp << QString(" %1(m_d%2, &s, set_qstring);") .arg(base, argList) << endl; cpp << " return s;" << endl; } else if (f.type.name == "QByteArray") { cpp << QString(" %1 s;").arg(f.type.name) << endl; cpp << QString(" %1(m_d%2, &s, set_qbytearray);") .arg(base, argList) << endl; cpp << " return s;" << endl; } else { cpp << QString(" return %1(m_d%2);") .arg(base, argList) << endl; } cpp << "}" << endl; } } void writeHeader(const Configuration& conf) { DifferentFileWriter w(conf.hFile.absoluteFilePath()); QTextStream h(&w.buffer); const QString guard(conf.hFile.fileName().replace('.', '_').toUpper()); h << QString(R"(/* generated by rust_qt_binding_generator */ #ifndef %1 #define %1 #include #include )").arg(guard); for (auto object: conf.objects) { h << "class " << object.name << ";\n"; } for (auto object: conf.objects) { writeHeaderObject(h, object, conf); } h << QString("#endif // %1\n").arg(guard); } void writeCpp(const Configuration& conf) { DifferentFileWriter w(conf.cppFile.absoluteFilePath()); QTextStream cpp(&w.buffer); cpp << QString(R"(/* generated by rust_qt_binding_generator */ #include "%1" namespace { )").arg(conf.hFile.fileName()); for (auto option: conf.optionalTypes()) { if (option != "QString" && option != "QByteArray") { cpp << QString(R"( struct option_%1 { public: %1 value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_%1 must be a POD type."); )").arg(option); } } if (conf.types().contains("QString")) { cpp << R"( typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes); void set_qstring(QString* val, const char* utf8, int nbytes) { *val = QString::fromUtf8(utf8, nbytes); } )"; } if (conf.types().contains("QByteArray")) { cpp << R"( typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { if (v->isNull() && nbytes == 0) { *v = QByteArray(bytes, nbytes); } else { v->truncate(0); v->append(bytes, nbytes); } } )"; } if (conf.hasListOrTree()) { cpp << R"( struct qmodelindex_t { int row; quintptr id; }; inline QVariant cleanNullQVariant(const QVariant& v) { return (v.isNull()) ?QVariant() :v; } )"; } for (auto o: conf.objects) { for (auto p: o.properties) { if (p.type.type == BindingType::Object) { continue; } cpp << " inline void " << changedF(o, p) << "(" << o.name << "* o)\n"; cpp << " {\n emit o->" << p.name << "Changed();\n }\n"; } } cpp << "}\n"; for (auto object: conf.objects) { if (object.type != ObjectType::Object) { writeCppModel(cpp, object); } cpp << "extern \"C\" {\n"; writeObjectCDecl(cpp, object, conf); cpp << "};\n" << endl; } for (auto object: conf.objects) { writeCppObject(cpp, object, conf); } } diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index 758e5fd..96ce2ff 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -1,526 +1,528 @@ /* generated by rust_qt_binding_generator */ #include "test_list_rust.h" namespace { struct option_quintptr { public: quintptr value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_quintptr must be a POD type."); typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes); void set_qstring(QString* val, const char* utf8, int nbytes) { *val = QString::fromUtf8(utf8, nbytes); } struct qmodelindex_t { int row; quintptr id; }; inline QVariant cleanNullQVariant(const QVariant& v) { return (v.isNull()) ?QVariant() :v; } } extern "C" { quint8 no_role_data_user_age(const NoRole::Private*, int); bool no_role_set_data_user_age(NoRole::Private*, int, quint8); void no_role_data_user_name(const NoRole::Private*, int, QString*, qstring_set); bool no_role_set_data_user_name(NoRole::Private*, int, const ushort* s, int len); void no_role_sort(NoRole::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int no_role_row_count(const NoRole::Private*); bool no_role_insert_rows(NoRole::Private*, int, int); bool no_role_remove_rows(NoRole::Private*, int, int); bool no_role_can_fetch_more(const NoRole::Private*); void no_role_fetch_more(NoRole::Private*); } int NoRole::columnCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : 1; } bool NoRole::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int NoRole::rowCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : no_role_row_count(m_d); } bool NoRole::insertRows(int row, int count, const QModelIndex &) { return no_role_insert_rows(m_d, row, count); } bool NoRole::removeRows(int row, int count, const QModelIndex &) { return no_role_remove_rows(m_d, row, count); } QModelIndex NoRole::index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); } QModelIndex NoRole::parent(const QModelIndex &) const { return QModelIndex(); } bool NoRole::canFetchMore(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : no_role_can_fetch_more(m_d); } void NoRole::fetchMore(const QModelIndex &parent) { if (!parent.isValid()) { no_role_fetch_more(m_d); } } void NoRole::updatePersistentIndexes() {} void NoRole::sort(int column, Qt::SortOrder order) { no_role_sort(m_d, column, order); } Qt::ItemFlags NoRole::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); if (i.column() == 0) { flags |= Qt::ItemIsEditable; } return flags; } quint8 NoRole::userAge(int row) const { return no_role_data_user_age(m_d, row); } bool NoRole::setUserAge(int row, quint8 value) { bool set = false; set = no_role_set_data_user_age(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QString NoRole::userName(int row) const { QString s; no_role_data_user_name(m_d, row, &s, set_qstring); return s; } bool NoRole::setUserName(int row, const QString& value) { bool set = false; set = no_role_set_data_user_name(m_d, row, value.utf16(), value.length()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant NoRole::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::UserRole + 0: return QVariant::fromValue(userAge(index.row())); case Qt::UserRole + 1: return QVariant::fromValue(userName(index.row())); } + break; } return QVariant(); } int NoRole::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash NoRole::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "userAge"); names.insert(Qt::UserRole + 1, "userName"); return names; } QVariant NoRole::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool NoRole::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } bool NoRole::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.column() == 0) { if (role == Qt::UserRole + 0) { if (value.canConvert(qMetaTypeId())) { return setUserAge(index.row(), value.value()); } } if (role == Qt::UserRole + 1) { if (value.canConvert(qMetaTypeId())) { return setUserName(index.row(), value.value()); } } } return false; } extern "C" { NoRole::Private* no_role_new(NoRole*, void (*)(const NoRole*), void (*)(NoRole*), void (*)(NoRole*), void (*)(NoRole*, quintptr, quintptr), void (*)(NoRole*), void (*)(NoRole*), void (*)(NoRole*, int, int), void (*)(NoRole*), void (*)(NoRole*, int, int, int), void (*)(NoRole*), void (*)(NoRole*, int, int), void (*)(NoRole*)); void no_role_free(NoRole::Private*); }; extern "C" { void persons_data_user_name(const Persons::Private*, int, QString*, qstring_set); bool persons_set_data_user_name(Persons::Private*, int, const ushort* s, int len); void persons_sort(Persons::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int persons_row_count(const Persons::Private*); bool persons_insert_rows(Persons::Private*, int, int); bool persons_remove_rows(Persons::Private*, int, int); 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 { return (parent.isValid()) ? 0 : persons_row_count(m_d); } bool Persons::insertRows(int row, int count, const QModelIndex &) { return persons_insert_rows(m_d, row, count); } bool Persons::removeRows(int row, int count, const QModelIndex &) { return persons_remove_rows(m_d, row, count); } QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); } QModelIndex Persons::parent(const QModelIndex &) const { return QModelIndex(); } bool Persons::canFetchMore(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : persons_can_fetch_more(m_d); } void Persons::fetchMore(const QModelIndex &parent) { if (!parent.isValid()) { persons_fetch_more(m_d); } } void Persons::updatePersistentIndexes() {} void Persons::sort(int column, Qt::SortOrder order) { persons_sort(m_d, column, order); } Qt::ItemFlags Persons::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); if (i.column() == 0) { flags |= Qt::ItemIsEditable; } return flags; } QString Persons::userName(int row) const { QString s; persons_data_user_name(m_d, row, &s, set_qstring); return s; } bool Persons::setUserName(int row, const QString& value) { bool set = false; set = persons_set_data_user_name(m_d, row, value.utf16(), value.length()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant Persons::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 0: return QVariant::fromValue(userName(index.row())); } + break; } return QVariant(); } int Persons::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash Persons::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "userName"); return names; } QVariant Persons::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } bool Persons::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.column() == 0) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { if (value.canConvert(qMetaTypeId())) { return setUserName(index.row(), value.value()); } } } return false; } extern "C" { Persons::Private* persons_new(Persons*, void (*)(const Persons*), void (*)(Persons*), void (*)(Persons*), void (*)(Persons*, quintptr, quintptr), void (*)(Persons*), void (*)(Persons*), void (*)(Persons*, int, int), void (*)(Persons*), void (*)(Persons*, int, int, int), void (*)(Persons*), void (*)(Persons*, int, int), void (*)(Persons*)); void persons_free(Persons::Private*); }; NoRole::NoRole(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } NoRole::NoRole(QObject *parent): QAbstractItemModel(parent), m_d(no_role_new(this, [](const NoRole* o) { emit o->newDataReady(QModelIndex()); }, [](NoRole* o) { emit o->layoutAboutToBeChanged(); }, [](NoRole* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](NoRole* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 0, last)); }, [](NoRole* o) { o->beginResetModel(); }, [](NoRole* o) { o->endResetModel(); }, [](NoRole* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](NoRole* o) { o->endInsertRows(); }, [](NoRole* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](NoRole* o) { o->endMoveRows(); }, [](NoRole* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](NoRole* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &NoRole::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } NoRole::~NoRole() { if (m_ownsPrivate) { no_role_free(m_d); } } void NoRole::initHeaderData() { } Persons::Persons(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } Persons::Persons(QObject *parent): QAbstractItemModel(parent), m_d(persons_new(this, [](const Persons* o) { emit o->newDataReady(QModelIndex()); }, [](Persons* o) { emit o->layoutAboutToBeChanged(); }, [](Persons* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](Persons* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 0, last)); }, [](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, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](Persons* o) { o->endMoveRows(); }, [](Persons* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](Persons* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } Persons::~Persons() { if (m_ownsPrivate) { persons_free(m_d); } } void Persons::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName")); } diff --git a/tests/test_list_types_rust.cpp b/tests/test_list_types_rust.cpp index d202492..5c53df4 100644 --- a/tests/test_list_types_rust.cpp +++ b/tests/test_list_types_rust.cpp @@ -1,704 +1,705 @@ /* generated by rust_qt_binding_generator */ #include "test_list_types_rust.h" namespace { struct option_bool { public: bool value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_bool must be a POD type."); struct option_quintptr { public: quintptr value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_quintptr must be a POD type."); typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes); void set_qstring(QString* val, const char* utf8, int nbytes) { *val = QString::fromUtf8(utf8, nbytes); } typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { if (v->isNull() && nbytes == 0) { *v = QByteArray(bytes, nbytes); } else { v->truncate(0); v->append(bytes, nbytes); } } struct qmodelindex_t { int row; quintptr id; }; inline QVariant cleanNullQVariant(const QVariant& v) { return (v.isNull()) ?QVariant() :v; } } extern "C" { bool list_data_boolean(const List::Private*, int); bool list_set_data_boolean(List::Private*, int, bool); void list_data_bytearray(const List::Private*, int, QByteArray*, qbytearray_set); bool list_set_data_bytearray(List::Private*, int, const char* s, int len); float list_data_f32(const List::Private*, int); bool list_set_data_f32(List::Private*, int, float); double list_data_f64(const List::Private*, int); bool list_set_data_f64(List::Private*, int, double); qint16 list_data_i16(const List::Private*, int); bool list_set_data_i16(List::Private*, int, qint16); qint32 list_data_i32(const List::Private*, int); bool list_set_data_i32(List::Private*, int, qint32); qint64 list_data_i64(const List::Private*, int); bool list_set_data_i64(List::Private*, int, qint64); qint8 list_data_i8(const List::Private*, int); bool list_set_data_i8(List::Private*, int, qint8); option_bool list_data_optional_boolean(const List::Private*, int); bool list_set_data_optional_boolean(List::Private*, int, bool); bool list_set_data_optional_boolean_none(List::Private*, int); void list_data_optional_bytearray(const List::Private*, int, QByteArray*, qbytearray_set); bool list_set_data_optional_bytearray(List::Private*, int, const char* s, int len); bool list_set_data_optional_bytearray_none(List::Private*, int); void list_data_optional_string(const List::Private*, int, QString*, qstring_set); bool list_set_data_optional_string(List::Private*, int, const ushort* s, int len); bool list_set_data_optional_string_none(List::Private*, int); void list_data_string(const List::Private*, int, QString*, qstring_set); bool list_set_data_string(List::Private*, int, const ushort* s, int len); quint16 list_data_u16(const List::Private*, int); bool list_set_data_u16(List::Private*, int, quint16); quint32 list_data_u32(const List::Private*, int); bool list_set_data_u32(List::Private*, int, quint32); quint64 list_data_u64(const List::Private*, int); bool list_set_data_u64(List::Private*, int, quint64); quint8 list_data_u8(const List::Private*, int); bool list_set_data_u8(List::Private*, int, quint8); void list_sort(List::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int list_row_count(const List::Private*); bool list_insert_rows(List::Private*, int, int); bool list_remove_rows(List::Private*, int, int); bool list_can_fetch_more(const List::Private*); void list_fetch_more(List::Private*); } int List::columnCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : 1; } bool List::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int List::rowCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : list_row_count(m_d); } bool List::insertRows(int row, int count, const QModelIndex &) { return list_insert_rows(m_d, row, count); } bool List::removeRows(int row, int count, const QModelIndex &) { return list_remove_rows(m_d, row, count); } QModelIndex List::index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); } QModelIndex List::parent(const QModelIndex &) const { return QModelIndex(); } bool List::canFetchMore(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : list_can_fetch_more(m_d); } void List::fetchMore(const QModelIndex &parent) { if (!parent.isValid()) { list_fetch_more(m_d); } } void List::updatePersistentIndexes() {} void List::sort(int column, Qt::SortOrder order) { list_sort(m_d, column, order); } Qt::ItemFlags List::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); if (i.column() == 0) { flags |= Qt::ItemIsEditable; } return flags; } bool List::boolean(int row) const { return list_data_boolean(m_d, row); } bool List::setBoolean(int row, bool value) { bool set = false; set = list_set_data_boolean(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QByteArray List::bytearray(int row) const { QByteArray b; list_data_bytearray(m_d, row, &b, set_qbytearray); return b; } bool List::setBytearray(int row, const QByteArray& value) { bool set = false; set = list_set_data_bytearray(m_d, row, value.data(), value.length()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } float List::f32(int row) const { return list_data_f32(m_d, row); } bool List::setF32(int row, float value) { bool set = false; set = list_set_data_f32(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } double List::f64(int row) const { return list_data_f64(m_d, row); } bool List::setF64(int row, double value) { bool set = false; set = list_set_data_f64(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } qint16 List::i16(int row) const { return list_data_i16(m_d, row); } bool List::setI16(int row, qint16 value) { bool set = false; set = list_set_data_i16(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } qint32 List::i32(int row) const { return list_data_i32(m_d, row); } bool List::setI32(int row, qint32 value) { bool set = false; set = list_set_data_i32(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } qint64 List::i64(int row) const { return list_data_i64(m_d, row); } bool List::setI64(int row, qint64 value) { bool set = false; set = list_set_data_i64(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } qint8 List::i8(int row) const { return list_data_i8(m_d, row); } bool List::setI8(int row, qint8 value) { bool set = false; set = list_set_data_i8(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant List::optionalBoolean(int row) const { QVariant v; v = list_data_optional_boolean(m_d, row); return v; } bool List::setOptionalBoolean(int row, const QVariant& value) { bool set = false; if (value.isNull() || !value.isValid()) { set = list_set_data_optional_boolean_none(m_d, row); } else { if (!value.canConvert(qMetaTypeId())) { return false; } set = list_set_data_optional_boolean(m_d, row, value.value()); } if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QByteArray List::optionalBytearray(int row) const { QByteArray b; list_data_optional_bytearray(m_d, row, &b, set_qbytearray); return b; } bool List::setOptionalBytearray(int row, const QByteArray& value) { bool set = false; if (value.isNull()) { set = list_set_data_optional_bytearray_none(m_d, row); } else { set = list_set_data_optional_bytearray(m_d, row, value.data(), value.length()); } if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QString List::optionalString(int row) const { QString s; list_data_optional_string(m_d, row, &s, set_qstring); return s; } bool List::setOptionalString(int row, const QString& value) { bool set = false; if (value.isNull()) { set = list_set_data_optional_string_none(m_d, row); } else { set = list_set_data_optional_string(m_d, row, value.utf16(), value.length()); } if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QString List::string(int row) const { QString s; list_data_string(m_d, row, &s, set_qstring); return s; } bool List::setString(int row, const QString& value) { bool set = false; set = list_set_data_string(m_d, row, value.utf16(), value.length()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } quint16 List::u16(int row) const { return list_data_u16(m_d, row); } bool List::setU16(int row, quint16 value) { bool set = false; set = list_set_data_u16(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } quint32 List::u32(int row) const { return list_data_u32(m_d, row); } bool List::setU32(int row, quint32 value) { bool set = false; set = list_set_data_u32(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } quint64 List::u64(int row) const { return list_data_u64(m_d, row); } bool List::setU64(int row, quint64 value) { bool set = false; set = list_set_data_u64(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } quint8 List::u8(int row) const { return list_data_u8(m_d, row); } bool List::setU8(int row, quint8 value) { bool set = false; set = list_set_data_u8(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant List::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::UserRole + 0: return QVariant::fromValue(boolean(index.row())); case Qt::UserRole + 1: return QVariant::fromValue(bytearray(index.row())); case Qt::UserRole + 2: return QVariant::fromValue(f32(index.row())); case Qt::UserRole + 3: return QVariant::fromValue(f64(index.row())); case Qt::UserRole + 4: return QVariant::fromValue(i16(index.row())); case Qt::UserRole + 5: return QVariant::fromValue(i32(index.row())); case Qt::UserRole + 6: return QVariant::fromValue(i64(index.row())); case Qt::UserRole + 7: return QVariant::fromValue(i8(index.row())); case Qt::UserRole + 8: return optionalBoolean(index.row()); case Qt::UserRole + 9: return cleanNullQVariant(QVariant::fromValue(optionalBytearray(index.row()))); case Qt::UserRole + 10: return cleanNullQVariant(QVariant::fromValue(optionalString(index.row()))); case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 11: return QVariant::fromValue(string(index.row())); case Qt::UserRole + 12: return QVariant::fromValue(u16(index.row())); case Qt::UserRole + 13: return QVariant::fromValue(u32(index.row())); case Qt::UserRole + 14: return QVariant::fromValue(u64(index.row())); case Qt::UserRole + 15: return QVariant::fromValue(u8(index.row())); } + break; } return QVariant(); } int List::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash List::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "boolean"); names.insert(Qt::UserRole + 1, "bytearray"); names.insert(Qt::UserRole + 2, "f32"); names.insert(Qt::UserRole + 3, "f64"); names.insert(Qt::UserRole + 4, "i16"); names.insert(Qt::UserRole + 5, "i32"); names.insert(Qt::UserRole + 6, "i64"); names.insert(Qt::UserRole + 7, "i8"); names.insert(Qt::UserRole + 8, "optionalBoolean"); names.insert(Qt::UserRole + 9, "optionalBytearray"); names.insert(Qt::UserRole + 10, "optionalString"); names.insert(Qt::UserRole + 11, "string"); names.insert(Qt::UserRole + 12, "u16"); names.insert(Qt::UserRole + 13, "u32"); names.insert(Qt::UserRole + 14, "u64"); names.insert(Qt::UserRole + 15, "u8"); return names; } QVariant List::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool List::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } bool List::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.column() == 0) { if (role == Qt::UserRole + 0) { if (value.canConvert(qMetaTypeId())) { return setBoolean(index.row(), value.value()); } } if (role == Qt::UserRole + 1) { if (value.canConvert(qMetaTypeId())) { return setBytearray(index.row(), value.value()); } } if (role == Qt::UserRole + 2) { if (value.canConvert(qMetaTypeId())) { return setF32(index.row(), value.value()); } } if (role == Qt::UserRole + 3) { if (value.canConvert(qMetaTypeId())) { return setF64(index.row(), value.value()); } } if (role == Qt::UserRole + 4) { if (value.canConvert(qMetaTypeId())) { return setI16(index.row(), value.value()); } } if (role == Qt::UserRole + 5) { if (value.canConvert(qMetaTypeId())) { return setI32(index.row(), value.value()); } } if (role == Qt::UserRole + 6) { if (value.canConvert(qMetaTypeId())) { return setI64(index.row(), value.value()); } } if (role == Qt::UserRole + 7) { if (value.canConvert(qMetaTypeId())) { return setI8(index.row(), value.value()); } } if (role == Qt::UserRole + 8) { return setOptionalBoolean(index.row(), value); } if (role == Qt::UserRole + 9) { if (!value.isValid() || value.isNull() ||value.canConvert(qMetaTypeId())) { return setOptionalBytearray(index.row(), value.value()); } } if (role == Qt::UserRole + 10) { if (!value.isValid() || value.isNull() ||value.canConvert(qMetaTypeId())) { return setOptionalString(index.row(), value.value()); } } if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 11) { if (value.canConvert(qMetaTypeId())) { return setString(index.row(), value.value()); } } if (role == Qt::UserRole + 12) { if (value.canConvert(qMetaTypeId())) { return setU16(index.row(), value.value()); } } if (role == Qt::UserRole + 13) { if (value.canConvert(qMetaTypeId())) { return setU32(index.row(), value.value()); } } if (role == Qt::UserRole + 14) { if (value.canConvert(qMetaTypeId())) { return setU64(index.row(), value.value()); } } if (role == Qt::UserRole + 15) { if (value.canConvert(qMetaTypeId())) { return setU8(index.row(), value.value()); } } } return false; } extern "C" { List::Private* list_new(List*, void (*)(const List*), void (*)(List*), void (*)(List*), void (*)(List*, quintptr, quintptr), void (*)(List*), void (*)(List*), void (*)(List*, int, int), void (*)(List*), void (*)(List*, int, int, int), void (*)(List*), void (*)(List*, int, int), void (*)(List*)); void list_free(List::Private*); }; List::List(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } List::List(QObject *parent): QAbstractItemModel(parent), m_d(list_new(this, [](const List* o) { emit o->newDataReady(QModelIndex()); }, [](List* o) { emit o->layoutAboutToBeChanged(); }, [](List* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](List* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), o->createIndex(last, 0, last)); }, [](List* o) { o->beginResetModel(); }, [](List* o) { o->endResetModel(); }, [](List* o, int first, int last) { o->beginInsertRows(QModelIndex(), first, last); }, [](List* o) { o->endInsertRows(); }, [](List* o, int first, int last, int destination) { o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); }, [](List* o) { o->endMoveRows(); }, [](List* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, [](List* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &List::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } List::~List() { if (m_ownsPrivate) { list_free(m_d); } } void List::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("string")); } diff --git a/tests/test_tree_rust.cpp b/tests/test_tree_rust.cpp index 4d747a5..375aa2d 100644 --- a/tests/test_tree_rust.cpp +++ b/tests/test_tree_rust.cpp @@ -1,340 +1,341 @@ /* generated by rust_qt_binding_generator */ #include "test_tree_rust.h" namespace { struct option_quintptr { public: quintptr value; bool some; operator QVariant() const { if (some) { return QVariant::fromValue(value); } return QVariant(); } }; static_assert(std::is_pod::value, "option_quintptr must be a POD type."); typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes); void set_qstring(QString* val, const char* utf8, int nbytes) { *val = QString::fromUtf8(utf8, nbytes); } struct qmodelindex_t { int row; quintptr id; }; inline QVariant cleanNullQVariant(const QVariant& v) { return (v.isNull()) ?QVariant() :v; } } extern "C" { void persons_data_user_name(const Persons::Private*, quintptr, QString*, qstring_set); bool persons_set_data_user_name(Persons::Private*, quintptr, const ushort* s, int len); void persons_sort(Persons::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int persons_row_count(const Persons::Private*, option_quintptr); bool persons_can_fetch_more(const Persons::Private*, option_quintptr); void persons_fetch_more(Persons::Private*, option_quintptr); quintptr persons_index(const Persons::Private*, option_quintptr, int); qmodelindex_t persons_parent(const Persons::Private*, quintptr); int persons_row(const Persons::Private*, quintptr); option_quintptr persons_check_row(const Persons::Private*, quintptr, int); } int Persons::columnCount(const QModelIndex &) const { return 1; } bool Persons::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int Persons::rowCount(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return 0; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return persons_row_count(m_d, rust_parent); } bool Persons::insertRows(int, int, const QModelIndex &) { return false; // not supported yet } bool Persons::removeRows(int, int, const QModelIndex &) { return false; // not supported yet } QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const { if (row < 0 || column < 0 || column >= 1) { return QModelIndex(); } if (parent.isValid() && parent.column() != 0) { return QModelIndex(); } if (row >= rowCount(parent)) { return QModelIndex(); } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; const quintptr id = persons_index(m_d, rust_parent, row); return createIndex(row, column, id); } QModelIndex Persons::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } const qmodelindex_t parent = persons_parent(m_d, index.internalId()); return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); } bool Persons::canFetchMore(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) { return false; } const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; return persons_can_fetch_more(m_d, rust_parent); } void Persons::fetchMore(const QModelIndex &parent) { const option_quintptr rust_parent = { parent.internalId(), parent.isValid() }; persons_fetch_more(m_d, rust_parent); } void Persons::updatePersistentIndexes() { const auto from = persistentIndexList(); auto to = from; auto len = to.size(); for (int i = 0; i < len; ++i) { auto index = to.at(i); auto row = persons_check_row(m_d, index.internalId(), index.row()); if (row.some) { to[i] = createIndex(row.value, index.column(), index.internalId()); } else { to[i] = QModelIndex(); } } changePersistentIndexList(from, to); } void Persons::sort(int column, Qt::SortOrder order) { persons_sort(m_d, column, order); } Qt::ItemFlags Persons::flags(const QModelIndex &i) const { auto flags = QAbstractItemModel::flags(i); if (i.column() == 0) { flags |= Qt::ItemIsEditable; } return flags; } QString Persons::userName(const QModelIndex& index) const { QString s; persons_data_user_name(m_d, index.internalId(), &s, set_qstring); return s; } bool Persons::setUserName(const QModelIndex& index, const QString& value) { bool set = false; set = persons_set_data_user_name(m_d, index.internalId(), value.utf16(), value.length()); if (set) { emit dataChanged(index, index); } return set; } QVariant Persons::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 0: return QVariant::fromValue(userName(index)); } + break; } return QVariant(); } int Persons::role(const char* name) const { auto names = roleNames(); auto i = names.constBegin(); while (i != names.constEnd()) { if (i.value() == name) { return i.key(); } ++i; } return -1; } QHash Persons::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "userName"); return names; } QVariant Persons::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); } bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (orientation != Qt::Horizontal) { return false; } m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); return true; } bool Persons::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.column() == 0) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { if (value.canConvert(qMetaTypeId())) { return setUserName(index, value.value()); } } } return false; } extern "C" { Persons::Private* persons_new(Persons*, void (*)(const Persons*, option_quintptr), void (*)(Persons*), void (*)(Persons*), void (*)(Persons*, quintptr, quintptr), void (*)(Persons*), void (*)(Persons*), void (*)(Persons*, option_quintptr, int, int), void (*)(Persons*), void (*)(Persons*, option_quintptr, int, int, option_quintptr, int), void (*)(Persons*), void (*)(Persons*, option_quintptr, int, int), void (*)(Persons*)); void persons_free(Persons::Private*); }; Persons::Persons(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } Persons::Persons(QObject *parent): QAbstractItemModel(parent), m_d(persons_new(this, [](const Persons* o, option_quintptr id) { if (id.some) { int row = persons_row(o->m_d, id.value); emit o->newDataReady(o->createIndex(row, 0, id.value)); } else { emit o->newDataReady(QModelIndex()); } }, [](Persons* o) { emit o->layoutAboutToBeChanged(); }, [](Persons* o) { o->updatePersistentIndexes(); emit o->layoutChanged(); }, [](Persons* o, quintptr first, quintptr last) { quintptr frow = persons_row(o->m_d, first); quintptr lrow = persons_row(o->m_d, first); o->dataChanged(o->createIndex(frow, 0, first), o->createIndex(lrow, 0, last)); }, [](Persons* o) { o->beginResetModel(); }, [](Persons* o) { o->endResetModel(); }, [](Persons* o, option_quintptr id, int first, int last) { if (id.some) { int row = persons_row(o->m_d, id.value); o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginInsertRows(QModelIndex(), first, last); } }, [](Persons* o) { o->endInsertRows(); }, [](Persons* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) { QModelIndex s; if (sourceParent.some) { int row = persons_row(o->m_d, sourceParent.value); s = o->createIndex(row, 0, sourceParent.value); } QModelIndex d; if (destinationParent.some) { int row = persons_row(o->m_d, destinationParent.value); d = o->createIndex(row, 0, destinationParent.value); } o->beginMoveRows(s, first, last, d, destination); }, [](Persons* o) { o->endMoveRows(); }, [](Persons* o, option_quintptr id, int first, int last) { if (id.some) { int row = persons_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](Persons* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } Persons::~Persons() { if (m_ownsPrivate) { persons_free(m_d); } } void Persons::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName")); }