21 #include "kcharselect.h"
22 #include "kcharselect.moc"
28 #include <tqfontdatabase.h>
30 #include <tqkeycode.h>
32 #include <tqpainter.h>
36 #include <tqstylesheet.h>
37 #include <tqtooltip.h>
38 #include <tqvalidator.h>
40 #include <tdeapplication.h>
43 #include <klineedit.h>
46 class KCharSelectTableToolTip;
47 class KCharSelectTable::KCharSelectTablePrivate
50 KCharSelectTableToolTip *t =
nullptr;
53 class KCharSelect::KCharSelectPrivate
56 TQLineEdit *unicodeLine =
nullptr;
57 TQScrollBar *scrollBar =
nullptr;
60 TQFontDatabase * KCharSelect::fontDataBase = 0;
62 void KCharSelect::cleanupFontDatabase()
75 class KCharSelectTableToolTip :
public TQToolTip
80 : TQToolTip(parent->viewport()), cst(parent) {}
83 void maybeTip(
const TQPoint &pnt) {
86 TQPoint cPnt = cst->viewportToContents(pnt);
88 int col = cst->columnAt( cPnt.x() );
89 int row = cst->rowAt( cPnt.y() );
90 if ( col < 0 || row < 0 || col > cst->numCols()-1 || row > cst->numRows()-1 ) {
93 TQChar ch = cst->charAt( row, col );
98 TQRect r = cst->cellGeometry( row, col );
99 r = TQRect( cst->contentsToViewport(r.topLeft()), r.size());
101 TQString hex = TQString().sprintf(
"%04X", ch.unicode() );
102 TQString character = TQStyleSheet::escape(ch);
104 tip(r, i18n(
"Character",
105 "<qt><font size=\"+4\" face=\"%1\">%2</font>"
106 "<br>Unicode code point: U+%3"
107 "<br>(In decimal: %4)"
108 "<br>(Character: %5)"
109 "</qt>" ).arg( cst->fontName() )
126 KCharSelectTable::KCharSelectTable( TQWidget *parent,
const char *name,
const TQString &_font,
127 const TQChar &_chr,
int _tableNum )
128 : TQGridView( parent,
name, TQt::WNoAutoErase ), vFont( _font ), vChr( _chr ),
129 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ),
142 d->t =
new KCharSelectTableToolTip(
this);
144 setFocusPolicy( TQWidget::StrongFocus );
147 KCharSelectTable::~KCharSelectTable () {
153 void KCharSelectTable::setFont(
const TQString &_font )
162 void KCharSelectTable::setChar(
const TQChar &_chr )
171 void KCharSelectTable::setTableNum(
int _tableNum )
173 if(_tableNum == vTableNum)
175 focusItem = TQChar( _tableNum * 256 );
177 vTableNum = _tableNum;
180 emit tableNumChanged(vTableNum);
184 TQSize KCharSelectTable::sizeHint()
const
187 int h = cellHeight();
192 return TQSize( w, h );
196 void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
198 const int new_w = e->size().width() / numCols();
199 const int new_h = e->size().height() / numRows();
201 if( new_w != cellWidth())
202 setCellWidth( new_w );
203 if( new_h != cellHeight())
204 setCellHeight( new_h );
206 TQGridView::viewportResizeEvent(e);
210 void KCharSelectTable::paintCell(
class TQPainter* p,
int row,
int col )
212 const int w = cellWidth();
213 const int h = cellHeight();
214 const int x2 = w - 1;
215 const int y2 = h - 1;
222 TQFont font = TQFont( vFont );
223 font.setPixelSize(
int(.7 * h) );
225 unsigned short c = vTableNum * 256;
226 c += row * numCols();
229 if ( c == vChr.unicode() ) {
230 p->setBrush( TQBrush( colorGroup().highlight() ) );
232 p->drawRect( 0, 0, w, h );
233 p->setPen( colorGroup().highlightedText() );
234 vPos = TQPoint( col, row );
236 TQFontMetrics fm = TQFontMetrics( font );
238 p->setBrush( TQBrush( colorGroup().base() ) );
240 p->setBrush( TQBrush( colorGroup().button() ) );
242 p->drawRect( 0, 0, w, h );
243 p->setPen( colorGroup().text() );
246 if ( c == focusItem.unicode() && hasFocus() ) {
247 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
249 focusPos = TQPoint( col, row );
254 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
256 p->setPen( colorGroup().text() );
257 p->drawLine( x2, 0, x2, y2 );
258 p->drawLine( 0, y2, x2, y2 );
261 p->drawLine( 0, 0, x2, 0 );
263 p->drawLine( 0, 0, 0, y2 );
267 void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
269 contentsMouseMoveEvent(e);
273 void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
275 contentsMouseMoveEvent(e);
276 emit doubleClicked();
280 void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
282 contentsMouseMoveEvent( e );
284 if( e->isAccepted() ) {
285 emit activated( chr() );
291 void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
293 const int row = rowAt( e->y() );
294 const int col = columnAt( e->x() );
295 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
296 const TQPoint oldPos = vPos;
301 vChr = charAt( vPos.y(), vPos.x() );
303 const TQPoint oldFocus = focusPos;
308 updateCell( oldFocus.y(), oldFocus.x() );
309 updateCell( oldPos.y(), oldPos.x() );
310 updateCell( vPos.y(), vPos.x() );
312 emit highlighted( vChr );
315 emit focusItemChanged( focusItem );
316 emit focusItemChanged();
325 void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
327 switch ( e->key() ) {
341 if ( tableNum() < 255 ) {
342 setTableNum( tableNum() + 1 );
346 if ( tableNum() > 0 ) {
347 setTableNum( tableNum() - 1 );
351 emit activated(
' ' );
353 emit highlighted(
' ' );
356 case Key_Enter:
case Key_Return: {
357 const TQPoint oldPos = vPos;
362 updateCell( oldPos.y(), oldPos.x() );
363 updateCell( vPos.y(), vPos.x() );
365 emit activated( vChr );
367 emit highlighted( vChr );
374 void KCharSelectTable::gotoLeft()
376 if ( focusPos.x() > 0 ) {
382 void KCharSelectTable::gotoRight()
384 if ( focusPos.x() < numCols()-1 ) {
390 void KCharSelectTable::gotoUp()
392 if ( focusPos.y() > 0 ) {
394 }
else if ( tableNum() > 0 ) {
395 emit tableNumChanged(--vTableNum);
396 doGoto(0, numRows()-1);
397 repaintContents(
false );
402 void KCharSelectTable::gotoDown()
404 if ( focusPos.y() < numRows()-1 ) {
406 }
else if ( tableNum() < 255 ) {
407 emit tableNumChanged(++vTableNum);
408 doGoto(0, -(numRows()-1));
409 repaintContents(
false );
414 void KCharSelectTable::doGoto(
int dx,
int dy)
416 TQPoint oldPos = focusPos;
417 focusPos += TQPoint(dx, dy);
418 focusItem = charAt( focusPos.y(), focusPos.x() );
420 updateCell( oldPos.y(), oldPos.x() );
421 updateCell( focusPos.y(), focusPos.x() );
423 emit focusItemChanged( vChr );
424 emit focusItemChanged();
428 TQChar KCharSelectTable::charAt(
int row,
int col)
const
430 return TQChar( vTableNum * 256 + numCols() * row + col );
439 : TQVBox( parent, name ), d(new KCharSelectPrivate)
442 TQHBox*
const bar =
new TQHBox(
this );
445 TQLabel*
const lFont =
new TQLabel( i18n(
"Font:" ), bar );
446 lFont->resize( lFont->sizeHint() );
447 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
448 lFont->setMaximumWidth( lFont->sizeHint().width() );
450 fontCombo =
new TQComboBox(
true, bar );
452 fontCombo->resize( fontCombo->sizeHint() );
454 connect( fontCombo, TQ_SIGNAL( activated(
const TQString & ) ),
this, TQ_SLOT( fontSelected(
const TQString & ) ) );
456 TQLabel*
const lTable =
new TQLabel( i18n(
"Table:" ), bar );
457 lTable->resize( lTable->sizeHint() );
458 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
459 lTable->setMaximumWidth( lTable->sizeHint().width() );
461 tableSpinBox =
new TQSpinBox( 0, 255, 1, bar );
462 tableSpinBox->resize( tableSpinBox->sizeHint() );
464 TQLabel*
const lUnicode =
new TQLabel( i18n(
"&Unicode code point:" ), bar );
465 lUnicode->resize( lUnicode->sizeHint() );
466 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
467 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
469 const TQRegExp rx(
"[a-fA-F0-9]{1,4}" );
470 TQValidator*
const validator =
new TQRegExpValidator( rx,
this );
473 d->unicodeLine->setValidator(validator);
474 lUnicode->setBuddy(d->unicodeLine);
475 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
476 slotUpdateUnicode(_chr);
478 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotUnicodeEntered() ) );
480 TQHBox*
const box =
new TQHBox(
this);
483 charTable =
new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
484 const TQSize sz( charTable->contentsWidth() + 4 ,
485 charTable->contentsHeight() + 4 );
486 charTable->resize( sz );
488 charTable->setMinimumSize( sz );
489 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
490 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
491 charTable->installEventFilter(
this );
493 d->scrollBar =
new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
495 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
498 connect( tableSpinBox, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
499 connect( d->scrollBar, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
500 connect( charTable, TQ_SIGNAL( tableNumChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
502 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( slotUpdateUnicode(
const TQChar & ) ) );
503 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( charHighlighted(
const TQChar & ) ) );
504 connect( charTable, TQ_SIGNAL( highlighted() ),
this, TQ_SLOT( charHighlighted() ) );
505 connect( charTable, TQ_SIGNAL( activated(
const TQChar & ) ),
this, TQ_SLOT( charActivated(
const TQChar & ) ) );
506 connect( charTable, TQ_SIGNAL( activated() ),
this, TQ_SLOT( charActivated() ) );
507 connect( charTable, TQ_SIGNAL( focusItemChanged(
const TQChar & ) ),
508 this, TQ_SLOT( charFocusItemChanged(
const TQChar & ) ) );
509 connect( charTable, TQ_SIGNAL( focusItemChanged() ),
this, TQ_SLOT( charFocusItemChanged() ) );
511 connect( charTable, TQ_SIGNAL(doubleClicked()),
this,TQ_SLOT(slotDoubleClicked()));
513 setFocusPolicy( TQWidget::StrongFocus );
514 setFocusProxy( charTable );
517 KCharSelect::~KCharSelect()
525 return TQVBox::sizeHint();
531 const TQValueList<TQString>::Iterator it = fontList.find( _font );
532 if ( it != fontList.end() ) {
533 TQValueList<TQString>::Iterator it2 = fontList.begin();
535 for ( ; it != it2; ++it2, ++pos);
536 fontCombo->setCurrentItem( pos );
537 charTable->setFont( _font );
546 charTable->setChar( _chr );
547 slotUpdateUnicode( _chr );
553 tableSpinBox->setValue( _tableNum );
554 d->scrollBar->setValue( _tableNum );
555 charTable->setTableNum( _tableNum );
559 void KCharSelect::fillFontCombo()
561 if ( !fontDataBase ) {
562 fontDataBase =
new TQFontDatabase();
563 tqAddPostRoutine( cleanupFontDatabase );
565 fontList=fontDataBase->families();
566 fontCombo->insertStringList( fontList );
570 void KCharSelect::fontSelected(
const TQString &_font )
572 charTable->setFont( _font );
573 emit fontChanged( _font );
577 void KCharSelect::tableChanged(
int _value )
583 void KCharSelect::slotUnicodeEntered( )
585 const TQString s = d->unicodeLine->text();
590 const int uc = s.toInt(&ok, 16);
594 const int table = uc / 256;
595 charTable->setTableNum( table );
596 tableSpinBox->setValue(table);
597 d->scrollBar->setValue(table);
599 charTable->setChar( ch );
604 void KCharSelect::slotUpdateUnicode(
const TQChar &c )
606 const int uc = c.unicode();
608 s.sprintf(
"%04X", uc);
609 d->unicodeLine->setText(s);
613 bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
615 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
617 return TQApplication::sendEvent( d->scrollBar, e );
623 void KCharSelectTable::virtual_hook(
int,
void*)
626 void KCharSelect::virtual_hook(
int,
void* )