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 ), vFont( _font ), vChr( _chr ),
129 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ),
132 setBackgroundColor( colorGroup().base() );
140 repaintContents(
false );
142 d->t =
new KCharSelectTableToolTip(
this);
144 setFocusPolicy( TQWidget::StrongFocus );
145 setBackgroundMode( TQWidget::NoBackground );
148 KCharSelectTable::~KCharSelectTable () {
154 void KCharSelectTable::setFont(
const TQString &_font )
159 repaintContents(
false );
163 void KCharSelectTable::setChar(
const TQChar &_chr )
168 repaintContents(
false );
172 void KCharSelectTable::setTableNum(
int _tableNum )
174 if(_tableNum == vTableNum)
176 focusItem = TQChar( _tableNum * 256 );
178 vTableNum = _tableNum;
179 repaintContents(
false );
181 emit tableNumChanged(vTableNum);
185 TQSize KCharSelectTable::sizeHint()
const
188 int h = cellHeight();
193 return TQSize( w, h );
197 void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
199 const int new_w = e->size().width() / numCols();
200 const int new_h = e->size().height() / numRows();
202 if( new_w != cellWidth())
203 setCellWidth( new_w );
204 if( new_h != cellHeight())
205 setCellHeight( new_h );
207 TQGridView::viewportResizeEvent(e);
211 void KCharSelectTable::paintCell(
class TQPainter* p,
int row,
int col )
213 const int w = cellWidth();
214 const int h = cellHeight();
215 const int x2 = w - 1;
216 const int y2 = h - 1;
223 TQFont font = TQFont( vFont );
224 font.setPixelSize(
int(.7 * h) );
226 unsigned short c = vTableNum * 256;
227 c += row * numCols();
230 if ( c == vChr.unicode() ) {
231 p->setBrush( TQBrush( colorGroup().highlight() ) );
233 p->drawRect( 0, 0, w, h );
234 p->setPen( colorGroup().highlightedText() );
235 vPos = TQPoint( col, row );
237 TQFontMetrics fm = TQFontMetrics( font );
239 p->setBrush( TQBrush( colorGroup().base() ) );
241 p->setBrush( TQBrush( colorGroup().button() ) );
243 p->drawRect( 0, 0, w, h );
244 p->setPen( colorGroup().text() );
247 if ( c == focusItem.unicode() && hasFocus() ) {
248 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
250 focusPos = TQPoint( col, row );
255 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
257 p->setPen( colorGroup().text() );
258 p->drawLine( x2, 0, x2, y2 );
259 p->drawLine( 0, y2, x2, y2 );
262 p->drawLine( 0, 0, x2, 0 );
264 p->drawLine( 0, 0, 0, y2 );
268 void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
270 contentsMouseMoveEvent(e);
274 void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
276 contentsMouseMoveEvent(e);
277 emit doubleClicked();
281 void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
283 contentsMouseMoveEvent( e );
285 if( e->isAccepted() ) {
286 emit activated( chr() );
292 void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
294 const int row = rowAt( e->y() );
295 const int col = columnAt( e->x() );
296 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
297 const TQPoint oldPos = vPos;
302 vChr = charAt( vPos.y(), vPos.x() );
304 const TQPoint oldFocus = focusPos;
309 repaintCell( oldFocus.y(), oldFocus.x(),
true );
310 repaintCell( oldPos.y(), oldPos.x(),
true );
311 repaintCell( vPos.y(), vPos.x(),
true );
313 emit highlighted( vChr );
316 emit focusItemChanged( focusItem );
317 emit focusItemChanged();
326 void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
328 switch ( e->key() ) {
342 if ( tableNum() < 255 ) {
343 setTableNum( tableNum() + 1 );
347 if ( tableNum() > 0 ) {
348 setTableNum( tableNum() - 1 );
352 emit activated(
' ' );
354 emit highlighted(
' ' );
357 case Key_Enter:
case Key_Return: {
358 const TQPoint oldPos = vPos;
363 repaintCell( oldPos.y(), oldPos.x(),
true );
364 repaintCell( vPos.y(), vPos.x(),
true );
366 emit activated( vChr );
368 emit highlighted( vChr );
375 void KCharSelectTable::gotoLeft()
377 if ( focusPos.x() > 0 ) {
383 void KCharSelectTable::gotoRight()
385 if ( focusPos.x() < numCols()-1 ) {
391 void KCharSelectTable::gotoUp()
393 if ( focusPos.y() > 0 ) {
395 }
else if ( tableNum() > 0 ) {
396 emit tableNumChanged(--vTableNum);
397 doGoto(0, numRows()-1);
398 repaintContents(
false );
403 void KCharSelectTable::gotoDown()
405 if ( focusPos.y() < numRows()-1 ) {
407 }
else if ( tableNum() < 255 ) {
408 emit tableNumChanged(++vTableNum);
409 doGoto(0, -(numRows()-1));
410 repaintContents(
false );
415 void KCharSelectTable::doGoto(
int dx,
int dy)
417 TQPoint oldPos = focusPos;
418 focusPos += TQPoint(dx, dy);
419 focusItem = charAt( focusPos.y(), focusPos.x() );
421 repaintCell( oldPos.y(), oldPos.x(),
true );
422 repaintCell( focusPos.y(), focusPos.x(),
true );
424 emit focusItemChanged( vChr );
425 emit focusItemChanged();
429 TQChar KCharSelectTable::charAt(
int row,
int col)
const
431 return TQChar( vTableNum * 256 + numCols() * row + col );
440 : TQVBox( parent, name ), d(new KCharSelectPrivate)
443 TQHBox*
const bar =
new TQHBox(
this );
446 TQLabel*
const lFont =
new TQLabel( i18n(
"Font:" ), bar );
447 lFont->resize( lFont->sizeHint() );
448 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
449 lFont->setMaximumWidth( lFont->sizeHint().width() );
451 fontCombo =
new TQComboBox(
true, bar );
453 fontCombo->resize( fontCombo->sizeHint() );
455 connect( fontCombo, TQ_SIGNAL( activated(
const TQString & ) ),
this, TQ_SLOT( fontSelected(
const TQString & ) ) );
457 TQLabel*
const lTable =
new TQLabel( i18n(
"Table:" ), bar );
458 lTable->resize( lTable->sizeHint() );
459 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
460 lTable->setMaximumWidth( lTable->sizeHint().width() );
462 tableSpinBox =
new TQSpinBox( 0, 255, 1, bar );
463 tableSpinBox->resize( tableSpinBox->sizeHint() );
465 TQLabel*
const lUnicode =
new TQLabel( i18n(
"&Unicode code point:" ), bar );
466 lUnicode->resize( lUnicode->sizeHint() );
467 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
468 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
470 const TQRegExp rx(
"[a-fA-F0-9]{1,4}" );
471 TQValidator*
const validator =
new TQRegExpValidator( rx,
this );
474 d->unicodeLine->setValidator(validator);
475 lUnicode->setBuddy(d->unicodeLine);
476 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
477 slotUpdateUnicode(_chr);
479 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotUnicodeEntered() ) );
481 TQHBox*
const box =
new TQHBox(
this);
484 charTable =
new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
485 const TQSize sz( charTable->contentsWidth() + 4 ,
486 charTable->contentsHeight() + 4 );
487 charTable->resize( sz );
489 charTable->setMinimumSize( sz );
490 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
491 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
492 charTable->installEventFilter(
this );
494 d->scrollBar =
new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
496 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
499 connect( tableSpinBox, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
500 connect( d->scrollBar, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
501 connect( charTable, TQ_SIGNAL( tableNumChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
503 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( slotUpdateUnicode(
const TQChar & ) ) );
504 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( charHighlighted(
const TQChar & ) ) );
505 connect( charTable, TQ_SIGNAL( highlighted() ),
this, TQ_SLOT( charHighlighted() ) );
506 connect( charTable, TQ_SIGNAL( activated(
const TQChar & ) ),
this, TQ_SLOT( charActivated(
const TQChar & ) ) );
507 connect( charTable, TQ_SIGNAL( activated() ),
this, TQ_SLOT( charActivated() ) );
508 connect( charTable, TQ_SIGNAL( focusItemChanged(
const TQChar & ) ),
509 this, TQ_SLOT( charFocusItemChanged(
const TQChar & ) ) );
510 connect( charTable, TQ_SIGNAL( focusItemChanged() ),
this, TQ_SLOT( charFocusItemChanged() ) );
512 connect( charTable, TQ_SIGNAL(doubleClicked()),
this,TQ_SLOT(slotDoubleClicked()));
514 setFocusPolicy( TQWidget::StrongFocus );
515 setFocusProxy( charTable );
518 KCharSelect::~KCharSelect()
526 return TQVBox::sizeHint();
532 const TQValueList<TQString>::Iterator it = fontList.find( _font );
533 if ( it != fontList.end() ) {
534 TQValueList<TQString>::Iterator it2 = fontList.begin();
536 for ( ; it != it2; ++it2, ++pos);
537 fontCombo->setCurrentItem( pos );
538 charTable->setFont( _font );
547 charTable->setChar( _chr );
548 slotUpdateUnicode( _chr );
554 tableSpinBox->setValue( _tableNum );
555 d->scrollBar->setValue( _tableNum );
556 charTable->setTableNum( _tableNum );
560 void KCharSelect::fillFontCombo()
562 if ( !fontDataBase ) {
563 fontDataBase =
new TQFontDatabase();
564 tqAddPostRoutine( cleanupFontDatabase );
566 fontList=fontDataBase->families();
567 fontCombo->insertStringList( fontList );
571 void KCharSelect::fontSelected(
const TQString &_font )
573 charTable->setFont( _font );
574 emit fontChanged( _font );
578 void KCharSelect::tableChanged(
int _value )
584 void KCharSelect::slotUnicodeEntered( )
586 const TQString s = d->unicodeLine->text();
591 const int uc = s.toInt(&ok, 16);
595 const int table = uc / 256;
596 charTable->setTableNum( table );
597 tableSpinBox->setValue(table);
598 d->scrollBar->setValue(table);
600 charTable->setChar( ch );
605 void KCharSelect::slotUpdateUnicode(
const TQChar &c )
607 const int uc = c.unicode();
609 s.sprintf(
"%04X", uc);
610 d->unicodeLine->setText(s);
614 bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
616 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
618 return TQApplication::sendEvent( d->scrollBar, e );
624 void KCharSelectTable::virtual_hook(
int,
void*)
627 void KCharSelect::virtual_hook(
int,
void* )