46 #include <QFontDatabase> 47 #include <QMouseEvent> 48 #include <QPaintEvent> 63 setMouseTracking(
true );
70 displayFont.setFamily( font.family() );
71 squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
80 displayFont.setPointSizeF( fontSize );
81 squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
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 );
101 displayFont.setStyleStrategy( QFont::PreferDefault );
103 displayFont.setStyleStrategy( QFont::NoFontMerging );
110 if ( columns == cols || cols < 1 )
119 lastKey = character.unicode();
126 return QSize( columns*squareSize, ( 65536 / columns )*squareSize );
133 QPoint widgetPosition = mapFromGlobal( event->globalPos() );
134 uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
136 QString text =
tr(
"<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
137 .arg( displayFont.family() )
140 QToolTip::showText( event->globalPos(), text, this );
147 if ( event->button() == Qt::LeftButton )
149 lastKey = (
event->y() / squareSize ) * columns + event->x() / squareSize;
150 if ( QChar( lastKey ).category() != QChar::Other_NotAssigned )
155 QWidget::mousePressEvent( event );
162 QPainter painter(
this );
163 painter.fillRect( event->rect(), QBrush( Qt::white ) );
164 painter.setFont( displayFont );
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;
176 painter.setPen( QPen( Qt::gray ) );
177 for (
int row = beginRow; row <= endRow; ++row )
179 for (
int column = beginColumn; column <= endColumn; ++column )
181 painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize );
188 QFontMetrics fontMetrics( displayFont );
189 painter.setPen( QPen( Qt::black ) );
190 for (
int row = beginRow; row <= endRow; ++row )
193 for (
int column = beginColumn; column <= endColumn; ++column )
196 int key = row * columns + column;
197 painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize );
199 if ( key == lastKey )
200 painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
202 painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
203 row*squareSize + 4 + fontMetrics.ascent(),
204 QString( QChar( key ) ) );