diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 2ec8098..460080e 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -1,1767 +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(); + Q_EMIT o->inputChanged(); } inline void fibonacciResultChanged(Fibonacci* o) { - emit o->resultChanged(); + Q_EMIT o->resultChanged(); } inline void fileSystemTreePathChanged(FileSystemTree* o) { - emit o->pathChanged(); + Q_EMIT o->pathChanged(); } inline void processesActiveChanged(Processes* o) { - emit o->activeChanged(); + Q_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); + Q_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); + Q_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); + Q_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(nullptr), 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()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](FibonacciList* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](FibonacciList* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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)); + Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value)); } else { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); } }, [](FileSystemTree* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](FileSystemTree* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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)); + Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value)); } else { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); } }, [](Processes* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](Processes* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](TimeSeries* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](TimeSeries* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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(nullptr), 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(nullptr), m_ownsPrivate(false) { initHeaderData(); } FibonacciList::FibonacciList(QObject *parent): QAbstractItemModel(parent), m_d(fibonacci_list_new(this, [](const FibonacciList* o) { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](FibonacciList* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](FibonacciList* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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(nullptr), 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)); + Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value)); } else { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); } }, [](FileSystemTree* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](FileSystemTree* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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(nullptr), 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)); + Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value)); } else { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); } }, [](Processes* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](Processes* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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(nullptr), m_ownsPrivate(false) { initHeaderData(); } TimeSeries::TimeSeries(QObject *parent): QAbstractItemModel(parent), m_d(time_series_new(this, [](const TimeSeries* o) { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](TimeSeries* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](TimeSeries* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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/demo/src/Bindings.h b/demo/src/Bindings.h index c536e02..0db77ed 100644 --- a/demo/src/Bindings.h +++ b/demo/src/Bindings.h @@ -1,271 +1,271 @@ /* generated by rust_qt_binding_generator */ #ifndef BINDINGS_H #define BINDINGS_H #include #include class Demo; class Fibonacci; class FibonacciList; class FileSystemTree; class Processes; class TimeSeries; class Demo : public QObject { Q_OBJECT public: class Private; private: Fibonacci* const m_fibonacci; FibonacciList* const m_fibonacciList; FileSystemTree* const m_fileSystemTree; Processes* const m_processes; TimeSeries* const m_timeSeries; Private * m_d; bool m_ownsPrivate; Q_PROPERTY(Fibonacci* fibonacci READ fibonacci NOTIFY fibonacciChanged FINAL) Q_PROPERTY(FibonacciList* fibonacciList READ fibonacciList NOTIFY fibonacciListChanged FINAL) Q_PROPERTY(FileSystemTree* fileSystemTree READ fileSystemTree NOTIFY fileSystemTreeChanged FINAL) Q_PROPERTY(Processes* processes READ processes NOTIFY processesChanged FINAL) Q_PROPERTY(TimeSeries* timeSeries READ timeSeries NOTIFY timeSeriesChanged FINAL) explicit Demo(bool owned, QObject *parent); public: explicit Demo(QObject *parent = nullptr); ~Demo(); const Fibonacci* fibonacci() const; Fibonacci* fibonacci(); const FibonacciList* fibonacciList() const; FibonacciList* fibonacciList(); const FileSystemTree* fileSystemTree() const; FileSystemTree* fileSystemTree(); const Processes* processes() const; Processes* processes(); const TimeSeries* timeSeries() const; TimeSeries* timeSeries(); -signals: +Q_SIGNALS: void fibonacciChanged(); void fibonacciListChanged(); void fileSystemTreeChanged(); void processesChanged(); void timeSeriesChanged(); }; class Fibonacci : public QObject { Q_OBJECT friend class Demo; public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(quint32 input READ input WRITE setInput NOTIFY inputChanged FINAL) Q_PROPERTY(quint64 result READ result NOTIFY resultChanged FINAL) explicit Fibonacci(bool owned, QObject *parent); public: explicit Fibonacci(QObject *parent = nullptr); ~Fibonacci(); quint32 input() const; void setInput(quint32 v); quint64 result() const; -signals: +Q_SIGNALS: void inputChanged(); void resultChanged(); }; class FibonacciList : public QAbstractItemModel { Q_OBJECT friend class Demo; public: class Private; private: Private * m_d; bool m_ownsPrivate; explicit FibonacciList(bool owned, QObject *parent); public: explicit FibonacciList(QObject *parent = nullptr); ~FibonacciList(); 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; Q_INVOKABLE quint64 fibonacciNumber(int row) const; Q_INVOKABLE quint64 row(int row) const; -signals: +Q_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(); -signals: +Q_SIGNALS: }; class FileSystemTree : public QAbstractItemModel { Q_OBJECT friend class Demo; public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL) explicit FileSystemTree(bool owned, QObject *parent); public: explicit FileSystemTree(QObject *parent = nullptr); ~FileSystemTree(); QString path() const; void setPath(const QString& v); 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; Q_INVOKABLE QByteArray fileIcon(const QModelIndex& index) const; Q_INVOKABLE QString fileName(const QModelIndex& index) const; Q_INVOKABLE QString filePath(const QModelIndex& index) const; Q_INVOKABLE qint32 filePermissions(const QModelIndex& index) const; Q_INVOKABLE QVariant fileSize(const QModelIndex& index) const; Q_INVOKABLE qint32 fileType(const QModelIndex& index) const; -signals: +Q_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(); -signals: +Q_SIGNALS: void pathChanged(); }; class Processes : public QAbstractItemModel { Q_OBJECT friend class Demo; public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL) explicit Processes(bool owned, QObject *parent); public: explicit Processes(QObject *parent = nullptr); ~Processes(); bool active() const; void setActive(bool v); 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; Q_INVOKABLE QString cmd(const QModelIndex& index) const; Q_INVOKABLE quint8 cpuPercentage(const QModelIndex& index) const; Q_INVOKABLE float cpuUsage(const QModelIndex& index) const; Q_INVOKABLE quint64 memory(const QModelIndex& index) const; Q_INVOKABLE QString name(const QModelIndex& index) const; Q_INVOKABLE quint32 pid(const QModelIndex& index) const; Q_INVOKABLE quint32 uid(const QModelIndex& index) const; -signals: +Q_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(); -signals: +Q_SIGNALS: void activeChanged(); }; class TimeSeries : public QAbstractItemModel { Q_OBJECT friend class Demo; public: class Private; private: Private * m_d; bool m_ownsPrivate; explicit TimeSeries(bool owned, QObject *parent); public: explicit TimeSeries(QObject *parent = nullptr); ~TimeSeries(); 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; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE float cos(int row) const; Q_INVOKABLE bool setCos(int row, float value); Q_INVOKABLE float sin(int row) const; Q_INVOKABLE bool setSin(int row, float value); Q_INVOKABLE float time(int row) const; Q_INVOKABLE bool setTime(int row, float value); -signals: +Q_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(); -signals: +Q_SIGNALS: }; #endif // BINDINGS_H diff --git a/src/cpp.cpp b/src/cpp.cpp index 8749c8d..6800ee4 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: +Q_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); + Q_EMIT dataChanged(index, index); } return set; } )"; } else { cpp << R"( if (set) { - emit dataChanged(index, index); + Q_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 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; + h << "Q_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()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](%1* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](%1* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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)); + Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value)); } else { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); } }, [](%1* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](%1* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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(nullptr),\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 Q_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_functions_rust.cpp b/tests/test_functions_rust.cpp index 48429f0..2390fdc 100644 --- a/tests/test_functions_rust.cpp +++ b/tests/test_functions_rust.cpp @@ -1,96 +1,96 @@ /* generated by rust_qt_binding_generator */ #include "test_functions_rust.h" namespace { 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); } } inline void personUserNameChanged(Person* o) { - emit o->userNameChanged(); + Q_EMIT o->userNameChanged(); } } extern "C" { Person::Private* person_new(Person*, void (*)(Person*)); void person_free(Person::Private*); void person_user_name_get(const Person::Private*, QString*, qstring_set); void person_user_name_set(Person::Private*, const ushort *str, int len); void person_append(Person::Private*, const ushort*, int, quint32); void person_double_name(Person::Private*); void person_greet(const Person::Private*, const ushort*, int, QString*, qstring_set); void person_quote(const Person::Private*, const ushort*, int, const ushort*, int, QString*, qstring_set); void person_quote_bytes(const Person::Private*, const char*, int, const char*, int, QByteArray*, qbytearray_set); quint8 person_vowels_in_name(const Person::Private*); }; Person::Person(bool /*owned*/, QObject *parent): QObject(parent), m_d(nullptr), m_ownsPrivate(false) { } Person::Person(QObject *parent): QObject(parent), m_d(person_new(this, personUserNameChanged)), m_ownsPrivate(true) { } Person::~Person() { if (m_ownsPrivate) { person_free(m_d); } } QString Person::userName() const { QString v; person_user_name_get(m_d, &v, set_qstring); return v; } void Person::setUserName(const QString& v) { person_user_name_set(m_d, reinterpret_cast(v.data()), v.size()); } void Person::append(const QString& suffix, quint32 amount) { return person_append(m_d, suffix.utf16(), suffix.size(), amount); } void Person::doubleName() { return person_double_name(m_d); } QString Person::greet(const QString& name) const { QString s; person_greet(m_d, name.utf16(), name.size(), &s, set_qstring); return s; } QString Person::quote(const QString& prefix, const QString& suffix) const { QString s; person_quote(m_d, prefix.utf16(), prefix.size(), suffix.utf16(), suffix.size(), &s, set_qstring); return s; } QByteArray Person::quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const { QByteArray s; person_quote_bytes(m_d, prefix.data(), prefix.size(), suffix.data(), suffix.size(), &s, set_qbytearray); return s; } quint8 Person::vowelsInName() const { return person_vowels_in_name(m_d); } diff --git a/tests/test_functions_rust.h b/tests/test_functions_rust.h index f59aa7a..927910b 100644 --- a/tests/test_functions_rust.h +++ b/tests/test_functions_rust.h @@ -1,34 +1,34 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_FUNCTIONS_RUST_H #define TEST_FUNCTIONS_RUST_H #include #include class Person; class Person : public QObject { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged FINAL) explicit Person(bool owned, QObject *parent); public: explicit Person(QObject *parent = nullptr); ~Person(); QString userName() const; void setUserName(const QString& v); Q_INVOKABLE void append(const QString& suffix, quint32 amount); Q_INVOKABLE void doubleName(); Q_INVOKABLE QString greet(const QString& name) const; Q_INVOKABLE QString quote(const QString& prefix, const QString& suffix) const; Q_INVOKABLE QByteArray quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const; Q_INVOKABLE quint8 vowelsInName() const; -signals: +Q_SIGNALS: void userNameChanged(); }; #endif // TEST_FUNCTIONS_RUST_H diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index da1989b..02f6db0 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -1,528 +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); + Q_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); + Q_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); + Q_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(nullptr), m_ownsPrivate(false) { initHeaderData(); } NoRole::NoRole(QObject *parent): QAbstractItemModel(parent), m_d(no_role_new(this, [](const NoRole* o) { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](NoRole* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](NoRole* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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(nullptr), m_ownsPrivate(false) { initHeaderData(); } Persons::Persons(QObject *parent): QAbstractItemModel(parent), m_d(persons_new(this, [](const Persons* o) { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](Persons* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](Persons* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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_rust.h b/tests/test_list_rust.h index cc1e99e..12f3711 100644 --- a/tests/test_list_rust.h +++ b/tests/test_list_rust.h @@ -1,98 +1,98 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_LIST_RUST_H #define TEST_LIST_RUST_H #include #include class NoRole; class Persons; class NoRole : public QAbstractItemModel { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; explicit NoRole(bool owned, QObject *parent); public: explicit NoRole(QObject *parent = nullptr); ~NoRole(); 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; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE quint8 userAge(int row) const; Q_INVOKABLE bool setUserAge(int row, quint8 value); Q_INVOKABLE QString userName(int row) const; Q_INVOKABLE bool setUserName(int row, const QString& value); -signals: +Q_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(); -signals: +Q_SIGNALS: }; class Persons : public QAbstractItemModel { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; explicit Persons(bool owned, QObject *parent); public: explicit Persons(QObject *parent = nullptr); ~Persons(); 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; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE QString userName(int row) const; Q_INVOKABLE bool setUserName(int row, const QString& value); -signals: +Q_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(); -signals: +Q_SIGNALS: }; #endif // TEST_LIST_RUST_H diff --git a/tests/test_list_types_rust.cpp b/tests/test_list_types_rust.cpp index 52dcb89..c8ca158 100644 --- a/tests/test_list_types_rust.cpp +++ b/tests/test_list_types_rust.cpp @@ -1,705 +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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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); + Q_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(nullptr), m_ownsPrivate(false) { initHeaderData(); } List::List(QObject *parent): QAbstractItemModel(parent), m_d(list_new(this, [](const List* o) { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); }, [](List* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](List* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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_list_types_rust.h b/tests/test_list_types_rust.h index ee9d0a4..7232f4b 100644 --- a/tests/test_list_types_rust.h +++ b/tests/test_list_types_rust.h @@ -1,82 +1,82 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_LIST_TYPES_RUST_H #define TEST_LIST_TYPES_RUST_H #include #include class List; class List : public QAbstractItemModel { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; explicit List(bool owned, QObject *parent); public: explicit List(QObject *parent = nullptr); ~List(); 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; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE bool boolean(int row) const; Q_INVOKABLE bool setBoolean(int row, bool value); Q_INVOKABLE QByteArray bytearray(int row) const; Q_INVOKABLE bool setBytearray(int row, const QByteArray& value); Q_INVOKABLE float f32(int row) const; Q_INVOKABLE bool setF32(int row, float value); Q_INVOKABLE double f64(int row) const; Q_INVOKABLE bool setF64(int row, double value); Q_INVOKABLE qint16 i16(int row) const; Q_INVOKABLE bool setI16(int row, qint16 value); Q_INVOKABLE qint32 i32(int row) const; Q_INVOKABLE bool setI32(int row, qint32 value); Q_INVOKABLE qint64 i64(int row) const; Q_INVOKABLE bool setI64(int row, qint64 value); Q_INVOKABLE qint8 i8(int row) const; Q_INVOKABLE bool setI8(int row, qint8 value); Q_INVOKABLE QVariant optionalBoolean(int row) const; Q_INVOKABLE bool setOptionalBoolean(int row, const QVariant& value); Q_INVOKABLE QByteArray optionalBytearray(int row) const; Q_INVOKABLE bool setOptionalBytearray(int row, const QByteArray& value); Q_INVOKABLE QString optionalString(int row) const; Q_INVOKABLE bool setOptionalString(int row, const QString& value); Q_INVOKABLE QString string(int row) const; Q_INVOKABLE bool setString(int row, const QString& value); Q_INVOKABLE quint16 u16(int row) const; Q_INVOKABLE bool setU16(int row, quint16 value); Q_INVOKABLE quint32 u32(int row) const; Q_INVOKABLE bool setU32(int row, quint32 value); Q_INVOKABLE quint64 u64(int row) const; Q_INVOKABLE bool setU64(int row, quint64 value); Q_INVOKABLE quint8 u8(int row) const; Q_INVOKABLE bool setU8(int row, quint8 value); -signals: +Q_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(); -signals: +Q_SIGNALS: }; #endif // TEST_LIST_TYPES_RUST_H diff --git a/tests/test_object_rust.cpp b/tests/test_object_rust.cpp index 1a553cc..ee023fa 100644 --- a/tests/test_object_rust.cpp +++ b/tests/test_object_rust.cpp @@ -1,50 +1,50 @@ /* generated by rust_qt_binding_generator */ #include "test_object_rust.h" namespace { 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); } inline void personUserNameChanged(Person* o) { - emit o->userNameChanged(); + Q_EMIT o->userNameChanged(); } } extern "C" { Person::Private* person_new(Person*, void (*)(Person*)); void person_free(Person::Private*); void person_user_name_get(const Person::Private*, QString*, qstring_set); void person_user_name_set(Person::Private*, const ushort *str, int len); }; Person::Person(bool /*owned*/, QObject *parent): QObject(parent), m_d(nullptr), m_ownsPrivate(false) { } Person::Person(QObject *parent): QObject(parent), m_d(person_new(this, personUserNameChanged)), m_ownsPrivate(true) { } Person::~Person() { if (m_ownsPrivate) { person_free(m_d); } } QString Person::userName() const { QString v; person_user_name_get(m_d, &v, set_qstring); return v; } void Person::setUserName(const QString& v) { person_user_name_set(m_d, reinterpret_cast(v.data()), v.size()); } diff --git a/tests/test_object_rust.h b/tests/test_object_rust.h index 361937e..4b62d90 100644 --- a/tests/test_object_rust.h +++ b/tests/test_object_rust.h @@ -1,28 +1,28 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_OBJECT_RUST_H #define TEST_OBJECT_RUST_H #include #include class Person; class Person : public QObject { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged FINAL) explicit Person(bool owned, QObject *parent); public: explicit Person(QObject *parent = nullptr); ~Person(); QString userName() const; void setUserName(const QString& v); -signals: +Q_SIGNALS: void userNameChanged(); }; #endif // TEST_OBJECT_RUST_H diff --git a/tests/test_object_types_rust.cpp b/tests/test_object_types_rust.cpp index 002777b..a7553cd 100644 --- a/tests/test_object_types_rust.cpp +++ b/tests/test_object_types_rust.cpp @@ -1,362 +1,362 @@ /* generated by rust_qt_binding_generator */ #include "test_object_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_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); } } inline void objectBooleanChanged(Object* o) { - emit o->booleanChanged(); + Q_EMIT o->booleanChanged(); } inline void objectBytearrayChanged(Object* o) { - emit o->bytearrayChanged(); + Q_EMIT o->bytearrayChanged(); } inline void objectF32Changed(Object* o) { - emit o->f32Changed(); + Q_EMIT o->f32Changed(); } inline void objectF64Changed(Object* o) { - emit o->f64Changed(); + Q_EMIT o->f64Changed(); } inline void objectI16Changed(Object* o) { - emit o->i16Changed(); + Q_EMIT o->i16Changed(); } inline void objectI32Changed(Object* o) { - emit o->i32Changed(); + Q_EMIT o->i32Changed(); } inline void objectI64Changed(Object* o) { - emit o->i64Changed(); + Q_EMIT o->i64Changed(); } inline void objectI8Changed(Object* o) { - emit o->i8Changed(); + Q_EMIT o->i8Changed(); } inline void objectOptionalBooleanChanged(Object* o) { - emit o->optionalBooleanChanged(); + Q_EMIT o->optionalBooleanChanged(); } inline void objectOptionalBytearrayChanged(Object* o) { - emit o->optionalBytearrayChanged(); + Q_EMIT o->optionalBytearrayChanged(); } inline void objectOptionalStringChanged(Object* o) { - emit o->optionalStringChanged(); + Q_EMIT o->optionalStringChanged(); } inline void objectOptionalU64Changed(Object* o) { - emit o->optionalU64Changed(); + Q_EMIT o->optionalU64Changed(); } inline void objectStringChanged(Object* o) { - emit o->stringChanged(); + Q_EMIT o->stringChanged(); } inline void objectStringByFunctionChanged(Object* o) { - emit o->stringByFunctionChanged(); + Q_EMIT o->stringByFunctionChanged(); } inline void objectU16Changed(Object* o) { - emit o->u16Changed(); + Q_EMIT o->u16Changed(); } inline void objectU32Changed(Object* o) { - emit o->u32Changed(); + Q_EMIT o->u32Changed(); } inline void objectU64Changed(Object* o) { - emit o->u64Changed(); + Q_EMIT o->u64Changed(); } inline void objectU8Changed(Object* o) { - emit o->u8Changed(); + Q_EMIT o->u8Changed(); } } extern "C" { Object::Private* object_new(Object*, void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*)); void object_free(Object::Private*); bool object_boolean_get(const Object::Private*); void object_boolean_set(Object::Private*, bool); void object_bytearray_get(const Object::Private*, QByteArray*, qbytearray_set); void object_bytearray_set(Object::Private*, const char* bytes, int len); float object_f32_get(const Object::Private*); void object_f32_set(Object::Private*, float); double object_f64_get(const Object::Private*); void object_f64_set(Object::Private*, double); qint16 object_i16_get(const Object::Private*); void object_i16_set(Object::Private*, qint16); qint32 object_i32_get(const Object::Private*); void object_i32_set(Object::Private*, qint32); qint64 object_i64_get(const Object::Private*); void object_i64_set(Object::Private*, qint64); qint8 object_i8_get(const Object::Private*); void object_i8_set(Object::Private*, qint8); option_bool object_optional_boolean_get(const Object::Private*); void object_optional_boolean_set(Object::Private*, bool); void object_optional_boolean_set_none(Object::Private*); void object_optional_bytearray_get(const Object::Private*, QByteArray*, qbytearray_set); void object_optional_bytearray_set(Object::Private*, const char* bytes, int len); void object_optional_bytearray_set_none(Object::Private*); void object_optional_string_get(const Object::Private*, QString*, qstring_set); void object_optional_string_set(Object::Private*, const ushort *str, int len); void object_optional_string_set_none(Object::Private*); option_quint64 object_optional_u64_get(const Object::Private*); void object_optional_u64_set(Object::Private*, quint64); void object_optional_u64_set_none(Object::Private*); void object_string_get(const Object::Private*, QString*, qstring_set); void object_string_set(Object::Private*, const ushort *str, int len); void object_string_by_function_get(const Object::Private*, QString*, qstring_set); void object_string_by_function_set(Object::Private*, const ushort *str, int len); quint16 object_u16_get(const Object::Private*); void object_u16_set(Object::Private*, quint16); quint32 object_u32_get(const Object::Private*); void object_u32_set(Object::Private*, quint32); quint64 object_u64_get(const Object::Private*); void object_u64_set(Object::Private*, quint64); quint8 object_u8_get(const Object::Private*); void object_u8_set(Object::Private*, quint8); }; Object::Object(bool /*owned*/, QObject *parent): QObject(parent), m_d(nullptr), m_ownsPrivate(false) { } Object::Object(QObject *parent): QObject(parent), m_d(object_new(this, objectBooleanChanged, objectBytearrayChanged, objectF32Changed, objectF64Changed, objectI16Changed, objectI32Changed, objectI64Changed, objectI8Changed, objectOptionalBooleanChanged, objectOptionalBytearrayChanged, objectOptionalStringChanged, objectOptionalU64Changed, objectStringChanged, objectStringByFunctionChanged, objectU16Changed, objectU32Changed, objectU64Changed, objectU8Changed)), m_ownsPrivate(true) { } Object::~Object() { if (m_ownsPrivate) { object_free(m_d); } } bool Object::boolean() const { return object_boolean_get(m_d); } void Object::setBoolean(bool v) { object_boolean_set(m_d, v); } QByteArray Object::bytearray() const { QByteArray v; object_bytearray_get(m_d, &v, set_qbytearray); return v; } void Object::setBytearray(const QByteArray& v) { object_bytearray_set(m_d, v.data(), v.size()); } float Object::f32() const { return object_f32_get(m_d); } void Object::setF32(float v) { object_f32_set(m_d, v); } double Object::f64() const { return object_f64_get(m_d); } void Object::setF64(double v) { object_f64_set(m_d, v); } qint16 Object::i16() const { return object_i16_get(m_d); } void Object::setI16(qint16 v) { object_i16_set(m_d, v); } qint32 Object::i32() const { return object_i32_get(m_d); } void Object::setI32(qint32 v) { object_i32_set(m_d, v); } qint64 Object::i64() const { return object_i64_get(m_d); } void Object::setI64(qint64 v) { object_i64_set(m_d, v); } qint8 Object::i8() const { return object_i8_get(m_d); } void Object::setI8(qint8 v) { object_i8_set(m_d, v); } QVariant Object::optionalBoolean() const { QVariant v; auto r = object_optional_boolean_get(m_d); if (r.some) { v.setValue(r.value); } return r; } void Object::setOptionalBoolean(const QVariant& v) { if (v.isNull() || !v.canConvert()) { object_optional_boolean_set_none(m_d); } else { object_optional_boolean_set(m_d, v.value()); } } QByteArray Object::optionalBytearray() const { QByteArray v; object_optional_bytearray_get(m_d, &v, set_qbytearray); return v; } void Object::setOptionalBytearray(const QByteArray& v) { if (v.isNull()) { object_optional_bytearray_set_none(m_d); } else { object_optional_bytearray_set(m_d, v.data(), v.size()); } } QString Object::optionalString() const { QString v; object_optional_string_get(m_d, &v, set_qstring); return v; } void Object::setOptionalString(const QString& v) { if (v.isNull()) { object_optional_string_set_none(m_d); } else { object_optional_string_set(m_d, reinterpret_cast(v.data()), v.size()); } } QVariant Object::optionalU64() const { QVariant v; auto r = object_optional_u64_get(m_d); if (r.some) { v.setValue(r.value); } return r; } void Object::setOptionalU64(const QVariant& v) { if (v.isNull() || !v.canConvert()) { object_optional_u64_set_none(m_d); } else { object_optional_u64_set(m_d, v.value()); } } QString Object::string() const { QString v; object_string_get(m_d, &v, set_qstring); return v; } void Object::setString(const QString& v) { object_string_set(m_d, reinterpret_cast(v.data()), v.size()); } QString Object::stringByFunction() const { QString v; object_string_by_function_get(m_d, &v, set_qstring); return v; } void Object::setStringByFunction(const QString& v) { object_string_by_function_set(m_d, reinterpret_cast(v.data()), v.size()); } quint16 Object::u16() const { return object_u16_get(m_d); } void Object::setU16(quint16 v) { object_u16_set(m_d, v); } quint32 Object::u32() const { return object_u32_get(m_d); } void Object::setU32(quint32 v) { object_u32_set(m_d, v); } quint64 Object::u64() const { return object_u64_get(m_d); } void Object::setU64(quint64 v) { object_u64_set(m_d, v); } quint8 Object::u8() const { return object_u8_get(m_d); } void Object::setU8(quint8 v) { object_u8_set(m_d, v); } diff --git a/tests/test_object_types_rust.h b/tests/test_object_types_rust.h index fd40f43..d8fa651 100644 --- a/tests/test_object_types_rust.h +++ b/tests/test_object_types_rust.h @@ -1,96 +1,96 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_OBJECT_TYPES_RUST_H #define TEST_OBJECT_TYPES_RUST_H #include #include class Object; class Object : public QObject { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(bool boolean READ boolean WRITE setBoolean NOTIFY booleanChanged FINAL) Q_PROPERTY(QByteArray bytearray READ bytearray WRITE setBytearray NOTIFY bytearrayChanged FINAL) Q_PROPERTY(float f32 READ f32 WRITE setF32 NOTIFY f32Changed FINAL) Q_PROPERTY(double f64 READ f64 WRITE setF64 NOTIFY f64Changed FINAL) Q_PROPERTY(qint16 i16 READ i16 WRITE setI16 NOTIFY i16Changed FINAL) Q_PROPERTY(qint32 i32 READ i32 WRITE setI32 NOTIFY i32Changed FINAL) Q_PROPERTY(qint64 i64 READ i64 WRITE setI64 NOTIFY i64Changed FINAL) Q_PROPERTY(qint8 i8 READ i8 WRITE setI8 NOTIFY i8Changed FINAL) Q_PROPERTY(QVariant optionalBoolean READ optionalBoolean WRITE setOptionalBoolean NOTIFY optionalBooleanChanged FINAL) Q_PROPERTY(QByteArray optionalBytearray READ optionalBytearray WRITE setOptionalBytearray NOTIFY optionalBytearrayChanged FINAL) Q_PROPERTY(QString optionalString READ optionalString WRITE setOptionalString NOTIFY optionalStringChanged FINAL) Q_PROPERTY(QVariant optionalU64 READ optionalU64 WRITE setOptionalU64 NOTIFY optionalU64Changed FINAL) Q_PROPERTY(QString string READ string WRITE setString NOTIFY stringChanged FINAL) Q_PROPERTY(QString stringByFunction READ stringByFunction WRITE setStringByFunction NOTIFY stringByFunctionChanged FINAL) Q_PROPERTY(quint16 u16 READ u16 WRITE setU16 NOTIFY u16Changed FINAL) Q_PROPERTY(quint32 u32 READ u32 WRITE setU32 NOTIFY u32Changed FINAL) Q_PROPERTY(quint64 u64 READ u64 WRITE setU64 NOTIFY u64Changed FINAL) Q_PROPERTY(quint8 u8 READ u8 WRITE setU8 NOTIFY u8Changed FINAL) explicit Object(bool owned, QObject *parent); public: explicit Object(QObject *parent = nullptr); ~Object(); bool boolean() const; void setBoolean(bool v); QByteArray bytearray() const; void setBytearray(const QByteArray& v); float f32() const; void setF32(float v); double f64() const; void setF64(double v); qint16 i16() const; void setI16(qint16 v); qint32 i32() const; void setI32(qint32 v); qint64 i64() const; void setI64(qint64 v); qint8 i8() const; void setI8(qint8 v); QVariant optionalBoolean() const; void setOptionalBoolean(const QVariant& v); QByteArray optionalBytearray() const; void setOptionalBytearray(const QByteArray& v); QString optionalString() const; void setOptionalString(const QString& v); QVariant optionalU64() const; void setOptionalU64(const QVariant& v); QString string() const; void setString(const QString& v); QString stringByFunction() const; void setStringByFunction(const QString& v); quint16 u16() const; void setU16(quint16 v); quint32 u32() const; void setU32(quint32 v); quint64 u64() const; void setU64(quint64 v); quint8 u8() const; void setU8(quint8 v); -signals: +Q_SIGNALS: void booleanChanged(); void bytearrayChanged(); void f32Changed(); void f64Changed(); void i16Changed(); void i32Changed(); void i64Changed(); void i8Changed(); void optionalBooleanChanged(); void optionalBytearrayChanged(); void optionalStringChanged(); void optionalU64Changed(); void stringChanged(); void stringByFunctionChanged(); void u16Changed(); void u32Changed(); void u64Changed(); void u8Changed(); }; #endif // TEST_OBJECT_TYPES_RUST_H diff --git a/tests/test_objects_rust.cpp b/tests/test_objects_rust.cpp index 69a7a5b..37c616a 100644 --- a/tests/test_objects_rust.cpp +++ b/tests/test_objects_rust.cpp @@ -1,125 +1,125 @@ /* generated by rust_qt_binding_generator */ #include "test_objects_rust.h" namespace { 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); } inline void innerObjectDescriptionChanged(InnerObject* o) { - emit o->descriptionChanged(); + Q_EMIT o->descriptionChanged(); } } extern "C" { Group::Private* group_new(Group*, Person*, InnerObject*, void (*)(InnerObject*)); void group_free(Group::Private*); Person::Private* group_person_get(const Group::Private*); }; extern "C" { InnerObject::Private* inner_object_new(InnerObject*, void (*)(InnerObject*)); void inner_object_free(InnerObject::Private*); void inner_object_description_get(const InnerObject::Private*, QString*, qstring_set); void inner_object_description_set(InnerObject::Private*, const ushort *str, int len); }; extern "C" { Person::Private* person_new(Person*, InnerObject*, void (*)(InnerObject*)); void person_free(Person::Private*); InnerObject::Private* person_object_get(const Person::Private*); }; Group::Group(bool /*owned*/, QObject *parent): QObject(parent), m_person(new Person(false, this)), m_d(nullptr), m_ownsPrivate(false) { } Group::Group(QObject *parent): QObject(parent), m_person(new Person(false, this)), m_d(group_new(this, m_person, m_person->m_object, innerObjectDescriptionChanged)), m_ownsPrivate(true) { m_person->m_d = group_person_get(m_d); m_person->m_object->m_d = person_object_get(m_person->m_d); } Group::~Group() { if (m_ownsPrivate) { group_free(m_d); } } const Person* Group::person() const { return m_person; } Person* Group::person() { return m_person; } InnerObject::InnerObject(bool /*owned*/, QObject *parent): QObject(parent), m_d(nullptr), m_ownsPrivate(false) { } InnerObject::InnerObject(QObject *parent): QObject(parent), m_d(inner_object_new(this, innerObjectDescriptionChanged)), m_ownsPrivate(true) { } InnerObject::~InnerObject() { if (m_ownsPrivate) { inner_object_free(m_d); } } QString InnerObject::description() const { QString v; inner_object_description_get(m_d, &v, set_qstring); return v; } void InnerObject::setDescription(const QString& v) { inner_object_description_set(m_d, reinterpret_cast(v.data()), v.size()); } Person::Person(bool /*owned*/, QObject *parent): QObject(parent), m_object(new InnerObject(false, this)), m_d(nullptr), m_ownsPrivate(false) { } Person::Person(QObject *parent): QObject(parent), m_object(new InnerObject(false, this)), m_d(person_new(this, m_object, innerObjectDescriptionChanged)), m_ownsPrivate(true) { m_object->m_d = person_object_get(m_d); } Person::~Person() { if (m_ownsPrivate) { person_free(m_d); } } const InnerObject* Person::object() const { return m_object; } InnerObject* Person::object() { return m_object; } diff --git a/tests/test_objects_rust.h b/tests/test_objects_rust.h index 7a02f12..c8d6a96 100644 --- a/tests/test_objects_rust.h +++ b/tests/test_objects_rust.h @@ -1,74 +1,74 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_OBJECTS_RUST_H #define TEST_OBJECTS_RUST_H #include #include class Group; class InnerObject; class Person; class Group : public QObject { Q_OBJECT friend class Person; public: class Private; private: Person* const m_person; Private * m_d; bool m_ownsPrivate; Q_PROPERTY(Person* person READ person NOTIFY personChanged FINAL) explicit Group(bool owned, QObject *parent); public: explicit Group(QObject *parent = nullptr); ~Group(); const Person* person() const; Person* person(); -signals: +Q_SIGNALS: void personChanged(); }; class InnerObject : public QObject { Q_OBJECT friend class Group; friend class Person; public: class Private; private: Private * m_d; bool m_ownsPrivate; Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged FINAL) explicit InnerObject(bool owned, QObject *parent); public: explicit InnerObject(QObject *parent = nullptr); ~InnerObject(); QString description() const; void setDescription(const QString& v); -signals: +Q_SIGNALS: void descriptionChanged(); }; class Person : public QObject { Q_OBJECT friend class Group; public: class Private; private: InnerObject* const m_object; Private * m_d; bool m_ownsPrivate; Q_PROPERTY(InnerObject* object READ object NOTIFY objectChanged FINAL) explicit Person(bool owned, QObject *parent); public: explicit Person(QObject *parent = nullptr); ~Person(); const InnerObject* object() const; InnerObject* object(); -signals: +Q_SIGNALS: void objectChanged(); }; #endif // TEST_OBJECTS_RUST_H diff --git a/tests/test_tree_rust.cpp b/tests/test_tree_rust.cpp index 2baa0d9..416d4e2 100644 --- a/tests/test_tree_rust.cpp +++ b/tests/test_tree_rust.cpp @@ -1,341 +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); + Q_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(nullptr), 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)); + Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value)); } else { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); } }, [](Persons* o) { - emit o->layoutAboutToBeChanged(); + Q_EMIT o->layoutAboutToBeChanged(); }, [](Persons* o) { o->updatePersistentIndexes(); - emit o->layoutChanged(); + Q_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")); } diff --git a/tests/test_tree_rust.h b/tests/test_tree_rust.h index 0eabbd9..ff02484 100644 --- a/tests/test_tree_rust.h +++ b/tests/test_tree_rust.h @@ -1,52 +1,52 @@ /* generated by rust_qt_binding_generator */ #ifndef TEST_TREE_RUST_H #define TEST_TREE_RUST_H #include #include class Persons; class Persons : public QAbstractItemModel { Q_OBJECT public: class Private; private: Private * m_d; bool m_ownsPrivate; explicit Persons(bool owned, QObject *parent); public: explicit Persons(QObject *parent = nullptr); ~Persons(); 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; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE QString userName(const QModelIndex& index) const; Q_INVOKABLE bool setUserName(const QModelIndex& index, const QString& value); -signals: +Q_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(); -signals: +Q_SIGNALS: }; #endif // TEST_TREE_RUST_H