perfapps/perfmon/ui/hb/app/src/settingsview.cpp
changeset 51 b048e15729d6
child 53 819e59dfc032
equal deleted inserted replaced
44:5db69f4c3d06 51:b048e15729d6
       
     1 /*
       
     2 * Copyright (c) 2010 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 "settingsview.h"
       
    19 
       
    20 #include <HbAction>
       
    21 #include <HbToolBar>
       
    22 #include <HbDataForm>
       
    23 #include <HbDataFormModel>
       
    24 #include <HbDataFormModelItem>
       
    25 #include <HbDataFormViewItem>
       
    26 #include <HbLineEdit>
       
    27 #include <HbPushButton>
       
    28 
       
    29 #include "enginewrapper.h"
       
    30 
       
    31 const QStringList PRIORITY_ITEMS = QStringList() << SettingsView::tr("Much less") <<
       
    32     SettingsView::tr("Less") << SettingsView::tr("Normal") <<
       
    33     SettingsView::tr("More") << SettingsView::tr("Much more") <<
       
    34     SettingsView::tr("Real time") << SettingsView::tr("Abs. very low") <<
       
    35     SettingsView::tr("Abs. low") << SettingsView::tr("Abs background") <<
       
    36     SettingsView::tr("Abs. foreground") << SettingsView::tr("Abs high");
       
    37 
       
    38 const QStringList CPU_SAMPLING = QStringList() << SettingsView::tr("CPU Time") <<
       
    39     SettingsView::tr("NOPs");
       
    40 
       
    41 const QStringList POPUP_LOCATION = QStringList() << SettingsView::tr("Top right") <<
       
    42     SettingsView::tr("Bottom middle");
       
    43 
       
    44 const QStringList POPUP_VISIBILITY = QStringList() << SettingsView::tr("On") <<
       
    45     SettingsView::tr("Backgr. only") << SettingsView::tr("Off");
       
    46 
       
    47 const QStringList SOURCES = QStringList() << SettingsView::tr("CPU") <<
       
    48     SettingsView::tr("Ram") << SettingsView::tr("C:") << SettingsView::tr("D:") <<
       
    49     SettingsView::tr("E:") << SettingsView::tr("F:") << SettingsView::tr("G:") <<
       
    50     SettingsView::tr("H:") << SettingsView::tr("I:") << SettingsView::tr("Power");
       
    51 
       
    52 const QStringList LOG_MODES = QStringList() << SettingsView::tr("RDebug") <<
       
    53     SettingsView::tr("Log file") << SettingsView::tr("RDebug & log file");
       
    54 
       
    55 
       
    56 SettingsView::SettingsView(EngineWrapper &engine)
       
    57     : mEngine(engine), mLogFilePathItem(0)
       
    58 {
       
    59     setTitle(tr("Settings"));
       
    60 
       
    61     //create toolbar showing launch popup
       
    62     HbToolBar *toolBar = this->toolBar();
       
    63     HbAction *actionOk = new HbAction(tr("Ok"), toolBar);
       
    64     HbAction *actionCancel = new HbAction(tr("Cancel"), toolBar);
       
    65     
       
    66     toolBar->addAction(actionOk);
       
    67     toolBar->addAction(actionCancel);
       
    68 
       
    69     //create setting form
       
    70     mSettingsForm = new HbDataForm();
       
    71 
       
    72     //create a model class
       
    73     mModel = new HbDataFormModel(this);
       
    74     createModel(*mModel);
       
    75     load(engine.settings());
       
    76 
       
    77     connect(mSettingsForm, SIGNAL(itemShown(const QModelIndex)),
       
    78             this, SLOT(dataItemDisplayed(const QModelIndex)));
       
    79 
       
    80     // Set created model to form
       
    81     mSettingsForm->setModel(mModel);
       
    82     setWidget(mSettingsForm);
       
    83 
       
    84 
       
    85     connect(actionOk, SIGNAL(triggered()), this, SLOT(accept()));
       
    86     connect(actionCancel, SIGNAL(triggered()), this, SLOT(reject()));
       
    87     connect(this, SIGNAL(aboutToClose()), this, SLOT(accept()));
       
    88 
       
    89 }
       
    90 
       
    91 void SettingsView::createModel(HbDataFormModel &model)
       
    92 {
       
    93     // General page
       
    94     HbDataFormModelItem *generalPage = model.appendDataFormPage(tr("General"));
       
    95 
       
    96     mHeartBeatItem = model.appendDataFormItem(
       
    97             HbDataFormModelItem::TextItem, tr("Heart beat (ms)"), generalPage);
       
    98     mHeartBeatItem->setContentWidgetData("maximum", 99999);
       
    99     mHeartBeatItem->setContentWidgetData("minimum", 0);
       
   100     mHeartBeatItem->setContentWidgetData("inputMethodHints", Qt::ImhDigitsOnly);
       
   101 
       
   102     mMaxSamplesItem = model.appendDataFormItem(
       
   103             HbDataFormModelItem::TextItem, tr("Max samples"), generalPage);
       
   104     mMaxSamplesItem->setContentWidgetData("maximum", 9999);
       
   105     mMaxSamplesItem->setContentWidgetData("minimum", 0);
       
   106     mMaxSamplesItem->setContentWidgetData("inputMethodHints", Qt::ImhDigitsOnly);
       
   107 
       
   108     mPriorityItem = model.appendDataFormItem(
       
   109             HbDataFormModelItem::RadioButtonListItem, tr("Priority"), generalPage);
       
   110     mPriorityItem->setContentWidgetData("items", PRIORITY_ITEMS);
       
   111 
       
   112     mCpuSamplingItem = model.appendDataFormItem(
       
   113             HbDataFormModelItem::RadioButtonListItem, tr("CPU sampling mode"), generalPage);
       
   114     mCpuSamplingItem->setContentWidgetData("items", CPU_SAMPLING);
       
   115     
       
   116     mKeepBacklightItem = model.appendDataFormItem(
       
   117             HbDataFormModelItem::CheckBoxItem, tr("Backlight"), generalPage);
       
   118     mKeepBacklightItem->setContentWidgetData("text", tr("Keep backlight on"));
       
   119 
       
   120     mPowerMonitoringEnabledItem = model.appendDataFormItem(
       
   121             HbDataFormModelItem::CheckBoxItem, tr("Power"), generalPage);
       
   122     mPowerMonitoringEnabledItem->setContentWidgetData("text", tr("Enable power monitoring"));
       
   123 
       
   124     // Gray out selection if feature is not supported.
       
   125     if (!mEngine.PowerMonitoringSupported())
       
   126         {
       
   127         mPowerMonitoringEnabledItem->setEnabled(false);
       
   128         }
       
   129     
       
   130     // Data popup page
       
   131     HbDataFormModelItem *dataPopupPage = model.appendDataFormPage(tr("Data popup"));
       
   132 
       
   133     mPopupVisibilityItem = model.appendDataFormItem(
       
   134             HbDataFormModelItem::RadioButtonListItem, tr("Visibility"),
       
   135             dataPopupPage);
       
   136     mPopupVisibilityItem->setContentWidgetData("items", POPUP_VISIBILITY);
       
   137 
       
   138     mPopupLocationItem = model.appendDataFormItem(
       
   139             HbDataFormModelItem::RadioButtonListItem, tr("Location"), dataPopupPage);
       
   140     mPopupLocationItem->setContentWidgetData("items", POPUP_LOCATION);
       
   141 
       
   142     mPopupSourcesItem = model.appendDataFormItem(
       
   143             HbDataFormModelItem::MultiselectionItem, tr("Sources"), dataPopupPage);
       
   144     mPopupSourcesItem->setContentWidgetData("items", SOURCES);
       
   145 
       
   146     // Graphs page
       
   147     HbDataFormModelItem *graphsPage = model.appendDataFormPage(tr("Graphs"));
       
   148 
       
   149     mVerticalBarPeriodItem = model.appendDataFormItem(
       
   150             HbDataFormModelItem::TextItem, tr("Vertical bar period (s)"), graphsPage);
       
   151     mVerticalBarPeriodItem->setContentWidgetData("maximum", 999);
       
   152     mVerticalBarPeriodItem->setContentWidgetData("minimum", 0);
       
   153     mVerticalBarPeriodItem->setContentWidgetData("inputMethodHints", Qt::ImhDigitsOnly);
       
   154 
       
   155     mGraphSourcesItem = model.appendDataFormItem(
       
   156             HbDataFormModelItem::MultiselectionItem, tr("Sources"), graphsPage);
       
   157     mGraphSourcesItem->setContentWidgetData("items", SOURCES);
       
   158 
       
   159     // Logging page
       
   160     mLogPage = model.appendDataFormPage(tr("Logging"));
       
   161 
       
   162     mLogModeItem = model.appendDataFormItem(
       
   163             HbDataFormModelItem::RadioButtonListItem, tr("Mode"), mLogPage);
       
   164     mLogModeItem->setContentWidgetData("items", LOG_MODES);
       
   165     mSettingsForm->addConnection(mLogModeItem, SIGNAL(itemSelected(int)),
       
   166                                  this, SLOT(logModeChanged(int)));
       
   167 
       
   168     mLogSourcesItem = model.appendDataFormItem(
       
   169             HbDataFormModelItem::MultiselectionItem, tr("Sources"), mLogPage);
       
   170     mLogSourcesItem->setContentWidgetData("items", SOURCES);
       
   171 }
       
   172 
       
   173 void SettingsView::load(const PerfMonSettings& settings)
       
   174 {
       
   175     mHeartBeatItem->setContentWidgetData("text", settings.heartBeat());
       
   176     mMaxSamplesItem->setContentWidgetData("text", settings.maxSamples());
       
   177     mPriorityItem->setContentWidgetData("selected", settings.priority());
       
   178     mCpuSamplingItem->setContentWidgetData("selected", settings.cpuMode());
       
   179     mKeepBacklightItem->setContentWidgetData("checkState", settings.keepBacklightOn() ? Qt::Checked : Qt::Unchecked);
       
   180     mPowerMonitoringEnabledItem->setContentWidgetData("checkState", settings.powerMonitoringEnabled() ? Qt::Checked : Qt::Unchecked);
       
   181 
       
   182     mPopupVisibilityItem->setContentWidgetData("selected", settings.dataPopupVisibility());
       
   183     mPopupLocationItem->setContentWidgetData("selected", settings.dataPopupLocation());
       
   184     mPopupSourcesItem->setContentWidgetData("selectedItems",
       
   185             qVariantFromValue(settings.dataPopupSources().enabledIndexes()));
       
   186 
       
   187     mVerticalBarPeriodItem->setContentWidgetData("text", settings.graphVerticalBarPeriod());
       
   188     mGraphSourcesItem->setContentWidgetData("selectedItems",
       
   189             qVariantFromValue(settings.graphSources().enabledIndexes()));
       
   190 
       
   191     mLogModeItem->setContentWidgetData("selected", settings.loggingMode());
       
   192     mLogFilePath = settings.loggingFilePath();
       
   193     if (settings.loggingMode() > ELoggingModeRDebug)
       
   194         createLogFilePathItem();
       
   195     mLogSourcesItem->setContentWidgetData("selectedItems",
       
   196             qVariantFromValue(settings.loggingSources().enabledIndexes()));
       
   197 }
       
   198 
       
   199 void SettingsView::save(PerfMonSettings& settings)
       
   200 {
       
   201     settings.setHeartBeat(mHeartBeatItem->contentWidgetData("text").toInt());
       
   202     settings.setMaxSamples(mMaxSamplesItem->contentWidgetData("text").toInt());
       
   203     settings.setPriority(mPriorityItem->contentWidgetData("selected").toInt());
       
   204     settings.setCpuMode(mCpuSamplingItem->contentWidgetData("selected").toInt());
       
   205     settings.setKeepBacklightOn(mKeepBacklightItem->contentWidgetData("checkState").toInt() == Qt::Checked);
       
   206     settings.setPowerMonitoringEnabled(mPowerMonitoringEnabledItem->contentWidgetData("checkState").toInt() == Qt::Checked);
       
   207 
       
   208     settings.setDataPopupVisibility(mPopupVisibilityItem->contentWidgetData("selected").toInt());
       
   209     settings.setDataPopupLocation(mPopupLocationItem->contentWidgetData("selected").toInt());
       
   210     settings.dataPopupSources().setEnabledIndexes(
       
   211             qVariantValue<QList<QVariant> >(mPopupSourcesItem->contentWidgetData("selectedItems")));
       
   212 
       
   213     settings.setGraphVerticalBarPeriod(mVerticalBarPeriodItem->contentWidgetData("text").toInt());
       
   214     settings.graphSources().setEnabledIndexes(
       
   215             qVariantValue<QList<QVariant> >(mGraphSourcesItem->contentWidgetData("selectedItems")));
       
   216 
       
   217     settings.setLoggingMode(mLogModeItem->contentWidgetData("selected").toInt());
       
   218     
       
   219     if (mLogFilePathItem)
       
   220         settings.setLoggingFilePath(mLogFilePathItem->contentWidgetData("text").toString());
       
   221     settings.loggingSources().setEnabledIndexes(
       
   222             qVariantValue<QList<QVariant> >(mLogSourcesItem->contentWidgetData("selectedItems")));
       
   223 }
       
   224 
       
   225 void SettingsView::dataItemDisplayed(const QModelIndex &index)
       
   226 {
       
   227     HbDataFormModelItem* modelItem = mModel->itemFromIndex(index);
       
   228     HbDataFormViewItem *viewItem = static_cast<HbDataFormViewItem*>(mSettingsForm->itemByIndex(index));
       
   229     HbWidget *dataContentWidget = viewItem->dataItemContentWidget();
       
   230 
       
   231     // set input method hint for edits
       
   232     // TODO: remove once setContentWidgetData works with inputMethodHints
       
   233     if (modelItem == mHeartBeatItem ||
       
   234         modelItem == mMaxSamplesItem ||
       
   235         modelItem == mVerticalBarPeriodItem)
       
   236     {
       
   237         HbLineEdit *edit = static_cast<HbLineEdit*>(dataContentWidget);
       
   238         edit->setInputMethodHints(Qt::ImhDigitsOnly);
       
   239     }
       
   240 }
       
   241 
       
   242 void SettingsView::logModeChanged(int index)
       
   243 {
       
   244     if (index == ELoggingModeRDebug && mLogFilePathItem) {
       
   245         removeLogFilePathItem();
       
   246     }
       
   247     else if (index != ELoggingModeRDebug && !mLogFilePathItem) {
       
   248         createLogFilePathItem();
       
   249     }
       
   250 }
       
   251 
       
   252 void SettingsView::createLogFilePathItem()
       
   253 {
       
   254     mLogFilePathItem = mModel->insertDataFormItem(mLogPage->indexOf(mLogModeItem) + 1,
       
   255             HbDataFormModelItem::TextItem, tr("Log file path"), mLogPage);
       
   256     mLogFilePathItem->setContentWidgetData("text", mLogFilePath);
       
   257 }
       
   258 
       
   259 void SettingsView::removeLogFilePathItem()
       
   260 {
       
   261     // we need to store current text so that it is not lost when 
       
   262     // user hides and then displays "Log file path" item again.
       
   263     mLogFilePath = mLogFilePathItem->contentWidgetData("text").toString();
       
   264     mModel->removeItem(mLogFilePathItem);
       
   265     mLogFilePathItem = 0;
       
   266 }
       
   267 
       
   268 void SettingsView::accept()
       
   269 {
       
   270     save(mEngine.settings());
       
   271     mEngine.updateSettings();
       
   272     emit finished(true);
       
   273 }
       
   274 
       
   275 void SettingsView::reject()
       
   276 {
       
   277     emit finished(false);
       
   278 }