1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "characterwidget.h" |
52 | |
53 | #include <QFontDatabase> |
54 | #include <QMouseEvent> |
55 | #include <QPainter> |
56 | #include <QToolTip> |
57 | |
58 | //! [0] |
59 | CharacterWidget::CharacterWidget(QWidget *parent) |
60 | : QWidget(parent) |
61 | { |
62 | calculateSquareSize(); |
63 | setMouseTracking(true); |
64 | } |
65 | //! [0] |
66 | |
67 | //! [1] |
68 | void CharacterWidget::updateFont(const QFont &font) |
69 | { |
70 | displayFont.setFamily(font.family()); |
71 | calculateSquareSize(); |
72 | adjustSize(); |
73 | update(); |
74 | } |
75 | //! [1] |
76 | |
77 | //! [2] |
78 | void CharacterWidget::updateSize(const QString &fontSize) |
79 | { |
80 | displayFont.setPointSize(fontSize.toInt()); |
81 | calculateSquareSize(); |
82 | adjustSize(); |
83 | update(); |
84 | } |
85 | //! [2] |
86 | |
87 | void CharacterWidget::updateStyle(const QString &fontStyle) |
88 | { |
89 | QFontDatabase fontDatabase; |
90 | const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy(); |
91 | displayFont = fontDatabase.font(family: displayFont.family(), style: fontStyle, pointSize: displayFont.pointSize()); |
92 | displayFont.setStyleStrategy(oldStrategy); |
93 | calculateSquareSize(); |
94 | adjustSize(); |
95 | update(); |
96 | } |
97 | |
98 | void CharacterWidget::updateFontMerging(bool enable) |
99 | { |
100 | if (enable) |
101 | displayFont.setStyleStrategy(QFont::PreferDefault); |
102 | else |
103 | displayFont.setStyleStrategy(QFont::NoFontMerging); |
104 | adjustSize(); |
105 | update(); |
106 | } |
107 | |
108 | void CharacterWidget::calculateSquareSize() |
109 | { |
110 | squareSize = qMax(a: 16, b: 4 + QFontMetrics(displayFont, this).height()); |
111 | } |
112 | |
113 | //! [3] |
114 | QSize CharacterWidget::sizeHint() const |
115 | { |
116 | return QSize(columns*squareSize, (65536 / columns) * squareSize); |
117 | } |
118 | //! [3] |
119 | |
120 | //! [4] |
121 | void CharacterWidget::mouseMoveEvent(QMouseEvent *event) |
122 | { |
123 | QPoint widgetPosition = mapFromGlobal(event->globalPos()); |
124 | uint key = (widgetPosition.y() / squareSize) * columns + widgetPosition.x() / squareSize; |
125 | |
126 | QString text = QString::fromLatin1(str: "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">" ).arg(a: displayFont.family()) |
127 | + QChar(key) |
128 | + QString::fromLatin1(str: "</span><p>Value: 0x" ) |
129 | + QString::number(key, base: 16); |
130 | QToolTip::showText(pos: event->globalPos(), text, w: this); |
131 | } |
132 | //! [4] |
133 | |
134 | //! [5] |
135 | void CharacterWidget::mousePressEvent(QMouseEvent *event) |
136 | { |
137 | if (event->button() == Qt::LeftButton) { |
138 | lastKey = (event->y() / squareSize) * columns + event->x() / squareSize; |
139 | if (QChar(lastKey).category() != QChar::Other_NotAssigned) |
140 | emit characterSelected(character: QString(QChar(lastKey))); |
141 | update(); |
142 | } |
143 | else |
144 | QWidget::mousePressEvent(event); |
145 | } |
146 | //! [5] |
147 | |
148 | //! [6] |
149 | void CharacterWidget::paintEvent(QPaintEvent *event) |
150 | { |
151 | QPainter painter(this); |
152 | painter.fillRect(event->rect(), QBrush(Qt::white)); |
153 | painter.setFont(displayFont); |
154 | //! [6] |
155 | |
156 | //! [7] |
157 | QRect redrawRect = event->rect(); |
158 | int beginRow = redrawRect.top() / squareSize; |
159 | int endRow = redrawRect.bottom() / squareSize; |
160 | int beginColumn = redrawRect.left() / squareSize; |
161 | int endColumn = redrawRect.right() / squareSize; |
162 | //! [7] |
163 | |
164 | //! [8] |
165 | painter.setPen(QPen(Qt::gray)); |
166 | for (int row = beginRow; row <= endRow; ++row) { |
167 | for (int column = beginColumn; column <= endColumn; ++column) { |
168 | painter.drawRect(x: column * squareSize, y: row * squareSize, w: squareSize, h: squareSize); |
169 | } |
170 | //! [8] //! [9] |
171 | } |
172 | //! [9] |
173 | |
174 | //! [10] |
175 | QFontMetrics fontMetrics(displayFont); |
176 | painter.setPen(QPen(Qt::black)); |
177 | for (int row = beginRow; row <= endRow; ++row) { |
178 | for (int column = beginColumn; column <= endColumn; ++column) { |
179 | int key = row * columns + column; |
180 | painter.setClipRect(x: column * squareSize, y: row * squareSize, w: squareSize, h: squareSize); |
181 | |
182 | if (key == lastKey) |
183 | painter.fillRect(x: column * squareSize + 1, y: row * squareSize + 1, |
184 | w: squareSize, h: squareSize, b: QBrush(Qt::red)); |
185 | |
186 | painter.drawText(x: column * squareSize + (squareSize / 2) - |
187 | fontMetrics.horizontalAdvance(QChar(key)) / 2, |
188 | y: row * squareSize + 4 + fontMetrics.ascent(), |
189 | s: QString(QChar(key))); |
190 | } |
191 | } |
192 | } |
193 | //! [10] |
194 | |