Add roleNames

master
Jos van den Oever 2017-08-11 00:15:46 +02:00
parent 630bf4b858
commit bced2e9ba3
4 changed files with 30 additions and 18 deletions

View File

@ -6,7 +6,7 @@
"implementationModule": "testimplementation" "implementationModule": "testimplementation"
}, },
"objects": [{ "objects": [{
"name": "Test", "name": "Person",
"type": "Object", "type": "Object",
"properties": [{ "properties": [{
"name": "userName", "name": "userName",
@ -33,7 +33,8 @@
"type": "List", "type": "List",
"properties": [{ "properties": [{
"name": "path", "name": "path",
"type": "QString" "type": "QString",
"write": true
}], }],
"roles": [{ "roles": [{
"name": "FileIcon", "name": "FileIcon",

View File

@ -2,8 +2,8 @@ use libc::c_int;
use types::*; use types::*;
use testinterface::*; use testinterface::*;
pub struct Test { pub struct Person {
emit: TestEmitter, emit: PersonEmitter,
user_name: String, user_name: String,
age: c_int, age: c_int,
active: bool, active: bool,
@ -11,9 +11,9 @@ pub struct Test {
icon: Vec<u8>, icon: Vec<u8>,
} }
impl TestTrait for Test { impl PersonTrait for Person {
fn create(emit: TestEmitter) -> Test { fn create(emit: PersonEmitter) -> Person {
Test { Person {
emit: emit, emit: emit,
user_name: String::new(), user_name: String::new(),
age: 0, age: 0,
@ -22,7 +22,7 @@ impl TestTrait for Test {
icon: Vec::new(), icon: Vec::new(),
} }
} }
fn emit(&self) -> &TestEmitter { fn emit(&self) -> &PersonEmitter {
&self.emit &self.emit
} }
fn get_user_name(&self) -> String { fn get_user_name(&self) -> String {
@ -77,8 +77,12 @@ impl DirectoryTrait for Directory {
fn get_path(&self) -> String { fn get_path(&self) -> String {
self.path.clone() self.path.clone()
} }
fn set_path(&mut self, value: String) {
self.path = value;
self.emit.path_changed();
}
fn row_count(&self) -> c_int { fn row_count(&self) -> c_int {
0 10
} }
fn file_icon(&self, row: c_int) -> Variant { fn file_icon(&self, row: c_int) -> Variant {
Variant::Bool(row > 0) Variant::Bool(row > 0)

View File

@ -156,6 +156,7 @@ void writeHeaderItemModel(QTextStream& h, const Object& o) {
QModelIndex index(int row, int column, const QModelIndex &parent) const; QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &index) const; QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
QHash<int, QByteArray> roleNames() const;
signals: signals:
void newDataReady(); void newDataReady();
)"); )");
@ -180,12 +181,12 @@ void writeCppListModel(QTextStream& cpp, const Object& o) {
} }
int %1::columnCount(const QModelIndex &parent) const int %1::columnCount(const QModelIndex &parent) const
{ {
return parent.isValid() ? 0 : 1; return (parent.isValid()) ? 0 : 1;
} }
int %1::rowCount(const QModelIndex &parent) const 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 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 << " break;\n";
} }
cpp << " }\n return v;\n}\n"; cpp << " }\n return v;\n}\n";
cpp << "QHash<int, QByteArray> " << o.name << "::roleNames() const {\n";
cpp << " QHash<int, QByteArray> 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) { void writeHeaderObject(QTextStream& h, const Object& o) {
@ -801,7 +809,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
} }
} }
if (o.type == ObjectTypeList) { 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) { for (auto role: o.roles) {
r << QString(" fn %1(&self, row: c_int) -> Variant {\n") r << QString(" fn %1(&self, row: c_int) -> Variant {\n")
.arg(snakeCase(role.name)); .arg(snakeCase(role.name));

View File

@ -9,7 +9,9 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QTreeView> #include <QTreeView>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QtQml/qqml.h>
#include <QQmlContext> #include <QQmlContext>
#include <QDebug>
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
@ -48,21 +50,18 @@ int main (int argc, char *argv[])
parser.process(app); parser.process(app);
aboutData.processCommandLine(&parser); aboutData.processCommandLine(&parser);
qmlRegisterType<Directory>("rust", 1, 0, "Directory");
qmlRegisterType<Person>("rust", 1, 0, "Person");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
RItemModel model; RItemModel model;
QTreeView view; QTreeView view;
view.setAnimated(true);
view.setUniformRowHeights(true); view.setUniformRowHeights(true);
view.setModel(&model); view.setModel(&model);
view.show(); view.show();
/*
*/
Directory directory;
engine.rootContext()->setContextProperty("fsModel", &model); engine.rootContext()->setContextProperty("fsModel", &model);
engine.rootContext()->setContextProperty("directory", &directory);
engine.load(QUrl(QStringLiteral("test.qml"))); engine.load(QUrl(QStringLiteral("test.qml")));
return app.exec(); return app.exec();