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 override
 
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 override
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 236 of file FontStyleModel.cpp.

237{
238 Q_UNUSED(parent)
239 return 1;
240}

References parent().

◆ data()

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

Definition at line 242 of file FontStyleModel.cpp.

243{
244 if (!index.isValid()) return QVariant();
245
246 KoSvgText::FontFamilyStyleInfo style = d->styles.at(index.row());
247 if (role == Qt::DisplayRole) {
248 if (style.localizedLabels.isEmpty()) return QString();
249 QString label = style.localizedLabels.value(QLocale(QLocale::English), style.localizedLabels.values().first());
250 Q_FOREACH(const QLocale &locale, d->locales) {
251 if (style.localizedLabels.keys().contains(locale)) {
252 label = style.localizedLabels.value(locale, label);
253 break;
254 }
255 }
256 return label;
257 } else if (role == Weight){
258 return style.instanceCoords.value(WEIGHT_TAG, 400.0);
259 } else if (role == Width){
260 return style.instanceCoords.value(WIDTH_TAG, 100.0);
261 } else if (role == StyleMode){
262 QFont::Style styleType = style.isItalic? style.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;
263 if (style.instanceCoords.value(ITALIC_TAG, 0) != 0) {
264 styleType = QFont::StyleItalic;
265 } else if (style.instanceCoords.value(SLANT_TAG, 0) != 0) {
266 styleType = QFont::StyleOblique;
267 }
268 return styleType;
269 } else if (role == Slant){
270 return -(style.instanceCoords.value(SLANT_TAG, 0));
271 } else if (role == AxisValues) {
272 QVariantHash vals;
273 for (auto it = style.instanceCoords.begin(); it != style.instanceCoords.end(); it++) {
274 if (!(it.key() == WEIGHT_TAG
275 || it.key() == WIDTH_TAG
276 || it.key() == SLANT_TAG
277 || it.key() == ITALIC_TAG)) {
278 vals.insert(it.key(), QVariant::fromValue(it.value()));
279 }
280 }
281 return vals;
282 }
283 return QVariant();
284}
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 216 of file FontStyleModel.cpp.

217{
218 Q_UNUSED(parent)
219 if (column != 0) return QModelIndex();
220 if (row >= 0 && row < d->styles.size()) return createIndex(row, column, &row);
221 return QModelIndex();
222}

References parent().

◆ parent()

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

Definition at line 224 of file FontStyleModel.cpp.

225{
226 Q_UNUSED(child)
227 return QModelIndex();
228}

◆ roleNames()

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

Definition at line 286 of file FontStyleModel.cpp.

287{
288 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
289 roles[Weight] = "weight";
290 roles[Width] = "width";
291 roles[StyleMode] = "stylemode";
292 roles[Slant] = "slant";
293 roles[AxisValues] = "axisvalues";
294 return roles;
295}

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

◆ rowCount()

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

Definition at line 230 of file FontStyleModel.cpp.

231{
232 Q_UNUSED(parent)
233 return d->styles.size();
234}

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 125 of file FontStyleModel.cpp.

126{
127 QHash<int, KoSvgText::FontFamilyStyleInfo> styles;
128 QVector<qreal> weights;
129 QVector<qreal> widths;
130 QVector<qreal> slants;
131 QVector<qreal> italics;
132 for (int i = 0; i< d->styles.size(); i++) {
133 styles.insert(i, d->styles.at(i));
134 weights.append(d->styles.value(i).instanceCoords.value(WEIGHT_TAG, 400.0));
135 widths.append(d->styles.value(i).instanceCoords.value(WIDTH_TAG, 100.0));
136 int fontStyle = d->styles.value(i).isItalic? d->styles.value(i).isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;
137 if (d->styles.value(i).instanceCoords.contains(SLANT_TAG) || fontStyle == QFont::StyleOblique) {
138 slants.append(d->styles.value(i).instanceCoords.value(SLANT_TAG, -11.0));
139 }
140 if (d->styles.value(i).instanceCoords.contains(ITALIC_TAG) || fontStyle == QFont::StyleItalic) {
141 italics.append(d->styles.value(i).instanceCoords.value(ITALIC_TAG, 1.0));
142 }
143 }
144
145 if (styles.size() > 1) {
146 styles = searchAxisTag(WIDTH_TAG, width, widths, 100.0, 100.0, styles);
147 }
148 if (styles.size() > 1) {
149 styles = searchAxisTag(WEIGHT_TAG, weight, weights, 400.0, 500.0, styles);
150 }
151 // Test all the italics and slant stuff, complete with fallbacks...
152 if (styles.size() > 1) {
153 QHash<int, KoSvgText::FontFamilyStyleInfo> italicStyles;
154 QHash<int, KoSvgText::FontFamilyStyleInfo> obliqueStyles;
155 if (!slants.isEmpty()) {
156 obliqueStyles = searchAxisTag(SLANT_TAG,
157 styleMode == QFont::StyleItalic? -11.0: -styleValue, slants,
158 0.0,
159 0.0,
160 styles);
161 }
162 if (!italics.isEmpty()) {
163 italicStyles = searchAxisTag(ITALIC_TAG,
164 1.0,
165 italics,
166 0.0,
167 0.0,
168 styles);
169 }
170 if (styleMode == QFont::StyleItalic) {
171 if (!italicStyles.isEmpty()) {
172 styles = italicStyles;
173 } else if (!obliqueStyles.isEmpty()) {
174 styles = obliqueStyles;
175 }
176 } else if (styleMode == QFont::StyleOblique) {
177 if (!obliqueStyles.isEmpty()) {
178 styles = obliqueStyles;
179 } else if (!italicStyles.isEmpty()) {
180 styles = italicStyles;
181 }
182 } else {
183 QHash<int, KoSvgText::FontFamilyStyleInfo> regularStyles;
184 for (auto it = styles.begin(); it!= styles.end(); it++) {
185 if (!obliqueStyles.contains(it.key()) && !italicStyles.contains(it.key())) {
186 regularStyles.insert(it.key(), it.value());
187 }
188 }
189 if (!regularStyles.isEmpty()) styles = regularStyles;
190 }
191 }
192 return styles.isEmpty()? 0: styles.keys().first();
193}
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 195 of file FontStyleModel.cpp.

196{
197 QStringList families = textPropertiesModel->fontFamilies();
199
200 if (!families.isEmpty() && d->fontModel) {
201
202 std::optional<QString> name = KoFontRegistry::instance()->wwsNameByFamilyName(families.first());
203 QString familyName = !name? families.first(): name.value();
204
205 QVector<KoResourceSP> res = d->fontModel->resourcesForFilename(familyName);
206 if (!res.isEmpty()) {
207 KoFontFamilySP family = res.first().staticCast<KoFontFamily>();
208 if (family) {
209 styles = family->styles();
210 }
211 }
212 }
213 setStylesInfo(styles);
214}
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: