21 #include "kcharselect.h"
22 #include "kcharselect.moc"
28 #include <tqfontdatabase.h>
30 #include <tqkeycode.h>
32 #include <tqpainter.h>
36 #include <tqtooltip.h>
37 #include <tqvalidator.h>
39 #include <tdeapplication.h>
42 #include <klineedit.h>
45 class KCharSelect::KCharSelectPrivate
48 TQLineEdit *unicodeLine;
51 TQFontDatabase * KCharSelect::fontDataBase = 0;
53 void KCharSelect::cleanupFontDatabase()
64 KCharSelectTable::KCharSelectTable( TQWidget *parent,
const char *name,
const TQString &_font,
65 const TQChar &_chr,
int _tableNum )
66 : TQGridView( parent,
name ), vFont( _font ), vChr( _chr ),
67 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ), d(0)
69 setBackgroundColor( colorGroup().base() );
77 repaintContents(
false );
81 setFocusPolicy( TQWidget::StrongFocus );
82 setBackgroundMode( TQWidget::NoBackground );
86 void KCharSelectTable::setFont(
const TQString &_font )
89 repaintContents(
false );
95 void KCharSelectTable::setChar(
const TQChar &_chr )
98 repaintContents(
false );
102 void KCharSelectTable::setTableNum(
int _tableNum )
104 focusItem = TQChar( _tableNum * 256 );
106 vTableNum = _tableNum;
107 repaintContents(
false );
113 TQSize KCharSelectTable::sizeHint()
const
116 int h = cellHeight();
121 return TQSize( w, h );
125 void KCharSelectTable::resizeEvent( TQResizeEvent * e )
127 const int new_w = (e->size().width() - 2*(margin()+frameWidth())) / numCols();
128 const int new_h = (e->size().height() - 2*(margin()+frameWidth())) / numRows();
130 if( new_w != cellWidth())
131 setCellWidth( new_w );
132 if( new_h != cellHeight())
133 setCellHeight( new_h );
139 void KCharSelectTable::paintCell(
class TQPainter* p,
int row,
int col )
141 const int w = cellWidth();
142 const int h = cellHeight();
143 const int x2 = w - 1;
144 const int y2 = h - 1;
151 TQFont font = TQFont( vFont );
152 font.setPixelSize(
int(.7 * h) );
154 unsigned short c = vTableNum * 256;
155 c += row * numCols();
158 if ( c == vChr.unicode() ) {
159 p->setBrush( TQBrush( colorGroup().highlight() ) );
161 p->drawRect( 0, 0, w, h );
162 p->setPen( colorGroup().highlightedText() );
163 vPos = TQPoint( col, row );
165 TQFontMetrics fm = TQFontMetrics( font );
167 p->setBrush( TQBrush( colorGroup().base() ) );
169 p->setBrush( TQBrush( colorGroup().button() ) );
171 p->drawRect( 0, 0, w, h );
172 p->setPen( colorGroup().text() );
175 if ( c == focusItem.unicode() && hasFocus() ) {
176 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
178 focusPos = TQPoint( col, row );
183 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
185 p->setPen( colorGroup().text() );
186 p->drawLine( x2, 0, x2, y2 );
187 p->drawLine( 0, y2, x2, y2 );
190 p->drawLine( 0, 0, x2, 0 );
192 p->drawLine( 0, 0, 0, y2 );
196 void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
198 const int row = rowAt( e->y() );
199 const int col = columnAt( e->x() );
200 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
201 const TQPoint oldPos = vPos;
206 vChr = TQChar( vTableNum * 256 + numCols() * vPos.y() + vPos.x() );
208 const TQPoint oldFocus = focusPos;
213 repaintCell( oldFocus.y(), oldFocus.x(),
true );
214 repaintCell( oldPos.y(), oldPos.x(),
true );
215 repaintCell( vPos.y(), vPos.x(),
true );
217 emit highlighted( vChr );
220 emit focusItemChanged( focusItem );
221 emit focusItemChanged();
226 void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
228 switch ( e->key() ) {
248 emit activated(
' ' );
250 emit highlighted(
' ' );
253 case Key_Enter:
case Key_Return: {
254 const TQPoint oldPos = vPos;
259 repaintCell( oldPos.y(), oldPos.x(),
true );
260 repaintCell( vPos.y(), vPos.x(),
true );
262 emit activated( vChr );
264 emit highlighted( vChr );
271 void KCharSelectTable::gotoLeft()
273 if ( focusPos.x() > 0 ) {
274 const TQPoint oldPos = focusPos;
276 focusPos.setX( focusPos.x() - 1 );
278 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
280 repaintCell( oldPos.y(), oldPos.x(),
true );
281 repaintCell( focusPos.y(), focusPos.x(),
true );
283 emit focusItemChanged( vChr );
284 emit focusItemChanged();
289 void KCharSelectTable::gotoRight()
291 if ( focusPos.x() < numCols()-1 ) {
292 const TQPoint oldPos = focusPos;
294 focusPos.setX( focusPos.x() + 1 );
296 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
298 repaintCell( oldPos.y(), oldPos.x(),
true );
299 repaintCell( focusPos.y(), focusPos.x(),
true );
301 emit focusItemChanged( vChr );
302 emit focusItemChanged();
307 void KCharSelectTable::gotoUp()
309 if ( focusPos.y() > 0 ) {
310 const TQPoint oldPos = focusPos;
312 focusPos.setY( focusPos.y() - 1 );
314 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
316 repaintCell( oldPos.y(), oldPos.x(),
true );
317 repaintCell( focusPos.y(), focusPos.x(),
true );
319 emit focusItemChanged( vChr );
320 emit focusItemChanged();
325 void KCharSelectTable::gotoDown()
327 if ( focusPos.y() < numRows()-1 ) {
328 const TQPoint oldPos = focusPos;
330 focusPos.setY( focusPos.y() + 1 );
332 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
334 repaintCell( oldPos.y(), oldPos.x(),
true );
335 repaintCell( focusPos.y(), focusPos.x(),
true );
337 emit focusItemChanged( vChr );
338 emit focusItemChanged();
343 void KCharSelectTable::setToolTips()
345 const int rowCount = numRows();
346 const int colCount = numCols();
347 for(
int i=0 ; i< rowCount; ++i )
349 for(
int j=0; j< colCount; ++j )
351 const TQRect r( cellWidth()*j, cellHeight()*i, cellWidth(), cellHeight() );
352 TQToolTip::remove(
this,r);
353 const ushort uni = vTableNum * 256 + numCols()*i + j;
355 s.sprintf(
"%04X", uint( uni ) );
359 case 0x3c: character =
"<";
break;
360 case 0x3e: character =
">";
break;
361 case 0x26: character =
"&";
break;
362 case 0x27: character =
"'";
break;
363 case 0x22: character =
""";
break;
364 default: character = TQChar( uni );
break;
366 TQToolTip::add(
this, r, i18n(
"Character",
"<qt><font size=\"+4\" face=\"%1\">%2</font><br>Unicode code point: U+%3<br>(In decimal: %4)<br>(Character: %5)</qt>" ).arg( vFont ).arg( character ).arg( s ).arg( uni ).arg( character ) );
377 : TQVBox( parent, name ), d(new KCharSelectPrivate)
380 TQHBox*
const bar =
new TQHBox(
this );
383 TQLabel*
const lFont =
new TQLabel( i18n(
"Font:" ), bar );
384 lFont->resize( lFont->sizeHint() );
385 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
386 lFont->setMaximumWidth( lFont->sizeHint().width() );
388 fontCombo =
new TQComboBox(
true, bar );
390 fontCombo->resize( fontCombo->sizeHint() );
392 connect( fontCombo, TQ_SIGNAL( activated(
const TQString & ) ),
this, TQ_SLOT( fontSelected(
const TQString & ) ) );
394 TQLabel*
const lTable =
new TQLabel( i18n(
"Table:" ), bar );
395 lTable->resize( lTable->sizeHint() );
396 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
397 lTable->setMaximumWidth( lTable->sizeHint().width() );
399 tableSpinBox =
new TQSpinBox( 0, 255, 1, bar );
400 tableSpinBox->resize( tableSpinBox->sizeHint() );
402 connect( tableSpinBox, TQ_SIGNAL( valueChanged(
int ) ),
this, TQ_SLOT( tableChanged(
int ) ) );
404 TQLabel*
const lUnicode =
new TQLabel( i18n(
"&Unicode code point:" ), bar );
405 lUnicode->resize( lUnicode->sizeHint() );
406 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
407 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
409 const TQRegExp rx(
"[a-fA-F0-9]{1,4}" );
410 TQValidator*
const validator =
new TQRegExpValidator( rx,
this );
413 d->unicodeLine->setValidator(validator);
414 lUnicode->setBuddy(d->unicodeLine);
415 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
416 slotUpdateUnicode(_chr);
418 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotUnicodeEntered() ) );
420 charTable =
new KCharSelectTable(
this, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
421 const TQSize sz( charTable->contentsWidth() + 4 ,
422 charTable->contentsHeight() + 4 );
423 charTable->resize( sz );
425 charTable->setMinimumSize( sz );
426 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
427 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
429 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
432 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( slotUpdateUnicode(
const TQChar & ) ) );
433 connect( charTable, TQ_SIGNAL( highlighted(
const TQChar & ) ),
this, TQ_SLOT( charHighlighted(
const TQChar & ) ) );
434 connect( charTable, TQ_SIGNAL( highlighted() ),
this, TQ_SLOT( charHighlighted() ) );
435 connect( charTable, TQ_SIGNAL( activated(
const TQChar & ) ),
this, TQ_SLOT( charActivated(
const TQChar & ) ) );
436 connect( charTable, TQ_SIGNAL( activated() ),
this, TQ_SLOT( charActivated() ) );
437 connect( charTable, TQ_SIGNAL( focusItemChanged(
const TQChar & ) ),
438 this, TQ_SLOT( charFocusItemChanged(
const TQChar & ) ) );
439 connect( charTable, TQ_SIGNAL( focusItemChanged() ),
this, TQ_SLOT( charFocusItemChanged() ) );
440 connect( charTable, TQ_SIGNAL( tableUp() ),
this, TQ_SLOT( charTableUp() ) );
441 connect( charTable, TQ_SIGNAL( tableDown() ),
this, TQ_SLOT( charTableDown() ) );
443 connect( charTable, TQ_SIGNAL(doubleClicked()),
this,TQ_SLOT(slotDoubleClicked()));
445 setFocusPolicy( TQWidget::StrongFocus );
446 setFocusProxy( charTable );
449 KCharSelect::~KCharSelect()
457 return TQVBox::sizeHint();
463 const TQValueList<TQString>::Iterator it = fontList.find( _font );
464 if ( it != fontList.end() ) {
465 TQValueList<TQString>::Iterator it2 = fontList.begin();
467 for ( ; it != it2; ++it2, ++pos);
468 fontCombo->setCurrentItem( pos );
469 charTable->setFont( _font );
478 charTable->setChar( _chr );
479 slotUpdateUnicode( _chr );
485 tableSpinBox->setValue( _tableNum );
486 charTable->setTableNum( _tableNum );
490 void KCharSelect::fillFontCombo()
492 if ( !fontDataBase ) {
493 fontDataBase =
new TQFontDatabase();
494 tqAddPostRoutine( cleanupFontDatabase );
496 fontList=fontDataBase->families();
497 fontCombo->insertStringList( fontList );
501 void KCharSelect::fontSelected(
const TQString &_font )
503 charTable->setFont( _font );
504 emit fontChanged( _font );
508 void KCharSelect::tableChanged(
int _value )
510 charTable->setTableNum( _value );
514 void KCharSelect::slotUnicodeEntered( )
516 const TQString s = d->unicodeLine->text();
521 const int uc = s.toInt(&ok, 16);
525 const int table = uc / 256;
526 charTable->setTableNum( table );
527 tableSpinBox->setValue(table);
529 charTable->setChar( ch );
533 void KCharSelect::slotUpdateUnicode(
const TQChar &c )
535 const int uc = c.unicode();
537 s.sprintf(
"%04X", uc);
538 d->unicodeLine->setText(s);
541 void KCharSelectTable::virtual_hook(
int,
void*)
544 void 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.
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
TQString name(StdAccel id)