summaryrefslogtreecommitdiffstats
path: root/kdeui/kwindowinfo.cpp
blob: 263deae84902389dbf4031a603da28686814cfe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *   copyright            : (C) 2001-2002 by Richard Moore
 *   License              : This file is released under the terms of the LGPL, version 2.
 *   email                : rich@kde.org
 */

#include <qobjectlist.h>
#include <qpixmap.h>
#include <qtimer.h>
#include <qtooltip.h>
#include <ksystemtray.h>
#include <kwin.h>

#include "kwindowinfo.h"
#include "kwindowinfo.moc"

static const int UNSPECIFIED_TIMEOUT = -1;
static const int DEFAULT_MESSAGE_TIMEOUT = 3000;

KWindowInfo::KWindowInfo( QWidget *parent, const char *name )
    : QObject( parent, name ), win( parent ), autoDel( false )
{
}

KWindowInfo::~KWindowInfo()
{
}

void KWindowInfo::showMessage( QWidget *window, const QString &text, int timeout )
{
    KWindowInfo *info = new KWindowInfo( window );
    info->autoDel = true;
    info->message( text, timeout );
    if ( timeout == 0 )
	delete info;
}

void KWindowInfo::showMessage( QWidget *window, const QString &text, const QPixmap &pix, int timeout )
{
    KWindowInfo *info = new KWindowInfo( window );
    info->autoDel = true;
    info->message( text, pix, timeout );
}

void KWindowInfo::message( const QString &text )
{
    message( text, QPixmap(), UNSPECIFIED_TIMEOUT );
}

void KWindowInfo::message( const QString &text, const QPixmap &pix )
{
    message( text, pix, UNSPECIFIED_TIMEOUT );
}

void KWindowInfo::message( const QString &text, int timeout )
{
    message( text, QPixmap(), timeout );
}

void KWindowInfo::message( const QString &text, const QPixmap &pix, int timeout )
{
    if ( timeout != 0 )
	save();

    display( text, pix );

    if ( timeout < 0 )
	timeout = DEFAULT_MESSAGE_TIMEOUT;
    if ( timeout != 0 )
	QTimer::singleShot( timeout, this, SLOT( restore() ) );
}

void KWindowInfo::permanent( const QString &text )
{
#ifdef Q_WS_X11
    oldMiniIcon = KWin::icon( win->winId(), 16, 16, true );
    oldIcon = KWin::icon( win->winId(), 34, 34, false );
    if ( oldIcon.isNull() )
	oldIcon = KWin::icon( win->winId(), 32, 32, true );
#endif

    permanent( text, oldIcon );
}

void KWindowInfo::permanent( const QString &text, const QPixmap &pix )
{
    if ( !oldText.isNull() ) {
	QObjectList *l = queryList( "QTimer" );
	QObjectListIt it( *l );
	QObject *obj;

	while ( (obj = it.current()) != 0 ) {
	    ++it;
	    delete obj;
	}
	delete l;
    }

    oldText = QString::null;
    display( text, pix );
}

void KWindowInfo::display( const QString &text, const QPixmap &pix )
{
    QPixmap icon;
    if ( pix.isNull() )
	icon.load( "bell.png" );
    else
	icon = pix;

    if ( win->inherits( "KSystemTray" ) ) {
	KSystemTray *tray = static_cast<KSystemTray *>( win );
	tray->setPixmap( icon );
	QToolTip::add( tray, text );
	return;
    }

    win->setCaption( text );
    win->setIcon( icon );
#ifdef Q_WS_X11
    KWin::setIcons( win->winId(), icon, icon );
#endif
}

void KWindowInfo::save()
{
    if ( !oldText.isNull() )
	return;

    if ( win->inherits( "KSystemTray" ) ) {
	KSystemTray *tray = static_cast<KSystemTray *>( win );
	oldIcon = *(tray->pixmap());
	oldText = QToolTip::textFor( tray );
	return;
    }

    oldText = win->caption();
#ifdef Q_WS_X11
    oldMiniIcon = KWin::icon( win->winId(), 16, 16, true );
    oldIcon = KWin::icon( win->winId(), 34, 34, false );
    if ( oldIcon.isNull() )
	oldIcon = KWin::icon( win->winId(), 32, 32, true );
#endif

    if ( oldIcon.isNull() ) {
	const QPixmap *px = win->icon();
	if ( px )
	    oldIcon = *px;
	else
	    oldIcon.resize( 0, 0 );
    }
}

void KWindowInfo::restore()
{
    if ( win->inherits( "KSystemTray" ) ) {
	KSystemTray *tray = static_cast<KSystemTray *>( win );
	tray->setPixmap( oldIcon );
	QToolTip::add( tray, oldText );
	oldText = QString::null;
	return;
    }

    win->setIcon( oldIcon );
#ifdef Q_WS_X11
    KWin::setIcons( win->winId(), oldIcon, oldMiniIcon );
#endif
    win->setCaption( oldText );
    oldText = QString::null;

    if ( autoDel )
	delete this;
}