src/hbwidgets/popups/hbselectiondialog_p.cpp
changeset 1 f7ac710697a9
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
       
     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 
       
    26 #include <QGraphicsLinearLayout>
       
    27 #include <qglobal.h>
       
    28 #include "hbabstractviewitem.h"
       
    29 #include "hbselectiondialog_p.h"
       
    30 #include <hblabel.h>
       
    31 #include <hblistwidget.h>
       
    32 #include <hblistwidgetitem.h>
       
    33 #include <QtDebug>
       
    34 #include <hbcheckbox.h>
       
    35 #include <hbaction.h>
       
    36 
       
    37 
       
    38 HbSelectionDialogContentWidget::HbSelectionDialogContentWidget(HbSelectionDialogPrivate *priv):HbWidget(),
       
    39 						mListWidget(0),d(priv),chkMark(0),lbCounter(0)
       
    40 {
       
    41     mListWidget = new HbListWidget(this);
       
    42 	HbStyle::setItemName(mListWidget, "list");
       
    43     QObject::connect(mListWidget,SIGNAL(activated(const QModelIndex&)),this,SLOT(_q_listItemSelected(QModelIndex)));
       
    44     QObject::connect(mListWidget,SIGNAL(activated(HbListWidgetItem *)),this,SLOT(_q_listWidgetItemSelected(HbListWidgetItem *)));
       
    45 }
       
    46 
       
    47 
       
    48 void HbSelectionDialogContentWidget::_q_listWidgetItemSelected(HbListWidgetItem *item)
       
    49 {
       
    50 	if(item){
       
    51 		updateCounter();
       
    52 	}
       
    53 }
       
    54 
       
    55 
       
    56 void HbSelectionDialogContentWidget::_q_listItemSelected(QModelIndex index)
       
    57 {
       
    58 	Q_UNUSED(index)
       
    59 	if(mListWidget->selectionMode()== HbAbstractItemView::SingleSelection ||
       
    60 	   mListWidget->selectionMode()== HbAbstractItemView::NoSelection){
       
    61 	   d->close();   
       
    62 	}
       
    63 	updateCounter();
       
    64 }
       
    65 
       
    66 int HbSelectionDialogContentWidget::selectedItemCount() const
       
    67 {
       
    68 	int selectedItems = 0;
       
    69 	QItemSelectionModel* selectionModel = mListWidget->selectionModel();
       
    70 	if(selectionModel){
       
    71 		selectedItems = selectionModel->selectedRows().count();
       
    72 	}
       
    73 	return selectedItems;
       
    74 }
       
    75 
       
    76 int HbSelectionDialogContentWidget::totalItemCount() const
       
    77 {
       
    78 	return mListWidget->count();
       
    79 }
       
    80 
       
    81 void HbSelectionDialogContentWidget::updateCounter()
       
    82 {
       
    83 	if(mListWidget->selectionMode()!= HbAbstractItemView::MultiSelection) return;
       
    84 	if(chkMark && lbCounter){
       
    85 		int totalItems = totalItemCount();
       
    86 		int selectedItems = selectedItemCount();
       
    87 
       
    88 		lbCounter->setText(QString(QString::number(selectedItems) + "/" + QString::number(totalItems)));
       
    89 		//update checked state of "MarkAll" checkbox 
       
    90 		if (selectedItems == totalItems){
       
    91 			chkMark->blockSignals(true); //should not call _q_checkboxclicked()
       
    92 			chkMark->setChecked(true);
       
    93 			chkMark->blockSignals(false);
       
    94 		}
       
    95 		else{
       
    96 			chkMark->blockSignals(true); //should not call _q_checkboxclicked()
       
    97 			chkMark->setChecked(false);
       
    98 			chkMark->blockSignals(false);
       
    99 		}
       
   100 	}
       
   101 }
       
   102 
       
   103 void HbSelectionDialogContentWidget::_q_checkboxclicked(int value)
       
   104 {
       
   105 	int totalItems = 0;
       
   106 	int selectedItems = 0;
       
   107 	QAbstractItemModel* itemModel = mListWidget->model();
       
   108 	QModelIndex indexStart,indexEnd;
       
   109 	if(itemModel){
       
   110 		indexStart = itemModel->index(0,0);
       
   111 		indexEnd = itemModel->index(itemModel->rowCount()-1,0);
       
   112 		totalItems = itemModel->rowCount();
       
   113 	}
       
   114 
       
   115 	QItemSelectionModel* selectionModel = mListWidget->selectionModel();
       
   116 	if(selectionModel){
       
   117 		selectedItems = selectionModel->selectedRows().count();
       
   118 		if(value){ //Select All
       
   119 			selectionModel->select(QItemSelection(indexStart,indexEnd),QItemSelectionModel::Select);
       
   120 		}
       
   121 		else{ //Select None
       
   122 			selectionModel->clear();
       
   123 		}
       
   124 	}
       
   125 	updateCounter();
       
   126 }
       
   127 
       
   128 
       
   129 void HbSelectionDialogContentWidget::showMarkWidget(bool bShow)
       
   130 {
       
   131 	if(bShow){
       
   132 			chkMark = new HbCheckBox(this);
       
   133 			chkMark->setText("Mark All");
       
   134 			lbCounter = new HbTextItem(this);
       
   135 			HbStyle::setItemName(chkMark,"checkbox");
       
   136 			HbStyle::setItemName(lbCounter,"counter");
       
   137 			setProperty("multiSelection",true);
       
   138 			connect(chkMark,SIGNAL(stateChanged ( int )),this,SLOT(_q_checkboxclicked(int)));
       
   139 			updateCounter();
       
   140 	}
       
   141 	else{
       
   142 		delete chkMark;chkMark=0;
       
   143 		delete lbCounter;lbCounter=0;
       
   144 		HbStyle::setItemName(chkMark,"");
       
   145 		HbStyle::setItemName(lbCounter,"");
       
   146 		setProperty("multiSelection",false);
       
   147 	}
       
   148 }
       
   149 
       
   150 HbSelectionDialogPrivate::HbSelectionDialogPrivate()
       
   151     :HbDialogPrivate()
       
   152 {
       
   153 	bOwnItems = false;
       
   154 }
       
   155 
       
   156 
       
   157 HbSelectionDialogPrivate::~HbSelectionDialogPrivate()
       
   158 {
       
   159 	if(!bOwnItems){
       
   160 			Q_Q(HbSelectionDialog);
       
   161 			HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   162 			if(cWidget){
       
   163 				if(cWidget->mListWidget){
       
   164                     int nRows = 0;
       
   165                     QAbstractItemModel* itemModel = cWidget->mListWidget->model();
       
   166                     if(itemModel){
       
   167                         nRows = itemModel->rowCount();
       
   168                         while(nRows){
       
   169                             cWidget->mListWidget->takeItem(0);
       
   170                             nRows = itemModel->rowCount();
       
   171                         }
       
   172                     }
       
   173 				}
       
   174 			}
       
   175 	}
       
   176 }
       
   177 void HbSelectionDialogPrivate::init()
       
   178 {
       
   179     qDebug()<<" Entering init()";
       
   180     Q_Q(HbSelectionDialog);
       
   181 
       
   182     bOwnItems = false;
       
   183 
       
   184     HbSelectionDialogContentWidget* contentWidget = new HbSelectionDialogContentWidget(this);
       
   185     q->setContentWidget(contentWidget);
       
   186 
       
   187     q->setPrimaryAction(new HbAction(QString(q->tr("Ok"))));
       
   188 
       
   189     q->setSecondaryAction(new HbAction(QString(q->tr("Cancel"))));
       
   190     q->setTimeout(0);
       
   191     q->setModal(true);
       
   192 
       
   193 
       
   194 }
       
   195 
       
   196 
       
   197 
       
   198 void HbSelectionDialogPrivate::setSelectionMode(HbAbstractItemView::SelectionMode mode)
       
   199 {
       
   200 	Q_Q(HbSelectionDialog);
       
   201 
       
   202 	mSelectionMode = mode;
       
   203 	switch(mode)
       
   204 	{
       
   205 	case HbAbstractItemView::SingleSelection:
       
   206 	case HbAbstractItemView::MultiSelection:
       
   207 	case HbAbstractItemView::NoSelection:
       
   208 	{
       
   209 		HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   210 		if(cWidget){
       
   211 			cWidget->mListWidget->setSelectionMode(mode);
       
   212 		}
       
   213 		if(mode == HbAbstractItemView::MultiSelection)
       
   214 			cWidget->showMarkWidget(true);	
       
   215 		else
       
   216 			cWidget->showMarkWidget(false);	
       
   217 	}
       
   218 	break;
       
   219 	case HbAbstractItemView::ContiguousSelection:
       
   220 		break;
       
   221 	}
       
   222 }
       
   223 
       
   224 QList<HbListWidgetItem*> HbSelectionDialogPrivate::widgetItems() const
       
   225 {
       
   226 	Q_Q(const HbSelectionDialog);
       
   227 
       
   228 	QList<HbListWidgetItem*> rows;
       
   229 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   230 	if(cWidget){
       
   231 		HbListWidget* widget = qobject_cast<HbListWidget*>(cWidget->mListWidget);
       
   232 		if(widget){
       
   233             int count = 0;
       
   234             QAbstractItemModel* itemModel = widget->model();
       
   235             if(itemModel)
       
   236                 count = itemModel->rowCount();
       
   237 			for(int i = 0; i < count; i++){
       
   238 				rows.append(widget->item(i));
       
   239 			}
       
   240 		}
       
   241 	}
       
   242 	return rows;
       
   243 }
       
   244 
       
   245 void HbSelectionDialogPrivate::setStringItems(const QStringList &items, int currentIndex)
       
   246 {
       
   247 	Q_Q(HbSelectionDialog);
       
   248 
       
   249 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   250 	if(!cWidget) return;
       
   251 	
       
   252     int nRows = 0;
       
   253 
       
   254 	if(cWidget->mListWidget){
       
   255 		int count = items.size();
       
   256 		for (int i = 0; i < count; ++i) {
       
   257 			HbListWidgetItem* modelItem = new HbListWidgetItem();
       
   258 			QString str = items.at(i);
       
   259 			modelItem->setText(str);
       
   260 			cWidget->mListWidget->addItem(modelItem);
       
   261 			
       
   262             QAbstractItemModel* itemModel = cWidget->mListWidget->model();
       
   263             if(itemModel)
       
   264                 nRows = itemModel->rowCount();
       
   265 		}
       
   266 		if(nRows > 0){ //if addition of rows was correct.
       
   267             QList<QVariant> currentRow;
       
   268             currentRow.append(QVariant(currentIndex));
       
   269             setSelectedItems(currentRow);
       
   270 		}
       
   271 		
       
   272 	}
       
   273 }
       
   274 
       
   275 QStringList HbSelectionDialogPrivate::stringItems() const
       
   276 {
       
   277 	QStringList list;
       
   278 	QList<HbListWidgetItem*> items = widgetItems();
       
   279 	int count = items.count();
       
   280 	for(int i = 0; i < count; i++){
       
   281                 QString text = items[i]->text();
       
   282                 if(!text.isEmpty()){
       
   283 			list += text;
       
   284 		}
       
   285 	}
       
   286 	return list;
       
   287 }
       
   288 
       
   289 void HbSelectionDialogPrivate::setModel(QAbstractItemModel* model)
       
   290 {
       
   291 	Q_Q(HbSelectionDialog);
       
   292 
       
   293 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   294 	if(cWidget){
       
   295 		cWidget->mListWidget->HbListView::setModel(model); //HbListView's implementation of setModel()
       
   296 	}
       
   297 }
       
   298 
       
   299 void HbSelectionDialogPrivate::setWidgetItems(const QList<HbListWidgetItem*> &items,bool transferOwnership,int currentIndex)
       
   300 {
       
   301 	Q_Q(HbSelectionDialog);
       
   302 
       
   303 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   304 	if(cWidget){
       
   305 		if(cWidget->mListWidget){
       
   306 			int count = items.count();
       
   307 			for(int i = 0; i < count; i++){
       
   308 				cWidget->mListWidget->addItem(items[i]);
       
   309 			}
       
   310 			cWidget->mListWidget->setCurrentRow(currentIndex);
       
   311 			
       
   312 		}
       
   313 		bOwnItems = transferOwnership;
       
   314 	}
       
   315 }
       
   316 
       
   317 QAbstractItemModel* HbSelectionDialogPrivate::model() const
       
   318 {
       
   319 	Q_Q(const HbSelectionDialog);
       
   320 	
       
   321 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   322 	if(cWidget){
       
   323 		return cWidget->mListWidget->HbListView::model(); //HbListView's implementation of model()
       
   324 	}
       
   325 	return 0;
       
   326 }
       
   327 
       
   328 QItemSelectionModel* HbSelectionDialogPrivate::selectionModel() const
       
   329 {
       
   330 	Q_Q(const HbSelectionDialog);
       
   331 	
       
   332 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   333 	if(cWidget){
       
   334 		return cWidget->mListWidget->selectionModel();
       
   335 	}
       
   336 	return 0;
       
   337 }
       
   338 
       
   339 void HbSelectionDialogPrivate::setSelectedItems(const QList<QVariant> items)
       
   340 {
       
   341 	Q_Q(const HbSelectionDialog);
       
   342     QItemSelectionModel *model = 0;
       
   343     model = selectionModel();
       
   344     if(model){
       
   345         Q_FOREACH(QVariant i,items) {
       
   346                 model->select(model->model()->index(i.toInt(),0),
       
   347                     QItemSelectionModel::Select);
       
   348         }
       
   349     }
       
   350 	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
       
   351 	if(cWidget){
       
   352 		cWidget->updateCounter();
       
   353 	}
       
   354 }
       
   355 
       
   356 QList<QVariant> HbSelectionDialogPrivate::selectedItems() const
       
   357 {
       
   358     QItemSelectionModel *model = 0;
       
   359     QList<QVariant> selIndexes;
       
   360     model = selectionModel();
       
   361     if(model){
       
   362         QModelIndexList indexes = model->selectedIndexes();
       
   363         int count = indexes.count();
       
   364         QModelIndex index;
       
   365         for(int i = 0 ; i < count ; i++){
       
   366             index = indexes[i];
       
   367             selIndexes.append(QVariant(index.row()));
       
   368         }
       
   369     }
       
   370     return selIndexes;
       
   371 
       
   372 }
       
   373 
       
   374 QModelIndexList HbSelectionDialogPrivate::selectedModelIndexes() const
       
   375 {
       
   376     QItemSelectionModel *model = 0;
       
   377     QModelIndexList selIndexes;
       
   378     model = selectionModel();
       
   379     if(model){
       
   380         selIndexes =  model->selectedIndexes();
       
   381     }
       
   382     return selIndexes;
       
   383 }
       
   384 
       
   385 void HbSelectionDialogPrivate::close()
       
   386 {
       
   387 	Q_Q(HbSelectionDialog);
       
   388 	q->close();
       
   389 }
       
   390