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