Krita Source Code Documentation
Loading...
Searching...
No Matches
GlyphPaletteDialog.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
8#include <QQmlEngine>
9#include <QQmlContext>
10#include <QQuickItem>
11#include <QHBoxLayout>
12
13#include <KLocalizedString>
14
15#include <KoResourcePaths.h>
16#include <KoFontGlyphModel.h>
17#include <KoFontRegistry.h>
18#include <KoSvgText.h>
19
21 : KoDialog(parent)
22 , m_altPopup(new GlyphPaletteAltPopup(this))
23 , m_model(new KoFontGlyphModel(this))
24 , m_charMapModel(new GlyphPaletteProxyModel(this))
25{
26 setMinimumSize(500, 300);
27
30 m_quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31
32 this->setWindowTitle(i18nc("@title:window", "Glyph Palette"));
33
34 m_charMapModel->setSourceModel(m_model);
36 connect(m_model, SIGNAL(modelReset()), m_charMapModel, SLOT(emitBlockLabelsChanged()));
37
38 m_quickWidget->rootContext()->setContextProperty("glyphModel", QVariant::fromValue(m_model));
39 m_quickWidget->rootContext()->setContextProperty("charMapProxyModel", QVariant::fromValue(m_charMapModel));
40
41 m_quickWidget->setSource(QUrl("qrc:/GlyphPalette.qml"));
42 if (!m_quickWidget->errors().empty()) {
43 qWarning() << "Errors in " << windowTitle() << ":" << m_quickWidget->errors();
44 }
45 connect(m_altPopup, SIGNAL(sigInsertRichText(int,int,bool,bool)), this, SLOT(slotInsertRichText(int,int,bool,bool)));
46 m_quickWidget->setPalette(this->palette());
47}
48
54
55void GlyphPaletteDialog::setGlyphModelFromProperties(const QPair<KoSvgTextProperties, KoSvgTextProperties> &properties, const QString &text)
56{
57 if (m_lastUsedProperties.second.cssFontInfo() == properties.second.cssFontInfo()) {
58 if (m_model && m_model->rowCount() > 0) {
59 if (text.isEmpty()) return;
60 QModelIndex idx = m_model->indexForString(text);
61 if (idx.isValid() && m_quickWidget->rootObject()) {
62 m_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));
63 return;
64 }
65 }
66 }
67 const qreal res = 72.0;
68 QVector<int> lengths;
69 const KoCSSFontInfo info = properties.second.cssFontInfo();
70 const std::vector<FT_FaceSP> faces = KoFontRegistry::instance()->facesForCSSValues(
71 lengths,
72 info,
73 text,
74 static_cast<quint32>(res),
75 static_cast<quint32>(res));
76
77 QString language = properties.second.propertyOrDefault(KoSvgTextProperties::TextLanguage).toString();
78 if (faces.empty()) return;
79 m_model->setFace(faces.front(), QLatin1String(language.toLatin1()));
80
81 QVariantMap map = properties.second.propertyOrDefault(KoSvgTextProperties::FontVariationSettingsId).toMap();
82
83 QModelIndex idx = m_model->indexForString(text);
84 if (m_quickWidget->rootObject()) {
85 m_quickWidget->rootObject()->setProperty("fontFamilies", QVariant::fromValue(info.families));
86 m_quickWidget->rootObject()->setProperty("fontSize", QVariant::fromValue(info.size));
87 m_quickWidget->rootObject()->setProperty("fontWeight", QVariant::fromValue(info.weight));
88 m_quickWidget->rootObject()->setProperty("fontWidth", QVariant::fromValue(info.width));
89 m_quickWidget->rootObject()->setProperty("fontStyle", QVariant::fromValue(info.slantMode));
90 m_quickWidget->rootObject()->setProperty("fontAxesValues", QVariant::fromValue(map));
91 m_quickWidget->rootObject()->setProperty("language", QVariant::fromValue(language));
92 if (idx.isValid()) {
93 m_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));
94 }
95 }
96 if (m_altPopup) {
97 m_altPopup->setMarkup(info.families, info.size, info.weight, info.width, info.slantMode, map, language);
98 }
99 m_lastUsedProperties = properties;
100}
101
102void GlyphPaletteDialog::slotInsertRichText(const int charRow, const int glyphRow, const bool replace, const bool useCharMap)
103{
104 if (m_quickWidget->rootObject()) {
105 QAbstractItemModel *model = useCharMap? qobject_cast<QAbstractItemModel*>(m_charMapModel): qobject_cast<QAbstractItemModel*>(m_model);
106 QModelIndex idx = model->index(charRow, 0);
107 QString text = idx.isValid()? model->data(idx, Qt::DisplayRole).toString(): QString();
109 QVariantMap otf;
110
111 if (glyphRow > -1) {
112 idx = model->index(glyphRow, 0, idx);
113 text = model->data(idx, Qt::DisplayRole).toString();
114 otf = model->data(idx, KoFontGlyphModel::OpenTypeFeatures).toMap();
115 }
116
117 if (!text.isEmpty()) {
118 KoSvgTextShape *richText = new KoSvgTextShape();
119
120 if (!otf.isEmpty()) {
121 props.setProperty(KoSvgTextProperties::FontFeatureSettingsId, QVariant::fromValue(otf));
122 }
123
124 richText->setPropertiesAtPos(-1, props);
125 richText->insertText(0, text);
126 emit signalInsertRichText(richText, replace);
127 }
128 if (m_altPopup->isVisible()) {
130 }
131 }
132
133}
134
135void GlyphPaletteDialog::slotShowPopupPalette(const int charRow, const int x, const int y, const int cellWidth, const int cellHeight)
136{
137 m_altPopup->setRootIndex(charRow);
138 m_altPopup->setCellSize(cellWidth, cellHeight);
139 m_altPopup->raise();
140 m_altPopup->show();
141 m_altPopup->move(this->mapToGlobal(QPoint(x, y)+m_quickWidget->pos()));
142 m_altPopup->activateWindow();
143}
144
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
The GlyphPaletteAltPopup class.
void setModel(QAbstractItemModel *model)
setModel Set the glyph proxy model.
void setCellSize(const int width, const int height)
setCellSize Set the size of the pop-up relative to its cellsize. The popup is 3 cells wide and 4 high...
void setMarkup(const QStringList &families, const int size, const int weight, const int width, const QFont::Style style, const QVariantMap &axes, const QString &language)
setMarkup Set the css font markup to be used inside the palette.
void setRootIndex(const int index)
setRootIndex Set the charmap root index to show glyph alts for.
GlyphPaletteDialog(QWidget *parent=nullptr)
void slotInsertRichText(const int charRow, const int glyphRow=-1, const bool replace=false, const bool useCharMap=false)
KoFontGlyphModel * m_model
GlyphPaletteProxyModel * m_charMapModel
QPair< KoSvgTextProperties, KoSvgTextProperties > m_lastUsedProperties
void signalInsertRichText(KoSvgTextShape *text, bool replace)
GlyphPaletteAltPopup * m_altPopup
void slotShowPopupPalette(const int charRow, const int x=0, const int y=0, const int cellWidth=100, const int cellHeight=100)
void setGlyphModelFromProperties(const QPair< KoSvgTextProperties, KoSvgTextProperties > &properties, const QString &text)
setGlyphModelFromProperties This updates the glyph model.
KisQQuickWidget * m_quickWidget
The GlyphPaletteProxyModel class.
The KisQQuickWidget class.
A dialog base class with standard buttons and predefined layouts.
Definition KoDialog.h:116
void setMainWidget(QWidget *widget)
Definition KoDialog.cpp:354
The KoFontGlyphModel class Creates a tree model of all the glyphs in a given face.
void setFace(FT_FaceSP face, QLatin1String language=QLatin1String(), bool samplesOnly=false)
setFace set the face to retrieve glyph data for.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex indexForString(QString grapheme)
std::vector< FT_FaceSP > facesForCSSValues(QVector< int > &lengths, KoCSSFontInfo info=KoCSSFontInfo(), const QString &text="", quint32 xRes=72, quint32 yRes=72, bool disableFontMatching=false, const QString &language=QString())
facesForCSSValues This selects a font with fontconfig using the given values. If "text" is not empty ...
static KoFontRegistry * instance()
@ FontFeatureSettingsId
QStringList.
@ FontVariationSettingsId
QStringList.
@ TextLanguage
a language string.
void setProperty(PropertyId id, const QVariant &value)
void setPropertiesAtPos(int pos, KoSvgTextProperties properties)
setPropertiesAtPos will set the properties at pos.
bool insertText(int pos, QString text)
insertText Insert a text somewhere in the KoTextShape.
rgba palette[MAX_PALETTE]
Definition palette.c:35
The KoCSSFontInfo class Convenience struct to make it easier to use KoFontRegistry....
QStringList families
QFont::Style slantMode