Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qimpenwordpick.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include <qpainter.h>
00022 #include "qimpenwordpick.h"
00023 
00024 QIMPenWordPick::QIMPenWordPick( QWidget *parent, const char *name, WFlags f )
00025  : QFrame( parent, name, f )
00026 {
00027     clickWord = -1;
00028     setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
00029 }
00030 
00031 void QIMPenWordPick::clear()
00032 {
00033     words.clear();
00034     repaint();
00035 }
00036 
00037 QSize QIMPenWordPick::sizeHint() const
00038 {
00039     return QSize( -1, font().pixelSize()+2 );
00040 }
00041 
00042 void QIMPenWordPick::setWords( const QIMPenMatch::MatchWordList &w )
00043 {
00044     words.clear();
00045     QListIterator<QIMPenMatch::MatchWord> it( w );
00046     for ( ; it.current(); ++it ) {
00047         words.append( it.current()->word );
00048     }
00049     repaint();
00050 }
00051 
00052 int QIMPenWordPick::onWord( QPoint p )
00053 {
00054     int x = 2;
00055     int idx = 0;
00056     for ( QStringList::Iterator it = words.begin(); it != words.end(); ++it ) {
00057         QString word = *it;
00058         int w = fontMetrics().width( word );
00059         if ( x + w > width() )
00060             break;
00061         if ( p.x() > x-2 && p.x() < x + w + 2 )
00062             return idx;
00063         x += w + 5;
00064         if ( !idx )
00065             x += 3;
00066         idx++;
00067     }
00068 
00069     return -1;
00070 }
00071 
00072 void QIMPenWordPick::paintEvent( QPaintEvent * )
00073 {
00074     QPainter p(this);
00075     int x = 2;
00076     int h = p.fontMetrics().ascent() + 1;
00077     int idx = 0;
00078     for ( QStringList::Iterator it = words.begin(); it != words.end(); ++it ) {
00079         QString word = *it;
00080         int w = p.fontMetrics().width( word );
00081         if ( x + w > width() )
00082             break;
00083         if ( idx == clickWord ) {
00084             p.fillRect( x, 0, w, height(), black );
00085             p.setPen( white );
00086         } else {
00087             p.setPen( colorGroup().text() );
00088         }
00089         p.drawText( x, h, word );
00090         x += w + 5;
00091         if ( !idx )
00092             x += 3;
00093         idx++;
00094     }
00095 }
00096 
00097 void QIMPenWordPick::mousePressEvent( QMouseEvent *e )
00098 {
00099     clickWord = onWord( e->pos() );
00100     repaint();
00101 }
00102 
00103 void QIMPenWordPick::mouseReleaseEvent( QMouseEvent *e )
00104 {
00105     int wordIdx = onWord( e->pos() );
00106     if ( wordIdx >= 0 && wordIdx == clickWord ) {
00107         //odebug << "Clicked " <<  words[wordIdx].latin1() << oendl;
00108         emit wordClicked( words[wordIdx] );
00109     }
00110     clickWord = -1;
00111     repaint();
00112 }
00113 

Generated on Sat Nov 5 16:16:02 2005 for OPIE by  doxygen 1.4.2