thememanager.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE games kwin4 program
00003    Copyright (c) 2006 Martin Heni <kde@heni-online.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // General includes
00022 // #include <typeinfo>
00023 
00024 // Qt includes
00025 #include <QImage>
00026 #include <QPixmap>
00027 #include <QPainter>
00028 #include <QColor>
00029 #include <QRectF>
00030 
00031 // KDE includes
00032 #include <kdebug.h>
00033 #include <kstandarddirs.h>
00034 
00035 // Local includes
00036 #include "thememanager.h"
00037 
00038 // Constructor for the theme manager
00039 ThemeManager::ThemeManager(QString themefile, QObject* parent, int initialSize)
00040     : QObject(parent)
00041 {
00042   mScale = initialSize;
00043   updateTheme(themefile);
00044 }
00045 
00046 
00047 // Register an object with the manager
00048 void ThemeManager::registerTheme(Themable* ob)
00049 {
00050   mObjects[ob] = 1;
00051 }
00052 
00053 
00054 // Unregister an object from the manager
00055 void ThemeManager::unregisterTheme(Themable* ob)
00056 {
00057   mObjects.remove(ob);
00058 }
00059 
00060 
00061 // Force an refresh of the theme object given
00062 void ThemeManager::updateTheme(Themable* ob)
00063 {
00064   ob->changeTheme();
00065 }
00066 
00067 
00068 // Update the theme file and refresh all registered objects. Used 
00069 // to really change the theme.
00070 void ThemeManager::updateTheme(QString themefile)
00071 {
00072   // Empty cache
00073   mPixmapCache.clear();
00074 
00075   // Process dirs
00076   QString rcfile = KStandardDirs::locate("data", themefile);
00077   kDebug() << "ThemeManager LOAD with theme "<<rcfile << endl;
00078 
00079   // Read config and SVG file for theme
00080   mConfig = new KConfig(rcfile, KConfig::NoGlobals);
00081   QString svgfile = config("general").readEntry("svgfile");
00082   svgfile = KStandardDirs::locate("data", svgfile);
00083   kDebug() << "Reading SVG master file  = " << svgfile << endl;
00084 
00085 
00086   mRenderer = new QSvgRenderer(this);
00087   bool result = mRenderer->load(svgfile);
00088   if (!result) 
00089   {
00090     kFatal() << "Cannot open file " << svgfile << endl;
00091   }
00092   kDebug() << "Renderer " << mRenderer<<" = " << result << endl;
00093 
00094   // Notify all theme objects of a change
00095   QHashIterator<Themable*, int> it(mObjects);
00096   while (it.hasNext())
00097   {
00098       it.next();
00099       Themable* ob = it.key();
00100       ob->changeTheme();
00101   }
00102 }
00103 
00104 
00105 // Rescale the theme. Call all registed objects so that they can refresh.
00106 void ThemeManager::rescale(int scale)
00107 {
00108   if (scale==mScale) return;
00109   mScale = scale;
00110   kDebug() << "Rescale to " << scale<<endl;
00111 
00112   QHashIterator<Themable*, int> it(mObjects);
00113   while (it.hasNext())
00114   {
00115       it.next();
00116       Themable* ob = it.key();
00117       ob->changeTheme();
00118   }
00119 }
00120 
00121 
00122 // Retreive the theme's scale
00123 double ThemeManager::getScale()
00124 {
00125   return (double)mScale;
00126 }  
00127 
00128 
00129 // Retreive the current theme configuration file.
00130 KConfigGroup ThemeManager::config(QString id)
00131 {
00132    KConfigGroup grp = mConfig->group(id); 
00133    return grp;
00134 }
00135 
00136 
00137 // Get a pixmap when its size is given (this can distort the image)
00138 const QPixmap ThemeManager::getPixmap(QString svgid, QSize size)
00139 {
00140   if (size.width() < 1 || size.height() < 1) 
00141     kFatal() << "ThemeManager::getPixmap Cannot create svgid ID " << svgid << " with zero size " << size << endl;
00142   
00143   QPixmap pixmap;
00144 
00145   //  Cached pixmap?
00146   if (mPixmapCache.contains(svgid))
00147   {
00148     pixmap = mPixmapCache[svgid]; 
00149     if (pixmap.size() == size) 
00150     {
00151       return pixmap;
00152     }
00153   }
00154 
00155   // Create new image
00156   QImage image(size, QImage::Format_ARGB32_Premultiplied);
00157   image.fill(0);
00158   QPainter p(&image);
00159   mRenderer->render(&p, svgid);
00160   pixmap = QPixmap::fromImage(image);
00161   if (pixmap.isNull())
00162     kFatal() << "ThemeManager::getPixmap Cannot load svgid ID " << svgid << endl;
00163 
00164   // Cache image
00165   mPixmapCache[svgid] = pixmap;
00166 
00167   return pixmap;
00168 }
00169 
00170 
00171 // Get a pixmap when only width is given (this keeps the aspect ratio)
00172 const QPixmap ThemeManager::getPixmap(QString svgid, double width)
00173 {
00174   QRectF rect   = mRenderer->boundsOnElement(svgid);
00175   double factor = width/rect.width();
00176   QSize size    = QSize(int(width),  int(rect.height()*factor));
00177   return getPixmap(svgid, size);
00178 }
00179 
00180 
00181 // Get a pixmap with original properties and a scale factor given with respect to
00182 // another SVG item.
00183 const QPixmap ThemeManager::getPixmap(QString svgid, QString svgref, double refwidth)
00184 {
00185   QRectF refrect    = mRenderer->boundsOnElement(svgref);
00186   QRectF rect       = mRenderer->boundsOnElement(svgid);
00187   double factor     = refwidth/refrect.width();
00188   QSize size        = QSize(int(rect.width()*factor),  int(rect.height()*factor));
00189   return getPixmap(svgid, size);
00190 }
00191 
00192 
00193 // ========================== Themable interface ===============================
00194 
00195 // Constructs a themable interface
00196 Themable::Themable()
00197 {
00198   mScale        = 1.0;
00199   mThemeManager = 0;
00200 }
00201 
00202 
00203 // Constructs a themeable interface given its id and the master theme manager. 
00204 // This automatically registeres the object with the manager.
00205 Themable::Themable(QString id, ThemeManager* thememanager)
00206 {
00207   mScale        = 1.0;
00208   mId           = id;
00209   mThemeManager = thememanager;
00210   if (!thememanager) return;
00211   thememanager->registerTheme(this);
00212 }
00213 
00214 
00215 // Destructs the themeable object
00216 Themable::~Themable()
00217 {
00218   if (mThemeManager) mThemeManager->unregisterTheme(this);
00219 }
00220 
00221 
00222 

Generated on Sun Mar 4 10:56:43 2007 for KWin4 by  doxygen 1.4.6