# kdeui Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:57:31 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001