phonebookui/pbkcommonui/src/cntdateeditorviewitem.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Thu, 27 May 2010 12:45:19 +0300
changeset 37 fd64c38c277d
parent 31 2a11b5b00470
child 47 7cbcb2896f0e
permissions -rw-r--r--
Revision: 201019 Kit: 2010121

/*
* Copyright (c) 2009 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:
*
*/
#include "cntdateeditorviewitem.h"
#include "cntdetailmodelitem.h"
#include "cntdetailconst.h"

#include <qcontactdetail.h>
#include <qcontactbirthday.h>
#include <qcontactanniversary.h>

#include <hbabstractviewitem.h>
#include <hbabstractitemview.h>
#include <hbdataformmodel.h>
#include <hbwidget.h>
#include <hbdatetimepicker.h>
#include <hbpushbutton.h>
#include <hbdialog.h>
#include <hbdatetimepicker.h>
#include <hblabel.h>
#include <hbaction.h>

CntDateEditorViewItem::CntDateEditorViewItem( QGraphicsItem* aParent ) : 
    CntDetailViewItem( aParent ),
    mButton(NULL)
{
    mLocale = QLocale::system();
}

CntDateEditorViewItem::~CntDateEditorViewItem()
{
}

HbAbstractViewItem* CntDateEditorViewItem::createItem()
{
    return new CntDateEditorViewItem( *this );
}

void CntDateEditorViewItem::clicked()
{
    HbDataFormModel* model = static_cast<HbDataFormModel*>(itemView()->model());
    CntDetailModelItem* item = static_cast<CntDetailModelItem*>( model->itemFromIndex(modelIndex()) );
    QContactDetail detail = item->detail();

    if ( detail.definitionName() == QContactBirthday::DefinitionName )
    {
        QContactBirthday bd = detail;
        editDate( bd.date(), hbTrId("txt_phob_formlabel_birthday") );
    }

    if ( detail.definitionName() == QContactAnniversary::DefinitionName )
    {
        QContactAnniversary anniversary = detail;
        editDate( anniversary.originalDate(), hbTrId("txt_phob_formlabel_anniversary") );
    }
}

HbWidget* CntDateEditorViewItem::createCustomWidget()
{
    mButton = new HbPushButton();
    connect( mButton, SIGNAL(clicked(bool)), this, SLOT(clicked()) );

    HbDataFormModel* model = static_cast<HbDataFormModel*>(itemView()->model());
    CntDetailModelItem* item = static_cast<CntDetailModelItem*>( model->itemFromIndex(modelIndex()) );

    QContactDetail detail = item->detail();
    QString text("No date set"); // loc missing

    if ( detail.definitionName() == QContactBirthday::DefinitionName )
    {
        QContactBirthday birthday = detail;
        if ( !birthday.isEmpty() )
        {
            text = mLocale.toString( birthday.date() );
        }
    }

    if ( detail.definitionName() == QContactAnniversary::DefinitionName )
    {
        QContactAnniversary anniversary = detail;
        if ( !anniversary.isEmpty() )
        {
            text = mLocale.toString( anniversary.originalDate() );
        }
    }
    mButton->setText( text );
    return mButton;
}

void CntDateEditorViewItem::editDate( QDate aCurrent, QString aTitle )
{
    HbDialog *popup = new HbDialog();
    popup->setDismissPolicy(HbDialog::NoDismiss);
    popup->setTimeout(HbPopup::NoTimeout);
    popup->setAttribute(Qt::WA_DeleteOnClose, true);

    HbDateTimePicker *picker = new HbDateTimePicker( popup );
    picker->setDisplayFormat( mLocale.dateFormat() );
    picker->setDateRange(CNT_DATEPICKER_FROM, CNT_DATEPICKER_TO );
    picker->setDate( aCurrent );

    HbLabel *headingText = new HbLabel( popup );
    headingText->setPlainText( aTitle );

    popup->setHeadingWidget(headingText);
    popup->setContentWidget(picker);
    popup->addAction(new HbAction(hbTrId("txt_common_button_ok"), popup));
    popup->addAction(new HbAction(hbTrId("txt_common_button_cancel"), popup));
    
    popup->open(this, SLOT(handleEditDate(HbAction*)));
}

void CntDateEditorViewItem::changeDate( QDate aNewDate )
{
    HbDataFormModel* model = static_cast<HbDataFormModel*>(itemView()->model());
    CntDetailModelItem* item = static_cast<CntDetailModelItem*>( model->itemFromIndex(modelIndex()) );
    QContactDetail detail = item->detail();

    QString buttonText("No date set"); // loc missing
    if ( detail.definitionName() == QContactBirthday::DefinitionName )
    {
        QContactBirthday bd = detail;
        if ( aNewDate != bd.date() )
        {
            bd.setDate( aNewDate );
            item->setDetail( bd );
        }
        buttonText = mLocale.toString( aNewDate );
    }

    if ( detail.definitionName() == QContactAnniversary::DefinitionName )
    {
        QContactAnniversary anniversary = detail;
        if ( aNewDate != anniversary.originalDate() )
        {
            anniversary.setOriginalDate( aNewDate );
            item->setDetail( anniversary );
        }
        buttonText = mLocale.toString( aNewDate );
    }

    mButton->setText( buttonText );
}

void CntDateEditorViewItem::handleEditDate( HbAction *aAction )
{
    HbDialog *popup = static_cast<HbDialog*>(sender());
    
    if (popup && aAction == popup->actions().first())
    {
        QDate date = static_cast<HbDateTimePicker*>(popup->contentWidget())->date();
        changeDate(date);
    }
            
}

// End of File