Kirigami-addons

nameutils.cpp
1// SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
2//
3// SPDX-License-Identifier: LGPL-2.0-or-later
4
5#include "nameutils.h"
6#include <QDebug>
7#include <QMap>
8#include <QQuickStyle>
9#include <QTextBoundaryFinder>
10#include <QVector>
11
12#include <array>
13
14bool contains(const QString &str, QChar::Script s)
15{
16 for (auto rune : str) {
17 if (rune.script() == s) {
18 return true;
19 }
20 }
21 return false;
22}
23
24QString NameUtils::initialsFromString(const QString &string)
25{
26 // "" -> ""
27 QString normalized = string.trimmed();
28 if (normalized.isEmpty()) {
29 return {};
30 }
31
32 normalized = string.normalized(QString::NormalizationForm_D);
33
34 if (normalized.startsWith(QLatin1Char('#')) || normalized.startsWith(QLatin1Char('@'))) {
35 normalized.remove(0, 1);
36 }
37
38 // Names written with Han and Hangul characters generally can be initialised by taking the
39 // first character
40 if (contains(normalized, QChar::Script_Han) || contains(normalized, QChar::Script_Hangul)) {
41 return normalized.at(0);
42 }
43
44 // "FirstName Name Name LastName"
45 normalized = normalized.trimmed();
46
47 // Remove stuff inside parantheses
48 normalized = normalized.split(QLatin1Char('('))[0];
49
50 if (normalized.isEmpty()) {
51 return {};
52 }
53
54 if (normalized.contains(QLatin1Char(' '))) {
55 // "FirstName Name Name LastName" -> "FirstName" "Name" "Name" "LastName"
56#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
57 const auto split = QStringView(normalized).split(QLatin1Char(' '));
58#else
59 const auto split = normalized.splitRef(QLatin1Char(' '));
60#endif
61
62 // "FirstName"
63 auto first = split.first();
64 // "LastName"
65 auto last = split.last();
66 if (first.isEmpty()) {
67 // "" "LastName" -> "L"
68 return QString(last.front()).toUpper();
69 }
70 if (last.isEmpty()) {
71 // "FirstName" "" -> "F"
72 return QString(first.front()).toUpper();
73 }
74 // "FirstName" "LastName" -> "FL"
75 return (QString(first.front()) + last.front()).toUpper();
76 // "OneName"
77 } else {
78 // "OneName" -> "O"
79 return QString(normalized.front()).toUpper();
80 }
81}
82
83/* clang-format off */
84const QMap<QString,QList<QColor>> c_colors = {
85 {
86 QStringLiteral("default"),
87 {
88 QColor(0xe93a9a),
89 QColor(0xe93d58),
90 QColor(0xe9643a),
91 QColor(0xef973c),
92 QColor(0xe8cb2d),
93 QColor(0xb6e521),
94 QColor(0x3dd425),
95 QColor(0x00d485),
96 QColor(0x00d3b8),
97 QColor(0x3daee9),
98 QColor(0xb875dc),
99 QColor(0x926ee4),
100 }
101 },
102 {
103 QStringLiteral("Material"),
104 {
105 QColor(0xf44336),
106 QColor(0xe91e63),
107 QColor(0x9c27b0),
108 QColor(0x673ab7),
109 QColor(0x3f51b5),
110 QColor(0x2196f3),
111 QColor(0x03a9f4),
112 QColor(0x00bcd4),
113 QColor(0x009688),
114 QColor(0x4caf50),
115 QColor(0x8bc34a),
116 QColor(0xcddc39),
117 QColor(0xffeb3b),
118 QColor(0xffc107),
119 QColor(0xff9800),
120 QColor(0xff5722),
121 }
122 }
123};
124/* clang-format on */
125
126QList<QColor> grabColors()
127{
128 if (c_colors.contains(QQuickStyle::name())) {
129 return c_colors[QQuickStyle::name()];
130 }
131 return c_colors[QStringLiteral("default")];
132}
133
134auto NameUtils::colorsFromString(const QString &string) -> QColor
135{
136 // We use a hash to get a "random" number that's always the same for
137 // a given string.
138 auto hash = qHash(string);
139 // hash modulo the length of the colors list minus one will always get us a valid
140 // index
141 const auto colors = grabColors();
142 auto index = hash % (colors.length() - 1);
143 // return a colour
144 return colors[index];
145}
146
147auto NameUtils::isStringUnsuitableForInitials(const QString &string) -> bool
148{
149 if (string.isEmpty()) {
150 return true;
151 }
152
153 bool isNumber;
154 string.toFloat(&isNumber);
155 if (isNumber) {
156 return true;
157 }
158
159 static const std::array<QChar::Script, 6> scripts{
162
163 std::any_of(string.cbegin(), string.cend(), [](const QChar &character) {
164 return std::find(scripts.begin(), scripts.end(), character.script()) == scripts.end();
165 });
166
167 return false;
168}
169
170#include "moc_nameutils.cpp"
KTEXTEDITOR_EXPORT size_t qHash(KTextEditor::Cursor cursor, size_t seed=0) noexcept
Script script(char32_t ucs4)
QString name()
NormalizationForm_D
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString first(qsizetype n) const const
QChar & front()
bool isEmpty() const const
QString last(qsizetype n) const const
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toUpper() const const
QString trimmed() const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.