diff options
| author | Felix Ernst <[email protected]> | 2022-03-23 15:03:32 +0000 |
|---|---|---|
| committer | Felix Ernst <[email protected]> | 2022-03-23 15:03:32 +0000 |
| commit | 45af4bc0e09ebacca4d7f8177dd5dc0a5fd109cd (patch) | |
| tree | 9343b0e28fc6403bd6597926f248d014cfd35f68 /src/kitemviews/kfileitemlisttostring.cpp | |
| parent | 49726ad591eca7ac7d11da48a058c512726126d4 (diff) | |
Allow for more explicit button labels
This commit allows us to very explicitly refer to any set of items
in text. This way buttons don't need to be labeled generically
like "Permanently Delete" but can be enriched to be labeled
"Permanently Delete "FileName"" or "Copy 7 Selected Folders" or
"Copy 6 Files" or "Rename "file1", "file2", "file3", "file4" and
"folder5"".
This commit tries to save translators a lot of work by using a
translation puzzle. This might be problematic for some languages.
The alternative on the other hand would mean that any label which
wants to be explicit would need to have over 10 translations just
for one label which seems quite bad as well.
A fallback is to be implemented for languages that can't really
accommodate for any specific word puzzle. This is explained in the
documentation.
Diffstat (limited to 'src/kitemviews/kfileitemlisttostring.cpp')
| -rw-r--r-- | src/kitemviews/kfileitemlisttostring.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/kitemviews/kfileitemlisttostring.cpp b/src/kitemviews/kfileitemlisttostring.cpp new file mode 100644 index 000000000..8e8f880e9 --- /dev/null +++ b/src/kitemviews/kfileitemlisttostring.cpp @@ -0,0 +1,82 @@ +/* + This file is part of the KDE project + SPDX-FileCopyrightText: 2022 Felix Ernst <[email protected]> + + SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL +*/ + +#include "kfileitemlisttostring.h" + +#include <KFileItem> +#include <KFileItemListProperties> +#include <KLocalizedString> + +#include <QFontMetrics> +#include <QString> + +QString fileItemListToString(KFileItemList items, int maximumTextWidth, QFontMetrics fontMetrics, ItemsState itemsState) +{ + QString text; + switch (items.count()) { + case 1: + text = i18nc("Textual representation of a file. %1 is the name of the file/folder.", + "\"%1\"", items.first().name()); + break; + case 2: + text = i18nc("Textual representation of two files. %1 and %2 are names of files/folders.", + "\"%1\" and \"%2\"", items.first().name(), items.last().name()); + break; + case 3: + text = i18nc("Textual representation of three files. %1, %2 and %3 are names of files/folders.", + "\"%1\", \"%2\" and \"%3\"", + items.first().name(), items.at(1).name(), items.last().name()); + break; + case 4: + text = i18nc("Textual representation of four files. %1, %2, %3 and %4 are names of files/folders.", + "\"%1\", \"%2\", \"%3\" and \"%4\"", + items.first().name(), items.at(1).name(), items.at(2).name(), items.last().name()); + break; + case 5: + text = i18nc("Textual representation of five files. %1, %2, %3, %4 and %5 are names of files/folders.", + "\"%1\", \"%2\", \"%3\", \"%4\" and \"%5\"", + items.first().name(), items.at(1).name(), items.at(2).name(), items.at(3).name(), items.last().name()); + break; + default: + text = QString(); + break; + } + + // At some point the added clarity from the text starts being less important than the text width. + if (!text.isEmpty() && fontMetrics.horizontalAdvance(text) <= maximumTextWidth) { + return text; + } + + const KFileItemListProperties properties(items); + if (itemsState == Selected) { + if (properties.isFile()) { + text = i18ncp("Textual representation of selected files. %1 is the number of files.", + "One Selected File", "%1 Selected Files", items.count()); + } else if (properties.isDirectory()) { + text = i18ncp("Textual representation of selected folders. %1 is the number of folders.", + "One Selected Folder", "%1 Selected Folders", items.count()); + } else { + text = i18ncp("Textual representation of selected fileitems. %1 is the number of files/folders.", + "One Selected Item", "%1 Selected Items", items.count()); + } + + if (fontMetrics.horizontalAdvance(text) <= maximumTextWidth) { + return text; + } + } + + if (properties.isFile()) { + return i18ncp("Textual representation of files. %1 is the number of files.", + "One File", "%1 Files", items.count()); + } else if (properties.isDirectory()) { + return i18ncp("Textual representation of folders. %1 is the number of folders.", + "One Folder", "%1 Folders", items.count()); + } else { + return i18ncp("Textual representation of fileitems. %1 is the number of files/folders.", + "One Item", "%1 Items", items.count()); + } +} |
