Repository navigation
Expand file tree
/
Copy pathmodel_items.cpp
More file actions
4098 lines (3521 loc) · 132 KB
/
Copy pathmodel_items.cpp
File metadata and controls
4098 lines (3521 loc) · 132 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: File item representation. Defines item_element class for individual files,
// manages thumbnail caching, metadata display, and selection state; and the groups they are listed in,
// whose detail form is headed by columns that sort the group and size themselves.
#include "pch.h"
#include "model_items.h"
#include "ui_dialog.h"
#include "model.h"
#include "model_index.h"
namespace df
{
std::function<void(core::file_path source, core::file_path destination)> rename_after_move_for_test;
std::function<void(std::string_view message)> rename_recovery_report_for_test;
}
#include "ui_controllers.h"
#include "ui_controls.h"
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
df_assert_pod(df::group_key);
df_assert_pod(df::file_group_histogram);
df_assert_movable(df::item_set);
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
static df::group_key media_type_index(const df::item_element_ptr& i)
{
df::group_key result;
const auto ft = i->file_type();
const auto group_key = ft->group->key;
result.order1 = ft->group->id;
result.type = group_key;
result.text1 = core::cache(ft->display_name(true));
result.group = ft->group;
return result;
}
static df::group_key shuffle_index_key(const df::item_element_ptr& /*i*/)
{
df::group_key result;
result.type = group_key_type::grouped_value;
return result;
}
static df::group_key extension_key(const df::item_element_ptr& i)
{
df::group_key result;
auto ext = i->extension();
if (!ext.empty())
{
result.type = group_key_type::grouped_value;
if (ext.front() == L'.') ext = ext.substr(1);
result.text1 = core::cache(ext);
}
return result;
}
static df::group_key folder_key(const df::item_element_ptr& i)
{
df::group_key result;
result.type = group_key_type::grouped_value;
if (i->is_folder())
{
result.text1 = i->folder().parent().text();
}
else
{
result.text1 = i->folder().text();
}
return result;
}
static df::group_key location_key(const df::item_element_ptr& i, const location_cache& locations,
std::map<attribution_cell, located_place>& resolved_places)
{
df::group_key result;
const auto* const ft = i->file_type();
if (ft->has_trait(file_traits::no_metadata_grouping)) return {ft->group->key};
const auto md = i->metadata();
if (md)
{
auto country = md->location_country;
auto state = md->location_state;
auto place = md->location_place;
if (md->coordinate.is_valid() && (is_empty(country) || is_empty(state) || is_empty(place)))
{
// locations.md 2.5: bounded attribution. A place too far away to be a truthful
// answer contributes nothing, so a remote item groups by country or not at all.
const attribution_cell cell(md->coordinate);
const auto found = resolved_places.find(cell);
const auto resolved = found != resolved_places.end() ? found->second
: locations.find_attributed(md->coordinate);
if (found == resolved_places.end() && locations.is_index_loaded())
{
resolved_places.emplace(cell, resolved);
}
if (is_empty(country)) country = resolved.place.country;
if (is_empty(state)) state = resolved.place.state;
if (is_empty(place)) place = resolved.place.place;
}
if (!is_empty(country) || !is_empty(state) || !is_empty(place))
{
result.type = group_key_type::grouped_value;
result.text1 = normalize_county_name(country);
result.text2 = state;
result.text3 = place;
}
}
return result;
}
static df::group_key camera_key(const df::item_element_ptr& i)
{
df::group_key result;
const auto* const ft = i->file_type();
if (ft->has_trait(file_traits::no_metadata_grouping)) return {ft->group->key};
const auto md = i->metadata();
if (md && !is_empty(md->camera_manufacturer))
{
result.type = group_key_type::grouped_value;
result.text1 = md->camera_manufacturer;
result.text2 = md->camera_model;
}
return result;
}
static df::group_key album_show_key(const df::item_element_ptr& i)
{
df::group_key result;
const auto* const ft = i->file_type();
if (ft->has_trait(file_traits::no_metadata_grouping)) return {ft->group->key};
const auto md = i->metadata();
if (md)
{
if (!is_empty(md->show))
{
result.type = group_key_type::grouped_value;
result.text1 = md->show;
result.text1_prop_type = prop::show;
if (md->season != 0)
{
result.order3 = md->season;
}
}
else if (!is_empty(md->album_artist) || !is_empty(md->artist) || !is_empty(md->album))
{
result.type = group_key_type::grouped_value;
if (!is_empty(md->album_artist))
{
result.text1 = md->album_artist;
result.text1_prop_type = prop::album_artist;
}
else if (!is_empty(md->artist))
{
result.text1 = md->artist;
result.text1_prop_type = prop::artist;
}
if (!is_empty(md->album))
{
result.text2 = md->album;
}
}
}
return result;
}
static std::string_view related_axis_text(const df::related_axis axis)
{
switch (axis)
{
case df::related_axis::film: return tt.related_group_film;
case df::related_axis::album: return tt.related_group_album;
case df::related_axis::series: return tt.related_group_series;
case df::related_axis::time: return tt.related_group_time;
case df::related_axis::location: return tt.related_group_location;
default: return tt.related_group_duplicates;
}
}
static icon_index related_axis_icon(const df::related_axis axis)
{
switch (axis)
{
case df::related_axis::film: return icon_index::video;
case df::related_axis::album: return icon_index::audio;
case df::related_axis::series: return icon_index::video;
case df::related_axis::time: return icon_index::time;
case df::related_axis::location: return icon_index::location;
default: return icon_index::compare;
}
}
static df::group_key related_key(const df::item_element_ptr& i)
{
const auto axis = df::related_axis_of(i->search().type);
df::group_key result;
result.type = group_key_type::grouped_value;
result.order1 = static_cast<int32_t>(axis);
result.text1 = core::cache(related_axis_text(axis));
result.icon = related_axis_icon(axis);
return result;
}
static df::group_key size_key(const df::item_element_ptr& i)
{
df::group_key result;
const auto size_bucket = prop::size_bucket(i->file_size().to_int64());
if (size_bucket == 0) return {group_key_type::grouped_no_value};
result.order1 = core::round(std::sqrt(size_bucket));
result.type = group_key_type::grouped_value;
return result;
}
static constexpr auto rating_base_num = 6;
static constexpr auto rating_reject_num = 7;
static df::group_key rating_key(const df::item_element_ptr& i)
{
df::group_key result;
const auto* const ft = i->file_type();
if (ft->has_trait(file_traits::no_metadata_grouping)) return {ft->group->key};
const auto md = i->metadata();
if (md)
{
if (md->rating != 0)
{
result.type = group_key_type::grouped_value;
if (md->rating == -1)
{
result.order1 = rating_reject_num;
}
else
{
result.order1 = rating_base_num - md->rating;
}
}
if (!is_empty(md->label))
{
result.text1 = md->label;
result.type = group_key_type::grouped_value;
}
}
return result;
}
uint32_t local_today_days()
{
return platform::utc_to_local(core::utc_now()).to_days();
}
df::group_key date_group_key(const core::date_t when, const uint32_t today)
{
df::group_key result;
result.type = group_key_type::grouped_value;
if (when.is_valid())
{
const auto day = when.to_days();
if (day == today)
{
result.order1 = 0;
}
else if (day + 1 == today)
{
result.order1 = 1;
}
else
{
const auto date = when.date();
result.order1 = 3000 - date.year;
result.order2 = 100 - date.month;
}
}
return result;
}
static df::group_key date_key(const core::date_t when, const df::item_element_ptr& i, const uint32_t today)
{
if (i->is_folder()) return {group_key_type::folder};
return date_group_key(when, today);
}
static df::group_key resolution_key(const df::item_element_ptr& i)
{
df::group_key result;
const auto* const ft = i->file_type();
if (ft->has_trait(file_traits::no_metadata_grouping)) return {ft->group->key};
const auto md = i->metadata();
if (md && md->width > 0 && md->height > 0)
{
const auto extent = sizei{md->width, md->height};
const auto display_name = ft->group->display_name(true);
if (ft->has_trait(file_traits::av))
{
const auto video_res = prop::format_video_resolution(extent);
if (!video_res.empty())
{
result.type = group_key_type::grouped_value;
result.order1 = ft->group->id;
result.order2 = 1 + core::round(std::sqrt(extent.cx * extent.cy));
result.text1 = core::cache(display_name);
result.text2 = core::cache(video_res);
return result;
}
result.type = group_key_type::grouped_value;
result.order1 = ft->group->id;
result.order2 = 1 + core::round(std::sqrt(extent.cx * extent.cy));
result.text1 = core::cache(display_name);
result.text2 = core::cache(std::format("{}x{}", extent.cx, extent.cy));
}
else
{
if (extent.cx <= 128 && extent.cy <= 128)
{
result.type = group_key_type::grouped_value;
result.order1 = 1;
result.order2 = 0;
result.text1 = core::cache(display_name);
result.text2 = "icon size"_c;
}
else
{
const auto mp = core::calc_mega_pixels(extent.cx, extent.cy);
if (mp < 0.9)
{
result.type = group_key_type::grouped_value;
result.order1 = 1;
result.order2 = 1;
result.text1 = core::cache(display_name);
result.text2 = "small size"_c;
}
else
{
const auto n = prop::exp_round(mp);
result.type = group_key_type::grouped_value;
result.order1 = 1;
result.order2 = 1 + core::round(std::sqrt(n));
result.text1 = core::cache(display_name);
result.text2 = core::cache(std::format("{} megapixels", n));
}
}
}
}
return result;
}
aspect_ratio_group calc_aspect_ratio_group(const sizei dimensions)
{
struct ratio_bucket
{
double ratio;
aspect_ratio_bucket bucket;
};
constexpr ratio_bucket buckets[] = {
{1.0, aspect_ratio_bucket::square},
{5.0 / 4.0, aspect_ratio_bucket::five_four},
{4.0 / 3.0, aspect_ratio_bucket::four_three},
{3.0 / 2.0, aspect_ratio_bucket::three_two},
{16.0 / 10.0, aspect_ratio_bucket::sixteen_ten},
{16.0 / 9.0, aspect_ratio_bucket::sixteen_nine},
{21.0 / 9.0, aspect_ratio_bucket::twenty_one_nine},
};
if (dimensions.cx <= 0 || dimensions.cy <= 0) return {};
const auto ratio = std::max(dimensions.cx, dimensions.cy) /
static_cast<double>(std::min(dimensions.cx, dimensions.cy));
for (const auto& bucket : buckets)
{
const auto difference = ratio > bucket.ratio ? ratio - bucket.ratio : bucket.ratio - ratio;
if (difference / bucket.ratio <= 0.03) return {bucket.bucket, dimensions.cy > dimensions.cx};
}
return {aspect_ratio_bucket::other, dimensions.cy > dimensions.cx};
}
static df::group_key aspect_ratio_key(const df::item_element_ptr& i)
{
const auto* const ft = i->file_type();
if (ft->has_trait(file_traits::no_metadata_grouping)) return {ft->group->key};
const auto md = i->metadata();
if (!md || md->width <= 0 || md->height <= 0) return {};
struct ratio_label
{
aspect_ratio_bucket bucket;
int order;
std::string_view landscape;
std::string_view portrait;
};
static constexpr ratio_label labels[] = {
{aspect_ratio_bucket::square, 1000, "1:1", "1:1"},
{aspect_ratio_bucket::five_four, 1250, "5:4", "4:5"},
{aspect_ratio_bucket::four_three, 1333, "4:3", "3:4"},
{aspect_ratio_bucket::three_two, 1500, "3:2", "2:3"},
{aspect_ratio_bucket::sixteen_ten, 1600, "16:10", "10:16"},
{aspect_ratio_bucket::sixteen_nine, 1778, "16:9", "9:16"},
{aspect_ratio_bucket::twenty_one_nine, 2333, "21:9", "9:21"},
};
const auto aspect_group = calc_aspect_ratio_group({md->width, md->height});
df::group_key result;
result.type = group_key_type::grouped_value;
result.order1 = aspect_group.is_portrait ? 1 : 0;
result.order2 = 10000;
for (const auto& label : labels)
{
if (aspect_group.bucket == label.bucket)
{
result.order2 = label.order;
result.text1 = core::cache(aspect_group.is_portrait ? label.portrait : label.landscape);
return result;
}
}
result.text1 = core::cache(aspect_group.is_portrait ? tt.aspect_ratio_other_portrait.sv()
: tt.aspect_ratio_other_landscape.sv());
return result;
}
class toggle_item_display_element final :
public std::enable_shared_from_this<toggle_item_display_element>,
public view_element
{
df::weak_item_group_ptr _parent;
const icon_index _icon = icon_index::details;
public:
toggle_item_display_element(df::weak_item_group_ptr parent) noexcept :
view_element(flex_item::right_justified | view_element_style::has_tooltip | view_element_style::can_invoke),
_parent(std::move(parent))
{
}
void render(render::draw_context& dc, const pointi element_offset) const override
{
render_background(dc, element_offset);
const auto clr = core::color(link_foreground_color(), dc.colors.alpha);
xdraw_icon(dc, _icon, bounds.offset(element_offset), clr, {});
}
sizei measure(render::measure_context& mc, const int /*width_limit*/) const override
{
return {mc.icon_cxy, mc.icon_cxy};
}
view_controller_ptr controller_from_location(const view_host_ptr& host, const pointi loc,
const pointi element_offset, hit_test_context& ctx) override
{
return default_controller_from_location(*this, host, loc, element_offset, ctx);
}
void dispatch_event(const view_element_event& event) override
{
if (event.type == view_element_event_type::invoke)
{
const auto p = _parent.lock();
if (p) p->toggle_display();
}
}
void tooltip(view_hover_element& hover, const pointi /*loc*/, const pointi element_offset) const override
{
hover.elements->add(std::make_shared<text_element>(tt.tooltip_toggle_details_selected));
hover.window_bounds = hover.active_bounds = bounds.offset(element_offset);
}
};
static void sort_items(df::item_elements& items, const group_by group_mode, const sort_by sort_order,
const bool group_by_dups)
{
constexpr auto size_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r) -> bool
{
const auto ll = l->file_size();
const auto rr = r->file_size();
return ll == rr ? icmp(l->name(), r->name()) < 0 : ll > rr;
};
constexpr auto modified_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
if (l->file_modified() == r->file_modified()) return icmp(l->name(), r->name()) < 0;
return l->file_modified() > r->file_modified();
};
constexpr auto created_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
if (l->media_created() == r->media_created()) return icmp(l->name(), r->name()) < 0;
return l->media_created() > r->media_created();
};
constexpr auto file_created_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
const auto ll = l->file_or_metadata_created();
const auto rr = r->file_or_metadata_created();
if (ll == rr) return icmp(l->name(), r->name()) < 0;
return ll > rr;
};
constexpr auto pixel_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r) -> bool
{
const auto lmd = l->metadata();
const auto rmd = r->metadata();
const auto ll = lmd ? core::calc_mega_pixels(lmd->width, lmd->height) : 0;
const auto rr = rmd ? core::calc_mega_pixels(rmd->width, rmd->height) : 0;
return ll == rr ? icmp(l->name(), r->name()) < 0 : ll > rr;
};
constexpr auto group_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
const auto ll = l->duplicates().group;
const auto rr = r->duplicates().group;
if (ll != rr) return ll < rr;
return icmp(l->name(), r->name()) < 0;
};
constexpr auto name_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
return core::icmp_natural(l->name(), r->name()) < 0;
};
// Inside a relation group the order is closeness to the item the search started at.
constexpr auto related_sorter = [](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
const auto ll = l->search().distance;
const auto rr = r->search().distance;
if (ll != rr) return ll < rr;
return core::icmp_natural(l->name(), r->name()) < 0;
};
// A show plays in season and episode order, a special ahead of its season's first episode and a dated
// episode by the day it aired; an album plays in disc and track order. Anything numbered by neither
// follows, by name - as do files whose names alone would have put 07x01 after S01E01.
constexpr auto sequence_key = [](const df::item_element_ptr& i) -> std::tuple<int, int, int, int64_t>
{
const auto md = i->metadata();
if (!md) return {1, 0, 0, 0};
if (!is_empty(md->show))
{
if (!prop::is_null(md->episode) || md->is_special) return {0, md->season, md->episode.x, 0};
if (md->kind == prop::media_kind::episode) return {0, md->season, 0, md->created().to_int64()};
return {1, 0, 0, 0};
}
if (!prop::is_null(md->track)) return {0, md->disk.x, md->track.x, 0};
return {1, 0, 0, 0};
};
constexpr auto sequence_sorter = [sequence_key](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
const auto ll = sequence_key(l);
const auto rr = sequence_key(r);
if (ll != rr) return ll < rr;
return core::icmp_natural(l->name(), r->name()) < 0;
};
const auto sorts_by_date = group_mode != group_by::related &&
(sort_order == sort_by::date_created || sort_order == sort_by::date_modified ||
sort_order == sort_by::date_original ||
(sort_order == sort_by::def &&
(group_mode == group_by::date_created || group_mode == group_by::date_modified ||
group_mode == group_by::date_original)));
const auto reverse_after = !setting.sort_dates_descending && sorts_by_date;
// Regrouping re-sorts every group even when nothing that affects order moved, and these comparators
// run case-insensitive natural string compares.
const auto apply = [&items, reverse_after](auto&& pred)
{
if (reverse_after)
{
const auto descending = [&pred](const df::item_element_ptr& l, const df::item_element_ptr& r)
{
return pred(r, l);
};
if (!std::ranges::is_sorted(items, descending))
{
std::ranges::sort(items, pred);
std::ranges::reverse(items);
}
}
else if (!std::ranges::is_sorted(items, pred))
{
std::ranges::sort(items, pred);
}
};
if (group_mode == group_by::shuffle)
{
apply([](const df::item_element_ptr& l, const df::item_element_ptr& r) { return l->random() < r->random(); });
}
else if (group_mode == group_by::related)
{
apply(related_sorter);
}
else if (sort_order == sort_by::size)
{
apply(size_sorter);
}
else if (sort_order == sort_by::date_modified)
{
apply(modified_sorter);
}
else if (sort_order == sort_by::date_created)
{
apply(file_created_sorter);
}
else if (sort_order == sort_by::date_original)
{
apply(created_sorter);
}
else if (sort_order == sort_by::name)
{
apply(name_sorter);
}
else if (group_mode == group_by::size)
{
apply(size_sorter);
}
else if (group_mode == group_by::date_modified)
{
apply(modified_sorter);
}
else if (group_mode == group_by::date_created)
{
apply(file_created_sorter);
}
else if (group_mode == group_by::date_original)
{
apply(created_sorter);
}
else if (group_mode == group_by::resolution)
{
apply(pixel_sorter);
}
else if (group_mode == group_by::aspect_ratio)
{
apply(name_sorter);
}
else if (group_mode == group_by::folder)
{
apply(name_sorter);
}
else if (group_mode == group_by::album_show)
{
apply(sequence_sorter);
}
else if (group_by_dups)
{
apply(group_sorter);
}
else
{
apply(name_sorter);
}
}
// The value a detail row shows in a column, as that column sorts it: numbers by value, text as it is read. A row with
// nothing in the column has no key, and follows those that have one whichever way the column sorts.
static ui::table_sort_key detail_sort_key(const df::item_display_info& shown, const df::detail_column c,
const bool created_is_file)
{
using enum df::detail_column;
const auto number = [](const auto n)
{
return n > 0 ? ui::table_sort_key(n) : ui::table_sort_key{};
};
const auto text = [](const std::string_view s)
{
return s.empty() ? ui::table_sort_key{} : ui::table_sort_key(s);
};
const auto date = [](const core::date_t d)
{
return d.is_valid() ? ui::table_sort_key(d.to_int64()) : ui::table_sort_key{};
};
switch (c)
{
case disk: return number(shown.disk);
case track: return number(shown.track);
case episode: return text(shown.episode.sv());
case year: return number(shown.year);
case title: return text(shown.name.sv());
// A reject is marked in the same column, ahead of the labels.
case flag: return shown.rating < 0 ? ui::table_sort_key(-1) : text(shown.label.sv());
case presence: return df::can_show_duplicates(shown) ? ui::table_sort_key(shown.duplicates) : ui::table_sort_key{};
case sidecars: return number(shown.sidecars);
case items: return number(shown.items);
case duration: return number(shown.duration);
case file_size: return shown.size.is_empty() ? ui::table_sort_key{} : ui::table_sort_key(shown.size.to_int64());
case bitrate: return text(shown.bitrate.sv());
case info: return text(shown.info);
case dimensions: return number(static_cast<int64_t>(shown.dimensions.cx) * shown.dimensions.cy);
case pixel_format: return text(shown.pixel_format.sv());
case audio_sample_rate: return number(shown.audio_sample_rate);
case created: return date(created_is_file ? shown.file_created : shown.created);
case modified: return date(shown.modified);
case icon: break;
}
return {};
}
void df::item_group::sort(const group_by group_mode, const sort_by sort_order, const bool group_by_dups)
{
// A group sorted from its headings keeps that order while they show; the rest follow the toolbar's.
const auto* const choice = _display == item_group_display::detail ? _state.detail_choice(_key) : nullptr;
if (choice && choice->sort_column >= 0 && choice->sort_column < detail_column_count)
{
const auto column = static_cast<detail_column>(choice->sort_column);
const auto created_is_file = _state.group_order() == group_by::date_created;
std::vector<std::pair<ui::table_sort_key, item_element_ptr>> keyed;
keyed.reserve(_items.size());
for (const auto& i : _items)
keyed.emplace_back(detail_sort_key(i->populate_info(), column, created_is_file), i);
std::ranges::stable_sort(keyed, [ascending = choice->ascending](const auto& l, const auto& r)
{
if (l.first.empty() != r.first.empty()) return r.first.empty();
if (const auto c = l.first.compare(r.first); c != 0) return ascending ? c < 0 : c > 0;
return core::icmp_natural(l.second->name(), r.second->name()) < 0;
});
for (auto i = 0_z; i < keyed.size(); ++i) _items[i] = std::move(keyed[i].second);
}
else
{
sort_items(_items, group_mode, sort_order, group_by_dups);
}
auto last_dup_group = 0u;
auto alt_background = false;
for (const auto& i : _items)
{
const auto dup_group = i->duplicates().group;
if (group_by_dups && last_dup_group != dup_group) alt_background = !alt_background;
i->alt_background = alt_background;
last_dup_group = dup_group;
}
_show_folder = group_by_dups || _state.search().has_related();
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
df::file_group_histogram df::item_set::summary() const
{
file_group_histogram result;
for (const auto& i : _items)
{
if (i->is_folder())
{
result.record(i->info());
}
else
{
result.record(i->file_type(), i->file_size());
}
}
return result;
}
df::history_range df::history_auto_range(const date_histogram& dates, const int current_year)
{
const auto window = history_range{current_year - history_window_years + 1, current_year};
std::array<uint64_t, max_history_years> yearly{};
uint64_t total = 0;
for (auto y = 0; y < max_history_years; ++y)
{
uint64_t sum = 0;
for (auto m = 0; m < 12; ++m) sum += dates.dates[static_cast<size_t>(y) * 12 + m].created;
yearly[y] = sum;
total += sum;
}
if (total == 0) return window;
// How much of the collection sits at or beyond each year, counting back from today. Both trims
// below are questions about a tail, and this answers them without rescanning.
std::array<uint64_t, max_history_years + 1> beyond{};
for (auto y = max_history_years - 1; y >= 0; --y) beyond[y] = beyond[y + 1] + yearly[y];
// The shortest run back from today that still holds nearly everything. A scanner that stamped
// 1900 on four negatives does not get to set the scale for twenty thousand photographs.
const auto keep = total * history_coverage_percent / 100;
uint64_t running = 0;
auto oldest = 0;
for (auto y = 0; y < max_history_years; ++y)
{
if (yearly[y] == 0) continue;
running += yearly[y];
oldest = y;
if (running >= keep) break;
}
// Real history thins out; it does not fall silent for six years and then resume. An island
// behind a long gap, holding only a sliver of the collection, is a camera whose clock reset.
auto gap = 0;
for (auto y = 1; y <= oldest; ++y)
{
if (yearly[y] == 0)
{
++gap;
continue;
}
if (gap >= history_gap_years && beyond[y] * 100 < total * history_island_percent)
{
oldest = y - gap - 1;
break;
}
gap = 0;
}
// Never narrower than one window, or the navigator has nothing to navigate.
return {std::min(current_year - oldest, window.first_year), current_year};
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
df::item_set_info df::item_set::info() const
{
item_set_info result;
for (const auto& i : _items)
{
const auto md = i->metadata();
if (md)
{
if (i->sidecars_count() > 0) result.sidecars += 1;
if (i->is_media())
{
if (md->tags.is_empty()) result.untagged += 1;
if (core::is_empty(md->location_country)) result.unlocated += 1;
if (md->rating == 0) result.unrated += 1;
if (core::is_empty(md->copyright_credit)) result.uncredited += 1;
}
}
if (i->duplicates().count > 1)
{
result.duplicates += 1;
}
if (i->rating() == -1) result.rejected += 1;
}
return result;
}
std::string_view item_presence_text(const item_presence v, const bool long_text)
{
if (v == item_presence::this_in)
{
return long_text ? tt.presence_this_in_long : tt.presence_this_in;
}
if (v == item_presence::similar_in)
{
return long_text ? tt.presence_similar_in_long : tt.presence_similar_in;
}
if (v == item_presence::not_in)
{
return long_text ? tt.presence_not_in_long : tt.presence_not_in;
}
if (v == item_presence::newer_in)
{
return long_text ? tt.presence_newer_in_long : tt.presence_newer_in;
}
if (v == item_presence::older_in)
{
return long_text ? tt.presence_older_in_long : tt.presence_older_in;
}
return tt.presence_loading;
}
static df::group_key presence_key(const item_presence& pr)
{
df::group_key result;
result.order1 = static_cast<int>(pr);
result.type = group_key_type::grouped_value;
switch (pr)
{
case item_presence::this_in:
result.text1 = core::cache(tt.presence_this_in);
result.icon = icon_index::check;
break;
case item_presence::similar_in:
result.text1 = core::cache(tt.presence_similar_in);
result.icon = icon_index::compare;
break;
case item_presence::not_in:
result.text1 = core::cache(tt.presence_not_in);
result.icon = icon_index::del;
break;
case item_presence::newer_in:
result.text1 = core::cache(tt.presence_newer_in);
result.icon = icon_index::add2;
break;
case item_presence::older_in:
result.text1 = core::cache(tt.presence_older_in);
result.icon = icon_index::remove2;
break;
default:
result.text1 = core::cache(tt.presence_loading);
result.icon = icon_index::unknown;
break;
}
return result;
}
static int calc_item_line_height()
{
return setting.item_scale_dimension();
}
std::shared_ptr<group_title_control> df::build_group_title(view_state& s, const view_host_base_ptr& view,
const item_group_ptr& g)
{
assert_true(platform::is_ui_thread());
const auto order = s.effective_group_order();