@@ -2,7 +2,7 @@ use std::mem;
22
33use slint:: { ComponentHandle , Model , ModelRc , VecModel } ;
44
5- use crate :: common:: { connect_i32_into_u64, get_int_size_idx, get_is_header_mode, get_str_name_idx , get_tool_model, set_tool_model} ;
5+ use crate :: common:: { connect_i32_into_u64, get_int_size_idx, get_is_header_mode, get_tool_model, set_tool_model} ;
66use crate :: { Callabler , CurrentTab , GuiState , MainListModel , MainWindow , SortMode } ;
77
88pub fn connect_sort ( app : & MainWindow ) {
@@ -13,32 +13,64 @@ pub fn connect_sort(app: &MainWindow) {
1313 let current_model = get_tool_model ( & app, active_tab) ;
1414
1515 let new_model = match sort_mode {
16- SortMode :: Size => sort_by_size ( & current_model, active_tab) ,
17- SortMode :: ParentName => sort_by_parent_name ( & current_model, active_tab) ,
16+ SortMode :: Size => sorts:: sort_by_size ( & current_model, active_tab) ,
17+ SortMode :: ParentName => sorts:: sort_by_parent_name ( & current_model, active_tab) ,
18+ SortMode :: ItemName => sorts:: sort_by_name ( & current_model, active_tab) ,
19+ SortMode :: FullName => sorts:: sort_by_full_name ( & current_model, active_tab) ,
1820 _ => todo ! ( ) ,
1921 } ;
2022
2123 set_tool_model ( & app, active_tab, new_model) ;
2224 } ) ;
2325}
2426
25- fn sort_by_parent_name ( model : & ModelRc < MainListModel > , active_tab : CurrentTab ) -> ModelRc < MainListModel > {
26- let sort_function = |e : & MainListModel | {
27- let name_idx = get_str_name_idx ( active_tab) ;
28- e. val_str . iter ( ) . nth ( name_idx) . expect ( "Failed to get name index" )
29- } ;
27+ mod sorts {
28+ use super :: * ;
29+ use crate :: common:: { get_str_name_idx, get_str_path_idx} ;
3030
31- common_sort_function ( model, active_tab, sort_function)
32- }
31+ pub ( super ) fn sort_by_full_name ( model : & ModelRc < MainListModel > , active_tab : CurrentTab ) -> ModelRc < MainListModel > {
32+ let sort_function = |e : & MainListModel | {
33+ let name_idx = get_str_name_idx ( active_tab) ;
34+ let path_idx = get_str_path_idx ( active_tab) ;
35+ let items = e. val_str . iter ( ) . collect :: < Vec < _ > > ( ) ;
36+ format ! ( "{}/{}" , items[ path_idx] , items[ name_idx] )
37+ } ;
38+
39+ common_sort_function ( model, active_tab, sort_function)
40+ }
3341
34- fn sort_by_size ( model : & ModelRc < MainListModel > , active_tab : CurrentTab ) -> ModelRc < MainListModel > {
35- let sort_function = |e : & MainListModel | {
36- let size_idx = get_int_size_idx ( active_tab) ;
37- let items = e. val_int . iter ( ) . collect :: < Vec < _ > > ( ) ;
38- connect_i32_into_u64 ( items[ size_idx] , items[ size_idx + 1 ] )
39- } ;
42+ pub ( super ) fn sort_by_name ( model : & ModelRc < MainListModel > , active_tab : CurrentTab ) -> ModelRc < MainListModel > {
43+ let sort_function = |e : & MainListModel | {
44+ let name_idx = get_str_name_idx ( active_tab) ;
45+ e. val_str
46+ . iter ( )
47+ . nth ( name_idx)
48+ . unwrap_or_else ( || panic ! ( "Failed to get name index - {name_idx} on {} items" , e. val_str. iter( ) . count( ) ) )
49+ } ;
50+
51+ common_sort_function ( model, active_tab, sort_function)
52+ }
53+ pub ( super ) fn sort_by_parent_name ( model : & ModelRc < MainListModel > , active_tab : CurrentTab ) -> ModelRc < MainListModel > {
54+ let sort_function = |e : & MainListModel | {
55+ let path_idx = get_str_path_idx ( active_tab) ;
56+ e. val_str
57+ . iter ( )
58+ . nth ( path_idx)
59+ . unwrap_or_else ( || panic ! ( "Failed to get name index - {path_idx} on {} items" , e. val_str. iter( ) . count( ) ) )
60+ } ;
4061
41- common_sort_function ( model, active_tab, sort_function)
62+ common_sort_function ( model, active_tab, sort_function)
63+ }
64+
65+ pub ( super ) fn sort_by_size ( model : & ModelRc < MainListModel > , active_tab : CurrentTab ) -> ModelRc < MainListModel > {
66+ let sort_function = |e : & MainListModel | {
67+ let size_idx = get_int_size_idx ( active_tab) ;
68+ let items = e. val_int . iter ( ) . collect :: < Vec < _ > > ( ) ;
69+ connect_i32_into_u64 ( items[ size_idx] , items[ size_idx + 1 ] )
70+ } ;
71+
72+ common_sort_function ( model, active_tab, sort_function)
73+ }
4274}
4375
4476fn common_sort_function < T : Ord > ( model : & ModelRc < MainListModel > , active_tab : CurrentTab , sort_function : impl Fn ( & MainListModel ) -> T ) -> ModelRc < MainListModel > {
@@ -99,8 +131,9 @@ fn group_by_header(model: &ModelRc<MainListModel>) -> Vec<(MainListModel, Vec<Ma
99131mod tests {
100132 use slint:: Model ;
101133
102- use crate :: common:: { get_int_size_idx, get_is_header_mode} ;
103- use crate :: connect_sort:: { convert_group_header_into_rc_model, group_by_header, sort_by_size} ;
134+ use crate :: common:: { get_int_size_idx, get_is_header_mode, get_str_name_idx, get_str_path_idx} ;
135+ use crate :: connect_sort:: sorts:: { sort_by_full_name, sort_by_name, sort_by_parent_name, sort_by_size} ;
136+ use crate :: connect_sort:: { convert_group_header_into_rc_model, group_by_header} ;
104137 use crate :: test_common:: { create_model_from_model_vec, get_model_vec} ;
105138 use crate :: { CurrentTab , MainListModel } ;
106139
@@ -137,29 +170,30 @@ mod tests {
137170 assert ! ( grouped. is_empty( ) ) ;
138171 }
139172
140- #[ test]
141- #[ should_panic]
142- fn group_by_header_panics_when_no_header_before_items ( ) {
143- let mut model = get_model_vec ( 3 ) ;
144- model[ 0 ] . header_row = false ;
145- model[ 1 ] . header_row = false ;
146- model[ 2 ] . header_row = false ;
147- let model = create_model_from_model_vec ( & model) ;
148-
149- group_by_header ( & model) ;
150- }
151-
152- #[ test]
153- #[ should_panic]
154- fn group_by_header_panics_when_group_is_empty ( ) {
155- let mut model = get_model_vec ( 3 ) ;
156- model[ 0 ] . header_row = true ;
157- model[ 1 ] . header_row = true ;
158- model[ 2 ] . header_row = true ;
159- let model = create_model_from_model_vec ( & model) ;
160-
161- group_by_header ( & model) ;
162- }
173+ // TODO
174+ // #[test]
175+ // #[should_panic]
176+ // fn group_by_header_panics_when_no_header_before_items() {
177+ // let mut model = get_model_vec(3);
178+ // model[0].header_row = false;
179+ // model[1].header_row = false;
180+ // model[2].header_row = false;
181+ // let model = create_model_from_model_vec(&model);
182+ //
183+ // group_by_header(&model);
184+ // }
185+ //
186+ // #[test]
187+ // #[should_panic]
188+ // fn group_by_header_panics_when_group_is_empty() {
189+ // let mut model = get_model_vec(3);
190+ // model[0].header_row = true;
191+ // model[1].header_row = true;
192+ // model[2].header_row = true;
193+ // let model = create_model_from_model_vec(&model);
194+ //
195+ // group_by_header(&model);
196+ // }
163197
164198 #[ test]
165199 fn convert_group_header_into_rc_model_combines_groups_correctly ( ) {
@@ -252,4 +286,77 @@ mod tests {
252286
253287 assert_eq ! ( sorted_model. row_count( ) , 0 ) ;
254288 }
289+
290+ #[ test]
291+ fn sort_by_parent_name_sorts_flat_model_correctly ( ) {
292+ let current_tab = CurrentTab :: BigFiles ;
293+ // To be sure that we set correct values in val_int, which must be equal to index
294+ assert_eq ! ( get_str_path_idx( current_tab) , 2 ) ;
295+ assert ! ( !get_is_header_mode( current_tab) ) ;
296+
297+ let mut model = get_model_vec ( 5 ) ;
298+ model[ 0 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "" . into ( ) , "E" . into ( ) ] ) ;
299+ model[ 1 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "" . into ( ) , "C" . into ( ) ] ) ;
300+ model[ 2 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "" . into ( ) , "D" . into ( ) ] ) ;
301+ model[ 3 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "" . into ( ) , "A" . into ( ) ] ) ;
302+ model[ 4 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "" . into ( ) , "B" . into ( ) ] ) ;
303+ let model = create_model_from_model_vec ( & model) ;
304+
305+ let sorted_model = sort_by_parent_name ( & model, current_tab) ;
306+
307+ assert_eq ! ( sorted_model. row_data( 0 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "" , "A" ] ) ;
308+ assert_eq ! ( sorted_model. row_data( 1 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "" , "B" ] ) ;
309+ assert_eq ! ( sorted_model. row_data( 2 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "" , "C" ] ) ;
310+ assert_eq ! ( sorted_model. row_data( 3 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "" , "D" ] ) ;
311+ assert_eq ! ( sorted_model. row_data( 4 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "" , "E" ] ) ;
312+ }
313+
314+ #[ test]
315+ fn sort_by_name_sorts_flat_model_correctly ( ) {
316+ let current_tab = CurrentTab :: BigFiles ;
317+ // To be sure that we set correct values in val_int, which must be equal to index
318+ assert_eq ! ( get_str_name_idx( current_tab) , 1 ) ;
319+ assert ! ( !get_is_header_mode( current_tab) ) ;
320+
321+ let mut model = get_model_vec ( 5 ) ;
322+ model[ 0 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "E" . into ( ) ] ) ;
323+ model[ 1 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "C" . into ( ) ] ) ;
324+ model[ 2 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "D" . into ( ) ] ) ;
325+ model[ 3 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "A" . into ( ) ] ) ;
326+ model[ 4 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "B" . into ( ) ] ) ;
327+ let model = create_model_from_model_vec ( & model) ;
328+
329+ let sorted_model = sort_by_name ( & model, current_tab) ;
330+
331+ assert_eq ! ( sorted_model. row_data( 0 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "A" ] ) ;
332+ assert_eq ! ( sorted_model. row_data( 1 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "B" ] ) ;
333+ assert_eq ! ( sorted_model. row_data( 2 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "C" ] ) ;
334+ assert_eq ! ( sorted_model. row_data( 3 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "D" ] ) ;
335+ assert_eq ! ( sorted_model. row_data( 4 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "E" ] ) ;
336+ }
337+
338+ #[ test]
339+ fn sort_by_full_name_sorts_flat_model_correctly ( ) {
340+ let current_tab = CurrentTab :: BigFiles ;
341+ // To be sure that we set correct values in val_int, which must be equal to index
342+ assert_eq ! ( get_str_name_idx( current_tab) , 1 ) ;
343+ assert_eq ! ( get_str_path_idx( current_tab) , 2 ) ;
344+ assert ! ( !get_is_header_mode( current_tab) ) ;
345+
346+ let mut model = get_model_vec ( 5 ) ;
347+ model[ 0 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "E" . into ( ) , "A" . into ( ) ] ) ;
348+ model[ 1 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "D" . into ( ) , "B" . into ( ) ] ) ;
349+ model[ 2 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "A" . into ( ) , "C" . into ( ) ] ) ;
350+ model[ 3 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "A" . into ( ) , "D" . into ( ) ] ) ;
351+ model[ 4 ] . val_str = create_model_from_model_vec ( & [ "" . into ( ) , "F" . into ( ) , "B" . into ( ) ] ) ;
352+ let model = create_model_from_model_vec ( & model) ;
353+
354+ let sorted_model = sort_by_full_name ( & model, current_tab) ;
355+
356+ assert_eq ! ( sorted_model. row_data( 0 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "E" , "A" ] ) ;
357+ assert_eq ! ( sorted_model. row_data( 1 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "D" , "B" ] ) ;
358+ assert_eq ! ( sorted_model. row_data( 2 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "F" , "B" ] ) ;
359+ assert_eq ! ( sorted_model. row_data( 3 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "A" , "C" ] ) ;
360+ assert_eq ! ( sorted_model. row_data( 4 ) . unwrap( ) . val_str. iter( ) . collect:: <Vec <_>>( ) , vec![ "" , "A" , "D" ] ) ;
361+ }
255362}
0 commit comments