| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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] | 
|---|