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
8 #include <QSortFilterProxyModel>
9 #include <QHeaderView>
10
11 #include "fontpreview.h"
12 #include "prefsfile.h"
13 #include "prefsmanager.h"
14 #include "scribusdoc.h"
15 #include "selection.h"
16 #include "sampleitem.h"
17 #include "fontlistmodel.h"
18 #include "iconmanager.h"
19
20
FontPreview(const QString & fontName,QWidget * parent,ScribusDoc * doc)21 FontPreview::FontPreview(const QString& fontName, QWidget* parent, ScribusDoc* doc)
22 : QDialog(parent, Qt::WindowFlags())
23 {
24 setupUi(this);
25 setModal(true);
26 setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
27 m_Doc = doc;
28
29 sampleItem = new SampleItem();
30 sampleItem->setDevicePixelRatio(devicePixelRatioF());
31
32 languageChange();
33
34 fontModel = new FontListModel(this, m_Doc, false);
35
36 proxyModel = new QSortFilterProxyModel();
37 proxyModel->setDynamicSortFilter(true);
38 proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
39 proxyModel->setSourceModel(fontModel);
40 proxyModel->setFilterKeyColumn(1);
41 proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
42 fontList->setModel(proxyModel);
43
44 // scribus config
45 defaultStr = tr("Woven silk pyjamas exchanged for blue quartz", "font preview");
46 prefs = PrefsManager::instance().prefsFile->getPluginContext("fontpreview");
47 uint srt = prefs->getUInt("sortColumn", 0);
48 bool extend = prefs->getBool("extendedView", false);
49 extendedCheckBox->setChecked(extend);
50 Qt::SortOrder srtOrder = (Qt::SortOrder) prefs->getUInt("sortColumnOrder", 0);
51
52 proxyModel->sort(srt, srtOrder);
53 fontList->horizontalHeader()->setSortIndicatorShown(true);
54 fontList->horizontalHeader()->setSortIndicator(srt, srtOrder);
55 xsize = prefs->getUInt("xsize", 640);
56 ysize = prefs->getUInt("ysize", 480);
57 sizeSpin->setValue(prefs->getUInt("fontSize", 18));
58 QString ph = prefs->get("phrase", defaultStr);
59 displayEdit->setText(ph);
60 displayButton_clicked();
61 resize(QSize(xsize, ysize).expandedTo(minimumSizeHint()));
62
63 setExtendedView(extend);
64
65 QString searchName;
66 if (!fontName.isEmpty())
67 searchName = fontName;
68 else
69 {
70 Q_ASSERT(m_Doc!=nullptr);
71 if (m_Doc->m_Selection->count() != 0)
72 searchName = m_Doc->currentStyle.charStyle().font().scName();
73 else
74 searchName = PrefsManager::instance().appPrefs.itemToolPrefs.textFont;
75 }
76 QModelIndexList found = fontModel->match(fontModel->index(0, 0),
77 Qt::DisplayRole, searchName,
78 1,
79 Qt::MatchContains | Qt::MatchWrap);
80 if (!found.empty())
81 {
82 fontList->scrollTo(found.at(0), QAbstractItemView::PositionAtCenter);
83 fontList->selectRow(found.at(0).row());
84 }
85
86 fontList->resizeColumnsToContents();
87
88 connect(displayButton, SIGNAL(clicked()), this, SLOT(displayButton_clicked()));
89 connect(searchEdit, SIGNAL(textChanged(QString)), this, SLOT(searchEdit_textChanged(QString)));
90 connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelButton_clicked()));
91 connect(resetDisplayButton, SIGNAL(clicked()), this, SLOT(resetDisplayButton_clicked()));
92 connect(sizeSpin, SIGNAL(valueChanged(int)), this, SLOT(sizeSpin_valueChanged(int)));
93 connect(fontList->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(fontList_currentChanged(const QModelIndex&, const QModelIndex&)));
94 connect(extendedCheckBox, SIGNAL(clicked(bool)), this, SLOT(setExtendedView(bool)));
95 }
96
~FontPreview()97 FontPreview::~FontPreview()
98 {
99 prefs->set("sortColumn", fontList->horizontalHeader()->sortIndicatorSection());
100 prefs->set("sortColumnOrder", fontList->horizontalHeader()->sortIndicatorOrder());
101 prefs->set("xsize", width());
102 prefs->set("ysize", height());
103 prefs->set("fontSize", sizeSpin->value());
104 prefs->set("phrase", displayEdit->text());
105 prefs->set("extendedView", extendedCheckBox->isChecked());
106 }
107
languageChange()108 void FontPreview::languageChange()
109 {
110 cancelButton->setToolTip(tr("Leave preview", "font preview"));
111 searchEdit->setToolTip("<qt>" + tr("Typing the text here provides quick searching in the font names. Searching is case insensitive. The given text is taken as substring.") + "</qt>");
112 sizeSpin->setToolTip(tr("Size of the selected font"));
113 }
114
showEvent(QShowEvent * event)115 void FontPreview::showEvent(QShowEvent * event)
116 {
117 paintSample();
118 QDialog::showEvent(event);
119 }
120
resizeEvent(QResizeEvent * event)121 void FontPreview::resizeEvent(QResizeEvent * event)
122 {
123 paintSample();
124 QDialog::resizeEvent(event);
125 }
126
allowSample()127 bool FontPreview::allowSample()
128 {
129 if (fontModel->rowCount() != 0)
130 return true;
131 fontPreview->setText("No font selected");
132 return false;
133 }
134
paintSample()135 void FontPreview::paintSample()
136 {
137 if (!allowSample())
138 return;
139
140 QString fontName(getCurrentFont());
141 if (fontName.isNull())
142 return;
143
144 sampleItem->setFontSize(sizeSpin->value() * 10, true);
145 sampleItem->setFont(fontName);
146 QPixmap pixmap = sampleItem->getSample(fontPreview->width(),
147 fontPreview->height());
148 fontPreview->clear();
149 if (!pixmap.isNull())
150 fontPreview->setPixmap(pixmap);
151 }
152
searchEdit_textChanged(const QString &)153 void FontPreview::searchEdit_textChanged(const QString &/*s*/)
154 {
155 fontList->blockSignals(true);
156 QString s(searchEdit->text());
157 if (s.isEmpty())
158 proxyModel->setFilterRegExp(QRegExp("*",
159 Qt::CaseInsensitive,
160 QRegExp::Wildcard));
161 else
162 {
163 QRegExp regExp(QString("*%1*").arg(s),
164 Qt::CaseInsensitive,
165 QRegExp::Wildcard);
166 proxyModel->setFilterRegExp(regExp);
167 }
168 fontList->resizeColumnsToContents();
169 fontList->blockSignals(false);
170 }
171
getCurrentFont()172 QString FontPreview::getCurrentFont()
173 {
174 QModelIndex ix(fontList->currentIndex());
175 if (!ix.isValid())
176 return QString();
177 return fontModel->nameForIndex(proxyModel->mapToSource(fontList->currentIndex()));
178 }
179
displayButton_clicked()180 void FontPreview::displayButton_clicked()
181 {
182 sampleItem->setText(displayEdit->text());
183 paintSample();
184 }
185
cancelButton_clicked()186 void FontPreview::cancelButton_clicked()
187 {
188 reject();
189 }
190
resetDisplayButton_clicked()191 void FontPreview::resetDisplayButton_clicked()
192 {
193 displayEdit->setText(defaultStr);
194 displayButton_clicked();
195 }
196
sizeSpin_valueChanged(int)197 void FontPreview::sizeSpin_valueChanged( int )
198 {
199 paintSample();
200 }
201
fontList_currentChanged(const QModelIndex &,const QModelIndex &)202 void FontPreview::fontList_currentChanged(const QModelIndex &, const QModelIndex &)
203 {
204 paintSample();
205 }
206
setExtendedView(bool state)207 void FontPreview::setExtendedView(bool state)
208 {
209 for (int i=0; i < fontList->model()->columnCount(); ++i)
210 {
211 if (i == 1)
212 continue;
213 fontList->setColumnHidden(i, !state);
214 }
215 }
216