mirror of
https://github.com/slint-ui/slint.git
synced 2025-07-24 05:26:29 +00:00
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`
This commit is contained in:
parent
107c68e35a
commit
a4313c3e32
3 changed files with 39 additions and 20 deletions
|
@ -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<typename ModelData>
|
|||
struct SortModelInner : private_api::ModelChangeListener
|
||||
{
|
||||
SortModelInner(std::shared_ptr<slint::Model<ModelData>> source_model,
|
||||
std::function<bool(const ModelData &, const ModelData &)> sort_fn,
|
||||
std::function<bool(const ModelData &, const ModelData &)> comp,
|
||||
slint::SortModel<ModelData> &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<slint::Model<ModelData>> source_model;
|
||||
std::function<bool(const ModelData &, const ModelData &)> sort_fn;
|
||||
std::function<bool(const ModelData &, const ModelData &)> comp;
|
||||
slint::SortModel<ModelData> &target_model;
|
||||
std::vector<int> sorted_rows;
|
||||
bool sorted_rows_dirty = true;
|
||||
|
@ -1195,11 +1195,11 @@ class SortModel : public Model<ModelData>
|
|||
|
||||
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<Model<ModelData>> source_model,
|
||||
std::function<bool(const ModelData &, const ModelData &)> sort_fn)
|
||||
std::function<bool(const ModelData &, const ModelData &)> comp)
|
||||
: inner(std::make_shared<private_api::SortModelInner<ModelData>>(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.
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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<M, S>(Pin<Box<ModelChangeListenerContainer<SortModelInner<M, S>>>>)
|
||||
pub struct SortModel<M, F>(Pin<Box<ModelChangeListenerContainer<SortModelInner<M, F>>>>)
|
||||
where
|
||||
M: Model + 'static,
|
||||
S: SortHelper<M::Data>;
|
||||
F: SortHelper<M::Data>;
|
||||
|
||||
impl<M, F> SortModel<M, F>
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue