00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "kcharselect.h"
00022
#include "kcharselect.moc"
00023
00024
#include <qbrush.h>
00025
#include <qcolor.h>
00026
#include <qevent.h>
00027
#include <qfont.h>
00028
#include <qfontdatabase.h>
00029
#include <qhbox.h>
00030
#include <qkeycode.h>
00031
#include <qlabel.h>
00032
#include <qlineedit.h>
00033
#include <qpainter.h>
00034
#include <qpen.h>
00035
#include <qregexp.h>
00036
#include <qstyle.h>
00037
#include <qtooltip.h>
00038
#include <qvalidator.h>
00039
00040
#include <kapplication.h>
00041
#include <kdebug.h>
00042
#include <kdialog.h>
00043
#include <klocale.h>
00044
00045
class KCharSelect::KCharSelectPrivate
00046 {
00047
public:
00048
QLineEdit *unicodeLine;
00049 };
00050
00051
QFontDatabase * KCharSelect::fontDataBase = 0;
00052
00053
void KCharSelect::cleanupFontDatabase()
00054 {
00055
delete fontDataBase;
00056 fontDataBase = 0;
00057 }
00058
00059
00060
00061
00062
00063
00064 KCharSelectTable::KCharSelectTable(
QWidget *parent,
const char *name,
const QString &_font,
00065
const QChar &_chr,
int _tableNum )
00066 :
QGridView( parent,
name ), vFont( _font ), vChr( _chr ),
00067 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 )
00068 {
00069 setBackgroundColor( colorGroup().base() );
00070
00071 setCellWidth( 20 );
00072 setCellHeight( 25 );
00073
00074 setNumCols( 32 );
00075 setNumRows( 8 );
00076
00077 repaintContents(
false );
00078
00079 setToolTips();
00080
00081 setFocusPolicy( QWidget::StrongFocus );
00082 setBackgroundMode( QWidget::NoBackground );
00083 }
00084
00085
00086
void KCharSelectTable::setFont(
const QString &_font )
00087 {
00088 vFont = _font;
00089 repaintContents(
false );
00090
00091 setToolTips();
00092 }
00093
00094
00095
void KCharSelectTable::setChar(
const QChar &_chr )
00096 {
00097 vChr = _chr;
00098 repaintContents(
false );
00099 }
00100
00101
00102
void KCharSelectTable::setTableNum(
int _tableNum )
00103 {
00104 focusItem =
QChar( _tableNum * 256 );
00105
00106 vTableNum = _tableNum;
00107 repaintContents(
false );
00108
00109 setToolTips();
00110 }
00111
00112
00113
QSize KCharSelectTable::sizeHint()
const
00114
{
00115
int w = cellWidth();
00116
int h = cellHeight();
00117
00118 w *= numCols();
00119 h *= numRows();
00120
00121
return QSize( w, h );
00122 }
00123
00124
00125
void KCharSelectTable::resizeEvent(
QResizeEvent * e )
00126 {
00127
int new_w = (e->size().width() - 2*(margin()+frameWidth())) / numCols();
00128
int new_h = (e->size().height() - 2*(margin()+frameWidth())) / numRows();
00129
00130
if( new_w != cellWidth())
00131 setCellWidth( new_w );
00132
if( new_h != cellHeight())
00133 setCellHeight( new_h );
00134
00135 setToolTips();
00136 }
00137
00138
00139
void KCharSelectTable::paintCell(
class QPainter* p,
int row,
int col )
00140 {
00141
int w = cellWidth();
00142
int h = cellHeight();
00143
int x2 = w - 1;
00144
int y2 = h - 1;
00145
00146
00147
00148
00149
00150
00151
QFont font =
QFont( vFont );
00152 font.setPixelSize(
int(.7 * h) );
00153
00154
unsigned short c = vTableNum * 256;
00155 c += row * numCols();
00156 c += col;
00157
00158
if ( c == vChr.unicode() ) {
00159 p->setBrush(
QBrush( colorGroup().highlight() ) );
00160 p->setPen( NoPen );
00161 p->drawRect( 0, 0, w, h );
00162 p->setPen( colorGroup().highlightedText() );
00163 vPos =
QPoint( col, row );
00164 }
else {
00165
QFontMetrics fm =
QFontMetrics( font );
00166
if( fm.inFont( c ) )
00167 p->setBrush(
QBrush( colorGroup().base() ) );
00168
else
00169 p->setBrush(
QBrush( colorGroup().button() ) );
00170 p->setPen( NoPen );
00171 p->drawRect( 0, 0, w, h );
00172 p->setPen( colorGroup().text() );
00173 }
00174
00175
if ( c == focusItem.unicode() && hasFocus() ) {
00176 style().drawPrimitive( QStyle::PE_FocusRect, p,
QRect( 2, 2, w - 4, h - 4 ),
00177 colorGroup() );
00178 focusPos =
QPoint( col, row );
00179 }
00180
00181 p->setFont( font );
00182
00183 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter,
QString(
QChar( c ) ) );
00184
00185 p->setPen( colorGroup().text() );
00186 p->drawLine( x2, 0, x2, y2 );
00187 p->drawLine( 0, y2, x2, y2 );
00188
00189
if ( row == 0 )
00190 p->drawLine( 0, 0, x2, 0 );
00191
if ( col == 0 )
00192 p->drawLine( 0, 0, 0, y2 );
00193 }
00194
00195
00196
void KCharSelectTable::mouseMoveEvent(
QMouseEvent *e )
00197 {
00198
int row = rowAt( e->y() );
00199
int col = columnAt( e->x() );
00200
if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
00201
QPoint oldPos = vPos;
00202
00203 vPos.setX( col );
00204 vPos.setY( row );
00205
00206 vChr =
QChar( vTableNum * 256 + numCols() * vPos.y() + vPos.x() );
00207
00208
QPoint oldFocus = focusPos;
00209
00210 focusPos = vPos;
00211 focusItem = vChr;
00212
00213 repaintCell( oldFocus.y(), oldFocus.x(),
true );
00214 repaintCell( oldPos.y(), oldPos.x(),
true );
00215 repaintCell( vPos.y(), vPos.x(),
true );
00216
00217 emit highlighted( vChr );
00218 emit highlighted();
00219
00220 emit focusItemChanged( focusItem );
00221 emit focusItemChanged();
00222 }
00223 }
00224
00225
00226
void KCharSelectTable::keyPressEvent(
QKeyEvent *e )
00227 {
00228
switch ( e->key() ) {
00229
case Key_Left:
00230 gotoLeft();
00231
break;
00232
case Key_Right:
00233 gotoRight();
00234
break;
00235
case Key_Up:
00236 gotoUp();
00237
break;
00238
case Key_Down:
00239 gotoDown();
00240
break;
00241
case Key_Next:
00242 emit tableDown();
00243
break;
00244
case Key_Prior:
00245 emit tableUp();
00246
break;
00247
case Key_Space:
00248 emit activated(
' ' );
00249 emit activated();
00250 emit highlighted(
' ' );
00251 emit highlighted();
00252
break;
00253
case Key_Enter:
case Key_Return: {
00254
QPoint oldPos = vPos;
00255
00256 vPos = focusPos;
00257 vChr = focusItem;
00258
00259 repaintCell( oldPos.y(), oldPos.x(),
true );
00260 repaintCell( vPos.y(), vPos.x(),
true );
00261
00262 emit activated( vChr );
00263 emit activated();
00264 emit highlighted( vChr );
00265 emit highlighted();
00266 }
break;
00267 }
00268 }
00269
00270
00271
void KCharSelectTable::gotoLeft()
00272 {
00273
if ( focusPos.x() > 0 ) {
00274
QPoint oldPos = focusPos;
00275
00276 focusPos.setX( focusPos.x() - 1 );
00277
00278 focusItem =
QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00279
00280 repaintCell( oldPos.y(), oldPos.x(),
true );
00281 repaintCell( focusPos.y(), focusPos.x(),
true );
00282
00283 emit focusItemChanged( vChr );
00284 emit focusItemChanged();
00285 }
00286 }
00287
00288
00289
void KCharSelectTable::gotoRight()
00290 {
00291
if ( focusPos.x() < numCols()-1 ) {
00292
QPoint oldPos = focusPos;
00293
00294 focusPos.setX( focusPos.x() + 1 );
00295
00296 focusItem =
QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00297
00298 repaintCell( oldPos.y(), oldPos.x(),
true );
00299 repaintCell( focusPos.y(), focusPos.x(),
true );
00300
00301 emit focusItemChanged( vChr );
00302 emit focusItemChanged();
00303 }
00304 }
00305
00306
00307
void KCharSelectTable::gotoUp()
00308 {
00309
if ( focusPos.y() > 0 ) {
00310
QPoint oldPos = focusPos;
00311
00312 focusPos.setY( focusPos.y() - 1 );
00313
00314 focusItem =
QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00315
00316 repaintCell( oldPos.y(), oldPos.x(),
true );
00317 repaintCell( focusPos.y(), focusPos.x(),
true );
00318
00319 emit focusItemChanged( vChr );
00320 emit focusItemChanged();
00321 }
00322 }
00323
00324
00325
void KCharSelectTable::gotoDown()
00326 {
00327
if ( focusPos.y() < numRows()-1 ) {
00328
QPoint oldPos = focusPos;
00329
00330 focusPos.setY( focusPos.y() + 1 );
00331
00332 focusItem =
QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00333
00334 repaintCell( oldPos.y(), oldPos.x(),
true );
00335 repaintCell( focusPos.y(), focusPos.x(),
true );
00336
00337 emit focusItemChanged( vChr );
00338 emit focusItemChanged();
00339 }
00340 }
00341
00342
00343
void KCharSelectTable::setToolTips()
00344 {
00345
for(
int i=0 ; i< numRows(); i++ )
00346 {
00347
for(
int j=0; j< numCols(); j++ )
00348 {
00349
QRect r( cellWidth()*j, cellHeight()*i, cellWidth(), cellHeight() );
00350 QToolTip::remove(
this,r);
00351
QString s;
00352 s.sprintf(
"%04X", vTableNum * 256 + numCols()*i + j);
00353 QToolTip::add(
this, r, i18n(
"Character code",
"UTF code: %1").arg(s));
00354 }
00355 }
00356 }
00357
00358
00359
00360
00361
00362
00363 KCharSelect::KCharSelect(
QWidget *parent,
const char *name,
const QString &_font,
const QChar &_chr,
int _tableNum )
00364 :
QVBox( parent, name )
00365 {
00366 d =
new KCharSelectPrivate;
00367 setSpacing( KDialog::spacingHint() );
00368
QHBox *bar =
new QHBox(
this );
00369 bar->setSpacing( KDialog::spacingHint() );
00370
00371
QLabel *lFont =
new QLabel( i18n(
"Font:" ), bar );
00372 lFont->resize( lFont->sizeHint() );
00373 lFont->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00374 lFont->setMaximumWidth( lFont->sizeHint().width() );
00375
00376 fontCombo =
new QComboBox(
true, bar );
00377 fillFontCombo();
00378 fontCombo->resize( fontCombo->sizeHint() );
00379
00380 connect( fontCombo, SIGNAL( activated(
const QString & ) ),
this, SLOT( fontSelected(
const QString & ) ) );
00381
00382 QLabel *lTable =
new QLabel( i18n(
"Table:" ), bar );
00383 lTable->resize( lTable->sizeHint() );
00384 lTable->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00385 lTable->setMaximumWidth( lTable->sizeHint().width() );
00386
00387 tableSpinBox =
new QSpinBox( 0, 255, 1, bar );
00388 tableSpinBox->resize( tableSpinBox->sizeHint() );
00389
00390 connect( tableSpinBox, SIGNAL( valueChanged(
int ) ),
this, SLOT( tableChanged(
int ) ) );
00391
00392 QLabel *lUnicode =
new QLabel( i18n(
"&UTF code:" ), bar );
00393 lUnicode->resize( lUnicode->sizeHint() );
00394 lUnicode->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00395 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
00396
00397
QRegExp rx(
"[a-fA-F0-9]{1,4}" );
00398
QValidator* validator =
new QRegExpValidator( rx,
this );
00399
00400 d->unicodeLine =
new QLineEdit( bar );
00401 d->unicodeLine->setValidator(validator);
00402 lUnicode->setBuddy(d->unicodeLine);
00403 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
00404 slotUpdateUnicode(_chr);
00405
00406 connect( d->unicodeLine, SIGNAL( returnPressed() ),
this, SLOT( slotUnicodeEntered() ) );
00407
00408 charTable =
new KCharSelectTable(
this, name, _font.isEmpty() ? QVBox::font().family() : _font, _chr, _tableNum );
00409
QSize sz( charTable->contentsWidth() + 4 ,
00410 charTable->contentsHeight() + 4 );
00411 charTable->resize( sz );
00412
00413 charTable->setMinimumSize( sz );
00414 charTable->setHScrollBarMode( QScrollView::AlwaysOff );
00415 charTable->setVScrollBarMode( QScrollView::AlwaysOff );
00416
00417
setFont( _font.isEmpty() ? QVBox::font().family() : _font );
00418
setTableNum( _tableNum );
00419
00420 connect( charTable, SIGNAL( highlighted(
const QChar & ) ),
this, SLOT( slotUpdateUnicode(
const QChar & ) ) );
00421 connect( charTable, SIGNAL( highlighted(
const QChar & ) ),
this, SLOT( charHighlighted(
const QChar & ) ) );
00422 connect( charTable, SIGNAL( highlighted() ),
this, SLOT( charHighlighted() ) );
00423 connect( charTable, SIGNAL( activated(
const QChar & ) ),
this, SLOT( charActivated(
const QChar & ) ) );
00424 connect( charTable, SIGNAL( activated() ),
this, SLOT( charActivated() ) );
00425 connect( charTable, SIGNAL( focusItemChanged(
const QChar & ) ),
00426
this, SLOT( charFocusItemChanged(
const QChar & ) ) );
00427 connect( charTable, SIGNAL( focusItemChanged() ),
this, SLOT( charFocusItemChanged() ) );
00428 connect( charTable, SIGNAL( tableUp() ),
this, SLOT( charTableUp() ) );
00429 connect( charTable, SIGNAL( tableDown() ),
this, SLOT( charTableDown() ) );
00430
00431 connect( charTable, SIGNAL(doubleClicked()),
this,SLOT(slotDoubleClicked()));
00432
00433 setFocusPolicy( QWidget::StrongFocus );
00434 setFocusProxy( charTable );
00435 }
00436
00437
00438 QSize KCharSelect::sizeHint()
const
00439
{
00440
return QVBox::sizeHint();
00441 }
00442
00443
00444 void KCharSelect::setFont(
const QString &_font )
00445 {
00446
QValueList<QString>::Iterator it = fontList.find( _font );
00447
if ( it != fontList.end() ) {
00448
QValueList<QString>::Iterator it2 = fontList.begin();
00449
int pos = 0;
00450
for ( ; it != it2; ++it2, ++pos);
00451 fontCombo->setCurrentItem( pos );
00452 charTable->
setFont( _font );
00453 }
00454
else
00455
kdWarning() <<
"Can't find Font: " << _font <<
endl;
00456 }
00457
00458
00459 void KCharSelect::setChar(
const QChar &_chr )
00460 {
00461 charTable->
setChar( _chr );
00462 slotUpdateUnicode( _chr );
00463 }
00464
00465
00466 void KCharSelect::setTableNum(
int _tableNum )
00467 {
00468 tableSpinBox->setValue( _tableNum );
00469 charTable->
setTableNum( _tableNum );
00470 }
00471
00472
00473
void KCharSelect::fillFontCombo()
00474 {
00475
if ( !fontDataBase ) {
00476 fontDataBase =
new QFontDatabase();
00477 qAddPostRoutine( cleanupFontDatabase );
00478 }
00479 fontList=fontDataBase->families();
00480 fontCombo->insertStringList( fontList );
00481 }
00482
00483
00484
void KCharSelect::fontSelected(
const QString &_font )
00485 {
00486 charTable->
setFont( _font );
00487 emit fontChanged( _font );
00488 }
00489
00490
00491
void KCharSelect::tableChanged(
int _value )
00492 {
00493 charTable->
setTableNum( _value );
00494 }
00495
00496
00497
void KCharSelect::slotUnicodeEntered( )
00498 {
00499
QString s = d->unicodeLine->text();
00500
if (s.isEmpty())
00501
return;
00502
00503
bool ok;
00504
int uc = s.toInt(&ok, 16);
00505
if (!ok)
00506
return;
00507
00508
int table = uc / 256;
00509 charTable->
setTableNum( table );
00510 tableSpinBox->setValue(table);
00511
QChar ch(uc);
00512 charTable->
setChar( ch );
00513 charActivated( ch );
00514 }
00515
00516
void KCharSelect::slotUpdateUnicode(
const QChar &c )
00517 {
00518
int uc = c.unicode();
00519
QString s;
00520 s.sprintf(
"%04X", uc);
00521 d->unicodeLine->setText(s);
00522 }
00523
00524
void KCharSelectTable::virtual_hook(
int,
void*)
00525 { }
00526
00527
void KCharSelect::virtual_hook(
int,
void* )
00528 { }
00529