locationpickerservice/src/locationpickersearchview.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Mon, 03 May 2010 12:27:22 +0300
changeset 17 0f22fb80ebba
parent 15 13ae750350c9
child 20 cd10d5b85554
permissions -rw-r--r--
Revision: 201015 Kit: 201018

/*
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: LocationPickerSearchView implementation
*
*/

#include <HbListViewItem>
#include <QStandardItemModel>
#include <HbSearchPanel>
#include <HbListView>
#include <HbTextItem>
#include <HbDocumentLoader>
#include <QGraphicsLinearLayout>

#include "locationpickerproxymodel.h"
#include "locationpickersearchview.h"
#include "locationpickerdatamanager.h"

// ======== MEMBER FUNCTIONS ========

// ----------------------------------------------------
// LocationPickerSearchView::LocationPickerSearchView()
// ----------------------------------------------------
LocationPickerSearchView::LocationPickerSearchView(HbDocumentLoader &aLoader)
    :mProxyModel(NULL),
    mModel(NULL),
    mListView(NULL),
    mSearchPanel(NULL),
    mDataManager(NULL),
    mEmptyLabel(NULL),
    mVerticalLayout(NULL),
    mDocumentLoader(aLoader)
{

}
// ----------------------------------------------------
// LocationPickerSearchView::~LocationPickerSearchView()
// ----------------------------------------------------
LocationPickerSearchView::~LocationPickerSearchView()
{
    if( mDataManager )
        delete mDataManager;
    delete mProxyModel;
    delete mModel;
    delete mEmptyLabel;
}

// ----------------------------------------------------
// LocationPickerSearchView::init()
// ----------------------------------------------------
void LocationPickerSearchView::init()
{   
    //get listview from docml
    mListView = qobject_cast<HbListView*>(
            mDocumentLoader.findObject(QString("SearchListView")));
    if(mListView == NULL)
    {
        qFatal("Error Reading Docml");   
    }
    //get search panel from docml
    mSearchPanel = qobject_cast<HbSearchPanel*>(
            mDocumentLoader.findObject(QString("searchPanel")));
    if(mListView == NULL)
    {
        qFatal("Error Reading Docml");
    }
    //conect to respective slots
    connect(mListView, SIGNAL(activated(const QModelIndex &)), this, SLOT(handleActivated
    (const QModelIndex &)));
    connect(mSearchPanel, SIGNAL(exitClicked()),this, SLOT(handleExit()));
    connect(mSearchPanel,SIGNAL(criteriaChanged(QString)),this,SLOT(doSearch(QString)));
    
    //Set graphics size for the list items.
    HbListViewItem *hbListItem = new HbListViewItem();
    hbListItem->setGraphicsSize(HbListViewItem::Thumbnail);
    mListView->setItemPrototype( hbListItem );

    // Create a standard model for the view list
    mModel = new QStandardItemModel(this);
    // create a data manager to populate the model
    mDataManager = new LocationPickerDataManager( *mModel, ELocationPickerSearchView );

    // Create the proxy model.
    mProxyModel = new LocationPickerProxyModel( Qt ::Vertical );
    mProxyModel->setSourceModel(mModel);
    mListView->setModel(mProxyModel);

    // set filter properties
    mProxyModel->setDynamicSortFilter(TRUE);
    mProxyModel->setSortRole(Qt::DisplayRole);
    mProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);

    // populate data
    bool populated = mDataManager->populateModel(Qt::Vertical);
    if(!populated)
    {
        // no entries to display.
        QStandardItem *modelItem = new QStandardItem();
        modelItem->setData(QVariant(hbTrId("txt_lint_list_no_location_entries_present")), Qt::DisplayRole);
        mModel->appendRow( modelItem );
    }

    // sort 
    mProxyModel->sort(0, Qt::AscendingOrder);
}

// ----------------------------------------------------
// LocationPickerSearchView::handleExit()
// ----------------------------------------------------
void LocationPickerSearchView::handleExit()
{   
    //emit signal to switch the current view
    emit switchView();
}

// ----------------------------------------------------
// LocationPickerSearchView::doSearch()
// ----------------------------------------------------
void LocationPickerSearchView::doSearch(QString aCriteria)
{
    // use the string to search
    mProxyModel->filterParameterChanged(aCriteria);
    mProxyModel->setFilterFixedString(aCriteria);
    //if no entries presentdisplay empty text item
    if (!mProxyModel->rowCount() )
    {
        if(!mEmptyLabel)
        {    
            QGraphicsWidget *widget = NULL;
            widget = mDocumentLoader.findWidget(QString("container"));
            if(widget == NULL)
            {
                qFatal("Error Reading Docml"); 
            }
            mVerticalLayout = static_cast<QGraphicsLinearLayout*>(widget->layout());
            if(mVerticalLayout == NULL)
            {
                qFatal("Error Reading Docml"); 
            }
            mVerticalLayout->removeItem(mListView);
            mListView->setVisible(false);
            mEmptyLabel = new HbTextItem(hbTrId("txt_lint_list_no_results"));
            mEmptyLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
            mEmptyLabel->setFontSpec(HbFontSpec(HbFontSpec::Primary));
            mEmptyLabel->setAlignment(Qt::AlignCenter);
            mVerticalLayout->insertItem(0, mEmptyLabel);
            mEmptyLabel->setVisible(true);
        }
    }
    //else display the result
    else if (mEmptyLabel)
    {   
        mVerticalLayout->removeItem(mEmptyLabel);
        mEmptyLabel->setVisible(false);
        delete mEmptyLabel;
        mEmptyLabel=NULL;
        mVerticalLayout->insertItem(0, mListView);
        mListView->setVisible(true);
    }
}

// ----------------------------------------------------
// LocationPickerSearchView::handleActivated()
// ----------------------------------------------------
void LocationPickerSearchView::handleActivated(const QModelIndex &aIndex)
{
    QModelIndex index = mProxyModel->mapToSource(aIndex);
    quint32 lm = 0;
    mDataManager->getData( index.row(), lm );
    //emit item is selectedsignal
    emit selectItem( lm );
}