src/hbwidgets/popups/hbselectiondialog_p.cpp
changeset 28 b7da29130b0e
parent 23 e6ad4ef83b23
child 30 80e4d18b72f5
--- a/src/hbwidgets/popups/hbselectiondialog_p.cpp	Thu Sep 02 20:44:51 2010 +0300
+++ b/src/hbwidgets/popups/hbselectiondialog_p.cpp	Fri Sep 17 08:32:10 2010 +0300
@@ -34,32 +34,69 @@
 #include <hbcheckbox.h>
 #include <hbaction.h>
 #include <hbstyleoption_p.h>
+#include <hbstyleprimitivedata.h>
+#include <hbstyleframeprimitivedata.h>
+#include <hbstyletextprimitivedata.h>
+#include <hbstringutil.h>
 
-HbSelectionDialogMarkWidget::HbSelectionDialogMarkWidget(QGraphicsItem *parent):HbWidget(parent),mBackgroundItem(0){
+HbSelectionDialogMarkWidget::HbSelectionDialogMarkWidget(QGraphicsItem *parent):HbWidget(parent),mBackgroundItem(0),mLbCounter(0){
     chkMark = new HbCheckBox(this);
-    chkMark->setText(hbTrId("txt_common_list_mark_all_items"));
-    lbCounter = new HbTextItem(this);
     HbStyle::setItemName(chkMark,"checkbox");
-    HbStyle::setItemName(lbCounter,"counter");
     createPrimitives();
 }
 
 void HbSelectionDialogMarkWidget::createPrimitives()
 {
-    if ( !mBackgroundItem ) {
-        mBackgroundItem = HbStylePrivate::createPrimitive( HbStylePrivate::P_TumbleView_background , this );
-        HbStyle::setItemName( mBackgroundItem , "background" );
+    if (!mBackgroundItem ) {
+        mBackgroundItem = style()->createPrimitive( HbStyle::PT_FrameItem ,"background",this );
+        mBackgroundItem->setZValue(-5);
+    }
+    if(!mLbCounter){
+        mLbCounter = style()->createPrimitive( HbStyle::PT_TextItem ,"counter",this );
     }
 }
 
+void HbSelectionDialogMarkWidget::recreatePrimitives()
+{
+    delete mBackgroundItem;
+    mBackgroundItem = style()->createPrimitive( HbStyle::PT_FrameItem ,"background",this );
+    mBackgroundItem->setZValue(-5);
+
+    delete mLbCounter;
+    mLbCounter = style()->createPrimitive( HbStyle::PT_TextItem ,"counter",this );
+
+}
+void HbSelectionDialogMarkWidget::initPrimitiveData(HbStylePrimitiveData *primitiveData, const QGraphicsObject *primitive)
+{
+      HbWidgetBase::initPrimitiveData(primitiveData, primitive); 
+      QString itemName = HbStyle::itemName(primitive); 
+      if (itemName == QLatin1String("background")) { 
+          HbStyleFramePrimitiveData* data = hbstyleprimitivedata_cast<HbStyleFramePrimitiveData*>(primitiveData); 
+          data->frameGraphicsName = QLatin1String("qtg_fr_groupbox");
+          data->frameType = HbFrameDrawer::NinePieces;
+      } 
+      else if (itemName == QLatin1String("counter")) { 
+            HbStyleTextPrimitiveData *data = hbstyleprimitivedata_cast<HbStyleTextPrimitiveData*>(primitiveData);
+            data->text = mCounterText;
+      } 
+
+}
+
+void HbSelectionDialogMarkWidget::updateCounter(const QString& text)
+{
+    mCounterText = text;
+    HbStyleTextPrimitiveData data;
+    initPrimitiveData(&data, mLbCounter);
+    style()->updatePrimitive(mLbCounter, &data, this);
+}
+
 void HbSelectionDialogMarkWidget::updatePrimitives()
 {
-    HbStyleOption option;
-    initStyleOption( &option );
-   
-    if ( mBackgroundItem ) {
-            HbStylePrivate::updatePrimitive( mBackgroundItem , HbStylePrivate::P_TumbleView_background , &option );
-    }       
+      if (mBackgroundItem) { 
+          HbStyleFramePrimitiveData frameData; 
+          initPrimitiveData(&frameData, mBackgroundItem); 
+          style()->updatePrimitive(mBackgroundItem, &frameData, this); 
+      }     
 }
 
 /*!
@@ -103,7 +140,7 @@
 HbSelectionDialogContentWidget::HbSelectionDialogContentWidget(HbSelectionDialogPrivate *priv):HbWidget(),
                         mListView(0),d(priv),markWidget(0)
 {
-    
+    mSelectionTitle = hbTrId("txt_common_list_mark_all_items");
 }
 
 void HbSelectionDialogContentWidget::_q_listWidgetItemSelected(HbListWidgetItem *item)
@@ -111,14 +148,6 @@
     Q_UNUSED(item)
 }
 
-void HbSelectionDialogContentWidget::_q_listItemSelected(QModelIndex index)
-{
-    Q_UNUSED(index)
-    if(mListView->selectionMode()== HbAbstractItemView::SingleSelection){
-       d->close();
-    }
-    updateCounter();
-}
 
 int HbSelectionDialogContentWidget::selectedItemCount() const
 {
@@ -153,6 +182,21 @@
         }
     }
 }
+
+QString HbSelectionDialogContentWidget::selectionTitle() const
+{
+    return mSelectionTitle;
+}
+
+void HbSelectionDialogContentWidget::setSelectionTitle(const QString& title)
+{
+    if(mSelectionTitle != title){
+        mSelectionTitle = title;
+        if(markWidget)
+            markWidget->chkMark->setText(mSelectionTitle);
+    }
+}
+
 void HbSelectionDialogContentWidget::updateCounter()
 {
     if(!mListView) return;
@@ -161,7 +205,7 @@
         int totalItems = totalItemCount();
         int selectedItems = selectedItemCount();
         markWidget->updatePrimitives();
-        markWidget->lbCounter->setText(QString(QString::number(selectedItems) + "/" + QString::number(totalItems)));
+        markWidget->updateCounter(QString(HbStringUtil::convertDigits(QString::number(selectedItems)) + "/" + HbStringUtil::convertDigits(QString::number(totalItems))));
         //update checked state of "MarkAll" checkbox 
         if (totalItems > 0 && (selectedItems == totalItems)){
             markWidget->chkMark->blockSignals(true); //should not call _q_checkboxclicked()
@@ -206,6 +250,7 @@
     if(bShow){
         if(!markWidget){
             markWidget = new HbSelectionDialogMarkWidget(this);
+            markWidget->chkMark->setText(mSelectionTitle);
         }
         else{
             markWidget->show();
@@ -236,8 +281,21 @@
     Q_UNUSED(selected);
     Q_UNUSED(deselected);
     updateCounter();
+    if(mListView->selectionMode()== HbAbstractItemView::MultiSelection)
+        d->_p_SelectionChanged();
 }
 
+void HbSelectionDialogContentWidget::_q_listItemSelected(QModelIndex index)
+{
+    Q_UNUSED(index)
+    if(mListView->selectionMode()== HbAbstractItemView::SingleSelection || mListView->selectionMode()== HbAbstractItemView::NoSelection){
+       d->close();
+    }
+    updateCounter();
+    //d->_p_SelectionChanged();
+}
+
+
 void HbSelectionDialogContentWidget::modelChanged(const QModelIndex &parent, int start,int end)
 {
     Q_UNUSED(parent);
@@ -271,6 +329,7 @@
 void HbSelectionDialogContentWidget::createListView()
 {
     if(mListView){
+        QObject::disconnect(mListView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this,SLOT(_q_selectionChanged(const QItemSelection&, const QItemSelection&)));
         HbListWidget* mView = qobject_cast<HbListWidget*>(mListView);
         if(mView){
             delete mListView;
@@ -286,6 +345,7 @@
             connectSlots();
             repolish();
     }
+    QObject::connect(mListView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this,SLOT(_q_selectionChanged(const QItemSelection&, const QItemSelection&)));
 }
 
 HbSelectionDialogPrivate::HbSelectionDialogPrivate()
@@ -316,20 +376,30 @@
     showActions(mSelectionMode);
 }
 
+void HbSelectionDialogPrivate::_p_SelectionChanged()
+{
+    Q_Q(HbSelectionDialog);
+    if(mSelectionMode == HbAbstractItemView::MultiSelection)
+    {
+        emit q->selectionChanged();
+    }
+}
+
 void HbSelectionDialogPrivate::showActions(HbAbstractItemView::SelectionMode selectionMode)
 {
     Q_Q(HbSelectionDialog);
-    if(selectionMode == HbAbstractItemView::SingleSelection){
+    if(selectionMode == HbAbstractItemView::SingleSelection || selectionMode == HbAbstractItemView::NoSelection){
 
         if(action1) {
             q->disconnect(action1,SIGNAL(triggered()),q,SLOT(accept()));
             q->removeAction(action1);
-            action1 = 0;
         }
         if(action2 == NULL){
         action2 =new HbAction(hbTrId("txt_common_button_cancel"),q);
         q->connect(action2,SIGNAL(triggered()),q,SLOT(reject()));
-        q->insertAction(0,action2);
+        }
+        if(q->actions().contains(action2) == false){
+            q->insertAction(0,action2);
         }
     }
     else{
@@ -339,12 +409,19 @@
             q->connect(action2,SIGNAL(triggered()),q,SLOT(reject()));
             q->insertAction(0,action2);
         }    
+        if(q->actions().contains(action2) == false)
+            q->insertAction(0,action2);
+
         if(action1 == NULL){
             action1 =new HbAction(hbTrId("txt_common_button_ok"),q);
             q->connect(action1,SIGNAL(triggered()),q,SLOT(accept()));
-            q->insertAction(action2,action1);
         }    
+       if(q->actions().contains(action1) == false)
+        {
+            q->insertAction(q->actions().first(),action1);
+        }
     }
+    
 }
 
 void HbSelectionDialogPrivate::setSelectionMode(HbAbstractItemView::SelectionMode mode)
@@ -354,6 +431,7 @@
     mSelectionMode = mode;
     switch(mSelectionMode)
     {
+    case HbAbstractItemView::NoSelection:
     case HbAbstractItemView::SingleSelection:
     case HbAbstractItemView::MultiSelection:
     {
@@ -368,7 +446,7 @@
         showActions(mSelectionMode);
     }
     break;
-    case HbAbstractItemView::NoSelection:
+    default:
     break;
     }
 }
@@ -555,16 +633,21 @@
     QList<QVariant> selIndexes;
     model = selectionModel();
     if(model){
-        QModelIndexList indexes = model->selectedIndexes();
-        int count = indexes.count();
-        QModelIndex index;
-        for(int i = 0 ; i < count ; i++){
-            index = indexes[i];
+        if(mSelectionMode == HbAbstractItemView::NoSelection){
+            QModelIndex index = model->currentIndex();
             selIndexes.append(QVariant(index.row()));
         }
+        else{
+            QModelIndexList indexes = model->selectedIndexes();
+            int count = indexes.count();
+            QModelIndex index;
+            for(int i = 0 ; i < count ; i++){
+                index = indexes[i];
+                selIndexes.append(QVariant(index.row()));
+            }
+        }
     }
     return selIndexes;
-
 }
 
 QModelIndexList HbSelectionDialogPrivate::selectedModelIndexes() const
@@ -573,7 +656,13 @@
     QModelIndexList selIndexes;
     model = selectionModel();
     if(model){
-        selIndexes =  model->selectedIndexes();
+        if(mSelectionMode == HbAbstractItemView::NoSelection){
+            QModelIndex index = model->currentIndex();
+            selIndexes.append(index);
+        }
+        else{
+            selIndexes =  model->selectedIndexes();
+        }
     }
     return selIndexes;
 }
@@ -584,3 +673,22 @@
     q->accept(); //emit the signal
 }
 
+QString HbSelectionDialogPrivate::selectionTitle() const
+{
+    Q_Q(const HbSelectionDialog);
+    QString str;
+    HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
+    if(cWidget){
+        str = cWidget->selectionTitle();
+    }
+    return str;
+}
+
+void HbSelectionDialogPrivate::setSelectionTitle(const QString& title)
+{
+    Q_Q(HbSelectionDialog);
+    HbSelectionDialogContentWidget* cWidget = qobject_cast<HbSelectionDialogContentWidget*>(q->contentWidget());
+    if(cWidget){
+        cWidget->setSelectionTitle(title);
+    }
+}