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 ), 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 );
148KCharSelectTable::~KCharSelectTable () {
154void KCharSelectTable::setFont(
const TQString &_font )
159 repaintContents(
false );
163void KCharSelectTable::setChar(
const TQChar &_chr )
168 repaintContents(
false );
172void KCharSelectTable::setTableNum(
int _tableNum )
174 if(_tableNum == vTableNum)
176 focusItem = TQChar( _tableNum * 256 );
178 vTableNum = _tableNum;
179 repaintContents(
false );
181 emit tableNumChanged(vTableNum);
185TQSize KCharSelectTable::sizeHint()
const
188 int h = cellHeight();
193 return TQSize( w, h );
198void KCharSelectTable::resizeEvent( TQResizeEvent *e )
200 TQGridView::resizeEvent(e);
204void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
206 const int new_w = e->size().width() / numCols();
207 const int new_h = e->size().height() / numRows();
209 if( new_w != cellWidth())
210 setCellWidth( new_w );
211 if( new_h != cellHeight())
212 setCellHeight( new_h );
214 TQGridView::viewportResizeEvent(e);
218void KCharSelectTable::paintCell(
class TQPainter* p,
int row,
int col )
220 const int w = cellWidth();
221 const int h = cellHeight();
222 const int x2 = w - 1;
223 const int y2 = h - 1;
230 TQFont font = TQFont( vFont );
231 font.setPixelSize(
int(.7 * h) );
233 unsigned short c = vTableNum * 256;
234 c += row * numCols();
237 if ( c == vChr.unicode() ) {
238 p->setBrush( TQBrush( colorGroup().highlight() ) );
240 p->drawRect( 0, 0, w, h );
241 p->setPen( colorGroup().highlightedText() );
242 vPos = TQPoint( col, row );
244 TQFontMetrics fm = TQFontMetrics( font );
246 p->setBrush( TQBrush( colorGroup().base() ) );
248 p->setBrush( TQBrush( colorGroup().button() ) );
250 p->drawRect( 0, 0, w, h );
251 p->setPen( colorGroup().text() );
254 if ( c == focusItem.unicode() && hasFocus() ) {
255 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
257 focusPos = TQPoint( col, row );
262 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
264 p->setPen( colorGroup().text() );
265 p->drawLine( x2, 0, x2, y2 );
266 p->drawLine( 0, y2, x2, y2 );
269 p->drawLine( 0, 0, x2, 0 );
271 p->drawLine( 0, 0, 0, y2 );
276void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
278 TQGridView::mouseMoveEvent(e);
282void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
284 contentsMouseMoveEvent(e);
288void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
290 contentsMouseMoveEvent(e);
291 emit doubleClicked();
295void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
297 contentsMouseMoveEvent( e );
299 if( e->isAccepted() ) {
300 emit activated( chr() );
306void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
308 const int row = rowAt( e->y() );
309 const int col = columnAt( e->x() );
310 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
311 const TQPoint oldPos = vPos;
316 vChr = charAt( vPos.y(), vPos.x() );
318 const TQPoint oldFocus = focusPos;
323 repaintCell( oldFocus.y(), oldFocus.x(),
true );
324 repaintCell( oldPos.y(), oldPos.x(),
true );
325 repaintCell( vPos.y(), vPos.x(),
true );
327 emit highlighted( vChr );
330 emit focusItemChanged( focusItem );
331 emit focusItemChanged();
340void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
342 switch ( e->key() ) {
356 if ( tableNum() < 255 ) {
357 setTableNum( tableNum() + 1 );
361 if ( tableNum() > 0 ) {
362 setTableNum( tableNum() - 1 );
366 emit activated(
' ' );
368 emit highlighted(
' ' );
371 case Key_Enter:
case Key_Return: {
372 const TQPoint oldPos = vPos;
377 repaintCell( oldPos.y(), oldPos.x(),
true );
378 repaintCell( vPos.y(), vPos.x(),
true );
380 emit activated( vChr );
382 emit highlighted( vChr );
389void KCharSelectTable::gotoLeft()
391 if ( focusPos.x() > 0 ) {
397void KCharSelectTable::gotoRight()
399 if ( focusPos.x() < numCols()-1 ) {
405void KCharSelectTable::gotoUp()
407 if ( focusPos.y() > 0 ) {
409 }
else if ( tableNum() > 0 ) {
410 emit tableNumChanged(--vTableNum);
411 doGoto(0, numRows()-1);
412 repaintContents(
false );
417void KCharSelectTable::gotoDown()
419 if ( focusPos.y() < numRows()-1 ) {
421 }
else if ( tableNum() < 255 ) {
422 emit tableNumChanged(++vTableNum);
423 doGoto(0, -(numRows()-1));
424 repaintContents(
false );
429void KCharSelectTable::doGoto(
int dx,
int dy)
431 TQPoint oldPos = focusPos;
432 focusPos += TQPoint(dx, dy);
433 focusItem = charAt( focusPos.y(), focusPos.x() );
435 repaintCell( oldPos.y(), oldPos.x(),
true );
436 repaintCell( focusPos.y(), focusPos.x(),
true );
438 emit focusItemChanged( vChr );
439 emit focusItemChanged();
443TQChar KCharSelectTable::charAt(
int row,
int col)
const
445 return TQChar( vTableNum * 256 + numCols() * row + col );
454 : TQVBox( parent, name ), d(new KCharSelectPrivate)
457 TQHBox*
const bar =
new TQHBox(
this );
460 TQLabel*
const lFont =
new TQLabel( i18n(
"Font:" ), bar );
461 lFont->resize( lFont->sizeHint() );
462 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
463 lFont->setMaximumWidth( lFont->sizeHint().width() );
465 fontCombo =
new TQComboBox(
true, bar );
467 fontCombo->resize( fontCombo->sizeHint() );
469 connect( fontCombo, TQ_SIGNAL( activated(
const TQString & ) ),
this, TQ_SLOT( fontSelected(
const TQString & ) ) );
471 TQLabel*
const lTable =
new TQLabel( i18n(
"Table:" ), bar );
472 lTable->resize( lTable->sizeHint() );
473 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
474 lTable->setMaximumWidth( lTable->sizeHint().width() );
476 tableSpinBox =
new TQSpinBox( 0, 255, 1, bar );
477 tableSpinBox->resize( tableSpinBox->sizeHint() );
479 TQLabel*
const lUnicode =
new TQLabel( i18n(
"&Unicode code point:" ), bar );
480 lUnicode->resize( lUnicode->sizeHint() );
481 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
482 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
484 const TQRegExp rx(
"[a-fA-F0-9]{1,4}" );
485 TQValidator*
const validator =
new TQRegExpValidator( rx,
this );
488 d->unicodeLine->setValidator(validator);
489 lUnicode->setBuddy(d->unicodeLine);
490 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
491 slotUpdateUnicode(_chr);
493 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotUnicodeEntered() ) );
495 TQHBox*
const box =
new TQHBox(
this);
498 charTable =
new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
499 const TQSize sz( charTable->contentsWidth() + 4 ,
500 charTable->contentsHeight() + 4 );
501 charTable->resize( sz );
503 charTable->setMinimumSize( sz );
504 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
505 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
506 charTable->installEventFilter(
this );
508 d->scrollBar =
new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
510 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
513 connect( tableSpinBox, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
514 connect( d->scrollBar, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
515 connect( charTable, TQ_SIGNAL( tableNumChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
517 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( slotUpdateUnicode(
const TQChar & ) ) );
518 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( charHighlighted(
const TQChar & ) ) );
519 connect( charTable, TQ_SIGNAL( highlighted() ),
this, TQ_SLOT( charHighlighted() ) );
520 connect( charTable, TQ_SIGNAL( activated(
const TQChar & ) ),
this, TQ_SLOT( charActivated(
const TQChar & ) ) );
521 connect( charTable, TQ_SIGNAL( activated() ),
this, TQ_SLOT( charActivated() ) );
522 connect( charTable, TQ_SIGNAL( focusItemChanged(
const TQChar & ) ),
523 this, TQ_SLOT( charFocusItemChanged(
const TQChar & ) ) );
524 connect( charTable, TQ_SIGNAL( focusItemChanged() ),
this, TQ_SLOT( charFocusItemChanged() ) );
526 connect( charTable, TQ_SIGNAL(doubleClicked()),
this,TQ_SLOT(slotDoubleClicked()));
528 setFocusPolicy( TQWidget::StrongFocus );
529 setFocusProxy( charTable );
532KCharSelect::~KCharSelect()
540 return TQVBox::sizeHint();
546 const TQValueList<TQString>::Iterator it = fontList.find( _font );
547 if ( it != fontList.end() ) {
548 TQValueList<TQString>::Iterator it2 = fontList.begin();
550 for ( ; it != it2; ++it2, ++pos);
551 fontCombo->setCurrentItem( pos );
552 charTable->setFont( _font );
561 charTable->setChar( _chr );
562 slotUpdateUnicode( _chr );
568 tableSpinBox->setValue( _tableNum );
569 d->scrollBar->setValue( _tableNum );
570 charTable->setTableNum( _tableNum );
574void KCharSelect::fillFontCombo()
576 if ( !fontDataBase ) {
577 fontDataBase =
new TQFontDatabase();
578 tqAddPostRoutine( cleanupFontDatabase );
580 fontList=fontDataBase->families();
581 fontCombo->insertStringList( fontList );
585void KCharSelect::fontSelected(
const TQString &_font )
587 charTable->setFont( _font );
588 emit fontChanged( _font );
592void KCharSelect::tableChanged(
int _value )
598void KCharSelect::slotUnicodeEntered( )
600 const TQString s = d->unicodeLine->text();
605 const int uc = s.toInt(&ok, 16);
609 const int table = uc / 256;
610 charTable->setTableNum( table );
611 tableSpinBox->setValue(table);
612 d->scrollBar->setValue(table);
614 charTable->setChar( ch );
619void KCharSelect::slotUpdateUnicode(
const TQChar &c )
621 const int uc = c.unicode();
623 s.sprintf(
"%04X", uc);
624 d->unicodeLine->setText(s);
628bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
630 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
632 return TQApplication::sendEvent( d->scrollBar, e );
638void KCharSelectTable::virtual_hook(
int,
void*)
641void 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)