File: fontlistmodel.cpp

package info (click to toggle)
scribus 1.4.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 246,704 kB
  • ctags: 25,243
  • sloc: cpp: 272,543; xml: 15,558; ansic: 3,438; python: 3,427; makefile: 1,160; perl: 94; sh: 41
file content (257 lines) | stat: -rw-r--r-- 6,778 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/

#include <QCheckBox>
#include "fontlistmodel.h"
#include "prefsmanager.h"
#include "util_icon.h"
#include "scribusdoc.h"
#include "commonstrings.h"


FontListModel::FontListModel(QObject * parent, ScribusDoc * doc)
	: QAbstractTableModel(parent),
	m_doc(doc),
	m_fonts(PrefsManager::instance()->appPrefs.AvailFonts)
{
	ttfFont = loadIcon("font_truetype16.png");
	otfFont = loadIcon("font_otf16.png");
	psFont = loadIcon("font_type1_16.png");
	substFont = loadIcon("font_subst16.png");
	m_font_values = m_fonts.values();
}

void FontListModel::setFonts(SCFonts f)
{
	m_fonts = f;
	m_font_values = m_fonts.values();
	reset();
}

int FontListModel::rowCount(const QModelIndex&) const
{
	return m_fonts.size();
}

int FontListModel::rowCount()
{
	return m_fonts.size();
}

int FontListModel::columnCount(const QModelIndex&) const
{
	return 12;
}

QVariant FontListModel::headerData(int section,
								   Qt::Orientation orientation,
								   int role) const
{
	if (orientation == Qt::Vertical)
		return QVariant(); // no verticals
	// TODO for tooltips etc.
	if (role != Qt::DisplayRole)
		return QVariant();

	switch (section)
	{
		case FontListModel::FontName:
			return tr("Font Name");
		case FontListModel::FontUsable:
			return tr("Use Font");
		case FontListModel::FontFamily:
			return tr("Family");
		case FontListModel::FontStyle:
			return tr("Style");
		case FontListModel::FontVariant:
			return tr("Variant");
		case FontListModel::FontType:
			return tr("Type");
		case FontListModel::FontFormat:
			return tr("Format");
		case FontListModel::FontEmbed:
			return tr("Embed in PostScript");
		case FontListModel::FontSubset:
			return tr("Subset");
		case FontListModel::FontAccess:
			return tr("Access");
		case FontListModel::FontInDoc:
			return tr("Used in Doc");
		case FontListModel::FontFile:
			return tr("Path to Font File");
		default:
			return "Never should be shown";
	};

	// dummy return
	return QVariant();
}

QVariant FontListModel::data(const QModelIndex & index,
							 int role) const
{
//	ScFace font = m_fonts[m_fonts.keys().at(index.row())];
	ScFace font = m_font_values[index.row()];

	if (role == Qt::DecorationRole && index.column() == FontListModel::FontType)
	{
		switch (font.type())
		{
			case ScFace::TYPE0:
			case ScFace::TYPE1:
			case ScFace::TYPE3:
				return QVariant(psFont);
			case ScFace::TTF:
				return QVariant(ttfFont);
			case ScFace::OTF:
				return QVariant(otfFont);
			default:
				return QVariant();
		};
	}

	if (role == Qt::DisplayRole)
	{
		switch (index.column())
		{
			case FontListModel::FontName:
				return font.scName();
			case FontListModel::FontFamily:
				return font.family();
			case FontListModel::FontStyle:
				return font.style();
			case FontListModel::FontVariant:
				return font.variant();
			case FontListModel::FontType:
			{
				switch (font.type())
				{
					case ScFace::TYPE0:
					case ScFace::TYPE1:
					case ScFace::TYPE3:
						return "Type1";
					case ScFace::TTF:
						return "TrueType";
					case ScFace::CFF:
						return "CFF";
					case ScFace::OTF:
						return "OpenType";
					default:
						return tr("Unknown", "font type");
				};
			}
			case FontListModel::FontFormat:
			{
				switch (font.format())
				{
					case ScFace::PFA: return "PFA";
					case ScFace::PFB: return "PFB";
					case ScFace::TYPE2: return "TYPE2";
					case ScFace::TYPE42: return "TYPE42";
					case ScFace::SFNT: return "SFNT";
					case ScFace::TTCF: return "TTCF";
					default: return tr("Unknown", "font format");
				};
			}
			case FontListModel::FontAccess:
			{
				QFileInfo fi(font.fontFilePath());
				return fi.absoluteFilePath().contains(QDir::homePath()) ?
						tr("User", "font preview"):
						tr("System", "font preview");
			}
			case FontListModel::FontInDoc:
			{
				if (m_doc && m_doc->UsedFonts.contains(font.scName()))
					return CommonStrings::trYes;
				return QVariant();
			}
			case FontListModel::FontFile:
				return font.fontFilePath();
			case FontListModel::SortIndex:
				return font.scName().toLower();
			default:
				return QVariant();
		};
	}

	if (role == Qt::ToolTipRole
		   &&
		   (index.column() == FontListModel::FontUsable
		   || index.column() == FontListModel::FontEmbed
		   || index.column() == FontListModel::FontSubset
		   )
	   )
	{
		return tr("Click to change the value");
	}

	if (role == Qt::CheckStateRole && index.column() == FontListModel::FontUsable)
		return (font.usable() ? Qt::Checked : Qt::Unchecked);
	if (role == Qt::CheckStateRole && index.column() == FontListModel::FontEmbed)
		return (font.embedPs() ? Qt::Checked : Qt::Unchecked);
	if (role == Qt::CheckStateRole && index.column() == FontListModel::FontSubset)
		return (font.subset() ? Qt::Checked : Qt::Unchecked);

	return QVariant();
}

Qt::ItemFlags FontListModel::flags(const QModelIndex &index) const
{
	Qt::ItemFlags defaultFlags =  Qt::ItemIsEnabled | Qt::ItemIsSelectable;

	if (!index.isValid())
		return QAbstractTableModel::flags(index);
	if (index.column() == FontListModel::FontUsable
		   || index.column() == FontListModel::FontEmbed
		   || index.column() == FontListModel::FontSubset)
		return Qt::ItemIsUserCheckable | /*Qt::ItemIsEditable |*/ defaultFlags;
	else
		return defaultFlags;
}

bool FontListModel::setData(const QModelIndex & index,
							const QVariant & value,
							int role)
{
	if (!index.isValid() || role != Qt::CheckStateRole)
	{
		qDebug("FontListModel::setData() out of Qt::CheckStateRole role");
		return false;
	}

/*	ScFace f = m_fonts[m_fonts.keys().at(index.row())];

	if (index.column() == FontListModel::FontUsable)
		m_fonts[m_fonts.keys().at(index.row())].usable(!f.usable());
	else if (index.column() == FontListModel::FontEmbed)
		m_fonts[m_fonts.keys().at(index.row())].embedPs(!f.embedPs());
	else if (index.column() == FontListModel::FontSubset)
		m_fonts[m_fonts.keys().at(index.row())].subset(!f.subset());
	else
		qDebug("FontListModel::setData() out of defined editable columns"); */

	ScFace f = m_font_values[index.row()];

	if (index.column() == FontListModel::FontUsable)
		f.usable(!f.usable());
	else if (index.column() == FontListModel::FontEmbed)
		f.embedPs(!f.embedPs());
	else if (index.column() == FontListModel::FontSubset)
		f.subset(!f.subset());
	else
		qDebug("FontListModel::setData() out of defined editable columns");

	emit dataChanged(index, index);

	return true;
}

QString FontListModel::nameForIndex(const QModelIndex & index)
{
	return m_fonts.keys().at(index.row());
}