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