Rev 14448 | Rev 15098 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
13761 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | |||
14447 | cbradney | 8 | #include "ui/prefs_fonts.h" |
9 | #include "prefsstructs.h" |
||
13761 | cbradney | 10 | |
14447 | cbradney | 11 | |
12 | #include <QListWidget> |
||
13 | #include <QListWidgetItem> |
||
14 | #include <QTreeWidget> |
||
15 | #include <QTreeWidgetItem> |
||
16 | #include <QTableWidget> |
||
17 | #include <QHeaderView> |
||
18 | #include <QLabel> |
||
19 | #include <QFileDialog> |
||
20 | #include <QFileInfo> |
||
21 | #include <QFile> |
||
22 | #include <QSpacerItem> |
||
23 | #include <QPixmap> |
||
24 | |||
25 | #include "prefscontext.h" |
||
26 | #include "prefsfile.h" |
||
27 | #include "sccombobox.h" |
||
28 | #include "scribuscore.h" |
||
29 | #include "scribusdoc.h" |
||
30 | #include "prefsmanager.h" |
||
31 | #include "scconfig.h" |
||
32 | #include "util.h" |
||
33 | #include "util_color.h" |
||
34 | #include "util_icon.h" |
||
35 | #include "scpaths.h" |
||
36 | #include "fontlistmodel.h" |
||
37 | #include "fontlistview.h" |
||
38 | |||
15021 | cbradney | 39 | Prefs_Fonts::Prefs_Fonts(QWidget* parent, ScribusDoc* doc) |
14447 | cbradney | 40 | : Prefs_Pane(parent), |
41 | m_doc(NULL) |
||
13761 | cbradney | 42 | { |
43 | setupUi(this); |
||
44 | |||
14447 | cbradney | 45 | RList = PrefsManager::instance()->appPrefs.fontPrefs.GFontSub; |
46 | UsedFonts.clear(); |
||
47 | CurrentPath = ""; |
||
48 | setMinimumSize(fontMetrics().width( tr( "Available Fonts" )+ tr( "Font Substitutions" )+ tr( "Additional Paths" ))+180, 200); |
||
49 | |||
50 | fontListTableView->setModel(new FontListModel(fontListTableView)); |
||
51 | |||
52 | fontSubstitutionsTableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem( tr("Font Name"))); |
||
53 | fontSubstitutionsTableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem( tr("Replacement"))); |
||
54 | fontSubstitutionsTableWidget->setSortingEnabled(false); |
||
55 | fontSubstitutionsTableWidget->setSelectionBehavior( QAbstractItemView::SelectRows ); |
||
56 | QHeaderView *header = fontSubstitutionsTableWidget->horizontalHeader(); |
||
57 | header->setMovable(false); |
||
58 | header->setClickable(false); |
||
59 | header->setResizeMode(QHeaderView::Stretch); |
||
60 | fontSubstitutionsTableWidget->verticalHeader()->hide(); |
||
61 | fontSubstitutionsTableWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); |
||
62 | |||
63 | |||
64 | // If we're being called for global application preferences, not document |
||
65 | // preferences, we let the user customize font search paths. Because things |
||
66 | // go rather badly if paths are changed/removed while a doc is open, the |
||
67 | // control is also not displayed if there is a document open. |
||
68 | if (m_doc==0 && !ScCore->primaryMainWindow()->HaveDoc) |
||
69 | { |
||
70 | whyBlankLabel->resize(0,0); |
||
71 | whyBlankLabel->hide(); |
||
72 | readPaths(); |
||
73 | changeButton->setEnabled(false); |
||
74 | removeButton->setEnabled(false); |
||
75 | connect(pathListWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(SelectPath(QListWidgetItem*))); |
||
76 | connect(addButton, SIGNAL(clicked()), this, SLOT(AddPath())); |
||
77 | connect(changeButton, SIGNAL(clicked()), this, SLOT(ChangePath())); |
||
78 | connect(removeButton, SIGNAL(clicked()), this, SLOT(DelPath())); |
||
79 | } |
||
80 | else |
||
81 | { |
||
82 | pathListWidget->resize(0,0); |
||
83 | changeButton->resize(0,0); |
||
84 | addButton->resize(0,0); |
||
85 | removeButton->resize(0,0); |
||
86 | pathListWidget->hide(); |
||
87 | changeButton->hide(); |
||
88 | addButton->hide(); |
||
89 | removeButton->hide(); |
||
90 | |||
91 | // Rather than just making the tab vanish when editing doc-specific settings |
||
92 | // (we don't support per-doc font paths), show a useful explanation. |
||
93 | whyBlankLabel->setText("<qt>" + |
||
94 | tr("Font search paths can only be set in File > Preferences, and only when " |
||
95 | "there is no document currently open. Close any open documents, then " |
||
96 | "use File > Preferences > Fonts to change the font search path.") + "</qt>"); |
||
97 | additionalPathsButtonsVerticalSpacer->changeSize(0,0); |
||
98 | //additionalPathsButtonsVerticalSpacer->invalidate(); |
||
99 | additionalPathsLabelVerticalSpacer->changeSize(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding); |
||
100 | additionalPathsLabelVerticalSpacer->invalidate(); |
||
101 | } |
||
102 | |||
103 | // signals and slots connections |
||
104 | connect(fontSubstitutionsTableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(ReplaceSel(int, int))); |
||
105 | connect(deleteSubstitutionButton, SIGNAL(clicked()), this, SLOT(DelEntry())); |
||
106 | |||
107 | |||
108 | |||
109 | //fontListTableView |
||
110 | //fontSubstitutionsTableWidget |
||
13761 | cbradney | 111 | } |
112 | |||
113 | Prefs_Fonts::~Prefs_Fonts() |
||
114 | { |
||
115 | } |
||
116 | |||
14447 | cbradney | 117 | void Prefs_Fonts::languageChange() |
118 | { |
||
119 | } |
||
120 | |||
121 | void Prefs_Fonts::restoreDefaults(struct ApplicationPrefs *prefsData) |
||
122 | { |
||
123 | // SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.AvailFonts); |
||
14448 | cbradney | 124 | fontListTableView->setFonts(prefsData->fontPrefs.AvailFonts); |
14447 | cbradney | 125 | /* |
126 | DON'T REMOVE THIS COMMENTS, PLEASE! (Petr) |
||
127 | It's just a performance vs. functionality test. |
||
128 | availFonts->clear(); |
||
129 | // FIXME: This is main preformance issue. It's about 90% of all preference reads! - PV |
||
130 | availFonts->GetFonts(HomeP); */ |
||
131 | /* Are you wondering why this condition? See the comment at |
||
132 | line #102 (or somewhere near) as reference. Hint: PathList |
||
133 | is not initialized for example... - PV */ |
||
134 | /* if (!DocAvail && !ScCore->primaryMainWindow()->HaveDoc) |
||
135 | { |
||
136 | for (uint a = 0; a < PathList->count(); ++a) |
||
137 | { |
||
138 | QString dir = ScPaths::separatorsToSlashes(PathList->text(a)); |
||
139 | availFonts->AddScalableFonts(dir +"/"); //, docc->DocName); |
||
140 | availFonts->updateFontMap(); |
||
141 | } |
||
142 | } */ |
||
143 | // UsedFonts.clear(); |
||
144 | // fontFlags.clear(); |
||
145 | // fontList->clear(); |
||
146 | // SCFontsIterator it(*availFonts); |
||
147 | // for ( ; it.hasNext(); it.next()) |
||
148 | // { |
||
149 | // if (it.current().isNone()) |
||
150 | // continue; |
||
151 | // fontSet foS; |
||
152 | // QTreeWidgetItem *row = new QTreeWidgetItem(fontList); |
||
153 | // row->setText(0, it.currentKey()); |
||
154 | |||
155 | // foS.FlagUse = it.current().usable(); |
||
156 | // row->setIcon(1, foS.FlagUse ? checkOn : checkOff); |
||
157 | // if (foS.FlagUse) |
||
158 | // UsedFonts.append(it.currentKey()); |
||
159 | |||
160 | // foS.FlagPS = it.current().embedPs(); |
||
161 | // row->setIcon(2, foS.FlagPS ? checkOn : checkOff); |
||
162 | |||
163 | // foS.FlagSub = it.current().subset(); |
||
164 | // row->setIcon(3, foS.FlagSub ? checkOn : checkOff); |
||
165 | |||
166 | // ScFace::FontType type = it.current().type(); |
||
167 | // foS.FlagOTF = (type == ScFace::OTF) ? true : false; |
||
168 | // if (it.current().isReplacement()) |
||
169 | // row->setIcon(0, substFont); |
||
170 | // else if (type == ScFace::TYPE1) |
||
171 | // row->setIcon(0, psFont); |
||
172 | // else if (type == ScFace::TTF) |
||
173 | // row->setIcon(0, ttfFont); |
||
174 | // else if (type == ScFace::OTF) |
||
175 | // row->setIcon(0, otfFont); |
||
176 | |||
177 | // foS.FlagNames = it.current().hasNames(); |
||
178 | // row->setText(4, it.current().fontPath()); |
||
179 | // fontFlags.insert(it.currentKey(), foS); |
||
180 | // } |
||
181 | // fontList->sortByColumn(0, Qt::AscendingOrder); |
||
182 | // fontList->resizeColumnToContents(0); |
||
183 | // fontList->resizeColumnToContents(4); |
||
184 | // UsedFonts.sort(); |
||
185 | FlagsRepl.clear(); |
||
186 | fontSubstitutionsTableWidget->clearContents(); |
||
14448 | cbradney | 187 | fontSubstitutionsTableWidget->setRowCount(prefsData->fontPrefs.GFontSub.count()); |
14447 | cbradney | 188 | int a = 0; |
189 | QMap<QString,QString>::Iterator itfsu; |
||
190 | for (itfsu = RList.begin(); itfsu != RList.end(); ++itfsu) |
||
191 | { |
||
192 | QTableWidgetItem* tWidgetItem = new QTableWidgetItem(itfsu.key()); |
||
193 | tWidgetItem->setFlags(tWidgetItem->flags() & ~Qt::ItemIsEditable); |
||
194 | fontSubstitutionsTableWidget->setItem(a, 0, tWidgetItem); |
||
195 | ScComboBox *item = new ScComboBox(fontSubstitutionsTableWidget); |
||
196 | fontSubstitutionsTableWidget->setCellWidget(a, 1, item); |
||
197 | item->setEditable(false); |
||
198 | item->addItem(itfsu.value()); |
||
199 | setCurrentComboItem(item, itfsu.value()); |
||
200 | FlagsRepl.append(item); |
||
201 | a++; |
||
202 | } |
||
14448 | cbradney | 203 | updateFontList(prefsData); |
14447 | cbradney | 204 | } |
205 | |||
206 | void Prefs_Fonts::saveGuiToPrefs(struct ApplicationPrefs *prefsData) const |
||
207 | { |
||
14448 | cbradney | 208 | prefsData->fontPrefs.GFontSub.clear(); |
209 | uint a = 0; |
||
210 | for (QMap<QString,QString>::ConstIterator itfsu = RList.begin(); itfsu != RList.end(); ++itfsu) |
||
211 | prefsData->fontPrefs.GFontSub[itfsu.key()] = FlagsRepl.at(a++)->currentText(); |
||
14447 | cbradney | 212 | |
213 | } |
||
214 | |||
215 | void Prefs_Fonts::ReplaceSel(int, int) |
||
216 | { |
||
217 | deleteSubstitutionButton->setEnabled(true); |
||
218 | } |
||
219 | |||
14448 | cbradney | 220 | void Prefs_Fonts::updateFontList(struct ApplicationPrefs *prefsData) |
14447 | cbradney | 221 | { |
222 | QString tmp; |
||
223 | UsedFonts.clear(); |
||
14448 | cbradney | 224 | SCFonts fonts = prefsData->fontPrefs.AvailFonts; |
14447 | cbradney | 225 | SCFontsIterator it(fonts); |
226 | for ( ; it.hasNext() ; it.next()) |
||
227 | { |
||
228 | if (fonts[it.currentKey()].usable()) |
||
229 | UsedFonts.append(it.currentKey()); |
||
230 | } |
||
231 | UsedFonts.sort(); |
||
232 | |||
233 | for (int b = 0; b < FlagsRepl.count(); ++b) |
||
234 | { |
||
235 | tmp = FlagsRepl.at(b)->currentText(); |
||
236 | FlagsRepl.at(b)->clear(); |
||
237 | FlagsRepl.at(b)->addItems(UsedFonts); |
||
238 | if (UsedFonts.contains(tmp) != 0) |
||
239 | setCurrentComboItem(FlagsRepl.at(b), tmp); |
||
240 | else |
||
241 | FlagsRepl.at(b)->setCurrentIndex(0); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | void Prefs_Fonts::DelEntry() |
||
246 | { |
||
247 | int r = fontSubstitutionsTableWidget->currentRow(); |
||
248 | QString tmp = fontSubstitutionsTableWidget->item(r, 0)->text(); |
||
249 | fontSubstitutionsTableWidget->removeRow(r); |
||
250 | delete FlagsRepl.takeAt(r); |
||
251 | RList.remove(tmp); |
||
252 | deleteSubstitutionButton->setEnabled(false); |
||
253 | } |
||
254 | |||
255 | void Prefs_Fonts::readPaths() |
||
256 | { |
||
257 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
258 | PrefsContext *fontPrefsContext = PrefsManager::instance()->prefsFile->getContext("Fonts"); |
||
259 | PrefsTable *fontPathTable = fontPrefsContext->getTable("ExtraFontDirs"); |
||
260 | pathListWidget->clear(); |
||
261 | for (int i = 0; i < fontPathTable->getRowCount(); ++i) |
||
262 | pathListWidget->addItem( QDir::convertSeparators(fontPathTable->get(i,0)) ); |
||
263 | } |
||
264 | |||
265 | void Prefs_Fonts::writePaths() |
||
266 | { |
||
267 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
268 | PrefsContext *fontPrefsContext = PrefsManager::instance()->prefsFile->getContext("Fonts"); |
||
269 | PrefsTable *fontPathTable = fontPrefsContext->getTable("ExtraFontDirs"); |
||
270 | fontPathTable->clear(); |
||
271 | for (int i = 0; i < pathListWidget->count(); ++i) |
||
272 | fontPathTable->set(i, 0, QDir::fromNativeSeparators(pathListWidget->item(i)->text())); |
||
273 | } |
||
274 | |||
275 | void Prefs_Fonts::SelectPath(QListWidgetItem *c) |
||
276 | { |
||
277 | if (m_doc==0) |
||
278 | { |
||
279 | changeButton->setEnabled(true); |
||
280 | removeButton->setEnabled(true); |
||
281 | } |
||
282 | CurrentPath = c->text(); |
||
283 | } |
||
284 | |||
285 | void Prefs_Fonts::AddPath() |
||
286 | { |
||
287 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
288 | PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs"); |
||
289 | CurrentPath = dirs->get("fontprefs", "."); |
||
290 | QString s = QFileDialog::getExistingDirectory(this, tr("Choose a Directory"), CurrentPath); |
||
291 | if (!s.isEmpty()) |
||
292 | { |
||
293 | dirs->set("fontprefs", s.left(s.lastIndexOf("/", -2))); |
||
294 | if( s.endsWith("/") ) |
||
295 | s = s.left(s.length()-1); |
||
296 | QString s2 = QDir::convertSeparators(s); |
||
297 | if (pathListWidget->findItems(s2, Qt::MatchExactly).count() != 0) |
||
298 | return; |
||
299 | pathListWidget->addItem(s2); |
||
300 | writePaths(); |
||
301 | changeButton->setEnabled(false); |
||
302 | removeButton->setEnabled(false); |
||
303 | CurrentPath = s; |
||
304 | SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
305 | QString dir = QDir::fromNativeSeparators(s2); |
||
306 | availFonts->AddScalableFonts(dir +"/"); |
||
307 | availFonts->updateFontMap(); |
||
308 | restoreDefaults(NULL); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | void Prefs_Fonts::ChangePath() |
||
313 | { |
||
314 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
315 | QString s = QFileDialog::getExistingDirectory(this, tr("Choose a Directory"), CurrentPath); |
||
316 | if (!s.isEmpty()) |
||
317 | { |
||
318 | if( s.endsWith("/") ) |
||
319 | s = s.left(s.length()-1); |
||
320 | QString s2 = QDir::convertSeparators(s2); |
||
321 | if (pathListWidget->findItems(s2, Qt::MatchExactly).count() != 0) |
||
322 | return; |
||
323 | QString path = pathListWidget->currentItem()->text(); |
||
324 | SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
325 | SCFontsIterator it(*availFonts); |
||
326 | for ( ; it.hasNext(); it.next()) |
||
327 | { |
||
328 | if (it.current().isNone()) |
||
329 | continue; |
||
330 | QFileInfo fi(it.current().fontFilePath()); |
||
331 | if (fi.absolutePath() == path) |
||
332 | availFonts->remove(it.currentKey()); |
||
333 | } |
||
334 | pathListWidget->currentItem()->setText(s2); |
||
335 | writePaths(); |
||
336 | CurrentPath = s; |
||
337 | QString dir = QDir::fromNativeSeparators(s2); |
||
338 | availFonts->AddScalableFonts(dir +"/"); |
||
339 | availFonts->updateFontMap(); |
||
340 | restoreDefaults(NULL); |
||
341 | changeButton->setEnabled(false); |
||
342 | removeButton->setEnabled(false); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | void Prefs_Fonts::DelPath() |
||
347 | { |
||
348 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
349 | QFile fx(PrefsManager::instance()->preferencesLocation()+"/scribusfont13.rc"); |
||
350 | QString path = pathListWidget->currentItem()->text(); |
||
351 | if (fx.open(QIODevice::WriteOnly)) |
||
352 | { |
||
353 | if (pathListWidget->count() == 1) |
||
354 | pathListWidget->clear(); |
||
355 | else |
||
356 | delete pathListWidget->takeItem(pathListWidget->currentRow()); |
||
357 | writePaths(); |
||
358 | } |
||
359 | else |
||
360 | return; |
||
361 | SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
362 | SCFontsIterator it(*availFonts); |
||
363 | for ( ; it.hasNext(); it.next()) |
||
364 | { |
||
365 | if (it.current().isNone()) |
||
366 | continue; |
||
367 | QFileInfo fi(it.current().fontFilePath()); |
||
368 | if (fi.absolutePath() == path) |
||
369 | availFonts->remove(it.currentKey()); |
||
370 | } |
||
371 | availFonts->updateFontMap(); |
||
372 | CurrentPath = ""; |
||
373 | restoreDefaults(NULL); |
||
374 | changeButton->setEnabled(false); |
||
375 | removeButton->setEnabled(false); |
||
376 | } |
||
377 |