author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "mainwindow.h" |
|
44 |
||
45 |
MainWindow::MainWindow(QWidget *parent) |
|
46 |
: QMainWindow(parent) |
|
47 |
{ |
|
48 |
setupUi(this); |
|
49 |
||
50 |
sampleSizes << 32 << 24 << 16 << 14 << 12 << 8 << 4 << 2 << 1; |
|
51 |
markedCount = 0; |
|
52 |
setupFontTree(); |
|
53 |
||
54 |
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
connect(fontTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
this, SLOT(showFont(QTreeWidgetItem*))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
connect(fontTree, SIGNAL(itemChanged(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
this, SLOT(updateStyles(QTreeWidgetItem*,int))); |
0 | 59 |
|
60 |
fontTree->setItemSelected(fontTree->topLevelItem(0), true); |
|
61 |
showFont(fontTree->topLevelItem(0)); |
|
62 |
} |
|
63 |
||
64 |
void MainWindow::setupFontTree() |
|
65 |
{ |
|
66 |
QFontDatabase database; |
|
67 |
fontTree->setColumnCount(1); |
|
68 |
fontTree->setHeaderLabels(QStringList() << tr("Font")); |
|
69 |
||
70 |
foreach (QString family, database.families()) { |
|
71 |
const QStringList styles = database.styles(family); |
|
72 |
if (styles.isEmpty()) |
|
73 |
continue; |
|
74 |
||
75 |
QTreeWidgetItem *familyItem = new QTreeWidgetItem(fontTree); |
|
76 |
familyItem->setText(0, family); |
|
77 |
familyItem->setCheckState(0, Qt::Unchecked); |
|
78 |
||
79 |
foreach (QString style, styles) { |
|
80 |
QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem); |
|
81 |
styleItem->setText(0, style); |
|
82 |
styleItem->setCheckState(0, Qt::Unchecked); |
|
83 |
styleItem->setData(0, Qt::UserRole, |
|
84 |
QVariant(database.weight(family, style))); |
|
85 |
styleItem->setData(0, Qt::UserRole + 1, |
|
86 |
QVariant(database.italic(family, style))); |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
void MainWindow::on_clearAction_triggered() |
|
92 |
{ |
|
93 |
QTreeWidgetItem *currentItem = fontTree->currentItem(); |
|
94 |
foreach (QTreeWidgetItem *item, fontTree->selectedItems()) |
|
95 |
fontTree->setItemSelected(item, false); |
|
96 |
fontTree->setItemSelected(currentItem, true); |
|
97 |
} |
|
98 |
||
99 |
void MainWindow::on_markAction_triggered() |
|
100 |
{ |
|
101 |
markUnmarkFonts(Qt::Checked); |
|
102 |
} |
|
103 |
||
104 |
void MainWindow::on_unmarkAction_triggered() |
|
105 |
{ |
|
106 |
markUnmarkFonts(Qt::Unchecked); |
|
107 |
} |
|
108 |
||
109 |
void MainWindow::markUnmarkFonts(Qt::CheckState state) |
|
110 |
{ |
|
111 |
QList<QTreeWidgetItem *> items = fontTree->selectedItems(); |
|
112 |
foreach (QTreeWidgetItem *item, items) { |
|
113 |
if (item->checkState(0) != state) |
|
114 |
item->setCheckState(0, state); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
void MainWindow::showFont(QTreeWidgetItem *item) |
|
119 |
{ |
|
120 |
if (!item) |
|
121 |
return; |
|
122 |
||
123 |
QString family; |
|
124 |
QString style; |
|
125 |
int weight; |
|
126 |
bool italic; |
|
127 |
||
128 |
if (item->parent()) { |
|
129 |
family = item->parent()->text(0); |
|
130 |
style = item->text(0); |
|
131 |
weight = item->data(0, Qt::UserRole).toInt(); |
|
132 |
italic = item->data(0, Qt::UserRole + 1).toBool(); |
|
133 |
} else { |
|
134 |
family = item->text(0); |
|
135 |
style = item->child(0)->text(0); |
|
136 |
weight = item->child(0)->data(0, Qt::UserRole).toInt(); |
|
137 |
italic = item->child(0)->data(0, Qt::UserRole + 1).toBool(); |
|
138 |
} |
|
139 |
||
140 |
QString oldText = textEdit->toPlainText().trimmed(); |
|
141 |
bool modified = textEdit->document()->isModified(); |
|
142 |
textEdit->clear(); |
|
143 |
textEdit->document()->setDefaultFont(QFont(family, 32, weight, italic)); |
|
144 |
||
145 |
QTextCursor cursor = textEdit->textCursor(); |
|
146 |
QTextBlockFormat blockFormat; |
|
147 |
blockFormat.setAlignment(Qt::AlignCenter); |
|
148 |
cursor.insertBlock(blockFormat); |
|
149 |
||
150 |
if (modified) |
|
151 |
cursor.insertText(QString(oldText)); |
|
152 |
else |
|
153 |
cursor.insertText(QString("%1 %2").arg(family).arg(style)); |
|
154 |
||
155 |
textEdit->document()->setModified(modified); |
|
156 |
} |
|
157 |
||
158 |
void MainWindow::updateStyles(QTreeWidgetItem *item, int column) |
|
159 |
{ |
|
160 |
if (!item || column != 0) |
|
161 |
return; |
|
162 |
||
163 |
Qt::CheckState state = item->checkState(0); |
|
164 |
QTreeWidgetItem *parent = item->parent(); |
|
165 |
||
166 |
if (parent) { |
|
167 |
||
168 |
// Only count style items. |
|
169 |
if (state == Qt::Checked) |
|
170 |
++markedCount; |
|
171 |
else |
|
172 |
--markedCount; |
|
173 |
||
174 |
if (state == Qt::Checked && |
|
175 |
parent->checkState(0) == Qt::Unchecked) { |
|
176 |
// Mark parent items when child items are checked. |
|
177 |
parent->setCheckState(0, Qt::Checked); |
|
178 |
||
179 |
} else if (state == Qt::Unchecked && |
|
180 |
parent->checkState(0) == Qt::Checked) { |
|
181 |
||
182 |
bool marked = false; |
|
183 |
for (int row = 0; row < parent->childCount(); ++row) { |
|
184 |
if (parent->child(row)->checkState(0) == Qt::Checked) { |
|
185 |
marked = true; |
|
186 |
break; |
|
187 |
} |
|
188 |
} |
|
189 |
// Unmark parent items when all child items are unchecked. |
|
190 |
if (!marked) |
|
191 |
parent->setCheckState(0, Qt::Unchecked); |
|
192 |
} |
|
193 |
} else { |
|
194 |
int row; |
|
195 |
int number = 0; |
|
196 |
for (row = 0; row < item->childCount(); ++row) { |
|
197 |
if (item->child(row)->checkState(0) == Qt::Checked) |
|
198 |
++number; |
|
199 |
} |
|
200 |
||
201 |
// Mark/unmark all child items when marking/unmarking top-level |
|
202 |
// items. |
|
203 |
if (state == Qt::Checked && number == 0) { |
|
204 |
for (row = 0; row < item->childCount(); ++row) { |
|
205 |
if (item->child(row)->checkState(0) == Qt::Unchecked) |
|
206 |
item->child(row)->setCheckState(0, Qt::Checked); |
|
207 |
} |
|
208 |
} else if (state == Qt::Unchecked && number > 0) { |
|
209 |
for (row = 0; row < item->childCount(); ++row) { |
|
210 |
if (item->child(row)->checkState(0) == Qt::Checked) |
|
211 |
item->child(row)->setCheckState(0, Qt::Unchecked); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
printAction->setEnabled(markedCount > 0); |
|
217 |
printPreviewAction->setEnabled(markedCount > 0); |
|
218 |
} |
|
219 |
||
220 |
void MainWindow::on_printAction_triggered() |
|
221 |
{ |
|
222 |
pageMap = currentPageMap(); |
|
223 |
||
224 |
if (pageMap.count() == 0) |
|
225 |
return; |
|
226 |
||
227 |
QPrinter printer(QPrinter::HighResolution); |
|
228 |
QPrintDialog dialog(&printer, this); |
|
229 |
if (dialog.exec() != QDialog::Accepted) |
|
230 |
return; |
|
231 |
||
232 |
int from = printer.fromPage(); |
|
233 |
int to = printer.toPage(); |
|
234 |
if (from <= 0 && to <= 0) |
|
235 |
printer.setFromTo(1, pageMap.keys().count()); |
|
236 |
||
237 |
printDocument(&printer); |
|
238 |
} |
|
239 |
||
240 |
void MainWindow::printDocument(QPrinter *printer) |
|
241 |
{ |
|
242 |
printer->setFromTo(1, pageMap.count()); |
|
243 |
||
244 |
QProgressDialog progress(tr("Preparing font samples..."), tr("&Cancel"), |
|
245 |
0, pageMap.count(), this); |
|
246 |
progress.setWindowModality(Qt::ApplicationModal); |
|
247 |
progress.setWindowTitle(tr("Font Sampler")); |
|
248 |
progress.setMinimum(printer->fromPage() - 1); |
|
249 |
progress.setMaximum(printer->toPage()); |
|
250 |
||
251 |
QPainter painter; |
|
252 |
painter.begin(printer); |
|
253 |
bool firstPage = true; |
|
254 |
||
255 |
for (int page = printer->fromPage(); page <= printer->toPage(); ++page) { |
|
256 |
||
257 |
if (!firstPage) |
|
258 |
printer->newPage(); |
|
259 |
||
260 |
qApp->processEvents(); |
|
261 |
if (progress.wasCanceled()) |
|
262 |
break; |
|
263 |
||
264 |
printPage(page - 1, &painter, printer); |
|
265 |
progress.setValue(page); |
|
266 |
firstPage = false; |
|
267 |
} |
|
268 |
||
269 |
painter.end(); |
|
270 |
} |
|
271 |
||
272 |
void MainWindow::on_printPreviewAction_triggered() |
|
273 |
{ |
|
274 |
pageMap = currentPageMap(); |
|
275 |
||
276 |
if (pageMap.count() == 0) |
|
277 |
return; |
|
278 |
||
279 |
QPrinter printer(QPrinter::HighResolution); |
|
280 |
QPrintPreviewDialog preview(&printer, this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
281 |
connect(&preview, SIGNAL(paintRequested(QPrinter*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
282 |
this, SLOT(printDocument(QPrinter*))); |
0 | 283 |
preview.exec(); |
284 |
} |
|
285 |
||
286 |
QMap<QString, StyleItems> MainWindow::currentPageMap() |
|
287 |
{ |
|
288 |
QMap<QString, StyleItems> pageMap; |
|
289 |
||
290 |
for (int row = 0; row < fontTree->topLevelItemCount(); ++row) { |
|
291 |
QTreeWidgetItem *familyItem = fontTree->topLevelItem(row); |
|
292 |
QString family; |
|
293 |
||
294 |
if (familyItem->checkState(0) == Qt::Checked) { |
|
295 |
family = familyItem->text(0); |
|
296 |
pageMap[family] = StyleItems(); |
|
297 |
} |
|
298 |
||
299 |
for (int childRow = 0; childRow < familyItem->childCount(); ++childRow) { |
|
300 |
QTreeWidgetItem *styleItem = familyItem->child(childRow); |
|
301 |
if (styleItem->checkState(0) == Qt::Checked) |
|
302 |
pageMap[family].append(styleItem); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
return pageMap; |
|
307 |
} |
|
308 |
||
309 |
void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer) |
|
310 |
{ |
|
311 |
QString family = pageMap.keys()[index]; |
|
312 |
StyleItems items = pageMap[family]; |
|
313 |
||
314 |
// Find the dimensions of the text on each page. |
|
315 |
qreal width = 0.0; |
|
316 |
qreal height = 0.0; |
|
317 |
foreach (QTreeWidgetItem *item, items) { |
|
318 |
QString style = item->text(0); |
|
319 |
int weight = item->data(0, Qt::UserRole).toInt(); |
|
320 |
bool italic = item->data(0, Qt::UserRole + 1).toBool(); |
|
321 |
||
322 |
// Calculate the maximum width and total height of the text. |
|
323 |
foreach (int size, sampleSizes) { |
|
324 |
QFont font(family, size, weight, italic); |
|
325 |
font = QFont(font, painter->device()); |
|
326 |
QFontMetricsF fontMetrics(font); |
|
327 |
QRectF rect = fontMetrics.boundingRect( |
|
328 |
QString("%1 %2").arg(family).arg(style)); |
|
329 |
width = qMax(rect.width(), width); |
|
330 |
height += rect.height(); |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
qreal xScale = printer->pageRect().width() / width; |
|
335 |
qreal yScale = printer->pageRect().height() / height; |
|
336 |
qreal scale = qMin(xScale, yScale); |
|
337 |
||
338 |
qreal remainingHeight = printer->pageRect().height()/scale - height; |
|
339 |
qreal spaceHeight = (remainingHeight/4.0) / (items.count() + 1); |
|
340 |
qreal interLineHeight = (remainingHeight/4.0) / (sampleSizes.count() * items.count()); |
|
341 |
||
342 |
painter->save(); |
|
343 |
painter->translate(printer->pageRect().width()/2.0, printer->pageRect().height()/2.0); |
|
344 |
painter->scale(scale, scale); |
|
345 |
painter->setBrush(QBrush(Qt::black)); |
|
346 |
||
347 |
qreal x = -width/2.0; |
|
348 |
qreal y = -height/2.0 - remainingHeight/4.0 + spaceHeight; |
|
349 |
||
350 |
foreach (QTreeWidgetItem *item, items) { |
|
351 |
QString style = item->text(0); |
|
352 |
int weight = item->data(0, Qt::UserRole).toInt(); |
|
353 |
bool italic = item->data(0, Qt::UserRole + 1).toBool(); |
|
354 |
||
355 |
// Draw each line of text. |
|
356 |
foreach (int size, sampleSizes) { |
|
357 |
QFont font(family, size, weight, italic); |
|
358 |
font = QFont(font, painter->device()); |
|
359 |
QFontMetricsF fontMetrics(font); |
|
360 |
QRectF rect = fontMetrics.boundingRect(QString("%1 %2").arg( |
|
361 |
font.family()).arg(style)); |
|
362 |
y += rect.height(); |
|
363 |
painter->setFont(font); |
|
364 |
painter->drawText(QPointF(x, y), |
|
365 |
QString("%1 %2").arg(family).arg(style)); |
|
366 |
y += interLineHeight; |
|
367 |
} |
|
368 |
y += spaceHeight; |
|
369 |
} |
|
370 |
||
371 |
painter->restore(); |
|
372 |
} |