From a4313c3e3208bb34dccd0614a1538a2da68bcc4b Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Wed, 26 Oct 2022 11:16:12 +0200 Subject: [PATCH] API changes for SortModel and FilterModel (rust and cpp) (#1768) * Rust SortModel: Rename parameter `S` to `F` * Rust SortModel: Rename parameter `apply_sorting` to `reset` * Rust FilterModel: Rename parameter `apply_filter` to `reset` * C++ SortModel: Rename parameter `sort_fn` to `comp` * C++ SortModel: Add pub reset function * C++ FilterModel: Rename parameter `apply_filter` to `reset` --- api/cpp/include/slint.h | 23 +++++++++++----------- examples/7guis/crud.rs | 2 +- internal/core/model/adapters.rs | 34 ++++++++++++++++++++++++++------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/api/cpp/include/slint.h b/api/cpp/include/slint.h index b816ed4a1..f9f1873f9 100644 --- a/api/cpp/include/slint.h +++ b/api/cpp/include/slint.h @@ -975,7 +975,7 @@ public: /// Re-applies the model's filter function on each row of the source model. Use this if state /// external to the filter function has changed. - void apply_filter() { inner->reset(); } + void reset() { inner->reset(); } /// Given the \a filtered_row index, this function returns the corresponding row index in the /// source model. @@ -1063,9 +1063,9 @@ template struct SortModelInner : private_api::ModelChangeListener { SortModelInner(std::shared_ptr> source_model, - std::function sort_fn, + std::function comp, slint::SortModel &target_model) - : source_model(source_model), sort_fn(sort_fn), target_model(target_model) + : source_model(source_model), comp(comp), target_model(target_model) { } @@ -1089,7 +1089,7 @@ struct SortModelInner : private_api::ModelChangeListener std::lower_bound(sorted_rows.begin(), sorted_rows.end(), inserted_value, [this](int sorted_row, const ModelData &inserted_value) { auto sorted_elem = source_model->row_data(sorted_row); - return sort_fn(*sorted_elem, inserted_value); + return comp(*sorted_elem, inserted_value); }); insertion_point = sorted_rows.insert(insertion_point, row); @@ -1112,7 +1112,7 @@ struct SortModelInner : private_api::ModelChangeListener std::lower_bound(sorted_rows.begin(), sorted_rows.end(), changed_value, [this](int sorted_row, const ModelData &changed_value) { auto sorted_elem = source_model->row_data(sorted_row); - return sort_fn(*sorted_elem, changed_value); + return comp(*sorted_elem, changed_value); }); insertion_point = sorted_rows.insert(insertion_point, changed_row); @@ -1171,14 +1171,14 @@ struct SortModelInner : private_api::ModelChangeListener std::sort(sorted_rows.begin(), sorted_rows.end(), [this](int lhs_index, int rhs_index) { auto lhs_elem = source_model->row_data(lhs_index); auto rhs_elem = source_model->row_data(rhs_index); - return sort_fn(*lhs_elem, *rhs_elem); + return comp(*lhs_elem, *rhs_elem); }); sorted_rows_dirty = false; } std::shared_ptr> source_model; - std::function sort_fn; + std::function comp; slint::SortModel &target_model; std::vector sorted_rows; bool sorted_rows_dirty = true; @@ -1195,11 +1195,11 @@ class SortModel : public Model public: /// Constructs a new SortModel that provides a sorted view on the \a source_model by applying - /// the order given by the specified \a sort_fn. + /// the order given by the specified \a comp. SortModel(std::shared_ptr> source_model, - std::function sort_fn) + std::function comp) : inner(std::make_shared>(std::move(source_model), - std::move(sort_fn), *this)) + std::move(comp), *this)) { inner->source_model->attach_peer(inner); } @@ -1216,10 +1216,9 @@ public: { inner->source_model->set_row_data(inner->sorted_rows[i], value); } - /// Re-applies the model's sort function on each row of the source model. Use this if state /// external to the sort function has changed. - void sort() { inner->reset(); } + void reset() { inner->reset(); } /// Given the \a sorted_row_index, this function returns the corresponding row index in the /// source model. diff --git a/examples/7guis/crud.rs b/examples/7guis/crud.rs index 6050f11ab..e210947eb 100644 --- a/examples/7guis/crud.rs +++ b/examples/7guis/crud.rs @@ -80,7 +80,7 @@ pub fn main() { main_window.on_prefixEdited(move || { let main_window = main_window_weak.unwrap(); *prefix.borrow_mut() = main_window.get_prefix(); - filtered_model.apply_filter(); + filtered_model.reset(); }); } diff --git a/internal/core/model/adapters.rs b/internal/core/model/adapters.rs index 9742b65fb..0ec2c2cd5 100644 --- a/internal/core/model/adapters.rs +++ b/internal/core/model/adapters.rs @@ -9,6 +9,10 @@ use super::*; /// /// When the other Model is updated, the `MapModel` is updated accordingly. /// +/// Generic parameters: +/// * `M` the type of the wrapped `Model`. +/// * `F` the map function. +/// /// ## Example /// /// Here we have a [`VecModel`] holding rows of a custom type `Name`. @@ -260,6 +264,10 @@ where /// /// When the other Model is updated, the `FilterModel` is updated accordingly. /// +/// Generic parameters: +/// * `M` the type of the wrapped `Model`. +/// * `F` the filter function. +/// /// ## Example /// /// Here we have a [`VecModel`] holding [`SharedString`]s. @@ -341,11 +349,19 @@ where Self(container) } - /// Manually reapply the filter. You need to run this e.g. if the filtering function compares - /// against mutable state and it has changed. + /// Manually reapply the filter. You need to run this e.g. if the filtering function depends on + /// mutable state and it has changed. This method is deprecated use `reset` instead. + #[deprecated(note = "Use reset() instead")] pub fn apply_filter(&self) { + self.reset() + } + + /// Manually reapply the filter. You need to run this e.g. if the filtering function depends on + /// mutable state and it has changed. + pub fn reset(&self) { self.0.reset(); } + /// Gets the row index of the underlying unfiltered model for a given filtered row index. pub fn unfiltered_row(&self, filtered_row: usize) -> usize { self.0.mapping.borrow()[filtered_row] @@ -596,6 +612,10 @@ where /// /// When the other Model is updated, the `Sorted` is updated accordingly. /// +/// Generic parameters: +/// * `M` the type of the wrapped `Model`. +/// * `F` a type that provides an order to model rows. It is constrained by the internal trait `SortHelper`, which is used to sort the model in ascending order if the model data supports it, or by a given sort function. +/// /// ## Example /// /// Here we have a [`VecModel`] holding [`SharedString`]s. @@ -681,10 +701,10 @@ where /// assert_eq!(sorted_model.row_data(1).unwrap(), SharedString::from("Lorem")); /// assert_eq!(sorted_model.row_data(2).unwrap(), SharedString::from("opsom")); /// ``` -pub struct SortModel(Pin>>>) +pub struct SortModel(Pin>>>) where M: Model + 'static, - S: SortHelper; + F: SortHelper; impl SortModel where @@ -739,9 +759,9 @@ where Self(container) } - /// Manually reapply the sorting. You need to run this e.g. if the sort function compares - /// against mutable state and it has changed. - pub fn apply_sorting(&self) { + /// Manually reapply the sorting. You need to run this e.g. if the sort function depends + /// on mutable state and it has changed. + pub fn reset(&self) { self.0.reset(); }