|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "characterwidget.h" |
|
45 |
|
46 //! [0] |
|
47 CharacterWidget::CharacterWidget(QWidget *parent) |
|
48 : QWidget(parent) |
|
49 { |
|
50 squareSize = 24; |
|
51 columns = 16; |
|
52 lastKey = -1; |
|
53 setMouseTracking(true); |
|
54 } |
|
55 //! [0] |
|
56 |
|
57 //! [1] |
|
58 void CharacterWidget::updateFont(const QFont &font) |
|
59 { |
|
60 displayFont.setFamily(font.family()); |
|
61 squareSize = qMax(24, QFontMetrics(displayFont).xHeight() * 3); |
|
62 adjustSize(); |
|
63 update(); |
|
64 } |
|
65 //! [1] |
|
66 |
|
67 //! [2] |
|
68 void CharacterWidget::updateSize(const QString &fontSize) |
|
69 { |
|
70 displayFont.setPointSize(fontSize.toInt()); |
|
71 squareSize = qMax(24, QFontMetrics(displayFont).xHeight() * 3); |
|
72 adjustSize(); |
|
73 update(); |
|
74 } |
|
75 //! [2] |
|
76 |
|
77 void CharacterWidget::updateStyle(const QString &fontStyle) |
|
78 { |
|
79 QFontDatabase fontDatabase; |
|
80 const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy(); |
|
81 displayFont = fontDatabase.font(displayFont.family(), fontStyle, displayFont.pointSize()); |
|
82 displayFont.setStyleStrategy(oldStrategy); |
|
83 squareSize = qMax(24, QFontMetrics(displayFont).xHeight() * 3); |
|
84 adjustSize(); |
|
85 update(); |
|
86 } |
|
87 |
|
88 void CharacterWidget::updateFontMerging(bool enable) |
|
89 { |
|
90 if (enable) |
|
91 displayFont.setStyleStrategy(QFont::PreferDefault); |
|
92 else |
|
93 displayFont.setStyleStrategy(QFont::NoFontMerging); |
|
94 adjustSize(); |
|
95 update(); |
|
96 } |
|
97 |
|
98 //! [3] |
|
99 QSize CharacterWidget::sizeHint() const |
|
100 { |
|
101 return QSize(columns*squareSize, (65536/columns)*squareSize); |
|
102 } |
|
103 //! [3] |
|
104 |
|
105 //! [4] |
|
106 void CharacterWidget::mouseMoveEvent(QMouseEvent *event) |
|
107 { |
|
108 QPoint widgetPosition = mapFromGlobal(event->globalPos()); |
|
109 uint key = (widgetPosition.y()/squareSize)*columns + widgetPosition.x()/squareSize; |
|
110 |
|
111 QString text = QString::fromLatin1("<p>Character: <span style=\"font-size: 24pt; font-family: %1\">").arg(displayFont.family()) |
|
112 + QChar(key) |
|
113 + QString::fromLatin1("</span><p>Value: 0x") |
|
114 + QString::number(key, 16); |
|
115 QToolTip::showText(event->globalPos(), text, this); |
|
116 } |
|
117 //! [4] |
|
118 |
|
119 //! [5] |
|
120 void CharacterWidget::mousePressEvent(QMouseEvent *event) |
|
121 { |
|
122 if (event->button() == Qt::LeftButton) { |
|
123 lastKey = (event->y()/squareSize)*columns + event->x()/squareSize; |
|
124 if (QChar(lastKey).category() != QChar::NoCategory) |
|
125 emit characterSelected(QString(QChar(lastKey))); |
|
126 update(); |
|
127 } |
|
128 else |
|
129 QWidget::mousePressEvent(event); |
|
130 } |
|
131 //! [5] |
|
132 |
|
133 //! [6] |
|
134 void CharacterWidget::paintEvent(QPaintEvent *event) |
|
135 { |
|
136 QPainter painter(this); |
|
137 painter.fillRect(event->rect(), QBrush(Qt::white)); |
|
138 painter.setFont(displayFont); |
|
139 //! [6] |
|
140 |
|
141 //! [7] |
|
142 QRect redrawRect = event->rect(); |
|
143 int beginRow = redrawRect.top()/squareSize; |
|
144 int endRow = redrawRect.bottom()/squareSize; |
|
145 int beginColumn = redrawRect.left()/squareSize; |
|
146 int endColumn = redrawRect.right()/squareSize; |
|
147 //! [7] |
|
148 |
|
149 //! [8] |
|
150 painter.setPen(QPen(Qt::gray)); |
|
151 for (int row = beginRow; row <= endRow; ++row) { |
|
152 for (int column = beginColumn; column <= endColumn; ++column) { |
|
153 painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize); |
|
154 } |
|
155 //! [8] //! [9] |
|
156 } |
|
157 //! [9] |
|
158 |
|
159 //! [10] |
|
160 QFontMetrics fontMetrics(displayFont); |
|
161 painter.setPen(QPen(Qt::black)); |
|
162 for (int row = beginRow; row <= endRow; ++row) { |
|
163 |
|
164 for (int column = beginColumn; column <= endColumn; ++column) { |
|
165 |
|
166 int key = row*columns + column; |
|
167 painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize); |
|
168 |
|
169 if (key == lastKey) |
|
170 painter.fillRect(column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush(Qt::red)); |
|
171 |
|
172 painter.drawText(column*squareSize + (squareSize / 2) - fontMetrics.width(QChar(key))/2, |
|
173 row*squareSize + 4 + fontMetrics.ascent(), |
|
174 QString(QChar(key))); |
|
175 } |
|
176 } |
|
177 } |
|
178 //! [10] |