creator/src/notifications.cpp
changeset 19 4b22a598b890
parent 17 4f2773374eff
child 23 c9bf25a20c9f
equal deleted inserted replaced
17:4f2773374eff 19:4b22a598b890
    22 #include <hbpopup.h>
    22 #include <hbpopup.h>
    23 #include <hblineedit.h>
    23 #include <hblineedit.h>
    24 #include <hbvalidator.h>
    24 #include <hbvalidator.h>
    25 #include <hbabstractitemview.h>
    25 #include <hbabstractitemview.h>
    26 #include <hbprogressdialog.h>
    26 #include <hbprogressdialog.h>
       
    27 #include <hbdatetimepicker.h>
    27 
    28 
    28 #include <QString>
    29 #include <QString>
    29 #include <QDate>
    30 #include <QDate>
    30 #include <QRegExp>
    31 #include <QRegExp>
       
    32 #include <QList>
    31 
    33 
    32 #include "notifications.h"
    34 #include "notifications.h"
    33 
    35 
    34 // ---------------------------------------------------------------------------
    36 // ---------------------------------------------------------------------------
    35 
    37 
    81 // ---------------------------------------------------------------------------
    83 // ---------------------------------------------------------------------------
    82 
    84 
    83 void Notifications::showGlobalNote(const QString& text, HbMessageBox::MessageBoxType type, HbPopup::DefaultTimeout timeout)
    85 void Notifications::showGlobalNote(const QString& text, HbMessageBox::MessageBoxType type, HbPopup::DefaultTimeout timeout)
    84 {
    86 {
    85     showMessageBox(type, text, QString("Creator"), timeout);
    87     showMessageBox(type, text, QString("Creator"), timeout);
    86 }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 
       
    90 bool Notifications::entriesQueryDialog(int& numberOfEntries, const QString& text, bool acceptsZero)
       
    91 {
       
    92 	bool err = false;
       
    93 	HbDialog *popup = new HbDialog();
       
    94     popup->setDismissPolicy(HbPopup::TapOutside);
       
    95     popup->setTimeout(HbPopup::NoTimeout);
       
    96 	
       
    97 	HbLabel *title = new HbLabel();
       
    98     HbLineEdit *edit = new HbLineEdit();
       
    99 	HbAction *actionOk = new HbAction("Ok");
       
   100 	HbAction *actionCancel = new HbAction("Cancel");
       
   101 	
       
   102 	title->setPlainText(text);
       
   103 	popup->setHeadingWidget(title);
       
   104 	popup->setContentWidget(edit);
       
   105 	
       
   106 	HbValidator *validator = new HbValidator();
       
   107 	QString tmp;
       
   108     if (acceptsZero == false) {
       
   109 		tmp.append("[1-9]{1}\\d{1,4}");
       
   110 	}
       
   111 	else {
       
   112 		tmp.append("^[0-9]{5}");
       
   113 	}
       
   114         
       
   115 	QRegExp rxBasic(tmp);
       
   116 	validator->addField(new QRegExpValidator(rxBasic, 0), "");
       
   117     edit->setValidator(validator);
       
   118 	edit->setText(QString::number(numberOfEntries));
       
   119 	edit->setSelection(0, QString::number(numberOfEntries).length());
       
   120 	
       
   121 	popup->setPrimaryAction(actionOk);
       
   122     popup->setSecondaryAction(actionCancel);
       
   123 
       
   124     // Launch popup syncronously
       
   125     popup->setAttribute(Qt::WA_DeleteOnClose);
       
   126     // TODO: handle dialog close & user input
       
   127     popup->open();
       
   128 
       
   129 	// continue if ok selected and valid user input exists in line editor
       
   130     /*if (action && action->text() == "Ok" && edit->text() != "") {
       
   131 		numberOfEntries = edit->text().toInt(&err, 10);
       
   132 	}*/
       
   133 	return err;
       
   134 }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 bool Notifications::timeQueryDialog(QDate& date, const QString& text)
       
   138 {
       
   139 	bool err = false;
       
   140     HbDialog *popup = new HbDialog();
       
   141     popup->setDismissPolicy(HbPopup::TapOutside);
       
   142     popup->setTimeout(HbPopup::NoTimeout);
       
   143 	
       
   144 	HbLabel *title = new HbLabel();
       
   145     HbLineEdit *edit = new HbLineEdit();
       
   146 	HbAction *actionOk = new HbAction("Ok");
       
   147 	HbAction *actionCancel = new HbAction("Cancel");
       
   148     
       
   149 	title->setPlainText(text);
       
   150 	popup->setHeadingWidget(title);
       
   151 	popup->setContentWidget(edit);
       
   152 
       
   153     HbValidator *validator =new HbValidator;
       
   154     QString dateString("(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\\d\\d");
       
   155 	QRegExp rxDate(dateString);
       
   156 	validator->addField(new QRegExpValidator(rxDate, 0), "");
       
   157     edit->setValidator(validator);
       
   158 	edit->setText(date.toString());
       
   159 	edit->setSelection(0, date.toString().length());
       
   160 
       
   161     
       
   162 	popup->setPrimaryAction(actionOk);
       
   163     popup->setSecondaryAction(actionCancel);
       
   164 
       
   165     // Launch popup syncronously
       
   166     popup->setAttribute(Qt::WA_DeleteOnClose);
       
   167     // TODO: handle dialog close & user input
       
   168     popup->open();
       
   169 
       
   170 	// continue if ok selected and valid user input exists in line editor
       
   171     /*if (action && action->text() == "Ok" && edit->text() != "") {
       
   172 		date = QDate::fromString(edit->text());
       
   173         err = true;
       
   174 	}*/
       
   175 	return err;    
       
   176 }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 
       
   180 bool Notifications::yesNoQueryDialog(const QString& text)
       
   181 {
       
   182     HbMessageBox::question(text, 0, 0);
       
   183     return false;
       
   184 }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 
       
   188 void Notifications::popupListDialog(const QString& text, QStringList& items, HbAbstractItemView::SelectionMode mode, QObject* receiver, const char* member)
       
   189 {
       
   190 	HbSelectionDialog *dlg = new HbSelectionDialog;
       
   191 	dlg->setHeadingWidget(new HbLabel(text, dlg));
       
   192 	dlg->setStringItems(items);
       
   193 	dlg->setSelectionMode(mode); 
       
   194 	dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   195 	dlg->open(receiver, member);
       
   196 }
    88 }
   197 
    89 
   198 // ---------------------------------------------------------------------------
    90 // ---------------------------------------------------------------------------
   199 
    91 
   200 bool Notifications::directoryQueryDialog(const QString& text, QString& directory)
    92 bool Notifications::directoryQueryDialog(const QString& text, QString& directory)
   231 	}*/
   123 	}*/
   232 	return err;
   124 	return err;
   233 }
   125 }
   234 
   126 
   235 // ---------------------------------------------------------------------------
   127 // ---------------------------------------------------------------------------
       
   128 
       
   129 CreatorYesNoDialog::CreatorYesNoDialog(MUIObserver* observer, int userData) : 
       
   130     HbMessageBox(HbMessageBox::MessageTypeQuestion, NULL),
       
   131     CreatorDialog(observer, userData)
       
   132 {
       
   133 }
       
   134 
       
   135 void CreatorYesNoDialog::launch(const QString& text, const QString& label, MUIObserver* observer, int userData) throw(std::exception)
       
   136 {
       
   137     CreatorYesNoDialog* dlg = new CreatorYesNoDialog(observer, userData);
       
   138     dlg->setText(text);
       
   139     if(label.length())
       
   140         dlg->setHeadingWidget(new HbLabel(label, dlg));
       
   141     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   142     dlg->setTimeout(HbPopup::NoTimeout);
       
   143     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   144 }
       
   145 
       
   146 void CreatorYesNoDialog::DialogClosed(HbAction *action)
       
   147 {
       
   148     TBool PositiveAction(EFalse);
       
   149     if(action && (action->softKeyRole()==QAction::PositiveSoftKey || !action->text().compare("yes", Qt::CaseInsensitive))){
       
   150         PositiveAction = ETrue;
       
   151     }
       
   152     NotifyObserver(PositiveAction);
       
   153 }
       
   154 
       
   155 CreatorInputDialog::CreatorInputDialog(int* value, MUIObserver* module, int userData) : 
       
   156     HbInputDialog(NULL),
       
   157     CreatorDialog(module, userData),
       
   158     mValue(value)
       
   159 {
       
   160     if(!value)
       
   161         throw std::invalid_argument("value cannot be the null!");
       
   162 }
       
   163 
       
   164 void CreatorInputDialog::launch(const QString& label, int* value, bool acceptsZero, MUIObserver* observer, int userData) throw( std::exception )
       
   165 {
       
   166     CreatorInputDialog* dlg = new CreatorInputDialog(value, observer, userData);
       
   167     dlg->setPromptText(label);
       
   168     dlg->setInputMode(IntInput);
       
   169     HbValidator *validator = new HbValidator(dlg);
       
   170     QString tmp;
       
   171     if (acceptsZero == false) {
       
   172         tmp.append("[1-9]{1}\\d{1,4}");
       
   173     }
       
   174     else {
       
   175         tmp.append("^[0-9]{5}");
       
   176     }
       
   177         
       
   178     QRegExp rxBasic(tmp);
       
   179     validator->addField(new QRegExpValidator(rxBasic, 0), "");
       
   180     dlg->setValidator(validator);
       
   181     dlg->setValue(QVariant(*value));
       
   182     dlg->lineEdit()->setSelection(0, dlg->value().toString().length());
       
   183     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   184     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   185 }
       
   186 
       
   187 void CreatorInputDialog::DialogClosed(HbAction *action)
       
   188 {
       
   189     TBool PositiveAction(EFalse);
       
   190     if(action && (action->softKeyRole()==QAction::PositiveSoftKey || !action->text().compare("ok", Qt::CaseInsensitive))){
       
   191         bool ok = false;
       
   192         *mValue = value().toInt(&ok);
       
   193         PositiveAction = ok ? ETrue : EFalse;
       
   194     }
       
   195     NotifyObserver(PositiveAction);
       
   196 }
       
   197 
       
   198 CreatorSelectionDialog::CreatorSelectionDialog(int* selectedItem, MUIObserver* observer, int userData) throw(std::exception) : 
       
   199     HbSelectionDialog(NULL),
       
   200     CreatorDialog(observer, userData),
       
   201     mSelectedItem(selectedItem),
       
   202     mSelectedItems(NULL)
       
   203 {
       
   204     if(!mSelectedItem)
       
   205         throw std::invalid_argument("selectedItem cannot be null!");
       
   206 }
       
   207 
       
   208 CreatorSelectionDialog::CreatorSelectionDialog(CArrayFixFlat<TInt>* selectedItems, MUIObserver* observer, int userData) throw(std::exception) : 
       
   209     HbSelectionDialog(NULL),
       
   210     CreatorDialog(observer, userData),
       
   211     mSelectedItem(NULL),
       
   212     mSelectedItems(selectedItems)
       
   213 {
       
   214     if(!mSelectedItems)
       
   215         throw std::invalid_argument("selectedItems cannot be null!");
       
   216 }
       
   217 
       
   218 void CreatorSelectionDialog::launch(const QString& label, const QStringList& items, int* selectedItem, MUIObserver* observer, int userData) throw(std::exception)
       
   219 {
       
   220     CreatorSelectionDialog* dlg = new CreatorSelectionDialog(selectedItem, observer, userData);
       
   221     if(label.length())
       
   222         dlg->setHeadingWidget(new HbLabel(label, dlg));
       
   223     dlg->setStringItems(items);
       
   224     dlg->setSelectionMode(HbAbstractItemView::SingleSelection); 
       
   225     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   226     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   227 }
       
   228 
       
   229 void CreatorSelectionDialog::launch(const QString& label, const QStringList& items, CArrayFixFlat<TInt>* selectedItems, MUIObserver* observer, int userData) throw(std::exception)
       
   230 {
       
   231     CreatorSelectionDialog* dlg = new CreatorSelectionDialog(selectedItems, observer, userData);
       
   232     if(label.length())
       
   233         dlg->setHeadingWidget(new HbLabel(label, dlg));
       
   234     dlg->setStringItems(items);
       
   235     dlg->setSelectionMode(HbAbstractItemView::MultiSelection); 
       
   236     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   237     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   238 }
       
   239 
       
   240 void CreatorSelectionDialog::DialogClosed(HbAction *action)
       
   241 {
       
   242     TBool PositiveAction(EFalse);
       
   243     //CreatorSelectionDialog* dlg = qobject_cast<CreatorSelectionDialog*>(sender());
       
   244     if(!action || (action && action->softKeyRole()==QAction::SelectSoftKey) || (action && !action->text().compare("ok", Qt::CaseInsensitive))){
       
   245         if(selectedItems().count()){
       
   246             try{
       
   247                 if( selectionMode() == HbAbstractItemView::SingleSelection && mSelectedItem){
       
   248                     bool ok(false);
       
   249                     *mSelectedItem = selectedItems().at(0).toInt(&ok);
       
   250                     if(!ok)
       
   251                         throw std::invalid_argument("cannot obtain selected item!");
       
   252                 }
       
   253                 if( selectionMode() == HbAbstractItemView::MultiSelection && mSelectedItems){
       
   254                     QList<QVariant> items = selectedItems();
       
   255                     foreach( QVariant item, items){
       
   256                         QT_TRAP_THROWING( mSelectedItems->AppendL( item.toInt() ) );
       
   257                     }
       
   258                 }
       
   259                 PositiveAction = ETrue;
       
   260             }
       
   261             catch (std::exception& e){
       
   262                 Notifications::error( QString("exception: ")+e.what() );
       
   263             }
       
   264         }
       
   265     }
       
   266     NotifyObserver(PositiveAction);
       
   267 }
       
   268 
       
   269 CreatorDateTimeDialog::CreatorDateTimeDialog(TTime* value, MUIObserver* observer, int userData) : 
       
   270     HbDialog(NULL),
       
   271     CreatorDialog(observer, userData),
       
   272     mValue(value)
       
   273 {
       
   274 }
       
   275 
       
   276 void CreatorDateTimeDialog::launch(const QString& label, TTime* value, MUIObserver* observer, int userData) throw( std::exception )
       
   277 {
       
   278     CreatorDateTimeDialog* dlg = new CreatorDateTimeDialog(value, observer, userData);
       
   279     if(!value)
       
   280         throw std::invalid_argument("value cannot be the null!");
       
   281     if(label.length())
       
   282         dlg->setHeadingWidget( new HbLabel(label, dlg) );
       
   283     
       
   284     // TTime to QDate
       
   285     TBuf<20> timeString;
       
   286     _LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3");
       
   287     TRAP_IGNORE( value->FormatL(timeString, KDateString) );
       
   288     QString temp = QString::fromUtf16(timeString.Ptr(), timeString.Length());
       
   289     temp.replace(QChar('/'), QChar('-'));
       
   290     QDate date = QDate::fromString(temp, "dd-MM-yyyy");
       
   291     HbDateTimePicker* widget = new HbDateTimePicker( date, dlg );
       
   292     dlg->setContentWidget( widget );
       
   293     dlg->addAction(new HbAction("Ok", dlg));
       
   294     dlg->addAction(new HbAction("Cancel", dlg));
       
   295     dlg->setModal(true); // Dialog is modal  
       
   296     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   297     dlg->setTimeout(HbPopup::NoTimeout);
       
   298     dlg->open(dlg, SLOT(DialogClosed(HbAction*)));
       
   299 }
       
   300 
       
   301 void CreatorDateTimeDialog::DialogClosed(HbAction *action)
       
   302 {
       
   303     TBool PositiveAction(EFalse);
       
   304     if(action && (action->softKeyRole()==QAction::PositiveSoftKey || !action->text().compare("ok", Qt::CaseInsensitive))){
       
   305         QString str = qobject_cast<HbDateTimePicker*>(contentWidget())->date().toString(Qt::ISODate);
       
   306         str.remove('-');
       
   307         str += ":000000";// 0h 0m 0s
       
   308         TBuf<30> dateTimeString(str.utf16());
       
   309         mValue->Set(dateTimeString);
       
   310         PositiveAction = ETrue;
       
   311     }
       
   312     NotifyObserver(PositiveAction);
       
   313 }
       
   314 //End of File