source: trunk/examples/widgets/charactermap/characterwidget.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "characterwidget.h"
45
46//! [0]
47CharacterWidget::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]
58void 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]
68void 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
77void 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
88void 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]
99QSize CharacterWidget::sizeHint() const
100{
101 return QSize(columns*squareSize, (65536/columns)*squareSize);
102}
103//! [3]
104
105//! [4]
106void 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]
120void 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]
134void 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]
Note: See TracBrowser for help on using the repository browser.