kdeui Library API Documentation

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., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "ksqueezedtextlabel.h" 00020 #include <qtooltip.h> 00021 00022 KSqueezedTextLabel::KSqueezedTextLabel( const QString &text , QWidget *parent, const char *name ) 00023 : QLabel ( parent, name ) { 00024 setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed )); 00025 fullText = text; 00026 squeezeTextToLabel(); 00027 } 00028 00029 KSqueezedTextLabel::KSqueezedTextLabel( QWidget *parent, const char *name ) 00030 : QLabel ( parent, name ) { 00031 setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed )); 00032 } 00033 00034 void KSqueezedTextLabel::resizeEvent( QResizeEvent * ) { 00035 squeezeTextToLabel(); 00036 } 00037 00038 QSize KSqueezedTextLabel::minimumSizeHint() const 00039 { 00040 QSize sh = QLabel::minimumSizeHint(); 00041 sh.setWidth(-1); 00042 return sh; 00043 } 00044 00045 void KSqueezedTextLabel::setText( const QString &text ) { 00046 fullText = text; 00047 squeezeTextToLabel(); 00048 } 00049 00050 void KSqueezedTextLabel::squeezeTextToLabel() { 00051 QFontMetrics fm(fontMetrics()); 00052 int labelWidth = size().width(); 00053 int textWidth = fm.width(fullText); 00054 if (textWidth > labelWidth) { 00055 // start with the dots only 00056 QString squeezedText = "..."; 00057 int squeezedWidth = fm.width(squeezedText); 00058 00059 // estimate how many letters we can add to the dots on both sides 00060 int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2; 00061 if (labelWidth < squeezedWidth) letters=1; 00062 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00063 squeezedWidth = fm.width(squeezedText); 00064 00065 if (squeezedWidth < labelWidth) { 00066 // we estimated too short 00067 // add letters while text < label 00068 do { 00069 letters++; 00070 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00071 squeezedWidth = fm.width(squeezedText); 00072 } while (squeezedWidth < labelWidth); 00073 letters--; 00074 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00075 } else if (squeezedWidth > labelWidth) { 00076 // we estimated too long 00077 // remove letters while text > label 00078 do { 00079 letters--; 00080 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00081 squeezedWidth = fm.width(squeezedText); 00082 } while (letters && squeezedWidth > labelWidth); 00083 } 00084 00085 if (letters < 5) { 00086 // too few letters added -> we give up squeezing 00087 QLabel::setText(fullText); 00088 } else { 00089 QLabel::setText(squeezedText); 00090 } 00091 00092 QToolTip::remove( this ); 00093 QToolTip::add( this, fullText ); 00094 00095 } else { 00096 QLabel::setText(fullText); 00097 00098 QToolTip::remove( this ); 00099 QToolTip::hide(); 00100 00101 }; 00102 } 00103 00104 void KSqueezedTextLabel::virtual_hook( int, void* ) 00105 { /*BASE::virtual_hook( id, data );*/ } 00106 00107 #include "ksqueezedtextlabel.moc"
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 Mon Aug 30 22:54:00 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003