QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
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 
118 {
119  lastKey = character.unicode();
120  update();
121 }
122 
125 {
126  return QSize( columns*squareSize, ( 65536 / columns )*squareSize );
127 }
129 
132 {
133  QPoint widgetPosition = mapFromGlobal( event->globalPos() );
134  uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
135 
136  QString text = tr( "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
137  .arg( displayFont.family() )
138  .arg( QChar( key ) )
139  .arg( key, 16 );
140  QToolTip::showText( event->globalPos(), text, this );
141 }
143 
146 {
147  if ( event->button() == Qt::LeftButton )
148  {
149  lastKey = ( event->y() / squareSize ) * columns + event->x() / squareSize;
150  if ( QChar( lastKey ).category() != QChar::Other_NotAssigned )
151  emit characterSelected( QChar( lastKey ) );
152  update();
153  }
154  else
155  QWidget::mousePressEvent( event );
156 }
158 
161 {
162  QPainter painter( this );
163  painter.fillRect( event->rect(), QBrush( Qt::white ) );
164  painter.setFont( displayFont );
166 
168  QRect redrawRect = event->rect();
169  int beginRow = redrawRect.top() / squareSize;
170  int endRow = redrawRect.bottom() / squareSize;
171  int beginColumn = redrawRect.left() / squareSize;
172  int endColumn = redrawRect.right() / squareSize;
174 
176  painter.setPen( QPen( Qt::gray ) );
177  for ( int row = beginRow; row <= endRow; ++row )
178  {
179  for ( int column = beginColumn; column <= endColumn; ++column )
180  {
181  painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize );
182  }
184  }
186 
188  QFontMetrics fontMetrics( displayFont );
189  painter.setPen( QPen( Qt::black ) );
190  for ( int row = beginRow; row <= endRow; ++row )
191  {
192 
193  for ( int column = beginColumn; column <= endColumn; ++column )
194  {
195 
196  int key = row * columns + column;
197  painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize );
198 
199  if ( key == lastKey )
200  painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
201 
202  painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
203  row*squareSize + 4 + fontMetrics.ascent(),
204  QString( QChar( key ) ) );
205  }
206  }
207 }
int ascent() const
QSize sizeHint() const override
[3]
void fillRect(const QRectF &rectangle, const QBrush &brush)
CharacterWidget(QWidget *parent=nullptr)
[0]
StyleStrategy styleStrategy() const
int right() const
void updateFont(const QFont &font)
[0]
void updateStyle(const QString &fontStyle)
[2]
int x() const
void setCharacter(QChar character)
void adjustSize()
void showText(const QPoint &pos, const QString &text, QWidget *w)
QString tr(const char *sourceText, const char *disambiguation, int n)
void updateSize(double fontSize)
[1]
void update()
int x() const
int y() const
const QRect & rect() const
void drawRect(const QRectF &rectangle)
void setFont(const QFont &font)
const QPoint & globalPos() const
void updateFontMerging(bool enable)
virtual void mousePressEvent(QMouseEvent *event)
int top() const
void setPen(const QColor &color)
int left() const
Qt::MouseButton button() const
void paintEvent(QPaintEvent *event) override
[5]
void setStyleStrategy(StyleStrategy s)
void drawText(const QPointF &position, const QString &text)
ushort unicode() const
void mousePressEvent(QMouseEvent *event) override
[4]
const QFont & font() const
int width(const QString &text, int len) const
void setPointSizeF(qreal pointSize)
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
QFontMetrics fontMetrics() const
QString family() const
QPoint mapFromGlobal(const QPoint &pos) const
void setFamily(const QString &family)
void updateColumns(int cols)
int bottom() const
void setMouseTracking(bool enable)
QFont font(const QString &family, const QString &style, int pointSize) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void mouseMoveEvent(QMouseEvent *event) override
[3]
virtual bool event(QEvent *event)
void characterSelected(QChar character)
int pointSize() const
void rect(int *x, int *y, int *width, int *height) const