88 const QFontDatabase fontDatabase;
89 const QFont::StyleStrategy oldStrategy = mDisplayFont.styleStrategy();
90 mDisplayFont = fontDatabase.font( mDisplayFont.family(), fontStyle, mDisplayFont.pointSize() );
91 mDisplayFont.setStyleStrategy( oldStrategy );
92 mSquareSize = std::max( 34, QFontMetrics( mDisplayFont ).xHeight() * 3 );
118 const bool changed =
character.unicode() != mLastKey;
120 QWidget *widget = parentWidget();
123 QScrollArea *scrollArea = qobject_cast<QScrollArea *>( widget->parent() );
124 if ( scrollArea && mLastKey < 65536 )
126 scrollArea->ensureVisible( 0, mLastKey / mColumns * mSquareSize );
148 const QFontMetrics fm( mDisplayFont );
150 if ( event->key() == Qt::Key_Right )
152 int next = std::min( mLastKey + 1, 0xfffc );
153 while ( next < 0xfffc && !fm.inFont( QChar( next ) ) )
159 else if ( event->key() == Qt::Key_Left )
161 int next = mLastKey - 1;
162 while ( next > 0 && !fm.inFont( QChar( next ) ) )
168 else if ( event->key() == Qt::Key_Down )
170 int next = std::min( mLastKey + mColumns, 0xfffc );
171 while ( next < 0xfffc && !fm.inFont( QChar( next ) ) )
173 next = std::min( next + mColumns, 0xfffc );
177 else if ( event->key() == Qt::Key_Up )
179 int next = std::max( 0, mLastKey - mColumns );
180 while ( next > 0 && !fm.inFont( QChar( next ) ) )
182 next = std::max( 0, next - mColumns );
186 else if ( event->key() == Qt::Key_Home )
189 while ( next < 0xfffc && !fm.inFont( QChar( next ) ) )
195 else if ( event->key() == Qt::Key_End )
198 while ( next > 0 && !fm.inFont( QChar( next ) ) )
204 else if ( !event->text().isEmpty() )
206 QChar chr =
event->text().at( 0 );
207 if ( chr.unicode() != mLastKey )
216 const QPoint widgetPosition = mapFromGlobal( event->globalPos() );
217 const uint key = ( widgetPosition.y() / mSquareSize ) * mColumns + widgetPosition.x() / mSquareSize;
219 const QString text = QStringLiteral(
"<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>" )
220 .arg( mDisplayFont.family() )
222 .arg( tr(
"Character" ), tr(
"Decimal" ) )
224 .arg( tr(
"Hex" ), QString::number( key, 16 ) );
225 QToolTip::showText( event->globalPos(), text,
this );
243 QPainter painter(
this );
244 painter.setFont( mDisplayFont );
246 const QFontMetrics fontMetrics( mDisplayFont );
248 const QRect redrawRect =
event->rect();
249 const int beginRow = redrawRect.top() / mSquareSize;
250 const int endRow = redrawRect.bottom() / mSquareSize;
251 const int beginColumn = redrawRect.left() / mSquareSize;
252 const int endColumn = std::min( mColumns - 1, redrawRect.right() / mSquareSize );
254 const QPalette palette = qApp->palette();
255 painter.setPen( QPen( palette.color( QPalette::Mid ) ) );
256 for (
int row = beginRow; row <= endRow; ++row )
258 for (
int column = beginColumn; column <= endColumn; ++column )
260 const int key = row * mColumns + column;
261 painter.setBrush( fontMetrics.inFont( QChar( key ) ) ? QBrush( palette.color( QPalette::Base ) ) : Qt::NoBrush );
262 painter.drawRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
266 for (
int row = beginRow; row <= endRow; ++row )
268 for (
int column = beginColumn; column <= endColumn; ++column )
270 const int key = row * mColumns + column;
271 painter.setClipRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
272 painter.setPen( QPen( palette.color( key == mLastKey ? QPalette::HighlightedText : QPalette::WindowText ) ) );
274 if ( key == mLastKey )
275 painter.fillRect( column * mSquareSize + 1, row * mSquareSize + 1, mSquareSize, mSquareSize, QBrush( palette.color( QPalette::Highlight ) ) );
277 if ( fontMetrics.inFont( QChar( key ) ) )
279 painter.drawText( column * mSquareSize + ( mSquareSize / 2 ) - fontMetrics.boundingRect( QChar( key ) ).width() / 2, row * mSquareSize + 4 + fontMetrics.ascent(), QString( QChar( key ) ) );