KWidgetsAddons

common_helpers.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2008 Chusslove Illich <caslav.ilic@gmx.net>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include <common_helpers_p.h>
9
10// If pos points to alphanumeric X in "...(X)...", which is preceded or
11// followed only by non-alphanumerics, then "(X)" gets removed.
12static QString removeReducedCJKAccMark(const QString &label, int pos)
13{
14 if (pos > 0 && pos + 1 < label.length() //
15 && label[pos - 1] == QLatin1Char('(') //
16 && label[pos + 1] == QLatin1Char(')') //
17 && label[pos].isLetterOrNumber()) {
18 // Check if at start or end, ignoring non-alphanumerics.
19 int len = label.length();
20 int p1 = pos - 2;
21 while (p1 >= 0 && !label[p1].isLetterOrNumber()) {
22 --p1;
23 }
24 ++p1;
25 int p2 = pos + 2;
26 while (p2 < len && !label[p2].isLetterOrNumber()) {
27 ++p2;
28 }
29 --p2;
30
31 const QStringView sview{label};
32 if (p1 == 0) {
33 return sview.left(pos - 1) + sview.mid(p2 + 1);
34 } else if (p2 + 1 == len) {
35 return sview.left(p1) + sview.mid(pos + 2);
36 }
37 }
38 return label;
39}
40
41QString removeAcceleratorMarker(const QString &label_)
42{
43 QString label = label_;
44
45 int p = 0;
46 bool accmarkRemoved = false;
47 while (true) {
48 p = label.indexOf(QLatin1Char('&'), p);
49 if (p < 0 || p + 1 == label.length()) {
50 break;
51 }
52
53 if (label.at(p + 1).isLetterOrNumber()) {
54 // Valid accelerator.
55 label.remove(p, 1);
56
57 // May have been an accelerator in CJK-style "(&X)"
58 // at the start or end of text.
59 label = removeReducedCJKAccMark(label, p);
60
61 accmarkRemoved = true;
62 } else if (label.at(p + 1) == QLatin1Char('&')) {
63 // Escaped accelerator marker.
64 label.remove(p, 1);
65 }
66
67 ++p;
68 }
69
70 // If no marker was removed, and there are CJK characters in the label,
71 // also try to remove reduced CJK marker -- something may have removed
72 // ampersand beforehand.
73 if (!accmarkRemoved) {
74 bool hasCJK = false;
75 for (const QChar c : std::as_const(label)) {
76 if (c.unicode() >= 0x2e00) { // rough, but should be sufficient
77 hasCJK = true;
78 break;
79 }
80 }
81 if (hasCJK) {
82 p = 0;
83 while (true) {
84 p = label.indexOf(QLatin1Char('('), p);
85 if (p < 0) {
86 break;
87 }
88 label = removeReducedCJKAccMark(label, p + 1);
89 ++p;
90 }
91 }
92 }
93
94 return label;
95}
96
97QString dateFormatWith4DigitYear(const QLocale &locale, QLocale::FormatType format)
98{
99 // Clearly a workaround for QLocale using "yy" way too often for years
100 // and so you get no way to distinguish between 1913 and 2013 anymore...
101 // bummer.
102 QString res = locale.dateFormat(format);
103 res.replace(QLatin1String("yy"), QLatin1String("yyyy"));
104 res.replace(QLatin1String("yyyyyyyy"), QLatin1String("yyyy"));
105 return res;
106}
QString label(StandardShortcut id)
bool isLetterOrNumber(char32_t ucs4)
QString dateFormat(FormatType format) const const
const QChar at(qsizetype position) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.