Expose the sort() function to rust

master
Jos van den Oever 2017-08-15 23:12:43 +02:00
parent 2f87e2c74c
commit 27cc22ddc2
8 changed files with 57 additions and 1 deletions

View File

@ -77,6 +77,7 @@ pub trait TreeTrait {
fn row_count(&self, row: c_int, parent: usize) -> c_int;
fn can_fetch_more(&self, c_int, usize) -> bool { false }
fn fetch_more(&mut self, c_int, usize) {}
fn sort(&mut self, c_int, SortOrder) {}
fn file_name(&self, row: c_int, parent: usize) -> String;
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
@ -150,6 +151,10 @@ pub unsafe extern "C" fn tree_can_fetch_more(ptr: *const Tree, row: c_int, paren
pub unsafe extern "C" fn tree_fetch_more(ptr: *mut Tree, row: c_int, parent: usize) {
(&mut *ptr).fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_sort(ptr: *mut Tree, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_name(ptr: *const Tree,

View File

@ -196,6 +196,7 @@ pub trait DirectoryTrait {
fn row_count(&self) -> c_int;
fn can_fetch_more(&self) -> bool { false }
fn fetch_more(&mut self) {}
fn sort(&mut self, c_int, SortOrder) {}
fn file_name(&self, row: c_int) -> String;
fn file_icon(&self, row: c_int) -> Vec<u8>;
fn file_path(&self, row: c_int) -> String;
@ -265,6 +266,10 @@ pub unsafe extern "C" fn directory_can_fetch_more(ptr: *const Directory) -> bool
pub unsafe extern "C" fn directory_fetch_more(ptr: *mut Directory) {
(&mut *ptr).fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn directory_sort(ptr: *mut Directory, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn directory_data_file_name(ptr: *const Directory,
@ -366,6 +371,7 @@ pub trait TestTreeTrait {
fn row_count(&self, row: c_int, parent: usize) -> c_int;
fn can_fetch_more(&self, c_int, usize) -> bool { false }
fn fetch_more(&mut self, c_int, usize) {}
fn sort(&mut self, c_int, SortOrder) {}
fn file_name(&self, row: c_int, parent: usize) -> String;
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
@ -437,6 +443,10 @@ pub unsafe extern "C" fn test_tree_can_fetch_more(ptr: *const TestTree, row: c_i
pub unsafe extern "C" fn test_tree_fetch_more(ptr: *mut TestTree, row: c_int, parent: usize) {
(&mut *ptr).fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn test_tree_sort(ptr: *mut TestTree, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn test_tree_data_file_name(ptr: *const TestTree,

View File

@ -74,3 +74,9 @@ impl QModelIndex {
}
}
#[repr(C)]
pub enum SortOrder {
Ascending = 0,
Descending = 1
}

View File

@ -164,6 +164,7 @@ extern "C" {
void directory_data_file_icon(const Directory::Private*, int, QByteArray*, qbytearray_set);
void directory_data_file_path(const Directory::Private*, int, QString*, qstring_set);
int directory_data_file_permissions(const Directory::Private*, int);
void directory_sort(Directory::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int directory_row_count(const Directory::Private*);
bool directory_can_fetch_more(const Directory::Private*);
@ -209,6 +210,10 @@ void Directory::fetchMore(const QModelIndex &parent)
}
}
void Directory::sort(int column, Qt::SortOrder order)
{
directory_sort(d, column, order);
}
QVariant Directory::data(const QModelIndex &index, int role) const
{
QVariant v;
@ -314,6 +319,7 @@ extern "C" {
void test_tree_data_file_icon(const TestTree::Private*, int, quintptr, QByteArray*, qbytearray_set);
void test_tree_data_file_path(const TestTree::Private*, int, quintptr, QString*, qstring_set);
int test_tree_data_file_permissions(const TestTree::Private*, int, quintptr);
void test_tree_sort(TestTree::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int test_tree_row_count(const TestTree::Private*, int, quintptr);
bool test_tree_can_fetch_more(const TestTree::Private*, int, quintptr);
@ -373,6 +379,10 @@ void TestTree::fetchMore(const QModelIndex &parent)
test_tree_fetch_more(d, parent.row(), parent.internalId());
}
void TestTree::sort(int column, Qt::SortOrder order)
{
test_tree_sort(d, column, order);
}
QVariant TestTree::data(const QModelIndex &index, int role) const
{
QVariant v;

View File

@ -60,6 +60,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()
@ -92,6 +93,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()

View File

@ -106,6 +106,7 @@ extern "C" {
int tree_data_file_permissions(const Tree::Private*, int, quintptr);
int tree_data_file_type(const Tree::Private*, int, quintptr);
qulonglong tree_data_file_size(const Tree::Private*, int, quintptr);
void tree_sort(Tree::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int tree_row_count(const Tree::Private*, int, quintptr);
bool tree_can_fetch_more(const Tree::Private*, int, quintptr);
@ -165,6 +166,10 @@ void Tree::fetchMore(const QModelIndex &parent)
tree_fetch_more(d, parent.row(), parent.internalId());
}
void Tree::sort(int column, Qt::SortOrder order)
{
tree_sort(d, column, order);
}
QVariant Tree::data(const QModelIndex &index, int role) const
{
QVariant v;

View File

@ -27,6 +27,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()

View File

@ -280,6 +280,7 @@ void writeHeaderItemModel(QTextStream& h) {
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()
@ -306,6 +307,7 @@ void writeCppModel(QTextStream& cpp, const Object& o) {
.arg(o.name, lcname, snakeCase(role.name), role.type.cppSetType, indexDecl);
}
}
cpp << QString(" void %2_sort(%1::Private*, int 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*);
@ -415,13 +417,17 @@ void %1::fetchMore(const QModelIndex &parent)
}
cpp << QString(R"(
void %1::sort(int column, Qt::SortOrder order)
{
%2_sort(d, column, order);
}
QVariant %1::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
QByteArray b;
switch (index.column()) {
)").arg(o.name);
)").arg(o.name, lcname);
for (int col = 0; col < o.columnRoles.size(); ++col) {
auto roles = o.columnRoles[col];
@ -880,6 +886,7 @@ pub trait %1Trait {
}
r << QString(R"( fn can_fetch_more(&self%1) -> bool { false }
fn fetch_more(&mut self%1) {}
fn sort(&mut self, c_int, SortOrder) {}
)").arg(index);
if (o.type == ObjectType::List) {
index = ", row: c_int";
@ -1023,6 +1030,10 @@ pub unsafe extern "C" fn %2_can_fetch_more(ptr: *const %1%3) -> bool {
pub unsafe extern "C" fn %2_fetch_more(ptr: *mut %1%3) {
(&mut *ptr).fetch_more(%4)
}
#[no_mangle]
pub unsafe extern "C" fn %2_sort(ptr: *mut %1, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
)").arg(o.name, lcname, indexDecl, index);
if (o.type == ObjectType::UniformTree) {
indexDecl = ", parent: usize";
@ -1268,6 +1279,12 @@ impl QModelIndex {
}
}
#[repr(C)]
pub enum SortOrder {
Ascending = 0,
Descending = 1
}
)");
}