00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#include "ksqueezedtextlabel.h"
00020
#include "kstringhandler.h"
00021
#include <qtooltip.h>
00022
00023
KSqueezedTextLabel::KSqueezedTextLabel(
const QString &text ,
QWidget *parent,
const char *name )
00024 :
QLabel ( parent,
name ) {
00025 setSizePolicy(
QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
00026 fullText = text;
00027 squeezeTextToLabel();
00028 }
00029
00030 KSqueezedTextLabel::KSqueezedTextLabel(
QWidget *parent,
const char *name )
00031 :
QLabel ( parent, name ) {
00032 setSizePolicy(
QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
00033 }
00034
00035 void KSqueezedTextLabel::resizeEvent(
QResizeEvent * ) {
00036
squeezeTextToLabel();
00037 }
00038
00039
QSize KSqueezedTextLabel::minimumSizeHint()
const
00040
{
00041
QSize sh = QLabel::minimumSizeHint();
00042 sh.setWidth(-1);
00043
return sh;
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
QString squeezedText =
KStringHandler::cPixelSqueeze(fullText, fm, labelWidth);
00062 QLabel::setText(squeezedText);
00063
00064 QToolTip::remove(
this );
00065 QToolTip::add(
this, fullText );
00066
00067 }
else {
00068 QLabel::setText(fullText);
00069
00070 QToolTip::remove(
this );
00071 QToolTip::hide();
00072
00073 };
00074 }
00075
00076
void KSqueezedTextLabel::virtual_hook(
int,
void* )
00077 { }
00078
00079
#include "ksqueezedtextlabel.moc"