| Home · All Classes · Main Classes · Deprecated |
00001 /*************************************************************************** 00002 ** 00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 00004 ** All rights reserved. 00005 ** Contact: Nokia Corporation (directui@nokia.com) 00006 ** 00007 ** This file is part of libmeegotouch. 00008 ** 00009 ** If you have questions regarding the use of this file, please contact 00010 ** Nokia at directui@nokia.com. 00011 ** 00012 ** This library is free software; you can redistribute it and/or 00013 ** modify it under the terms of the GNU Lesser General Public 00014 ** License version 2.1 as published by the Free Software Foundation 00015 ** and appearing in the file LICENSE.LGPL included in the packaging 00016 ** of this file. 00017 ** 00018 ****************************************************************************/ 00019 00020 #include "mdebug.h" 00021 #include "mstyle.h" 00022 #include "mstyle_p.h" 00023 #include "mtheme.h" 00024 #include "mtheme_p.h" 00025 #include "mapplication.h" 00026 #include "mapplicationwindow.h" 00027 00028 // TODO: get rid of this include, make MStyle not to use mgen 00029 #include "gen_mstyledata.h" 00030 #include <QMetaProperty> 00031 00032 /* TODO: Will be removed once all the pointers are removed from the styles 00033 and pixmaps & scalable images doesn't need release calls */ 00034 Q_DECLARE_METATYPE(const QPixmap *) 00035 Q_DECLARE_METATYPE(const MScalableImage *) 00036 void releaseAllocatedResourcesFromStyle(const MStyle *style) 00037 { 00038 // TODO: Fix this in mtheme so, that the pixmaps are not pointers 00039 // and therefore we don't need to call release at all. 00040 const int propertyCount = style->metaObject()->propertyCount(); 00041 for (int i = 0; i < propertyCount; ++i) { 00042 const QMetaProperty &property = style->metaObject()->property(i); 00043 if (property.isReadable()) { 00044 const char *type = property.typeName(); 00045 if (strcmp(type, "const QPixmap*") == 0) { 00046 MTheme::releasePixmap(qvariant_cast<const QPixmap *>(property.read(style))); 00047 } else if (strcmp(type, "const MScalableImage*") == 0) { 00048 MTheme::releaseScalableImage(qvariant_cast<const MScalableImage *>(property.read(style))); 00049 } 00050 } 00051 } 00052 } 00053 00055 // PRIVATE CLASS // 00057 int MStyle::references() const 00058 { 00059 return data->references; 00060 } 00061 00062 int MStyle::addReference() 00063 { 00064 data->references++; 00065 return data->references; 00066 } 00067 00068 int MStyle::removeReference() 00069 { 00070 data->references--; 00071 if (data->references <= 0) { 00072 releaseAllocatedResourcesFromStyle(this); 00073 /* TODO: deleteLater breaks theme change (when theme libraries are unloaded and deleteLater refs them) 00074 deleteLater(); // Use deleteLater() for QObjects instead of "delete this" 00075 */ 00076 MThemePrivate::removeLeakedStyle(this); 00077 delete this; 00078 return 0; 00079 } 00080 return data->references; 00081 } 00082 00083 MStyleContainerPrivate::MStyleContainerPrivate() : 00084 currentMode("") 00085 { 00086 cachedCurrentStyle[0] = 0; 00087 cachedCurrentStyle[1] = 0; 00088 parent = NULL; 00089 q_ptr = NULL; 00090 } 00091 00092 MStyleContainerPrivate::~MStyleContainerPrivate() 00093 { 00094 releaseStyles(); 00095 } 00096 00097 void MStyleContainerPrivate::releaseStyles() 00098 { 00099 for (int i = 0; i < 2; ++i) { 00100 QHashIterator<QString, const MStyle*> iter(cachedStyles[i]); 00101 while (iter.hasNext()) { 00102 iter.next(); 00103 MTheme::releaseStyle(iter.value()); 00104 } 00105 cachedStyles[i].clear(); 00106 cachedCurrentStyle[i] = 0; 00107 } 00108 } 00109 00111 // PUBLIC CLASS // 00113 00114 // public constructor 00115 MStyleContainer::MStyleContainer() : 00116 d_ptr(new MStyleContainerPrivate) 00117 { 00118 d_ptr->q_ptr = this; 00119 MThemePrivate::registerStyleContainer(this); 00120 } 00121 00122 // protected constructor 00123 MStyleContainer::MStyleContainer(MStyleContainerPrivate *dd) : 00124 d_ptr(dd) 00125 { 00126 d_ptr->q_ptr = this; 00127 MThemePrivate::registerStyleContainer(this); 00128 } 00129 00130 // destructor 00131 MStyleContainer::~MStyleContainer() 00132 { 00133 MThemePrivate::unregisterStyleContainer(this); 00134 delete d_ptr; 00135 } 00136 00137 void MStyleContainer::initialize(const QString &objectName, const QString &type, const MWidgetController *parent) 00138 { 00139 // The style type should never be "default" - that would probably be a view type 00140 // that's mistakenly being used as a style type. 00141 // The caller probably means "" instead. 00142 Q_ASSERT(type != "default"); 00143 00144 d_ptr->objectName = objectName; 00145 d_ptr->type = type; 00146 d_ptr->parent = parent; 00147 00148 reloadStyles(); 00149 } 00150 00151 // set the container objectName 00152 void MStyleContainer::setObjectName(const QString &objectName) 00153 { 00154 if (objectName != d_ptr->objectName) { 00155 d_ptr->objectName = objectName; 00156 reloadStyles(); 00157 } 00158 } 00159 00160 // set the container typeName 00161 void MStyleContainer::setType(const QString &type) 00162 { 00163 // The style type should never be "default" - that would probably be a view type 00164 // that's mistakenly being used as a style type. 00165 // The caller probably means "" instead. 00166 Q_ASSERT(type != "default"); 00167 00168 if (type != d_ptr->type) { 00169 d_ptr->type = type; 00170 reloadStyles(); 00171 } 00172 } 00173 // returns the object name of the style container 00174 QString MStyleContainer::objectName() const 00175 { 00176 return d_ptr->objectName; 00177 } 00178 00179 // returns the type name of the style container 00180 QString MStyleContainer::type() const 00181 { 00182 return d_ptr->type; 00183 } 00184 00185 void MStyleContainer::setParent(const MWidgetController *parent) 00186 { 00187 if (d_ptr->parent != parent) { 00188 d_ptr->parent = parent; 00189 reloadStyles(); 00190 } 00191 } 00192 00193 const MWidgetController *MStyleContainer::parent() const 00194 { 00195 return d_ptr->parent; 00196 } 00197 00198 // getter for derived classes 00199 const MStyle *MStyleContainer::currentStyle() const 00200 { 00201 M::Orientation orientation = M::Landscape; 00202 00203 const MWindow *activeWindow = MApplication::activeWindow(); 00204 if (activeWindow) 00205 orientation = activeWindow->orientation(); 00206 00207 if (d_ptr->cachedCurrentStyle[orientation] != 0) { 00208 return d_ptr->cachedCurrentStyle[orientation]; 00209 } 00210 00211 const MStyle* style; 00212 QHash<QString, const MStyle*>::iterator iter = d_ptr->cachedStyles[orientation].find(currentMode()); 00213 if (iter != d_ptr->cachedStyles[orientation].end()) { 00214 style = *iter; 00215 } else { 00216 style = MTheme::style(styleType(), objectName(), currentMode(), type(), orientation, parent()); 00217 d_ptr->cachedStyles[orientation].insert(currentMode(), style); 00218 } 00219 d_ptr->cachedCurrentStyle[orientation] = style; 00220 return style; 00221 } 00222 00223 void MStyleContainer::setCurrentMode(const QString &mode) 00224 { 00225 if (mode != d_ptr->currentMode) { 00226 d_ptr->cachedCurrentStyle[0] = 0; 00227 d_ptr->cachedCurrentStyle[1] = 0; 00228 d_ptr->currentMode = mode; 00229 } 00230 } 00231 00232 QString MStyleContainer::currentMode() const 00233 { 00234 return d_ptr->currentMode; 00235 } 00236 00237 // virtual method which returns the name of the style in this container instance 00238 const char *MStyleContainer::styleType() const 00239 { 00240 return "MStyle"; 00241 } 00242 00243 // virtual method which will reload all styles used in this container instance 00244 // this method will be called when e.g. objectName changes 00245 void MStyleContainer::reloadStyles() 00246 { 00247 d_ptr->releaseStyles(); 00248 } 00249 00250 // sets the current style to default 00251 void MStyleContainer::setModeDefault() 00252 { 00253 setCurrentMode(""); 00254 } 00255 00256 MStyleContainer::dummyNeverToBeUsedPtr MStyleContainer::_dummyNeverToBeUsed() 00257 { 00258 return &MStyleContainer::operator->; 00259 }
| Copyright © 2010 Nokia Corporation | Generated on Thu Nov 4 2010 18:14:21 (PDT) Doxygen 1.7.1 |
MeeGo Touch |