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("#e93a9a"),
89 QColor("#e93d58"),
90 QColor("#e9643a"),
91 QColor("#ef973c"),
92 QColor("#e8cb2d"),
93 QColor("#b6e521"),
94 QColor("#3dd425"),
95 QColor("#00d485"),
96 QColor("#00d3b8"),
97 QColor("#3daee9"),
98 QColor("#b875dc"),
99 QColor("#926ee4"),
100 }
101 },
102 {
103 QStringLiteral("Material"),
104 {
105 QColor("#f44336"),
106 QColor("#e91e63"),
107 QColor("#9c27b0"),
108 QColor("#673ab7"),
109 QColor("#3f51b5"),
110 QColor("#2196f3"),
111 QColor("#03a9f4"),
112 QColor("#00bcd4"),
113 QColor("#009688"),
114 QColor("#4caf50"),
115 QColor("#8bc34a"),
116 QColor("#cddc39"),
117 QColor("#ffeb3b"),
118 QColor("#ffc107"),
119 QColor("#ff9800"),
120 QColor("#ff5722"),
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 auto index = hash % (grabColors().length() - 1);
142 // return a colour
143 return grabColors()[index];
144}
145
146auto NameUtils::isStringUnsuitableForInitials(const QString &string) -> bool
147{
148 if (string.isEmpty()) {
149 return true;
150 }
151
152 bool isNumber;
153 string.toFloat(&isNumber);
154 if (isNumber) {
155 return true;
156 }
157
158 static const std::array<QChar::Script, 6> scripts{
161
162 std::any_of(string.cbegin(), string.cend(), [](const QChar &character) {
163 return std::find(scripts.begin(), scripts.end(), character.script()) == scripts.end();
164 });
165
166 return false;
167}
168
169#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 Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.