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