Kirigami2

avatar.cpp
1 // SPDX-FileCopyrightText: 2020 Carson Black <[email protected]>
2 //
3 // SPDX-License-Identifier: LGPL-2.0-or-later
4 
5 #include "avatar.h"
6 #include <QDebug>
7 #include <QMap>
8 #include <QQuickStyle>
9 #include <QTextBoundaryFinder>
10 #include <QVector>
11 
12 bool contains(const QString &str, QChar::Script s)
13 {
14  for (auto rune : str) {
15  if (rune.script() == s) {
16  return true;
17  }
18  }
19  return false;
20 }
21 
22 QString NameUtils::initialsFromString(const QString &string)
23 {
24  // "" -> ""
25  if (string.isEmpty()) {
26  return {};
27  }
28 
29  QString normalized = string.normalized(QString::NormalizationForm_D);
30 
31  if (normalized.startsWith(QLatin1Char('#')) || normalized.startsWith(QLatin1Char('@'))) {
32  normalized.remove(0, 1);
33  }
34 
35  // Names written with Han and Hangul characters generally can be initialised by taking the
36  // first character
37  if (contains(normalized, QChar::Script_Han) || contains(normalized, QChar::Script_Hangul)) {
38  return normalized.at(0);
39  }
40 
41  // "FirstName Name Name LastName"
42  normalized = normalized.trimmed();
43  if (normalized.contains(QLatin1Char(' '))) {
44  // "FirstName Name Name LastName" -> "FirstName" "Name" "Name" "LastName"
45 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
46  const auto split = QStringView(normalized).split(QLatin1Char(' '));
47 #else
48  const auto split = normalized.splitRef(QLatin1Char(' '));
49 #endif
50 
51  // "FirstName"
52  auto first = split.first();
53  // "LastName"
54  auto last = split.last();
55  if (first.isEmpty()) {
56  // "" "LastName" -> "L"
57  return QString(last.front());
58  }
59  if (last.isEmpty()) {
60  // "FirstName" "" -> "F"
61  return QString(first.front());
62  }
63  // "FirstName" "LastName" -> "FL"
64  return QString(first.front()) + last.front();
65  // "OneName"
66  } else {
67  // "OneName" -> "O"
68  return QString(normalized.front());
69  }
70 }
71 
72 /* clang-format off */
73 const QMap<QString,QList<QColor>> c_colors = {
74  {
75  QStringLiteral("default"),
76  {
77  QColor("#e93a9a"),
78  QColor("#e93d58"),
79  QColor("#e9643a"),
80  QColor("#ef973c"),
81  QColor("#e8cb2d"),
82  QColor("#b6e521"),
83  QColor("#3dd425"),
84  QColor("#00d485"),
85  QColor("#00d3b8"),
86  QColor("#3daee9"),
87  QColor("#b875dc"),
88  QColor("#926ee4"),
89  }
90  },
91  {
92  QStringLiteral("Material"),
93  {
94  QColor("#f44336"),
95  QColor("#e91e63"),
96  QColor("#9c27b0"),
97  QColor("#673ab7"),
98  QColor("#3f51b5"),
99  QColor("#2196f3"),
100  QColor("#03a9f4"),
101  QColor("#00bcd4"),
102  QColor("#009688"),
103  QColor("#4caf50"),
104  QColor("#8bc34a"),
105  QColor("#cddc39"),
106  QColor("#ffeb3b"),
107  QColor("#ffc107"),
108  QColor("#ff9800"),
109  QColor("#ff5722"),
110  }
111  }
112 };
113 /* clang-format on */
114 
115 QList<QColor> grabColors()
116 {
117  if (c_colors.contains(QQuickStyle::name())) {
118  return c_colors[QQuickStyle::name()];
119  }
120  return c_colors[QStringLiteral("default")];
121 }
122 
123 auto NameUtils::colorsFromString(const QString &string) -> QColor
124 {
125  // We use a hash to get a "random" number that's always the same for
126  // a given string.
127  auto hash = qHash(string);
128  // hash modulo the length of the colors list minus one will always get us a valid
129  // index
130  auto index = hash % (grabColors().length() - 1);
131  // return a colour
132  return grabColors()[index];
133 }
134 
135 auto NameUtils::isStringUnsuitableForInitials(const QString &string) -> bool
136 {
137  if (string.isEmpty()) {
138  return true;
139  }
140 
141  bool isNumber;
142  string.toFloat(&isNumber);
143  if (isNumber) {
144  return true;
145  }
146 
148 
149  for (auto character : string) {
150  if (!scripts.contains(character.script())) {
151  return true;
152  }
153  }
154  return false;
155 }
NormalizationForm_D
QString trimmed() const const
QChar front() const const
QString normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const const
QVector< QStringRef > splitRef(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
QString name()
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
const QChar at(int position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:14:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.