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 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 // start with the dots only 00062 QString squeezedText = "..."; 00063 int squeezedWidth = fm.width(squeezedText); 00064 00065 // estimate how many letters we can add to the dots on both sides 00066 int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2; 00067 if (labelWidth < squeezedWidth) letters=1; 00068 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00069 squeezedWidth = fm.width(squeezedText); 00070 00071 if (squeezedWidth < labelWidth) { 00072 // we estimated too short 00073 // add letters while text < label 00074 do { 00075 letters++; 00076 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00077 squeezedWidth = fm.width(squeezedText); 00078 } while (squeezedWidth < labelWidth); 00079 letters--; 00080 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00081 } else if (squeezedWidth > labelWidth) { 00082 // we estimated too long 00083 // remove letters while text > label 00084 do { 00085 letters--; 00086 squeezedText = fullText.left(letters) + "..." + fullText.right(letters); 00087 squeezedWidth = fm.width(squeezedText); 00088 } while (letters && squeezedWidth > labelWidth); 00089 } 00090 00091 if (letters < 5) { 00092 // too few letters added -> we give up squeezing 00093 QLabel::setText(fullText); 00094 } else { 00095 QLabel::setText(squeezedText); 00096 } 00097 00098 QToolTip::remove( this ); 00099 QToolTip::add( this, fullText ); 00100 00101 } else { 00102 QLabel::setText(fullText); 00103 00104 QToolTip::remove( this ); 00105 QToolTip::hide(); 00106 00107 }; 00108 } 00109 00110 void KSqueezedTextLabel::virtual_hook( int, void* ) 00111 { /*BASE::virtual_hook( id, data );*/ } 00112 00113 #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 Fri Oct 8 11:14:28 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003