src/hbwidgets/popups/hbselectiondialog_p.cpp
changeset 6 c3690ec91ef8
parent 5 627c4a0fd0e7
child 7 923ff622b8b9
--- a/src/hbwidgets/popups/hbselectiondialog_p.cpp	Fri Jun 11 13:58:22 2010 +0300
+++ b/src/hbwidgets/popups/hbselectiondialog_p.cpp	Wed Jun 23 18:33:25 2010 +0300
@@ -35,27 +35,20 @@
 #include <hbaction.h>
 
 HbSelectionDialogContentWidget::HbSelectionDialogContentWidget(HbSelectionDialogPrivate *priv):HbWidget(),
-                        mListWidget(0),d(priv),chkMark(0),lbCounter(0)
+                        mListView(0),d(priv),chkMark(0),lbCounter(0),markWidgetShown(false)
 {
-    mListWidget = new HbListWidget(this);
-    HbStyle::setItemName(mListWidget, "list");
-    QObject::connect(mListWidget,SIGNAL(activated(const QModelIndex&)),this,SLOT(_q_listItemSelected(QModelIndex)));
-    QObject::connect(mListWidget,SIGNAL(activated(HbListWidgetItem *)),this,SLOT(_q_listWidgetItemSelected(HbListWidgetItem *)));
+    
 }
-
 void HbSelectionDialogContentWidget::_q_listWidgetItemSelected(HbListWidgetItem *item)
 {
-    if(item){
-        updateCounter();
-    }
+	Q_UNUSED(item)
 }
 
 void HbSelectionDialogContentWidget::_q_listItemSelected(QModelIndex index)
 {
     Q_UNUSED(index)
-    if(mListWidget->selectionMode()== HbAbstractItemView::SingleSelection ||
-       mListWidget->selectionMode()== HbAbstractItemView::NoSelection){
-       d->close();   
+	if(mListView->selectionMode()== HbAbstractItemView::SingleSelection){
+       d->close();
     }
     updateCounter();
 }
@@ -63,21 +56,28 @@
 int HbSelectionDialogContentWidget::selectedItemCount() const
 {
     int selectedItems = 0;
-    QItemSelectionModel* selectionModel = mListWidget->selectionModel();
-    if(selectionModel){
-        selectedItems = selectionModel->selectedRows().count();
-    }
+	if(mListView){
+		QItemSelectionModel* selectionModel = mListView->selectionModel();
+		if(selectionModel){
+			selectedItems = selectionModel->selectedRows().count();
+		}
+	}
     return selectedItems;
 }
 
 int HbSelectionDialogContentWidget::totalItemCount() const
 {
-    return mListWidget->count();
+	int nCount = 0;
+	if(mListView && mListView->model()){
+		nCount = mListView->model()->rowCount();
+	}
+	return nCount;
 }
 
 void HbSelectionDialogContentWidget::updateCounter()
 {
-    if(mListWidget->selectionMode()!= HbAbstractItemView::MultiSelection) return;
+	if(!mListView) return;
+    if(mListView->selectionMode()!= HbAbstractItemView::MultiSelection) return;
     if(chkMark && lbCounter){
         int totalItems = totalItemCount();
         int selectedItems = selectedItemCount();
@@ -101,19 +101,17 @@
 {
     int totalItems = 0;
     int selectedItems = 0;
-    QAbstractItemModel* itemModel = mListWidget->model();
+    QAbstractItemModel* itemModel = mListView->model();
     QModelIndex indexStart,indexEnd;
     if(itemModel){
         indexStart = itemModel->index(0,0);
         indexEnd = itemModel->index(itemModel->rowCount()-1,0);
         totalItems = itemModel->rowCount();
-        Q_UNUSED( totalItems ); // todo: remove totalItems if not needed
     }
 
-    QItemSelectionModel* selectionModel = mListWidget->selectionModel();
+    QItemSelectionModel* selectionModel = mListView->selectionModel();
     if(selectionModel){
         selectedItems = selectionModel->selectedRows().count();
-        Q_UNUSED( selectedItems ); // todo: remove selectedItems if not needed
         if(value){ //Select All
             selectionModel->select(QItemSelection(indexStart,indexEnd),QItemSelectionModel::Select);
         }
@@ -126,7 +124,8 @@
 
 void HbSelectionDialogContentWidget::showMarkWidget(bool bShow)
 {
-    if(bShow){
+	if(bShow){
+		if(!markWidgetShown){
             chkMark = new HbCheckBox(this);
             chkMark->setText("Mark All");
             lbCounter = new HbTextItem(this);
@@ -135,6 +134,8 @@
             setProperty("multiSelection",true);
             connect(chkMark,SIGNAL(stateChanged ( int )),this,SLOT(_q_checkboxclicked(int)));
             updateCounter();
+			markWidgetShown = true;
+		}
     }
     else{
         delete chkMark;chkMark=0;
@@ -142,9 +143,51 @@
         HbStyle::setItemName(chkMark,"");
         HbStyle::setItemName(lbCounter,"");
         setProperty("multiSelection",false);
+		markWidgetShown = false;
     }
 }
 
+void HbSelectionDialogContentWidget::connectSlots()
+{
+    QObject::connect(mListView,SIGNAL(activated(const QModelIndex&)),this,SLOT(_q_listItemSelected(QModelIndex)));
+}
+
+void HbSelectionDialogContentWidget::createListWidget()
+{
+	if(mListView){
+		HbListWidget* mView = qobject_cast<HbListWidget*>(mListView);
+		if(!mView){
+			delete mListView;
+			mListView = new HbListWidget(this);
+			HbStyle::setItemName(mListView, "list");
+			connectSlots();
+		}
+	}
+	else{
+			mListView = new HbListWidget(this);
+			HbStyle::setItemName(mListView, "list");
+			connectSlots();
+	}
+}
+
+void HbSelectionDialogContentWidget::createListView()
+{
+	if(mListView){
+		HbListView* mView = qobject_cast<HbListView*>(mListView);
+		if(!mView){
+			delete mListView;
+			mListView = new HbListView(this);
+			HbStyle::setItemName(mListView, "list");
+			connectSlots();
+		}
+	}
+	else{
+			mListView = new HbListView(this);
+			HbStyle::setItemName(mListView, "list");
+			connectSlots();
+	}
+}
+
 HbSelectionDialogPrivate::HbSelectionDialogPrivate()
     :HbDialogPrivate()
 {
@@ -153,23 +196,7 @@
 
 HbSelectionDialogPrivate::~HbSelectionDialogPrivate()
 {
-    if(!bOwnItems){
-            Q_Q(HbSelectionDialog);
-            HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
-            if(cWidget){
-                if(cWidget->mListWidget){
-                    int nRows = 0;
-                    QAbstractItemModel* itemModel = cWidget->mListWidget->model();
-                    if(itemModel){
-                        nRows = itemModel->rowCount();
-                        while(nRows){
-                            cWidget->mListWidget->takeItem(0);
-                            nRows = itemModel->rowCount();
-                        }
-                    }
-                }
-            }
-    }
+	clearItems(bOwnItems);	
 }
 
 void HbSelectionDialogPrivate::init()
@@ -178,14 +205,21 @@
     Q_Q(HbSelectionDialog);
 
     bOwnItems = false;
-
+	mSelectionMode = HbAbstractItemView::SingleSelection;
     HbSelectionDialogContentWidget* contentWidget = new HbSelectionDialogContentWidget(this);
     q->setContentWidget(contentWidget);
 
-    q->addAction(new HbAction(q->tr("Ok"), q));
+    HbAction *action1=new HbAction(hbTrId("txt_common_button_ok"),q);
+    q->addAction(action1);
+    q->connect(action1,SIGNAL(triggered()),q,SLOT(accept()));
 
-    q->addAction(new HbAction(q->tr("Cancel"), q));
-    q->setTimeout(0);
+    HbAction *action2=new HbAction(hbTrId("txt_common_button_cancel"),q);
+    q->addAction(action2);
+    q->connect(action2,SIGNAL(triggered()),q,SLOT(reject()));
+
+
+    q->setDismissPolicy(HbPopup::NoDismiss);
+    q->setTimeout(HbPopup::NoTimeout);      
     q->setModal(true);
 }
 
@@ -194,23 +228,56 @@
     Q_Q(HbSelectionDialog);
 
     mSelectionMode = mode;
-    switch(mode)
+    switch(mSelectionMode)
     {
     case HbAbstractItemView::SingleSelection:
     case HbAbstractItemView::MultiSelection:
-    case HbAbstractItemView::NoSelection:
     {
         HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
-        if(cWidget){
-            cWidget->mListWidget->setSelectionMode(mode);
-        }
-        if(mode == HbAbstractItemView::MultiSelection)
-            cWidget->showMarkWidget(true);    
-        else
-            cWidget->showMarkWidget(false);    
+        if(cWidget && cWidget->mListView){
+            cWidget->mListView->setSelectionMode(mSelectionMode);
+			if(mode == HbAbstractItemView::MultiSelection)
+				cWidget->showMarkWidget(true);    
+			else
+				cWidget->showMarkWidget(false);    
+		}
     }
     break;
-    }
+    case HbAbstractItemView::NoSelection:
+	break;
+	}
+}
+
+
+void HbSelectionDialogPrivate::clearItems(bool keepItems)
+{
+	Q_Q(HbSelectionDialog);
+	HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
+	if(cWidget){
+		HbListWidget* mWidget = qobject_cast<HbListWidget*>(cWidget->mListView);
+		if(mWidget){
+				if(keepItems){
+				int nRows = 0;
+				QAbstractItemModel* itemModel = mWidget->model();
+				if(itemModel){
+					nRows = itemModel->rowCount();
+					while(nRows){
+						mWidget->takeItem(0);
+						nRows = itemModel->rowCount();
+					}
+				}
+			}
+			else{
+				mWidget->clear();
+			}
+			bOwnItems = false;
+			return;
+		}
+		HbListView* mView = qobject_cast<HbListView*>(cWidget->mListView);
+		if(mView){
+			cWidget->mListView->setModel(0);
+		}
+	}
 }
 
 QList<HbListWidgetItem*> HbSelectionDialogPrivate::widgetItems() const
@@ -220,7 +287,7 @@
     QList<HbListWidgetItem*> rows;
     HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
     if(cWidget){
-        HbListWidget* widget = qobject_cast<HbListWidget*>(cWidget->mListWidget);
+        HbListWidget* widget = qobject_cast<HbListWidget*>(cWidget->mListView);
         if(widget){
             int count = 0;
             QAbstractItemModel* itemModel = widget->model();
@@ -242,16 +309,19 @@
     if(!cWidget) return;
     
     int nRows = 0;
-
-    if(cWidget->mListWidget){
+	cWidget->createListWidget();
+	setSelectionMode(mSelectionMode);
+    if(cWidget->mListView){
         int count = items.size();
+		if(count > 0) clearItems(bOwnItems); //Clear the existing items first
         for (int i = 0; i < count; ++i) {
             HbListWidgetItem* modelItem = new HbListWidgetItem();
             QString str = items.at(i);
             modelItem->setText(str);
-            cWidget->mListWidget->addItem(modelItem);
+			HbListWidget* widget = (HbListWidget*)cWidget->mListView;
+            widget->addItem(modelItem);
             
-            QAbstractItemModel* itemModel = cWidget->mListWidget->model();
+            QAbstractItemModel* itemModel = cWidget->mListView->model();
             if(itemModel)
                 nRows = itemModel->rowCount();
         }
@@ -284,7 +354,9 @@
 
     HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
     if(cWidget){
-        cWidget->mListWidget->HbListView::setModel(model); //HbListView's implementation of setModel()
+		cWidget->createListView();
+		setSelectionMode(mSelectionMode);
+        cWidget->mListView->setModel(model); 
     }
 }
 
@@ -294,12 +366,16 @@
 
     HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
     if(cWidget){
-        if(cWidget->mListWidget){
+		cWidget->createListWidget();
+		setSelectionMode(mSelectionMode);
+        if(cWidget->mListView){
+			HbListWidget* widget = (HbListWidget*)cWidget->mListView;
             int count = items.count();
+			if(count > 0) clearItems(bOwnItems); //Clear the existing items first
             for(int i = 0; i < count; i++){
-                cWidget->mListWidget->addItem(items[i]);
+                widget->addItem(items[i]);
             }
-            cWidget->mListWidget->setCurrentRow(currentIndex);
+            widget->setCurrentRow(currentIndex);
             
         }
         bOwnItems = transferOwnership;
@@ -311,8 +387,8 @@
     Q_Q(const HbSelectionDialog);
     
     HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
-    if(cWidget){
-        return cWidget->mListWidget->HbListView::model(); //HbListView's implementation of model()
+    if(cWidget && cWidget->mListView){
+		return cWidget->mListView->model();
     }
     return 0;
 }
@@ -322,8 +398,8 @@
     Q_Q(const HbSelectionDialog);
     
     HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
-    if(cWidget){
-        return cWidget->mListWidget->selectionModel();
+    if(cWidget && cWidget->mListView){
+        return cWidget->mListView->selectionModel();
     }
     return 0;
 }
@@ -334,15 +410,16 @@
     QItemSelectionModel *model = 0;
     model = selectionModel();
     if(model){
+		model->clearSelection();
         Q_FOREACH(QVariant i,items) {
                 model->select(model->model()->index(i.toInt(),0),
                     QItemSelectionModel::Select);
         }
-    }
-    HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
-    if(cWidget){
-        cWidget->updateCounter();
-    }
+		HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
+		if(cWidget){
+			cWidget->updateCounter();
+		}
+	}
 }
 
 QList<QVariant> HbSelectionDialogPrivate::selectedItems() const
@@ -377,5 +454,6 @@
 void HbSelectionDialogPrivate::close()
 {
     Q_Q(HbSelectionDialog);
-    q->close();
+	q->accept(); //emit the signal
 }
+