Rev 13761 | Rev 14448 | 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 | |||
13761 | cbradney | 39 | Prefs_Fonts::Prefs_Fonts(QWidget* parent) |
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); |
||
124 | fontListTableView->setFonts(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
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(); |
||
187 | fontSubstitutionsTableWidget->setRowCount(PrefsManager::instance()->appPrefs.fontPrefs.GFontSub.count()); |
||
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 | } |
||
203 | UpdateFliste(); |
||
204 | } |
||
205 | |||
206 | void Prefs_Fonts::saveGuiToPrefs(struct ApplicationPrefs *prefsData) const |
||
207 | { |
||
208 | |||
209 | } |
||
210 | |||
211 | void Prefs_Fonts::ReplaceSel(int, int) |
||
212 | { |
||
213 | deleteSubstitutionButton->setEnabled(true); |
||
214 | } |
||
215 | |||
216 | void Prefs_Fonts::UpdateFliste() |
||
217 | { |
||
218 | QString tmp; |
||
219 | UsedFonts.clear(); |
||
220 | SCFonts fonts = PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts; |
||
221 | SCFontsIterator it(fonts); |
||
222 | for ( ; it.hasNext() ; it.next()) |
||
223 | { |
||
224 | if (fonts[it.currentKey()].usable()) |
||
225 | UsedFonts.append(it.currentKey()); |
||
226 | } |
||
227 | UsedFonts.sort(); |
||
228 | |||
229 | for (int b = 0; b < FlagsRepl.count(); ++b) |
||
230 | { |
||
231 | tmp = FlagsRepl.at(b)->currentText(); |
||
232 | FlagsRepl.at(b)->clear(); |
||
233 | FlagsRepl.at(b)->addItems(UsedFonts); |
||
234 | if (UsedFonts.contains(tmp) != 0) |
||
235 | setCurrentComboItem(FlagsRepl.at(b), tmp); |
||
236 | else |
||
237 | FlagsRepl.at(b)->setCurrentIndex(0); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | void Prefs_Fonts::DelEntry() |
||
242 | { |
||
243 | int r = fontSubstitutionsTableWidget->currentRow(); |
||
244 | QString tmp = fontSubstitutionsTableWidget->item(r, 0)->text(); |
||
245 | fontSubstitutionsTableWidget->removeRow(r); |
||
246 | delete FlagsRepl.takeAt(r); |
||
247 | RList.remove(tmp); |
||
248 | deleteSubstitutionButton->setEnabled(false); |
||
249 | } |
||
250 | |||
251 | void Prefs_Fonts::readPaths() |
||
252 | { |
||
253 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
254 | PrefsContext *fontPrefsContext = PrefsManager::instance()->prefsFile->getContext("Fonts"); |
||
255 | PrefsTable *fontPathTable = fontPrefsContext->getTable("ExtraFontDirs"); |
||
256 | pathListWidget->clear(); |
||
257 | for (int i = 0; i < fontPathTable->getRowCount(); ++i) |
||
258 | pathListWidget->addItem( QDir::convertSeparators(fontPathTable->get(i,0)) ); |
||
259 | } |
||
260 | |||
261 | void Prefs_Fonts::writePaths() |
||
262 | { |
||
263 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
264 | PrefsContext *fontPrefsContext = PrefsManager::instance()->prefsFile->getContext("Fonts"); |
||
265 | PrefsTable *fontPathTable = fontPrefsContext->getTable("ExtraFontDirs"); |
||
266 | fontPathTable->clear(); |
||
267 | for (int i = 0; i < pathListWidget->count(); ++i) |
||
268 | fontPathTable->set(i, 0, QDir::fromNativeSeparators(pathListWidget->item(i)->text())); |
||
269 | } |
||
270 | |||
271 | void Prefs_Fonts::SelectPath(QListWidgetItem *c) |
||
272 | { |
||
273 | if (m_doc==0) |
||
274 | { |
||
275 | changeButton->setEnabled(true); |
||
276 | removeButton->setEnabled(true); |
||
277 | } |
||
278 | CurrentPath = c->text(); |
||
279 | } |
||
280 | |||
281 | void Prefs_Fonts::AddPath() |
||
282 | { |
||
283 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
284 | PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs"); |
||
285 | CurrentPath = dirs->get("fontprefs", "."); |
||
286 | QString s = QFileDialog::getExistingDirectory(this, tr("Choose a Directory"), CurrentPath); |
||
287 | if (!s.isEmpty()) |
||
288 | { |
||
289 | dirs->set("fontprefs", s.left(s.lastIndexOf("/", -2))); |
||
290 | if( s.endsWith("/") ) |
||
291 | s = s.left(s.length()-1); |
||
292 | QString s2 = QDir::convertSeparators(s); |
||
293 | if (pathListWidget->findItems(s2, Qt::MatchExactly).count() != 0) |
||
294 | return; |
||
295 | pathListWidget->addItem(s2); |
||
296 | writePaths(); |
||
297 | changeButton->setEnabled(false); |
||
298 | removeButton->setEnabled(false); |
||
299 | CurrentPath = s; |
||
300 | SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
301 | QString dir = QDir::fromNativeSeparators(s2); |
||
302 | availFonts->AddScalableFonts(dir +"/"); |
||
303 | availFonts->updateFontMap(); |
||
304 | restoreDefaults(NULL); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | void Prefs_Fonts::ChangePath() |
||
309 | { |
||
310 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
311 | QString s = QFileDialog::getExistingDirectory(this, tr("Choose a Directory"), CurrentPath); |
||
312 | if (!s.isEmpty()) |
||
313 | { |
||
314 | if( s.endsWith("/") ) |
||
315 | s = s.left(s.length()-1); |
||
316 | QString s2 = QDir::convertSeparators(s2); |
||
317 | if (pathListWidget->findItems(s2, Qt::MatchExactly).count() != 0) |
||
318 | return; |
||
319 | QString path = pathListWidget->currentItem()->text(); |
||
320 | SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
321 | SCFontsIterator it(*availFonts); |
||
322 | for ( ; it.hasNext(); it.next()) |
||
323 | { |
||
324 | if (it.current().isNone()) |
||
325 | continue; |
||
326 | QFileInfo fi(it.current().fontFilePath()); |
||
327 | if (fi.absolutePath() == path) |
||
328 | availFonts->remove(it.currentKey()); |
||
329 | } |
||
330 | pathListWidget->currentItem()->setText(s2); |
||
331 | writePaths(); |
||
332 | CurrentPath = s; |
||
333 | QString dir = QDir::fromNativeSeparators(s2); |
||
334 | availFonts->AddScalableFonts(dir +"/"); |
||
335 | availFonts->updateFontMap(); |
||
336 | restoreDefaults(NULL); |
||
337 | changeButton->setEnabled(false); |
||
338 | removeButton->setEnabled(false); |
||
339 | } |
||
340 | } |
||
341 | |||
342 | void Prefs_Fonts::DelPath() |
||
343 | { |
||
344 | Q_ASSERT(m_doc==0); // should never be called in doc-specific prefs |
||
345 | QFile fx(PrefsManager::instance()->preferencesLocation()+"/scribusfont13.rc"); |
||
346 | QString path = pathListWidget->currentItem()->text(); |
||
347 | if (fx.open(QIODevice::WriteOnly)) |
||
348 | { |
||
349 | if (pathListWidget->count() == 1) |
||
350 | pathListWidget->clear(); |
||
351 | else |
||
352 | delete pathListWidget->takeItem(pathListWidget->currentRow()); |
||
353 | writePaths(); |
||
354 | } |
||
355 | else |
||
356 | return; |
||
357 | SCFonts* availFonts=&(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts); |
||
358 | SCFontsIterator it(*availFonts); |
||
359 | for ( ; it.hasNext(); it.next()) |
||
360 | { |
||
361 | if (it.current().isNone()) |
||
362 | continue; |
||
363 | QFileInfo fi(it.current().fontFilePath()); |
||
364 | if (fi.absolutePath() == path) |
||
365 | availFonts->remove(it.currentKey()); |
||
366 | } |
||
367 | availFonts->updateFontMap(); |
||
368 | CurrentPath = ""; |
||
369 | restoreDefaults(NULL); |
||
370 | changeButton->setEnabled(false); |
||
371 | removeButton->setEnabled(false); |
||
372 | } |
||
373 |