ksqueezedtextlabel.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "ksqueezedtextlabel.h" 00020 #include "kstringhandler.h" 00021 #include <qtooltip.h> 00022 00023 KSqueezedTextLabel::KSqueezedTextLabel( const QString &text , QWidget *parent, const char *name ) 00024 : QLabel ( parent, name ) { 00025 setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed )); 00026 fullText = text; 00027 squeezeTextToLabel(); 00028 } 00029 00030 KSqueezedTextLabel::KSqueezedTextLabel( QWidget *parent, const char *name ) 00031 : QLabel ( parent, name ) { 00032 setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed )); 00033 } 00034 00035 void KSqueezedTextLabel::resizeEvent( QResizeEvent * ) { 00036 squeezeTextToLabel(); 00037 } 00038 00039 QSize KSqueezedTextLabel::minimumSizeHint() const 00040 { 00041 QSize sh = QLabel::minimumSizeHint(); 00042 sh.setWidth(-1); 00043 return sh; 00044 } 00045 00046 QSize KSqueezedTextLabel::sizeHint() const 00047 { 00048 return QSize(contentsRect().width(), QLabel::sizeHint().height()); 00049 } 00050 00051 void KSqueezedTextLabel::setText( const QString &text ) { 00052 fullText = text; 00053 squeezeTextToLabel(); 00054 } 00055 00056 void KSqueezedTextLabel::squeezeTextToLabel() { 00057 QFontMetrics fm(fontMetrics()); 00058 int labelWidth = size().width(); 00059 int textWidth = fm.width(fullText); 00060 if (textWidth > labelWidth) { 00061 QString squeezedText = KStringHandler::cPixelSqueeze(fullText, fm, labelWidth); 00062 QLabel::setText(squeezedText); 00063 00064 QToolTip::remove( this ); 00065 QToolTip::add( this, fullText ); 00066 00067 } else { 00068 QLabel::setText(fullText); 00069 00070 QToolTip::remove( this ); 00071 QToolTip::hide(); 00072 00073 } 00074 } 00075 00076 void KSqueezedTextLabel::setAlignment( int alignment ) 00077 { 00078 // save fullText and restore it 00079 QString tmpFull(fullText); 00080 QLabel::setAlignment(alignment); 00081 fullText = tmpFull; 00082 } 00083 00084 void KSqueezedTextLabel::virtual_hook( int, void* ) 00085 { /*BASE::virtual_hook( id, data );*/ } 00086 00087 #include "ksqueezedtextlabel.moc"