creator/src/notifications.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     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 
       
    19 #include <hbmessagebox.h>
       
    20 #include <hblabel.h>
       
    21 #include <hbaction.h>
       
    22 #include <hbpopup.h>
       
    23 #include <hblineedit.h>
       
    24 #include <hbvalidator.h>
       
    25 #include <hbabstractitemview.h>
       
    26 #include <hbprogressdialog.h>
       
    27 #include <hbdatetimepicker.h>
       
    28 
       
    29 #include <QString>
       
    30 #include <QDate>
       
    31 #include <QRegExp>
       
    32 #include <QList>
       
    33 
       
    34 #include "notifications.h"
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 
       
    38 void Notifications::showMessageBox(HbMessageBox::MessageBoxType type, const QString &text, const QString &label, int timeout )
       
    39 {
       
    40     HbMessageBox *messageBox = new HbMessageBox(type);
       
    41     messageBox->setText(text);
       
    42     if(label.length())
       
    43         {
       
    44         HbLabel *header = new HbLabel(label, messageBox);
       
    45         messageBox->setHeadingWidget(header);
       
    46         }
       
    47     messageBox->setAttribute(Qt::WA_DeleteOnClose);
       
    48     messageBox->setTimeout(timeout);
       
    49     messageBox->open();
       
    50 }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 
       
    54 void Notifications::about()
       
    55 {
       
    56     showMessageBox(HbMessageBox::MessageTypeInformation,
       
    57         "Version 6.1.0 - June 18th 2010. Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. Licensed under Eclipse Public License v1.0.",
       
    58         "About Creator", 
       
    59         HbPopup::NoTimeout
       
    60         );
       
    61 }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 
       
    65 void Notifications::error(const QString& errorMessage)
       
    66 {
       
    67     showMessageBox(HbMessageBox::MessageTypeWarning, errorMessage, QString("Error"), 3000);
       
    68 }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 
       
    73 HbProgressDialog* Notifications::showProgressBar(const QString& text, int max)
       
    74 {
       
    75 	HbProgressDialog *note = new HbProgressDialog(HbProgressDialog::ProgressDialog);
       
    76     note->setText(text);
       
    77     note->setMaximum(max);
       
    78     note->setAutoClose(false);
       
    79     note->show();
       
    80     return note;
       
    81 
       
    82 }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 
       
    86 void Notifications::showGlobalNote(const QString& text, HbMessageBox::MessageBoxType type, HbPopup::DefaultTimeout timeout)
       
    87 {
       
    88     showMessageBox(type, text, QString("Creator"), timeout);
       
    89 }
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 
       
    94 CreatorYesNoDialog::CreatorYesNoDialog(MUIObserver* observer, int userData) : 
       
    95     HbMessageBox(HbMessageBox::MessageTypeQuestion, NULL),
       
    96     CreatorDialog(observer, userData)
       
    97 {
       
    98 }
       
    99 
       
   100 void CreatorYesNoDialog::launch(const QString& text, const QString& label, MUIObserver* observer, int userData) throw(std::exception)
       
   101 {
       
   102     CreatorYesNoDialog* dlg = new CreatorYesNoDialog(observer, userData);
       
   103     dlg->setText(text);
       
   104     if(label.length())
       
   105         dlg->setHeadingWidget(new HbLabel(label, dlg));
       
   106     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   107     dlg->setTimeout(HbPopup::NoTimeout);
       
   108     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   109 }
       
   110 
       
   111 void CreatorYesNoDialog::DialogClosed(HbAction *action)
       
   112 {
       
   113     TBool PositiveAction(EFalse);
       
   114     if(action && (action->softKeyRole()==QAction::PositiveSoftKey || !action->text().compare("yes", Qt::CaseInsensitive))){
       
   115         PositiveAction = ETrue;
       
   116     }
       
   117     NotifyObserver(PositiveAction);
       
   118 }
       
   119 
       
   120 CreatorInputDialog::CreatorInputDialog(int* value, MUIObserver* module, int userData) : 
       
   121     HbInputDialog(NULL),
       
   122     CreatorDialog(module, userData),
       
   123     mIntValue(value), 
       
   124     mStrValue(mDummy)// will not be used
       
   125 {
       
   126     if(!value)
       
   127         throw std::invalid_argument("value cannot be the null!");
       
   128 }
       
   129 
       
   130 CreatorInputDialog::CreatorInputDialog(TDes& value, MUIObserver* module, int userData) : 
       
   131     HbInputDialog(NULL),
       
   132     CreatorDialog(module, userData),
       
   133     mIntValue(NULL),
       
   134     mStrValue(value)
       
   135 {
       
   136 }
       
   137 
       
   138 void CreatorInputDialog::launch(const QString& label, int* value, bool acceptsZero, MUIObserver* observer, int userData) throw( std::exception )
       
   139 {
       
   140     CreatorInputDialog* dlg = new CreatorInputDialog(value, observer, userData);
       
   141     dlg->setPromptText(label);
       
   142     dlg->setInputMode(IntInput);
       
   143     HbValidator *validator = new HbValidator(dlg);
       
   144     QString tmp;
       
   145     if (acceptsZero == false) {
       
   146         tmp.append("[1-9]{1}\\d{1,4}");
       
   147     }
       
   148     else {
       
   149         tmp.append("^[0-9]{5}");
       
   150     }
       
   151         
       
   152     QRegExp rxBasic(tmp);
       
   153     validator->addField(new QRegExpValidator(rxBasic, 0), "");
       
   154     dlg->setValidator(validator);
       
   155     dlg->setValue(QVariant(*value));
       
   156     dlg->lineEdit()->setSelection(0, dlg->value().toString().length());
       
   157     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   158     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   159 }
       
   160 
       
   161 void CreatorInputDialog::launch(const QString& label, TDes& value, MUIObserver* observer, int userData) throw( std::exception )
       
   162 {
       
   163     CreatorInputDialog* dlg = new CreatorInputDialog(value, observer, userData);
       
   164     dlg->setPromptText(label);
       
   165     dlg->lineEdit()->setMaxLength(value.MaxLength());
       
   166     dlg->setValue(QString::fromUtf16(value.Ptr(), value.Length()));
       
   167     dlg->lineEdit()->setSelection(0, dlg->value().toString().length());
       
   168     dlg->setInputMode(TextInput);
       
   169     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   170     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   171 }
       
   172 
       
   173 void CreatorInputDialog::DialogClosed(HbAction *action)
       
   174 {
       
   175     TBool PositiveAction(EFalse);
       
   176     if(action && (action->softKeyRole()==QAction::PositiveSoftKey || !action->text().compare("ok", Qt::CaseInsensitive))){
       
   177         bool ok = true;
       
   178         if( inputMode() == IntInput )
       
   179             *mIntValue = value().toInt(&ok);
       
   180         else if( inputMode() == TextInput && mStrValue.MaxLength() >= value().toString().length() )
       
   181             mStrValue.Copy(value().toString().utf16());
       
   182         else
       
   183             ok = false;
       
   184         PositiveAction = ok ? ETrue : EFalse;
       
   185     }
       
   186     NotifyObserver(PositiveAction);
       
   187 }
       
   188 
       
   189 CreatorSelectionDialog::CreatorSelectionDialog(int* selectedItem, MUIObserver* observer, int userData) throw(std::exception) : 
       
   190     HbSelectionDialog(NULL),
       
   191     CreatorDialog(observer, userData),
       
   192     mSelectedItem(selectedItem),
       
   193     mSelectedItems(NULL)
       
   194 {
       
   195     if(!mSelectedItem)
       
   196         throw std::invalid_argument("selectedItem cannot be null!");
       
   197 }
       
   198 
       
   199 CreatorSelectionDialog::CreatorSelectionDialog(CArrayFixFlat<TInt>* selectedItems, MUIObserver* observer, int userData) throw(std::exception) : 
       
   200     HbSelectionDialog(NULL),
       
   201     CreatorDialog(observer, userData),
       
   202     mSelectedItem(NULL),
       
   203     mSelectedItems(selectedItems)
       
   204 {
       
   205     if(!mSelectedItems)
       
   206         throw std::invalid_argument("selectedItems cannot be null!");
       
   207 }
       
   208 
       
   209 void CreatorSelectionDialog::launch(const QString& label, const QStringList& items, int* selectedItem, MUIObserver* observer, int userData) throw(std::exception)
       
   210 {
       
   211     CreatorSelectionDialog* dlg = new CreatorSelectionDialog(selectedItem, observer, userData);
       
   212     if(label.length())
       
   213         dlg->setHeadingWidget(new HbLabel(label, dlg));
       
   214     dlg->setStringItems(items);
       
   215     dlg->setSelectionMode(HbAbstractItemView::SingleSelection); 
       
   216     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   217     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   218 }
       
   219 
       
   220 void CreatorSelectionDialog::launch(const QString& label, const QStringList& items, CArrayFixFlat<TInt>* selectedItems, MUIObserver* observer, int userData) throw(std::exception)
       
   221 {
       
   222     CreatorSelectionDialog* dlg = new CreatorSelectionDialog(selectedItems, observer, userData);
       
   223     if(label.length())
       
   224         dlg->setHeadingWidget(new HbLabel(label, dlg));
       
   225     dlg->setStringItems(items);
       
   226     dlg->setSelectionMode(HbAbstractItemView::MultiSelection); 
       
   227     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   228     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   229 }
       
   230 
       
   231 void CreatorSelectionDialog::DialogClosed(HbAction *action)
       
   232 {
       
   233     TBool PositiveAction(EFalse);
       
   234     //CreatorSelectionDialog* dlg = qobject_cast<CreatorSelectionDialog*>(sender());
       
   235     if(!action || (action && action->softKeyRole()==QAction::SelectSoftKey) || (action && !action->text().compare("ok", Qt::CaseInsensitive))){
       
   236         if(selectedItems().count()){
       
   237             try{
       
   238                 if( selectionMode() == HbAbstractItemView::SingleSelection && mSelectedItem){
       
   239                     bool ok(false);
       
   240                     *mSelectedItem = selectedItems().at(0).toInt(&ok);
       
   241                     if(!ok)
       
   242                         throw std::invalid_argument("cannot obtain selected item!");
       
   243                 }
       
   244                 if( selectionMode() == HbAbstractItemView::MultiSelection && mSelectedItems){
       
   245                     QList<QVariant> items = selectedItems();
       
   246                     foreach( QVariant item, items){
       
   247                         QT_TRAP_THROWING( mSelectedItems->AppendL( item.toInt() ) );
       
   248                     }
       
   249                 }
       
   250                 PositiveAction = ETrue;
       
   251             }
       
   252             catch (std::exception& e){
       
   253                 Notifications::error( QString("exception: ")+e.what() );
       
   254             }
       
   255         }
       
   256     }
       
   257     NotifyObserver(PositiveAction);
       
   258 }
       
   259 
       
   260 CreatorDateTimeDialog::CreatorDateTimeDialog(TTime* value, MUIObserver* observer, int userData) : 
       
   261     HbDialog(NULL),
       
   262     CreatorDialog(observer, userData),
       
   263     mValue(value)
       
   264 {
       
   265 }
       
   266 
       
   267 void CreatorDateTimeDialog::launch(const QString& label, TTime* value, MUIObserver* observer, int userData) throw( std::exception )
       
   268 {
       
   269     CreatorDateTimeDialog* dlg = new CreatorDateTimeDialog(value, observer, userData);
       
   270     if(!value)
       
   271         throw std::invalid_argument("value cannot be the null!");
       
   272     if(label.length())
       
   273         dlg->setHeadingWidget( new HbLabel(label, dlg) );
       
   274     
       
   275     // TTime to QDate
       
   276     TBuf<20> timeString;
       
   277     _LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3");
       
   278     TRAP_IGNORE( value->FormatL(timeString, KDateString) );
       
   279     QString temp = QString::fromUtf16(timeString.Ptr(), timeString.Length());
       
   280     temp.replace(QChar('/'), QChar('-'));
       
   281     QDate date = QDate::fromString(temp, "dd-MM-yyyy");
       
   282     HbDateTimePicker* widget = new HbDateTimePicker( date, dlg );
       
   283     dlg->setContentWidget( widget );
       
   284     dlg->addAction(new HbAction("Ok", dlg));
       
   285     dlg->addAction(new HbAction("Cancel", dlg));
       
   286     dlg->setModal(true); // Dialog is modal  
       
   287     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   288     dlg->setTimeout(HbPopup::NoTimeout);
       
   289     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   290 }
       
   291 
       
   292 void CreatorDateTimeDialog::DialogClosed(HbAction *action)
       
   293 {
       
   294     TBool PositiveAction(EFalse);
       
   295     if(action && (action->softKeyRole()==QAction::PositiveSoftKey || !action->text().compare("ok", Qt::CaseInsensitive))){
       
   296         QString str = qobject_cast<HbDateTimePicker*>(contentWidget())->date().toString(Qt::ISODate);
       
   297         str.remove('-');
       
   298         str += ":000000";// 0h 0m 0s
       
   299         TBuf<30> dateTimeString(str.utf16());
       
   300         mValue->Set(dateTimeString);
       
   301         PositiveAction = ETrue;
       
   302     }
       
   303     NotifyObserver(PositiveAction);
       
   304 }
       
   305 //End of File