Krita Source Code Documentation
Loading...
Searching...
No Matches
FontStyleModel Class Reference

The FontStyleModel class. More...

#include <FontStyleModel.h>

+ Inheritance diagram for FontStyleModel:

Classes

struct  Private
 

Public Member Functions

Q_INVOKABLE QVariantHash axesValues (int row)
 
int columnCount (const QModelIndex &parent) const override
 
QVariant data (const QModelIndex &index, int role) const override
 
 FontStyleModel (QObject *parent=nullptr)
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex parent (const QModelIndex &child) const
 
QHash< int, QByteArray > roleNames () const override
 
int rowCount (const QModelIndex &parent) const override
 
Q_INVOKABLE int rowForStyle (const qreal &weight, const qreal &width, const int &styleMode, const qreal &styleValue)
 Find the closest style that represents the current width, weight and stylemode.
 
Q_INVOKABLE void setFromTextPropertiesModel (KoSvgTextPropertiesModel *textPropertiesModel)
 Update styles model from current text properties.
 
void setStylesInfo (QList< KoSvgText::FontFamilyStyleInfo > styles)
 Set the base style info;.
 
Q_INVOKABLE qreal slantValue (int row)
 
Q_INVOKABLE int styleModeValue (int row)
 
Q_INVOKABLE qreal weightValue (int row)
 
Q_INVOKABLE qreal widthValue (int row)
 
 ~FontStyleModel ()
 

Private Types

enum  Roles {
  Weight = Qt::UserRole + 1 , Width , StyleMode , Slant ,
  AxisValues
}
 

Private Attributes

const QScopedPointer< Privated
 

Detailed Description

The FontStyleModel class.

This models the styles info present in KoFontFamily Resources.

Definition at line 19 of file FontStyleModel.h.

Member Enumeration Documentation

◆ Roles

enum FontStyleModel::Roles
private
Enumerator
Weight 

qreal, represents 'wgth'

Width 

qreal, represents 'wdth'

StyleMode 

QFont::Style.

Slant 

qreal, represents 'slnt'

AxisValues 

other axis values

Definition at line 22 of file FontStyleModel.h.

22 {
23 Weight = Qt::UserRole + 1,
24 Width,
26 Slant,
28 };
@ AxisValues
other axis values
@ StyleMode
QFont::Style.
@ Width
qreal, represents 'wdth'
@ Weight
qreal, represents 'wgth'
@ Slant
qreal, represents 'slnt'

Constructor & Destructor Documentation

◆ FontStyleModel()

FontStyleModel::FontStyleModel ( QObject * parent = nullptr)

Definition at line 25 of file FontStyleModel.cpp.

26 : QAbstractItemModel(parent)
27 , d(new Private)
28{
30 QList<QLocale> locales;
31 Q_FOREACH (const QString langCode, KLocalizedString::languages()) {
32 locales.append(QLocale(langCode));
33 }
34 d->locales = locales;
35}
QModelIndex parent(const QModelIndex &child) const
const QScopedPointer< Private > d
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
const QString FontFamilies

References d, and ResourceType::FontFamilies.

◆ ~FontStyleModel()

FontStyleModel::~FontStyleModel ( )

Definition at line 37 of file FontStyleModel.cpp.

38{
39}

Member Function Documentation

◆ axesValues()

QVariantHash FontStyleModel::axesValues ( int row)

Definition at line 89 of file FontStyleModel.cpp.

90{
91 return data(index(row, 0), AxisValues).toHash();
92}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override

References AxisValues, data(), and index().

◆ columnCount()

int FontStyleModel::columnCount ( const QModelIndex & parent) const
override

Definition at line 225 of file FontStyleModel.cpp.

226{
227 Q_UNUSED(parent)
228 return 1;
229}

References parent().

◆ data()

QVariant FontStyleModel::data ( const QModelIndex & index,
int role ) const
override

Definition at line 231 of file FontStyleModel.cpp.

232{
233 if (!index.isValid()) return QVariant();
234
235 KoSvgText::FontFamilyStyleInfo style = d->styles.at(index.row());
236 if (role == Qt::DisplayRole) {
237 if (style.localizedLabels.isEmpty()) return QString();
238 QString label = style.localizedLabels.value(QLocale(QLocale::English), style.localizedLabels.values().first());
239 Q_FOREACH(const QLocale &locale, d->locales) {
240 if (style.localizedLabels.keys().contains(locale)) {
241 label = style.localizedLabels.value(locale, label);
242 break;
243 }
244 }
245 return label;
246 } else if (role == Weight){
247 return style.instanceCoords.value(WEIGHT_TAG, 400.0);
248 } else if (role == Width){
249 return style.instanceCoords.value(WIDTH_TAG, 100.0);
250 } else if (role == StyleMode){
251 QFont::Style styleType = style.isItalic? style.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;
252 if (style.instanceCoords.value(ITALIC_TAG, 0) != 0) {
253 styleType = QFont::StyleItalic;
254 } else if (style.instanceCoords.value(SLANT_TAG, 0) != 0) {
255 styleType = QFont::StyleOblique;
256 }
257 return styleType;
258 } else if (role == Slant){
259 return -(style.instanceCoords.value(SLANT_TAG, 0));
260 } else if (role == AxisValues) {
261 QVariantHash vals;
262 for (auto it = style.instanceCoords.begin(); it != style.instanceCoords.end(); it++) {
263 if (!(it.key() == WEIGHT_TAG
264 || it.key() == WIDTH_TAG
265 || it.key() == SLANT_TAG
266 || it.key() == ITALIC_TAG)) {
267 vals.insert(it.key(), QVariant::fromValue(it.value()));
268 }
269 }
270 return vals;
271 }
272 return QVariant();
273}
const QString SLANT_TAG
const QString WEIGHT_TAG
const QString ITALIC_TAG
const QString WIDTH_TAG
QHash< QString, float > instanceCoords
Definition KoSvgText.h:816
QHash< QLocale, QString > localizedLabels
Definition KoSvgText.h:815

References AxisValues, d, index(), KoSvgText::FontFamilyStyleInfo::instanceCoords, KoSvgText::FontFamilyStyleInfo::isItalic, KoSvgText::FontFamilyStyleInfo::isOblique, ITALIC_TAG, KoSvgText::FontFamilyStyleInfo::localizedLabels, Slant, SLANT_TAG, StyleMode, Weight, WEIGHT_TAG, Width, and WIDTH_TAG.

◆ index()

QModelIndex FontStyleModel::index ( int row,
int column,
const QModelIndex & parent = QModelIndex() ) const
override

Definition at line 205 of file FontStyleModel.cpp.

206{
207 Q_UNUSED(parent)
208 if (column != 0) return QModelIndex();
209 if (row >= 0 && row < d->styles.size()) return createIndex(row, column, &row);
210 return QModelIndex();
211}

References parent().

◆ parent()

QModelIndex FontStyleModel::parent ( const QModelIndex & child) const

Definition at line 213 of file FontStyleModel.cpp.

214{
215 Q_UNUSED(child)
216 return QModelIndex();
217}

◆ roleNames()

QHash< int, QByteArray > FontStyleModel::roleNames ( ) const
override

Definition at line 275 of file FontStyleModel.cpp.

276{
277 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
278 roles[Weight] = "weight";
279 roles[Width] = "width";
280 roles[StyleMode] = "stylemode";
281 roles[Slant] = "slant";
282 roles[AxisValues] = "axisvalues";
283 return roles;
284}

References AxisValues, Slant, StyleMode, Weight, and Width.

◆ rowCount()

int FontStyleModel::rowCount ( const QModelIndex & parent) const
override

Definition at line 219 of file FontStyleModel.cpp.

220{
221 Q_UNUSED(parent)
222 return d->styles.size();
223}

References d, and parent().

◆ rowForStyle()

int FontStyleModel::rowForStyle ( const qreal & weight,
const qreal & width,
const int & styleMode,
const qreal & styleValue )

Find the closest style that represents the current width, weight and stylemode.

Definition at line 114 of file FontStyleModel.cpp.

115{
116 QHash<int, KoSvgText::FontFamilyStyleInfo> styles;
117 QVector<qreal> weights;
118 QVector<qreal> widths;
119 QVector<qreal> slants;
120 QVector<qreal> italics;
121 for (int i = 0; i< d->styles.size(); i++) {
122 styles.insert(i, d->styles.at(i));
123 weights.append(d->styles.value(i).instanceCoords.value(WEIGHT_TAG, 400.0));
124 widths.append(d->styles.value(i).instanceCoords.value(WIDTH_TAG, 100.0));
125 int fontStyle = d->styles.value(i).isItalic? d->styles.value(i).isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;
126 if (d->styles.value(i).instanceCoords.contains(SLANT_TAG) || fontStyle == QFont::StyleOblique) {
127 slants.append(d->styles.value(i).instanceCoords.value(SLANT_TAG, -11.0));
128 }
129 if (d->styles.value(i).instanceCoords.contains(ITALIC_TAG) || fontStyle == QFont::StyleItalic) {
130 italics.append(d->styles.value(i).instanceCoords.value(ITALIC_TAG, 1.0));
131 }
132 }
133
134 if (styles.size() > 1) {
135 styles = searchAxisTag(WIDTH_TAG, width, widths, 100.0, 100.0, styles);
136 }
137 if (styles.size() > 1) {
138 styles = searchAxisTag(WEIGHT_TAG, weight, weights, 400.0, 500.0, styles);
139 }
140 // Test all the italics and slant stuff, complete with fallbacks...
141 if (styles.size() > 1) {
142 QHash<int, KoSvgText::FontFamilyStyleInfo> italicStyles;
143 QHash<int, KoSvgText::FontFamilyStyleInfo> obliqueStyles;
144 if (!slants.isEmpty()) {
145 obliqueStyles = searchAxisTag(SLANT_TAG,
146 styleMode == QFont::StyleItalic? -11.0: -styleValue, slants,
147 0.0,
148 0.0,
149 styles);
150 }
151 if (!italics.isEmpty()) {
152 italicStyles = searchAxisTag(ITALIC_TAG,
153 1.0,
154 italics,
155 0.0,
156 0.0,
157 styles);
158 }
159 if (styleMode == QFont::StyleItalic) {
160 if (!italicStyles.isEmpty()) {
161 styles = italicStyles;
162 } else if (!obliqueStyles.isEmpty()) {
163 styles = obliqueStyles;
164 }
165 } else if (styleMode == QFont::StyleOblique) {
166 if (!obliqueStyles.isEmpty()) {
167 styles = obliqueStyles;
168 } else if (!italicStyles.isEmpty()) {
169 styles = italicStyles;
170 }
171 } else {
172 QHash<int, KoSvgText::FontFamilyStyleInfo> regularStyles;
173 for (auto it = styles.begin(); it!= styles.end(); it++) {
174 if (!obliqueStyles.contains(it.key()) && !italicStyles.contains(it.key())) {
175 regularStyles.insert(it.key(), it.value());
176 }
177 }
178 if (!regularStyles.isEmpty()) styles = regularStyles;
179 }
180 }
181 return styles.isEmpty()? 0: styles.keys().first();
182}
QHash< int, KoSvgText::FontFamilyStyleInfo > searchAxisTag(const QString &tag, const qreal &value, const QVector< qreal > &values, const qreal &defaultVal, const qreal &defaultValUpper, const QHash< int, KoSvgText::FontFamilyStyleInfo > &styles)

References d, ITALIC_TAG, searchAxisTag(), SLANT_TAG, WEIGHT_TAG, and WIDTH_TAG.

◆ setFromTextPropertiesModel()

void FontStyleModel::setFromTextPropertiesModel ( KoSvgTextPropertiesModel * textPropertiesModel)

Update styles model from current text properties.

Definition at line 184 of file FontStyleModel.cpp.

185{
186 QStringList families = textPropertiesModel->fontFamilies();
188
189 if (!families.isEmpty() && d->fontModel) {
190
191 std::optional<QString> name = KoFontRegistry::instance()->wwsNameByFamilyName(families.first());
192 QString familyName = !name? families.first(): name.value();
193
194 QVector<KoResourceSP> res = d->fontModel->resourcesForFilename(familyName);
195 if (!res.isEmpty()) {
196 KoFontFamilySP family = res.first().staticCast<KoFontFamily>();
197 if (family) {
198 styles = family->styles();
199 }
200 }
201 }
202 setStylesInfo(styles);
203}
void setStylesInfo(QList< KoSvgText::FontFamilyStyleInfo > styles)
Set the base style info;.
The KoFontFamily class Abstract representation of a Weight/Width/Slant font family,...
std::optional< QString > wwsNameByFamilyName(const QString familyName) const
static KoFontRegistry * instance()
const char * name(StandardAction id)

References d, KoFontRegistry::instance(), setStylesInfo(), and KoFontRegistry::wwsNameByFamilyName().

◆ setStylesInfo()

void FontStyleModel::setStylesInfo ( QList< KoSvgText::FontFamilyStyleInfo > styles)

Set the base style info;.

Definition at line 54 of file FontStyleModel.cpp.

55{
56 beginResetModel();
57 std::sort(styles.begin(), styles.end(), styleLowerThan);
58 d->styles = styles;
59 endResetModel();
60}
static bool styleLowerThan(const KoSvgText::FontFamilyStyleInfo &a, const KoSvgText::FontFamilyStyleInfo &b)

References d, and styleLowerThan().

◆ slantValue()

qreal FontStyleModel::slantValue ( int row)

Definition at line 84 of file FontStyleModel.cpp.

85{
86 return -(d->styles.value(row).instanceCoords.value(SLANT_TAG, 0));
87}

References d, and SLANT_TAG.

◆ styleModeValue()

int FontStyleModel::styleModeValue ( int row)

Definition at line 72 of file FontStyleModel.cpp.

73{
74 KoSvgText::FontFamilyStyleInfo style = d->styles.value(row);
75 QFont::Style styleType = style.isItalic? style.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;
76 if (style.instanceCoords.value(ITALIC_TAG, 0) != 0) {
77 styleType = QFont::StyleItalic;
78 } else if (style.instanceCoords.value(SLANT_TAG, 0) != 0) {
79 styleType = QFont::StyleOblique;
80 }
81 return styleType;
82}

References d, KoSvgText::FontFamilyStyleInfo::instanceCoords, KoSvgText::FontFamilyStyleInfo::isItalic, KoSvgText::FontFamilyStyleInfo::isOblique, ITALIC_TAG, and SLANT_TAG.

◆ weightValue()

qreal FontStyleModel::weightValue ( int row)

Definition at line 62 of file FontStyleModel.cpp.

63{
64 return d->styles.value(row).instanceCoords.value(WEIGHT_TAG, 400.0);
65}

References d, and WEIGHT_TAG.

◆ widthValue()

qreal FontStyleModel::widthValue ( int row)

Definition at line 67 of file FontStyleModel.cpp.

68{
69 return d->styles.value(row).instanceCoords.value(WIDTH_TAG, 100.0);
70}

References d, and WIDTH_TAG.

Member Data Documentation

◆ d

const QScopedPointer<Private> FontStyleModel::d
private

Definition at line 59 of file FontStyleModel.h.


The documentation for this class was generated from the following files: