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 )
120 return QSize( columns*squareSize, ( 65536 / columns )*squareSize );
127 QPoint widgetPosition = mapFromGlobal( event->globalPos() );
128 uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
130 QString text =
tr(
"<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
131 .arg( displayFont.family() )
134 QToolTip::showText( event->globalPos(), text, this );
141 if ( event->button() == Qt::LeftButton )
143 lastKey = (
event->y() / squareSize ) * columns + event->x() / squareSize;
144 if ( QChar( lastKey ).category() != QChar::Other_NotAssigned )
156 QPainter painter(
this );
157 painter.fillRect( event->rect(), QBrush( Qt::white ) );
158 painter.setFont( displayFont );
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;
170 painter.setPen( QPen( Qt::gray ) );
171 for (
int row = beginRow; row <= endRow; ++row )
173 for (
int column = beginColumn; column <= endColumn; ++column )
175 painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize );
182 QFontMetrics fontMetrics( displayFont );
183 painter.setPen( QPen( Qt::black ) );
184 for (
int row = beginRow; row <= endRow; ++row )
187 for (
int column = beginColumn; column <= endColumn; ++column )
190 int key = row * columns + column;
191 painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize );
193 if ( key == lastKey )
194 painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
196 painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
197 row*squareSize + 4 + fontMetrics.ascent(),
198 QString( QChar( key ) ) );