kdeui Library API Documentation

krootpixmap.cpp

00001 /* vi: ts=8 sts=4 sw=4
00002  *
00003  * $Id: krootpixmap.cpp,v 1.18 2002/08/04 14:07:53 coolo 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 <kdebug.h>
00024 #include <netwm.h>
00025 #include <dcopclient.h>
00026 
00027 #include <ksharedpixmap.h>
00028 #include <krootpixmap.h>
00029 
00030 
00031 KRootPixmap::KRootPixmap( QWidget *widget, const char *name )
00032     : QObject(widget, name ? name : "KRootPixmap" ), m_pWidget(widget)
00033 {
00034     init();
00035 }
00036 
00037 KRootPixmap::KRootPixmap( QWidget *widget, QObject *parent, const char *name )
00038     : QObject( parent, name ? name : "KRootPixmap" ), m_pWidget(widget)
00039 {
00040     init();
00041 }
00042 
00043 void KRootPixmap::init()
00044 {
00045     m_Fade = 0;
00046     m_pPixmap = new KSharedPixmap;
00047     m_pTimer = new QTimer( this );
00048     m_bInit = false;
00049     m_bActive = false;
00050     m_bCustomPaint = false;
00051 
00052     connect(kapp, SIGNAL(backgroundChanged(int)), SLOT(slotBackgroundChanged(int)));
00053     connect(m_pPixmap, SIGNAL(done(bool)), SLOT(slotDone(bool)));
00054     connect(m_pTimer, SIGNAL(timeout()), SLOT(repaint()));
00055 
00056     QObject *obj = m_pWidget;
00057     while (obj->parent())
00058         obj = obj->parent();
00059     obj->installEventFilter(this);
00060 }
00061 
00062 KRootPixmap::~KRootPixmap()
00063 {
00064     delete m_pPixmap;
00065 }
00066 
00067 
00068 int KRootPixmap::currentDesktop() const
00069 {
00070     NETRootInfo rinfo( qt_xdisplay(), NET::CurrentDesktop );
00071     rinfo.activate();
00072     return rinfo.currentDesktop();
00073 }
00074 
00075 
00076 void KRootPixmap::start()
00077 {
00078     if (m_bActive)
00079         return;
00080 
00081     m_bActive = true;
00082     if ( !isAvailable() )
00083     {
00084         // We will get a KIPC message when the shared pixmap is available.
00085         enableExports();
00086         return;
00087     }
00088     if (m_bInit)
00089         repaint(true);
00090 }
00091 
00092 
00093 void KRootPixmap::stop()
00094 {
00095     m_bActive = false;
00096     m_pTimer->stop();
00097 }
00098 
00099 
00100 void KRootPixmap::setFadeEffect(double fade, const QColor &color)
00101 {
00102     if (fade < 0)
00103         m_Fade = 0;
00104     else if (fade > 1)
00105         m_Fade = 1;
00106     else
00107         m_Fade = fade;
00108     m_FadeColor = color;
00109 
00110     if ( m_bActive && m_bInit ) repaint(true);
00111 }
00112 
00113 
00114 bool KRootPixmap::eventFilter(QObject *, QEvent *event)
00115 {
00116     // Initialise after the first show or paint event on the managed widget.
00117     if (!m_bInit && ((event->type() == QEvent::Show) || (event->type() == QEvent::Paint)))
00118     {
00119         m_bInit = true;
00120         m_Desk = currentDesktop();
00121     }
00122 
00123     if (!m_bActive)
00124         return false;
00125 
00126     switch (event->type())
00127     {
00128     case QEvent::Resize:
00129     case QEvent::Move:
00130         m_pTimer->start(100, true);
00131         break;
00132 
00133     case QEvent::Paint:
00134         m_pTimer->start(0, true);
00135         break;
00136 
00137     default:
00138         break;
00139     }
00140 
00141     return false; // always continue processing
00142 }
00143 
00144 
00145 void KRootPixmap::repaint()
00146 {
00147     repaint(false);
00148 }
00149 
00150 
00151 void KRootPixmap::repaint(bool force)
00152 {
00153     QPoint p1 = m_pWidget->mapToGlobal(m_pWidget->rect().topLeft());
00154     QPoint p2 = m_pWidget->mapToGlobal(m_pWidget->rect().bottomRight());
00155     if (!force && (m_Rect == QRect(p1, p2)))
00156         return;
00157 
00158     // Due to northwest bit gravity, we don't need to do anything if the
00159     // bottom right corner of the widget is moved inward.
00160     // That said, konsole clears the background when it is resized, so
00161     // we have to reset the background pixmap.
00162     if ((p1 == m_Rect.topLeft()) && (m_pWidget->width() < m_Rect.width()) &&
00163         (m_pWidget->height() < m_Rect.height())
00164        )
00165     {
00166         updateBackground( m_pPixmap );
00167         return;
00168     }
00169     m_Rect = QRect(p1, p2);
00170     m_Desk = currentDesktop();
00171 
00172     // KSharedPixmap will correctly generate a tile for us.
00173     m_pPixmap->loadFromShared(QString("DESKTOP%1").arg(m_Desk), m_Rect);
00174 }
00175 
00176 bool KRootPixmap::isAvailable() const
00177 {
00178     QString name = QString("DESKTOP%1").arg( currentDesktop() );
00179     return m_pPixmap->isAvailable(name);
00180 }
00181 
00182 
00183 void KRootPixmap::enableExports()
00184 {
00185     kdDebug(270) << k_lineinfo << "activating background exports.\n";
00186     DCOPClient *client = kapp->dcopClient();
00187     if (!client->isAttached())
00188         client->attach();
00189     QByteArray data;
00190     QDataStream args( data, IO_WriteOnly );
00191     args << 1;
00192     client->send( "kdesktop", "KBackgroundIface", "setExport(int)", data );
00193 }
00194 
00195 
00196 void KRootPixmap::slotDone(bool success)
00197 {
00198     if (!success)
00199     {
00200         kdWarning(270) << k_lineinfo << "loading of desktop background failed.\n";
00201         return;
00202     }
00203 
00204     // We need to test active as the pixmap might become available
00205     // after the widget has been destroyed.
00206     if ( m_bActive )
00207         updateBackground( m_pPixmap );
00208 }
00209 
00210 void KRootPixmap::updateBackground( KSharedPixmap *spm )
00211 {
00212     QPixmap pm = *spm;
00213 
00214     if (m_Fade > 1e-6)
00215     {
00216         KPixmapIO io;
00217         QImage img = io.convertToImage(pm);
00218         img = KImageEffect::fade(img, m_Fade, m_FadeColor);
00219         pm = io.convertToPixmap(img);
00220     }
00221 
00222     if ( !m_bCustomPaint )
00223         m_pWidget->setBackgroundPixmap( pm );
00224     else {
00225         emit backgroundUpdated( pm );
00226     }
00227 }
00228 
00229 
00230 void KRootPixmap::slotBackgroundChanged(int desk)
00231 {
00232     if (!m_bInit || !m_bActive)
00233         return;
00234 
00235     if (desk == m_Desk)
00236         repaint(true);
00237 }
00238 
00239 #include "krootpixmap.moc"
00240 #endif
KDE Logo
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:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001