camerauis/cameraxui/cxui/src/cxuisettingxmlreader.cpp
changeset 19 d9aefe59d544
child 21 fa6d9f75d6a6
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QPair>
       
    19 #include <QtXml>
       
    20 #include <QFile>
       
    21 
       
    22 
       
    23 #include "cxutils.h"
       
    24 #include "cxuienums.h"
       
    25 #include "cxuisettingxmlreader.h"
       
    26 #include "cxuisettingradiobuttonlist.h"
       
    27 
       
    28 using namespace CxUiSettings;
       
    29 
       
    30 namespace
       
    31 {
       
    32     static const char *TAG_ROOT                           = "cxui";
       
    33     static const char *TAG_SETTINGS                       = "settings";
       
    34     static const char *TAG_SETTING_LIST                   = "setting_list";
       
    35     static const char *TAG_SETTING_SLIDER                 = "setting_slider";
       
    36     static const char *TAG_ITEM                           = "item";
       
    37     static const char *TAG_ITEM_VARIANT                   = "lnItem";
       
    38     static const char *TAG_SETUP                          = "setup";
       
    39 
       
    40     static const char *ATTRIBUTE_ID                       = "id";
       
    41     static const char *ATTRIBUTE_HEADING                  = "heading";
       
    42     static const char *ATTRIBUTE_PREVIEW                  = "preview";
       
    43     static const char *ATTRIBUTE_LISTBOXTYPE              = "type";
       
    44     static const char *ATTRIBUTE_HEADING_ICON             = "setting_icon";
       
    45     static const char *ATTRIBUTE_ICON                     = "icon";
       
    46 
       
    47     static const char *ATTRIBUTE_VALUE                    = "value";
       
    48     static const char *ATTRIBUTE_LOCALIZATION_ID          = "string";
       
    49     static const char *ATTRIBUTE_LOCALIZATION_ID_LN_VALUE = "lnValue";
       
    50 
       
    51     static const char *ATTRIBUTE_SLIDER_MIN               = "min";
       
    52     static const char *ATTRIBUTE_SLIDER_MAX               = "max";
       
    53     static const char *ATTRIBUTE_SLIDER_STEP              = "step";
       
    54 
       
    55     // possible values that can be used in xml for "ATTRIBUTE_LISTBOXTYPE"
       
    56     static const char *SINGLE_LINE_LISTBOX                = "SingleLineListBox";
       
    57     static const char *TWO_LINE_LISTBOX                   = "TwoLineListBox";
       
    58 }
       
    59 
       
    60 
       
    61 CxuiSettingXmlReader::CxuiSettingXmlReader()
       
    62   : mXmlUri(),
       
    63     mParsed(false),
       
    64     mListParams(),
       
    65     mSliderParams()
       
    66 {
       
    67 }
       
    68 
       
    69 
       
    70 CxuiSettingXmlReader::~CxuiSettingXmlReader()
       
    71 {
       
    72     clear();
       
    73 }
       
    74 
       
    75 
       
    76 /*!
       
    77 * Get the settings info based on the setting id
       
    78 * @param setting denotes the setting key id.
       
    79 * @param Returns list of settings specific to radiobuttonlist.
       
    80 * @return Were the params found, i.e. is the setting supported.
       
    81 */
       
    82 CxUiSettings::RadioButtonListParams CxuiSettingXmlReader::getListSettingInfo(const QString &setting, bool &found)
       
    83 {
       
    84     CX_DEBUG_ENTER_FUNCTION();
       
    85     RadioButtonListParams p;
       
    86 
       
    87     // Parse current XML file now if not already done.
       
    88     parse();
       
    89 
       
    90     if (mListParams.contains(setting)){
       
    91         CX_ASSERT_ALWAYS(mListParams.value(setting));
       
    92         p = *mListParams.value(setting);
       
    93         found = true;
       
    94     } else {
       
    95         found = false;
       
    96     }
       
    97 
       
    98     CX_DEBUG_EXIT_FUNCTION();
       
    99     return p;
       
   100 }
       
   101 
       
   102 
       
   103 
       
   104 /*!
       
   105 * Get the settings info based on the setting id
       
   106 * @param setting Denotes the setting key id.
       
   107 * @param found Were the params found, i.e. is the setting supported.
       
   108 * @return Returns list of settings specific to slider setting.
       
   109 */
       
   110 CxUiSettings::SliderParams CxuiSettingXmlReader::getSliderSettingInfo(const QString &setting, bool &found)
       
   111 {
       
   112     CX_DEBUG_ENTER_FUNCTION();
       
   113     SliderParams p;
       
   114 
       
   115     // Parse current XML file now if not already done.
       
   116     parse();
       
   117 
       
   118     if (mSliderParams.contains(setting)){
       
   119         CX_ASSERT_ALWAYS(mSliderParams.value(setting));
       
   120         p = *mSliderParams.value(setting);
       
   121         found = true;
       
   122     } else {
       
   123         found = false;
       
   124     }
       
   125 
       
   126     CX_DEBUG_EXIT_FUNCTION();
       
   127     return p;
       
   128 }
       
   129 
       
   130 /*!
       
   131 * Set new source for setting XML.
       
   132 * @param uri URI for the setting XML. Note: Currently only local file path is supported.
       
   133 */
       
   134 void CxuiSettingXmlReader::setXmlSource(const QString& uri)
       
   135 {
       
   136     CX_DEBUG_ENTER_FUNCTION();
       
   137     CX_DEBUG(("Settings mapping XML: XML source set to [%s]", uri.toAscii().constData()));
       
   138 
       
   139     if(mXmlUri.compare(uri) != 0) {
       
   140         // Clear any old content and set flag that parsing needs to be done again.
       
   141         clear();
       
   142         mXmlUri = uri;
       
   143     }
       
   144     CX_DEBUG_EXIT_FUNCTION();
       
   145 }
       
   146 
       
   147 /*!
       
   148 * Clear existing setting info and raise flag for needed parsing.
       
   149 */
       
   150 void CxuiSettingXmlReader::clear()
       
   151 {
       
   152     CX_DEBUG_ENTER_FUNCTION();
       
   153 
       
   154     mParsed = false;
       
   155 
       
   156     qDeleteAll(mListParams.values());
       
   157     mListParams.clear();
       
   158 
       
   159     qDeleteAll(mSliderParams.values());
       
   160     mSliderParams.clear();
       
   161 
       
   162     CX_DEBUG_EXIT_FUNCTION();
       
   163 }
       
   164 
       
   165 /*!
       
   166 * Parses the XML and creates all settings list defined in XML
       
   167 */
       
   168 void CxuiSettingXmlReader::parse()
       
   169 {
       
   170     CX_DEBUG_ENTER_FUNCTION();
       
   171 
       
   172     if (mParsed) {
       
   173         CX_DEBUG(("Settings mapping XML: Already parsed our XML, using cached content") );
       
   174         CX_DEBUG_EXIT_FUNCTION();
       
   175         return;
       
   176     }
       
   177 
       
   178     QDomDocument document("dummy");
       
   179     QFile file(mXmlUri);
       
   180 
       
   181     if (!file.open(QFile::ReadOnly|QFile::Text)) {
       
   182         CX_DEBUG(("Settings mapping XML: could not open file [%s]", mXmlUri.toAscii().constData()));
       
   183         return;
       
   184     }
       
   185 
       
   186     bool ok = document.setContent(&file);
       
   187     file.close();
       
   188 
       
   189     if (!ok) {
       
   190         CX_DEBUG(("Settings mapping XML: could not set content"));
       
   191         return;
       
   192     }
       
   193 
       
   194     QDomElement root = document.documentElement();
       
   195     if (root.tagName() != TAG_ROOT) {
       
   196         CX_DEBUG(("Settings mapping XML: root name wrong %s", root.tagName().toAscii().constData()));
       
   197         return;
       
   198     }
       
   199 
       
   200     QDomElement e = findChildElement(root, TAG_SETTINGS);
       
   201     if (e.isNull()) {
       
   202         CX_DEBUG(("Settings mapping XML: could not find settings tag"));
       
   203         return;
       
   204     }
       
   205 
       
   206     parseSettings(e);
       
   207 
       
   208     // We've done parsing. No need to do it again.
       
   209     mParsed = true;
       
   210 
       
   211     CX_DEBUG_EXIT_FUNCTION();
       
   212 }
       
   213 
       
   214 
       
   215 /*!
       
   216 * Parses the settings in XML and creates settings requested using the DOM element
       
   217 */
       
   218 void CxuiSettingXmlReader::parseSettings(const QDomElement& element) {
       
   219     CX_DEBUG_ENTER_FUNCTION();
       
   220 
       
   221     if (element.isNull()) {
       
   222         //!@todo throw exception
       
   223         return;
       
   224     }
       
   225 
       
   226     QString tagName;
       
   227     QDomElement e;
       
   228     // Go through list of items
       
   229     for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
       
   230         e = n.toElement();
       
   231         tagName = e.tagName();
       
   232 
       
   233         if (tagName == TAG_SETTING_LIST) {
       
   234             // <setting_list id="WHITE_BALANCE" heading="qtn_setting_title_wb">
       
   235             RadioButtonListParams *p(parseSettingList(e));
       
   236             if (p) {
       
   237                 mListParams.insert(p->mSettingId,p);
       
   238             }
       
   239         } else if(tagName == TAG_SETTING_SLIDER) {
       
   240             // <setting_slider id="EXPOSURE_COMPENSATION" heading="qtn_setting_title_ev">
       
   241             SliderParams *p(parseSettingSlider(e));
       
   242             if (p) {
       
   243                 mSliderParams.insert(p->mSettingId,p);
       
   244             }
       
   245         }
       
   246     }
       
   247 
       
   248     CX_DEBUG_EXIT_FUNCTION();
       
   249 }
       
   250 
       
   251 
       
   252 /*!
       
   253 * Creates the settings list based on the DomElement
       
   254 returns radiobutton list data
       
   255 */
       
   256 CxUiSettings::RadioButtonListParams *CxuiSettingXmlReader::parseSettingList(const QDomElement& element)
       
   257 {
       
   258     CX_DEBUG_ENTER_FUNCTION();
       
   259 
       
   260     // Read the heading and id.
       
   261     if (element.isNull()) {
       
   262         return NULL;
       
   263     }
       
   264 
       
   265     RadioButtonListParams *p = new RadioButtonListParams();
       
   266 
       
   267     CX_DEBUG(("Parsing radio button list for setting id [%s]", element.attribute(ATTRIBUTE_ID).toAscii().constData()));
       
   268 
       
   269     // XML something like this
       
   270     // <setting_list id="WHITE_BALANCE" heading="qtn_setting_title_wb">
       
   271     p->mHeading = hbTrId(element.attribute(ATTRIBUTE_HEADING).toAscii().constData());
       
   272     p->mSettingId = element.attribute(ATTRIBUTE_ID);
       
   273     p->mHeadingIcon = element.attribute(ATTRIBUTE_HEADING_ICON);
       
   274 
       
   275     // Set the list box attributes
       
   276 
       
   277     // Example: preview="1"
       
   278     p->mPreview = element.attribute(ATTRIBUTE_PREVIEW).toInt() == 1;
       
   279 
       
   280     // Example: type="SingleLineListBox"
       
   281     if (element.attribute(ATTRIBUTE_LISTBOXTYPE) == SINGLE_LINE_LISTBOX) {
       
   282         p->mListboxType = CxuiSettingRadioButtonList::SingleLine;
       
   283     } else if(element.attribute(ATTRIBUTE_LISTBOXTYPE) == TWO_LINE_LISTBOX) {
       
   284         p->mListboxType = CxuiSettingRadioButtonList::TwoLine;
       
   285     }
       
   286 
       
   287     QDomElement e;
       
   288     p->mSettingPairList.clear();
       
   289 
       
   290     // Go through list of items
       
   291     // XML something like this
       
   292     // <item value="0" string="qtn_whitebalance_auto" />
       
   293     for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
       
   294         e = n.toElement();
       
   295 
       
   296         // get the string text id from the xml
       
   297         QString string = e.attribute(ATTRIBUTE_LOCALIZATION_ID);
       
   298         // get the equivalent engine value from xml
       
   299         QVariant value = e.attribute(ATTRIBUTE_VALUE);
       
   300 
       
   301         CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   302         if (e.tagName() == TAG_ITEM) {
       
   303             // get the localizable string from the hbtrid
       
   304             string = hbTrId(string.toAscii().constData());
       
   305         } else if (e.tagName() == TAG_ITEM_VARIANT) {
       
   306             QString lnValue = e.attribute(ATTRIBUTE_LOCALIZATION_ID_LN_VALUE);
       
   307             // format the setting string
       
   308             string = hbTrId(string.toAscii().constData(), lnValue.toInt());
       
   309         }
       
   310 
       
   311         CX_DEBUG(("attribute [%s] value[%s]", ATTRIBUTE_LOCALIZATION_ID, string.toAscii().constData()));
       
   312 
       
   313         if (!string.isNull()) {
       
   314             CxUiSettings::SettingItem setting;
       
   315             setting.mItem = string;
       
   316             setting.mValue = value;
       
   317             setting.mIcon = e.attribute(ATTRIBUTE_ICON);
       
   318             p->mSettingPairList.append(setting);
       
   319         }
       
   320     }
       
   321 
       
   322     CX_DEBUG_EXIT_FUNCTION();
       
   323     return p;
       
   324 }
       
   325 
       
   326 
       
   327 
       
   328 /*!
       
   329 * Creates the slider settings based on the DomElement
       
   330 @param element specifies the dom element
       
   331 returns slider parameters
       
   332 */
       
   333 CxUiSettings::SliderParams *CxuiSettingXmlReader::parseSettingSlider(const QDomElement& element)
       
   334 {
       
   335     CX_DEBUG_ENTER_FUNCTION();
       
   336 
       
   337     // Read the heading and id.
       
   338     if (element.isNull()) {
       
   339         return NULL;
       
   340     }
       
   341 
       
   342     SliderParams *p = new SliderParams();
       
   343 
       
   344     CX_DEBUG(("Parsing slider for setting id [%s]", element.attribute(ATTRIBUTE_ID).toAscii().constData()));
       
   345 
       
   346     // XML something like this
       
   347     // <setting_list id="WHITE_BALANCE" heading="qtn_setting_title_wb">
       
   348     p->mHeading = hbTrId(element.attribute(ATTRIBUTE_HEADING).toAscii().constData());
       
   349     p->mSettingId = element.attribute(ATTRIBUTE_ID);
       
   350     p->mHeadingIcon = element.attribute(ATTRIBUTE_HEADING_ICON);
       
   351 
       
   352     QDomNode n = element.firstChild();
       
   353     QDomElement e = n.toElement();
       
   354 
       
   355     p->mSettingStrings.clear();
       
   356 
       
   357     CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   358 
       
   359     if (e.tagName() == TAG_SETUP) {
       
   360         // get the min text id from the xml
       
   361         QString minString = e.attribute(ATTRIBUTE_SLIDER_MIN);
       
   362 
       
   363         // get the max text id from the xml
       
   364         QString maxString = e.attribute(ATTRIBUTE_SLIDER_MAX);
       
   365 
       
   366         // get the step text id from the xml
       
   367         QVariant stepString = e.attribute(ATTRIBUTE_SLIDER_STEP);
       
   368         p->mStep = stepString.toReal();
       
   369 
       
   370         // get the ln value from xml to generate strings for slider
       
   371         QString lnValue = e.attribute(ATTRIBUTE_LOCALIZATION_ID_LN_VALUE);
       
   372 
       
   373         // Don't create labels for slider if no strings are defined in settinsg xml
       
   374         if (!minString.isEmpty() && !maxString.isEmpty()) {
       
   375             qreal step = p->mStep;
       
   376             qreal value = lnValue.toDouble();
       
   377             // generating all negative valued strings for slider setting
       
   378             while (value > 0) {
       
   379                 // format the setting string
       
   380                 QString str =  hbTrId(minString.toAscii().constData()).arg(value,0,'f',1);
       
   381                 p->mSettingStrings.append(str);
       
   382                 value = value - step;
       
   383             }
       
   384 
       
   385             // generating all non-negative valued strings for slider setting
       
   386             step = p->mStep;
       
   387             value = 0;
       
   388             while(value <= lnValue.toInt()) {
       
   389                 // format the setting string
       
   390                 QString str = hbTrId(maxString.toAscii().constData()).arg(value,0,'f',1);
       
   391                 p->mSettingStrings.append(str);
       
   392                 value = value + step;
       
   393             }
       
   394         }
       
   395 
       
   396         int min = -lnValue.toInt();
       
   397         int max = lnValue.toInt();
       
   398 
       
   399         p->mRange = qMakePair(min, max);
       
   400     }
       
   401 
       
   402     CX_DEBUG_EXIT_FUNCTION();
       
   403     return p;
       
   404 }
       
   405 
       
   406 
       
   407 
       
   408 /*!
       
   409 * CxuiSettingXmlReader::findChildElement
       
   410 */
       
   411 QDomElement CxuiSettingXmlReader::findChildElement(const QDomNode& parent, const QString& name) {
       
   412     CX_DEBUG(("findChildElement name[%s]", name.toAscii().constData()));
       
   413 
       
   414     for (QDomNode n = parent.firstChild(); !n.isNull(); n = n.nextSibling()) {
       
   415         QDomElement e = n.toElement();
       
   416         if (e.tagName() == name) {
       
   417             return e;
       
   418         }
       
   419     }
       
   420 
       
   421     return QDomElement();
       
   422 }