libtdepim

krsqueezedtextlabel.cpp
1 /* This file has been copied from the KDE libraries and slightly modified.
2  This can be removed as soon as tdelibs provides the same functionality.
3 
4  Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "krsqueezedtextlabel.h"
22 #include "kstringhandler.h"
23 #include <tqtooltip.h>
24 
25 KRSqueezedTextLabel::KRSqueezedTextLabel( const TQString &text , TQWidget *parent, const char *name )
26  : TQLabel ( parent, name ) {
27  setSizePolicy(TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
28  fullText = text;
29  squeezeTextToLabel();
30 }
31 
32 KRSqueezedTextLabel::KRSqueezedTextLabel( TQWidget *parent, const char *name )
33  : TQLabel ( parent, name ) {
34  setSizePolicy(TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
35 }
36 
37 void KRSqueezedTextLabel::resizeEvent( TQResizeEvent * ) {
39 }
40 
41 TQSize KRSqueezedTextLabel::minimumSizeHint() const
42 {
43  TQSize sh = TQLabel::minimumSizeHint();
44  sh.setWidth(-1);
45  return sh;
46 }
47 
48 TQSize KRSqueezedTextLabel::sizeHint() const
49 {
50  return TQSize(contentsRect().width(), TQLabel::sizeHint().height());
51 }
52 
53 void KRSqueezedTextLabel::setText( const TQString &text ) {
54  fullText = text;
56 }
57 
59  TQFontMetrics fm(fontMetrics());
60  int labelWidth = size().width();
61  int textWidth = fm.width(fullText);
62  if (textWidth > labelWidth) {
63  TQString squeezedText = KStringHandler::rPixelSqueeze(fullText, fm, labelWidth);
64  TQLabel::setText(squeezedText);
65 
66  TQToolTip::remove( this );
67  TQToolTip::add( this, fullText );
68 
69  } else {
70  TQLabel::setText(fullText);
71 
72  TQToolTip::remove( this );
73  TQToolTip::hide();
74 
75  }
76 }
77 
78 void KRSqueezedTextLabel::setAlignment( int alignment )
79 {
80  // save fullText and restore it
81  TQString tmpFull(fullText);
82  TQLabel::setAlignment(alignment);
83  fullText = tmpFull;
84 }
85 
86 #include "krsqueezedtextlabel.moc"
void squeezeTextToLabel()
does the dirty work
KRSqueezedTextLabel(TQWidget *parent, const char *name=0)
Default constructor.
virtual void setAlignment(int)
Overridden for internal reasons; the API remains unaffected.
void resizeEvent(TQResizeEvent *)
used when widget is resized