00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#include "ksqueezedtextlabel.h"
00020
#include <qtooltip.h>
00021
00022
KSqueezedTextLabel::KSqueezedTextLabel(
const QString &text ,
QWidget *parent,
const char *name )
00023 :
QLabel ( parent,
name ) {
00024 setSizePolicy(
QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
00025 fullText = text;
00026 squeezeTextToLabel();
00027 }
00028
00029 KSqueezedTextLabel::KSqueezedTextLabel(
QWidget *parent,
const char *name )
00030 :
QLabel ( parent, name ) {
00031 setSizePolicy(
QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
00032 }
00033
00034 void KSqueezedTextLabel::resizeEvent(
QResizeEvent * ) {
00035
squeezeTextToLabel();
00036 }
00037
00038
QSize KSqueezedTextLabel::minimumSizeHint()
const
00039
{
00040
QSize sh = QLabel::minimumSizeHint();
00041 sh.setWidth(-1);
00042
return sh;
00043 }
00044
00045
00046
QSize KSqueezedTextLabel::sizeHint()
const
00047
{
00048
return QSize(contentsRect().width(), QLabel::sizeHint().height());
00049 }
00050
00051
void KSqueezedTextLabel::setText(
const QString &text ) {
00052 fullText = text;
00053
squeezeTextToLabel();
00054 }
00055
00056 void KSqueezedTextLabel::squeezeTextToLabel() {
00057
QFontMetrics fm(fontMetrics());
00058
int labelWidth = size().width();
00059
int textWidth = fm.width(fullText);
00060
if (textWidth > labelWidth) {
00061
00062
QString squeezedText =
"...";
00063
int squeezedWidth = fm.width(squeezedText);
00064
00065
00066
int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2;
00067
if (labelWidth < squeezedWidth) letters=1;
00068 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
00069 squeezedWidth = fm.width(squeezedText);
00070
00071
if (squeezedWidth < labelWidth) {
00072
00073
00074
do {
00075 letters++;
00076 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
00077 squeezedWidth = fm.width(squeezedText);
00078 }
while (squeezedWidth < labelWidth);
00079 letters--;
00080 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
00081 }
else if (squeezedWidth > labelWidth) {
00082
00083
00084
do {
00085 letters--;
00086 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
00087 squeezedWidth = fm.width(squeezedText);
00088 }
while (letters && squeezedWidth > labelWidth);
00089 }
00090
00091
if (letters < 5) {
00092
00093 QLabel::setText(fullText);
00094 }
else {
00095 QLabel::setText(squeezedText);
00096 }
00097
00098 QToolTip::remove(
this );
00099 QToolTip::add(
this, fullText );
00100
00101 }
else {
00102 QLabel::setText(fullText);
00103
00104 QToolTip::remove(
this );
00105 QToolTip::hide();
00106
00107 };
00108 }
00109
00110
void KSqueezedTextLabel::virtual_hook(
int,
void* )
00111 { }
00112
00113
#include "ksqueezedtextlabel.moc"