ksqueezedtextlabel.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
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     
00056     QString squeezedText = "...";
00057     int squeezedWidth = fm.width(squeezedText);
00058 
00059     
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         
00067         
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         
00077         
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         
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 {  }
00106 
00107 #include "ksqueezedtextlabel.moc"
 
This file is part of the documentation for kdeui Library Version 3.2.0.