From bced2e9ba3db6da2a6640a0835dd818eb61bb412 Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Fri, 11 Aug 2017 00:15:46 +0200 Subject: [PATCH] Add roleNames --- bindings.json | 5 +++-- common-rust/src/testimplementation.rs | 18 +++++++++++------- .../rust_qt_binding_generator.cpp | 14 +++++++++++--- src/main.cpp | 11 +++++------ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/bindings.json b/bindings.json index 99d9719..1ae77de 100644 --- a/bindings.json +++ b/bindings.json @@ -6,7 +6,7 @@ "implementationModule": "testimplementation" }, "objects": [{ - "name": "Test", + "name": "Person", "type": "Object", "properties": [{ "name": "userName", @@ -33,7 +33,8 @@ "type": "List", "properties": [{ "name": "path", - "type": "QString" + "type": "QString", + "write": true }], "roles": [{ "name": "FileIcon", diff --git a/common-rust/src/testimplementation.rs b/common-rust/src/testimplementation.rs index fa73055..8ef1af5 100644 --- a/common-rust/src/testimplementation.rs +++ b/common-rust/src/testimplementation.rs @@ -2,8 +2,8 @@ use libc::c_int; use types::*; use testinterface::*; -pub struct Test { - emit: TestEmitter, +pub struct Person { + emit: PersonEmitter, user_name: String, age: c_int, active: bool, @@ -11,9 +11,9 @@ pub struct Test { icon: Vec, } -impl TestTrait for Test { - fn create(emit: TestEmitter) -> Test { - Test { +impl PersonTrait for Person { + fn create(emit: PersonEmitter) -> Person { + Person { emit: emit, user_name: String::new(), age: 0, @@ -22,7 +22,7 @@ impl TestTrait for Test { icon: Vec::new(), } } - fn emit(&self) -> &TestEmitter { + fn emit(&self) -> &PersonEmitter { &self.emit } fn get_user_name(&self) -> String { @@ -77,8 +77,12 @@ impl DirectoryTrait for Directory { fn get_path(&self) -> String { self.path.clone() } + fn set_path(&mut self, value: String) { + self.path = value; + self.emit.path_changed(); + } fn row_count(&self) -> c_int { - 0 + 10 } fn file_icon(&self, row: c_int) -> Variant { Variant::Bool(row > 0) diff --git a/rust_qt_binding_generator/rust_qt_binding_generator.cpp b/rust_qt_binding_generator/rust_qt_binding_generator.cpp index a5c77f5..2f38ea3 100644 --- a/rust_qt_binding_generator/rust_qt_binding_generator.cpp +++ b/rust_qt_binding_generator/rust_qt_binding_generator.cpp @@ -156,6 +156,7 @@ void writeHeaderItemModel(QTextStream& h, const Object& o) { QModelIndex index(int row, int column, const QModelIndex &parent) const; QModelIndex parent(const QModelIndex &index) const; int rowCount(const QModelIndex &parent) const; + QHash roleNames() const; signals: void newDataReady(); )"); @@ -180,12 +181,12 @@ void writeCppListModel(QTextStream& cpp, const Object& o) { } int %1::columnCount(const QModelIndex &parent) const { - return parent.isValid() ? 0 : 1; + return (parent.isValid()) ? 0 : 1; } int %1::rowCount(const QModelIndex &parent) const { - return %2_row_count(d, parent); + return (parent.isValid()) ? 0 : %2_row_count(d, parent); } QModelIndex %1::index(int row, int column, const QModelIndex &parent) const @@ -214,6 +215,13 @@ QVariant %1::data(const QModelIndex &index, int role) const cpp << " break;\n"; } cpp << " }\n return v;\n}\n"; + cpp << "QHash " << o.name << "::roleNames() const {\n"; + cpp << " QHash names;\n"; + for (auto role: o.roles) { + cpp << " names.insert(" << role.value << ", \"" << role.name << "\");\n"; + } + cpp << " return names;\n"; + cpp << "}"; } void writeHeaderObject(QTextStream& h, const Object& o) { @@ -801,7 +809,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) { } } if (o.type == ObjectTypeList) { - r << " fn row_count(&self) -> c_int {\n 0\n }\n"; + r << " fn row_count(&self) -> c_int {\n 10\n }\n"; for (auto role: o.roles) { r << QString(" fn %1(&self, row: c_int) -> Variant {\n") .arg(snakeCase(role.name)); diff --git a/src/main.cpp b/src/main.cpp index a97874c..37efb4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,9 @@ #include #include #include +#include #include +#include int main (int argc, char *argv[]) { @@ -48,21 +50,18 @@ int main (int argc, char *argv[]) parser.process(app); aboutData.processCommandLine(&parser); + qmlRegisterType("rust", 1, 0, "Directory"); + qmlRegisterType("rust", 1, 0, "Person"); + QQmlApplicationEngine engine; RItemModel model; QTreeView view; - view.setAnimated(true); view.setUniformRowHeights(true); view.setModel(&model); view.show(); -/* -*/ - - Directory directory; engine.rootContext()->setContextProperty("fsModel", &model); - engine.rootContext()->setContextProperty("directory", &directory); engine.load(QUrl(QStringLiteral("test.qml"))); return app.exec();