kdeui Library API Documentation

kpassivepopup.cpp

00001 /*
00002  *   copyright            : (C) 2001-2002 by Richard Moore
00003  *   License              : This file is released under the terms of the LGPL, version 2.
00004  *   email                : rich@kde.org
00005  */
00006 
00007 #include <kconfig.h>
00008 
00009 #include <qapplication.h>
00010 #include <qlabel.h>
00011 #include <qlayout.h>
00012 #include <qtimer.h>
00013 #include <qvbox.h>
00014 
00015 #include <kdebug.h>
00016 #include <kdialog.h>
00017 #include <kpixmap.h>
00018 #include <kpixmapeffect.h>
00019 #include <kglobalsettings.h>
00020 
00021 #include "config.h"
00022 #ifdef Q_WS_X11
00023 #include <netwm.h> 
00024 #endif
00025 
00026 #include "kpassivepopup.h"
00027 #include "kpassivepopup.moc"
00028 
00029 static const int DEFAULT_POPUP_TIME = 6*1000;
00030 static const int POPUP_FLAGS = Qt::WStyle_Customize | Qt::WDestructiveClose | Qt::WX11BypassWM
00031                              | Qt::WStyle_StaysOnTop | Qt::WStyle_Tool | Qt::WStyle_NoBorder;
00032 
00033 
00034 KPassivePopup::KPassivePopup( QWidget *parent, const char *name, WFlags f )
00035     : QFrame( 0, name, f ? f : POPUP_FLAGS ),
00036       window( parent ? parent->winId() : 0L ), msgView( 0 ), topLayout( 0 ),
00037       hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new QTimer( this, "hide_timer" ) ), m_autoDelete( false ), d( 0 )
00038 {
00039     init();
00040 }
00041 
00042 KPassivePopup::KPassivePopup( WId win, const char *name, WFlags f )
00043     : QFrame( 0, name, f ? f : POPUP_FLAGS ),
00044       window( win ), msgView( 0 ), topLayout( 0 ),
00045       hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new QTimer( this, "hide_timer" ) ), m_autoDelete( false ), d( 0 )
00046 {
00047     init();
00048 }
00049 
00050 void KPassivePopup::init()
00051 {
00052     setFrameStyle( QFrame::Box| QFrame::Plain );
00053     setLineWidth( 2 );
00054     connect( hideTimer, SIGNAL( timeout() ), SLOT( hide() ) );
00055     connect( this, SIGNAL( clicked() ), SLOT( hide() ) );
00056 }
00057 
00058 KPassivePopup::~KPassivePopup()
00059 {
00060 }
00061 
00062 void KPassivePopup::setView( QWidget *child )
00063 {
00064     delete msgView;
00065     msgView = child;
00066 
00067     delete topLayout;
00068     topLayout = new QVBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() );
00069     topLayout->addWidget( msgView );
00070     topLayout->activate();
00071 }
00072 
00073 void KPassivePopup::setView( const QString &caption, const QString &text,
00074                              const QPixmap &icon )
00075 {
00076     // kdDebug() << "KPassivePopup::setView " << caption << ", " << text << endl;
00077     setView( standardView( caption, text, icon, this ) );
00078 }
00079 
00080 QVBox * KPassivePopup::standardView( const QString& caption,
00081                                      const QString& text,
00082                                      const QPixmap& icon,
00083                                      QWidget *parent )
00084 {
00085     QVBox *vb = new QVBox( parent ? parent : this );
00086     vb->setSpacing( KDialog::spacingHint() );
00087 
00088     QHBox *hb=0;
00089     if ( !icon.isNull() ) {
00090     hb = new QHBox( vb );
00091     hb->setMargin( 0 );
00092     hb->setSpacing( KDialog::spacingHint() );
00093     ttlIcon = new QLabel( hb, "title_icon" );
00094     ttlIcon->setPixmap( icon );
00095         ttlIcon->setAlignment( AlignLeft );
00096     }
00097 
00098     if ( !caption.isEmpty() ) {
00099     ttl = new QLabel( caption, hb ? hb : vb, "title_label" );
00100     QFont fnt = ttl->font();
00101     fnt.setBold( true );
00102     ttl->setFont( fnt );
00103     ttl->setAlignment( Qt::AlignHCenter );
00104         if ( hb )
00105             hb->setStretchFactor( ttl, 10 ); // enforce centering
00106     }
00107 
00108     if ( !text.isEmpty() ) {
00109         msg = new QLabel( text, vb, "msg_label" );
00110         msg->setAlignment( AlignLeft );
00111     }
00112 
00113     return vb;
00114 }
00115 
00116 void KPassivePopup::setView( const QString &caption, const QString &text )
00117 {
00118     setView( caption, text, QPixmap() );
00119 }
00120 
00121 void KPassivePopup::setTimeout( int delay )
00122 {
00123     hideDelay = delay;
00124     if( hideTimer->isActive() )
00125         hideTimer->changeInterval( delay );
00126 }
00127 
00128 void KPassivePopup::setAutoDelete( bool autoDelete )
00129 {
00130     m_autoDelete = autoDelete;
00131 }
00132 
00133 void KPassivePopup::mouseReleaseEvent( QMouseEvent *e )
00134 {
00135     emit clicked();
00136     emit clicked( e->pos() );
00137 }
00138 
00139 //
00140 // Main Implementation
00141 //
00142 
00143 void KPassivePopup::show()
00144 {
00145     if ( size() != sizeHint() )
00146     resize( sizeHint() );
00147 
00148     positionSelf();
00149     QFrame::show();
00150 
00151     int delay = hideDelay;
00152     if ( delay < 0 )
00153     delay = DEFAULT_POPUP_TIME;
00154 
00155     if ( delay > 0 ) {
00156     hideTimer->start( delay );
00157     }
00158 }
00159 
00160 void KPassivePopup::hideEvent( QHideEvent * )
00161 {
00162     hideTimer->stop();
00163     if ( m_autoDelete )
00164         deleteLater();
00165 }
00166 
00167 QRect KPassivePopup::defaultArea() const
00168 {
00169 #ifdef Q_WS_X11
00170     NETRootInfo info( qt_xdisplay(),
00171                       NET::NumberOfDesktops |
00172                       NET::CurrentDesktop |
00173                       NET::WorkArea,
00174                       -1, false );
00175     info.activate();
00176     NETRect workArea = info.workArea( info.currentDesktop() );
00177     QRect r;
00178     r.setRect( workArea.pos.x, workArea.pos.y, 0, 0 ); // top left
00179 #else
00180     // FIX IT
00181     QRect r;
00182     r.setRect( 100, 100, 200, 200 ); // top left
00183 #endif
00184     return r;
00185 }
00186 
00187 void KPassivePopup::positionSelf()
00188 {
00189     QRect target;
00190 
00191 #ifdef Q_WS_X11
00192     if ( !window ) {
00193         target = defaultArea();
00194     }
00195 
00196     else {
00197         NETWinInfo ni( qt_xdisplay(), window, qt_xrootwin(),
00198                        NET::WMIconGeometry | NET::WMKDESystemTrayWinFor );
00199 
00200         // Figure out where to put the popup. Note that we must handle
00201         // windows that skip the taskbar cleanly
00202         if ( ni.kdeSystemTrayWinFor() ) {
00203             NETRect frame, win;
00204             ni.kdeGeometry( frame, win );
00205             target.setRect( win.pos.x, win.pos.y,
00206                             win.size.width, win.size.height );
00207         }
00208         else if ( ni.state() & NET::SkipTaskbar ) {
00209             target = defaultArea();
00210         }
00211         else {
00212             NETRect r = ni.iconGeometry();
00213             target.setRect( r.pos.x, r.pos.y, r.size.width, r.size.height );
00214                 if ( target.isNull() ) { // bogus value, use the exact position
00215                     NETRect dummy;
00216                     ni.kdeGeometry( dummy, r );
00217                     target.setRect( r.pos.x, r.pos.y, 
00218                                     r.size.width, r.size.height);
00219                 }
00220         }
00221     }
00222 #else
00223         target = defaultArea();
00224 #endif
00225     moveNear( target );
00226 }
00227 
00228 void KPassivePopup::moveNear( QRect target )
00229 {
00230     QPoint pos = target.topLeft();
00231     int x = pos.x();
00232     int y = pos.y();
00233     int w = width();
00234     int h = height();
00235 
00236     QRect r = KGlobalSettings::desktopGeometry(QPoint(x+w/2,y+h/2));
00237 
00238     if ( x < r.center().x() )
00239     x = x + target.width();
00240     else
00241     x = x - w;
00242 
00243     // It's apparently trying to go off screen, so display it ALL at the bottom.
00244     if ( (y + h) > r.bottom() )
00245     y = r.bottom() - h;
00246 
00247     if ( (x + w) > r.right() )
00248     x = r.right() - w;
00249 
00250     if ( y < r.top() )
00251         y = r.top();
00252 
00253     if ( x < r.left() )
00254     x = r.left();
00255 
00256     move( x, y );
00257 }
00258 
00259 //
00260 // Convenience Methods
00261 //
00262 
00263 KPassivePopup *KPassivePopup::message( const QString &caption, const QString &text,
00264                        const QPixmap &icon,
00265                        QWidget *parent, const char *name, int timeout )
00266 {
00267     KPassivePopup *pop = new KPassivePopup( parent, name );
00268     pop->setAutoDelete( true );
00269     pop->setView( caption, text, icon );
00270     pop->hideDelay = timeout;
00271     pop->show();
00272 
00273     return pop;
00274 }
00275 
00276 KPassivePopup *KPassivePopup::message( const QString &text, QWidget *parent, const char *name )
00277 {
00278     return message( QString::null, text, QPixmap(), parent, name );
00279 }
00280 
00281 KPassivePopup *KPassivePopup::message( const QString &caption, const QString &text,
00282                        QWidget *parent, const char *name )
00283 {
00284     return message( caption, text, QPixmap(), parent, name );
00285 }
00286 
00287 KPassivePopup *KPassivePopup::message( const QString &caption, const QString &text,
00288                        const QPixmap &icon, WId parent, const char *name, int timeout )
00289 {
00290     KPassivePopup *pop = new KPassivePopup( parent, name );
00291     pop->setAutoDelete( true );
00292     pop->setView( caption, text, icon );
00293     pop->hideDelay = timeout;
00294     pop->show();
00295 
00296     return pop;
00297 }
00298 
00299 // Local Variables:
00300 // c-basic-offset: 4
00301 // End:
00302 
00303 
KDE Logo
This file is part of the documentation for kdeui Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 15 10:27:02 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003