diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 1e55512..0fa12db 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -1,1504 +1,1504 @@ /* 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(value); + return QVariant::fromValue(value); } return QVariant(); } }; struct option_QString { public: QString value; bool some; operator QVariant() const { if (some) { - return QVariant(value); + return QVariant::fromValue(value); } return QVariant(); } }; struct option_quint64 { public: quint64 value; bool some; operator QVariant() const { if (some) { - return QVariant(value); + return QVariant::fromValue(value); } return QVariant(); } }; 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) { v->truncate(0); v->append(bytes, nbytes); } struct qmodelindex_t { int row; quintptr id; }; inline void fibonacciInputChanged(Fibonacci* o) { emit o->inputChanged(); } inline void fibonacciResultChanged(Fibonacci* o) { emit o->resultChanged(); } inline void fileSystemTreePathChanged(FileSystemTree* o) { emit o->pathChanged(); } inline void processesActiveChanged(Processes* o) { emit o->activeChanged(); } } extern "C" { Demo::Private* demo_new(Demo*, Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*), FibonacciList*, void (*)(const FibonacciList*), void (*)(FibonacciList*, quintptr, quintptr), void (*)(FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int), void (*)(FibonacciList*), void (*)(FibonacciList*, int, int), void (*)(FibonacciList*), FileSystemTree*, void (*)(FileSystemTree*), void (*)(const FileSystemTree*, quintptr, bool), void (*)(FileSystemTree*, quintptr, quintptr), void (*)(FileSystemTree*), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, int), void (*)(FileSystemTree*), Processes*, void (*)(Processes*), void (*)(const Processes*, quintptr, bool), void (*)(Processes*, quintptr, quintptr), void (*)(Processes*), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, int), void (*)(Processes*), TimeSeries*, void (*)(const TimeSeries*), void (*)(TimeSeries*, quintptr, quintptr), void (*)(TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*, 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*, uint); 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 &parent) { return fibonacci_list_insert_rows(m_d, row, count); } bool FibonacciList::removeRows(int row, int count, const QModelIndex &parent) { 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::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; } QVariant FibonacciList::fibonacciNumber(int row) const { QVariant v; - v = fibonacci_list_data_fibonacci_number(m_d, row); + v.setValue(fibonacci_list_data_fibonacci_number(m_d, row)); return v; } QVariant FibonacciList::row(int row) const { QVariant v; - v = fibonacci_list_data_row(m_d, row); + v.setValue(fibonacci_list_data_row(m_d, row)); return v; } 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 fibonacciNumber(index.row()); case Qt::DisplayRole: case Qt::UserRole + 1: return row(index.row()); } case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 0: return fibonacciNumber(index.row()); } } return QVariant(); } 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*, quintptr, quintptr), void (*)(FibonacciList*), void (*)(FibonacciList*), void (*)(FibonacciList*, 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*, quintptr, bool); bool file_system_tree_can_fetch_more(const FileSystemTree::Private*, quintptr, bool); void file_system_tree_fetch_more(FileSystemTree::Private*, quintptr, bool); quintptr file_system_tree_index(const FileSystemTree::Private*, quintptr, bool, int); qmodelindex_t file_system_tree_parent(const FileSystemTree::Private*, quintptr); int file_system_tree_row(const FileSystemTree::Private*, quintptr); } 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; } return file_system_tree_row_count(m_d, parent.internalId(), parent.isValid()); } 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 quintptr id = file_system_tree_index(m_d, parent.internalId(), parent.isValid(), 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; } return file_system_tree_can_fetch_more(m_d, parent.internalId(), parent.isValid()); } void FileSystemTree::fetchMore(const QModelIndex &parent) { file_system_tree_fetch_more(m_d, parent.internalId(), parent.isValid()); } 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; } QVariant FileSystemTree::fileIcon(const QModelIndex& index) const { QVariant v; QByteArray b; file_system_tree_data_file_icon(m_d, index.internalId(), &b, set_qbytearray); if (!b.isNull()) v.setValue(b); return v; } QVariant FileSystemTree::fileName(const QModelIndex& index) const { QVariant v; QString s; file_system_tree_data_file_name(m_d, index.internalId(), &s, set_qstring); if (!s.isNull()) v.setValue(s); return v; } QVariant FileSystemTree::filePath(const QModelIndex& index) const { QVariant v; QString s; file_system_tree_data_file_path(m_d, index.internalId(), &s, set_qstring); if (!s.isNull()) v.setValue(s); return v; } QVariant FileSystemTree::filePermissions(const QModelIndex& index) const { QVariant v; - v = file_system_tree_data_file_permissions(m_d, index.internalId()); + v.setValue(file_system_tree_data_file_permissions(m_d, index.internalId())); return v; } QVariant FileSystemTree::fileSize(const QModelIndex& index) const { QVariant v; v = file_system_tree_data_file_size(m_d, index.internalId()); return v; } QVariant FileSystemTree::fileType(const QModelIndex& index) const { QVariant v; - v = file_system_tree_data_file_type(m_d, index.internalId()); + v.setValue(file_system_tree_data_file_type(m_d, index.internalId())); return v; } 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 fileIcon(index); case Qt::DisplayRole: case Qt::UserRole + 1: return fileName(index); case Qt::UserRole + 2: return filePath(index); case Qt::UserRole + 3: return filePermissions(index); case Qt::UserRole + 4: return fileSize(index); case Qt::UserRole + 5: return fileType(index); } case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 4: return fileSize(index); } case 2: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 2: return filePath(index); } case 3: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 3: return filePermissions(index); } case 4: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 5: return fileType(index); } } return QVariant(); } 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*, quintptr, bool), void (*)(FileSystemTree*, quintptr, quintptr), void (*)(FileSystemTree*), void (*)(FileSystemTree*), void (*)(FileSystemTree*, option_quintptr, int, 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); uint processes_data_pid(const Processes::Private*, quintptr); uint 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*, quintptr, bool); bool processes_can_fetch_more(const Processes::Private*, quintptr, bool); void processes_fetch_more(Processes::Private*, quintptr, bool); quintptr processes_index(const Processes::Private*, quintptr, bool, int); qmodelindex_t processes_parent(const Processes::Private*, quintptr); int processes_row(const Processes::Private*, quintptr); } 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; } return processes_row_count(m_d, parent.internalId(), parent.isValid()); } 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 quintptr id = processes_index(m_d, parent.internalId(), parent.isValid(), 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; } return processes_can_fetch_more(m_d, parent.internalId(), parent.isValid()); } void Processes::fetchMore(const QModelIndex &parent) { processes_fetch_more(m_d, parent.internalId(), parent.isValid()); } 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; } QVariant Processes::cmd(const QModelIndex& index) const { QVariant v; QString s; processes_data_cmd(m_d, index.internalId(), &s, set_qstring); if (!s.isNull()) v.setValue(s); return v; } QVariant Processes::cpuPercentage(const QModelIndex& index) const { QVariant v; - v = processes_data_cpu_percentage(m_d, index.internalId()); + v.setValue(processes_data_cpu_percentage(m_d, index.internalId())); return v; } QVariant Processes::cpuUsage(const QModelIndex& index) const { QVariant v; - v = processes_data_cpu_usage(m_d, index.internalId()); + v.setValue(processes_data_cpu_usage(m_d, index.internalId())); return v; } QVariant Processes::memory(const QModelIndex& index) const { QVariant v; - v = processes_data_memory(m_d, index.internalId()); + v.setValue(processes_data_memory(m_d, index.internalId())); return v; } QVariant Processes::name(const QModelIndex& index) const { QVariant v; QString s; processes_data_name(m_d, index.internalId(), &s, set_qstring); if (!s.isNull()) v.setValue(s); return v; } QVariant Processes::pid(const QModelIndex& index) const { QVariant v; - v = processes_data_pid(m_d, index.internalId()); + v.setValue(processes_data_pid(m_d, index.internalId())); return v; } QVariant Processes::uid(const QModelIndex& index) const { QVariant v; - v = processes_data_uid(m_d, index.internalId()); + v.setValue(processes_data_uid(m_d, index.internalId())); return v; } 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 cmd(index); case Qt::UserRole + 1: return cpuPercentage(index); case Qt::UserRole + 2: return cpuUsage(index); case Qt::UserRole + 3: return memory(index); case Qt::DisplayRole: case Qt::UserRole + 4: return name(index); case Qt::ToolTipRole: case Qt::UserRole + 5: return pid(index); case Qt::UserRole + 6: return uid(index); } case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 2: return cpuUsage(index); } case 2: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 3: return memory(index); } } return QVariant(); } 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*, quintptr, bool), void (*)(Processes*, quintptr, quintptr), void (*)(Processes*), void (*)(Processes*), void (*)(Processes*, option_quintptr, int, 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 &parent) { return time_series_insert_rows(m_d, row, count); } bool TimeSeries::removeRows(int row, int count, const QModelIndex &parent) { 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::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; } QVariant TimeSeries::cos(int row) const { QVariant v; - v = time_series_data_cos(m_d, row); + v.setValue(time_series_data_cos(m_d, row)); return v; } bool TimeSeries::setCos(int row, const QVariant& value) { if (!value.canConvert(qMetaTypeId())) { return false; } bool set = false; set = time_series_set_data_cos(m_d, row, value.value()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant TimeSeries::sin(int row) const { QVariant v; - v = time_series_data_sin(m_d, row); + v.setValue(time_series_data_sin(m_d, row)); return v; } bool TimeSeries::setSin(int row, const QVariant& value) { if (!value.canConvert(qMetaTypeId())) { return false; } bool set = false; set = time_series_set_data_sin(m_d, row, value.value()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant TimeSeries::time(int row) const { QVariant v; - v = time_series_data_time(m_d, row); + v.setValue(time_series_data_time(m_d, row)); return v; } bool TimeSeries::setTime(int row, const QVariant& value) { if (!value.canConvert(qMetaTypeId())) { return false; } bool set = false; set = time_series_set_data_time(m_d, row, value.value()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant TimeSeries::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::UserRole + 0: return cos(index.row()); case Qt::UserRole + 1: return sin(index.row()); case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 2: return time(index.row()); } case 1: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 1: return sin(index.row()); } case 2: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 0: return cos(index.row()); } } return QVariant(); } 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) { return setCos(index.row(), value); } if (role == Qt::UserRole + 1) { return setSin(index.row(), value); } if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 2) { return setTime(index.row(), value); } } if (index.column() == 1) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 1) { return setSin(index.row(), value); } } if (index.column() == 2) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { return setCos(index.row(), value); } } return false; } extern "C" { TimeSeries::Private* time_series_new(TimeSeries*, void (*)(const TimeSeries*), void (*)(TimeSeries*, quintptr, quintptr), void (*)(TimeSeries*), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int), void (*)(TimeSeries*), void (*)(TimeSeries*, int, int), void (*)(TimeSeries*)); void time_series_free(TimeSeries::Private*); }; Demo::Demo(bool /*owned*/, QObject *parent): QObject(parent), m_fibonacci(new Fibonacci(false, this)), m_fibonacciList(new FibonacciList(false, this)), m_fileSystemTree(new FileSystemTree(false, this)), m_processes(new Processes(false, this)), m_timeSeries(new TimeSeries(false, this)), m_d(0), m_ownsPrivate(false) { } Demo::Demo(QObject *parent): QObject(parent), m_fibonacci(new Fibonacci(false, this)), m_fibonacciList(new FibonacciList(false, this)), m_fileSystemTree(new FileSystemTree(false, this)), m_processes(new Processes(false, this)), m_timeSeries(new TimeSeries(false, this)), m_d(demo_new(this, m_fibonacci, fibonacciInputChanged, fibonacciResultChanged, m_fibonacciList, [](const FibonacciList* o) { emit o->newDataReady(QModelIndex()); }, [](FibonacciList* o, 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) { o->beginRemoveRows(QModelIndex(), first, last); }, [](FibonacciList* o) { o->endRemoveRows(); } , m_fileSystemTree, fileSystemTreePathChanged, [](const FileSystemTree* o, quintptr id, bool valid) { if (valid) { int row = file_system_tree_row(o->m_d, id); emit o->newDataReady(o->createIndex(row, 0, id)); } else { emit o->newDataReady(QModelIndex()); } }, [](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 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, quintptr id, bool valid) { if (valid) { int row = processes_row(o->m_d, id); emit o->newDataReady(o->createIndex(row, 0, id)); } else { emit o->newDataReady(QModelIndex()); } }, [](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 id, int first, int last) { if (id.some) { int row = processes_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](Processes* o) { o->endRemoveRows(); } , m_timeSeries, [](const TimeSeries* o) { emit o->newDataReady(QModelIndex()); }, [](TimeSeries* o, 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) { o->beginRemoveRows(QModelIndex(), first, last); }, [](TimeSeries* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { m_fibonacci->m_d = demo_fibonacci_get(m_d); m_fibonacciList->m_d = demo_fibonacci_list_get(m_d); m_fileSystemTree->m_d = demo_file_system_tree_get(m_d); m_processes->m_d = demo_processes_get(m_d); m_timeSeries->m_d = demo_time_series_get(m_d); connect(this->m_fibonacciList, &FibonacciList::newDataReady, this->m_fibonacciList, [this](const QModelIndex& i) { this->m_fibonacciList->fetchMore(i); }, Qt::QueuedConnection); connect(this->m_fileSystemTree, &FileSystemTree::newDataReady, this->m_fileSystemTree, [this](const QModelIndex& i) { this->m_fileSystemTree->fetchMore(i); }, Qt::QueuedConnection); connect(this->m_processes, &Processes::newDataReady, this->m_processes, [this](const QModelIndex& i) { this->m_processes->fetchMore(i); }, Qt::QueuedConnection); connect(this->m_timeSeries, &TimeSeries::newDataReady, this->m_timeSeries, [this](const QModelIndex& i) { this->m_timeSeries->fetchMore(i); }, Qt::QueuedConnection); } Demo::~Demo() { if (m_ownsPrivate) { demo_free(m_d); } } const Fibonacci* Demo::fibonacci() const { return m_fibonacci; } Fibonacci* Demo::fibonacci() { return m_fibonacci; } const FibonacciList* Demo::fibonacciList() const { return m_fibonacciList; } FibonacciList* Demo::fibonacciList() { return m_fibonacciList; } const FileSystemTree* Demo::fileSystemTree() const { return m_fileSystemTree; } FileSystemTree* Demo::fileSystemTree() { return m_fileSystemTree; } const Processes* Demo::processes() const { return m_processes; } Processes* Demo::processes() { return m_processes; } const TimeSeries* Demo::timeSeries() const { return m_timeSeries; } TimeSeries* Demo::timeSeries() { return m_timeSeries; } Fibonacci::Fibonacci(bool /*owned*/, QObject *parent): QObject(parent), m_d(0), m_ownsPrivate(false) { } Fibonacci::Fibonacci(QObject *parent): QObject(parent), m_d(fibonacci_new(this, fibonacciInputChanged, fibonacciResultChanged)), m_ownsPrivate(true) { } Fibonacci::~Fibonacci() { if (m_ownsPrivate) { fibonacci_free(m_d); } } quint32 Fibonacci::input() const { return fibonacci_input_get(m_d); } void Fibonacci::setInput(uint v) { fibonacci_input_set(m_d, v); } quint64 Fibonacci::result() const { return fibonacci_result_get(m_d); } FibonacciList::FibonacciList(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } FibonacciList::FibonacciList(QObject *parent): QAbstractItemModel(parent), m_d(fibonacci_list_new(this, [](const FibonacciList* o) { emit o->newDataReady(QModelIndex()); }, [](FibonacciList* o, 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) { o->beginRemoveRows(QModelIndex(), first, last); }, [](FibonacciList* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &FibonacciList::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } FibonacciList::~FibonacciList() { if (m_ownsPrivate) { fibonacci_list_free(m_d); } } void FibonacciList::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("row")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("fibonacciNumber")); } FileSystemTree::FileSystemTree(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } FileSystemTree::FileSystemTree(QObject *parent): QAbstractItemModel(parent), m_d(file_system_tree_new(this, fileSystemTreePathChanged, [](const FileSystemTree* o, quintptr id, bool valid) { if (valid) { int row = file_system_tree_row(o->m_d, id); emit o->newDataReady(o->createIndex(row, 0, id)); } else { emit o->newDataReady(QModelIndex()); } }, [](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 id, int first, int last) { if (id.some) { int row = file_system_tree_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](FileSystemTree* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &FileSystemTree::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } FileSystemTree::~FileSystemTree() { if (m_ownsPrivate) { file_system_tree_free(m_d); } } void FileSystemTree::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("fileName")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("fileSize")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("filePath")); m_headerData.insert(qMakePair(3, Qt::DisplayRole), QVariant("filePermissions")); m_headerData.insert(qMakePair(4, Qt::DisplayRole), QVariant("fileType")); } QString FileSystemTree::path() const { QString v; file_system_tree_path_get(m_d, &v, set_qstring); return v; } void FileSystemTree::setPath(const QString& v) { if (v.isNull()) { file_system_tree_path_set_none(m_d); } else { file_system_tree_path_set(m_d, reinterpret_cast(v.data()), v.size()); } } Processes::Processes(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } Processes::Processes(QObject *parent): QAbstractItemModel(parent), m_d(processes_new(this, processesActiveChanged, [](const Processes* o, quintptr id, bool valid) { if (valid) { int row = processes_row(o->m_d, id); emit o->newDataReady(o->createIndex(row, 0, id)); } else { emit o->newDataReady(QModelIndex()); } }, [](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 id, int first, int last) { if (id.some) { int row = processes_row(o->m_d, id.value); o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); } else { o->beginRemoveRows(QModelIndex(), first, last); } }, [](Processes* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &Processes::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } Processes::~Processes() { if (m_ownsPrivate) { processes_free(m_d); } } void Processes::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("name")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("cpuUsage")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("memory")); } bool Processes::active() const { return processes_active_get(m_d); } void Processes::setActive(bool v) { processes_active_set(m_d, v); } TimeSeries::TimeSeries(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } TimeSeries::TimeSeries(QObject *parent): QAbstractItemModel(parent), m_d(time_series_new(this, [](const TimeSeries* o) { emit o->newDataReady(QModelIndex()); }, [](TimeSeries* o, 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) { o->beginRemoveRows(QModelIndex(), first, last); }, [](TimeSeries* o) { o->endRemoveRows(); } )), m_ownsPrivate(true) { connect(this, &TimeSeries::newDataReady, this, [this](const QModelIndex& i) { this->fetchMore(i); }, Qt::QueuedConnection); initHeaderData(); } TimeSeries::~TimeSeries() { if (m_ownsPrivate) { time_series_free(m_d); } } void TimeSeries::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("time")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("sin")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("cos")); } diff --git a/src/cpp.cpp b/src/cpp.cpp index 57cce0a..8522da3 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -1,1022 +1,1025 @@ /* * 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; } 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; 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) { if (o.type == ObjectType::List) { h << QString(" Q_INVOKABLE QVariant %1(int row) const;\n").arg(ip.name); if (ip.write) { h << QString(" Q_INVOKABLE bool set%1(int row, const QVariant& value);\n").arg(upperInitial(ip.name)); } } else { h << QString(" Q_INVOKABLE QVariant %1(const QModelIndex& index) const;\n").arg(ip.name); if (ip.write) { h << QString(" Q_INVOKABLE bool set%1(const QModelIndex& index, const QVariant& value);\n").arg(upperInitial(ip.name)); } } } h << R"( signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); )"; } bool isColumnWrite(const Object& o, int col) { for (auto ip: o.itemProperties) { if (ip.write && 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 if (o.type == ObjectType::List) { idx = ", row"; cpp << QString("QVariant %1::%2(int row) const\n{\n") .arg(o.name, ip.name); } else { cpp << QString("QVariant %1::%2(const QModelIndex& index) const\n{\n") .arg(o.name, ip.name); } cpp << " QVariant v;\n"; 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 << " if (!s.isNull()) v.setValue(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 << " if (!b.isNull()) v.setValue(b);\n"; + } else if (!ip.optional) { + cpp << QString(" v.setValue(%1_data_%2(m_d%3));\n") + .arg(lcname, snakeCase(ip.name), idx); } else { cpp << QString(" v = %1_data_%2(m_d%3);\n") .arg(lcname, snakeCase(ip.name), idx); } cpp << " return v;\n"; cpp << "}\n\n"; if (!ip.write) { return; } // setter if (o.type == ObjectType::List) { idx = ", row"; cpp << QString("bool %1::set%2(int row, const QVariant& value)\n{\n") .arg(o.name, upperInitial(ip.name)); } else { cpp << QString("bool %1::set%2(const QModelIndex& index, const QVariant& value)\n{\n") .arg(o.name, upperInitial(ip.name)); } cpp << QString(" if (!value.canConvert(qMetaTypeId<%1>())) {\n return false;\n }\n").arg(ip.type.name); cpp << " bool set = false;\n"; if (ip.optional) { QString test = "!value.isValid()"; if (ip.type.isComplex()) { test += " || value.isNull()"; } 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"; } QString val = QString("value.value<%1>()").arg(ip.type.name); if (ip.type.isComplex()) { cpp << QString(" const %1 s = %2;\n").arg(ip.type.name, val); if (ip.type.name == "QString") { val = "s.utf16(), s.length()"; } else { val = "s.data(), s.length()"; } } cpp << QString(" set = %1_set_data_%2(m_d%3, %4);") .arg(lcname, snakeCase(ip.name), idx, val) << endl; if (o.type == ObjectType::List) { cpp << R"( if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } )"; } else { cpp << R"( if (set) { emit dataChanged(index, index); } return set; } )"; } } void writeCppModel(QTextStream& cpp, const Object& o) { const QString lcname(snakeCase(o.name)); QString indexDecl = ", int"; QString index = ", index.row()"; if (o.type == ObjectType::Tree) { indexDecl = ", quintptr"; index = ", index.internalId()"; } cpp << "extern \"C\" {\n"; for (auto ip: o.itemProperties) { if (ip.type.isComplex()) { cpp << QString(" void %2_data_%3(const %1::Private*%5, %4);\n") .arg(o.name, lcname, snakeCase(ip.name), cGetType(ip.type), indexDecl); } else { cpp << QString(" %4 %2_data_%3(const %1::Private*%5);\n") .arg(o.name, lcname, snakeCase(ip.name), cppSetType(ip), indexDecl); } if (ip.write) { if (ip.type.name == "QString") { cpp << QString(" bool %2_set_data_%3(%1::Private*%4, const ushort* s, int len);") .arg(o.name, lcname, snakeCase(ip.name), indexDecl) << endl; } else if (ip.type.name == "QByteArray") { cpp << QString(" bool %2_set_data_%3(%1::Private*%4, const char* s, int len);") .arg(o.name, lcname, snakeCase(ip.name), indexDecl) << endl; } else { cpp << QString(" bool %2_set_data_%3(%1::Private*%5, %4);") .arg(o.name, lcname, snakeCase(ip.name), ip.type.cSetType, indexDecl) << endl; } if (ip.optional) { cpp << QString(" bool %2_set_data_%3_none(%1::Private*%4);") .arg(o.name, lcname, snakeCase(ip.name), indexDecl) << endl; } } } cpp << QString(" void %2_sort(%1::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);\n").arg(o.name, lcname); if (o.type == ObjectType::List) { cpp << QString(R"( int %2_row_count(const %1::Private*); bool %2_insert_rows(%1::Private*, int, int); bool %2_remove_rows(%1::Private*, int, int); bool %2_can_fetch_more(const %1::Private*); void %2_fetch_more(%1::Private*); } int %1::columnCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : %3; } bool %1::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } int %1::rowCount(const QModelIndex &parent) const { return (parent.isValid()) ? 0 : %2_row_count(m_d); } bool %1::insertRows(int row, int count, const QModelIndex &parent) { return %2_insert_rows(m_d, row, count); } bool %1::removeRows(int row, int count, const QModelIndex &parent) { 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); } } )").arg(o.name, lcname, QString::number(o.columnCount)); } else { cpp << QString(R"( int %2_row_count(const %1::Private*, quintptr, bool); bool %2_can_fetch_more(const %1::Private*, quintptr, bool); void %2_fetch_more(%1::Private*, quintptr, bool); quintptr %2_index(const %1::Private*, quintptr, bool, int); qmodelindex_t %2_parent(const %1::Private*, quintptr); int %2_row(const %1::Private*, quintptr); } 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; } return %2_row_count(m_d, parent.internalId(), parent.isValid()); } 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 quintptr id = %2_index(m_d, parent.internalId(), parent.isValid(), 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; } return %2_can_fetch_more(m_d, parent.internalId(), parent.isValid()); } void %1::fetchMore(const QModelIndex &parent) { %2_fetch_more(m_d, parent.internalId(), parent.isValid()); } )").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); if (o.type == ObjectType::List) { cpp << QString(" return %1(index.row());\n").arg(ip.name); } else { cpp << QString(" return %1(index);\n").arg(ip.name); } } cpp << " }\n"; } cpp << " }\n return QVariant();\n}\n\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"; if (o.type == ObjectType::List) { cpp << QString(" return set%1(index.row(), value);\n") .arg(upperInitial(ip.name)); } else { cpp << QString(" return set%1(index, value);\n") .arg(upperInitial(ip.name)); } 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; h << QString(" Q_PROPERTY(%1 %2 READ %2 %3NOTIFY %2Changed FINAL)") .arg(p.type.name + (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 { h << " " << p.type.name << " " << p.name << "() const;" << endl; if (p.write) { h << " void set" << upperInitial(p.name) << "(" << p.type.cppSetType << " 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++) { h << QString("%1 %2%3").arg(a->type.cppSetType, a->name, a + 1 < f.args.end() ? ", " : ""); } h << QString(")%1;") .arg(f.mut ? "" : " const"); h << endl; } if (baseType(o) == "QAbstractItemModel") { writeHeaderItemModel(h, o); } h << "signals:" << endl; for (auto p: o.properties) { h << " void " << p.name << "Changed();" << endl; } h << "};" << endl; } void constructorArgsDecl(QTextStream& cpp, const Object& o, const Configuration& conf) { cpp << o.name << "*"; for (auto p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(", "); constructorArgsDecl(cpp, conf.findObject(p.type.name), conf); } else { cpp << QString(", void (*)(%1*)").arg(o.name); } } if (o.type == ObjectType::List) { cpp << QString(R"(, void (*)(const %1*), void (*)(%1*, quintptr, quintptr), void (*)(%1*), void (*)(%1*), void (*)(%1*, int, int), void (*)(%1*), void (*)(%1*, int, int), void (*)(%1*))").arg(o.name); } if (o.type == ObjectType::Tree) { cpp << QString(R"(, void (*)(const %1*, quintptr, bool), void (*)(%1*, quintptr, quintptr), void (*)(%1*), void (*)(%1*), void (*)(%1*, option_quintptr, int, int), void (*)(%1*), void (*)(%1*, option_quintptr, int, int), void (*)(%1*))").arg(o.name); } } QString changedF(const Object& o, const Property& p) { return lowerInitial(o.name) + upperInitial(p.name) + "Changed"; } void constructorArgs(QTextStream& cpp, const QString& prefix, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << ", " << prefix << "m_" << p.name; constructorArgs(cpp, "m_" + p.name + "->", conf.findObject(p.type.name), conf); } else { cpp << ",\n " << changedF(o, p); } } if (o.type == ObjectType::List) { cpp << QString(R"(, [](const %1* o) { emit o->newDataReady(QModelIndex()); }, [](%1* o, 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) { 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, quintptr id, bool valid) { if (valid) { int row = %2_row(o->m_d, id); emit o->newDataReady(o->createIndex(row, 0, id)); } else { emit o->newDataReady(QModelIndex()); } }, [](%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 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.rustType; } } // 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 { cpp << QString(" %3 %2_get(const %1::Private*);") .arg(o.name, base, p.type.name) << endl; } if (p.write) { QString t = p.type.cSetType; if (t == "qstring_t") { t = "const ushort *str, int len"; } else if (t == "qbytearray_t") { t = "const char* bytes, int len"; } cpp << QString(" void %2_set(%1::Private*, %3);") .arg(o.name, base, t) << endl; if (p.optional) { cpp << QString(" void %2_set_none(%1::Private*);") .arg(o.name, base) << endl; } } } for (const Function& f: o.functions) { writeFunctionCDecl(cpp, f, lcname, o); } } void initializeMembersEmpty(QTextStream& cpp, const Object& o, const Configuration& conf) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { initializeMembersEmpty(cpp, conf.findObject(p.type.name), conf); cpp << QString(" %1m_%2(new %3(false, this)),\n") .arg(p.name, p.type.name); } } } void initializeMembersZero(QTextStream& cpp, const Object& o) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(" m_%1(new %2(false, this)),\n") .arg(p.name, p.type.name); } } } void initializeMembers(QTextStream& cpp, const QString& prefix, const Object& o, const Configuration& conf) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(" %1m_%2->m_d = %3_%4_get(%1m_d);\n") .arg(prefix, p.name, snakeCase(o.name), snakeCase(p.name)); initializeMembers(cpp, "m_" + p.name + "->", conf.findObject(p.type.name), conf); } } } void connect(QTextStream& cpp, const QString& d, const Object& o, const Configuration& conf) { for (auto p: o.properties) { if (p.type.type == BindingType::Object) { connect(cpp, d + "->m_" + p.name, conf.findObject(p.type.name), conf); } } if (o.type != ObjectType::Object) { cpp << QString(R"( connect(%2, &%1::newDataReady, %2, [this](const QModelIndex& i) { %2->fetchMore(i); }, Qt::QueuedConnection); )").arg(o.name, d); } } void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); cpp << QString("%1::%1(bool /*owned*/, QObject *parent):\n %2(parent),") .arg(o.name, baseType(o)) << endl; for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { cpp << QString(" m_%1(new %2(false, this)),\n") .arg(p.name, p.type.name); } } cpp << " m_d(0),\n m_ownsPrivate(false)\n{\n"; if (o.type != ObjectType::Object) { cpp << " initHeaderData();\n"; } cpp << QString("}\n\n%1::%1(QObject *parent):\n %2(parent),") .arg(o.name, baseType(o)) << endl; initializeMembersZero(cpp, o); cpp << QString(" m_d(%1_new(this").arg(lcname); constructorArgs(cpp, "", o, conf); cpp << ")),\n m_ownsPrivate(true)\n{\n"; initializeMembers(cpp, "", o, conf); connect(cpp, "this", o, conf); if (o.type != ObjectType::Object) { cpp << " initHeaderData();\n"; } cpp << QString(R"(} %1::~%1() { if (m_ownsPrivate) { %2_free(m_d); } } )").arg(o.name, lcname); if (o.type != ObjectType::Object) { cpp << QString("void %1::initHeaderData() {\n").arg(o.name); for (int col = 0; col < o.columnCount; ++col) { for (auto ip: o.itemProperties) { auto roles = ip.roles.value(col); if (roles.contains(Qt::DisplayRole)) { cpp << QString(" m_headerData.insert(qMakePair(%1, Qt::DisplayRole), QVariant(\"%2\"));\n").arg(QString::number(col), ip.name); } } } cpp << "}\n"; } for (const Property& p: o.properties) { const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name)); if (p.type.type == BindingType::Object) { cpp << QString(R"(const %3* %1::%2() const { return m_%2; } %3* %1::%2() { return m_%2; } )").arg(o.name, p.name, p.type.name); } else if (p.type.isComplex()) { cpp << QString("%3 %1::%2() const\n{\n").arg(o.name, p.name, p.type.name); cpp << " " << p.type.name << " v;\n"; cpp << " " << base << "_get(m_d, &v, set_" << p.type.name.toLower() << ");\n"; cpp << " return v;\n}\n"; } else { 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) { cpp << "void " << o.name << "::set" << upperInitial(p.name) << "(" << p.type.cppSetType << " v) {" << endl; if (p.optional) { cpp << QString(" if (v.isNull()) {") << 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 { 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 { qWarning() << p.type.cppSetType; 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(", %2%3.utf16(), %2%3.size()").arg(a->name, a + 1 < f.args.end() ? ", " : "")); } else { argList.append(QString(", %2%3").arg(a->name, a + 1 < f.args.end() ? ", " : "")); } } 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 { 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()) { cpp << QString(R"( struct option_%1 { public: %1 value; bool some; operator QVariant() const { if (some) { - return QVariant(value); + return QVariant::fromValue(value); } return QVariant(); } }; )").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) { v->truncate(0); v->append(bytes, nbytes); } )"; } if (conf.hasListOrTree()) { cpp << R"( struct qmodelindex_t { int row; quintptr id; }; )"; } for (auto o: conf.objects) { for (auto p: o.properties) { if (p.type.type == BindingType::Object) { continue; } cpp << " inline void " << changedF(o, p) << "(" << o.name << "* o)\n"; cpp << " {\n emit o->" << p.name << "Changed();\n }\n"; } } cpp << "}\n"; for (auto object: conf.objects) { if (object.type != ObjectType::Object) { writeCppModel(cpp, object); } cpp << "extern \"C\" {\n"; writeObjectCDecl(cpp, object, conf); cpp << "};\n" << endl; } for (auto object: conf.objects) { writeCppObject(cpp, object, conf); } } diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index 65c0df2..df22d67 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -1,238 +1,238 @@ /* 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(value); + return QVariant::fromValue(value); } return QVariant(); } }; 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; }; } 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 &parent) { return persons_insert_rows(m_d, row, count); } bool Persons::removeRows(int row, int count, const QModelIndex &parent) { 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::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; } QVariant Persons::userName(int row) const { QVariant v; QString s; persons_data_user_name(m_d, row, &s, set_qstring); if (!s.isNull()) v.setValue(s); return v; } bool Persons::setUserName(int row, const QVariant& value) { if (!value.canConvert(qMetaTypeId())) { return false; } bool set = false; const QString s = value.value(); set = persons_set_data_user_name(m_d, row, s.utf16(), s.length()); if (set) { QModelIndex index = createIndex(row, 0, row); emit dataChanged(index, index); } return set; } QVariant Persons::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 0: return userName(index.row()); } } return QVariant(); } 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) { return setUserName(index.row(), value); } } return false; } extern "C" { Persons::Private* persons_new(Persons*, void (*)(const Persons*), void (*)(Persons*, quintptr, quintptr), void (*)(Persons*), void (*)(Persons*), void (*)(Persons*, int, int), void (*)(Persons*), void (*)(Persons*, int, int), void (*)(Persons*)); void persons_free(Persons::Private*); }; Persons::Persons(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } Persons::Persons(QObject *parent): QAbstractItemModel(parent), m_d(persons_new(this, [](const Persons* o) { emit o->newDataReady(QModelIndex()); }, [](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) { 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.cpp b/tests/test_tree_rust.cpp index ad686d4..390c7d0 100644 --- a/tests/test_tree_rust.cpp +++ b/tests/test_tree_rust.cpp @@ -1,270 +1,270 @@ /* 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(value); + return QVariant::fromValue(value); } return QVariant(); } }; 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; }; } 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*, quintptr, bool); bool persons_can_fetch_more(const Persons::Private*, quintptr, bool); void persons_fetch_more(Persons::Private*, quintptr, bool); quintptr persons_index(const Persons::Private*, quintptr, bool, int); qmodelindex_t persons_parent(const Persons::Private*, quintptr); int persons_row(const Persons::Private*, quintptr); } 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; } return persons_row_count(m_d, parent.internalId(), parent.isValid()); } 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 quintptr id = persons_index(m_d, parent.internalId(), parent.isValid(), 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; } return persons_can_fetch_more(m_d, parent.internalId(), parent.isValid()); } void Persons::fetchMore(const QModelIndex &parent) { persons_fetch_more(m_d, parent.internalId(), parent.isValid()); } 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; } QVariant Persons::userName(const QModelIndex& index) const { QVariant v; QString s; persons_data_user_name(m_d, index.internalId(), &s, set_qstring); if (!s.isNull()) v.setValue(s); return v; } bool Persons::setUserName(const QModelIndex& index, const QVariant& value) { if (!value.canConvert(qMetaTypeId())) { return false; } bool set = false; const QString s = value.value(); set = persons_set_data_user_name(m_d, index.internalId(), s.utf16(), s.length()); if (set) { emit dataChanged(index, index); } return set; } QVariant Persons::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: switch (role) { case Qt::DisplayRole: case Qt::EditRole: case Qt::UserRole + 0: return userName(index); } } return QVariant(); } 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) { return setUserName(index, value); } } return false; } extern "C" { Persons::Private* persons_new(Persons*, void (*)(const Persons*, quintptr, bool), void (*)(Persons*, quintptr, quintptr), void (*)(Persons*), void (*)(Persons*), void (*)(Persons*, option_quintptr, int, int), void (*)(Persons*), void (*)(Persons*, option_quintptr, int, int), void (*)(Persons*)); void persons_free(Persons::Private*); }; Persons::Persons(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), m_ownsPrivate(false) { initHeaderData(); } Persons::Persons(QObject *parent): QAbstractItemModel(parent), m_d(persons_new(this, [](const Persons* o, quintptr id, bool valid) { if (valid) { int row = persons_row(o->m_d, id); emit o->newDataReady(o->createIndex(row, 0, id)); } else { emit o->newDataReady(QModelIndex()); } }, [](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 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")); }