Debian Codegraph @

tests/manual/examples/widgets/painting/fontsampler/mainwindow.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include <QtWidgets>
5 #if defined(QT_PRINTSUPPORT_LIB)
6 #include <QtPrintSupport/qtprintsupportglobal.h>
7 #if QT_CONFIG(printdialog)
8 #include <QPrinter>
9 #include <QPrintDialog>
10 #if QT_CONFIG(printpreviewdialog)
11 #include <QPrintPreviewDialog>
12 #endif
13 #endif
14 #endif
15
16 #include "mainwindow.h"
17
18 MainWindow::MainWindow(QWidget *parent)
19 : QMainWindow(parent)
20 {
21 setupUi(this);
22
23 sampleSizes << 32 << 24 << 16 << 14 << 12 << 8 << 4 << 2 << 1;
24 markedCount = 0;
25 setupFontTree();
26
27 connect(quitAction, &QAction::triggered,
28 qApp, &QApplication::quit);
29 connect(fontTree, &QTreeWidget::currentItemChanged,
30 this, &MainWindow::showFont);
31 connect(fontTree, &QTreeWidget::itemChanged,
32 this, &MainWindow::updateStyles);
33
34 fontTree->topLevelItem(0)->setSelected(true);
35 showFont(fontTree->topLevelItem(0));
36 }
37
38 void MainWindow::setupFontTree()
39 {
40 fontTree->setColumnCount(1);
41 fontTree->setHeaderLabels({ tr("Font") });
42
43 const QStringList fontFamilies = QFontDatabase::families();
44 for (const QString &family : fontFamilies) {
45 const QStringList styles = QFontDatabase::styles(family);
46 if (styles.isEmpty())
47 continue;
48
49 QTreeWidgetItem *familyItem = new QTreeWidgetItem(fontTree);
50 familyItem->setText(0, family);
51 familyItem->setCheckState(0, Qt::Unchecked);
52 familyItem->setFlags(familyItem->flags() | Qt::ItemIsAutoTristate);
53
54 for (const QString &style : styles) {
55 QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem);
56 styleItem->setText(0, style);
57 styleItem->setCheckState(0, Qt::Unchecked);
58 styleItem->setData(0, Qt::UserRole, QVariant(QFontDatabase::weight(family, style)));
59 styleItem->setData(0, Qt::UserRole + 1, QVariant(QFontDatabase::italic(family, style)));
60 }
61 }
62 }
63
64 void MainWindow::on_clearAction_triggered()
65 {
66 const QList<QTreeWidgetItem *> items = fontTree->selectedItems();
67 for (QTreeWidgetItem *item : items)
68 item->setSelected(false);
69 fontTree->currentItem()->setSelected(true);
70 }
71
72 void MainWindow::on_markAction_triggered()
73 {
74 markUnmarkFonts(Qt::Checked);
75 }
76
77 void MainWindow::on_unmarkAction_triggered()
78 {
79 markUnmarkFonts(Qt::Unchecked);
80 }
81
82 void MainWindow::markUnmarkFonts(Qt::CheckState state)
83 {
84 const QList<QTreeWidgetItem *> items = fontTree->selectedItems();
85 for (QTreeWidgetItem *item : items) {
86 if (item->checkState(0) != state)
87 item->setCheckState(0, state);
88 }
89 }
90
91 void MainWindow::showFont(QTreeWidgetItem *item)
92 {
93 if (!item)
94 return;
95
96 QString family;
97 QString style;
98 int weight;
99 bool italic;
100
101 if (item->parent()) {
102 family = item->parent()->text(0);
103 style = item->text(0);
104 weight = item->data(0, Qt::UserRole).toInt();
105 italic = item->data(0, Qt::UserRole + 1).toBool();
106 } else {
107 family = item->text(0);
108 style = item->child(0)->text(0);
109 weight = item->child(0)->data(0, Qt::UserRole).toInt();
110 italic = item->child(0)->data(0, Qt::UserRole + 1).toBool();
111 }
112
113 QString oldText = textEdit->toPlainText().trimmed();
114 bool modified = textEdit->document()->isModified();
115 textEdit->clear();
116 QFont font(family, 32, weight, italic);
117 font.setStyleName(style);
118 textEdit->document()->setDefaultFont(font);
119
120 QTextCursor cursor = textEdit->textCursor();
121 QTextBlockFormat blockFormat;
122 blockFormat.setAlignment(Qt::AlignCenter);
123 cursor.insertBlock(blockFormat);
124
125 if (modified)
126 cursor.insertText(QString(oldText));
127 else
128 cursor.insertText(QString("%1 %2").arg(family).arg(style));
129
130 textEdit->document()->setModified(modified);
131 }
132
133 void MainWindow::updateStyles(QTreeWidgetItem *item, int column)
134 {
135 if (!item || column != 0)
136 return;
137
138 Qt::CheckState state = item->checkState(0);
139 QTreeWidgetItem *parent = item->parent();
140
141 if (parent) {
142 // Only count style items.
143 if (state == Qt::Checked)
144 ++markedCount;
145 else
146 --markedCount;
147 }
148
149 printAction->setEnabled(markedCount > 0);
150 printPreviewAction->setEnabled(markedCount > 0);
151 }
152
153 QMap<QString, StyleItems> MainWindow::currentPageMap()
154 {
155 QMap<QString, StyleItems> pageMap;
156
157 for (int row = 0; row < fontTree->topLevelItemCount(); ++row) {
158 QTreeWidgetItem *familyItem = fontTree->topLevelItem(row);
159 QString family;
160
161 if (familyItem->checkState(0) == Qt::Checked) {
162 family = familyItem->text(0);
163 pageMap[family] = StyleItems();
164 }
165
166 for (int childRow = 0; childRow < familyItem->childCount(); ++childRow) {
167 QTreeWidgetItem *styleItem = familyItem->child(childRow);
168 if (styleItem->checkState(0) == Qt::Checked)
169 pageMap[family].append(styleItem);
170 }
171 }
172
173 return pageMap;
174 }
175
176 void MainWindow::on_printAction_triggered()
177 {
178 #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
179 pageMap = currentPageMap();
180
181 if (pageMap.count() == 0)
182 return;
183
184 QPrinter printer(QPrinter::HighResolution);
185 QPrintDialog dialog(&printer, this);
186 if (dialog.exec() != QDialog::Accepted)
187 return;
188
189 int from = printer.fromPage();
190 int to = printer.toPage();
191 if (from <= 0 && to <= 0)
192 printer.setFromTo(1, pageMap.keys().count());
193
194 printDocument(&printer);
195 #endif
196 }
197
198 void MainWindow::printDocument(QPrinter *printer)
199 {
200 #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
201 printer->setFromTo(1, pageMap.count());
202
203 QProgressDialog progress(tr("Preparing font samples..."), tr("&Cancel"),
204 0, pageMap.count(), this);
205 progress.setWindowModality(Qt::ApplicationModal);
206 progress.setWindowTitle(tr("Font Sampler"));
207 progress.setMinimum(printer->fromPage() - 1);
208 progress.setMaximum(printer->toPage());
209
210 QPainter painter;
211 painter.begin(printer);
212 bool firstPage = true;
213
214 for (int page = printer->fromPage(); page <= printer->toPage(); ++page) {
215
216 if (!firstPage)
217 printer->newPage();
218
219 qApp->processEvents();
220 if (progress.wasCanceled())
221 break;
222
223 printPage(page - 1, &painter, printer);
224 progress.setValue(page);
225 firstPage = false;
226 }
227
228 painter.end();
229 #endif
230 }
231
232 void MainWindow::on_printPreviewAction_triggered()
233 {
234 #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printpreviewdialog)
235 pageMap = currentPageMap();
236
237 if (pageMap.count() == 0)
238 return;
239
240 QPrinter printer(QPrinter::HighResolution);
241 QPrintPreviewDialog preview(&printer, this);
242 connect(&preview, &QPrintPreviewDialog::paintRequested,
243 this, &MainWindow::printDocument);
244 preview.exec();
245 #endif
246 }
247
248 void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer)
249 {
250 #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
251 const QString family = std::next(pageMap.begin(), index).key();
252 const StyleItems items = pageMap.value(family);
253
254 // Find the dimensions of the text on each page.
255 qreal width = 0.0;
256 qreal height = 0.0;
257 for (const QTreeWidgetItem *item : items) {
258 QString style = item->text(0);
259 int weight = item->data(0, Qt::UserRole).toInt();
260 bool italic = item->data(0, Qt::UserRole + 1).toBool();
261
262 // Calculate the maximum width and total height of the text.
263 for (int size : std::as_const(sampleSizes)) {
264 QFont font(family, size, weight, italic);
265 font.setStyleName(style);
266 font = QFont(font, painter->device());
267 QFontMetricsF fontMetrics(font);
268 QRectF rect = fontMetrics.boundingRect(
269 QString("%1 %2").arg(family).arg(style));
270 width = qMax(rect.width(), width);
271 height += rect.height();
272 }
273 }
274
275 qreal xScale = printer->pageRect(QPrinter::DevicePixel).width() / width;
276 qreal yScale = printer->pageRect(QPrinter::DevicePixel).height() / height;
277 qreal scale = qMin(xScale, yScale);
278
279 qreal remainingHeight = printer->pageRect(QPrinter::DevicePixel).height()/scale - height;
280 qreal spaceHeight = (remainingHeight / 4.0) / (items.count() + 1);
281 qreal interLineHeight = (remainingHeight / 4.0) / (sampleSizes.count() * items.count());
282
283 painter->save();
284 painter->translate(printer->pageRect(QPrinter::DevicePixel).width() / 2.0, printer->pageRect(QPrinter::DevicePixel).height() / 2.0);
285 painter->scale(scale, scale);
286 painter->setBrush(QBrush(Qt::black));
287
288 qreal x = -width / 2.0;
289 qreal y = -height / 2.0 - remainingHeight / 4.0 + spaceHeight;
290
291 for (const QTreeWidgetItem *item : items) {
292 QString style = item->text(0);
293 int weight = item->data(0, Qt::UserRole).toInt();
294 bool italic = item->data(0, Qt::UserRole + 1).toBool();
295
296 // Draw each line of text.
297 for (int size : std::as_const(sampleSizes)) {
298 QFont font(family, size, weight, italic);
299 font.setStyleName(style);
300 font = QFont(font, painter->device());
301 QFontMetricsF fontMetrics(font);
302 QRectF rect = fontMetrics.boundingRect(QString("%1 %2").arg(
303 font.family()).arg(style));
304 y += rect.height();
305 painter->setFont(font);
306 painter->drawText(QPointF(x, y), QString("%1 %2").arg(family).arg(style));
307 y += interLineHeight;
308 }
309 y += spaceHeight;
310 }
311
312 painter->restore();
313 #endif
314 }