src/hbcore/core/hblayoutparameters.cpp
changeset 21 4633027730f5
child 28 b7da29130b0e
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hblayoutparameters_p.h"
       
    27 #include "hbsharedcache_p.h"
       
    28 #include "hbmemoryutils_p.h"
       
    29 #include "hbdeviceprofile.h"
       
    30 #include "hbmemorymanager_p.h"
       
    31 #include <QtAlgorithms>
       
    32 
       
    33 static const QString GLOBAL_PARAMETERS_LOCATION =
       
    34         QLatin1String(":/themes/style/hbdefault/variables/layout/zoom/0/hbglobalparameters.css");
       
    35 
       
    36 QStringList HbLayoutParameters::specialVariableNames()
       
    37 {
       
    38     QStringList variables;
       
    39 
       
    40     //order must match HbLayoutParameters::SpecialParameter-enum.
       
    41     variables << "hb-param-screen-width"
       
    42               << "hb-param-screen-height"
       
    43               << "hb-param-screen-short-edge"
       
    44               << "hb-param-screen-long-edge";
       
    45 
       
    46     return variables;
       
    47 }
       
    48 
       
    49 #ifndef HB_BIN_CSS
       
    50 QVector<HbCss::Value> HbLayoutParameters::specialVariableValues(const HbDeviceProfile &profile)
       
    51 {
       
    52     QVector<HbCss::Value> values;
       
    53     values.reserve(NumParameters);
       
    54     for (int i = 0; i < NumParameters; ++i) {
       
    55         values.append(HbCss::Value());
       
    56         values.last().type = HbCss::Value::Number;;
       
    57     }
       
    58     QSizeF pSize = profile.logicalSize();
       
    59 
       
    60     values[ScreenWidth].variant = pSize.width();
       
    61     values[ScreenHeight].variant = pSize.height();
       
    62     values[ShortEdge].variant =  qMin(pSize.height(), pSize.width());
       
    63     values[LongEdge].variant = qMax(pSize.height(), pSize.width());
       
    64 
       
    65     return values;
       
    66 }
       
    67 
       
    68 class BinaryCreatorHelper
       
    69 {
       
    70 public:
       
    71     BinaryCreatorHelper() : mValueBase(0) {}
       
    72     ~BinaryCreatorHelper();
       
    73     void constructBinary(const QString &parameterFile);
       
    74     
       
    75     const HbParameterItem *begin() const {
       
    76         return mItems.constData();
       
    77     }
       
    78     qint32 size() const {
       
    79         return mItems.size();
       
    80     }
       
    81     const char *valueBase() const {
       
    82         return mValueBase;
       
    83     }
       
    84     const char *nameBase() const {
       
    85         return mNames.constData();
       
    86     }
       
    87 private:
       
    88     QVector<HbParameterItem> mItems;
       
    89     QByteArray mNames;
       
    90     const char *mValueBase;
       
    91 };
       
    92 
       
    93 BinaryCreatorHelper::~BinaryCreatorHelper()
       
    94 {
       
    95     //destroy values
       
    96     Q_FOREACH(const HbParameterItem &item, mItems) {
       
    97         if (!item.special && item.valueOffset) {
       
    98             HbCss::Value *value = HbMemoryUtils::getAddress<HbCss::Value>(
       
    99                     HbMemoryManager::HeapMemory, item.valueOffset);
       
   100             HbMemoryUtils::release(value);
       
   101         }
       
   102     }
       
   103 }
       
   104 
       
   105 void BinaryCreatorHelper::constructBinary(const QString &parameterFile)
       
   106 {
       
   107     if (!mItems.isEmpty()) return;
       
   108     //parse global parameter-file to heap.
       
   109     HbCss::Parser parser;
       
   110     parser.init(parameterFile, true);
       
   111     HbCss::StyleSheet *styleSheet = HbMemoryUtils::create<HbCss::StyleSheet>(HbMemoryManager::HeapMemory);
       
   112     parser.parse(styleSheet);
       
   113     
       
   114     //calculate the total memory needed.
       
   115     int parameterCount = 0;
       
   116     int parameterNamesTotal = 0;
       
   117     const int ruleCount = styleSheet->variableRules.count();
       
   118     for (int i = 0; i < ruleCount; ++i) {
       
   119         const HbVector<HbCss::Declaration> &decls = styleSheet->variableRules.at(i).declarations;
       
   120         parameterCount += decls.count();
       
   121         for (int j = 0; j < parameterCount; ++j) {
       
   122             parameterNamesTotal += decls.at(j).property.length() + 1; //extra for '\0'
       
   123         }
       
   124     }
       
   125     QStringList specialVariables = HbLayoutParameters::specialVariableNames();
       
   126     parameterCount += specialVariables.count();
       
   127     Q_FOREACH(const QString &variable, specialVariables) {
       
   128         parameterNamesTotal += variable.length() + 1; //extra for '\0'
       
   129     }
       
   130 
       
   131     mItems.reserve(parameterCount);
       
   132     mNames.reserve(parameterNamesTotal);
       
   133     
       
   134     //create the parameter items and add them to vector,
       
   135     //fill the parameterNames bytearray.
       
   136     GET_MEMORY_MANAGER(HbMemoryManager::HeapMemory)
       
   137     for (int i = 0; i < ruleCount; ++i) {
       
   138         const HbVector<HbCss::Declaration> &decls = styleSheet->variableRules.at(i).declarations;
       
   139         const int declsCount = decls.count();
       
   140         for (int j = 0; j < declsCount; ++j) {
       
   141             const HbCss::Declaration &decl = decls.at(j);
       
   142             QString parameterName(decl.property);
       
   143             quint32 hash = HbSharedCache::hash(QStringRef(&parameterName));
       
   144             qptrdiff valueOffset = manager->alloc(sizeof(HbCss::Value));
       
   145             new ((char*)manager->base() + valueOffset) HbCss::Value(decl.values.at(0));
       
   146             mItems.append(HbParameterItem(hash, valueOffset, mNames.size(), false));
       
   147             QByteArray latinName(parameterName.toLatin1());
       
   148             mNames.append(latinName.constData(), latinName.length() + 1);
       
   149         }
       
   150     }
       
   151     //add special variables
       
   152     for(int i = 0; i < specialVariables.count(); ++i) {
       
   153         quint32 hash = HbSharedCache::hash(QStringRef(&specialVariables.at(i)));
       
   154         mItems.append(HbParameterItem(hash, i, mNames.size(), true));
       
   155         QByteArray latinName(specialVariables.at(0).toLatin1());
       
   156         mNames.append(latinName.constData(), latinName.length() + 1);
       
   157     }
       
   158 
       
   159     qSort(mItems); //sorts by hash value
       
   160     HbMemoryUtils::release(styleSheet);
       
   161     mValueBase = static_cast<const char*>(manager->base());
       
   162 }
       
   163 
       
   164 HbLayoutParameters::HbLayoutParameters()
       
   165     : d(0)
       
   166 {
       
   167 #ifdef Q_OS_SYMBIAN
       
   168     //try get layoutparameters from sharedcache first. 
       
   169     d = HbSharedCache::instance()->layoutParameters();
       
   170 #endif    
       
   171     if (!d) {
       
   172         static BinaryCreatorHelper binCreator;
       
   173         binCreator.constructBinary(GLOBAL_PARAMETERS_LOCATION);
       
   174         d = new HbLayoutParametersPrivate(binCreator.begin(), binCreator.size(), 
       
   175                                           binCreator.valueBase(), binCreator.nameBase());
       
   176     }
       
   177 }
       
   178 
       
   179 void HbLayoutParameters::init(const HbDeviceProfile &profile)
       
   180 {
       
   181     if (d->mSpecialVariables.isEmpty() || d->mProfileName != profile.name()) {
       
   182         d->mSpecialVariables = specialVariableValues(profile);
       
   183         d->mProfileName = profile.name();
       
   184     }
       
   185 }
       
   186 
       
   187 HbLayoutParameters::const_iterator HbLayoutParameters::find(const QString &parameter) const
       
   188 {
       
   189     quint32 parameterHash = HbSharedCache::hash(QStringRef(&parameter));
       
   190     return qBinaryFind(begin(), end(), HbParameterItem(parameterHash, 0, 0, 0));
       
   191 }
       
   192 
       
   193 #endif //#ifndef HB_BIN_CSS