Diffstat (limited to 'microkde/kdeui/ksqueezedtextlabel.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | microkde/kdeui/ksqueezedtextlabel.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/microkde/kdeui/ksqueezedtextlabel.cpp b/microkde/kdeui/ksqueezedtextlabel.cpp new file mode 100644 index 0000000..37fa29a --- a/dev/null +++ b/microkde/kdeui/ksqueezedtextlabel.cpp | |||
@@ -0,0 +1,107 @@ | |||
1 | /* This file is part of the KDE libraries | ||
2 | Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License version 2 as published by the Free Software Foundation. | ||
7 | |||
8 | This library is distributed in the hope that it will be useful, | ||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
11 | Library General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU Library General Public License | ||
14 | along with this library; see the file COPYING.LIB. If not, write to | ||
15 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
16 | Boston, MA 02111-1307, USA. | ||
17 | */ | ||
18 | |||
19 | #include "ksqueezedtextlabel.h" | ||
20 | #include <qtooltip.h> | ||
21 | |||
22 | KSqueezedTextLabel::KSqueezedTextLabel( const QString &text , QWidget *parent, const char *name ) | ||
23 | : QLabel ( parent, name ) { | ||
24 | setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed )); | ||
25 | fullText = text; | ||
26 | squeezeTextToLabel(); | ||
27 | } | ||
28 | |||
29 | KSqueezedTextLabel::KSqueezedTextLabel( QWidget *parent, const char *name ) | ||
30 | : QLabel ( parent, name ) { | ||
31 | setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed )); | ||
32 | } | ||
33 | |||
34 | void KSqueezedTextLabel::resizeEvent( QResizeEvent * ) { | ||
35 | squeezeTextToLabel(); | ||
36 | } | ||
37 | |||
38 | QSize KSqueezedTextLabel::minimumSizeHint() const | ||
39 | { | ||
40 | QSize sh = QLabel::minimumSizeHint(); | ||
41 | sh.setWidth(-1); | ||
42 | return sh; | ||
43 | } | ||
44 | |||
45 | void KSqueezedTextLabel::setText( const QString &text ) { | ||
46 | fullText = text; | ||
47 | squeezeTextToLabel(); | ||
48 | } | ||
49 | |||
50 | void KSqueezedTextLabel::squeezeTextToLabel() { | ||
51 | QFontMetrics fm(fontMetrics()); | ||
52 | int labelWidth = size().width(); | ||
53 | int textWidth = fm.width(fullText); | ||
54 | if (textWidth > labelWidth) { | ||
55 | // start with the dots only | ||
56 | QString squeezedText = "..."; | ||
57 | int squeezedWidth = fm.width(squeezedText); | ||
58 | |||
59 | // estimate how many letters we can add to the dots on both sides | ||
60 | int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2; | ||
61 | if (labelWidth < squeezedWidth) letters=1; | ||
62 | squeezedText = fullText.left(letters) + "..." + fullText.right(letters); | ||
63 | squeezedWidth = fm.width(squeezedText); | ||
64 | |||
65 | if (squeezedWidth < labelWidth) { | ||
66 | // we estimated too short | ||
67 | // add letters while text < label | ||
68 | do { | ||
69 | letters++; | ||
70 | squeezedText = fullText.left(letters) + "..." + fullText.right(letters); | ||
71 | squeezedWidth = fm.width(squeezedText); | ||
72 | } while (squeezedWidth < labelWidth); | ||
73 | letters--; | ||
74 | squeezedText = fullText.left(letters) + "..." + fullText.right(letters); | ||
75 | } else if (squeezedWidth > labelWidth) { | ||
76 | // we estimated too long | ||
77 | // remove letters while text > label | ||
78 | do { | ||
79 | letters--; | ||
80 | squeezedText = fullText.left(letters) + "..." + fullText.right(letters); | ||
81 | squeezedWidth = fm.width(squeezedText); | ||
82 | } while (letters && squeezedWidth > labelWidth); | ||
83 | } | ||
84 | |||
85 | if (letters < 5) { | ||
86 | // too few letters added -> we give up squeezing | ||
87 | QLabel::setText(fullText); | ||
88 | } else { | ||
89 | QLabel::setText(squeezedText); | ||
90 | } | ||
91 | |||
92 | //US QToolTip::remove( this ); | ||
93 | //US QToolTip::add( this, fullText ); | ||
94 | |||
95 | } else { | ||
96 | QLabel::setText(fullText); | ||
97 | |||
98 | //US QToolTip::remove( this ); | ||
99 | //US QToolTip::hide(); | ||
100 | |||
101 | }; | ||
102 | } | ||
103 | |||
104 | void KSqueezedTextLabel::virtual_hook( int, void* ) | ||
105 | { /*BASE::virtual_hook( id, data );*/ } | ||
106 | |||
107 | //US #include "ksqueezedtextlabel.moc" | ||