]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlisttostring.cpp
Merge branch 'release/22.04'
[dolphin.git] / src / kitemviews / kfileitemlisttostring.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "kfileitemlisttostring.h"
9
10 #include <KFileItem>
11 #include <KFileItemListProperties>
12 #include <KLocalizedString>
13
14 #include <QFontMetrics>
15 #include <QString>
16
17 QString fileItemListToString(KFileItemList items, int maximumTextWidth, QFontMetrics fontMetrics, ItemsState itemsState)
18 {
19 QString text;
20 switch (items.count()) {
21 case 1:
22 text = i18nc("Textual representation of a file. %1 is the name of the file/folder.",
23 "\"%1\"", items.first().name());
24 break;
25 case 2:
26 text = i18nc("Textual representation of two files. %1 and %2 are names of files/folders.",
27 "\"%1\" and \"%2\"", items.first().name(), items.last().name());
28 break;
29 case 3:
30 text = i18nc("Textual representation of three files. %1, %2 and %3 are names of files/folders.",
31 "\"%1\", \"%2\" and \"%3\"",
32 items.first().name(), items.at(1).name(), items.last().name());
33 break;
34 case 4:
35 text = i18nc("Textual representation of four files. %1, %2, %3 and %4 are names of files/folders.",
36 "\"%1\", \"%2\", \"%3\" and \"%4\"",
37 items.first().name(), items.at(1).name(), items.at(2).name(), items.last().name());
38 break;
39 case 5:
40 text = i18nc("Textual representation of five files. %1, %2, %3, %4 and %5 are names of files/folders.",
41 "\"%1\", \"%2\", \"%3\", \"%4\" and \"%5\"",
42 items.first().name(), items.at(1).name(), items.at(2).name(), items.at(3).name(), items.last().name());
43 break;
44 default:
45 text = QString();
46 break;
47 }
48
49 // At some point the added clarity from the text starts being less important than the text width.
50 if (!text.isEmpty() && fontMetrics.horizontalAdvance(text) <= maximumTextWidth) {
51 return text;
52 }
53
54 const KFileItemListProperties properties(items);
55 if (itemsState == Selected) {
56 if (properties.isFile()) {
57 text = i18ncp("Textual representation of selected files. %1 is the number of files.",
58 "One Selected File", "%1 Selected Files", items.count());
59 } else if (properties.isDirectory()) {
60 text = i18ncp("Textual representation of selected folders. %1 is the number of folders.",
61 "One Selected Folder", "%1 Selected Folders", items.count());
62 } else {
63 text = i18ncp("Textual representation of selected fileitems. %1 is the number of files/folders.",
64 "One Selected Item", "%1 Selected Items", items.count());
65 }
66
67 if (fontMetrics.horizontalAdvance(text) <= maximumTextWidth) {
68 return text;
69 }
70 }
71
72 if (properties.isFile()) {
73 return i18ncp("Textual representation of files. %1 is the number of files.",
74 "One File", "%1 Files", items.count());
75 } else if (properties.isDirectory()) {
76 return i18ncp("Textual representation of folders. %1 is the number of folders.",
77 "One Folder", "%1 Folders", items.count());
78 } else {
79 return i18ncp("Textual representation of fileitems. %1 is the number of files/folders.",
80 "One Item", "%1 Items", items.count());
81 }
82 }