homescreenapp/stateplugins/hsmenuworkerstateplugin/src/hscollectionnamedialog.cpp
changeset 35 f9ce957a272c
child 36 cdae8c6c3876
equal deleted inserted replaced
5:c743ef5928ba 35:f9ce957a272c
       
     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: Menu name collection state
       
    15  *
       
    16  */
       
    17 
       
    18 #include <hbaction.h>
       
    19 #include <hbinputdialog.h>
       
    20 #include <hblineedit.h>
       
    21 #include <hbvalidator.h>
       
    22 #include <hsmenuservice.h>
       
    23 #include <QDebug>
       
    24 
       
    25 #include "hscollectionnamedialog.h"
       
    26 
       
    27 /*!
       
    28  \class HsCollectionNameDialog
       
    29  \ingroup group_hsmenustateplugin
       
    30  \brief Rename Collection State.
       
    31  Collection name state
       
    32  */
       
    33 
       
    34 /*!
       
    35  Maximum length if collection name
       
    36  */
       
    37 const int qMaxStrLength = 248;
       
    38 
       
    39 /*!
       
    40  Constructor
       
    41  \param parent owner
       
    42  \retval void
       
    43  */
       
    44 HsCollectionNameDialog::HsCollectionNameDialog(const int &itemId)
       
    45 {
       
    46     HSMENUTEST_FUNC_ENTRY("HsInputDialog::HsInputDialog");
       
    47     mCollectionsNames = HsMenuService::getCollectionNames();
       
    48     setInputMode(HbInputDialog::TextInput);
       
    49     setPromptText(hbTrId("txt_applib_title_collection_name"));
       
    50     lineEdit()->setMaxLength(qMaxStrLength);
       
    51     if (itemId) {
       
    52         setValue(HsMenuService::getName(itemId));
       
    53         mCollectionsNames.removeOne(value().toString());
       
    54     } else {
       
    55         setValue(hbTrId("txt_applib_dialog_entry_collection"));
       
    56     }
       
    57     HSMENUTEST_FUNC_EXIT("HsInputDialog::HsInputDialog");
       
    58 }
       
    59 
       
    60 /*!
       
    61  Destructor
       
    62  */
       
    63 HsCollectionNameDialog::~HsCollectionNameDialog()
       
    64 {
       
    65     mCollectionsNames.clear();
       
    66     mLastCollectionName.clear();
       
    67 }
       
    68 
       
    69 /*!
       
    70  Gets new collection's name.
       
    71  \param item_id Item's id.
       
    72  \retval Selected action.
       
    73  */
       
    74 #ifdef COVERAGE_MEASUREMENT
       
    75 #pragma CTC SKIP // Reason: Modal inputdialog exec
       
    76 #endif //COVERAGE_MEASUREMENT
       
    77 HbAction *HsCollectionNameDialog::exec()
       
    78 {
       
    79     HSMENUTEST_FUNC_ENTRY("HsInputDialog::exec");
       
    80     onTextChanged(value().toString());
       
    81     makeConnect();
       
    82 
       
    83     HbAction *action = HbInputDialog::exec();
       
    84 
       
    85     makeDisconnect();
       
    86     HSMENUTEST_FUNC_EXIT("HsInputDialog::exec");
       
    87     return action;
       
    88 }
       
    89 #ifdef COVERAGE_MEASUREMENT
       
    90 #pragma CTC ENDSKIP // Reason: Modal inputdialog exec
       
    91 #endif //COVERAGE_MEASUREMENT
       
    92 /*!
       
    93  Gets new collection's name.
       
    94  \param name name of collection.
       
    95  \param addLastName true
       
    96  if last found name is need to add to validation
       
    97  \retval new collection name.
       
    98  */
       
    99 QString HsCollectionNameDialog::newName(const QString &name,
       
   100                                         bool addLastName)
       
   101 {
       
   102     HSMENUTEST_FUNC_ENTRY("HsInputDialog::newName");
       
   103     QString newName(name);
       
   104     int numToAppend(1);
       
   105     if (addLastName && mLastCollectionName.length() > 0
       
   106             && !mCollectionsNames.contains(mLastCollectionName)) {
       
   107         mCollectionsNames << mLastCollectionName;
       
   108     }
       
   109     while (mCollectionsNames.contains(newName)) {
       
   110         newName = name;
       
   111         newName.append("(");
       
   112         if (numToAppend < 10) {
       
   113             newName.append("0");
       
   114         }
       
   115         newName.append(QString::number(numToAppend));
       
   116         newName.append(")");
       
   117         numToAppend++;
       
   118         if (!addLastName) {
       
   119             if (mLastCollectionName.length() > 0
       
   120                     && !mCollectionsNames.contains(mLastCollectionName)) {
       
   121                 mCollectionsNames << mLastCollectionName;
       
   122             }
       
   123             mCollectionsNames.removeOne(name);
       
   124             mLastCollectionName = name;
       
   125         }
       
   126     }
       
   127     HSMENUTEST_FUNC_EXIT("HsInputDialog::newName");
       
   128     return newName;
       
   129 }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 void HsCollectionNameDialog::makeConnect()
       
   135 {
       
   136     /*connect(lineEdit(), SIGNAL(textChanged(const QString &text)),
       
   137      SLOT(onTextChanged(const QString &text)));*/
       
   138 
       
   139     connect(lineEdit(), SIGNAL(contentsChanged()),
       
   140             SLOT(onContentsChanged()));
       
   141 }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void HsCollectionNameDialog::makeDisconnect()
       
   147 {
       
   148     /*disconnect(lineEdit(), SIGNAL(textChanged(const QString &text)),
       
   149      this, SLOT(onTextChanged(const QString &text)));*/
       
   150     disconnect(lineEdit(), SIGNAL(contentsChanged()),
       
   151                this, SLOT(onContentsChanged()));
       
   152 }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 void HsCollectionNameDialog::onTextChanged(const QString &text)
       
   158 {
       
   159     qDebug() << QString("HsInputDialog::onTextChanged( %1 )").arg(text);
       
   160     HSMENUTEST_FUNC_ENTRY("HsInputDialog::onTextChanged");
       
   161     if (text.trimmed() == "") {
       
   162         primaryAction()->setEnabled(false);
       
   163     } else {
       
   164         primaryAction()->setEnabled(true);
       
   165     }
       
   166 
       
   167     QString newText = newName(text);
       
   168     if (newText != text) {
       
   169         makeDisconnect();
       
   170         lineEdit()->setText(newText);
       
   171         lineEdit()->setSelection(text.length(), newText.length()
       
   172                                  - text.length());
       
   173         makeConnect();
       
   174     }
       
   175     HSMENUTEST_FUNC_EXIT("HsInputDialog::onTextChanged");
       
   176 }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void HsCollectionNameDialog::onContentsChanged()
       
   182 {
       
   183     qDebug("HsInputDialog::onContentsChanged()");
       
   184     onTextChanged(value().toString());
       
   185 }