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 
00036 #include <klocale.h>
00037 #include <kdebug.h>
00038 #include <kdialog.h>
00039 #include <kapplication.h>
00040 
00041 QFontDatabase * KCharSelect::fontDataBase = 0;
00042 
00043 void KCharSelect::cleanupFontDatabase()
00044 {
00045     delete fontDataBase;
00046     fontDataBase = 0;
00047 }
00048 
00049 /******************************************************************/
00050 /* Class: KCharSelectTable                    */
00051 /******************************************************************/
00052 
00053 //==================================================================
00054 KCharSelectTable::KCharSelectTable( QWidget *parent, const char *name, const QString &_font,
00055                     const QChar &_chr, int _tableNum )
00056     : QGridView( parent, name ), vFont( _font ), vChr( _chr ),
00057       vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 )
00058 {
00059     setBackgroundColor( colorGroup().base() );
00060 
00061     setCellWidth( 20 );
00062     setCellHeight( 25 );
00063 
00064     setNumCols( 32 );
00065     setNumRows( 8 );
00066 
00067     repaintContents( false );
00068 
00069     setFocusPolicy( QWidget::StrongFocus );
00070     setBackgroundMode( QWidget::NoBackground );
00071 }
00072 
00073 //==================================================================
00074 void KCharSelectTable::setFont( const QString &_font )
00075 {
00076     vFont = _font;
00077     repaintContents( false );
00078 }
00079 
00080 //==================================================================
00081 void KCharSelectTable::setChar( const QChar &_chr )
00082 {
00083     vChr = _chr;
00084     repaintContents( false );
00085 }
00086 
00087 //==================================================================
00088 void KCharSelectTable::setTableNum( int _tableNum )
00089 {
00090     focusItem = QChar( _tableNum * 256 );
00091 
00092     vTableNum = _tableNum;
00093     repaintContents( false );
00094 }
00095 
00096 //==================================================================
00097 QSize KCharSelectTable::sizeHint() const
00098 {
00099     int w = cellWidth();
00100     int h = cellHeight();
00101 
00102     w *= numCols();
00103     h *= numRows();
00104 
00105     return QSize( w, h );
00106 }
00107 
00108 //==================================================================
00109 void KCharSelectTable::resizeEvent( QResizeEvent * e )
00110 {
00111     int new_w   = (e->size().width()  - 2*(margin()+frameWidth())) / numCols();
00112     int new_h   = (e->size().height() - 2*(margin()+frameWidth())) / numRows();
00113 
00114     if( new_w !=  cellWidth())
00115         setCellWidth( new_w );
00116     if( new_h !=  cellHeight())
00117         setCellHeight( new_h );
00118 }
00119 
00120 //==================================================================
00121 void KCharSelectTable::paintCell( class QPainter* p, int row, int col )
00122 {
00123     int w = cellWidth();
00124     int h = cellHeight();
00125     int x2 = w - 1;
00126     int y2 = h - 1;
00127 
00128     //if( row == 0 && col == 0 ) {
00129     //    printf("Repaint %d\n", temp++);
00130     //    fflush( stdout );
00131     //    }
00132 
00133     QFont font = QFont( vFont );
00134     font.setPixelSize( int(.7 * h) );
00135 
00136     unsigned short c = vTableNum * 256;
00137     c += row * numCols();
00138     c += col;
00139 
00140     if ( c == vChr.unicode() ) {
00141     p->setBrush( QBrush( colorGroup().highlight() ) );
00142     p->setPen( NoPen );
00143     p->drawRect( 0, 0, w, h );
00144     p->setPen( colorGroup().highlightedText() );
00145     vPos = QPoint( col, row );
00146     } else {
00147     QFontMetrics fm = QFontMetrics( font );
00148     if( fm.inFont( c ) )
00149         p->setBrush( QBrush( colorGroup().base() ) );
00150     else
00151         p->setBrush( QBrush( colorGroup().button() ) );
00152     p->setPen( NoPen );
00153     p->drawRect( 0, 0, w, h );
00154     p->setPen( colorGroup().text() );
00155     }
00156 
00157     if ( c == focusItem.unicode() && hasFocus() ) {
00158 #if QT_VERSION < 300
00159     style().drawFocusRect( p, QRect( 2, 2, w - 4, h - 4 ), colorGroup() );
00160 #else
00161     style().drawPrimitive( QStyle::PE_FocusRect, p, QRect( 2, 2, w - 4, h - 4 ), 
00162                    colorGroup() );
00163 #endif
00164     focusPos = QPoint( col, row );
00165     }
00166 
00167     p->setFont( font );
00168 
00169     p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, QString( QChar( c ) ) );
00170 
00171     p->setPen( colorGroup().text() );
00172     p->drawLine( x2, 0, x2, y2 );
00173     p->drawLine( 0, y2, x2, y2 );
00174 
00175     if ( row == 0 )
00176     p->drawLine( 0, 0, x2, 0 );
00177     if ( col == 0 )
00178     p->drawLine( 0, 0, 0, y2 );
00179 }
00180 
00181 //==================================================================
00182 void KCharSelectTable::mouseMoveEvent( QMouseEvent *e )
00183 {
00184     int row = rowAt( e->y() );
00185     int col = columnAt( e->x() );
00186     if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
00187     QPoint oldPos = vPos;
00188 
00189     vPos.setX( col );
00190     vPos.setY( row );
00191 
00192     vChr = QChar( vTableNum * 256 + numCols() * vPos.y() + vPos.x() );
00193 
00194     QPoint oldFocus = focusPos;
00195 
00196     focusPos = vPos;
00197     focusItem = vChr;
00198 
00199     repaintCell( oldFocus.y(), oldFocus.x(), true );
00200     repaintCell( oldPos.y(), oldPos.x(), true );
00201     repaintCell( vPos.y(), vPos.x(), true );
00202 
00203     emit highlighted( vChr );
00204     emit highlighted();
00205 
00206     emit focusItemChanged( focusItem );
00207     emit focusItemChanged();
00208     }
00209 }
00210 
00211 //==================================================================
00212 void KCharSelectTable::keyPressEvent( QKeyEvent *e )
00213 {
00214     switch ( e->key() ) {
00215     case Key_Left:
00216     gotoLeft();
00217     break;
00218     case Key_Right:
00219     gotoRight();
00220     break;
00221     case Key_Up:
00222     gotoUp();
00223     break;
00224     case Key_Down:
00225     gotoDown();
00226     break;
00227     case Key_Next:
00228     emit tableDown();
00229     break;
00230     case Key_Prior:
00231     emit tableUp();
00232     break;
00233     case Key_Space:
00234     emit activated( ' ' );
00235     emit activated();
00236     emit highlighted( ' ' );
00237     emit highlighted();
00238         break;
00239     case Key_Enter: case Key_Return: {
00240     QPoint oldPos = vPos;
00241 
00242     vPos = focusPos;
00243     vChr = focusItem;
00244 
00245     repaintCell( oldPos.y(), oldPos.x(), true );
00246     repaintCell( vPos.y(), vPos.x(), true );
00247 
00248     emit activated( vChr );
00249     emit activated();
00250     emit highlighted( vChr );
00251     emit highlighted();
00252     } break;
00253     }
00254 }
00255 
00256 //==================================================================
00257 void KCharSelectTable::gotoLeft()
00258 {
00259     if ( focusPos.x() > 0 ) {
00260     QPoint oldPos = focusPos;
00261 
00262     focusPos.setX( focusPos.x() - 1 );
00263 
00264     focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00265 
00266     repaintCell( oldPos.y(), oldPos.x(), true );
00267     repaintCell( focusPos.y(), focusPos.x(), true );
00268 
00269     emit focusItemChanged( vChr );
00270     emit focusItemChanged();
00271     }
00272 }
00273 
00274 //==================================================================
00275 void KCharSelectTable::gotoRight()
00276 {
00277     if ( focusPos.x() < numCols()-1 ) {
00278     QPoint oldPos = focusPos;
00279 
00280     focusPos.setX( focusPos.x() + 1 );
00281 
00282     focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00283 
00284     repaintCell( oldPos.y(), oldPos.x(), true );
00285     repaintCell( focusPos.y(), focusPos.x(), true );
00286 
00287     emit focusItemChanged( vChr );
00288     emit focusItemChanged();
00289     }
00290 }
00291 
00292 //==================================================================
00293 void KCharSelectTable::gotoUp()
00294 {
00295     if ( focusPos.y() > 0 ) {
00296     QPoint oldPos = focusPos;
00297 
00298     focusPos.setY( focusPos.y() - 1 );
00299 
00300     focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00301 
00302     repaintCell( oldPos.y(), oldPos.x(), true );
00303     repaintCell( focusPos.y(), focusPos.x(), true );
00304 
00305     emit focusItemChanged( vChr );
00306     emit focusItemChanged();
00307     }
00308 }
00309 
00310 //==================================================================
00311 void KCharSelectTable::gotoDown()
00312 {
00313     if ( focusPos.y() < numRows()-1 ) {
00314     QPoint oldPos = focusPos;
00315 
00316     focusPos.setY( focusPos.y() + 1 );
00317 
00318     focusItem = QChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
00319 
00320     repaintCell( oldPos.y(), oldPos.x(), true );
00321     repaintCell( focusPos.y(), focusPos.x(), true );
00322 
00323     emit focusItemChanged( vChr );
00324     emit focusItemChanged();
00325     }
00326 }
00327 
00328 /******************************************************************/
00329 /* Class: KCharSelect                         */
00330 /******************************************************************/
00331 
00332 //==================================================================
00333 KCharSelect::KCharSelect( QWidget *parent, const char *name, const QString &_font, const QChar &_chr, int _tableNum )
00334     : QVBox( parent, name )
00335 {
00336     setSpacing( KDialog::spacingHint() );
00337     QHBox *bar = new QHBox( this );
00338     bar->setSpacing( KDialog::spacingHint() );
00339 
00340     QLabel *lFont = new QLabel( i18n( "  Font:  " ), bar );
00341     lFont->resize( lFont->sizeHint() );
00342     lFont->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00343     lFont->setMaximumWidth( lFont->sizeHint().width() );
00344 
00345     fontCombo = new QComboBox( true, bar );
00346     fillFontCombo();
00347     fontCombo->resize( fontCombo->sizeHint() );
00348 
00349     connect( fontCombo, SIGNAL( activated( const QString & ) ), this, SLOT( fontSelected( const QString & ) ) );
00350 
00351     QLabel *lTable = new QLabel( i18n( "  Table:  " ), bar );
00352     lTable->resize( lTable->sizeHint() );
00353     lTable->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00354     lTable->setMaximumWidth( lTable->sizeHint().width() );
00355 
00356     tableSpinBox = new QSpinBox( 0, 255, 1, bar );
00357     tableSpinBox->resize( tableSpinBox->sizeHint() );
00358 
00359     connect( tableSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( tableChanged( int ) ) );
00360 
00361     charTable = new KCharSelectTable( this, name, _font.isEmpty() ? QVBox::font().family() : _font, _chr, _tableNum );
00362     QSize sz( charTable->contentsWidth()  +  4 ,
00363               charTable->contentsHeight() +  4 );
00364     charTable->resize( sz );
00365     //charTable->setMaximumSize( sz );
00366     charTable->setMinimumSize( sz );
00367     charTable->setHScrollBarMode( QScrollView::AlwaysOff );
00368     charTable->setVScrollBarMode( QScrollView::AlwaysOff );
00369 
00370     setFont( _font.isEmpty() ? QVBox::font().family() : _font );
00371     setTableNum( _tableNum );
00372 
00373     connect( charTable, SIGNAL( highlighted( const QChar & ) ), this, SLOT( charHighlighted( const QChar & ) ) );
00374     connect( charTable, SIGNAL( highlighted() ), this, SLOT( charHighlighted() ) );
00375     connect( charTable, SIGNAL( activated( const QChar & ) ), this, SLOT( charActivated( const QChar & ) ) );
00376     connect( charTable, SIGNAL( activated() ), this, SLOT( charActivated() ) );
00377     connect( charTable, SIGNAL( focusItemChanged( const QChar & ) ),
00378          this, SLOT( charFocusItemChanged( const QChar & ) ) );
00379     connect( charTable, SIGNAL( focusItemChanged() ), this, SLOT( charFocusItemChanged() ) );
00380     connect( charTable, SIGNAL( tableUp() ), this, SLOT( charTableUp() ) );
00381     connect( charTable, SIGNAL( tableDown() ), this, SLOT( charTableDown() ) );
00382 
00383     connect( charTable, SIGNAL(doubleClicked()),this,SLOT(slotDoubleClicked()));
00384 
00385     setFocusPolicy( QWidget::StrongFocus );
00386     setFocusProxy( charTable );
00387 }
00388 
00389 //==================================================================
00390 QSize KCharSelect::sizeHint() const
00391 {
00392     return QVBox::sizeHint();
00393 }
00394 
00395 //==================================================================
00396 void KCharSelect::setFont( const QString &_font )
00397 {
00398     QValueList<QString>::Iterator it = fontList.find( _font );
00399     if ( it != fontList.end() ) {
00400     QValueList<QString>::Iterator it2 = fontList.begin();
00401     int pos = 0;
00402     for ( ; it != it2; ++it2, ++pos);
00403     fontCombo->setCurrentItem( pos );
00404     charTable->setFont( _font );
00405     }
00406     else
00407     kdWarning() << "Can't find Font: " << _font << endl;
00408 }
00409 
00410 //==================================================================
00411 void KCharSelect::setChar( const QChar &_chr )
00412 {
00413     charTable->setChar( _chr );
00414 }
00415 
00416 //==================================================================
00417 void KCharSelect::setTableNum( int _tableNum )
00418 {
00419     tableSpinBox->setValue( _tableNum );
00420     charTable->setTableNum( _tableNum );
00421 }
00422 
00423 //==================================================================
00424 void KCharSelect::fillFontCombo()
00425 {
00426     if ( !fontDataBase ) {
00427     fontDataBase = new QFontDatabase();
00428     qAddPostRoutine( cleanupFontDatabase );
00429     }
00430     fontList=fontDataBase->families();
00431     fontCombo->insertStringList( fontList );
00432 }
00433 
00434 //==================================================================
00435 void KCharSelect::fontSelected( const QString &_font )
00436 {
00437     charTable->setFont( _font );
00438     emit fontChanged( _font );
00439 }
00440 
00441 //==================================================================
00442 void KCharSelect::tableChanged( int _value )
00443 {
00444     charTable->setTableNum( _value );
00445 }
00446 
00447 void KCharSelectTable::virtual_hook( int, void*)
00448 { /*BASE::virtual_hook( id, data );*/ }
00449 
00450 void KCharSelect::virtual_hook( int, void* )
00451 { /*BASE::virtual_hook( id, data );*/ }
00452 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:57 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001