libtdepim

krsqueezedtextlabel.cpp

00001 /* This file has been copied from the KDE libraries and slightly modified.
00002    This can be removed as soon as tdelibs 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 <tqtooltip.h>
00024 
00025 KRSqueezedTextLabel::KRSqueezedTextLabel( const TQString &text , TQWidget *parent, const char *name )
00026  : TQLabel ( parent, name ) {
00027   setSizePolicy(TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
00028   fullText = text;
00029   squeezeTextToLabel();
00030 }
00031 
00032 KRSqueezedTextLabel::KRSqueezedTextLabel( TQWidget *parent, const char *name )
00033  : TQLabel ( parent, name ) {
00034   setSizePolicy(TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
00035 }
00036 
00037 void KRSqueezedTextLabel::resizeEvent( TQResizeEvent * ) {
00038   squeezeTextToLabel();
00039 }
00040 
00041 TQSize KRSqueezedTextLabel::minimumSizeHint() const
00042 {
00043   TQSize sh = TQLabel::minimumSizeHint();
00044   sh.setWidth(-1);
00045   return sh;
00046 }
00047 
00048 TQSize KRSqueezedTextLabel::sizeHint() const
00049 {
00050   return TQSize(contentsRect().width(), TQLabel::sizeHint().height());
00051 }
00052 
00053 void KRSqueezedTextLabel::setText( const TQString &text ) {
00054   fullText = text;
00055   squeezeTextToLabel();
00056 }
00057 
00058 void KRSqueezedTextLabel::squeezeTextToLabel() {
00059   TQFontMetrics fm(fontMetrics());
00060   int labelWidth = size().width();
00061   int textWidth = fm.width(fullText);
00062   if (textWidth > labelWidth) {
00063     TQString squeezedText = KStringHandler::rPixelSqueeze(fullText, fm, labelWidth);
00064     TQLabel::setText(squeezedText);
00065 
00066     TQToolTip::remove( this );
00067     TQToolTip::add( this, fullText );
00068 
00069   } else {
00070     TQLabel::setText(fullText);
00071 
00072     TQToolTip::remove( this );
00073     TQToolTip::hide();
00074 
00075   }
00076 }
00077 
00078 void KRSqueezedTextLabel::setAlignment( int alignment )
00079 {
00080   // save fullText and restore it
00081   TQString tmpFull(fullText);
00082   TQLabel::setAlignment(alignment);
00083   fullText = tmpFull;
00084 }
00085 
00086 #include "krsqueezedtextlabel.moc"