Limit size of notification popups

This works around badly designed applications that abuse the notification subsystem to display large chunks of text
pull/1/head
Timothy Pearson 9 years ago
parent 6a0fb5c342
commit 70526c5a3e

@ -123,11 +123,39 @@ void KPassivePopup::setView( const TQString &caption, const TQString &text,
setView( standardView( caption, text, icon, this ) ); setView( standardView( caption, text, icon, this ) );
} }
static void truncateStringToFit(TQString &string, TQFont font, int max_width) {
bool truncated = false;
TQFontMetrics fm(font);
while (fm.width(string) > max_width) {
string.truncate(string.length() - 1);
truncated = true;
}
if (truncated) {
string += " ...";
}
}
TQVBox * KPassivePopup::standardView(const TQString& caption, TQVBox * KPassivePopup::standardView(const TQString& caption,
const TQString& text, const TQString& text,
const TQPixmap& icon, const TQPixmap& icon,
TQWidget *parent) TQWidget *parent)
{ {
TQString sizedCaption = caption;
TQString sizedText = text;
#ifdef Q_WS_X11
int max_width;
NETRootInfo info( tqt_xdisplay(),
NET::NumberOfDesktops |
NET::CurrentDesktop |
NET::WorkArea,
-1, false );
info.activate();
NETRect workArea = info.workArea(info.currentDesktop());
max_width = workArea.size.width / 3;
#endif
TQVBox *vb = new TQVBox( parent ? parent : this ); TQVBox *vb = new TQVBox( parent ? parent : this );
vb->setSpacing( KDialog::spacingHint() ); vb->setSpacing( KDialog::spacingHint() );
@ -141,18 +169,46 @@ TQVBox * KPassivePopup::standardView( const TQString& caption,
ttlIcon->setAlignment( AlignLeft ); ttlIcon->setAlignment( AlignLeft );
} }
if ( !caption.isEmpty() ) { if ( !sizedCaption.isEmpty() ) {
ttl = new TQLabel( caption, hb ? hb : vb, "title_label" ); ttl = new TQLabel( sizedCaption, hb ? hb : vb, "title_label" );
TQFont fnt = ttl->font(); TQFont fnt = ttl->font();
#ifdef Q_WS_X11
truncateStringToFit(sizedCaption, fnt, max_width);
ttl->setText(sizedCaption);
#endif
fnt.setBold( true ); fnt.setBold( true );
ttl->setFont( fnt ); ttl->setFont( fnt );
ttl->setAlignment( Qt::AlignHCenter ); ttl->setAlignment( Qt::AlignHCenter );
if ( hb ) if ( hb ) {
hb->setStretchFactor( ttl, 10 ); // enforce centering hb->setStretchFactor( ttl, 10 ); // enforce centering
} }
}
if ( !sizedText.isEmpty() ) {
msg = new TQLabel( sizedText, vb, "msg_label" );
#ifdef Q_WS_X11
TQStringList textLines = TQStringList::split("\n", sizedText, true);
for (TQStringList::Iterator it = textLines.begin(); it != textLines.end(); ++it) {
truncateStringToFit(*it, msg->font(), max_width);
}
if ( !text.isEmpty() ) { // Limit message to 5 lines of text
msg = new TQLabel( text, vb, "msg_label" ); if (textLines.count() > 5) {
int count = 3;
TQStringList truncatedLines;
for (TQStringList::Iterator it = textLines.begin(); it != textLines.end(); ++it) {
truncatedLines.append(*it);
if (count > 5) {
truncatedLines.append("...");
break;
}
count++;
}
textLines = truncatedLines;
}
sizedText = textLines.join("\n");
msg->setText(sizedText);
#endif
msg->setAlignment( AlignLeft ); msg->setAlignment( AlignLeft );
} }
@ -194,17 +250,23 @@ void KPassivePopup::mouseReleaseEvent( TQMouseEvent *e )
void KPassivePopup::show() void KPassivePopup::show()
{ {
if ( size() != sizeHint() ) TQSize desiredSize = sizeHint();
resize( sizeHint() );
if (size() != desiredSize) {
resize(desiredSize);
}
if ( d->fixedPosition.isNull() ) if (d->fixedPosition.isNull()) {
positionSelf(); positionSelf();
}
else { else {
if( d->popupStyle == Balloon ) if( d->popupStyle == Balloon ) {
setAnchor(d->fixedPosition); setAnchor(d->fixedPosition);
else }
else {
move(d->fixedPosition); move(d->fixedPosition);
} }
}
TQFrame::show(); TQFrame::show();
int delay = hideDelay; int delay = hideDelay;
@ -346,14 +408,15 @@ void KPassivePopup::setAnchor(const TQPoint &anchor)
void KPassivePopup::paintEvent( TQPaintEvent* pe ) void KPassivePopup::paintEvent( TQPaintEvent* pe )
{ {
if( d->popupStyle == Balloon ) if( d->popupStyle == Balloon ) {
{
TQPainter p; TQPainter p;
p.begin( this ); p.begin( this );
p.drawPolygon( d->surround ); p.drawPolygon( d->surround );
} else }
else {
TQFrame::paintEvent( pe ); TQFrame::paintEvent( pe );
} }
}
void KPassivePopup::updateMask() void KPassivePopup::updateMask()
{ {

Loading…
Cancel
Save