21#include "kcharselect.h"
22#include "kcharselect.moc"
28#include <tqfontdatabase.h>
36#include <tqstylesheet.h>
38#include <tqvalidator.h>
40#include <tdeapplication.h>
46class KCharSelectTableToolTip;
47class KCharSelectTable::KCharSelectTablePrivate
50 KCharSelectTableToolTip *t =
nullptr;
53class KCharSelect::KCharSelectPrivate
56 TQLineEdit *unicodeLine =
nullptr;
57 TQScrollBar *scrollBar =
nullptr;
60TQFontDatabase * KCharSelect::fontDataBase = 0;
62void KCharSelect::cleanupFontDatabase()
75class 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->font() )
126KCharSelectTable::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 );
147KCharSelectTable::~KCharSelectTable () {
153void KCharSelectTable::setFont(
const TQString &_font )
162void KCharSelectTable::setChar(
const TQChar &_chr )
171void KCharSelectTable::setTableNum(
int _tableNum )
173 if(_tableNum == vTableNum)
175 focusItem = TQChar( _tableNum * 256 );
177 vTableNum = _tableNum;
180 emit tableNumChanged(vTableNum);
184TQSize KCharSelectTable::sizeHint()
const
187 int h = cellHeight();
192 return TQSize( w, h );
197void KCharSelectTable::resizeEvent( TQResizeEvent *e )
199 TQGridView::resizeEvent(e);
203void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
205 const int new_w = e->size().width() / numCols();
206 const int new_h = e->size().height() / numRows();
208 if( new_w != cellWidth())
209 setCellWidth( new_w );
210 if( new_h != cellHeight())
211 setCellHeight( new_h );
213 TQGridView::viewportResizeEvent(e);
217void KCharSelectTable::paintCell(
class TQPainter* p,
int row,
int col )
219 const int w = cellWidth();
220 const int h = cellHeight();
221 const int x2 = w - 1;
222 const int y2 = h - 1;
229 TQFont font = TQFont( vFont );
230 font.setPixelSize(
int(.7 * h) );
232 unsigned short c = vTableNum * 256;
233 c += row * numCols();
236 if ( c == vChr.unicode() ) {
237 p->setBrush( TQBrush( colorGroup().highlight() ) );
239 p->drawRect( 0, 0, w, h );
240 p->setPen( colorGroup().highlightedText() );
241 vPos = TQPoint( col, row );
243 TQFontMetrics fm = TQFontMetrics( font );
245 p->setBrush( TQBrush( colorGroup().base() ) );
247 p->setBrush( TQBrush( colorGroup().button() ) );
249 p->drawRect( 0, 0, w, h );
250 p->setPen( colorGroup().text() );
253 if ( c == focusItem.unicode() && hasFocus() ) {
254 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
256 focusPos = TQPoint( col, row );
261 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
263 p->setPen( colorGroup().text() );
264 p->drawLine( x2, 0, x2, y2 );
265 p->drawLine( 0, y2, x2, y2 );
268 p->drawLine( 0, 0, x2, 0 );
270 p->drawLine( 0, 0, 0, y2 );
275void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
277 TQGridView::mouseMoveEvent(e);
281void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
283 contentsMouseMoveEvent(e);
287void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
289 contentsMouseMoveEvent(e);
290 emit doubleClicked();
294void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
296 contentsMouseMoveEvent( e );
298 if( e->isAccepted() ) {
299 emit activated( chr() );
305void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
307 const int row = rowAt( e->y() );
308 const int col = columnAt( e->x() );
309 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
310 const TQPoint oldPos = vPos;
315 vChr = charAt( vPos.y(), vPos.x() );
317 const TQPoint oldFocus = focusPos;
322 updateCell( oldFocus.y(), oldFocus.x() );
323 updateCell( oldPos.y(), oldPos.x() );
324 updateCell( vPos.y(), vPos.x() );
326 emit highlighted( vChr );
329 emit focusItemChanged( focusItem );
330 emit focusItemChanged();
339void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
341 switch ( e->key() ) {
355 if ( tableNum() < 255 ) {
356 setTableNum( tableNum() + 1 );
360 if ( tableNum() > 0 ) {
361 setTableNum( tableNum() - 1 );
365 emit activated(
' ' );
367 emit highlighted(
' ' );
370 case Key_Enter:
case Key_Return: {
371 const TQPoint oldPos = vPos;
376 updateCell( oldPos.y(), oldPos.x() );
377 updateCell( vPos.y(), vPos.x() );
379 emit activated( vChr );
381 emit highlighted( vChr );
388void KCharSelectTable::gotoLeft()
390 if ( focusPos.x() > 0 ) {
396void KCharSelectTable::gotoRight()
398 if ( focusPos.x() < numCols()-1 ) {
404void KCharSelectTable::gotoUp()
406 if ( focusPos.y() > 0 ) {
408 }
else if ( tableNum() > 0 ) {
409 emit tableNumChanged(--vTableNum);
410 doGoto(0, numRows()-1);
411 repaintContents(
false );
416void KCharSelectTable::gotoDown()
418 if ( focusPos.y() < numRows()-1 ) {
420 }
else if ( tableNum() < 255 ) {
421 emit tableNumChanged(++vTableNum);
422 doGoto(0, -(numRows()-1));
423 repaintContents(
false );
428void KCharSelectTable::doGoto(
int dx,
int dy)
430 TQPoint oldPos = focusPos;
431 focusPos += TQPoint(dx, dy);
432 focusItem = charAt( focusPos.y(), focusPos.x() );
434 updateCell( oldPos.y(), oldPos.x() );
435 updateCell( focusPos.y(), focusPos.x() );
437 emit focusItemChanged( vChr );
438 emit focusItemChanged();
442TQChar KCharSelectTable::charAt(
int row,
int col)
const
444 return TQChar( vTableNum * 256 + numCols() * row + col );
453 : TQVBox( parent, name ), d(new KCharSelectPrivate)
456 TQHBox*
const bar =
new TQHBox(
this );
459 TQLabel*
const lFont =
new TQLabel( i18n(
"Font:" ), bar );
460 lFont->resize( lFont->sizeHint() );
461 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
462 lFont->setMaximumWidth( lFont->sizeHint().width() );
464 fontCombo =
new TQComboBox(
true, bar );
466 fontCombo->resize( fontCombo->sizeHint() );
468 connect( fontCombo, TQ_SIGNAL( activated(
const TQString & ) ),
this, TQ_SLOT( fontSelected(
const TQString & ) ) );
470 TQLabel*
const lTable =
new TQLabel( i18n(
"Table:" ), bar );
471 lTable->resize( lTable->sizeHint() );
472 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
473 lTable->setMaximumWidth( lTable->sizeHint().width() );
475 tableSpinBox =
new TQSpinBox( 0, 255, 1, bar );
476 tableSpinBox->resize( tableSpinBox->sizeHint() );
478 TQLabel*
const lUnicode =
new TQLabel( i18n(
"&Unicode code point:" ), bar );
479 lUnicode->resize( lUnicode->sizeHint() );
480 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
481 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
483 const TQRegExp rx(
"[a-fA-F0-9]{1,4}" );
484 TQValidator*
const validator =
new TQRegExpValidator( rx,
this );
487 d->unicodeLine->setValidator(validator);
488 lUnicode->setBuddy(d->unicodeLine);
489 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
490 slotUpdateUnicode(_chr);
492 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotUnicodeEntered() ) );
494 TQHBox*
const box =
new TQHBox(
this);
497 charTable =
new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
498 const TQSize sz( charTable->contentsWidth() + 4 ,
499 charTable->contentsHeight() + 4 );
500 charTable->resize( sz );
502 charTable->setMinimumSize( sz );
503 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
504 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
505 charTable->installEventFilter(
this );
507 d->scrollBar =
new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
509 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
512 connect( tableSpinBox, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
513 connect( d->scrollBar, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
514 connect( charTable, TQ_SIGNAL( tableNumChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
516 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( slotUpdateUnicode(
const TQChar & ) ) );
517 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( charHighlighted(
const TQChar & ) ) );
518 connect( charTable, TQ_SIGNAL( highlighted() ),
this, TQ_SLOT( charHighlighted() ) );
519 connect( charTable, TQ_SIGNAL( activated(
const TQChar & ) ),
this, TQ_SLOT( charActivated(
const TQChar & ) ) );
520 connect( charTable, TQ_SIGNAL( activated() ),
this, TQ_SLOT( charActivated() ) );
521 connect( charTable, TQ_SIGNAL( focusItemChanged(
const TQChar & ) ),
522 this, TQ_SLOT( charFocusItemChanged(
const TQChar & ) ) );
523 connect( charTable, TQ_SIGNAL( focusItemChanged() ),
this, TQ_SLOT( charFocusItemChanged() ) );
525 connect( charTable, TQ_SIGNAL(doubleClicked()),
this,TQ_SLOT(slotDoubleClicked()));
527 setFocusPolicy( TQWidget::StrongFocus );
528 setFocusProxy( charTable );
531KCharSelect::~KCharSelect()
539 return TQVBox::sizeHint();
545 const TQValueList<TQString>::Iterator it = fontList.find( _font );
546 if ( it != fontList.end() ) {
547 TQValueList<TQString>::Iterator it2 = fontList.begin();
549 for ( ; it != it2; ++it2, ++pos);
550 fontCombo->setCurrentItem( pos );
551 charTable->setFont( _font );
560 charTable->setChar( _chr );
561 slotUpdateUnicode( _chr );
567 tableSpinBox->setValue( _tableNum );
568 d->scrollBar->setValue( _tableNum );
569 charTable->setTableNum( _tableNum );
573void KCharSelect::fillFontCombo()
575 if ( !fontDataBase ) {
576 fontDataBase =
new TQFontDatabase();
577 tqAddPostRoutine( cleanupFontDatabase );
579 fontList=fontDataBase->families();
580 fontCombo->insertStringList( fontList );
584void KCharSelect::fontSelected(
const TQString &_font )
586 charTable->setFont( _font );
587 emit fontChanged( _font );
591void KCharSelect::tableChanged(
int _value )
597void KCharSelect::slotUnicodeEntered( )
599 const TQString s = d->unicodeLine->text();
604 const int uc = s.toInt(&ok, 16);
608 const int table = uc / 256;
609 charTable->setTableNum( table );
610 tableSpinBox->setValue(table);
611 d->scrollBar->setValue(table);
613 charTable->setChar( ch );
618void KCharSelect::slotUpdateUnicode(
const TQChar &c )
620 const int uc = c.unicode();
622 s.sprintf(
"%04X", uc);
623 d->unicodeLine->setText(s);
627bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
629 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
631 return TQApplication::sendEvent( d->scrollBar, e );
637void KCharSelectTable::virtual_hook(
int,
void*)
640void KCharSelect::virtual_hook(
int,
void* )
Character selection table.
KCharSelect(TQWidget *parent, const char *name, const TQString &font=TQString::null, const TQChar &chr=' ', int tableNum=0)
Constructor.
virtual void setChar(const TQChar &chr)
Sets the currently selected character to chr.
virtual void setFont(const TQString &font)
Sets the font which is displayed to font.
virtual TQSize sizeHint() const
Reimplemented.
virtual void setTableNum(int tableNum)
Sets the currently displayed table to tableNum.
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
An enhanced TQLineEdit widget for inputting text.
kdbgstream kdWarning(int area=0)
kndbgstream & endl(kndbgstream &s)
TQString name(StdAccel id)