kdeui Library API Documentation

krootpixmap.cpp

00001 /* vi: ts=8 sts=4 sw=4
00002  *
00003  * $Id: krootpixmap.cpp,v 1.23.2.1 2004/03/15 16:54:02 lunakl Exp $
00004  *
00005  * This file is part of the KDE project, module kdeui.
00006  * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
00007  *
00008  * You can Freely distribute this program under the GNU Library
00009  * General Public License. See the file "COPYING.LIB" for the exact
00010  * licensing terms.
00011  */
00012 
00013 #include <qwidget.h>
00014 #include <qtimer.h>
00015 #include <qrect.h>
00016 #include <qimage.h>
00017 
00018 #ifndef Q_WS_QWS //FIXME
00019 #include <kapplication.h>
00020 #include <kimageeffect.h>
00021 #include <kpixmapio.h>
00022 #include <kwinmodule.h>
00023 #include <kwin.h>
00024 #include <kdebug.h>
00025 #include <netwm.h>
00026 #include <dcopclient.h>
00027 
00028 #include <ksharedpixmap.h>
00029 #include <krootpixmap.h>
00030 
00031 class KRootPixmapData
00032 {
00033 public:
00034     QWidget *toplevel;
00035     KWinModule *kwin;
00036 };
00037 
00038 
00039 KRootPixmap::KRootPixmap( QWidget *widget, const char *name )
00040     : QObject(widget, name ? name : "KRootPixmap" ), m_pWidget(widget)
00041 {
00042     init();
00043 }
00044 
00045 KRootPixmap::KRootPixmap( QWidget *widget, QObject *parent, const char *name )
00046     : QObject( parent, name ? name : "KRootPixmap" ), m_pWidget(widget)
00047 {
00048     init();
00049 }
00050 
00051 void KRootPixmap::init()
00052 {
00053     d = new KRootPixmapData;
00054     m_Fade = 0;
00055     m_pPixmap = new KSharedPixmap;
00056     m_pTimer = new QTimer( this );
00057     m_bInit = false;
00058     m_bActive = false;
00059     m_bCustomPaint = false;
00060 
00061     connect(kapp, SIGNAL(backgroundChanged(int)), SLOT(slotBackgroundChanged(int)));
00062     connect(m_pPixmap, SIGNAL(done(bool)), SLOT(slotDone(bool)));
00063     connect(m_pTimer, SIGNAL(timeout()), SLOT(repaint()));
00064 
00065     d->kwin = new KWinModule( this );
00066     connect( d->kwin, SIGNAL(currentDesktopChanged(int)), SLOT(desktopChanged(int)) );
00067 
00068     d->toplevel = m_pWidget->topLevelWidget();
00069     d->toplevel->installEventFilter(this);
00070 }
00071 
00072 KRootPixmap::~KRootPixmap()
00073 {
00074     delete m_pPixmap;
00075     delete d;
00076 }
00077 
00078 
00079 int KRootPixmap::currentDesktop() const
00080 {
00081     NETRootInfo rinfo( qt_xdisplay(), NET::CurrentDesktop );
00082     rinfo.activate();
00083     return rinfo.currentDesktop();
00084 }
00085 
00086 
00087 void KRootPixmap::start()
00088 {
00089     if (m_bActive)
00090     return;
00091 
00092     m_bActive = true;
00093     if ( !isAvailable() )
00094     {
00095     // We will get a KIPC message when the shared pixmap is available.
00096     enableExports();
00097     return;
00098     }
00099     if (m_bInit)
00100     repaint(true);
00101 }
00102 
00103 
00104 void KRootPixmap::stop()
00105 {
00106     m_bActive = false;
00107     m_pTimer->stop();
00108 }
00109 
00110 
00111 void KRootPixmap::setFadeEffect(double fade, const QColor &color)
00112 {
00113     if (fade < 0)
00114     m_Fade = 0;
00115     else if (fade > 1)
00116     m_Fade = 1;
00117     else
00118     m_Fade = fade;
00119     m_FadeColor = color;
00120 
00121     if ( m_bActive && m_bInit ) repaint(true);
00122 }
00123 
00124 
00125 bool KRootPixmap::eventFilter(QObject *, QEvent *event)
00126 {
00127     // Initialise after the first show or paint event on the managed widget.
00128     if (!m_bInit && ((event->type() == QEvent::Show) || (event->type() == QEvent::Paint)))
00129     {
00130     m_bInit = true;
00131     m_Desk = currentDesktop();
00132     }
00133 
00134     if (!m_bActive)
00135     return false;
00136 
00137     switch (event->type())
00138     {
00139     case QEvent::Resize:
00140     case QEvent::Move:
00141     m_pTimer->start(100, true);
00142     break;
00143 
00144     case QEvent::Paint:
00145     m_pTimer->start(0, true);
00146     break;
00147 
00148     case QEvent::Reparent:
00149         d->toplevel->removeEventFilter(this);
00150         d->toplevel = m_pWidget->topLevelWidget();
00151         d->toplevel->installEventFilter(this);
00152         break;
00153 
00154     default:
00155     break;
00156     }
00157 
00158     return false; // always continue processing
00159 }
00160 
00161 void KRootPixmap::desktopChanged( int desk )
00162 {
00163     if( !m_pWidget->isVisible())
00164         return; // not visible, no need to update
00165     QWidget* widget = m_pWidget->topLevelWidget();
00166     if( !widget->testWFlags( WX11BypassWM )
00167         && !KWin::windowInfo( widget->winId(), NET::WMDesktop ).isOnCurrentDesktop())
00168         return; // not on current desktop -> not visible, no need to update
00169     repaint(true);
00170 }
00171 
00172 void KRootPixmap::repaint()
00173 {
00174     repaint(false);
00175 }
00176 
00177 
00178 void KRootPixmap::repaint(bool force)
00179 {
00180     QPoint p1 = m_pWidget->mapToGlobal(m_pWidget->rect().topLeft());
00181     QPoint p2 = m_pWidget->mapToGlobal(m_pWidget->rect().bottomRight());
00182     if (!force && (m_Rect == QRect(p1, p2)))
00183     return;
00184 
00185     // Due to northwest bit gravity, we don't need to do anything if the
00186     // bottom right corner of the widget is moved inward.
00187     // That said, konsole clears the background when it is resized, so
00188     // we have to reset the background pixmap.
00189     if ((p1 == m_Rect.topLeft()) && (m_pWidget->width() < m_Rect.width()) &&
00190     (m_pWidget->height() < m_Rect.height())
00191        )
00192     {
00193     updateBackground( m_pPixmap );
00194     return;
00195     }
00196     m_Rect = QRect(p1, p2);
00197     m_Desk = currentDesktop();
00198 
00199     // KSharedPixmap will correctly generate a tile for us.
00200     m_pPixmap->loadFromShared(pixmapName(m_Desk), m_Rect);
00201 }
00202 
00203 bool KRootPixmap::isAvailable() const
00204 {
00205     return m_pPixmap->isAvailable(pixmapName(m_Desk));
00206 }
00207 
00208 QString KRootPixmap::pixmapName(int desk) {
00209     QString pattern = QString("DESKTOP%1");
00210     int screen_number = DefaultScreen(qt_xdisplay());
00211     if (screen_number) {
00212         pattern = QString("SCREEN%1-DESKTOP").arg(screen_number) + "%1";
00213     }
00214     return pattern.arg( desk );
00215 }
00216 
00217 
00218 void KRootPixmap::enableExports()
00219 {
00220     kdDebug(270) << k_lineinfo << "activating background exports.\n";
00221     DCOPClient *client = kapp->dcopClient();
00222     if (!client->isAttached())
00223     client->attach();
00224     QByteArray data;
00225     QDataStream args( data, IO_WriteOnly );
00226     args << 1;
00227 
00228     QCString appname( "kdesktop" );
00229     int screen_number = DefaultScreen(qt_xdisplay());
00230     if ( screen_number )
00231         appname.sprintf("kdesktop-screen-%d", screen_number );
00232 
00233     client->send( appname, "KBackgroundIface", "setExport(int)", data );
00234 }
00235 
00236 
00237 void KRootPixmap::slotDone(bool success)
00238 {
00239     if (!success)
00240     {
00241     kdWarning(270) << k_lineinfo << "loading of desktop background failed.\n";
00242     return;
00243     }
00244 
00245     // We need to test active as the pixmap might become available
00246     // after the widget has been destroyed.
00247     if ( m_bActive )
00248     updateBackground( m_pPixmap );
00249 }
00250 
00251 void KRootPixmap::updateBackground( KSharedPixmap *spm )
00252 {
00253     QPixmap pm = *spm;
00254 
00255     if (m_Fade > 1e-6)
00256     {
00257     KPixmapIO io;
00258     QImage img = io.convertToImage(pm);
00259     img = KImageEffect::fade(img, m_Fade, m_FadeColor);
00260     pm = io.convertToPixmap(img);
00261     }
00262 
00263     if ( !m_bCustomPaint )
00264     m_pWidget->setBackgroundPixmap( pm );
00265     else {
00266     emit backgroundUpdated( pm );
00267     }
00268 }
00269 
00270 
00271 void KRootPixmap::slotBackgroundChanged(int desk)
00272 {
00273     if (!m_bInit || !m_bActive)
00274     return;
00275 
00276     if (desk == m_Desk)
00277     repaint(true);
00278 }
00279 
00280 #include "krootpixmap.moc"
00281 #endif
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jan 21 09:57:28 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003