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"
},
"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",

View File

@ -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<u8>,
}
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)

View File

@ -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<int, QByteArray> 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<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) {
@ -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));

View File

@ -9,7 +9,9 @@
#include <QCommandLineParser>
#include <QTreeView>
#include <QQmlApplicationEngine>
#include <QtQml/qqml.h>
#include <QQmlContext>
#include <QDebug>
int main (int argc, char *argv[])
{
@ -48,21 +50,18 @@ int main (int argc, char *argv[])
parser.process(app);
aboutData.processCommandLine(&parser);
qmlRegisterType<Directory>("rust", 1, 0, "Directory");
qmlRegisterType<Person>("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();