src/hbwidgets/popups/hblistdialog.cpp
changeset 2 06ff229162e9
parent 1 f7ac710697a9
child 3 11d3954df52a
child 4 ae1717029441
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 #include "hblistdialog.h"
       
    26 #include "hbwidget_p.h"
       
    27 #include "hblistdialog_p.h"
       
    28 #include "hblabel.h"
       
    29 #include "hbaction.h"
       
    30 #include "hbabstractviewitem.h"
       
    31 #include <hbinstance.h>
       
    32 #include "hbglobal_p.h"
       
    33 
       
    34 #include <QtDebug>
       
    35 #include <QGraphicsScene>
       
    36 #include <hblistwidgetitem.h>
       
    37 #include <hblistwidget.h>
       
    38 #include <hbradiobuttonlist.h>
       
    39 /*!
       
    40   \this class is deprecated. Use HbSelectionDialog.
       
    41 */
       
    42 
       
    43 /*!
       
    44   \deprecated HbListDialog(QGraphicsItem*)
       
    45 	is deprecated. \this is deprecated, use HbSelectionDialog.
       
    46 
       
    47   Constructor	
       
    48 */
       
    49 
       
    50 HbListDialog::HbListDialog(QGraphicsItem* parent): 
       
    51                                 HbDialog(*new HbListDialogPrivate, parent)
       
    52 {
       
    53 	HB_DEPRECATED("HbListDialog class is deprecated. Use HbSelectionDialog");
       
    54 
       
    55     Q_D(HbListDialog);
       
    56     d->init();
       
    57     setDismissPolicy(NoDismiss);
       
    58 }
       
    59 
       
    60 /*!
       
    61   @beta
       
    62   Destructor 
       
    63  */
       
    64 HbListDialog::~HbListDialog()
       
    65 {
       
    66 }
       
    67 
       
    68 /*!
       
    69  @beta
       
    70  Protected. 
       
    71  */
       
    72 void HbListDialog::showEvent(QShowEvent *event)
       
    73 {    
       
    74     HbDialog::showEvent(event);
       
    75 }
       
    76 
       
    77 /*!
       
    78     @beta
       
    79    Sets the \a SelectionMode of the list. It can be 
       
    80    \a SingleSelection or \a MultiSelection or \a NoSelection. 
       
    81    If \a ListMode is \a RadioButtonListInput, \a MultiSelection mode will not 
       
    82    work. Default value is \a NoSelection.
       
    83    \sa selectionMode. 
       
    84  */
       
    85 void HbListDialog::setSelectionMode(HbAbstractItemView::SelectionMode mode)
       
    86 {
       
    87 	Q_D(HbListDialog);
       
    88 	
       
    89 	d->setSelectionMode(mode);
       
    90 }
       
    91 
       
    92 /*!
       
    93     @beta
       
    94     Returns current \a SelectionMode of the list. It can be set
       
    95     by \a setSelectionMode(). Default value is \a NoSelection.
       
    96 */	
       
    97 HbAbstractItemView::SelectionMode HbListDialog::selectionMode() const
       
    98 {
       
    99 	Q_D(const HbListDialog);
       
   100 	return d->mSelectionMode;
       
   101 }
       
   102 
       
   103 /*!
       
   104     @beta
       
   105     Sets the string list items to be displayed.\a items is the
       
   106     list of strings and \a current is the index of default selection.
       
   107     \sa stringItems()
       
   108 */	
       
   109 void HbListDialog::setStringItems(const QStringList &items,int current)
       
   110 {
       
   111 	Q_D(HbListDialog);
       
   112 	d->setStringItems(items,current);
       
   113 }
       
   114 
       
   115 /*!
       
   116     @beta
       
   117     Returns list of string list items earlier set by \a setStringItems().
       
   118 */	
       
   119 QStringList HbListDialog::stringItems() const
       
   120 {
       
   121 	Q_D(const HbListDialog);
       
   122 	return d->stringItems();
       
   123 }
       
   124 
       
   125 /*!
       
   126    @beta
       
   127    Returns list of selected indexes. List contains only one item if
       
   128    \a SelectionMode is \a NoSelection or \a SingleSelection. It may 
       
   129    contain more items if \a SelectionMode is \a MultiSelection.
       
   130    \sa 	setSelectionMode(), \sa selectionMode()
       
   131  */
       
   132 QList<int> HbListDialog::selectedItems() const
       
   133 {
       
   134 	Q_D(const HbListDialog);
       
   135 	return d->selectedItems();
       
   136 }
       
   137 
       
   138 /*!
       
   139    @beta
       
   140    set the item selected.
       
   141    It can select one item if \a Selection mode is \a SingleSelection
       
   142    it can select more item if \a SelectionMode is \a MultiSelection.
       
   143 */
       
   144 void HbListDialog::setSelectedItems(QList<int> items) 
       
   145 {
       
   146 	Q_D(HbListDialog);
       
   147 	d->setSelectedItems(items);
       
   148 }
       
   149 
       
   150 /*!
       
   151     @beta
       
   152    Returns list of selected model indexes. List contains only one item if
       
   153    \a SelectionMode is \a NoSelection or \a SingleSelection. It may 
       
   154    contain more items if \a SelectionMode is \a MultiSelection.
       
   155    \sa 	setSelectionMode(), \sa selectionMode()
       
   156  */
       
   157 QModelIndexList HbListDialog::selectedModelIndexes() const
       
   158 {
       
   159 	Q_D(const HbListDialog);
       
   160 	return d->selectedModelIndexes();
       
   161 }
       
   162 
       
   163 /*!
       
   164     @beta
       
   165     Sets the list of custom list items to be displayed.\a items is the
       
   166     list of custom items.\a bTransferOwnership is a flag defining the owner
       
   167     of the items. If \a true, items will be deleted when dialog is deleted else
       
   168     user is responsible for deleting the items.Default value is \a false.
       
   169     \a current is the index of default selection.
       
   170     \sa widgetItems();
       
   171 */	
       
   172 void HbListDialog::setWidgetItems(QList<HbListWidgetItem*> &items,bool bTransferOwnership,int current)
       
   173 {
       
   174 	Q_D(HbListDialog);
       
   175 	d->setWidgetItems(items,bTransferOwnership,current);
       
   176 }
       
   177 
       
   178 /*!
       
   179     @beta
       
   180     Returns list of custom list items earlier set by \a setWidgetItems().
       
   181 */	
       
   182 QList<HbListWidgetItem*> HbListDialog::widgetItems() const
       
   183 {
       
   184 	Q_D(const HbListDialog);
       
   185 	return d->widgetItems();
       
   186 }
       
   187 
       
   188 /*!
       
   189     @beta
       
   190     Sets the Model containing data for the list items.
       
   191 */	
       
   192 void HbListDialog::setModel(QAbstractItemModel* model)
       
   193 {
       
   194 	Q_D(HbListDialog);
       
   195 	d->setModel(model);
       
   196 }
       
   197 
       
   198 /*!
       
   199     @beta
       
   200     Returns Model stored by list.
       
   201 */	
       
   202 QAbstractItemModel* HbListDialog::model() const
       
   203 {
       
   204 	Q_D(const HbListDialog);
       
   205 	return d->model();
       
   206 }
       
   207 /*!                                  
       
   208     Static convenience function to let the user select item(s) from a
       
   209     string list. \a label is the text which is shown to the user (it
       
   210     should say what should be entered). \a list is the string list which 
       
   211     is inserted into the list and \a current is 
       
   212     the number of the item which should be the current item. 
       
   213 
       
   214     If \a ok is non-null \e *\a ok will be set to true if the user
       
   215     pressed \gui OK and to false if the user pressed \gui Cancel. \a mode
       
   216     defines the selection mode of the list. It can be either of \NoSelection,
       
   217     \SingleSelection and \MultiSelection. The dialog's parent is \a parent. 
       
   218     The dialog will be modal.
       
   219 
       
   220     this functions connects to the receiverd slot with signature finished(HbAction*).
       
   221     there the selectedItems has to be queried.
       
   222 */
       
   223 void HbListDialog::getStringItems(const QString &label, 
       
   224                                     const QStringList &list,
       
   225                                     QObject *receiver,
       
   226                                     const char *member,
       
   227                                     int current,
       
   228                                     HbAbstractItemView::SelectionMode mode, 
       
   229                                     QGraphicsScene *scene, 
       
   230                                     QGraphicsItem *parent)
       
   231 {
       
   232 	HbListDialog *dlg = new HbListDialog(parent);
       
   233     if (scene && !parent) {
       
   234         scene->addItem(dlg);
       
   235     }
       
   236 	QStringList result;
       
   237 	QList<int> selIndexes;
       
   238     selIndexes << current;
       
   239     if(!label.isNull()) {
       
   240         dlg->setHeadingWidget(new HbLabel(label));
       
   241     }
       
   242     dlg->setStringItems(list,current);
       
   243 	dlg->setSelectionMode(mode);
       
   244     dlg->setSelectedItems(selIndexes);
       
   245     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   246     dlg->open(receiver,member);
       
   247 }
       
   248 
       
   249 /*!
       
   250    Static convenience function to let the user select item(s) from a
       
   251     list of user defined items. \a label is the text which is shown to the 
       
   252     user (it should say what should be entered). \a list is the list 
       
   253     of user defined items shown to the user.Ownership is not transferred.
       
   254     \a current is the number of the item which should be the current item. 
       
   255 
       
   256     If \a ok is non-null \e *\a ok will be set to true if the user
       
   257     pressed \gui OK and to false if the user pressed \gui Cancel. \a mode
       
   258     defines the selection mode of the list. It can be either of \NoSelection,
       
   259     \SingleSelection and \MultiSelection. The dialog's parent is \a parent. 
       
   260     The dialog will be modal.
       
   261 
       
   262     this functions connects to the receiverd slot with signature finished(HbAction*).
       
   263     there the selectedItems has to be queried.
       
   264 */
       
   265 void HbListDialog::getWidgetItems(const QString &label, 
       
   266                                     QList<HbListWidgetItem*> &list,
       
   267                                     QObject *receiver,
       
   268                                     const char *member,
       
   269                                     int current,
       
   270                                     HbAbstractItemView::SelectionMode mode, 
       
   271                                     QGraphicsScene *scene, 
       
   272                                     QGraphicsItem *parent)
       
   273 {
       
   274     HbListDialog *dlg = new HbListDialog(parent);
       
   275     if (scene && !parent) {
       
   276         scene->addItem(dlg);
       
   277     }
       
   278     QList<HbListWidgetItem*> result;
       
   279     QList<int> selIndexes;
       
   280     selIndexes << current;
       
   281     if(!label.isNull()) {
       
   282         dlg->setHeadingWidget(new HbLabel(label));
       
   283     }
       
   284 	dlg->setWidgetItems(list,false,current);
       
   285 	dlg->setSelectionMode(mode);
       
   286     dlg->setSelectedItems(selIndexes);
       
   287     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   288     dlg->open(receiver,member);
       
   289 }
       
   290 
       
   291 /*!
       
   292     Static convenience function to let the user select item(s) from a
       
   293     list of items defined by a user set model.\a label is the text which 
       
   294     is shown to the user (it should say what should be entered). \a model 
       
   295     is user defined model which list will use to render the items.
       
   296 	Ownership is not transferred.
       
   297     If \a ok is non-null \e *\a ok will be set to true if the user
       
   298     pressed \gui OK and to false if the user pressed \gui Cancel. \a mode
       
   299     defines the selection mode of the list. It can be either of \NoSelection,
       
   300     \SingleSelection and \MultiSelection. 
       
   301     The dialog's parent is \a parent. The dialog will be modal.
       
   302 
       
   303     this functions connects to the receiverd slot with signature finished(HbAction*).
       
   304     there the selectedItems has to be queried.
       
   305 */
       
   306 void HbListDialog::getModelIndexes(const QString &label, QAbstractItemModel* model,
       
   307                                    QObject *receiver,
       
   308                                    const char *member,
       
   309                                    HbAbstractItemView::SelectionMode mode, QGraphicsScene *scene, QGraphicsItem *parent)
       
   310 {
       
   311     HbListDialog *dlg = new HbListDialog(parent);
       
   312     if (scene && !parent) {
       
   313         scene->addItem(dlg);
       
   314     }
       
   315     QModelIndexList result;
       
   316     if(!label.isNull()) {
       
   317         dlg->setHeadingWidget(new HbLabel(label));
       
   318     }
       
   319 	dlg->setModel(model);
       
   320 	dlg->setSelectionMode(mode);
       
   321     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   322     dlg->open(receiver,member);
       
   323 }
       
   324 
       
   325 
       
   326 /*!
       
   327     \deprecated HbListDialog::getStringItems(const QString&,const QStringList,int,bool*,HbAbstractItemView::SelectionMode,QGraphicsScene*,QGraphicsItem*)
       
   328         is deprecated. Please use the other available HbListDialog::getStringItems(...) API.
       
   329     
       
   330 	Static convenience function to let the user select item(s) from a
       
   331     string list. \a label is the text which is shown to the user (it
       
   332     should say what should be entered). \a list is the string list which 
       
   333     is inserted into the list and \a current is 
       
   334     the number of the item which should be the current item. 
       
   335 
       
   336     If \a ok is non-null \e *\a ok will be set to true if the user
       
   337     pressed \gui OK and to false if the user pressed \gui Cancel. \a mode
       
   338     defines the selection mode of the list. It can be either of \NoSelection,
       
   339     \SingleSelection and \MultiSelection. The dialog's parent is \a parent. 
       
   340     The dialog will be modal.
       
   341 
       
   342     This function returns the list of selected string items.
       
   343     List will contain only one item if \a SelectionMode is \a NoSelection or \a SingleSelection 
       
   344     and may contain more items in case of \a MultiSelection.
       
   345 */
       
   346 QStringList HbListDialog::getStringItems(const QString &label, const QStringList &list,int current,
       
   347                                                 bool *ok,HbAbstractItemView::SelectionMode mode, QGraphicsScene *scene, QGraphicsItem *parent)
       
   348 {
       
   349     HB_DEPRECATED("HbListDialog::getStringItems is deprecated. Use HbListDialog::getStringItems(const QString &,const QStringList &,QObject *receiver,const char *,int,HbAbstractItemView::SelectionMode, QGraphicsScene *, QGraphicsItem *)");
       
   350 
       
   351 	Q_UNUSED(parent)
       
   352 	
       
   353 	HbListDialog *dlg = new HbListDialog();
       
   354     if (scene) {
       
   355         scene->addItem(dlg);
       
   356     }
       
   357     QStringList result;
       
   358     QList<int> selIndexes;
       
   359     selIndexes << current;
       
   360     if(!label.isNull())
       
   361         dlg->setHeadingWidget(new HbLabel(label));
       
   362     dlg->setStringItems(list,current);
       
   363     dlg->setSelectionMode(mode);
       
   364     dlg->setSelectedItems(selIndexes);
       
   365     HbAction* action = dlg->exec();
       
   366     if(action == dlg->secondaryAction()){ //Cancel was pressed
       
   367         if(ok)
       
   368             *ok = false;
       
   369     }
       
   370     else{ //OK was pressed
       
   371         if(ok)
       
   372             *ok = true;
       
   373         selIndexes = dlg->selectedItems();
       
   374     }
       
   375     delete dlg;
       
   376     for(int i = 0; i < selIndexes.count(); i++ ){
       
   377         result += list[selIndexes[i]];	
       
   378     }
       
   379     return result;
       
   380 }
       
   381 
       
   382 /*!
       
   383     \deprecated HbListDialog::getWidgetItems(const QString &, QList<HbListWidgetItem*> &,int,bool *,HbAbstractItemView::SelectionMode, QGraphicsScene *, QGraphicsItem *)
       
   384         is deprecated. Use the other available HbListDialog::getWidgetItems(...) API.
       
   385 
       
   386     Static convenience function to let the user select item(s) from a
       
   387     list of user defined items. \a label is the text which is shown to the 
       
   388     user (it should say what should be entered). \a list is the list 
       
   389     of user defined items shown to the user.Ownership is not transferred.
       
   390     \a current is the number of the item which should be the current item. 
       
   391 
       
   392     If \a ok is non-null \e *\a ok will be set to true if the user
       
   393     pressed \gui OK and to false if the user pressed \gui Cancel. \a mode
       
   394     defines the selection mode of the list. It can be either of \NoSelection,
       
   395     \SingleSelection and \MultiSelection. The dialog's parent is \a parent. 
       
   396     The dialog will be modal.
       
   397 
       
   398     This function returns the list of selected widget items.
       
   399     List will contain only one item if \a SelectionMode is \a NoSelection or
       
   400     \SingleSelection and may contain more items in case of \a MultipleSelection.
       
   401 */
       
   402 QList<HbListWidgetItem*> HbListDialog::getWidgetItems(const QString &label, QList<HbListWidgetItem*> &list,int current,
       
   403                                                         bool *ok,HbAbstractItemView::SelectionMode mode, QGraphicsScene *scene, QGraphicsItem *parent)
       
   404 {
       
   405     HB_DEPRECATED("HbListDialog::getWidgetItems is deprecated. Use HbListDialog::getWidgetItems(const QString &, QList<HbListWidgetItem*> &list,QObject *,const char *,int,HbAbstractItemView::SelectionMode, QGraphicsScene *, QGraphicsItem *)");
       
   406 
       
   407     Q_UNUSED(parent)
       
   408     HbListDialog *dlg = new HbListDialog();
       
   409     if (scene) {
       
   410         scene->addItem(dlg);
       
   411     }
       
   412     QList<HbListWidgetItem*> result;
       
   413     QList<int> selIndexes;
       
   414     selIndexes << current;
       
   415     if(!label.isNull())
       
   416         dlg->setHeadingWidget(new HbLabel(label));
       
   417     dlg->setWidgetItems(list,false,current);
       
   418     dlg->setSelectionMode(mode);
       
   419     dlg->setSelectedItems(selIndexes);
       
   420     HbAction* action = dlg->exec();
       
   421     if(action == dlg->secondaryAction()){ //Cancel was pressed
       
   422         if(ok)
       
   423             *ok = false;
       
   424     }
       
   425     else{ //OK was pressed
       
   426         if(ok)
       
   427             *ok = true;
       
   428         selIndexes = dlg->selectedItems();
       
   429     }
       
   430 	for(int i = 0; i < selIndexes.count(); i++ ){
       
   431 		result.append(list.at(selIndexes[i]));	
       
   432 	}
       
   433 	
       
   434 	delete dlg;
       
   435 
       
   436 	return result;
       
   437 }
       
   438 
       
   439 /*!
       
   440     \deprecated HbListDialog::getModelIndexes(const QString &, QAbstractItemModel* ,bool *,HbAbstractItemView::SelectionMode , QGraphicsScene *, QGraphicsItem *)
       
   441         is deprecated. Use the other available HbListDialog::getModexIndexes(...) API.
       
   442 
       
   443     Static convenience function to let the user select item(s) from a
       
   444     list of items defined by a user set model.\a label is the text which 
       
   445     is shown to the user (it should say what should be entered). \a model 
       
   446     is user defined model which list will use to render the items.
       
   447 	Ownership is not transferred.
       
   448     If \a ok is non-null \e *\a ok will be set to true if the user
       
   449     pressed \gui OK and to false if the user pressed \gui Cancel. \a mode
       
   450     defines the selection mode of the list. It can be either of \NoSelection,
       
   451     \SingleSelection and \MultiSelection. 
       
   452     The dialog's parent is \a parent. The dialog will be modal.
       
   453 
       
   454     This function returns the model indexes selcted items in the list.
       
   455     List will contain only one item if \a SelectionMode is \a SingleSelection or
       
   456     \NoSelection and may contain more items in case of \a MultipleSelection.
       
   457 */
       
   458 QModelIndexList HbListDialog::getModelIndexes(const QString &label, QAbstractItemModel* model,bool *ok,HbAbstractItemView::SelectionMode mode, QGraphicsScene *scene, QGraphicsItem *parent)
       
   459 {
       
   460     HB_DEPRECATED("HbListDialog::getModelIndexes is deprecated. Use HbListDialog::getModelIndexes(const QString &, QAbstractItemModel*,QObject *,const char *,HbAbstractItemView::SelectionMode, QGraphicsScene *, QGraphicsItem *)");
       
   461 
       
   462     Q_UNUSED(parent)
       
   463     HbListDialog *dlg = new HbListDialog();
       
   464     if (scene) {
       
   465         scene->addItem(dlg);
       
   466     }
       
   467     QModelIndexList result;
       
   468         if(!label.isNull())
       
   469             dlg->setHeadingWidget(new HbLabel(label));
       
   470 	dlg->setModel(model);
       
   471 	dlg->setSelectionMode(mode);
       
   472 	HbAction* action = dlg->exec();
       
   473 	if(action == dlg->secondaryAction()){ //Cancel was pressed
       
   474 		if(ok)
       
   475 			*ok = false;
       
   476 	}
       
   477 	else{ //OK was pressed
       
   478 		if(ok)
       
   479 			*ok = true;
       
   480 		result = dlg->selectedModelIndexes();
       
   481 	}
       
   482 	dlg->setModel(0);
       
   483 	delete dlg;
       
   484 	return result;
       
   485 }
       
   486 
       
   487 #include "moc_hblistdialog.cpp"