gtk4/auto/
sort_list_model.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5#[cfg(feature = "v4_12")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
7use crate::SectionModel;
8use crate::{ffi, Sorter};
9use glib::{
10    prelude::*,
11    signal::{connect_raw, SignalHandlerId},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16#[cfg(feature = "v4_12")]
17#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
18glib::wrapper! {
19    /// A `GListModel` that sorts the elements of an underlying model
20    /// according to a [`Sorter`][crate::Sorter].
21    ///
22    /// The model is a stable sort. If two items compare equal according
23    /// to the sorter, the one that appears first in the original model will
24    /// also appear first after sorting.
25    /// Note that if you change the sorter, the previous order will have no
26    /// influence on the new order. If you want that, consider using a
27    /// [`MultiSorter`][crate::MultiSorter] and appending the previous sorter to it.
28    ///
29    /// The model can be set up to do incremental sorting, so that
30    /// sorting long lists doesn't block the UI. See
31    /// [`set_incremental()`][Self::set_incremental()] for details.
32    ///
33    /// [`SortListModel`][crate::SortListModel] is a generic model and because of that it
34    /// cannot take advantage of any external knowledge when sorting.
35    /// If you run into performance issues with [`SortListModel`][crate::SortListModel],
36    /// it is strongly recommended that you write your own sorting list
37    /// model.
38    ///
39    /// [`SortListModel`][crate::SortListModel] allows sorting the items into sections. It
40    /// implements [`SectionModel`][crate::SectionModel] and when [`section-sorter`][struct@crate::SortListModel#section-sorter]
41    /// is set, it will sort all items with that sorter and items comparing
42    /// equal with it will be put into the same section.
43    /// The [`sorter`][struct@crate::SortListModel#sorter] will then be used to sort items
44    /// inside their sections.
45    ///
46    /// ## Properties
47    ///
48    ///
49    /// #### `incremental`
50    ///  If the model should sort items incrementally.
51    ///
52    /// Readable | Writeable
53    ///
54    ///
55    /// #### `item-type`
56    ///  The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
57    ///
58    /// Readable
59    ///
60    ///
61    /// #### `model`
62    ///  The model being sorted.
63    ///
64    /// Readable | Writeable
65    ///
66    ///
67    /// #### `n-items`
68    ///  The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
69    ///
70    /// Readable
71    ///
72    ///
73    /// #### `pending`
74    ///  Estimate of unsorted items remaining.
75    ///
76    /// Readable
77    ///
78    ///
79    /// #### `section-sorter`
80    ///  The section sorter for this model, if one is set.
81    ///
82    /// Readable | Writeable
83    ///
84    ///
85    /// #### `sorter`
86    ///  The sorter for this model.
87    ///
88    /// Readable | Writeable
89    ///
90    /// # Implements
91    ///
92    /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`SectionModelExt`][trait@crate::prelude::SectionModelExt]
93    #[doc(alias = "GtkSortListModel")]
94    pub struct SortListModel(Object<ffi::GtkSortListModel, ffi::GtkSortListModelClass>) @implements gio::ListModel, SectionModel;
95
96    match fn {
97        type_ => || ffi::gtk_sort_list_model_get_type(),
98    }
99}
100
101#[cfg(not(any(feature = "v4_12")))]
102glib::wrapper! {
103    #[doc(alias = "GtkSortListModel")]
104    pub struct SortListModel(Object<ffi::GtkSortListModel, ffi::GtkSortListModelClass>) @implements gio::ListModel;
105
106    match fn {
107        type_ => || ffi::gtk_sort_list_model_get_type(),
108    }
109}
110
111impl SortListModel {
112    /// Creates a new sort list model that uses the @sorter to sort @model.
113    /// ## `model`
114    /// the model to sort
115    /// ## `sorter`
116    /// the [`Sorter`][crate::Sorter] to sort @model with,
117    ///
118    /// # Returns
119    ///
120    /// a new [`SortListModel`][crate::SortListModel]
121    #[doc(alias = "gtk_sort_list_model_new")]
122    pub fn new(
123        model: Option<impl IsA<gio::ListModel>>,
124        sorter: Option<impl IsA<Sorter>>,
125    ) -> SortListModel {
126        assert_initialized_main_thread!();
127        unsafe {
128            from_glib_full(ffi::gtk_sort_list_model_new(
129                model.map(|p| p.upcast()).into_glib_ptr(),
130                sorter.map(|p| p.upcast()).into_glib_ptr(),
131            ))
132        }
133    }
134
135    // rustdoc-stripper-ignore-next
136    /// Creates a new builder-pattern struct instance to construct [`SortListModel`] objects.
137    ///
138    /// This method returns an instance of [`SortListModelBuilder`](crate::builders::SortListModelBuilder) which can be used to create [`SortListModel`] objects.
139    pub fn builder() -> SortListModelBuilder {
140        SortListModelBuilder::new()
141    }
142
143    /// Returns whether incremental sorting is enabled.
144    ///
145    /// See [`set_incremental()`][Self::set_incremental()].
146    ///
147    /// # Returns
148    ///
149    /// [`true`] if incremental sorting is enabled
150    #[doc(alias = "gtk_sort_list_model_get_incremental")]
151    #[doc(alias = "get_incremental")]
152    #[doc(alias = "incremental")]
153    pub fn is_incremental(&self) -> bool {
154        unsafe {
155            from_glib(ffi::gtk_sort_list_model_get_incremental(
156                self.to_glib_none().0,
157            ))
158        }
159    }
160
161    /// Gets the model currently sorted or [`None`] if none.
162    ///
163    /// # Returns
164    ///
165    /// The model that gets sorted
166    #[doc(alias = "gtk_sort_list_model_get_model")]
167    #[doc(alias = "get_model")]
168    pub fn model(&self) -> Option<gio::ListModel> {
169        unsafe { from_glib_none(ffi::gtk_sort_list_model_get_model(self.to_glib_none().0)) }
170    }
171
172    /// Estimates progress of an ongoing sorting operation.
173    ///
174    /// The estimate is the number of items that would still need to be
175    /// sorted to finish the sorting operation if this was a linear
176    /// algorithm. So this number is not related to how many items are
177    /// already correctly sorted.
178    ///
179    /// If you want to estimate the progress, you can use code like this:
180    /// **⚠️ The following code is in c ⚠️**
181    ///
182    /// ```c
183    /// pending = gtk_sort_list_model_get_pending (self);
184    /// model = gtk_sort_list_model_get_model (self);
185    /// progress = 1.0 - pending / (double) MAX (1, g_list_model_get_n_items (model));
186    /// ```
187    ///
188    /// If no sort operation is ongoing - in particular when
189    /// [`incremental`][struct@crate::SortListModel#incremental] is [`false`] - this
190    /// function returns 0.
191    ///
192    /// # Returns
193    ///
194    /// a progress estimate of remaining items to sort
195    #[doc(alias = "gtk_sort_list_model_get_pending")]
196    #[doc(alias = "get_pending")]
197    pub fn pending(&self) -> u32 {
198        unsafe { ffi::gtk_sort_list_model_get_pending(self.to_glib_none().0) }
199    }
200
201    /// Gets the section sorter that is used to sort items of @self into
202    /// sections.
203    ///
204    /// # Returns
205    ///
206    /// the sorter of #self
207    #[cfg(feature = "v4_12")]
208    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
209    #[doc(alias = "gtk_sort_list_model_get_section_sorter")]
210    #[doc(alias = "get_section_sorter")]
211    #[doc(alias = "section-sorter")]
212    pub fn section_sorter(&self) -> Option<Sorter> {
213        unsafe {
214            from_glib_none(ffi::gtk_sort_list_model_get_section_sorter(
215                self.to_glib_none().0,
216            ))
217        }
218    }
219
220    /// Gets the sorter that is used to sort @self.
221    ///
222    /// # Returns
223    ///
224    /// the sorter of #self
225    #[doc(alias = "gtk_sort_list_model_get_sorter")]
226    #[doc(alias = "get_sorter")]
227    pub fn sorter(&self) -> Option<Sorter> {
228        unsafe { from_glib_none(ffi::gtk_sort_list_model_get_sorter(self.to_glib_none().0)) }
229    }
230
231    /// Sets the sort model to do an incremental sort.
232    ///
233    /// When incremental sorting is enabled, the [`SortListModel`][crate::SortListModel] will not do
234    /// a complete sort immediately, but will instead queue an idle handler that
235    /// incrementally sorts the items towards their correct position. This of
236    /// course means that items do not instantly appear in the right place. It
237    /// also means that the total sorting time is a lot slower.
238    ///
239    /// When your filter blocks the UI while sorting, you might consider
240    /// turning this on. Depending on your model and sorters, this may become
241    /// interesting around 10,000 to 100,000 items.
242    ///
243    /// By default, incremental sorting is disabled.
244    ///
245    /// See [`pending()`][Self::pending()] for progress information
246    /// about an ongoing incremental sorting operation.
247    /// ## `incremental`
248    /// [`true`] to sort incrementally
249    #[doc(alias = "gtk_sort_list_model_set_incremental")]
250    #[doc(alias = "incremental")]
251    pub fn set_incremental(&self, incremental: bool) {
252        unsafe {
253            ffi::gtk_sort_list_model_set_incremental(
254                self.to_glib_none().0,
255                incremental.into_glib(),
256            );
257        }
258    }
259
260    /// Sets the model to be sorted.
261    ///
262    /// The @model's item type must conform to the item type of @self.
263    /// ## `model`
264    /// The model to be sorted
265    #[doc(alias = "gtk_sort_list_model_set_model")]
266    #[doc(alias = "model")]
267    pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
268        unsafe {
269            ffi::gtk_sort_list_model_set_model(
270                self.to_glib_none().0,
271                model.map(|p| p.as_ref()).to_glib_none().0,
272            );
273        }
274    }
275
276    /// Sets a new section sorter on @self.
277    /// ## `sorter`
278    /// the [`Sorter`][crate::Sorter] to sort @model with
279    #[cfg(feature = "v4_12")]
280    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
281    #[doc(alias = "gtk_sort_list_model_set_section_sorter")]
282    #[doc(alias = "section-sorter")]
283    pub fn set_section_sorter(&self, sorter: Option<&impl IsA<Sorter>>) {
284        unsafe {
285            ffi::gtk_sort_list_model_set_section_sorter(
286                self.to_glib_none().0,
287                sorter.map(|p| p.as_ref()).to_glib_none().0,
288            );
289        }
290    }
291
292    /// Sets a new sorter on @self.
293    /// ## `sorter`
294    /// the [`Sorter`][crate::Sorter] to sort @model with
295    #[doc(alias = "gtk_sort_list_model_set_sorter")]
296    #[doc(alias = "sorter")]
297    pub fn set_sorter(&self, sorter: Option<&impl IsA<Sorter>>) {
298        unsafe {
299            ffi::gtk_sort_list_model_set_sorter(
300                self.to_glib_none().0,
301                sorter.map(|p| p.as_ref()).to_glib_none().0,
302            );
303        }
304    }
305
306    #[doc(alias = "incremental")]
307    pub fn connect_incremental_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
308        unsafe extern "C" fn notify_incremental_trampoline<F: Fn(&SortListModel) + 'static>(
309            this: *mut ffi::GtkSortListModel,
310            _param_spec: glib::ffi::gpointer,
311            f: glib::ffi::gpointer,
312        ) {
313            let f: &F = &*(f as *const F);
314            f(&from_glib_borrow(this))
315        }
316        unsafe {
317            let f: Box_<F> = Box_::new(f);
318            connect_raw(
319                self.as_ptr() as *mut _,
320                b"notify::incremental\0".as_ptr() as *const _,
321                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
322                    notify_incremental_trampoline::<F> as *const (),
323                )),
324                Box_::into_raw(f),
325            )
326        }
327    }
328
329    #[doc(alias = "model")]
330    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
331        unsafe extern "C" fn notify_model_trampoline<F: Fn(&SortListModel) + 'static>(
332            this: *mut ffi::GtkSortListModel,
333            _param_spec: glib::ffi::gpointer,
334            f: glib::ffi::gpointer,
335        ) {
336            let f: &F = &*(f as *const F);
337            f(&from_glib_borrow(this))
338        }
339        unsafe {
340            let f: Box_<F> = Box_::new(f);
341            connect_raw(
342                self.as_ptr() as *mut _,
343                b"notify::model\0".as_ptr() as *const _,
344                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
345                    notify_model_trampoline::<F> as *const (),
346                )),
347                Box_::into_raw(f),
348            )
349        }
350    }
351
352    #[doc(alias = "pending")]
353    pub fn connect_pending_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
354        unsafe extern "C" fn notify_pending_trampoline<F: Fn(&SortListModel) + 'static>(
355            this: *mut ffi::GtkSortListModel,
356            _param_spec: glib::ffi::gpointer,
357            f: glib::ffi::gpointer,
358        ) {
359            let f: &F = &*(f as *const F);
360            f(&from_glib_borrow(this))
361        }
362        unsafe {
363            let f: Box_<F> = Box_::new(f);
364            connect_raw(
365                self.as_ptr() as *mut _,
366                b"notify::pending\0".as_ptr() as *const _,
367                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
368                    notify_pending_trampoline::<F> as *const (),
369                )),
370                Box_::into_raw(f),
371            )
372        }
373    }
374
375    #[cfg(feature = "v4_12")]
376    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
377    #[doc(alias = "section-sorter")]
378    pub fn connect_section_sorter_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
379        unsafe extern "C" fn notify_section_sorter_trampoline<F: Fn(&SortListModel) + 'static>(
380            this: *mut ffi::GtkSortListModel,
381            _param_spec: glib::ffi::gpointer,
382            f: glib::ffi::gpointer,
383        ) {
384            let f: &F = &*(f as *const F);
385            f(&from_glib_borrow(this))
386        }
387        unsafe {
388            let f: Box_<F> = Box_::new(f);
389            connect_raw(
390                self.as_ptr() as *mut _,
391                b"notify::section-sorter\0".as_ptr() as *const _,
392                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
393                    notify_section_sorter_trampoline::<F> as *const (),
394                )),
395                Box_::into_raw(f),
396            )
397        }
398    }
399
400    #[doc(alias = "sorter")]
401    pub fn connect_sorter_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
402        unsafe extern "C" fn notify_sorter_trampoline<F: Fn(&SortListModel) + 'static>(
403            this: *mut ffi::GtkSortListModel,
404            _param_spec: glib::ffi::gpointer,
405            f: glib::ffi::gpointer,
406        ) {
407            let f: &F = &*(f as *const F);
408            f(&from_glib_borrow(this))
409        }
410        unsafe {
411            let f: Box_<F> = Box_::new(f);
412            connect_raw(
413                self.as_ptr() as *mut _,
414                b"notify::sorter\0".as_ptr() as *const _,
415                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
416                    notify_sorter_trampoline::<F> as *const (),
417                )),
418                Box_::into_raw(f),
419            )
420        }
421    }
422}
423
424impl Default for SortListModel {
425    fn default() -> Self {
426        glib::object::Object::new::<Self>()
427    }
428}
429
430// rustdoc-stripper-ignore-next
431/// A [builder-pattern] type to construct [`SortListModel`] objects.
432///
433/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
434#[must_use = "The builder must be built to be used"]
435pub struct SortListModelBuilder {
436    builder: glib::object::ObjectBuilder<'static, SortListModel>,
437}
438
439impl SortListModelBuilder {
440    fn new() -> Self {
441        Self {
442            builder: glib::object::Object::builder(),
443        }
444    }
445
446    /// If the model should sort items incrementally.
447    pub fn incremental(self, incremental: bool) -> Self {
448        Self {
449            builder: self.builder.property("incremental", incremental),
450        }
451    }
452
453    /// The model being sorted.
454    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
455        Self {
456            builder: self.builder.property("model", model.clone().upcast()),
457        }
458    }
459
460    /// The section sorter for this model, if one is set.
461    #[cfg(feature = "v4_12")]
462    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
463    pub fn section_sorter(self, section_sorter: &impl IsA<Sorter>) -> Self {
464        Self {
465            builder: self
466                .builder
467                .property("section-sorter", section_sorter.clone().upcast()),
468        }
469    }
470
471    /// The sorter for this model.
472    pub fn sorter(self, sorter: &impl IsA<Sorter>) -> Self {
473        Self {
474            builder: self.builder.property("sorter", sorter.clone().upcast()),
475        }
476    }
477
478    // rustdoc-stripper-ignore-next
479    /// Build the [`SortListModel`].
480    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
481    pub fn build(self) -> SortListModel {
482        assert_initialized_main_thread!();
483        self.builder.build()
484    }
485}