91 const QFontDatabase fontDatabase;
92 const QFont::StyleStrategy oldStrategy = mDisplayFont.styleStrategy();
93 mDisplayFont = fontDatabase.font( mDisplayFont.family(), fontStyle, mDisplayFont.pointSize() );
94 mDisplayFont.setStyleStrategy( oldStrategy );
95 mSquareSize = std::max( 34, QFontMetrics( mDisplayFont ).xHeight() * 3 );
121 const bool changed =
character.unicode() != mLastKey;
123 QWidget *widget = parentWidget();
126 QScrollArea *scrollArea = qobject_cast<QScrollArea *>( widget->parent() );
127 if ( scrollArea && mLastKey < 65536 )
129 scrollArea->ensureVisible( 0, mLastKey / mColumns * mSquareSize );
151 const QFontMetrics fm( mDisplayFont );
153 if ( event->key() == Qt::Key_Right )
155 int next = std::min( mLastKey + 1, 0xfffc );
156 while ( next < 0xfffc && !fm.inFont( QChar( next ) ) )
162 else if ( event->key() == Qt::Key_Left )
164 int next = mLastKey - 1;
165 while ( next > 0 && !fm.inFont( QChar( next ) ) )
171 else if ( event->key() == Qt::Key_Down )
173 int next = std::min( mLastKey + mColumns, 0xfffc );
174 while ( next < 0xfffc && !fm.inFont( QChar( next ) ) )
176 next = std::min( next + mColumns, 0xfffc );
180 else if ( event->key() == Qt::Key_Up )
182 int next = std::max( 0, mLastKey - mColumns );
183 while ( next > 0 && !fm.inFont( QChar( next ) ) )
185 next = std::max( 0, next - mColumns );
189 else if ( event->key() == Qt::Key_Home )
192 while ( next < 0xfffc && !fm.inFont( QChar( next ) ) )
198 else if ( event->key() == Qt::Key_End )
201 while ( next > 0 && !fm.inFont( QChar( next ) ) )
207 else if ( !event->text().isEmpty() )
209 QChar chr =
event->text().at( 0 );
210 if ( chr.unicode() != mLastKey )
219 const QPoint widgetPosition = mapFromGlobal( event->globalPos() );
220 const uint key = ( widgetPosition.y() / mSquareSize ) * mColumns + widgetPosition.x() / mSquareSize;
222 const QString text = u
"<p style=\"text-align: center; font-size: 24pt; font-family: %1\">%2</p><p><table><tr><td>%3</td><td>%2</td></tr><tr><td>%4</td><td>%5</td></tr><tr><td>%6</td><td>0x%7</td></tr></table>"_s
223 .arg( mDisplayFont.family() )
225 .arg( tr(
"Character" ), tr(
"Decimal" ) )
227 .arg( tr(
"Hex" ), QString::number( key, 16 ) );
228 QToolTip::showText( event->globalPos(), text,
this );
246 QPainter painter(
this );
247 painter.setFont( mDisplayFont );
249 const QFontMetrics fontMetrics( mDisplayFont );
251 const QRect redrawRect =
event->rect();
252 const int beginRow = redrawRect.top() / mSquareSize;
253 const int endRow = redrawRect.bottom() / mSquareSize;
254 const int beginColumn = redrawRect.left() / mSquareSize;
255 const int endColumn = std::min( mColumns - 1, redrawRect.right() / mSquareSize );
257 const QPalette palette = qApp->palette();
258 painter.setPen( QPen( palette.color( QPalette::Mid ) ) );
259 for (
int row = beginRow; row <= endRow; ++row )
261 for (
int column = beginColumn; column <= endColumn; ++column )
263 const int key = row * mColumns + column;
264 painter.setBrush( fontMetrics.inFont( QChar( key ) ) ? QBrush( palette.color( QPalette::Base ) ) : Qt::NoBrush );
265 painter.drawRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
269 for (
int row = beginRow; row <= endRow; ++row )
271 for (
int column = beginColumn; column <= endColumn; ++column )
273 const int key = row * mColumns + column;
274 painter.setClipRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
275 painter.setPen( QPen( palette.color( key == mLastKey ? QPalette::HighlightedText : QPalette::WindowText ) ) );
277 if ( key == mLastKey )
278 painter.fillRect( column * mSquareSize + 1, row * mSquareSize + 1, mSquareSize, mSquareSize, QBrush( palette.color( QPalette::Highlight ) ) );
280 if ( fontMetrics.inFont( QChar( key ) ) )
282 painter.drawText( column * mSquareSize + ( mSquareSize / 2 ) - fontMetrics.boundingRect( QChar( key ) ).width() / 2, row * mSquareSize + 4 + fontMetrics.ascent(), QString( QChar( key ) ) );