libkdepim

krsqueezedtextlabel.cpp

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