QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
characterwidget.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation ([email protected])
6 **
7 ** This library/program is free software; you can redistribute it
8 ** and/or modify it under the terms of the GNU Library General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2 of the License, or ( at your option ) any later version.
11 **
12 ** This file is part of the examples of the Qt Toolkit.
13 **
14 ** $QT_BEGIN_LICENSE:LGPL$
15 ** Commercial Usage
16 ** Licensees holding valid Qt Commercial licenses may use this file in
17 ** accordance with the Qt Commercial License Agreement provided with the
18 ** Software or, alternatively, in accordance with the terms contained in
19 ** a written agreement between you and Nokia.
20 **
21 ** GNU Lesser General Public License Usage
22 ** Alternatively, this file may be used under the terms of the
23 ** GNU Lesser General Public License version 2.1 as published by the Free Software
24 ** Foundation and appearing in the file LICENSE.LGPL included in the
25 ** packaging of this file. Please review the following information to
26 ** ensure the GNU Lesser General Public License version 2.1 requirements
27 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
28 ** In addition, as a special exception, Nokia gives you certain additional
29 ** rights. These rights are described in the Nokia Qt LGPL Exception
30 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
31 **
32 ** GNU General Public License Usage
33 ** Alternatively, this file may be used under the terms of the GNU
34 ** General Public License version 3.0 as published by the Free Software
35 ** Foundation and appearing in the file LICENSE.GPL included in the
36 ** packaging of this file. Please review the following information to
37 ** ensure the GNU General Public License version 3.0 requirements will be
38 ** met: http://www.gnu.org/copyleft/gpl.html.
39 **
40 ** If you have questions regarding the use of this file, please contact
41 ** Nokia at [email protected].
42 ** $QT_END_LICENSE$
43 **
44 ****************************************************************************/
45 
46 #include <QFontDatabase>
47 #include <QMouseEvent>
48 #include <QPaintEvent>
49 #include <QPainter>
50 #include <QPen>
51 #include <QPoint>
52 #include <QToolTip>
53 
54 #include "characterwidget.h"
55 
58  : QWidget( parent )
59 {
60  squareSize = 24;
61  columns = 16;
62  lastKey = -1;
63  setMouseTracking( true );
64 }
66 
68 void CharacterWidget::updateFont( const QFont &font )
69 {
70  displayFont.setFamily( font.family() );
71  squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
72  adjustSize();
73  update();
74 }
76 
78 void CharacterWidget::updateSize( double fontSize )
79 {
80  displayFont.setPointSizeF( fontSize );
81  squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
82  adjustSize();
83  update();
84 }
86 
87 void CharacterWidget::updateStyle( const QString &fontStyle )
88 {
89  QFontDatabase fontDatabase;
90  const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy();
91  displayFont = fontDatabase.font( displayFont.family(), fontStyle, displayFont.pointSize() );
92  displayFont.setStyleStrategy( oldStrategy );
93  squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
94  adjustSize();
95  update();
96 }
97 
99 {
100  if ( enable )
101  displayFont.setStyleStrategy( QFont::PreferDefault );
102  else
103  displayFont.setStyleStrategy( QFont::NoFontMerging );
104  adjustSize();
105  update();
106 }
107 
109 {
110  if ( columns == cols || cols < 1 )
111  return;
112  columns = cols;
113  adjustSize();
114  update();
115 }
116 
119 {
120  return QSize( columns*squareSize, ( 65536 / columns )*squareSize );
121 }
123 
125 void CharacterWidget::mouseMoveEvent( QMouseEvent *event )
126 {
127  QPoint widgetPosition = mapFromGlobal( event->globalPos() );
128  uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
129 
130  QString text = tr( "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
131  .arg( displayFont.family() )
132  .arg( QChar( key ) )
133  .arg( key, 16 );
134  QToolTip::showText( event->globalPos(), text, this );
135 }
137 
139 void CharacterWidget::mousePressEvent( QMouseEvent *event )
140 {
141  if ( event->button() == Qt::LeftButton )
142  {
143  lastKey = ( event->y() / squareSize ) * columns + event->x() / squareSize;
144  if ( QChar( lastKey ).category() != QChar::Other_NotAssigned )
145  emit characterSelected( QChar( lastKey ) );
146  update();
147  }
148  else
149  QWidget::mousePressEvent( event );
150 }
152 
154 void CharacterWidget::paintEvent( QPaintEvent *event )
155 {
156  QPainter painter( this );
157  painter.fillRect( event->rect(), QBrush( Qt::white ) );
158  painter.setFont( displayFont );
160 
162  QRect redrawRect = event->rect();
163  int beginRow = redrawRect.top() / squareSize;
164  int endRow = redrawRect.bottom() / squareSize;
165  int beginColumn = redrawRect.left() / squareSize;
166  int endColumn = redrawRect.right() / squareSize;
168 
170  painter.setPen( QPen( Qt::gray ) );
171  for ( int row = beginRow; row <= endRow; ++row )
172  {
173  for ( int column = beginColumn; column <= endColumn; ++column )
174  {
175  painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize );
176  }
178  }
180 
182  QFontMetrics fontMetrics( displayFont );
183  painter.setPen( QPen( Qt::black ) );
184  for ( int row = beginRow; row <= endRow; ++row )
185  {
186 
187  for ( int column = beginColumn; column <= endColumn; ++column )
188  {
189 
190  int key = row * columns + column;
191  painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize );
192 
193  if ( key == lastKey )
194  painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
195 
196  painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
197  row*squareSize + 4 + fontMetrics.ascent(),
198  QString( QChar( key ) ) );
199  }
200  }
201 }