QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
57  : QWidget( parent )
58 {
59  setMouseTracking( true );
60 }
61 
62 void CharacterWidget::setFont( const QFont &font )
63 {
64  mDisplayFont.setFamily( font.family() );
65  mSquareSize = std::max( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
66  adjustSize();
67  update();
68 }
69 
70 void CharacterWidget::setFontSize( double fontSize )
71 {
72  mDisplayFont.setPointSizeF( fontSize );
73  mSquareSize = std::max( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
74  adjustSize();
75  update();
76 }
77 
78 void CharacterWidget::setFontStyle( const QString &fontStyle )
79 {
80  QFontDatabase fontDatabase;
81  const QFont::StyleStrategy oldStrategy = mDisplayFont.styleStrategy();
82  mDisplayFont = fontDatabase.font( mDisplayFont.family(), fontStyle, mDisplayFont.pointSize() );
83  mDisplayFont.setStyleStrategy( oldStrategy );
84  mSquareSize = std::max( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
85  adjustSize();
86  update();
87 }
88 
90 {
91  if ( enable )
92  mDisplayFont.setStyleStrategy( QFont::PreferDefault );
93  else
94  mDisplayFont.setStyleStrategy( QFont::NoFontMerging );
95  adjustSize();
96  update();
97 }
98 
100 {
101  if ( mColumns == columns || columns < 1 )
102  return;
103  mColumns = columns;
104  adjustSize();
105  update();
106 }
107 
109 {
110  mLastKey = character.unicode();
111  update();
112 }
113 
115 {
116  return QSize( mColumns * mSquareSize, ( 65536 / mColumns ) * mSquareSize );
117 }
118 
119 void CharacterWidget::mouseMoveEvent( QMouseEvent *event )
120 {
121  QPoint widgetPosition = mapFromGlobal( event->globalPos() );
122  uint key = ( widgetPosition.y() / mSquareSize ) * mColumns + widgetPosition.x() / mSquareSize;
123 
124  QString text = tr( "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
125  .arg( mDisplayFont.family() )
126  .arg( QChar( key ) )
127  .arg( key, 16 );
128  QToolTip::showText( event->globalPos(), text, this );
129 }
130 
131 void CharacterWidget::mousePressEvent( QMouseEvent *event )
132 {
133  if ( event->button() == Qt::LeftButton )
134  {
135  mLastKey = ( event->y() / mSquareSize ) * mColumns + event->x() / mSquareSize;
136  if ( QChar( mLastKey ).category() != QChar::Other_NotAssigned )
137  emit characterSelected( QChar( mLastKey ) );
138  update();
139  }
140  else
141  QWidget::mousePressEvent( event );
142 }
143 
144 void CharacterWidget::paintEvent( QPaintEvent *event )
145 {
146  QPainter painter( this );
147  painter.fillRect( event->rect(), QBrush( Qt::white ) );
148  painter.setFont( mDisplayFont );
149 
150  QRect redrawRect = event->rect();
151  int beginRow = redrawRect.top() / mSquareSize;
152  int endRow = redrawRect.bottom() / mSquareSize;
153  int beginColumn = redrawRect.left() / mSquareSize;
154  int endColumn = redrawRect.right() / mSquareSize;
155 
156  painter.setPen( QPen( Qt::gray ) );
157  for ( int row = beginRow; row <= endRow; ++row )
158  {
159  for ( int column = beginColumn; column <= endColumn; ++column )
160  {
161  painter.drawRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
162  }
163  }
164 
165  QFontMetrics fontMetrics( mDisplayFont );
166  painter.setPen( QPen( Qt::black ) );
167  for ( int row = beginRow; row <= endRow; ++row )
168  {
169 
170  for ( int column = beginColumn; column <= endColumn; ++column )
171  {
172 
173  int key = row * mColumns + column;
174  painter.setClipRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
175 
176  if ( key == mLastKey )
177  painter.fillRect( column * mSquareSize + 1, row * mSquareSize + 1, mSquareSize, mSquareSize, QBrush( Qt::red ) );
178 
179  painter.drawText( column * mSquareSize + ( mSquareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
180  row * mSquareSize + 4 + fontMetrics.ascent(),
181  QString( QChar( key ) ) );
182  }
183  }
184 }
QSize sizeHint() const override
CharacterWidget(QWidget *parent=nullptr)
Constructor for CharacterWidget.
void setCharacter(QChar character)
Sets the currently selected character in the widget.
void setFontSize(double fontSize)
Sets the font size (in points) to render in the widget.
QFont font() const
Returns the font shown in the widget.
void setColumns(int columns)
Sets the number of columns of characters to show in the widget.
void updateFontMerging(bool enable)
void paintEvent(QPaintEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void setFont(const QFont &font)
Sets the font to show in the widget.
void setFontStyle(const QString &fontStyle)
Sets the font style to show in the widget.
int columns() const
Returns the number of columns of characters shown in the widget.
QChar character() const
Returns the currently selected character in the widget.
void mouseMoveEvent(QMouseEvent *event) override
void characterSelected(QChar character)
Emitted when a character is selected in the widget.