calendarui/views/src/calenmonthgrid.cpp
branchRCL_3
changeset 66 bd7edf625bdd
parent 65 12af337248b1
equal deleted inserted replaced
65:12af337248b1 66:bd7edf625bdd
     1 /*
     1 /*
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
     3  * All rights reserved.
     3 * All rights reserved.
     4  * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5  * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6  * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Grid of the MonthView.
     8  *
    15  *
     9  * Initial Contributors:
    16 */
    10  * Nokia Corporation - initial contribution.
    17 
    11  *
    18 
    12  * Contributors: 
    19 
    13  *
    20 //debug
    14  * Description: Definition file for class CalenMonthGrid.
    21 #include "calendarui_debug.h"
    15  *
    22 
    16  */
    23 // INCLUDE FILES
    17 
       
    18 // System includes
       
    19 #include <hbgridview.h>
       
    20 #include <hbabstractviewitem.h>
       
    21 #include <hbstyleloader.h>
       
    22 #include <hbcolorscheme.h>
       
    23 #include <hbpangesture.h>
       
    24 #include <hbswipegesture.h>
       
    25 #include <hbtapgesture.h>
       
    26 #include <hbdeviceprofile.h>
       
    27 #include <hbinstance.h>
       
    28 
       
    29 // User includes
       
    30 #include "calenmonthgrid.h"
    24 #include "calenmonthgrid.h"
    31 #include "calengriditemprototype.h"
    25 #include "calenmonthcontainer.h"
    32 #include "calenmonthdata.h"
    26 #include "calenmonthcelllistboxdata.h"
    33 #include "calenmonthview.h"
    27 #include "calenmonthcelllistboxitemdrawer.h"
    34 #include "calendateutils.h"
    28 
    35 #include "calencommon.h"
    29 #include <aknlayoutscalable_apps.cdl.h>
    36 #include "calenconstants.h"
    30 
    37 #include "OstTraceDefinitions.h"
    31 // New line color groups in enhanced skinning
    38 #ifdef OST_TRACE_COMPILER_IN_USE
    32 static void DrawLAFLine(CWindowGc& aGc, const TAknLayoutRect& aArea,
    39 #include "calenmonthgridTraces.h"
    33                         const TAknsItemID& aSkinComponent, TInt aColorGroup)
    40 #endif
    34     {
    41 
    35     TRACE_ENTRY_POINT;
    42 
    36 
    43 // Constants
    37     TRgb lineColor = aArea.Color();
    44 #define SCROLL_SPEEED 3000 
    38     AknsUtils::GetCachedColor(AknsUtils::SkinInstance(), lineColor,
    45 #define GRIDLINE_WIDTH 0.075 //units
    39                               aSkinComponent, aColorGroup);
    46 #define MAX_PAN_DIRECTION_THRESHOLD 50
    40     aGc.SetBrushColor( lineColor );
    47 #define MIN_PAN_DIRECTION_THRESHOLD 20
    41     aGc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    48 
    42     aGc.Clear( aArea.Rect() );
    49 /*!
    43 
    50  \class CalenMonthGrid
    44     TRACE_EXIT_POINT;
    51 
    45     }
    52  Implements the month grid
    46 
    53  */
    47 static void DrawSecondaryLine(CWindowGc& aGc, const TAknLayoutRect& aArea)
    54 
    48     {
    55 /*!
    49     TRACE_ENTRY_POINT;
    56  Default constructor.
    50 
    57  */
    51     DrawLAFLine(aGc, aArea, KAknsIIDQsnLineColors, EAknsCIQsnLineColorsCG2);
    58 CalenMonthGrid::CalenMonthGrid(QGraphicsItem *parent):
    52 
    59 	HbGridView(parent),
    53     TRACE_EXIT_POINT;
    60 	mModel(0),
    54     }
    61 	mDirection(invalid),
    55 
    62 	mIsPanGesture(false),
    56 // ================= MEMBER FUNCTIONS =======================
    63 	mIsAtomicScroll(true),
    57 
    64 	mView(NULL),
    58 // C++ default constructor can NOT contain any code, that
    65 	mCurrentRow(-100),
    59 // might leave.
    66 	mIsNonActiveDayFocused(false),
    60 //
    67 	mIgnoreItemActivated(false),
    61 CCalenMonthGrid::CCalenMonthGrid
    68 	mGridLineColor(HbColorScheme::color("qtc_cal_grid_line")),
    62 (TTime aFirstDayOfGrid, CCalenMonthContainer* aMonthCont)
    69     mActiveDatesSet(false)
    63     : iFirstDayOfGrid(aFirstDayOfGrid),iMonthContainer(aMonthCont)
    70 {
    64     {
    71     OstTraceFunctionEntry0( CALENMONTHGRID_CALENMONTHGRID_ENTRY );
    65     TRACE_ENTRY_POINT;
    72     
    66     
    73 	setScrollDirections(Qt::Vertical);
    67     SetVerticalMargin(0);
    74 	setRowCount(KNumOfVisibleRows);
    68     SetHorizontalMargin(0);
    75 	setColumnCount(KCalenDaysInWeek);
    69     
    76 	setLongPressEnabled(false);
    70     TRACE_EXIT_POINT;
    77 	setItemRecycling(false);
    71     }
    78 	setSelectionMode(HbGridView::NoSelection);
    72 
    79 	setUniformItemSizes(true);
    73 // Destructor
    80 	setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
    74 CCalenMonthGrid::~CCalenMonthGrid()
    81 	setClampingStyle(HbScrollArea::StrictClamping);
    75     {
    82 	setEnabledAnimations(HbAbstractItemView::None);
    76     TRACE_ENTRY_POINT;
    83 	setFrictionEnabled(false);
    77     TRACE_EXIT_POINT;
    84 	setFlag(QGraphicsItem::ItemHasNoContents, false);
    78     }
    85 	
    79 
    86 	// Disable the rows and columns swapping on orientation change
    80 // ---------------------------------------------------------
    87 	setSwapDimensionsOnOrientationChange(false);
    81 // CCalenMonthGrid::Draw
    88 	
    82 // Drawing month gird
    89 	resetTransform();
    83 // (other items were commented in a header).
    90 	
    84 // ---------------------------------------------------------
    91 	// Get the content widget of the scroll area to draw the grid lines
    85 //
    92 	mContentWidget = contentWidget();
    86 void CCalenMonthGrid::Draw(const TRect& aRect)const
    93 
    87     {
    94 	// Get the localised dates well before
    88     TRACE_ENTRY_POINT;
    95 	// TODO: Need to update the mLocalisedDates when user changes the
    89     //const TBool useWeeks( UseWeeks() );
    96 	// phone language keeping calendar application in background
    90     CAknGrid::Draw( aRect );
    97 	HbExtendedLocale locale = HbExtendedLocale::system();
    91     
    98 	for (int i = 1; i <= 31; i++) {
    92     // For drawing Secondary grid lines
    99 		mLocalisedDates.append(locale.toString(i));
    93     DrawGridLines();
   100 	}
    94 
   101 	
    95     TRACE_EXIT_POINT;
   102 	// Connect to scrolling finished signal
    96     
   103 	connect(this, SIGNAL(scrollingEnded()), this,
    97     }
   104 			SLOT(scrollingFinished()));
    98 
   105 
    99 // ---------------------------------------------------------
   106     connect(
   100 // CCalenMonthGrid::DrawGridLines
   107                 HbTheme::instance(), SIGNAL(changed()),
   101 // Draws secondary lines of the grid
   108                 this, SLOT(handleThemeChange()));
   102 // (other items were commented in a header).
   109 				
   103 // ---------------------------------------------------------
   110 	// Disable the haptic feedback for the month grid during scrolling
   104 //
   111     setAttribute(Hb::InteractionDisabled);
   105 void CCalenMonthGrid::DrawGridLines()const
   112 	
   106     {
   113 	OstTraceFunctionExit0( CALENMONTHGRID_CALENMONTHGRID_EXIT );
   107     TRACE_ENTRY_POINT;
   114 }
   108         
   115 
   109     CWindowGc& gc = SystemGc();
   116 /*!
   110     TRect parentRect = iMonthContainer->Rect();
   117  Destructor
   111     TRect main_pane(iMonthContainer->ReducePreview( parentRect ) );
   118  */
   112         
   119 CalenMonthGrid::~CalenMonthGrid()
   113 
   120 {
   114     TAknLayoutRect main_cale_month_pane;
   121     OstTraceFunctionEntry0( DUP1_CALENMONTHGRID_CALENMONTHGRID_ENTRY );
   115     TInt layoutVariant = iMonthContainer->LayoutVariantIndex(CCalenMonthContainer::EMainCaleMonthPane);
   122     
   116     main_cale_month_pane.LayoutRect( main_pane, AknLayoutScalable_Apps::main_cale_month_pane(layoutVariant).LayoutLine() );
   123 	// Nothing Yet
   117     
   124     
   118     TAknLayoutRect cale_month_pane_g;
   125     OstTraceFunctionExit0( DUP1_CALENMONTHGRID_CALENMONTHGRID_EXIT );
   119 
   126 }
   120     // Get indexes for grid lines (cale_month_pane_g)
   127 
   121     TAknLayoutScalableTableLimits cale_month_pane_g_Limits = AknLayoutScalable_Apps::cale_month_pane_g_Limits();
   128 /*!
   122     TInt index( cale_month_pane_g_Limits.FirstIndex() );
   129  Stores the view pointer
   123     TInt end(   cale_month_pane_g_Limits.LastIndex() );
   130  */
   124     // First two are primary lines to separate heading and week number panes from grid
   131 void CalenMonthGrid::setView(CalenMonthView *view)
   125     // We draw them elsewhere
   132 {
   126     const TInt firstGridLineIndex = 2;
   133     OstTraceFunctionEntry0( CALENMONTHGRID_SETVIEW_ENTRY );
   127     layoutVariant = iMonthContainer->LayoutVariantIndex(CCalenMonthContainer::ECaleMonthPaneG );
   134     
   128     for ( index+=firstGridLineIndex; index<=end; ++index )
   135 	mView = view;
   129         {
   136 	
   130         cale_month_pane_g.LayoutRect( main_cale_month_pane.Rect(),
   137 	OstTraceFunctionExit0( CALENMONTHGRID_SETVIEW_EXIT );
   131                 AknLayoutScalable_Apps::cale_month_pane_g( index, layoutVariant ).LayoutLine() );
   138 }
   132         DrawSecondaryLine( gc, cale_month_pane_g );
   139 
       
   140 /*!
       
   141  Updates the model with the proper dates and sets the required user roles
       
   142  */
       
   143 void CalenMonthGrid::updateMonthGridModel(QList<CalenMonthData> &monthDataArray,
       
   144                         int indexToBeScrolled, bool isFirstTime)
       
   145 {
       
   146     OstTraceFunctionEntry0( CALENMONTHGRID_UPDATEMONTHGRIDMODEL_ENTRY );
       
   147     
       
   148 	int loopStart = 0;
       
   149 	int loopEnd = monthDataArray.count();
       
   150 	if (isFirstTime) {
       
   151 		// Create the model with only 42 items as visible to the user
       
   152 		mModel = new QStandardItemModel(KCalenDaysInWeek * KNumOfVisibleRows, 
       
   153 		                                1, this);
       
   154 		loopStart = (mView->rowsInPrevMonth()) * KCalenDaysInWeek;
       
   155 		loopEnd = loopStart + (KCalenDaysInWeek * KNumOfVisibleRows);
       
   156 	} else {
       
   157 		// Block the signals generated by model, this is being done as
       
   158 		// we want to avoid the overload of view listening to signals
       
   159 		mModel->blockSignals(true);
       
   160 		
       
   161 		// Check the counts
       
   162 		int dataCount = monthDataArray.count();
       
   163 		int rowCount = mModel->rowCount();
       
   164 		int countDiff = dataCount - rowCount;
       
   165 		if (countDiff < 0) {
       
   166 			// Delete extra rows in the model
       
   167 			mModel->removeRows(dataCount,abs(countDiff));
       
   168 		} else if (countDiff > 0) {
       
   169 			// Add the necessary number of rows
       
   170 			mModel->insertRows(rowCount,countDiff);
       
   171 		}
       
   172 		loopEnd = dataCount;
       
   173 	}
       
   174 	
       
   175 	QDateTime currDate = mView->getCurrentDay();
       
   176 	QDateTime currDateTime = CalenDateUtils::beginningOfDay(currDate);
       
   177 	QDateTime activeDay = mView->getActiveDay();
       
   178 	QDateTime activeDateTime = CalenDateUtils::beginningOfDay(activeDay);
       
   179 	
       
   180 	QModelIndex currentIndex;
       
   181 	int modelIndex = 0;
       
   182 	for (int i = loopStart; i < loopEnd; i++) {
       
   183 		QDateTime dateTime = monthDataArray[i].Day();
       
   184 		currentIndex = mModel->index(modelIndex++, 0);
       
   185 		
       
   186 		// Create the variant list to contain the date to depict a grid item
       
   187 		QVariantList itemData;
       
   188 		
       
   189 		// !!!NOTE!!!: Add the data in the order mentioned in the 
       
   190 		// CalendarNamespace::DataRole enum. Dont change the order.
       
   191 		itemData << mLocalisedDates.at(dateTime.date().day()-1); 
       
   192 		
       
   193 		// Check for active day
       
   194 		if (activeDateTime == CalenDateUtils::beginningOfDay(dateTime)) {
       
   195 			mCurrentRow = i;
       
   196 			// Set the focus attribute to true
       
   197 			itemData << true;
       
   198 		} else {
       
   199 			// reset the highlight
       
   200 			itemData << false;
       
   201 		}
       
   202 
       
   203 		// Check for current day
       
   204 		if (currDateTime == CalenDateUtils::beginningOfDay(dateTime)) {
       
   205 			// Set the underline attribute to true
       
   206 			itemData << true;
       
   207 		} else {			
       
   208 			itemData << false;
       
   209 		}
       
   210 
       
   211 		// Check for events
       
   212 		if (monthDataArray[i].HasEvents()) {
       
   213 			// Set the event indicator attribute
       
   214 			itemData << true;
       
   215 		} else {
       
   216 			itemData << false;
       
   217 		}
       
   218 		
       
   219 		// Add default text color
       
   220 		if (monthDataArray[i].isActive()) {
       
   221 			itemData << true;
       
   222 		} else {
       
   223 			itemData << false;
       
   224 		}
       
   225 		mModel->itemFromIndex(currentIndex)->setData(itemData);
       
   226 	}
       
   227 	
       
   228 	if (isFirstTime) {
       
   229 		
       
   230 		// Create the prototype
       
   231 		CalenGridItemPrototype* gridItemPrototype = new CalenGridItemPrototype(this);
       
   232 		
       
   233 	    connect(
       
   234 	            HbTheme::instance(), SIGNAL(changed()),
       
   235 	            gridItemPrototype, SLOT(handleThemeChange()));
       
   236 	    
       
   237 		// Set the mode and the prototype
       
   238 		setModel(mModel,gridItemPrototype);
       
   239 		
       
   240 		// Register the widgetml and css files
       
   241 		HbStyleLoader::registerFilePath(":/");
       
   242 		
       
   243 		// Set the layout name
       
   244 		setLayoutName("calendarCustomGridItem");
       
   245 	} else {
       
   246 		// Since, we have finished setData, Now unblock the signals
       
   247 		mModel->blockSignals(false);
       
   248 		
       
   249 		// Since till now, we had blocked signals being generated frm the mode
       
   250 		// view will be unaware of the items that we added. Hence, inform the view
       
   251 		// explicitly in one shot
       
   252 		QModelIndex leftIndex = mModel->index(0, 0);
       
   253 		QModelIndex rightIndex = mModel->index(loopEnd-1, 0);
       
   254 		dataChanged(leftIndex, rightIndex);
       
   255 		
       
   256 		// NOTE: To make sure that we always display proper month,
       
   257 		// two calls have been  made to scrollTo(), one with top
       
   258 		// visible item and other with bottom visible item
       
   259 		// Calculate the first visible item in the grid
       
   260 		QModelIndex firstVisibleIndex = mModel->index(indexToBeScrolled - 
       
   261 								(KNumOfVisibleRows * KCalenDaysInWeek - 1), 0);
       
   262 		scrollTo(firstVisibleIndex);
       
   263 		
       
   264 		
       
   265 		// Calculate the last visible item in the grid
       
   266 		QModelIndex lastVisibleIndex = mModel->index(indexToBeScrolled, 0);
       
   267 		scrollTo(lastVisibleIndex);
       
   268 	}
       
   269 	
       
   270 	OstTraceFunctionExit0( CALENMONTHGRID_UPDATEMONTHGRIDMODEL_EXIT );
       
   271 }
       
   272 
       
   273 /*!
       
   274 	Updates the view with jprevious month dates when calendar is opened for the 
       
   275 	first time to improve the opening time
       
   276  */
       
   277 void CalenMonthGrid::updateMonthGridWithInActiveMonths(
       
   278 										QList<CalenMonthData> &monthDataArray)
       
   279 {	
       
   280     OstTraceFunctionEntry0( CALENMONTHGRID_UPDATEMONTHGRIDWITHINACTIVEMONTHS_ENTRY );
       
   281 		
       
   282 	// Prepend the required rows
       
   283 	handlePrependingRows(monthDataArray);
       
   284 	
       
   285 	// Append the required rows
       
   286 	handleAppendingRows(monthDataArray);
       
   287 	
       
   288 	int rowsInPrevMonth = mView->rowsInPrevMonth();
       
   289 	
       
   290 	// Calculate the proper index to be scrolled to
       
   291 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
   292 	QModelIndex indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   293 	mIsAtomicScroll = true;
       
   294 	scrollTo(indexToBeScrolled);
       
   295 		
       
   296 	// Scroll to proper index
       
   297 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
   298 								   KCalenDaysInWeek) - 1;
       
   299 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   300 	mIsAtomicScroll = true;
       
   301 	scrollTo(indexToBeScrolled);
       
   302 	
       
   303 	// Update the sart position of the content widget
       
   304 	mStartPos = mContentWidget->pos();
       
   305 	
       
   306 	// Now connect to the signal which gets emitted when any item on the grid 
       
   307 	// is tapped.
       
   308 	connect(this, SIGNAL(activated(const QModelIndex &)), this,
       
   309 						SLOT(itemActivated(const QModelIndex &)));
       
   310 	
       
   311 	OstTraceFunctionExit0( CALENMONTHGRID_UPDATEMONTHGRIDWITHINACTIVEMONTHS_EXIT );
       
   312 }
       
   313 
       
   314 /*!
       
   315 	Updates the view with just event indicators
       
   316  */
       
   317 void CalenMonthGrid::updateMonthGridWithEventIndicators(
       
   318 										QList<CalenMonthData> &monthDataArray)
       
   319 {
       
   320     OstTraceFunctionEntry0( CALENMONTHGRID_UPDATEMONTHGRIDWITHEVENTINDICATORS_ENTRY );
       
   321     
       
   322 	int count(monthDataArray.count());
       
   323 	for(int i = 0; i < count; i++) {
       
   324 		// Check if the day has events
       
   325 		if (monthDataArray[i].HasEvents()) {
       
   326 			QModelIndex itemIndex = mModel->index(i,0);
       
   327 			QVariant itemData = itemIndex.data(Qt::UserRole + 1);
       
   328 			QVariantList list = itemData.toList();
       
   329 			list.replace(CalendarNamespace::CalendarMonthEventRole, true);
       
   330 			mModel->itemFromIndex(itemIndex)->setData(list);
       
   331 		}
       
   332 	}
       
   333 	
       
   334 	OstTraceFunctionExit0( CALENMONTHGRID_UPDATEMONTHGRIDWITHEVENTINDICATORS_EXIT );
       
   335 }
       
   336 
       
   337 /*!
       
   338  Scrolls the content dowmwards
       
   339  */
       
   340 void CalenMonthGrid::downGesture()
       
   341 {
       
   342     OstTraceFunctionEntry0( CALENMONTHGRID_DOWNGESTURE_ENTRY );
       
   343     
       
   344     // Make sure that content widget is properly placed
       
   345     // We are doing this as tapping on inactive date of previous month is leading to
       
   346     // position the grid at wrong place after scrolling down. Hence, set the grid
       
   347     // at proper position before we start actual scrolling
       
   348     if (mIsNonActiveDayFocused) {
       
   349         mIsAtomicScroll = true;
       
   350         int itemToBeScrolled = mView->rowsInPrevMonth() * KCalenDaysInWeek;
       
   351         QModelIndex indexToBeScrolled  = mModel->index(itemToBeScrolled, 0);
       
   352         scrollTo(indexToBeScrolled);
       
   353     }
       
   354    
       
   355     // Set the required flags
       
   356     mDirection = down;
       
   357     mIsAtomicScroll = false;
       
   358     
       
   359     // Set the active and inactive dates
       
   360     QDateTime activeMonth = mView->getActiveDay();
       
   361     // For previous month, substract one month to the current month
       
   362     setActiveDates(activeMonth.addMonths(-1).date());
       
   363     mActiveDatesSet = true;
       
   364     // Set the focus to proper date before scrolling to avoid somekind of
       
   365     // jerkiness
       
   366     if (!mIsNonActiveDayFocused) {
       
   367         setFocusToProperDay();
       
   368     }
       
   369     
       
   370     // Start the scrolling
       
   371     QPointF targetPos(0.0, 0.0);
       
   372     scrollContentsTo(targetPos,500);
       
   373     
       
   374     OstTraceFunctionExit0( CALENMONTHGRID_DOWNGESTURE_EXIT );
       
   375 }
       
   376 
       
   377 /*!
       
   378  Scrolls the content upwards
       
   379  */
       
   380 void CalenMonthGrid::upGesture()
       
   381 {
       
   382     OstTraceFunctionEntry0( CALENMONTHGRID_UPGESTURE_ENTRY );
       
   383     
       
   384    
       
   385     // Set the required flags
       
   386     mDirection = up;
       
   387     mIsAtomicScroll = false;
       
   388     
       
   389     // Set the active and inactive dates
       
   390     QDateTime activeMonth = mView->getActiveDay();
       
   391     // For next month, add one month to the current month
       
   392     setActiveDates(activeMonth.addMonths(1).date());
       
   393     mActiveDatesSet = true;
       
   394     // Set the focus to proper date before scrolling to avoid somekind of
       
   395     // jerkiness
       
   396     if (!mIsNonActiveDayFocused) {
       
   397         setFocusToProperDay();
       
   398     }
       
   399     
       
   400     // Start the scrolling
       
   401     QPointF targetPos(0.0, mStartPos.y() - size().height());
       
   402     scrollContentsTo(-targetPos,500);
       
   403 	
       
   404     OstTraceFunctionExit0( CALENMONTHGRID_UPGESTURE_EXIT );
       
   405 }
       
   406 
       
   407 /*!
       
   408  Function to listen mouse press events
       
   409  */
       
   410 void CalenMonthGrid::mousePressEvent(QGraphicsSceneMouseEvent* event)
       
   411 {
       
   412     OstTraceFunctionEntry0( CALENMONTHGRID_MOUSEPRESSEVENT_ENTRY );
       
   413     
       
   414 	// Pass it to parent
       
   415 	HbGridView::mousePressEvent(event);
       
   416 	
       
   417 	OstTraceFunctionExit0( CALENMONTHGRID_MOUSEPRESSEVENT_EXIT );
       
   418 }
       
   419 
       
   420 /*!
       
   421  Function to listen mouse release events
       
   422  */
       
   423 void CalenMonthGrid::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
       
   424 {
       
   425     OstTraceFunctionEntry0( CALENMONTHGRID_MOUSERELEASEEVENT_ENTRY );
       
   426     
       
   427 	// Pass it grid view if pan gesture is not in progress else pass it to
       
   428 	// scrollarea. Problem here is, if we pass to gridview when panning, then 
       
   429 	// its emitting item activated signal simply becasue of which focus item
       
   430 	// is getting changed when you finish the pan / shake
       
   431 	if (!mIsPanGesture) {
       
   432 		HbGridView::mouseReleaseEvent(event);
       
   433 	} else {
       
   434 		HbScrollArea::mouseReleaseEvent(event);
       
   435 	}
       
   436 	
       
   437 	OstTraceFunctionExit0( CALENMONTHGRID_MOUSERELEASEEVENT_EXIT );
       
   438 }
       
   439 
       
   440 /*!
       
   441     Function to list for all the gesture events
       
   442  */
       
   443 void CalenMonthGrid::gestureEvent(QGestureEvent *event)
       
   444 {
       
   445     OstTraceFunctionEntry0( CALENMONTHGRID_GESTUREEVENT_ENTRY );
       
   446     
       
   447    if(HbPanGesture *gesture = qobject_cast<HbPanGesture *>(event->gesture(Qt::PanGesture))) {
       
   448         if (gesture->state() == Qt::GestureStarted) {
       
   449             mIsAtomicScroll = false;
       
   450             if (!mIsPanGesture) {
       
   451                 mDirection = invalid;
       
   452                 mStartPos = mContentWidget->pos();
       
   453                 // TODO: This work around till framework provides an api
       
   454                 // to know the direciton of the pan, until then we need
       
   455                 // calculate the direction explicitly
       
   456                 // Get to know the direction of the gesture
       
   457                 // Use our defined threshold temporarily till scrollarea 
       
   458                 // frm orbit side is made clever enough not to scroll in other direction
       
   459                 // apart frm the registered scroll direction
       
   460                 QPointF delta = gesture->delta();
       
   461                 // Check the current orientation of the device and
       
   462                 // swap the vertical and horizontal distances in landscape
       
   463                 qreal horizontalDiff = 0.0;
       
   464                 qreal verticalDiff = 0.0;
       
   465                 if (hbInstance->allMainWindows().at(0)->orientation() == Qt::Vertical) {
       
   466                     horizontalDiff = delta.x();
       
   467                     verticalDiff = delta.y();
       
   468                 } else {
       
   469                     horizontalDiff = delta.y();
       
   470                     verticalDiff = delta.x();
       
   471                 }
       
   472                 if (abs(horizontalDiff) > MAX_PAN_DIRECTION_THRESHOLD) {
       
   473                     // Now see if y coord diff has crossed threshold
       
   474                     if (verticalDiff > MAX_PAN_DIRECTION_THRESHOLD) {
       
   475                         mIsPanGesture = true;
       
   476                         mIgnoreItemActivated = true;
       
   477                         mDirection = down;
       
   478                     } else if (verticalDiff < -MAX_PAN_DIRECTION_THRESHOLD){
       
   479                         mIsPanGesture = true;
       
   480                         mIgnoreItemActivated = true;
       
   481                         mDirection = up;
       
   482                     } else {
       
   483                         event->accept(Qt::PanGesture);
       
   484                         OstTraceFunctionExit0( CALENMONTHGRID_GESTUREEVENT_EXIT );
       
   485                         return;
       
   486                     }
       
   487                 } else if (abs(horizontalDiff) < MAX_PAN_DIRECTION_THRESHOLD) {
       
   488                    if (verticalDiff > MIN_PAN_DIRECTION_THRESHOLD) {
       
   489                         mIsPanGesture = true;
       
   490                         mIgnoreItemActivated = true;
       
   491                         mDirection = down;
       
   492                    } else if (verticalDiff < -MIN_PAN_DIRECTION_THRESHOLD){
       
   493                         mIsPanGesture = true;
       
   494                         mIgnoreItemActivated = true;
       
   495                         mDirection = up;
       
   496                    }else {
       
   497                        event->accept(Qt::PanGesture);
       
   498                        OstTraceFunctionExit0( DUP1_CALENMONTHGRID_GESTUREEVENT_EXIT );
       
   499                        return;
       
   500                    }
       
   501                 } 
       
   502             }
       
   503         }
   133         }
   504     } else if(HbSwipeGesture *gesture = qobject_cast<HbSwipeGesture *>(event->gesture(Qt::SwipeGesture))) {
   134         
   505         if (gesture->state() == Qt::GestureStarted) {
   135     TRACE_EXIT_POINT;
   506             mIsAtomicScroll = false;
   136     
   507             mDirection = invalid;
   137     }
   508             if (gesture->sceneVerticalDirection() == QSwipeGesture::Down) {
   138 
   509                 mDirection = down;
   139 
   510             } else if (gesture->sceneVerticalDirection() == QSwipeGesture::Up) {
   140 // ---------------------------------------------------------
   511                 mDirection = up;
   141 // CCalenMonthGrid::FirstDayOfGrid
   512             } else {
   142 // Return first day of grid
   513                 event->accept(Qt::SwipeGesture);
   143 // (other items were commented in a header).
   514                 OstTraceFunctionExit0( DUP2_CALENMONTHGRID_GESTUREEVENT_EXIT );
   144 // ---------------------------------------------------------
   515                 return;
   145 //
   516             }
   146 TTime CCalenMonthGrid::FirstDayOfGrid()
       
   147     {
       
   148     TRACE_ENTRY_POINT;
       
   149     
       
   150     TRACE_EXIT_POINT;
       
   151     return iFirstDayOfGrid;
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CCalenMonthGrid::SetFirstDayOfGrid
       
   156 // Set argument aDay to first day of Grid
       
   157 // (other items were commented in a header).
       
   158 // ---------------------------------------------------------
       
   159 //
       
   160 void CCalenMonthGrid::SetFirstDayOfGrid(TTime aDay)
       
   161     {
       
   162     TRACE_ENTRY_POINT;
       
   163     
       
   164     iFirstDayOfGrid = aDay;
       
   165     
       
   166     TRACE_EXIT_POINT;
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------
       
   170 // CCalenMonthGrid::CreateItemDrawerL
       
   171 // Creates CFormattedCellListBoxItemDrawer,
       
   172 // actually CCalenMonthCellListBoxItemDrawer.
       
   173 // (other items were commented in a header).
       
   174 // ---------------------------------------------------------
       
   175 //
       
   176 void CCalenMonthGrid::CreateItemDrawerL()
       
   177     {
       
   178     TRACE_ENTRY_POINT;
       
   179     
       
   180     CCalenMonthCellListBoxData* columnData = CCalenMonthCellListBoxData::NewL();
       
   181     CleanupStack::PushL( columnData );
       
   182 
       
   183     iItemDrawer = new(ELeave)
       
   184         CCalenMonthCellListBoxItemDrawer(Model(), this, iEikonEnv->NormalFont(), columnData);
       
   185 
       
   186     CleanupStack::Pop(); // columnData
       
   187     
       
   188     TRACE_EXIT_POINT;
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------
       
   192 // 
       
   193 // ---------------------------------------------------------
       
   194 //
       
   195 void CCalenMonthGrid::UpdateScrollBarsL()
       
   196     {
       
   197     TRACE_ENTRY_POINT;
       
   198     
       
   199     // Override default implementation and just turn scrollbars off
       
   200     // This is needed, because CAknGrid doesn't respect scrollbar
       
   201     // visibility settings, but turns them on e.g. in HandleResourceChange
       
   202     CEikScrollBarFrame* sbf = ScrollBarFrame();
       
   203     if ( sbf )
       
   204         {
       
   205         sbf->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, 
       
   206                                      CEikScrollBarFrame::EOff);
   517         }
   207         }
   518     } else if (HbTapGesture *gesture = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
   208     
   519         if(gesture->state() == Qt::GestureFinished) {
   209     TRACE_EXIT_POINT;
   520             // Check if scrolling is in progress
   210     }
   521             if (mDirection != invalid) {
       
   522                 // Set the pan flag to true so that grid adjusts to nearest
       
   523                 // month after tapping
       
   524                 mIsPanGesture = true;
       
   525                 handlePanGestureFinished();
       
   526             }
       
   527         }
       
   528     }
       
   529    
       
   530    if (mDirection!= invalid) {
       
   531         // Call the parent class to perform the pan gesture
       
   532         // When scrolling finished, month grid will adjust to show the proper month
       
   533         HbScrollArea::gestureEvent(event);
       
   534    }
       
   535    
       
   536    OstTraceFunctionExit0( DUP3_CALENMONTHGRID_GESTUREEVENT_EXIT );
       
   537 }
       
   538 
       
   539 /*!
       
   540  Gets called when scrolling finishes to update the model
       
   541  */
       
   542 void CalenMonthGrid::scrollingFinished()
       
   543 {
       
   544     OstTraceFunctionEntry0( CALENMONTHGRID_SCROLLINGFINISHED_ENTRY );
       
   545     
       
   546 	if (mIsPanGesture) {
       
   547 		handlePanGestureFinished();
       
   548 	} else if(!mIsAtomicScroll) {
       
   549 		QDateTime activeDate = mView->getActiveDay();
       
   550 		if(mDirection == down) { // down gesture
       
   551 			if (!mActiveDatesSet) {
       
   552 				setActiveDates(activeDate.addMonths(-1).date());
       
   553 				setFocusToProperDay();
       
   554 			}
       
   555 			prependRows();
       
   556 		} else if (mDirection == up) { //up gesture
       
   557 			if (!mActiveDatesSet) {
       
   558 				setActiveDates(activeDate.addMonths(1).date());
       
   559 				setFocusToProperDay();
       
   560 			}
       
   561 			appendRows();
       
   562 		}
       
   563 		mDirection = invalid;
       
   564 		mActiveDatesSet = false;
       
   565 	} else {
       
   566         mIsAtomicScroll = false;
       
   567         mDirection = invalid;
       
   568 	}
       
   569 	mIgnoreItemActivated = false;
       
   570 	
       
   571 	OstTraceFunctionExit0( CALENMONTHGRID_SCROLLINGFINISHED_EXIT );
       
   572 }
       
   573 
       
   574 /*!
       
   575  Function to handle completion of pan gesture
       
   576  */
       
   577 void CalenMonthGrid::handlePanGestureFinished()
       
   578 {
       
   579     OstTraceFunctionEntry0( CALENMONTHGRID_HANDLEPANGESTUREFINISHED_ENTRY );
       
   580     
       
   581 	mIsPanGesture = false;
       
   582 	// Get the first item that is visible
       
   583 	QList<HbAbstractViewItem *> list = visibleItems();
       
   584 	HbAbstractViewItem* item = list[0];
       
   585 	QModelIndex modelIndex = item->modelIndex();
       
   586 	
       
   587 	// Get the date which is visible at the above row
       
   588 	QList<CalenMonthData>& monthDataArray = mView->monthDataList();
       
   589 	QDateTime date = monthDataArray[modelIndex.row()].Day();
       
   590 	
       
   591 	// Check if this date belong to current active month or 
       
   592 	// previous month else future month
       
   593 	QDateTime activeMonth = mView->getActiveDay();
       
   594 	QDateTime prevMonth = activeMonth.addMonths(-1);
       
   595 	QDateTime nextMonth = activeMonth.addMonths(1);
       
   596 	int month = date.date().month();
       
   597 	if (month == activeMonth.date().month()) {
       
   598 		// Then pan is completed on current month
       
   599 		// Check if the date is more than half of the current month or it is
       
   600 		// more than or equal to half of the future month
       
   601 		if (date.date().day() > (activeMonth.date().daysInMonth()) / 2 ||
       
   602 				date.addDays(KNumOfVisibleRows*KCalenDaysInWeek).date().day() >=
       
   603 				(prevMonth.date().daysInMonth()) / 2) {
       
   604 			// up gesture to bring the next month
       
   605 			upGesture();
       
   606 		} else {
       
   607 			// we should again show the current month by scrolling downwards
       
   608 			mDirection = down;
       
   609 			mIsAtomicScroll = true;
       
   610 			scrollContentsTo(-mStartPos,500);
       
   611 		}
       
   612 	} else if (month == prevMonth.date().month()) {
       
   613 		// first visible item belongs to previous month
       
   614 		// Check if the date is more than half of the previous month
       
   615 		if (date.date().day() > (prevMonth.date().daysInMonth()) / 2) {
       
   616 			// we should again show the current month by scrolling upwards
       
   617 			mDirection = up;
       
   618 			mIsAtomicScroll = true;
       
   619 			scrollContentsTo(-mStartPos,500);
       
   620 		} else {
       
   621 			// down gesture to show the previous month
       
   622 			downGesture();
       
   623 		}
       
   624 	} else if (month == prevMonth.addMonths(-1).date().month()) {
       
   625 		// first visible date belong to previous to previous month
       
   626 		// hence, scroll down to bring the previous month
       
   627 		downGesture();
       
   628 	} else if (month == nextMonth.date().month()) {
       
   629 		// first visible item belongs to next month
       
   630 		// Check if the date is more than half of the next month
       
   631 		if (date.date().day() > (nextMonth.date().daysInMonth()) / 2) {
       
   632 			// up gesture to bring the next month
       
   633 			upGesture();
       
   634 		} else {
       
   635 			// we should again show the current month by scrolling upwards
       
   636 			mDirection = up;
       
   637 			scrollContentsTo(-mStartPos,500);
       
   638 		}
       
   639 	} else if (month == nextMonth.addMonths(1).date().month()) {
       
   640 		// first visible date belongs to next to next month
       
   641 		// hence, scroll up to show the next month
       
   642 		upGesture();
       
   643 	}
       
   644 	
       
   645 	OstTraceFunctionExit0( CALENMONTHGRID_HANDLEPANGESTUREFINISHED_EXIT );
       
   646 }
       
   647 
       
   648 /*!
       
   649  Called when down gesture is performed. Adds the new previous month details
       
   650  to the model
       
   651  */
       
   652 void CalenMonthGrid::prependRows()
       
   653 {
       
   654     OstTraceFunctionEntry0( CALENMONTHGRID_PREPENDROWS_ENTRY );
       
   655 	
       
   656 	// Block the signals generated by model, this is being done as
       
   657 	// we want to avoid the overload of view listening to signals
       
   658 	mModel->blockSignals(true);
       
   659 		
       
   660 	mIsNonActiveDayFocused = false;
       
   661 	
       
   662 	int rowsInFutMonthEarlier = mView->rowsInFutMonth();
       
   663 	int rowsInPrevMonthEarlier = mView->rowsInPrevMonth();
       
   664 	
       
   665 	// remove the cells in the future month
       
   666 	int deleteFromIndex = (rowsInPrevMonthEarlier + KNumOfVisibleRows) * KCalenDaysInWeek;
       
   667 	int numOfIndices = rowsInFutMonthEarlier * KCalenDaysInWeek;
       
   668 	
       
   669 	// Get the updated dates from the view
       
   670 	mView->updateModelWithPrevMonth();
       
   671 	QList<CalenMonthData >& monthDataList = mView->monthDataList();
       
   672 	
       
   673 	// Prepend the required rows
       
   674 	handlePrependingRows(monthDataList);
       
   675 		
       
   676 	// Since, we have finished setData, Now unblock the signals
       
   677 	mModel->blockSignals(false);
       
   678 	
       
   679 	int rowsInPrevMonth = mView->rowsInPrevMonth();
       
   680 	int countToBeAdded = rowsInPrevMonth * KCalenDaysInWeek;
       
   681 		
       
   682 	// Since till now, we had blocked signals being generated frm the model
       
   683 	// view will be unaware of the items that we added. Hence, inform the view
       
   684 	// explicitly in one shot
       
   685 	QModelIndex leftIndex = mModel->index(0, 0);
       
   686 	QModelIndex rightIndex = mModel->index(countToBeAdded-1, 0);
       
   687 	dataChanged(leftIndex, rightIndex);
       
   688 		
       
   689 	// Now remove the necessary items frm the model
       
   690 	mModel->removeRows(deleteFromIndex+countToBeAdded, numOfIndices);
       
   691 	mIsAtomicScroll = true;
       
   692 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
   693 	QModelIndex indexToBeScrolled  = mModel->index(itemToBeScrolled, 0);
       
   694 	scrollTo(indexToBeScrolled);
       
   695 	
       
   696 	// Scroll to proper index
       
   697 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
   698 								   KCalenDaysInWeek) - 1;
       
   699 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   700 	mIsAtomicScroll = true;
       
   701 	scrollTo(indexToBeScrolled);
       
   702 	// Update the mCurrentRow
       
   703 	mCurrentRow += countToBeAdded;
       
   704 	
       
   705 	// Update the sart position of the content widget
       
   706 	mStartPos = mContentWidget->pos();
       
   707 	
       
   708 	OstTraceFunctionExit0( CALENMONTHGRID_PREPENDROWS_EXIT );
       
   709 }
       
   710 
       
   711 /*!
       
   712 	Helper function that prepends the required rows to the model
       
   713  */
       
   714 void CalenMonthGrid::handlePrependingRows(QList<CalenMonthData > &monthDataList)
       
   715 {
       
   716     OstTraceFunctionEntry0( CALENMONTHGRID_HANDLEPREPENDINGROWS_ENTRY );
       
   717     
       
   718 	QDateTime currDate = mView->getCurrentDay();
       
   719 	QDateTime currDateTime = CalenDateUtils::beginningOfDay( currDate );
       
   720 	int rowsInPrevMonth = mView->rowsInPrevMonth();
       
   721 	// Add the new days
       
   722 	int countToBeAdded = rowsInPrevMonth * KCalenDaysInWeek;
       
   723 	
       
   724 	mModel->insertRows(0, countToBeAdded);
       
   725 	
       
   726 	for (int i = 0; i < countToBeAdded; i++) {
       
   727 		QDateTime dateTime = monthDataList[i].Day();
       
   728 		
       
   729 		// Get the localised string for the day
       
   730 		QModelIndex currentIndex = mModel->index(i, 0);
       
   731 		
       
   732 		// Create the variant list to contain the date to depict a grid item
       
   733 		QVariantList itemData;
       
   734 		
       
   735 		// NOTE: Add the data in the order mentioned in the 
       
   736 		// CalendarNamespace::DataRole enum. Dont change the order.
       
   737 		itemData << mLocalisedDates.at(dateTime.date().day()-1);;
       
   738 				
       
   739 		// Disable the focus role
       
   740 		itemData << false;
       
   741 		
       
   742 		// Check for current day
       
   743 		if  (currDateTime == CalenDateUtils::beginningOfDay( dateTime )) {
       
   744 			// Set the underline icon attribute
       
   745 			itemData << true;
       
   746 		} else {
       
   747 			itemData << false;
       
   748 		}
       
   749 		
       
   750 		// Update the event indicators
       
   751 		if (monthDataList[i].HasEvents()) {
       
   752 			// Set the event indicator attribute
       
   753 			itemData << true;
       
   754 		} else {
       
   755 			itemData << false;
       
   756 		}
       
   757 		
       
   758 		// Add default text color
       
   759 		itemData << false;
       
   760 		
       
   761 		// Set the data to model
       
   762 		mModel->itemFromIndex(currentIndex)->setData(itemData);
       
   763 	}
       
   764 	
       
   765 	OstTraceFunctionExit0( CALENMONTHGRID_HANDLEPREPENDINGROWS_EXIT );
       
   766 }
       
   767 
       
   768 /*!
       
   769  Called when Up gwsture is performed. Adds the new future month details
       
   770  to the model
       
   771  */
       
   772 void CalenMonthGrid::appendRows()
       
   773 {
       
   774     OstTraceFunctionEntry0( CALENMONTHGRID_APPENDROWS_ENTRY );
       
   775 	
       
   776 	// Block the signals generated by model, this is being done as
       
   777 	// we want to avoid the overload of view listening to signals
       
   778 	mModel->blockSignals(true);
       
   779 	
       
   780 	mIsNonActiveDayFocused = false;
       
   781 	
       
   782 	int rowsInFutMonth = mView->rowsInFutMonth();
       
   783 	int rowsInPrevMonth = mView->rowsInPrevMonth();
       
   784 	// remove the cells in the previous month
       
   785 	int countToBeDeleted = rowsInPrevMonth * KCalenDaysInWeek;
       
   786 		
       
   787 	// Get the updated dates from the view
       
   788 	mView->updateModelWithFutureMonth();
       
   789 	QList<CalenMonthData >& monthDataList = mView->monthDataList();
       
   790 	
       
   791 	// Get the model count before we add any rows into the mode
       
   792 	int rowCount = mModel->rowCount();
       
   793 	// Append the required rows
       
   794 	handleAppendingRows(monthDataList);
       
   795 	
       
   796 	// Since, we have finished setData, Now unblock the signals
       
   797 	mModel->blockSignals(false);
       
   798 	
       
   799 	// Since till now, we had blocked signals being generated frm the mode
       
   800 	// view will be unaware of the items that we added. Hence, inform the view
       
   801 	// explicitly in one shot
       
   802 	QModelIndex leftIndex = mModel->index(rowCount-1, 0);
       
   803 	QModelIndex rightIndex = mModel->index(mModel->rowCount()-1, 0);
       
   804 	dataChanged(leftIndex, rightIndex);
       
   805 		
       
   806 	// Update the mCurrentRow
       
   807 	mCurrentRow -= (countToBeDeleted);
       
   808 	for (int i = 0; i < countToBeDeleted; i++) {
       
   809 		mModel->removeRow(0);
       
   810 	}
       
   811 	
       
   812 	mIsAtomicScroll = true;
       
   813 	
       
   814 	rowsInFutMonth = mView->rowsInFutMonth();
       
   815 	rowsInPrevMonth = mView->rowsInPrevMonth();
       
   816 	
       
   817 	// Calculate the proper index to be scrolled to
       
   818 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
   819 	QModelIndex indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   820 	scrollTo(indexToBeScrolled);
       
   821 	
       
   822 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
   823 									   KCalenDaysInWeek) - 1;
       
   824 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   825 	mIsAtomicScroll = true;
       
   826 	scrollTo(indexToBeScrolled);
       
   827 	
       
   828 	// Update the sart position of the content widget
       
   829     mStartPos = mContentWidget->pos();
       
   830     
       
   831     OstTraceFunctionExit0( CALENMONTHGRID_APPENDROWS_EXIT );
       
   832 }
       
   833 
       
   834 /*!
       
   835 	Helper function that appends the required rows to the model
       
   836  */
       
   837 void CalenMonthGrid::handleAppendingRows(QList<CalenMonthData > &monthDataList)
       
   838 {
       
   839     OstTraceFunctionEntry0( CALENMONTHGRID_HANDLEAPPENDINGROWS_ENTRY );
       
   840     
       
   841 	QDateTime currDate = mView->getCurrentDay();
       
   842 	QDateTime currDateTime = CalenDateUtils::beginningOfDay( currDate );
       
   843 	int rowsInFutMonth = mView->rowsInFutMonth();
       
   844 	int countToBeAdded = rowsInFutMonth * KCalenDaysInWeek;
       
   845 	int lastVisibleIndex = monthDataList.count() - countToBeAdded;
       
   846 	
       
   847 	int rowCount = mModel->rowCount();
       
   848 	mModel->insertRows(rowCount, countToBeAdded);
       
   849 	
       
   850 	for (int i = 0; i < countToBeAdded; i++) {
       
   851 		QModelIndex currentIndex = mModel->index(rowCount + i, 0);
       
   852 				
       
   853 		QDateTime dateTime = monthDataList[lastVisibleIndex + i].Day();
       
   854 		
       
   855 		// Create the variant list to contain the date to depict a grid item
       
   856 		QVariantList itemData;
       
   857 		
       
   858 		// NOTE: Add the data in the order mentioned in the 
       
   859 		// CalendarNamespace::DataRole enum. Dont change the order.
       
   860 		itemData << mLocalisedDates.at(dateTime.date().day()-1);;
       
   861 		
       
   862 		// Disable the focus role
       
   863 		itemData << false;
       
   864 		
       
   865 		// Check for current day
       
   866 		if (currDateTime == CalenDateUtils::beginningOfDay( dateTime )) {
       
   867 			// Set the underline icon attribute
       
   868 			itemData << true;
       
   869 		} else {
       
   870 			itemData << false;
       
   871 		}
       
   872 		
       
   873 		// Update the event indicators
       
   874 		if (monthDataList[lastVisibleIndex + i].HasEvents()) {
       
   875 			// Set the event indicator attribute
       
   876 			itemData << true;
       
   877 		} else {
       
   878 			itemData << false;
       
   879 		}
       
   880 		
       
   881 		// Add default text color
       
   882 		itemData << false;
       
   883 				
       
   884 		// Set the data to model
       
   885 		mModel->itemFromIndex(currentIndex)->setData(itemData);
       
   886 	}
       
   887 	
       
   888 	OstTraceFunctionExit0( CALENMONTHGRID_HANDLEAPPENDINGROWS_EXIT );
       
   889 }
       
   890 
       
   891 /*!
       
   892  Slot to handle when a particular grid item is tapped
       
   893  */
       
   894 void CalenMonthGrid::itemActivated(const QModelIndex &index)
       
   895 {
       
   896     OstTraceFunctionEntry0( CALENMONTHGRID_ITEMACTIVATED_ENTRY );
       
   897     
       
   898 	if (mIgnoreItemActivated) {
       
   899 		mIgnoreItemActivated = false;
       
   900 		OstTraceFunctionExit0( CALENMONTHGRID_ITEMACTIVATED_EXIT );
       
   901 		return;
       
   902 	}
       
   903 	mIsNonActiveDayFocused = false;
       
   904 	// Check if the same item has been tapped twice
       
   905 	if (mCurrentRow == index.row()) {
       
   906 		// Launch the Day view
       
   907 		mView->launchDayView();
       
   908 	} else {
       
   909 		// Reset the focus attribute to this item		
       
   910 		QModelIndex itemIndex = mModel->index(mCurrentRow,0);
       
   911 		if(itemIndex.row() < 0 || itemIndex.row() >= mModel->rowCount() ||
       
   912 				itemIndex.column() < 0 || itemIndex.column() > mModel->columnCount()) {
       
   913 			OstTraceFunctionExit0( DUP1_CALENMONTHGRID_ITEMACTIVATED_EXIT );
       
   914 			return;
       
   915 		}
       
   916 		QVariant itemData = itemIndex.data(Qt::UserRole + 1);
       
   917 		QVariantList list = itemData.toList();
       
   918 		list.replace(CalendarNamespace::CalendarMonthFocusRole, false);
       
   919 		mModel->itemFromIndex(itemIndex)->setData(list);
       
   920 		
       
   921 		// Inform view to update the context and preview panes
       
   922 		mCurrentRow = index.row();
       
   923 		itemIndex = mModel->index(mCurrentRow,0);
       
   924 		itemData = itemIndex.data(Qt::UserRole + 1);
       
   925 		list = itemData.toList();
       
   926 		list.replace(CalendarNamespace::CalendarMonthFocusRole, 
       
   927 								 true);
       
   928 		mModel->itemFromIndex(itemIndex)->setData(list);
       
   929 		// Check if inactive date is tapped
       
   930 		QDateTime activeMonth = mView->getActiveDay();
       
   931 		int month = activeMonth.date().month();
       
   932 		QList<CalenMonthData >& monthDataList = mView->monthDataList();
       
   933 		if(month != monthDataList[mCurrentRow].Day().date().month()){
       
   934 			// Set the flag
       
   935 			mIsNonActiveDayFocused = true;
       
   936 			QDateTime nonActiveFocusedDay = monthDataList[mCurrentRow].Day();
       
   937 			
       
   938 			// Add one month to active month
       
   939 			activeMonth = activeMonth.addMonths(1);
       
   940 			if (activeMonth.date().month() == 
       
   941 			        nonActiveFocusedDay.date().month()) {
       
   942 				mDirection = up;
       
   943 				// up gesture
       
   944 				upGesture();
       
   945 			} else {
       
   946 				mDirection = down;
       
   947 				// down gesture
       
   948 				downGesture();
       
   949 			}
       
   950 		} 
       
   951 		mView->setContextForActiveDay(index.row());
       
   952 	}
       
   953 	
       
   954 	OstTraceFunctionExit0( DUP2_CALENMONTHGRID_ITEMACTIVATED_EXIT );
       
   955 }
       
   956 
       
   957 /*!
       
   958  Sets the focus to proper day after the flick scrollng
       
   959  */
       
   960 void CalenMonthGrid::setFocusToProperDay()
       
   961 {
       
   962     OstTraceFunctionEntry0( CALENMONTHGRID_SETFOCUSTOPROPERDAY_ENTRY );
       
   963     
       
   964 	// Calculate the new item to be focussed
       
   965 	QDateTime oldFocussedDate = mView->getActiveDay();
       
   966 	QList<CalenMonthData> monthDataList = mView->monthDataList();
       
   967 	int listCount = monthDataList.count();
       
   968 	int rowsInPrevMonth = mView->rowsInPrevMonth();
       
   969 	QDateTime dateToBeFocussed;
       
   970 	int indexStart = 0;
       
   971 	int indexEnd = listCount - 1;
       
   972 	if (mDirection == up) {
       
   973 		dateToBeFocussed = oldFocussedDate.addMonths(1); // add the month 
       
   974 		indexStart = (rowsInPrevMonth + 4) * KCalenDaysInWeek;
       
   975 	} else if (mDirection == down) {
       
   976 		dateToBeFocussed = oldFocussedDate.addMonths(-1); // substract the month
       
   977 		indexEnd = (rowsInPrevMonth + 1) * KCalenDaysInWeek;
       
   978 	}
       
   979 	// Reset the focus attribute to earlier current item
       
   980 	QModelIndex index = mModel->index(mCurrentRow,0);
       
   981 	QVariant itemData = index.data(Qt::UserRole + 1);
       
   982 	QVariantList list = itemData.toList();
       
   983 	list.replace(CalendarNamespace::CalendarMonthFocusRole, false);
       
   984 	mModel->itemFromIndex(index)->setData(list);
       
   985 	
       
   986 	// Search for this date in the model
       
   987 	for (int i = indexStart; i <= indexEnd; i++) {
       
   988 		if (monthDataList[i].Day().date() == dateToBeFocussed.date()) {
       
   989 			index = mModel->index(i,0);
       
   990 			itemData = index.data(Qt::UserRole + 1);
       
   991 			list = itemData.toList();
       
   992 			list.replace(CalendarNamespace::CalendarMonthFocusRole,
       
   993 										 true);
       
   994 			mModel->itemFromIndex(index)->setData(list);
       
   995 			mCurrentRow = i;
       
   996 			mView->setContextForActiveDay(i);
       
   997 			break;
       
   998 		}
       
   999 	}
       
  1000 	
       
  1001 	OstTraceFunctionExit0( CALENMONTHGRID_SETFOCUSTOPROPERDAY_EXIT );
       
  1002 }
       
  1003 
       
  1004 /*!
       
  1005  Sets the appropriate text color depending upon the active dates
       
  1006  */
       
  1007 void CalenMonthGrid::setActiveDates(QDate activeDate)
       
  1008 {
       
  1009     OstTraceFunctionEntry0( CALENMONTHGRID_SETACTIVEDATES_ENTRY );
       
  1010     
       
  1011 	// By default, text color will be set as inactive date color
       
  1012 	// set active date color only for the dates that fall in current month
       
  1013 	// So, in the whole data array, start from where the current month starts
       
  1014 	// and stop the loop where it the current month ends
       
  1015 	
       
  1016 	int start = 0;
       
  1017 	QList<CalenMonthData >& monthDataList = mView->monthDataList();
       
  1018 	int end = monthDataList.count();
       
  1019 	
       
  1020 	// Calculate the start and end values
       
  1021 	QDate firstDateInGrid = mView->firstDayOfGrid().date();
       
  1022 	
       
  1023 	// Get the date where active month starts
       
  1024 	QDate startOfActiveMonth(activeDate.year(), activeDate.month(),1);
       
  1025 	// Number of days frm start of the grid to start of the month
       
  1026 	start = firstDateInGrid.daysTo(startOfActiveMonth);
       
  1027 	
       
  1028 	// Get the date where active month ends
       
  1029 	QDate endOfActiveMonth = startOfActiveMonth.addDays(
       
  1030 													activeDate.daysInMonth());
       
  1031 	// Number of days frm start of the grid to end of the month
       
  1032 	end = firstDateInGrid.daysTo(endOfActiveMonth);
       
  1033 	
       
  1034 	// Set the active text color
       
  1035 	if (start >= 0 && end < monthDataList.count()) {
       
  1036         for (int i = start; i < end; i++) {	
       
  1037             QModelIndex index = mModel->index(i,0);
       
  1038             QVariant itemData = index.data(Qt::UserRole + 1);
       
  1039             QVariantList list = itemData.toList();
       
  1040             list.replace(CalendarNamespace::CalendarMonthTextColorRole, true);
       
  1041             mModel->itemFromIndex(index)->setData(list);
       
  1042         }
       
  1043 	}
       
  1044 	
       
  1045 	// Now set the inactive text color to those which were active before the swipe
       
  1046 	if (mDirection == invalid) {
       
  1047 		// no need to do anything as other dates will be in inactive dates color
       
  1048 		OstTraceFunctionExit0( CALENMONTHGRID_SETACTIVEDATES_EXIT );
       
  1049 		return;
       
  1050 	}
       
  1051 	
       
  1052 	if (mDirection == up) {
       
  1053 		// Came here as user did up gesture
       
  1054 		// Get the activeDate that was set before the swipe
       
  1055 		activeDate = activeDate.addMonths(-1);
       
  1056 		
       
  1057 		// Get the date where active month starts
       
  1058 		startOfActiveMonth = QDate(activeDate.year(), activeDate.month(),1);
       
  1059 		// Number of days frm start of the grid to start of the month
       
  1060 		start = firstDateInGrid.daysTo(startOfActiveMonth);
       
  1061 		
       
  1062 		// Get the date where active month ends
       
  1063 		QDate endOfActiveMonth = startOfActiveMonth.addDays(activeDate.daysInMonth());
       
  1064 		// Number of days frm start of the grid to end of the month
       
  1065 		end = firstDateInGrid.daysTo(endOfActiveMonth);
       
  1066 	} else if (mDirection == down) {
       
  1067 		// Came here as user did down gesture
       
  1068 		// Get the activeDate that was set before the swipe
       
  1069 		activeDate = activeDate.addMonths(1);
       
  1070 		
       
  1071 		// Get the activeDate that was set before the swipe
       
  1072 		startOfActiveMonth = QDate(activeDate.year(), activeDate.month(),1); 
       
  1073 		// Number of days frm start of the grid to start of the month
       
  1074 		start = firstDateInGrid.daysTo(startOfActiveMonth);
       
  1075 		
       
  1076 		// Get the date where active month ends
       
  1077 		QDate endOfActiveMonth = startOfActiveMonth.addDays(activeDate.daysInMonth());
       
  1078 		// Number of days frm start of the grid to end of the month
       
  1079 		end = firstDateInGrid.daysTo(endOfActiveMonth);
       
  1080 	}
       
  1081 	
       
  1082 	// Set the inactive text color
       
  1083 	if (start >= 0 && end < monthDataList.count()) {
       
  1084         for (int i = start; i < end; i++) {		
       
  1085             QModelIndex index = mModel->index(i,0);
       
  1086             QVariant itemData = index.data(Qt::UserRole + 1);
       
  1087             QVariantList list = itemData.toList();
       
  1088             list.replace(CalendarNamespace::CalendarMonthTextColorRole, false);
       
  1089             mModel->itemFromIndex(index)->setData(list);
       
  1090         }
       
  1091 	}
       
  1092 	
       
  1093 	OstTraceFunctionExit0( DUP1_CALENMONTHGRID_SETACTIVEDATES_EXIT );
       
  1094 }
       
  1095 
       
  1096 /*!
       
  1097  To get current foucsed index of monthGrid
       
  1098  */
       
  1099 int CalenMonthGrid::getCurrentIndex()
       
  1100 {
       
  1101     OstTraceFunctionEntry0( CALENMONTHGRID_GETCURRENTINDEX_ENTRY );
       
  1102     
       
  1103 	OstTraceFunctionExit0( CALENMONTHGRID_GETCURRENTINDEX_EXIT );
       
  1104 	return mCurrentRow;
       
  1105 }
       
  1106 
       
  1107 /*!
       
  1108  To set the focus to Index 
       
  1109  */
       
  1110 void CalenMonthGrid::setCurrentIdex(int index)
       
  1111 {
       
  1112     OstTraceFunctionEntry0( CALENMONTHGRID_SETCURRENTIDEX_ENTRY );
       
  1113     
       
  1114 	itemActivated(mModel->index(index, 0));
       
  1115 	
       
  1116 	OstTraceFunctionExit0( CALENMONTHGRID_SETCURRENTIDEX_EXIT );
       
  1117 }
       
  1118 
       
  1119 /*!
       
  1120  Function to override the default behavior of hbgridview on orientation change
       
  1121  */
       
  1122 void CalenMonthGrid::orientationChanged(Qt::Orientation newOrientation)
       
  1123 {
       
  1124     OstTraceFunctionEntry0( CALENMONTHGRID_ORIENTATIONCHANGED_ENTRY );
       
  1125     
       
  1126     Q_UNUSED(newOrientation)
       
  1127 	// We are overriding this function to avoid the default behavior of
       
  1128 	// hbgridview on orientation change as it swaps the row and column counts
       
  1129 	// Calculate the proper index to be scrolled to
       
  1130 	int rowsInPrevMonth;
       
  1131     int itemToBeScrolled;
       
  1132     QModelIndex indexToBeScrolled;
       
  1133 	if (newOrientation == Qt::Horizontal) {
       
  1134 		rowsInPrevMonth = mView->rowsInPrevMonth();
       
  1135 		itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
  1136 		indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
  1137 		mIsAtomicScroll = true;
       
  1138 		scrollTo(indexToBeScrolled);
       
  1139 	} else {
       
  1140 		rowsInPrevMonth = mView->rowsInPrevMonth();
       
  1141 		itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
  1142 		indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
  1143 		mIsAtomicScroll = true;
       
  1144 		scrollTo(indexToBeScrolled);
       
  1145 		
       
  1146 		itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
  1147 				KCalenDaysInWeek) - 1;
       
  1148 		indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
  1149 		mIsAtomicScroll = true;
       
  1150 		scrollTo(indexToBeScrolled);
       
  1151 	}
       
  1152 	
       
  1153 	OstTraceFunctionExit0( CALENMONTHGRID_ORIENTATIONCHANGED_EXIT );
       
  1154 }
       
  1155 
       
  1156 /*!
       
  1157  Paint function to draw grid lines
       
  1158  */
       
  1159 void CalenMonthGrid::paint(QPainter* painter,
       
  1160                           const QStyleOptionGraphicsItem* option,
       
  1161                           QWidget* widget)
       
  1162 {
       
  1163     OstTraceFunctionEntry0( CALENMONTHGRID_PAINT_ENTRY );
       
  1164     
       
  1165 	Q_UNUSED(option);
       
  1166 	Q_UNUSED(widget);
       
  1167 	painter->setRenderHint(QPainter::NonCosmeticDefaultPen);
       
  1168 	
       
  1169 	// Set the required attributes to the pen
       
  1170 	QPen pen;
       
  1171 	HbDeviceProfile deviceProf;
       
  1172 	qreal unitValue = deviceProf.unitValue();
       
  1173 	qreal widthInPixels = GRIDLINE_WIDTH * unitValue;
       
  1174 	pen.setStyle(Qt::SolidLine);
       
  1175 	pen.setWidth(widthInPixels);
       
  1176 	pen.setBrush(mGridLineColor);
       
  1177 	
       
  1178 	// Store the old pen
       
  1179 	QPen oldPen = painter->pen();
       
  1180 	
       
  1181 	// Set the new pen to the painter
       
  1182 	painter->setPen(pen);
       
  1183 	
       
  1184 	// Get the sizes of content widget
       
  1185 	qreal contentHeight = mContentWidget->size().height();
       
  1186 	qreal contentWidth = mContentWidget->size().width();
       
  1187 	qreal rowWidth = 0.0;
       
  1188 	int numOfRows = 0;
       
  1189 	QPointF startPoint = mContentWidget->pos();
       
  1190 	
       
  1191 	// NOTE!!!: There is a filcker when we blindly draw equally spaced lines
       
  1192 	// on complete content widget when scrolling is finished. This happens only
       
  1193 	// when content widget size is changed due to the change in total number
       
  1194 	// of rows when we append or prepend rows. Hence, to avoid this, we draw
       
  1195 	// lines on complete content widget only when it is scrolling.
       
  1196 	// That means, as soon as scrolling is finished, we will end up drawing 
       
  1197 	// only 6 lines that are visible to the user.
       
  1198 	if (mDirection == invalid) {
       
  1199 		// Start point is left most point on the screen
       
  1200 		startPoint = QPointF(0,0);
       
  1201 		rowWidth = size().height() / KNumOfVisibleRows;
       
  1202 		numOfRows = KNumOfVisibleRows;
       
  1203 	} else {
       
  1204 		// Get the num of rows
       
  1205 		numOfRows = mModel->rowCount() / KCalenDaysInWeek;
       
  1206 		// Draw horizontal lines
       
  1207 		rowWidth = contentHeight / numOfRows;
       
  1208 	}
       
  1209 	
       
  1210 	QPointF endPoint(startPoint.x() + contentWidth, 
       
  1211 	                 startPoint.y());
       
  1212 	
       
  1213 	// Create the list of points for which lines have to be drawn
       
  1214 	// List should have even number of points so that it draws all the lines
       
  1215 	// Painter draws the line for first two points in the list and then second 
       
  1216 	// line for next two points in the list like that. Hence, list should 
       
  1217 	// contain even number of points
       
  1218 	// Dont draw the first horizontal line as we have thick line seperator
       
  1219 	// coming between day names and the month grid
       
  1220 	QVector<QPointF> pointList;
       
  1221 	for (int i = 1; i < numOfRows; i++) {
       
  1222 		pointList.append(QPointF(startPoint.x(), 
       
  1223 		                         startPoint.y() + (i * rowWidth)));
       
  1224 		pointList.append(QPointF(endPoint.x(), endPoint.y() + (i * rowWidth)));
       
  1225 	}
       
  1226 	
       
  1227 	// Draw vertical lines
       
  1228 	qreal colWidth = contentWidth / KCalenDaysInWeek;
       
  1229 	endPoint = QPointF(startPoint.x(), 
       
  1230 	                   startPoint.y() + contentHeight);
       
  1231 	for (int i = 1; i < KCalenDaysInWeek; i++) {
       
  1232 		pointList.append(QPointF(startPoint.x() + (i * colWidth), 
       
  1233 		                         startPoint.y()));
       
  1234 		pointList.append(QPointF(endPoint.x() + (i * colWidth), endPoint.y()));
       
  1235 	}
       
  1236 	
       
  1237 	// Draw the lines for the points in the vector list
       
  1238 	painter->drawLines(pointList);
       
  1239 	
       
  1240 	// Set the old pen back
       
  1241 	painter->setPen(oldPen);
       
  1242 	
       
  1243 	OstTraceFunctionExit0( CALENMONTHGRID_PAINT_EXIT );
       
  1244 }
       
  1245 
       
  1246 /*!
       
  1247  Slot to handle the change in theme
       
  1248  */
       
  1249 void CalenMonthGrid::handleThemeChange()
       
  1250 {
       
  1251     OstTraceFunctionEntry0(CALENMONTHGRID_HANDLETHEMECHANGE_ENTRY);
       
  1252     
       
  1253     mGridLineColor = HbColorScheme::color("qtc_cal_grid_line");
       
  1254     
       
  1255     OstTraceFunctionExit0(CALENMONTHGRID_HANDLETHEMECHANGE_EXIT);
       
  1256 }
       
  1257 
       
  1258 // End of File
   211 // End of File