kdeui Library API Documentation

kcharselect.cpp

00001 /* This file is part of the KDE libraries 00002 00003 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include "kcharselect.h" 00022 #include "kcharselect.moc" 00023 00024 #include <qevent.h> 00025 #include <qfont.h> 00026 #include <qpen.h> 00027 #include <qbrush.h> 00028 #include <qpainter.h> 00029 #include <qcolor.h> 00030 #include <qlabel.h> 00031 #include <qhbox.h> 00032 #include <qkeycode.h> 00033 #include <qfontdatabase.h> 00034 #include <qstyle.h> 00035 #include <qtooltip.h> 00036 00037 #include <klocale.h> 00038 #include <kdebug.h> 00039 #include <kdialog.h> 00040 #include <kapplication.h> 00041 00042 QFontDatabase * KCharSelect::fontDataBase = 0; 00043 00044 void KCharSelect::cleanupFontDatabase() 00045 { 00046 delete fontDataBase; 00047 fontDataBase = 0; 00048 } 00049 00050 /******************************************************************/ 00051 /* Class: KCharSelectTable */ 00052 /******************************************************************/ 00053 00054 //================================================================== 00055 KCharSelectTable::KCharSelectTable( QWidget *parent, const char *name, const QString &_font, 00056 const QChar &_chr, int _tableNum ) 00057 : QGridView( parent, name ), vFont( _font ), vChr( _chr ), 00058 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ) 00059 { 00060 setBackgroundColor( colorGroup().base() ); 00061 00062 setCellWidth( 20 ); 00063 setCellHeight( 25 ); 00064 00065 setNumCols( 32 ); 00066 setNumRows( 8 ); 00067 00068 repaintContents( false ); 00069 00070 setToolTips(); 00071 00072 setFocusPolicy( QWidget::StrongFocus ); 00073 setBackgroundMode( QWidget::NoBackground ); 00074 } 00075 00076 //================================================================== 00077 void KCharSelectTable::setFont( const QString &_font ) 00078 { 00079 vFont = _font; 00080 repaintContents( false ); 00081 00082 setToolTips(); 00083 } 00084 00085 //================================================================== 00086 void KCharSelectTable::setChar( const QChar &_chr ) 00087 { 00088 vChr = _chr; 00089 repaintContents( false ); 00090 } 00091 00092 //================================================================== 00093 void KCharSelectTable::setTableNum( int _tableNum ) 00094 { 00095 focusItem = QChar( _tableNum * 256 ); 00096 00097 vTableNum = _tableNum; 00098 repaintContents( false ); 00099 00100 setToolTips(); 00101 } 00102 00103 //================================================================== 00104 QSize KCharSelectTable::sizeHint() const 00105 { 00106 int w = cellWidth(); 00107 int h = cellHeight(); 00108 00109 w *= numCols(); 00110 h *= numRows(); 00111 00112 return QSize( w, h ); 00113 } 00114 00115 //================================================================== 00116 void KCharSelectTable::resizeEvent( QResizeEvent * e ) 00117 { 00118 int new_w = (e->size().width() - 2*(margin()+frameWidth())) / numCols(); 00119 int new_h = (e->size().height() - 2*(margin()+frameWidth())) / numRows(); 00120 00121 if( new_w != cellWidth()) 00122 setCellWidth( new_w ); 00123 if( new_h != cellHeight()) 00124 setCellHeight( new_h ); 00125 00126 setToolTips(); 00127 } 00128 00129 //================================================================== 00130 void KCharSelectTable::paintCell( class QPainter* p, int row, int col ) 00131 { 00132 int w = cellWidth(); 00133 int h = cellHeight(); 00134 int x2 = w - 1; 00135 int y2 = h - 1; 00136 00137 //if( row == 0 && col == 0 ) { 00138 // printf("Repaint %d\n", temp++); 00139 // fflush( stdout ); 00140 // } 00141 00142 QFont font = QFont( vFont ); 00143 font.setPixelSize( int(.7 * h) ); 00144 00145 unsigned short c = vTableNum * 256; 00146 c += row * numCols(); 00147 c += col; 00148 00149 if ( c == vChr.unicode() ) { 00150 p->setBrush( QBrush( colorGroup().highlight() ) ); 00151 p->setPen( NoPen ); 00152 p->drawRect( 0, 0, w, h ); 00153 p->setPen( colorGroup().highlightedText() ); 00154 vPos = QPoint( col, row ); 00155 } else { 00156 QFontMetrics fm = QFontMetrics( font ); 00157 if( fm.inFont( c ) ) 00158 p->setBrush( QBrush( colorGroup().base() ) ); 00159 else 00160 p->setBrush( QBrush( colorGroup().button() ) ); 00161 p->setPen( NoPen ); 00162 p->drawRect( 0, 0, w, h ); 00163 p->setPen( colorGroup().text() ); 00164 } 00165 00166 if ( c == focusItem.unicode() && hasFocus() ) { 00167 style().drawPrimitive( QStyle::PE_FocusRect, p, QRect( 2, 2, w - 4, h - 4 ), 00168 colorGroup() ); 00169 focusPos = QPoint( col, row ); 00170 } 00171 00172 p->setFont( font ); 00173 00174 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, QString( QChar( c ) ) ); 00175 00176 p->setPen( colorGroup().text() ); 00177 p->drawLine( x2, 0, x2, y2 ); 00178 p->drawLine( 0, y2, x2, y2 ); 00179 00180 if ( row == 0 ) 00181 p->drawLine( 0, 0, x2, 0 ); 00182 if ( col == 0 ) 00183 p->drawLine( 0, 0, 0, y2 ); 00184 } 00185 00186 //================================================================== 00187 void KCharSelectTable::mouseMoveEvent( QMouseEvent *e ) 00188 { 00189 int row = rowAt( e->y() ); 00190 int col = columnAt( e->x() ); 00191 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) { 00192 QPoint oldPos = vPos; 00193 00194 vPos.setX( col ); 00195 vPos.setY( row ); 00196 00197 vChr = QChar( vTableNum * 256 + numCols() * vPos.y() + vPos.x() ); 00198 00199 QPoint oldFocus = focusPos; 00200 00201 focusPos = vPos; 00202 focusItem = vChr; 00203 00204 repaintCell( oldFocus.y(), oldFocus.x(), true ); 00205 repaintCell( oldPos.y(), oldPos.x(), true ); 00206 repaintCell( vPos.y(), vPos.x(), true ); 00207 00208 emit highlighted( vChr ); 00209 emit highlighted(); 00210 00211 emit focusItemChanged( focusItem ); 00212 emit focusItemChanged(); 00213 } 00214 } 00215 00216 //================================================================== 00217 void KCharSelectTable::keyPressEvent( QKeyEvent *e ) 00218 { 00219 switch ( e->key() ) { 00220 case Key_Left: 00221 gotoLeft(); 00222 break; 00223 case Key_Right: 00224 gotoRight(); 00225 break; 00226 case Key_Up: 00227 gotoUp(); 00228 break; 00229 case Key_Down: 00230 gotoDown(); 00231 break; 00232 case Key_Next: 00233 emit tableDown(); 00234 break; 00235 case Key_Prior: 00236 emit tableUp(); 00237 break; 00238 case Key_Space: 00239 emit activated( ' ' ); 00240 emit activated(); 00241 emit highlighted( ' ' ); 00242 emit highlighted(); 00243 break; 00244 case Key_Enter: case Key_Return: { 00245 QPoint oldPos = vPos; 00246 00247 vPos = focusPos; 00248 vChr = focusItem; 00249 00250 repaintCell( oldPos.y(), oldPos.x(), true ); 00251 repaintCell( vPos.y(), vPos.x(), true ); 00252 00253 emit activated( vChr ); 00254 emit activated(); 00255 emit highlighted( vChr ); 00256 emit highlighted(); 00257 } break; 00258 } 00259 } 00260 00261 //================================================================== 00262 void KCharSelectTable::gotoLeft() 00263 { 00264 if ( focusPos.x() > 0 ) { 00265 QPoint oldPos = focusPos; 00266 00267 focusPos.setX( focusPos.x() - 1 ); 00268 00269 focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() ); 00270 00271 repaintCell( oldPos.y(), oldPos.x(), true ); 00272 repaintCell( focusPos.y(), focusPos.x(), true ); 00273 00274 emit focusItemChanged( vChr ); 00275 emit focusItemChanged(); 00276 } 00277 } 00278 00279 //================================================================== 00280 void KCharSelectTable::gotoRight() 00281 { 00282 if ( focusPos.x() < numCols()-1 ) { 00283 QPoint oldPos = focusPos; 00284 00285 focusPos.setX( focusPos.x() + 1 ); 00286 00287 focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() ); 00288 00289 repaintCell( oldPos.y(), oldPos.x(), true ); 00290 repaintCell( focusPos.y(), focusPos.x(), true ); 00291 00292 emit focusItemChanged( vChr ); 00293 emit focusItemChanged(); 00294 } 00295 } 00296 00297 //================================================================== 00298 void KCharSelectTable::gotoUp() 00299 { 00300 if ( focusPos.y() > 0 ) { 00301 QPoint oldPos = focusPos; 00302 00303 focusPos.setY( focusPos.y() - 1 ); 00304 00305 focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() ); 00306 00307 repaintCell( oldPos.y(), oldPos.x(), true ); 00308 repaintCell( focusPos.y(), focusPos.x(), true ); 00309 00310 emit focusItemChanged( vChr ); 00311 emit focusItemChanged(); 00312 } 00313 } 00314 00315 //================================================================== 00316 void KCharSelectTable::gotoDown() 00317 { 00318 if ( focusPos.y() < numRows()-1 ) { 00319 QPoint oldPos = focusPos; 00320 00321 focusPos.setY( focusPos.y() + 1 ); 00322 00323 focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() ); 00324 00325 repaintCell( oldPos.y(), oldPos.x(), true ); 00326 repaintCell( focusPos.y(), focusPos.x(), true ); 00327 00328 emit focusItemChanged( vChr ); 00329 emit focusItemChanged(); 00330 } 00331 } 00332 00333 //================================================================== 00334 void KCharSelectTable::setToolTips() 00335 { 00336 for( int i=0 ; i< numRows(); i++ ) 00337 { 00338 for( int j=0; j< numCols(); j++ ) 00339 { 00340 QRect r( cellWidth()*j, cellHeight()*i, cellWidth(), cellHeight() ); 00341 QToolTip::remove(this,r); 00342 QToolTip::add(this, r, i18n("Character code","UTF code: %1").arg(QString::number(vTableNum * 256 + numCols()*i + j))); 00343 } 00344 } 00345 } 00346 00347 /******************************************************************/ 00348 /* Class: KCharSelect */ 00349 /******************************************************************/ 00350 00351 //================================================================== 00352 KCharSelect::KCharSelect( QWidget *parent, const char *name, const QString &_font, const QChar &_chr, int _tableNum ) 00353 : QVBox( parent, name ) 00354 { 00355 setSpacing( KDialog::spacingHint() ); 00356 QHBox *bar = new QHBox( this ); 00357 bar->setSpacing( KDialog::spacingHint() ); 00358 00359 QLabel *lFont = new QLabel( i18n( "Font:" ), bar ); 00360 lFont->resize( lFont->sizeHint() ); 00361 lFont->setAlignment( Qt::AlignRight | Qt::AlignVCenter ); 00362 lFont->setMaximumWidth( lFont->sizeHint().width() ); 00363 00364 fontCombo = new QComboBox( true, bar ); 00365 fillFontCombo(); 00366 fontCombo->resize( fontCombo->sizeHint() ); 00367 00368 connect( fontCombo, SIGNAL( activated( const QString & ) ), this, SLOT( fontSelected( const QString & ) ) ); 00369 00370 QLabel *lTable = new QLabel( i18n( "Table:" ), bar ); 00371 lTable->resize( lTable->sizeHint() ); 00372 lTable->setAlignment( Qt::AlignRight | Qt::AlignVCenter ); 00373 lTable->setMaximumWidth( lTable->sizeHint().width() ); 00374 00375 tableSpinBox = new QSpinBox( 0, 255, 1, bar ); 00376 tableSpinBox->resize( tableSpinBox->sizeHint() ); 00377 00378 connect( tableSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( tableChanged( int ) ) ); 00379 00380 charTable = new KCharSelectTable( this, name, _font.isEmpty() ? QVBox::font().family() : _font, _chr, _tableNum ); 00381 QSize sz( charTable->contentsWidth() + 4 , 00382 charTable->contentsHeight() + 4 ); 00383 charTable->resize( sz ); 00384 //charTable->setMaximumSize( sz ); 00385 charTable->setMinimumSize( sz ); 00386 charTable->setHScrollBarMode( QScrollView::AlwaysOff ); 00387 charTable->setVScrollBarMode( QScrollView::AlwaysOff ); 00388 00389 setFont( _font.isEmpty() ? QVBox::font().family() : _font ); 00390 setTableNum( _tableNum ); 00391 00392 connect( charTable, SIGNAL( highlighted( const QChar & ) ), this, SLOT( charHighlighted( const QChar & ) ) ); 00393 connect( charTable, SIGNAL( highlighted() ), this, SLOT( charHighlighted() ) ); 00394 connect( charTable, SIGNAL( activated( const QChar & ) ), this, SLOT( charActivated( const QChar & ) ) ); 00395 connect( charTable, SIGNAL( activated() ), this, SLOT( charActivated() ) ); 00396 connect( charTable, SIGNAL( focusItemChanged( const QChar & ) ), 00397 this, SLOT( charFocusItemChanged( const QChar & ) ) ); 00398 connect( charTable, SIGNAL( focusItemChanged() ), this, SLOT( charFocusItemChanged() ) ); 00399 connect( charTable, SIGNAL( tableUp() ), this, SLOT( charTableUp() ) ); 00400 connect( charTable, SIGNAL( tableDown() ), this, SLOT( charTableDown() ) ); 00401 00402 connect( charTable, SIGNAL(doubleClicked()),this,SLOT(slotDoubleClicked())); 00403 00404 setFocusPolicy( QWidget::StrongFocus ); 00405 setFocusProxy( charTable ); 00406 } 00407 00408 //================================================================== 00409 QSize KCharSelect::sizeHint() const 00410 { 00411 return QVBox::sizeHint(); 00412 } 00413 00414 //================================================================== 00415 void KCharSelect::setFont( const QString &_font ) 00416 { 00417 QValueList<QString>::Iterator it = fontList.find( _font ); 00418 if ( it != fontList.end() ) { 00419 QValueList<QString>::Iterator it2 = fontList.begin(); 00420 int pos = 0; 00421 for ( ; it != it2; ++it2, ++pos); 00422 fontCombo->setCurrentItem( pos ); 00423 charTable->setFont( _font ); 00424 } 00425 else 00426 kdWarning() << "Can't find Font: " << _font << endl; 00427 } 00428 00429 //================================================================== 00430 void KCharSelect::setChar( const QChar &_chr ) 00431 { 00432 charTable->setChar( _chr ); 00433 } 00434 00435 //================================================================== 00436 void KCharSelect::setTableNum( int _tableNum ) 00437 { 00438 tableSpinBox->setValue( _tableNum ); 00439 charTable->setTableNum( _tableNum ); 00440 } 00441 00442 //================================================================== 00443 void KCharSelect::fillFontCombo() 00444 { 00445 if ( !fontDataBase ) { 00446 fontDataBase = new QFontDatabase(); 00447 qAddPostRoutine( cleanupFontDatabase ); 00448 } 00449 fontList=fontDataBase->families(); 00450 fontCombo->insertStringList( fontList ); 00451 } 00452 00453 //================================================================== 00454 void KCharSelect::fontSelected( const QString &_font ) 00455 { 00456 charTable->setFont( _font ); 00457 emit fontChanged( _font ); 00458 } 00459 00460 //================================================================== 00461 void KCharSelect::tableChanged( int _value ) 00462 { 00463 charTable->setTableNum( _value ); 00464 } 00465 00466 void KCharSelectTable::virtual_hook( int, void*) 00467 { /*BASE::virtual_hook( id, data );*/ } 00468 00469 void KCharSelect::virtual_hook( int, void* ) 00470 { /*BASE::virtual_hook( id, data );*/ } 00471
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 12 15:08:14 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003