ipsservices/nmipssettings/src/nmipssettingscustomitem.cpp
changeset 23 2dc6caa42ec3
parent 20 ecc8def7944a
child 27 9ba4404ef423
equal deleted inserted replaced
20:ecc8def7944a 23:2dc6caa42ec3
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include <QVariant>
    18 #include <QVariant>
    19 #include <hbdatetimeedit.h>
    19 #include <HbAbstractItemView>
    20 #include <hbabstractitemview.h>
    20 #include <HbDataFormModel>
    21 #include <hbdataformmodel.h>
    21 #include <HbDataFormModelItem>
    22 #include <hbdataformmodelitem.h>
       
    23 
    22 
    24 #include "nmipssettingscustomitem.h"
    23 #include "nmipssettingscustomitem.h"
    25 #include "nmipssettingslabeledcombobox.h"
    24 #include "nmipssettingslabeledcombobox.h"
       
    25 #include "nmipssettingstimeeditor.h"
       
    26 #include "nmipssettingsmultiselectionitem.h"
    26 
    27 
    27 /*!
    28 /*!
    28     \class NmIpsSettingsCustomItem
    29     \class NmIpsSettingsCustomItem
    29     \brief The class implements a custom HbDataFormViewItem.
    30     \brief The class implements a custom HbDataFormViewItem.
    30 */
    31 */
    59     \return true if item type for the index is supported, otherwise false.
    60     \return true if item type for the index is supported, otherwise false.
    60 */
    61 */
    61 bool NmIpsSettingsCustomItem::canSetModelIndex(const QModelIndex &index) const
    62 bool NmIpsSettingsCustomItem::canSetModelIndex(const QModelIndex &index) const
    62 {
    63 {
    63     int type(index.data(HbDataFormModelItem::ItemTypeRole).toInt());
    64     int type(index.data(HbDataFormModelItem::ItemTypeRole).toInt());
    64     return type==LabeledComboBox || type==TimeEditor;
    65     return type == LabeledComboBox || type == TimeEditor || type == MultiSelectionItem;
    65 }
       
    66 
       
    67 /*!
       
    68     Sets the custom widget's properties from the model item.
       
    69 */
       
    70 void NmIpsSettingsCustomItem::restore()
       
    71 {
       
    72     HbDataFormModelItem::DataItemType itemType = static_cast<HbDataFormModelItem::DataItemType>(
       
    73         modelIndex().data(HbDataFormModelItem::ItemTypeRole).toInt());
       
    74 
       
    75     if (itemType==LabeledComboBox || itemType==TimeEditor) {
       
    76 
       
    77         HbDataFormModel* model = static_cast<HbDataFormModel*>
       
    78             (static_cast<HbAbstractViewItem*>(this)->itemView()->model());
       
    79         HbDataFormModelItem* modelItem = model->itemFromIndex(modelIndex());
       
    80         QHash<QString ,QVariant> properties =
       
    81             modelItem->data(HbDataFormModelItem::PropertyRole).toHash();
       
    82 
       
    83          if (itemType==TimeEditor) {
       
    84              // Set time editor properties. Simply copy all set properties to the widget.
       
    85              QStringList propertyNames = properties.keys();
       
    86 
       
    87              for (int index=0 ; index < propertyNames.count() ; index++) {
       
    88                  QString propName = propertyNames.at(index);
       
    89                  dataItemContentWidget()->setProperty(propName.toAscii().data(),
       
    90                                                       properties.value(propName));
       
    91              }
       
    92          } else {
       
    93              // Set combobox properties in specific order. currentIndex must be set last so that
       
    94              // both the labelTexts and comboItems have been set before. Also, labelTexts must be
       
    95              // set before comboItems.
       
    96              setWidgetProperty("labelTexts", properties);
       
    97              setWidgetProperty("comboItems", properties);
       
    98              setWidgetProperty("currentIndex", properties);
       
    99          }
       
   100     }
       
   101 }
    66 }
   102 
    67 
   103 /*!
    68 /*!
   104     Creates the custom widget.
    69     Creates the custom widget.
   105 */
    70 */
   109     int type(data.toInt());
    74     int type(data.toInt());
   110 
    75 
   111     HbWidget *widget = 0;
    76     HbWidget *widget = 0;
   112 
    77 
   113     switch (type) {
    78     switch (type) {
   114             case LabeledComboBox: {
    79         case LabeledComboBox: {
   115                 widget = new NmIpsSettingsLabeledComboBox();
    80             widget = new NmIpsSettingsLabeledComboBox();
   116                 break;
    81             break;
   117                 }
    82             }
   118             case TimeEditor: {
    83         case TimeEditor: {
   119                 HbDateTimeEdit *edit = new HbDateTimeEdit();
    84             widget = new NmIpsSettingsTimeEditor();
   120                 widget = edit;
    85             break;
   121                 break;
    86             }
   122                 }
    87         case MultiSelectionItem: {
   123             default: {
    88             widget = new NmIpsSettingsMultiSelectionItem();
   124                 break;
    89             connect(widget, SIGNAL(propertyChanged(QMap<QString, QVariant>)),
   125                 }
    90                 this, SLOT(propertyChanged(QMap<QString, QVariant>)));
       
    91             break;
       
    92         }
       
    93         default: {
       
    94             // Unknown/unhandled item type.
       
    95             break;
       
    96         }
   126     }
    97     }
   127 
    98 
   128     return widget;
    99     return widget;
       
   100 }
       
   101 
       
   102 /*!
       
   103     Sets the custom widget's properties from the model item.
       
   104 */
       
   105 void NmIpsSettingsCustomItem::restore()
       
   106 {
       
   107     HbDataFormModelItem::DataItemType itemType = static_cast<HbDataFormModelItem::DataItemType>(
       
   108         modelIndex().data(HbDataFormModelItem::ItemTypeRole).toInt());
       
   109 
       
   110     if (itemType == LabeledComboBox || itemType == TimeEditor || itemType == MultiSelectionItem) {
       
   111 
       
   112         HbDataFormModel *model = static_cast<HbDataFormModel *>
       
   113             (static_cast<HbAbstractViewItem *>(this)->itemView()->model());
       
   114 
       
   115         HbDataFormModelItem *modelItem = model->itemFromIndex(modelIndex());
       
   116 
       
   117         QHash<QString ,QVariant> properties =
       
   118             modelItem->data(HbDataFormModelItem::PropertyRole).toHash();
       
   119 
       
   120         if (itemType == LabeledComboBox) {
       
   121             // Set combobox properties in specific order. currentIndex must be set last so that
       
   122             // both the labelTexts and comboItems have been set before. Also, labelTexts must
       
   123             // have been set before comboItems. Otherwise the setting of current item doesn't work.
       
   124             // Block signals which are generated when comboItems are set.
       
   125             setWidgetProperty("labelTexts", properties);
       
   126             dataItemContentWidget()->blockSignals(true);
       
   127             setWidgetProperty("comboItems", properties);
       
   128             dataItemContentWidget()->blockSignals(false);
       
   129             setWidgetProperty("currentIndex", properties);
       
   130 
       
   131         } else {
       
   132             // Set item properties. Simply copy all set properties to the widget.
       
   133             QStringList propertyNames = properties.keys();
       
   134             foreach (QString key, propertyNames) {
       
   135                 QVariant value = properties.value(key);
       
   136                 dataItemContentWidget()->setProperty(key.toAscii().data(), value);
       
   137             }
       
   138         }
       
   139     }
       
   140 }
       
   141 
       
   142 /*!
       
   143     Copies all given properties from widget to model.
       
   144 
       
   145     \param properties Composited widget properties.
       
   146 */
       
   147 void NmIpsSettingsCustomItem::propertyChanged(QMap<QString, QVariant> properties)
       
   148 {
       
   149     HbDataFormModel* model =
       
   150         static_cast<HbDataFormModel*>(static_cast<HbAbstractViewItem*>(this)->itemView()->model());
       
   151 
       
   152     model->blockSignals(true);
       
   153     HbDataFormModelItem* modlItem = model->itemFromIndex(modelIndex());
       
   154     QStringList keys = properties.keys();
       
   155     foreach (QString key, keys) {
       
   156         modlItem->setContentWidgetData(key, properties.value(key));
       
   157     }
       
   158     model->blockSignals(false);
   129 }
   159 }
   130 
   160 
   131 /*!
   161 /*!
   132     Sets \a property to the content widget if found from \a properties.
   162     Sets \a property to the content widget if found from \a properties.
   133     \param property Name of the property to set.
   163     \param property Name of the property to set.
   138 {
   168 {
   139     if (properties.contains(property)) {
   169     if (properties.contains(property)) {
   140         dataItemContentWidget()->setProperty(property.toAscii().data(), properties.value(property));
   170         dataItemContentWidget()->setProperty(property.toAscii().data(), properties.value(property));
   141     }
   171     }
   142 }
   172 }
   143