[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    [Panel-devel] Starting work on background
From:       Sean Harmer <sh () theharmers ! co ! uk>
Date:       2007-10-26 23:42:46
Message-ID: 200710270042.46526.sh () theharmers ! co ! uk
[Download RAW message or body]

Hi Aaron,

I've spent the evening looking at the plasma source code as promised and have 
started hacking around with the background drawing code. Please find attached 
a patch that refactors the current background rendering code out of 
Plasma::Containment and into DefaultBackground. I've followed the suggestion 
in the comments and have moved the loading/scaling code into 
defaultBackground::constraintsUpdated().

Is this OK to commit? If this is OK I'll start to look at optimising the 
blitting of regions of SVG's rather than painting the whole thing every time.

Kind regards,

Sean

["move-to-background.patch" (text/x-diff)]

Index: workspace/libs/plasma/containment.cpp
===================================================================
--- workspace/libs/plasma/containment.cpp	(revision 729756)
+++ workspace/libs/plasma/containment.cpp	(working copy)
@@ -55,8 +55,6 @@
         : formFactor(Planar),
           location(Floating),
           layout(0),
-          background(0),
-          bitmapBackground(0),
           screen(0),
           immutable(false)
     {
@@ -67,16 +65,12 @@
         qDeleteAll(applets);
         applets.clear();
         delete layout;
-        delete bitmapBackground;
     }
 
     FormFactor formFactor;
     Location location;
     Layout* layout;
     Applet::List applets;
-    Plasma::Svg *background;
-    QPixmap* bitmapBackground;
-    QString wallpaperPath;
     QSize size;
     int screen;
     bool immutable;
@@ -108,18 +102,6 @@
     setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
     setAcceptDrops(true);
 
-    if (type() == DesktopContainment) {
-        KConfigGroup config(KGlobal::config(), "General");
-        d->wallpaperPath = config.readEntry("wallpaper", \
                KStandardDirs::locate("wallpaper", "plasma-default.png"));
-
-        kDebug() << "wallpaperPath is" << d->wallpaperPath << \
                QFile::exists(d->wallpaperPath);
-        if (d->wallpaperPath.isEmpty() ||
-            !QFile::exists(d->wallpaperPath)) {
-            kDebug() << "SVG wallpaper!";
-            d->background = new Plasma::Svg("widgets/wallpaper", this);
-        }
-    }
-
     //TODO: would be nice to not do this on init, as it causes Phase to init
     connect(Phase::self(), \
                SIGNAL(animationComplete(QGraphicsItem*,Plasma::Phase::Animation)),
             this, SLOT(appletDisappearComplete(QGraphicsItem*,Plasma::Phase::Animation)));
 @@ -150,50 +132,8 @@
                                  const QStyleOptionGraphicsItem *option,
                                  const QRect& contentsRect)
 {
-    //FIXME: this should probably ALL move to the Desktop containment, save for \
                drawing a rect
-    //       in case there is no other drawing going on
-    if (type() != DesktopContainment) {
-        return;
-    }
-
-    //kDebug() << "paintInterface of background";
-    //TODO: we should have a way to do this outside of the paint event!
-    if (d->background) {
-        d->background->resize(contentsRect.size());
-    } else if (!d->wallpaperPath.isEmpty()) {
-        if (!d->bitmapBackground || !(d->bitmapBackground->size() == \
                contentsRect.size())) {
-            delete d->bitmapBackground;
-            d->bitmapBackground = new QPixmap(d->wallpaperPath);
-            (*d->bitmapBackground) = \
                d->bitmapBackground->scaled(contentsRect.size());
-        }
-    } else {
-        // got nothing to paint!
-        //kDebug() << "got nothing?";
-        painter->drawRect(contentsRect.adjusted(1, 1, -1, -1));
-        return;
-    }
-
-    // draw the background untransformed (saves lots of per-pixel-math)
-    painter->save();
-    painter->resetTransform();
-
-    // blit the background (saves all the per-pixel-products that blending does)
-    painter->setCompositionMode(QPainter::CompositionMode_Source);
-
-    if (d->background) {
-        // Plasma::Svg doesn't support drawing only part of the image (it only
-        // supports drawing the whole image to a rect), so we blit to 0,0-w,h
-        d->background->paint(painter, contentsRect);
-    //kDebug() << "draw svg of background";
-    } else if (d->bitmapBackground) {
-        // for pixmaps we draw only the exposed part (untransformed since the
-        // bitmapBackground already has the size of the viewport)
-        painter->drawPixmap(option->exposedRect, *d->bitmapBackground, \
                option->exposedRect);
-    //kDebug() << "draw pixmap of background";
-    }
-
-    // restore transformation and composition mode
-    painter->restore();
+    // If the derived class can't be bothered - just draw a plane old rectangle
+    painter->drawRect(contentsRect.adjusted(1, 1, -1, -1));
 }
 
 QSizeF Containment::contentSizeHint() const
Index: workspace/plasma/containments/desktop/desktop.h
===================================================================
--- workspace/plasma/containments/desktop/desktop.h	(revision 729756)
+++ workspace/plasma/containments/desktop/desktop.h	(working copy)
@@ -22,6 +22,7 @@
 #include <QGraphicsItem>
 #include <QList>
 #include <QObject>
+#include <QStyleOptionGraphicsItem>
 
 #include <KIcon>
 
@@ -35,6 +36,7 @@
 namespace Plasma
 {
     class AppletBrowser;
+    class Svg;
 }
 
 /*class Tool : public QObject, public QGraphicsItem
@@ -89,6 +91,14 @@
 
     QList<QAction*> contextActions();
 
+    /**
+     * Paints a default background image. Nothing fancy, but that's what plugins
+     * are for. Reimplemented from Plasma::Containment;
+     */
+    void paintInterface(QPainter *painter,
+                        const QStyleOptionGraphicsItem *option,
+                        const QRect& contentsRect);
+
 signals:
     void zoomIn();
     void zoomOut();
@@ -108,6 +118,9 @@
     QAction *m_logoutAction;
     ToolBox *m_toolbox;
     Plasma::AppletBrowser *m_appletBrowser;
+    Plasma::Svg *m_background;
+    QPixmap* m_bitmapBackground;
+    QString m_wallpaperPath;
 };
 
 #endif // PLASMA_PANEL_H
Index: workspace/plasma/containments/desktop/desktop.cpp
===================================================================
--- workspace/plasma/containments/desktop/desktop.cpp	(revision 729756)
+++ workspace/plasma/containments/desktop/desktop.cpp	(working copy)
@@ -19,16 +19,20 @@
 #include "desktop.h"
 
 #include <QAction>
+#include <QDesktopWidget>
+#include <QFile>
 #include <QPainter>
 #include <QTimeLine>
 
 #include <KAuthorized>
 #include <KDebug>
 #include <KRun>
+#include <KStandardDirs>
 #include <KWindowSystem>
 
 #include "plasma/appletbrowser.h"
 #include "plasma/phase.h"
+#include "plasma/svg.h"
 #include "plasma/widgets/pushbutton.h"
 #include "workspace/kworkspace.h"
 
@@ -112,7 +116,7 @@
         tool->show();
         phase->moveItem(tool, Plasma::Phase::SlideIn, QPoint(x, y));
         //x += 0;
-        y += tool->boundingRect().height() + 5;
+        y += static_cast<int>(tool->boundingRect().height()) + 5;
     }
 
     if (m_animId) {
@@ -145,9 +149,9 @@
 void ToolBox::animate(qreal progress)
 {
     if (m_showing) {
-        m_animFrame = m_size * progress;
+        m_animFrame = static_cast<int>(m_size * progress);
     } else {
-        m_animFrame = m_size * (1 - progress);
+        m_animFrame = static_cast<int>(m_size * (1.0 - progress));
     }
 
     //kDebug() << "animating at" << progress << "for" << m_animFrame;
@@ -188,7 +192,10 @@
       m_lockAction(0),
       m_logoutAction(0),
       m_toolbox(0),
-      m_appletBrowser(0)
+      m_appletBrowser(0),
+      m_background(0),
+      m_bitmapBackground(0),
+      m_wallpaperPath(0)
 {
     kDebug() << "!!! loading desktop";
 }
@@ -199,6 +206,16 @@
 
 void DefaultDesktop::init()
 {
+    KConfigGroup config(KGlobal::config(), "General");
+    m_wallpaperPath = config.readEntry("wallpaper", \
KStandardDirs::locate("wallpaper", "plasma-default.png")); +
+    kDebug() << "wallpaperPath is" << m_wallpaperPath << \
QFile::exists(m_wallpaperPath); +    if (m_wallpaperPath.isEmpty() ||
+        !QFile::exists(m_wallpaperPath)) {
+        kDebug() << "SVG wallpaper!";
+        m_background = new Plasma::Svg("widgets/wallpaper", this);
+        }
+
     Containment::init();
     m_toolbox = new ToolBox(this);
     //m_toolbox->updateGeometry();
@@ -227,9 +244,24 @@
 
 void DefaultDesktop::constraintsUpdated(Plasma::Constraints constraints)
 {
+    //kDebug() << "DefaultDesktop constraints have changed";
     if (constraints & Plasma::ScreenConstraint && m_toolbox) {
         m_toolbox->setPos(geometry().width() - m_toolbox->boundingRect().width(), \
0);  }
+
+    QDesktopWidget desktop;
+    const QRect geom = desktop.screenGeometry(screen());
+    if (m_background) {
+        kDebug() << "Rescaling SVG wallpaper to" << geom.size();
+        m_background->resize(geom.size());
+    } else if (!m_wallpaperPath.isEmpty()) {
+        if (!m_bitmapBackground || !(m_bitmapBackground->size() == geom.size())) {
+            kDebug() << "Loading and scaling bitmap wallpaper to" << geom.size();
+            delete m_bitmapBackground;
+            m_bitmapBackground = new QPixmap(m_wallpaperPath);
+            (*m_bitmapBackground) = m_bitmapBackground->scaled(geom.size());
+        }
+    }
 }
 
 void DefaultDesktop::launchExplorer()
@@ -339,6 +371,38 @@
     }
 }
 
+void DefaultDesktop::paintInterface(QPainter *painter,
+                                    const QStyleOptionGraphicsItem *option,
+                                    const QRect& contentsRect)
+{
+    //kDebug() << "paintInterface of background";
+    if (!m_background && !m_bitmapBackground) {
+        Containment::paintInterface(painter, option, contentsRect);
+    }
+
+    // draw the background untransformed (saves lots of per-pixel-math)
+    painter->save();
+    painter->resetTransform();
+
+    // blit the background (saves all the per-pixel-products that blending does)
+    painter->setCompositionMode(QPainter::CompositionMode_Source);
+
+    if (m_background) {
+        // Plasma::Svg doesn't support drawing only part of the image (it only
+        // supports drawing the whole image to a rect), so we blit to 0,0-w,h
+        m_background->paint(painter, contentsRect);
+    //kDebug() << "draw svg of background";
+    } else if (m_bitmapBackground) {
+        // for pixmaps we draw only the exposed part (untransformed since the
+        // bitmapBackground already has the size of the viewport)
+        painter->drawPixmap(option->exposedRect, *m_bitmapBackground, \
option->exposedRect); +    //kDebug() << "draw pixmap of background";
+    }
+
+    // restore transformation and composition mode
+    painter->restore();
+}
+
 K_EXPORT_PLASMA_APPLET(desktop, DefaultDesktop)
 
 #include "desktop.moc"



_______________________________________________
Panel-devel mailing list
Panel-devel@kde.org
https://mail.kde.org/mailman/listinfo/panel-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic