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>
45 #include <tdeunicodedata.h>
47 class KCharSelectTableToolTip;
48 class KCharSelectTable::KCharSelectTablePrivate
51 KCharSelectTableToolTip *t =
nullptr;
54 class KCharSelect::KCharSelectPrivate
57 TQLineEdit *unicodeLine =
nullptr;
58 TQScrollBar *scrollBar =
nullptr;
61 TQFontDatabase * KCharSelect::fontDataBase = 0;
63 void KCharSelect::cleanupFontDatabase()
76 class KCharSelectTableToolTip :
public TQToolTip
81 : TQToolTip(parent->viewport()), cst(parent) {}
84 void maybeTip(
const TQPoint &pnt) {
87 TQPoint cPnt = cst->viewportToContents(pnt);
89 int col = cst->columnAt( cPnt.x() );
90 int row = cst->rowAt( cPnt.y() );
91 if ( col < 0 || row < 0 || col > cst->numCols()-1 || row > cst->numRows()-1 ) {
94 TQChar ch = cst->charAt( row, col );
99 TDEUnicodeData *unicodeData = TDEUnicodeData::instance();
101 const TDEUnicodeData::Category *category = unicodeData->findCategory(ch.unicode());
102 TQString categoryName = category ? i18n(category->name) : i18n(
"(unknown)");
104 const TDEUnicodeData::Subcategory *subcategory = category
105 ? unicodeData->findSubcategory(category, ch.unicode())
107 TQString subcategoryName = subcategory ? i18n(subcategory->name) : i18n(
"(unknown)");
109 TQRect r = cst->cellGeometry( row, col );
110 r = TQRect( cst->contentsToViewport(r.topLeft()), r.size());
112 TQString hex = TQString().sprintf(
"%04X", ch.unicode() );
113 TQString character = TQStyleSheet::escape(ch);
115 tip(r, i18n(
"Character",
117 "<tr><td colspan='2'><font size=\"+4\" face=\"%1\">%2</font></td></tr>"
118 "<tr><td>Category:</td><td><b>%3</b></td></tr>"
119 "<tr><td>Subcategory:</td><td><b>%4</b></td></tr>"
120 "<tr><td>Code point:</td><td><b>U+%5</b></td></tr>"
121 "<tr><td>Decimal:</td><td><b>%6</b></td></tr>"
122 "</table></qt>").arg( cst->font() )
125 .arg( subcategoryName )
140 KCharSelectTable::KCharSelectTable( TQWidget *parent,
const char *name,
const TQString &_font,
141 const TQChar &_chr,
int _tableNum )
142 : TQGridView( parent,
name, TQt::WNoAutoErase ), vFont( _font ), vChr( _chr ),
143 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ),
156 d->t =
new KCharSelectTableToolTip(
this);
158 setFocusPolicy( TQWidget::StrongFocus );
161 KCharSelectTable::~KCharSelectTable () {
167 void KCharSelectTable::setFont(
const TQString &_font )
176 void KCharSelectTable::setChar(
const TQChar &_chr )
185 void KCharSelectTable::setTableNum(
int _tableNum )
187 if(_tableNum == vTableNum)
189 focusItem = TQChar( _tableNum * 256 );
191 vTableNum = _tableNum;
194 emit tableNumChanged(vTableNum);
198 TQSize KCharSelectTable::sizeHint()
const
201 int h = cellHeight();
206 return TQSize( w, h );
211 void KCharSelectTable::resizeEvent( TQResizeEvent *e )
213 TQGridView::resizeEvent(e);
217 void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
219 const int new_w = e->size().width() / numCols();
220 const int new_h = e->size().height() / numRows();
222 if( new_w != cellWidth())
223 setCellWidth( new_w );
224 if( new_h != cellHeight())
225 setCellHeight( new_h );
227 TQGridView::viewportResizeEvent(e);
231 void KCharSelectTable::paintCell(
class TQPainter* p,
int row,
int col )
233 const int w = cellWidth();
234 const int h = cellHeight();
235 const int x2 = w - 1;
236 const int y2 = h - 1;
243 TQFont font = TQFont( vFont );
244 font.setPixelSize(
int(.7 * h) );
246 unsigned short c = vTableNum * 256;
247 c += row * numCols();
250 if ( c == vChr.unicode() ) {
251 p->setBrush( TQBrush( colorGroup().highlight() ) );
253 p->drawRect( 0, 0, w, h );
254 p->setPen( colorGroup().highlightedText() );
255 vPos = TQPoint( col, row );
257 TQFontMetrics fm = TQFontMetrics( font );
259 p->setBrush( TQBrush( colorGroup().base() ) );
261 p->setBrush( TQBrush( colorGroup().button() ) );
263 p->drawRect( 0, 0, w, h );
264 p->setPen( colorGroup().text() );
267 if ( c == focusItem.unicode() && hasFocus() ) {
268 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
270 focusPos = TQPoint( col, row );
275 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
277 p->setPen( colorGroup().text() );
278 p->drawLine( x2, 0, x2, y2 );
279 p->drawLine( 0, y2, x2, y2 );
282 p->drawLine( 0, 0, x2, 0 );
284 p->drawLine( 0, 0, 0, y2 );
289 void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
291 TQGridView::mouseMoveEvent(e);
295 void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
297 contentsMouseMoveEvent(e);
301 void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
303 contentsMouseMoveEvent(e);
304 emit doubleClicked();
308 void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
310 contentsMouseMoveEvent( e );
312 if( e->isAccepted() ) {
313 emit activated( chr() );
319 void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
321 const int row = rowAt( e->y() );
322 const int col = columnAt( e->x() );
323 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
324 const TQPoint oldPos = vPos;
329 vChr = charAt( vPos.y(), vPos.x() );
331 const TQPoint oldFocus = focusPos;
336 updateCell( oldFocus.y(), oldFocus.x() );
337 updateCell( oldPos.y(), oldPos.x() );
338 updateCell( vPos.y(), vPos.x() );
340 emit highlighted( vChr );
343 emit focusItemChanged( focusItem );
344 emit focusItemChanged();
353 void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
355 switch ( e->key() ) {
369 if ( tableNum() < 255 ) {
370 setTableNum( tableNum() + 1 );
374 if ( tableNum() > 0 ) {
375 setTableNum( tableNum() - 1 );
379 emit activated(
' ' );
381 emit highlighted(
' ' );
384 case Key_Enter:
case Key_Return: {
385 const TQPoint oldPos = vPos;
390 updateCell( oldPos.y(), oldPos.x() );
391 updateCell( vPos.y(), vPos.x() );
393 emit activated( vChr );
395 emit highlighted( vChr );
402 void KCharSelectTable::gotoLeft()
404 if ( focusPos.x() > 0 ) {
410 void KCharSelectTable::gotoRight()
412 if ( focusPos.x() < numCols()-1 ) {
418 void KCharSelectTable::gotoUp()
420 if ( focusPos.y() > 0 ) {
422 }
else if ( tableNum() > 0 ) {
423 emit tableNumChanged(--vTableNum);
424 doGoto(0, numRows()-1);
425 repaintContents(
false );
430 void KCharSelectTable::gotoDown()
432 if ( focusPos.y() < numRows()-1 ) {
434 }
else if ( tableNum() < 255 ) {
435 emit tableNumChanged(++vTableNum);
436 doGoto(0, -(numRows()-1));
437 repaintContents(
false );
442 void KCharSelectTable::doGoto(
int dx,
int dy)
444 TQPoint oldPos = focusPos;
445 focusPos += TQPoint(dx, dy);
446 focusItem = charAt( focusPos.y(), focusPos.x() );
448 updateCell( oldPos.y(), oldPos.x() );
449 updateCell( focusPos.y(), focusPos.x() );
451 emit focusItemChanged( vChr );
452 emit focusItemChanged();
456 TQChar KCharSelectTable::charAt(
int row,
int col)
const
458 return TQChar( vTableNum * 256 + numCols() * row + col );
467 : TQVBox( parent, name ), d(new KCharSelectPrivate)
470 TQHBox*
const bar =
new TQHBox(
this );
473 TQLabel*
const lFont =
new TQLabel( i18n(
"Font:" ), bar );
474 lFont->resize( lFont->sizeHint() );
475 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
476 lFont->setMaximumWidth( lFont->sizeHint().width() );
478 fontCombo =
new TQComboBox(
true, bar );
480 fontCombo->resize( fontCombo->sizeHint() );
482 connect( fontCombo, TQ_SIGNAL( activated(
const TQString & ) ),
this, TQ_SLOT( fontSelected(
const TQString & ) ) );
484 TQLabel*
const lTable =
new TQLabel( i18n(
"Table:" ), bar );
485 lTable->resize( lTable->sizeHint() );
486 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
487 lTable->setMaximumWidth( lTable->sizeHint().width() );
489 tableSpinBox =
new TQSpinBox( 0, 255, 1, bar );
490 tableSpinBox->resize( tableSpinBox->sizeHint() );
492 TQLabel*
const lUnicode =
new TQLabel( i18n(
"&Unicode code point:" ), bar );
493 lUnicode->resize( lUnicode->sizeHint() );
494 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
495 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
497 const TQRegExp rx(
"[a-fA-F0-9]{1,4}" );
498 TQValidator*
const validator =
new TQRegExpValidator( rx,
this );
501 d->unicodeLine->setValidator(validator);
502 lUnicode->setBuddy(d->unicodeLine);
503 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
504 slotUpdateUnicode(_chr);
506 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotUnicodeEntered() ) );
508 TQHBox*
const box =
new TQHBox(
this);
511 charTable =
new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
512 const TQSize sz( charTable->contentsWidth() + 4 ,
513 charTable->contentsHeight() + 4 );
514 charTable->resize( sz );
516 charTable->setMinimumSize( sz );
517 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
518 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
519 charTable->installEventFilter(
this );
521 d->scrollBar =
new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
523 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
526 connect( tableSpinBox, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
527 connect( d->scrollBar, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
528 connect( charTable, TQ_SIGNAL( tableNumChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
530 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( slotUpdateUnicode(
const TQChar & ) ) );
531 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( charHighlighted(
const TQChar & ) ) );
532 connect( charTable, TQ_SIGNAL( highlighted() ),
this, TQ_SLOT( charHighlighted() ) );
533 connect( charTable, TQ_SIGNAL( activated(
const TQChar & ) ),
this, TQ_SLOT( charActivated(
const TQChar & ) ) );
534 connect( charTable, TQ_SIGNAL( activated() ),
this, TQ_SLOT( charActivated() ) );
535 connect( charTable, TQ_SIGNAL( focusItemChanged(
const TQChar & ) ),
536 this, TQ_SLOT( charFocusItemChanged(
const TQChar & ) ) );
537 connect( charTable, TQ_SIGNAL( focusItemChanged() ),
this, TQ_SLOT( charFocusItemChanged() ) );
539 connect( charTable, TQ_SIGNAL(doubleClicked()),
this,TQ_SLOT(slotDoubleClicked()));
541 setFocusPolicy( TQWidget::StrongFocus );
542 setFocusProxy( charTable );
545 KCharSelect::~KCharSelect()
553 return TQVBox::sizeHint();
559 const TQValueList<TQString>::Iterator it = fontList.find( _font );
560 if ( it != fontList.end() ) {
561 TQValueList<TQString>::Iterator it2 = fontList.begin();
563 for ( ; it != it2; ++it2, ++pos);
564 fontCombo->setCurrentItem( pos );
565 charTable->setFont( _font );
574 charTable->setChar( _chr );
575 slotUpdateUnicode( _chr );
581 tableSpinBox->setValue( _tableNum );
582 d->scrollBar->setValue( _tableNum );
583 charTable->setTableNum( _tableNum );
587 void KCharSelect::fillFontCombo()
589 if ( !fontDataBase ) {
590 fontDataBase =
new TQFontDatabase();
591 tqAddPostRoutine( cleanupFontDatabase );
593 fontList=fontDataBase->families();
594 fontCombo->insertStringList( fontList );
598 void KCharSelect::fontSelected(
const TQString &_font )
600 charTable->setFont( _font );
601 emit fontChanged( _font );
605 void KCharSelect::tableChanged(
int _value )
611 void KCharSelect::slotUnicodeEntered( )
613 const TQString s = d->unicodeLine->text();
618 const int uc = s.toInt(&ok, 16);
622 const int table = uc / 256;
623 charTable->setTableNum( table );
624 tableSpinBox->setValue(table);
625 d->scrollBar->setValue(table);
627 charTable->setChar( ch );
632 void KCharSelect::slotUpdateUnicode(
const TQChar &c )
634 const int uc = c.unicode();
636 s.sprintf(
"%04X", uc);
637 d->unicodeLine->setText(s);
641 bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
643 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
645 return TQApplication::sendEvent( d->scrollBar, e );
651 void KCharSelectTable::virtual_hook(
int,
void*)
654 void KCharSelect::virtual_hook(
int,
void* )