Krita Source Code Documentation
Loading...
Searching...
No Matches
GlyphPaletteProxyModel.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
7#include <KLocalizedString>
8#include <QDebug>
10
16
18 : QSortFilterProxyModel(parent)
19 , d(new Private)
20{
21
22}
23
27
29{
30 return d->filterIndex;
31}
32
34{
35 return d->searchText;
36}
37
39{
40 QVariantList labels;
41
42 KoFontGlyphModel *model = qobject_cast<KoFontGlyphModel*>(sourceModel());
43 labels.append(QVariantMap({{"name", i18nc("@title", "All glyphs")}, {"value", 0}}));
44 if (model) {
45 for (int i=0; i < model->blocks().size(); i++) {
46 labels.append(QVariantMap({{"name", model->blocks().at(i).name}, {"value", i+1}}));
47 }
48 }
49 return labels;
50}
51
53{
54 QString adjustedText = text;
55 if (text.startsWith("U+")) {
56 bool ok = false;
57 const uint code = text.mid(2).toUInt(&ok, 16);
58 if (ok) {
59 adjustedText = QString::fromUcs4(&code, 1);
60 }
61 }
62 if (!d->searchText.isEmpty() && !adjustedText.isEmpty() && d->searchText.toUcs4().first() == adjustedText.toUcs4().first()) {
63 return;
64 } else {
65 d->searchText = adjustedText.isEmpty()? adjustedText: QString::fromUcs4(&adjustedText.toUcs4().first(), 1);
66 emit searchTextChanged();
67 invalidateFilter();
68 }
69}
70
72{
73 if (d->filterIndex == filter) {
74 return;
75 }
76 if (filter == 0) {
77 d->filterIndex = 0;
79 emit blockFilterChanged();
80 invalidateFilter();
81 } else {
82 KoFontGlyphModel *model = qobject_cast<KoFontGlyphModel*>(sourceModel());
83 if (model) {
84 int actualFilter = filter - 1;
85 if (actualFilter < model->blocks().size()) {
86 d->filterIndex = filter;
87 d->block = model->blocks().at(actualFilter);
88 emit blockFilterChanged();
89 invalidateFilter();
90 }
91 }
92 }
93}
94
99
100bool GlyphPaletteProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
101{
102 if (sourceParent.isValid()) return true;
103 const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
104 const QString main = sourceModel()->data(idx).toString();
105 if (main.isEmpty()) return false;
106 const uint firstChar = main.toUcs4().first();
107
108 if (!d->searchText.isEmpty()) {
109 const QString decomposition = QChar::decomposition(firstChar);
110 const uint searchFirst = d->searchText.toUcs4().first();
111 return searchFirst == firstChar
112 || (!decomposition.isEmpty() && searchFirst == decomposition.toUcs4().first());
113 }
114 if (d->filterIndex == 0) return true;
115
116 return d->block.match(firstChar);
117}
unsigned int uint
GlyphPaletteProxyModel(QObject *parent=nullptr)
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QScopedPointer< Private > d
void setBlockFilter(int filter)
setBlockFilter set the unicode block filter.
void setSearchText(const QString &text)
setSearchText
void emitBlockLabelsChanged()
emitBlockLabelsChanged This called "block labels changed" and is should be used when the source KoFon...
The KoFontGlyphModel class Creates a tree model of all the glyphs in a given face.
QVector< KoUnicodeBlockData > blocks() const
blocks
int main(int argc, char **argv)
Definition main.cpp:26
static KoUnicodeBlockData noBlock()