|
| 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 | +** 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 qt-info@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( double fontSize ) |
| 69 | +{ |
| 70 | + displayFont.setPointSizeF( fontSize ); |
| 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 | + { |
| 124 | + lastKey = ( event->y() / squareSize ) * columns + event->x() / squareSize; |
| 125 | + if ( QChar( lastKey ).category() != QChar::NoCategory ) |
| 126 | + emit characterSelected( QChar( lastKey ) ); |
| 127 | + update(); |
| 128 | + } |
| 129 | + else |
| 130 | + QWidget::mousePressEvent( event ); |
| 131 | +} |
| 132 | +//! [5] |
| 133 | + |
| 134 | +//! [6] |
| 135 | +void CharacterWidget::paintEvent( QPaintEvent *event ) |
| 136 | +{ |
| 137 | + QPainter painter( this ); |
| 138 | + painter.fillRect( event->rect(), QBrush( Qt::white ) ); |
| 139 | + painter.setFont( displayFont ); |
| 140 | +//! [6] |
| 141 | + |
| 142 | +//! [7] |
| 143 | + QRect redrawRect = event->rect(); |
| 144 | + int beginRow = redrawRect.top() / squareSize; |
| 145 | + int endRow = redrawRect.bottom() / squareSize; |
| 146 | + int beginColumn = redrawRect.left() / squareSize; |
| 147 | + int endColumn = redrawRect.right() / squareSize; |
| 148 | +//! [7] |
| 149 | + |
| 150 | +//! [8] |
| 151 | + painter.setPen( QPen( Qt::gray ) ); |
| 152 | + for ( int row = beginRow; row <= endRow; ++row ) |
| 153 | + { |
| 154 | + for ( int column = beginColumn; column <= endColumn; ++column ) |
| 155 | + { |
| 156 | + painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize ); |
| 157 | + } |
| 158 | +//! [8] //! [9] |
| 159 | + } |
| 160 | +//! [9] |
| 161 | + |
| 162 | +//! [10] |
| 163 | + QFontMetrics fontMetrics( displayFont ); |
| 164 | + painter.setPen( QPen( Qt::black ) ); |
| 165 | + for ( int row = beginRow; row <= endRow; ++row ) |
| 166 | + { |
| 167 | + |
| 168 | + for ( int column = beginColumn; column <= endColumn; ++column ) |
| 169 | + { |
| 170 | + |
| 171 | + int key = row * columns + column; |
| 172 | + painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize ); |
| 173 | + |
| 174 | + if ( key == lastKey ) |
| 175 | + painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) ); |
| 176 | + |
| 177 | + painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2, |
| 178 | + row*squareSize + 4 + fontMetrics.ascent(), |
| 179 | + QString( QChar( key ) ) ); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | +//! [10] |
0 commit comments