calendarui/views/src/calenmonthgrid.cpp
changeset 45 b6db4fd4947b
parent 23 fd30d51f876b
child 51 0b38fc5b94c6
equal deleted inserted replaced
23:fd30d51f876b 45:b6db4fd4947b
    14  * Description: Definition file for class CalenMonthGrid.
    14  * Description: Definition file for class CalenMonthGrid.
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 // System includes
    18 // System includes
    19 #include <qtimer.h>
       
    20 #include <hbmenu.h>
       
    21 #include <hbaction.h>
       
    22 #include <hbmainwindow.h>
       
    23 #include <hbgridview.h>
    19 #include <hbgridview.h>
    24 #include <hbabstractviewitem.h>
    20 #include <hbabstractviewitem.h>
    25 #include <hbstyleloader.h>
    21 #include <hbstyleloader.h>
    26 #include <hbcolorscheme.h>
    22 #include <hbcolorscheme.h>
       
    23 #include <hbpangesture.h>
       
    24 #include <hbswipegesture.h>
       
    25 #include <hbdeviceprofile.h>
    27 
    26 
    28 // User includes
    27 // User includes
    29 #include "calenmonthgrid.h"
    28 #include "calenmonthgrid.h"
    30 #include "calengriditemprototype.h"
    29 #include "calengriditemprototype.h"
    31 #include "calenmonthdata.h"
    30 #include "calenmonthdata.h"
    32 #include "calenmonthview.h"
    31 #include "calenmonthview.h"
    33 #include "calendateutils.h"
    32 #include "calendateutils.h"
    34 #include "calencommon.h"
    33 #include "calencommon.h"
       
    34 #include "calenconstants.h"
    35 
    35 
    36 // Constants
    36 // Constants
    37 #define SCROLL_SPEEED 1000 
    37 #define SCROLL_SPEEED 3000 
    38 #define GRIDLINE_WIDTH 0.075 //units
    38 #define GRIDLINE_WIDTH 0.075 //units
       
    39 #define MAX_PAN_DIRECTION_THRESHOLD 50
       
    40 #define MIN_PAN_DIRECTION_THRESHOLD 20
    39 
    41 
    40 /*!
    42 /*!
    41  \class CalenMonthGrid
    43  \class CalenMonthGrid
    42 
    44 
    43  Implements the month grid
    45  Implements the month grid
    51 	mModel(0),
    53 	mModel(0),
    52 	mDirection(invalid),
    54 	mDirection(invalid),
    53 	mIsPanGesture(false),
    55 	mIsPanGesture(false),
    54 	mIsAtomicScroll(true),
    56 	mIsAtomicScroll(true),
    55 	mView(NULL),
    57 	mView(NULL),
    56 	mCurrentRow(0),
    58 	mCurrentRow(-100),
    57 	mIsNonActiveDayFocused(false),
    59 	mIsNonActiveDayFocused(false),
    58 	mIgnoreItemActivated(false),
    60 	mIgnoreItemActivated(false),
    59 	mGridBorderColor(Qt::gray)
    61 	mGridBorderColor(Qt::gray)
    60 {
    62 {
    61 	setScrollDirections(Qt::Vertical);
    63 	setScrollDirections(Qt::Vertical);
    64 	setLongPressEnabled(false);
    66 	setLongPressEnabled(false);
    65 	setItemRecycling(false);
    67 	setItemRecycling(false);
    66 	setSelectionMode(HbGridView::NoSelection);
    68 	setSelectionMode(HbGridView::NoSelection);
    67 	setUniformItemSizes(true);
    69 	setUniformItemSizes(true);
    68 	setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
    70 	setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
    69 	setClampingStyle(HbScrollArea::StrictClamping );
    71 	setClampingStyle(HbScrollArea::StrictClamping);
       
    72 	setEnabledAnimations(HbAbstractItemView::None);
       
    73 	setFrictionEnabled(false);
       
    74 	setFlag(QGraphicsItem::ItemHasNoContents, false);
       
    75 	resetTransform();
    70 	
    76 	
    71 	// Get the content widget of the scroll area to draw the grid lines
    77 	// Get the content widget of the scroll area to draw the grid lines
    72 	mContentWidget = contentWidget();
    78 	mContentWidget = contentWidget();
    73 	
    79 	
    74 	// Get the color of the grid lines
    80 	// Get the all required colors 
       
    81 	// Color of the grid lines
    75 	mGridLineColor = HbColorScheme::color("qtc_cal_grid_line");
    82 	mGridLineColor = HbColorScheme::color("qtc_cal_grid_line");
    76 	
    83 	
    77 	// Create the prototype
    84 	// Get the localised dates well before
    78 	CalenGridItemPrototype* gridItemPrototype = new CalenGridItemPrototype(this);
    85 	// TODO: Need to update the mLocalisedDates when user changes the
    79 	// Create the model
    86 	// phone language keeping calendar application in background
    80 	mModel = new QStandardItemModel(14*KCalenDaysInWeek, 1, this);
    87 	HbExtendedLocale locale = HbExtendedLocale::system();
    81 	// Set the mode and the prototype
    88 	for (int i = 1; i <= 31; i++) {
    82 	setModel(mModel,gridItemPrototype);
    89 		mLocalisedDates.append(locale.toString(i));
    83 	
    90 	}
    84 	// Register the widgetml and css files
    91 	
    85 	HbStyleLoader::registerFilePath(":/");
    92 	// Connect to scrolling finished signal
    86 	
       
    87 	// Set the layout name
       
    88 	setLayoutName("calendarCustomGridItem");
       
    89 	
       
    90 	connect(this, SIGNAL(scrollingEnded()), this,
    93 	connect(this, SIGNAL(scrollingEnded()), this,
    91 			SLOT(scrollingFinished()));
    94 			SLOT(scrollingFinished()));
    92 	
       
    93 	// Connect to item activated signal
       
    94 	connect(this, SIGNAL(activated(const QModelIndex &)), this,
       
    95 				SLOT(itemActivated(const QModelIndex &)));
       
    96 }
    95 }
    97 
    96 
    98 /*!
    97 /*!
    99  Destructor
    98  Destructor
   100  */
    99  */
   101 CalenMonthGrid::~CalenMonthGrid()
   100 CalenMonthGrid::~CalenMonthGrid()
   102 {
   101 {
       
   102 	// Nothing Yet
   103 }
   103 }
   104 
   104 
   105 /*!
   105 /*!
   106  Stores the view pointer
   106  Stores the view pointer
   107  */
   107  */
   112 
   112 
   113 /*!
   113 /*!
   114  Updates the model with the proper dates and sets the required user roles
   114  Updates the model with the proper dates and sets the required user roles
   115  */
   115  */
   116 void CalenMonthGrid::updateMonthGridModel(QList<CalenMonthData> &monthDataArray,
   116 void CalenMonthGrid::updateMonthGridModel(QList<CalenMonthData> &monthDataArray,
   117                         int indexToBeScrolled)
   117                         int indexToBeScrolled, bool isFirstTime)
   118 {
   118 {
   119 	// Check the counts
   119 	int loopStart = 0;
   120 	int dataCount = monthDataArray.count();
   120 	int loopEnd = monthDataArray.count();
   121 	int rowCount = mModel->rowCount();
   121 	if (isFirstTime) {
   122 	int countDiff = dataCount - rowCount;
   122 		// Create the model with only 42 items as visible to the user
   123 	if (countDiff < 0) {
   123 		mModel = new QStandardItemModel(KCalenDaysInWeek * KNumOfVisibleRows, 
   124 		// Delete extra rows in the model
   124 		                                1, this);
   125 		mModel->removeRows(dataCount,abs(countDiff));
   125 		loopStart = (mView->rowsInPrevMonth()) * KCalenDaysInWeek;
   126 	} else if (countDiff > 0) {
   126 		loopEnd = loopStart + (KCalenDaysInWeek * KNumOfVisibleRows);
   127 		// Add the necessary number of rows
   127 	} else {
   128 		mModel->insertRows(rowCount,countDiff);
   128 		// Block the signals generated by model, this is being done as
   129 	}
   129 		// we want to avoid the overload of view listening to signals
       
   130 		mModel->blockSignals(true);
       
   131 		
       
   132 		// Check the counts
       
   133 		int dataCount = monthDataArray.count();
       
   134 		int rowCount = mModel->rowCount();
       
   135 		int countDiff = dataCount - rowCount;
       
   136 		if (countDiff < 0) {
       
   137 			// Delete extra rows in the model
       
   138 			mModel->removeRows(dataCount,abs(countDiff));
       
   139 		} else if (countDiff > 0) {
       
   140 			// Add the necessary number of rows
       
   141 			mModel->insertRows(rowCount,countDiff);
       
   142 		}
       
   143 		loopEnd = dataCount;
       
   144 	}
       
   145 	
   130 	QDateTime currDate = mView->getCurrentDay();
   146 	QDateTime currDate = mView->getCurrentDay();
   131 	QDateTime currDateTime = CalenDateUtils::beginningOfDay(currDate);
   147 	QDateTime currDateTime = CalenDateUtils::beginningOfDay(currDate);
   132 	QDateTime activeDay = mView->getActiveDay();
   148 	QDateTime activeDay = mView->getActiveDay();
   133 	QDateTime activeDateTime = CalenDateUtils::beginningOfDay(activeDay);
   149 	QDateTime activeDateTime = CalenDateUtils::beginningOfDay(activeDay);
   134 	
   150 	
   135 	QModelIndex currentIndex;
   151 	QModelIndex currentIndex;
   136 	
   152 	int modelIndex = 0;
   137 	// Get the default text color to be set
   153 	for (int i = loopStart; i < loopEnd; i++) {
   138 	QColor textColor = HbColorScheme::color("qtc_cal_month_notactive_dates");
       
   139 	HbExtendedLocale locale = HbExtendedLocale::system();
       
   140 	for (int i = 0; i < dataCount; i++) {
       
   141 		QDateTime dateTime = monthDataArray[i].Day();
   154 		QDateTime dateTime = monthDataArray[i].Day();
   142 		currentIndex = mModel->index(i, 0);
   155 		currentIndex = mModel->index(modelIndex++, 0);
   143 		// Get the localised string for the day
       
   144 		QString date = locale.toString(dateTime.date().day());
       
   145 		
   156 		
   146 		// Create the variant list to contain the date to depict a grid item
   157 		// Create the variant list to contain the date to depict a grid item
   147 		QVariantList itemData;
   158 		QVariantList itemData;
   148 		
   159 		
   149 		// NOTE: Add the data in the order mentioned in the 
   160 		// !!!NOTE!!!: Add the data in the order mentioned in the 
   150 		// CalendarNamespace::DataRole enum. Dont change the order.
   161 		// CalendarNamespace::DataRole enum. Dont change the order.
   151 		itemData << date; 
   162 		itemData << mLocalisedDates.at(dateTime.date().day()-1); 
   152 		
   163 		
   153 		// Check for active day
   164 		// Check for active day
   154 		if (activeDateTime == CalenDateUtils::beginningOfDay(dateTime)) {
   165 		if (activeDateTime == CalenDateUtils::beginningOfDay(dateTime)) {
   155 			mCurrentRow = currentIndex.row();
   166 			mCurrentRow = i;
   156 			// Set the focus icon
   167 			// Set the focus attribute to true
   157 			itemData << QString("qtg_fr_cal_focused_day_ind");
   168 			itemData << true;
   158 		} else {
   169 		} else {
   159 			// reset the highlight
   170 			// reset the highlight
   160 			itemData << QString("");
   171 			itemData << false;
   161 		}
   172 		}
   162 
   173 
   163 		// Check for current day
   174 		// Check for current day
   164 		if (currDateTime == CalenDateUtils::beginningOfDay(dateTime)) {
   175 		if (currDateTime == CalenDateUtils::beginningOfDay(dateTime)) {
   165 			// Set the underline icon
   176 			// Set the underline attribute to true
   166 			itemData << true;
   177 			itemData << true;
   167 		} else {			
   178 		} else {			
   168 			itemData << false;
   179 			itemData << false;
   169 		}
   180 		}
   170 
   181 
   171 		// Check for events
   182 		// Check for events
   172 		if (monthDataArray[i].HasEvents()) {
   183 		if (monthDataArray[i].HasEvents()) {
   173 			// Set the underline icon
   184 			// Set the event indicator attribute
   174 			itemData << QString("qtg_graf_cal_event_ind");
   185 			itemData << true;
   175 		} else {
   186 		} else {
   176 			itemData << QString("");
   187 			itemData << false;
   177 		}
   188 		}
   178 		
   189 		
   179 		// Add default text color
   190 		// Add default text color
   180 		itemData << textColor;
   191 		if (monthDataArray[i].isActive()) {
       
   192 			itemData << true;
       
   193 		} else {
       
   194 			itemData << false;
       
   195 		}
   181 		mModel->itemFromIndex(currentIndex)->setData(itemData);
   196 		mModel->itemFromIndex(currentIndex)->setData(itemData);
   182 	}
   197 	}
       
   198 	
       
   199 	if (isFirstTime) {
       
   200 		// Color of the today indicator
       
   201 		QColor todayIndColor = HbColorScheme::color("qtc_cal_month_current_day");
       
   202 		// Color of the active dates
       
   203 		QColor mActiveTextColor = 
       
   204 								HbColorScheme::color("qtc_cal_month_active_dates");
       
   205 		// Color of the inactive dates
       
   206 		QColor mInActiveTextColor = 
       
   207 							HbColorScheme::color("qtc_cal_month_notactive_dates");
       
   208 		
       
   209 		// Create the prototype
       
   210 		CalenGridItemPrototype* gridItemPrototype = new CalenGridItemPrototype(
       
   211 						todayIndColor, mActiveTextColor, mInActiveTextColor, this);
       
   212 		
       
   213 		// Set the mode and the prototype
       
   214 		setModel(mModel,gridItemPrototype);
       
   215 		
       
   216 		// Register the widgetml and css files
       
   217 		HbStyleLoader::registerFilePath(":/");
       
   218 		
       
   219 		// Set the layout name
       
   220 		setLayoutName("calendarCustomGridItem");
       
   221 	} else {
       
   222 		// Since, we have finished setData, Now unblock the signals
       
   223 		mModel->blockSignals(false);
       
   224 		
       
   225 		// Since till now, we had blocked signals being generated frm the mode
       
   226 		// view will be unaware of the items that we added. Hence, inform the view
       
   227 		// explicitly in one shot
       
   228 		QModelIndex leftIndex = mModel->index(0, 0);
       
   229 		QModelIndex rightIndex = mModel->index(loopEnd-1, 0);
       
   230 		dataChanged(leftIndex, rightIndex);
       
   231 		
       
   232 		// NOTE: To make sure that we always display proper month,
       
   233 		// two calls have been  made to scrollTo(), one with top
       
   234 		// visible item and other with bottom visible item
       
   235 		// Calculate the first visible item in the grid
       
   236 		QModelIndex firstVisibleIndex = mModel->index(indexToBeScrolled - 
       
   237 								(KNumOfVisibleRows * KCalenDaysInWeek - 1), 0);
       
   238 		scrollTo(firstVisibleIndex);
       
   239 		
       
   240 		
       
   241 		// Calculate the last visible item in the grid
       
   242 		QModelIndex lastVisibleIndex = mModel->index(indexToBeScrolled, 0);
       
   243 		scrollTo(lastVisibleIndex);
       
   244 	}
   183 	mMonthDataArray = monthDataArray;
   245 	mMonthDataArray = monthDataArray;
   184 	
   246 }
   185 	// Get the active month
   247 
   186 	QDateTime activeDate = mView->getActiveDay();
   248 /*!
   187 	// Set the text color properly
   249 	Updates the view with jprevious month dates when calendar is opened for the 
   188 	setActiveDates(activeDate.date());
   250 	first time to improve the opening time
   189 	
   251  */
   190 	// NOTE: To make sure that we always display proper month,
   252 void CalenMonthGrid::updateMonthGridWithInActiveMonths(
   191 	// two calls have been  made to scrollTo(), one with top
   253 										QList<CalenMonthData> &monthDataArray)
   192 	// visible item and other with bottom visible item
   254 {	
   193     // Calculate the first visible item in the grid
   255 	mMonthDataArray = monthDataArray;
   194     QModelIndex firstVisibleIndex = mModel->index(indexToBeScrolled - 
   256 		
   195                                         (KNumOfVisibleRows * KCalenDaysInWeek - 1), 0);
   257 	// Prepend the required rows
   196     scrollTo(firstVisibleIndex);
   258 	handlePrependingRows(monthDataArray);
   197     
   259 	
   198     
   260 	// Append the required rows
   199     // Calculate the last visible item in the grid
   261 	handleAppendingRows(monthDataArray);
   200     QModelIndex lastVisibleIndex = mModel->index(indexToBeScrolled, 0);
   262 	
   201     scrollTo(lastVisibleIndex);
   263 	int rowsInPrevMonth = mView->rowsInPrevMonth();
   202 }
   264 	
   203 
   265 	// Calculate the proper index to be scrolled to
   204 /*!
   266 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
   205  Listens for down gesture
   267 	QModelIndex indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   206  */
   268 	mIsAtomicScroll = true;
   207 void CalenMonthGrid::downGesture (int value)
   269 	scrollTo(indexToBeScrolled);
   208 {
   270 		
   209 	Q_UNUSED(value)	
   271 	// Scroll to proper index
   210 	mDirection = down;
   272 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
   211 	// Before we start scrolling, setthe active text color to previous month
   273 								   KCalenDaysInWeek) - 1;
   212 	QDateTime activeDate = mView->getActiveDay();
   274 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   213 	setActiveDates(activeDate.addMonths(-1).date());
   275 	mIsAtomicScroll = true;
   214 	HbScrollArea::downGesture(SCROLL_SPEEED);
   276 	scrollTo(indexToBeScrolled);
   215 }
   277 	
   216 
   278 	// Update the sart position of the content widget
   217 /*!
   279 	mStartPos = mContentWidget->pos();
   218  Listens for Up gesture
   280 	
   219  */
   281 	// Now connect to the signal which gets emitted when any item on the grid 
   220 void CalenMonthGrid::upGesture (int value)
   282 	// is tapped.
   221 {
   283 	connect(this, SIGNAL(activated(const QModelIndex &)), this,
   222 	Q_UNUSED(value)	
   284 						SLOT(itemActivated(const QModelIndex &)));
   223 	mDirection = up;
   285 }
   224 	// Before we start scrolling, setthe active text color to future month
   286 
   225 	QDateTime activeDate = mView->getActiveDay();
   287 /*!
   226 	setActiveDates(activeDate.addMonths(1).date());
   288 	Updates the view with just event indicators
   227 	HbScrollArea::upGesture(SCROLL_SPEEED);
   289  */
       
   290 void CalenMonthGrid::updateMonthGridWithEventIndicators(
       
   291 										QList<CalenMonthData> &monthDataArray)
       
   292 {
       
   293 	mMonthDataArray = monthDataArray;
       
   294 	for(int i = 0; i < monthDataArray.count(); i++) {
       
   295 		// Check if the day has events
       
   296 		if (monthDataArray[i].HasEvents()) {
       
   297 			QModelIndex itemIndex = mModel->index(i,0);
       
   298 			QVariant itemData = itemIndex.data(Qt::UserRole + 1);
       
   299 			QVariantList list = itemData.toList();
       
   300 			list.replace(CalendarNamespace::CalendarMonthEventRole, true);
       
   301 			mModel->itemFromIndex(itemIndex)->setData(list);
       
   302 		}
       
   303 	}
       
   304 }
       
   305 
       
   306 /*!
       
   307  Scrolls the content dowmwards
       
   308  */
       
   309 void CalenMonthGrid::downGesture()
       
   310 {
       
   311     // Make sure that content widget is properly placed
       
   312     if (mIsNonActiveDayFocused) {
       
   313         mIsAtomicScroll = true;
       
   314         int itemToBeScrolled = mView->rowsInPrevMonth() * KCalenDaysInWeek;
       
   315         QModelIndex indexToBeScrolled  = mModel->index(itemToBeScrolled, 0);
       
   316         scrollTo(indexToBeScrolled);
       
   317     }
       
   318     mDirection = down;
       
   319     mIsAtomicScroll = false;
       
   320     setAttribute(Hb::InteractionDisabled);
       
   321     QPointF targetPos(0.0, 0.0);
       
   322     scrollContentsTo(targetPos,500);
       
   323 }
       
   324 
       
   325 /*!
       
   326  Scrolls the content upwards
       
   327  */
       
   328 void CalenMonthGrid::upGesture()
       
   329 {
       
   330     // Make sure that content widget is properly placed
       
   331     if (mIsNonActiveDayFocused) {
       
   332         mIsAtomicScroll = true;
       
   333         int itemToBeScrolled = mView->rowsInPrevMonth() * KCalenDaysInWeek;
       
   334         itemToBeScrolled += KNumOfVisibleRows * KCalenDaysInWeek;
       
   335         QModelIndex indexToBeScrolled  = mModel->index(itemToBeScrolled, 0);
       
   336         scrollTo(indexToBeScrolled);
       
   337     }
       
   338     mDirection = up;
       
   339     mIsAtomicScroll = false;
       
   340     setAttribute(Hb::InteractionDisabled);
       
   341     QPointF targetPos(0.0, mStartPos.y() - size().height());
       
   342     scrollContentsTo(-targetPos,500);
       
   343 	
   228 }
   344 }
   229 
   345 
   230 /*!
   346 /*!
   231  Function to listen mouse press events
   347  Function to listen mouse press events
   232  */
   348  */
   233 void CalenMonthGrid::mousePressEvent(QGraphicsSceneMouseEvent* event)
   349 void CalenMonthGrid::mousePressEvent(QGraphicsSceneMouseEvent* event)
   234 {
   350 {
   235 	mPressedPos = event->pos();
       
   236 	// Pass it to parent
   351 	// Pass it to parent
   237 	HbGridView::mousePressEvent(event);
   352 	HbGridView::mousePressEvent(event);
   238 }
   353 }
   239 
   354 
   240 /*!
   355 /*!
   241  Function to listen mouse release events
   356  Function to listen mouse release events
   242  */
   357  */
   243 void CalenMonthGrid::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
   358 void CalenMonthGrid::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
   244 {
   359 {
   245 	int posDiff = mPressedPos.y() - event->pos().y();
   360 	// Pass it grid view if pan gesture is not in progress else pass it to
   246 	if (posDiff < -50) {
   361 	// scrollarea. Problem here is, if we pass to gridview when panning, then 
   247 		mDirection = down;
   362 	// its emitting item activated signal simply becasue of which focus item
   248 	} else if (posDiff > 50){
   363 	// is getting changed when you finish the pan / shake
   249 		mDirection = up;
       
   250 	}
       
   251 	// Pass it to parent
       
   252 	HbGridView::mouseReleaseEvent(event);
       
   253 }
       
   254 
       
   255 /*!
       
   256  Listens for pan gesture
       
   257  */
       
   258 void  CalenMonthGrid::panGesture(const QPointF &  delta)
       
   259 {
       
   260 	if (!mIsPanGesture) {
   364 	if (!mIsPanGesture) {
   261 		mIsPanGesture = true;
   365 		HbGridView::mouseReleaseEvent(event);
   262 		mIgnoreItemActivated = true;
   366 	} else {
   263 		
   367 		HbScrollArea::mouseReleaseEvent(event);
   264 		// Get to know the direction of the gesture
   368 	}
   265 		if (delta.y() > 0) {
   369 }
   266 			mDirection = down;
   370 
   267 		} else {
   371 /*!
   268 			mDirection = up;
   372     Function to list for all the gesture events
   269 		}
   373  */
   270 	} else { // This case is when user changes the direction while panning
   374 void CalenMonthGrid::gestureEvent(QGestureEvent *event)
   271 		// without lifting the finger
   375 {
   272 		// Check if direction has changed
   376    if(HbPanGesture *gesture = qobject_cast<HbPanGesture *>(event->gesture(Qt::PanGesture))) {
   273 		if (((delta.y() > 0) && (mDirection == up))
   377         if (gesture->state() == Qt::GestureStarted) {
   274 			|| ((delta.y() < 0) && (mDirection == down))) {
   378             setAttribute(Hb::InteractionDisabled);
   275 			// Direction has changed, ignore this pan
   379             mIsAtomicScroll = false;
   276 			return;
   380             if (!mIsPanGesture) {
   277 		}
   381                 mDirection = invalid;
   278 	}
   382                 mStartPos = mContentWidget->pos();
   279 	// Call the parent class to perform the pan gesture
   383                 // TODO: This work aroung till framework provides an api
   280 	// When scrolling finished, month grid will adjust to show the proper month
   384                 // to know the direciton of the pan, until then we need
   281 	HbScrollArea::panGesture(delta);
   385                 // calculate the direction explicitly
       
   386                 // Get to know the direction of the gesture
       
   387                 // Use our defined threshold temporarily till scrollarea 
       
   388                 // frm orbit side is made clever enough not to scroll in other direction
       
   389                 // apart frm the registered scroll direction
       
   390                 QPointF delta = gesture->delta();
       
   391                 if (abs(delta.x()) > MAX_PAN_DIRECTION_THRESHOLD) {
       
   392                     // Now see if y coord diff has crossed threshold
       
   393                     if (delta.y() > MAX_PAN_DIRECTION_THRESHOLD) {
       
   394                         mIsPanGesture = true;
       
   395                         mIgnoreItemActivated = true;
       
   396                         mDirection = down;
       
   397                     } else if (delta.y() < -MAX_PAN_DIRECTION_THRESHOLD){
       
   398                         mIsPanGesture = true;
       
   399                         mIgnoreItemActivated = true;
       
   400                         mDirection = up;
       
   401                     } else {
       
   402                         event->accept(Qt::PanGesture);
       
   403                         return;
       
   404                     }
       
   405                 } else if (abs(delta.x()) < MAX_PAN_DIRECTION_THRESHOLD) {
       
   406                    if (delta.y() > MIN_PAN_DIRECTION_THRESHOLD) {
       
   407                         mIsPanGesture = true;
       
   408                         mIgnoreItemActivated = true;
       
   409                         mDirection = down;
       
   410                    } else if (delta.y() < -MIN_PAN_DIRECTION_THRESHOLD){
       
   411                         mIsPanGesture = true;
       
   412                         mIgnoreItemActivated = true;
       
   413                         mDirection = up;
       
   414                    }else {
       
   415                        event->accept(Qt::PanGesture);
       
   416                        return;
       
   417                    }
       
   418                 } 
       
   419             }
       
   420         }
       
   421     } else if(HbSwipeGesture *gesture = qobject_cast<HbSwipeGesture *>(event->gesture(Qt::SwipeGesture))) {
       
   422         if (gesture->state() == Qt::GestureStarted) {
       
   423             setAttribute(Hb::InteractionDisabled);
       
   424             mIsAtomicScroll = false;
       
   425             mDirection = invalid;
       
   426             if (gesture->sceneVerticalDirection() == QSwipeGesture::Down) {
       
   427                 mDirection = down;
       
   428             } else if (gesture->sceneVerticalDirection() == QSwipeGesture::Up) {
       
   429                 mDirection = up;
       
   430             } else {
       
   431                 event->accept(Qt::SwipeGesture);
       
   432                 return;
       
   433             }
       
   434         }
       
   435     }
       
   436    
       
   437    if (mDirection!= invalid) {
       
   438         // Call the parent class to perform the pan gesture
       
   439         // When scrolling finished, month grid will adjust to show the proper month
       
   440         HbScrollArea::gestureEvent(event);
       
   441    }
   282 }
   442 }
   283 
   443 
   284 /*!
   444 /*!
   285  Gets called when scrolling finishes to update the model
   445  Gets called when scrolling finishes to update the model
   286  */
   446  */
   287 void CalenMonthGrid::scrollingFinished()
   447 void CalenMonthGrid::scrollingFinished()
   288 {
   448 {
   289 	
       
   290 	if (mIsPanGesture) {
   449 	if (mIsPanGesture) {
   291 		mIsPanGesture = false;
   450 		handlePanGestureFinished();
   292 		if (mDirection == up) {
       
   293 			// Make a request for upgesture
       
   294 			upGesture(SCROLL_SPEEED);
       
   295 			return; // return immediately
       
   296 		} else if (mDirection == down) {
       
   297 			// Make a request for upgesture
       
   298 			downGesture(SCROLL_SPEEED);
       
   299 			return; // return immediately
       
   300 		}
       
   301 	} else if(!mIsAtomicScroll) {
   451 	} else if(!mIsAtomicScroll) {
   302 		// Before we do anything, set the focus to proper date
   452 		QDateTime activeDate = mView->getActiveDay();
   303 		// Set it only when non active day was focussed. When inactive day
   453 		if(mDirection == down) { // down gesture
   304 		// was focussed, we need to focus the same day
   454 			if (!mIsNonActiveDayFocused) {
   305 		if (!mIsNonActiveDayFocused) {
   455 				setActiveDates(activeDate.addMonths(-1).date());
   306 			setFocusToProperDay();
   456 			}
   307 		}
   457 			prependRows();
   308 		// To improve the performance, lets start the timer for 10 ms, 
   458 		} else if (mDirection == up) { //up gesture
   309 		// return immediately and do the other things after that
   459 			if (!mIsNonActiveDayFocused) {
   310 		QTimer::singleShot(10, this, SLOT(timerExpired()));
   460 				setActiveDates(activeDate.addMonths(1).date());
       
   461 			}
       
   462 			appendRows();
       
   463 		}
       
   464 		mDirection = invalid;
   311 	} else {
   465 	} else {
   312         mIsAtomicScroll = false;
   466         mIsAtomicScroll = false;
       
   467         mDirection = invalid;
   313 	}
   468 	}
   314 	mIgnoreItemActivated = false;
   469 	mIgnoreItemActivated = false;
   315 }
   470 	setAttribute(Hb::InteractionDisabled, false);
   316 
   471 }
   317 void CalenMonthGrid::timerExpired()
   472 
   318 {
   473 /*!
   319 	if(mDirection == down) { // down gesture
   474  Function to handle completion of pan gesture
   320 		prependRows();
   475  */
   321 	} else if (mDirection == up) { //up gesture
   476 void CalenMonthGrid::handlePanGestureFinished()
   322 		appendRows();
   477 {
   323 	}
   478 	mIsPanGesture = false;
   324 	mDirection = invalid;
   479 	// Get the first item that is visible
       
   480 	QList<HbAbstractViewItem *> list = visibleItems();
       
   481 	HbAbstractViewItem* item = list[0];
       
   482 	QModelIndex modelIndex = item->modelIndex();
       
   483 	
       
   484 	// Get the date which is visible at the above row
       
   485 	QDateTime date = mMonthDataArray[modelIndex.row()].Day();
       
   486 	
       
   487 	// Check if this date belong to current active month or 
       
   488 	// previous month else future month
       
   489 	QDateTime activeMonth = mView->getActiveDay();
       
   490 	QDateTime prevMonth = activeMonth.addMonths(-1);
       
   491 	QDateTime nextMonth = activeMonth.addMonths(1);
       
   492 	int month = date.date().month();
       
   493 	if (month == activeMonth.date().month()) {
       
   494 		// Then pan is completed on current month
       
   495 		// Check if the date is more than half of the current month or it is
       
   496 		// more than or equal to half of the future month
       
   497 		if (date.date().day() > (activeMonth.date().daysInMonth()) / 2 ||
       
   498 				date.addDays(KNumOfVisibleRows*KCalenDaysInWeek).date().day() >=
       
   499 				(prevMonth.date().daysInMonth()) / 2) {
       
   500 			// up gesture to bring the next month
       
   501 			upGesture();
       
   502 		} else {
       
   503 			// we should again show the current month by scrolling downwards
       
   504 			mDirection = down;
       
   505 			mIsAtomicScroll = true;
       
   506 			setAttribute(Hb::InteractionDisabled);
       
   507 			scrollContentsTo(-mStartPos,500);
       
   508 		}
       
   509 	} else if (month == prevMonth.date().month()) {
       
   510 		// first visible item belongs to previous month
       
   511 		// Check if the date is more than half of the previous month
       
   512 		if (date.date().day() > (prevMonth.date().daysInMonth()) / 2) {
       
   513 			// we should again show the current month by scrolling upwards
       
   514 			mDirection = up;
       
   515 			mIsAtomicScroll = true;
       
   516 			setAttribute(Hb::InteractionDisabled);
       
   517 			scrollContentsTo(-mStartPos,500);
       
   518 		} else {
       
   519 			// down gesture to show the previous month
       
   520 			downGesture();
       
   521 		}
       
   522 	} else if (month == prevMonth.addMonths(-1).date().month()) {
       
   523 		// first visible date belong to previous to previous month
       
   524 		// hence, scroll down to bring the previous month
       
   525 		downGesture();
       
   526 	} else if (month == nextMonth.date().month()) {
       
   527 		// first visible item belongs to next month
       
   528 		// Check if the date is more than half of the next month
       
   529 		if (date.date().day() > (nextMonth.date().daysInMonth()) / 2) {
       
   530 			// up gesture to bring the next month
       
   531 			upGesture();
       
   532 		} else {
       
   533 			// we should again show the current month by scrolling upwards
       
   534 			mDirection = invalid;
       
   535 			setAttribute(Hb::InteractionDisabled);
       
   536 			scrollContentsTo(-mStartPos,500);
       
   537 		}
       
   538 	} else if (month == nextMonth.addMonths(1).date().month()) {
       
   539 		// first visible date belongs to next to next month
       
   540 		// hence, scroll up to show the next month
       
   541 		upGesture();
       
   542 	}
   325 }
   543 }
   326 
   544 
   327 /*!
   545 /*!
   328  Called when down gesture is performed. Adds the new previous month details
   546  Called when down gesture is performed. Adds the new previous month details
   329  to the model
   547  to the model
   330  */
   548  */
   331 void CalenMonthGrid::prependRows()
   549 void CalenMonthGrid::prependRows()
   332 {
   550 {
       
   551 	// Before we do anything, set the focus to proper date
       
   552 	// Set it only when non active day was focussed. When inactive day
       
   553 	// was focussed, we need to focus the same day
       
   554 	if (!mIsNonActiveDayFocused) {
       
   555 		setFocusToProperDay();
       
   556 	}
       
   557 	
       
   558 	// Block the signals generated by model, this is being done as
       
   559 	// we want to avoid the overload of view listening to signals
       
   560 	mModel->blockSignals(true);
       
   561 		
   333 	mIsNonActiveDayFocused = false;
   562 	mIsNonActiveDayFocused = false;
   334 	QDateTime currDate = mView->getCurrentDay();
   563 	
   335 	QDateTime currDateTime = CalenDateUtils::beginningOfDay( currDate );
       
   336 	int rowsInFutMonthEarlier = mView->rowsInFutMonth();
   564 	int rowsInFutMonthEarlier = mView->rowsInFutMonth();
   337 	int rowsInPrevMonthEarlier = mView->rowsInPrevMonth();
   565 	int rowsInPrevMonthEarlier = mView->rowsInPrevMonth();
       
   566 	
       
   567 	// remove the cells in the future month
       
   568 	int deleteFromIndex = (rowsInPrevMonthEarlier + KNumOfVisibleRows) * KCalenDaysInWeek;
       
   569 	int numOfIndices = rowsInFutMonthEarlier * KCalenDaysInWeek;
   338 	
   570 	
   339 	// Get the updated dates from the view
   571 	// Get the updated dates from the view
   340 	mView->updateModelWithPrevMonth();
   572 	mView->updateModelWithPrevMonth();
   341 	QList<CalenMonthData > monthDataList = mView->monthDataList();
   573 	QList<CalenMonthData > monthDataList = mView->monthDataList();
   342 	mMonthDataArray = monthDataList;
   574 	mMonthDataArray = monthDataList;
   343 	int listCount = monthDataList.count();
   575 	
   344 	// Get the updated rows to be inserted
   576 	// Prepend the required rows
       
   577 	handlePrependingRows(monthDataList);
       
   578 		
       
   579 	// Since, we have finished setData, Now unblock the signals
       
   580 	mModel->blockSignals(false);
       
   581 	
   345 	int rowsInPrevMonth = mView->rowsInPrevMonth();
   582 	int rowsInPrevMonth = mView->rowsInPrevMonth();
   346 	int rowsInFutMonth = mView->rowsInFutMonth();
   583 	int countToBeAdded = rowsInPrevMonth * KCalenDaysInWeek;
   347 	
   584 		
   348 	// remove the cells in the future month
   585 	// Since till now, we had blocked signals being generated frm the model
   349 	int deleteFromIndex = (rowsInPrevMonthEarlier + KNumOfVisibleRows) * KCalenDaysInWeek;
   586 	// view will be unaware of the items that we added. Hence, inform the view
   350 	int numOfIndices = rowsInFutMonthEarlier * KCalenDaysInWeek;
   587 	// explicitly in one shot
   351 	int count = mModel->rowCount();
   588 	QModelIndex leftIndex = mModel->index(0, 0);
   352 	
   589 	QModelIndex rightIndex = mModel->index(countToBeAdded-1, 0);
   353 	count = mModel->rowCount();
   590 	dataChanged(leftIndex, rightIndex);
   354 	
   591 		
       
   592 	// Now remove the necessary items frm the model
       
   593 	mModel->removeRows(deleteFromIndex+countToBeAdded, numOfIndices);
       
   594 	mIsAtomicScroll = true;
       
   595 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
   596 	QModelIndex indexToBeScrolled  = mModel->index(itemToBeScrolled, 0);
       
   597 	scrollTo(indexToBeScrolled);
       
   598 	
       
   599 	// Scroll to proper index
       
   600 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
   601 								   KCalenDaysInWeek) - 1;
       
   602 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   603 	mIsAtomicScroll = true;
       
   604 	scrollTo(indexToBeScrolled);
       
   605 	// Update the mCurrentRow
       
   606 	mCurrentRow += countToBeAdded;
       
   607 	
       
   608 	// Update the sart position of the content widget
       
   609 	mStartPos = mContentWidget->pos();
       
   610 }
       
   611 
       
   612 /*!
       
   613 	Helper function that prepends the required rows to the model
       
   614  */
       
   615 void CalenMonthGrid::handlePrependingRows(QList<CalenMonthData > &monthDataList)
       
   616 {
       
   617 	QDateTime currDate = mView->getCurrentDay();
       
   618 	QDateTime currDateTime = CalenDateUtils::beginningOfDay( currDate );
       
   619 	int rowsInPrevMonth = mView->rowsInPrevMonth();
   355 	// Add the new days
   620 	// Add the new days
   356 	int countToBeAdded = rowsInPrevMonth * KCalenDaysInWeek;
   621 	int countToBeAdded = rowsInPrevMonth * KCalenDaysInWeek;
   357 	
   622 	
   358 	mModel->insertRows(0, countToBeAdded);
   623 	mModel->insertRows(0, countToBeAdded);
   359 	count = mModel->rowCount();
       
   360 	
       
   361 	// Get the default text color to be set
       
   362 	QColor textColor = HbColorScheme::color("qtc_cal_month_notactive_dates");
       
   363 	HbExtendedLocale locale = HbExtendedLocale::system();
       
   364 	
   624 	
   365 	for (int i = 0; i < countToBeAdded; i++) {
   625 	for (int i = 0; i < countToBeAdded; i++) {
   366 		QDateTime dateTime = monthDataList[i].Day();
   626 		QDateTime dateTime = monthDataList[i].Day();
   367 		
   627 		
   368 		// Get the localised string for the day
   628 		// Get the localised string for the day
   369 		QString date = locale.toString(dateTime.date().day());
       
   370 		QModelIndex currentIndex = mModel->index(i, 0);
   629 		QModelIndex currentIndex = mModel->index(i, 0);
   371 		
   630 		
   372 		// Create the variant list to contain the date to depict a grid item
   631 		// Create the variant list to contain the date to depict a grid item
   373 		QVariantList itemData;
   632 		QVariantList itemData;
   374 		
   633 		
   375 		// NOTE: Add the data in the order mentioned in the 
   634 		// NOTE: Add the data in the order mentioned in the 
   376 		// CalendarNamespace::DataRole enum. Dont change the order.
   635 		// CalendarNamespace::DataRole enum. Dont change the order.
   377 		itemData << date;
   636 		itemData << mLocalisedDates.at(dateTime.date().day()-1);;
   378 				
   637 				
   379 		// Diable the focus role
   638 		// Disable the focus role
   380 		itemData << QString("");
   639 		itemData << false;
   381 		
   640 		
   382 		// Check for current day
   641 		// Check for current day
   383 		if  (currDateTime == CalenDateUtils::beginningOfDay( dateTime )) {
   642 		if  (currDateTime == CalenDateUtils::beginningOfDay( dateTime )) {
   384 			// Set the underline icon
   643 			// Set the underline icon attribute
   385 			itemData << true;
   644 			itemData << true;
   386 		} else {
   645 		} else {
   387 			itemData << false;
   646 			itemData << false;
   388 		}
   647 		}
   389 		
   648 		
   390 		// Update the event indicators
   649 		// Update the event indicators
   391 		if (monthDataList[i].HasEvents()) {
   650 		if (monthDataList[i].HasEvents()) {
   392 			// Set the event indicator icon
   651 			// Set the event indicator attribute
   393 			itemData << QString("qtg_graf_cal_event_ind");
   652 			itemData << true;
   394 		} else {
   653 		} else {
   395 			itemData << QString("");
   654 			itemData << false;
   396 		}
   655 		}
   397 		
   656 		
   398 		// Add default text color
   657 		// Add default text color
   399 		
   658 		itemData << false;
   400 		itemData << textColor;
       
   401 		
   659 		
   402 		// Set the data to model
   660 		// Set the data to model
   403 		mModel->itemFromIndex(currentIndex)->setData(itemData);
   661 		mModel->itemFromIndex(currentIndex)->setData(itemData);
   404 	}
   662 	}
   405 	
       
   406 	// Update the mCurrentRow
       
   407 	mCurrentRow += countToBeAdded;
       
   408 	// Scroll to proper index
       
   409 	int itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
   410 								   KCalenDaysInWeek) - 1;
       
   411 	QModelIndex indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   412 	QMap<int, QVariant> data;
       
   413 	data = mModel->itemData(indexToBeScrolled);
       
   414 	QVariant value = data.value(Qt::DisplayRole);
       
   415 	int date = value.toInt();
       
   416 	mIsAtomicScroll = true;
       
   417 	scrollTo(indexToBeScrolled);
       
   418 	
       
   419 	// Now remove the necessary items frm the model
       
   420 	mModel->removeRows(deleteFromIndex+countToBeAdded, numOfIndices);
       
   421 	mIsAtomicScroll = true;
       
   422 	itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
   423 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   424 	scrollTo(indexToBeScrolled);
       
   425 }
   663 }
   426 
   664 
   427 /*!
   665 /*!
   428  Called when Up gwsture is performed. Adds the new future month details
   666  Called when Up gwsture is performed. Adds the new future month details
   429  to the model
   667  to the model
   430  */
   668  */
   431 void CalenMonthGrid::appendRows()
   669 void CalenMonthGrid::appendRows()
   432 {
   670 {
       
   671 	// Before we do anything, set the focus to proper date
       
   672 	// Set it only when non active day was focussed. When inactive day
       
   673 	// was focussed, we need to focus the same day
       
   674 	if (!mIsNonActiveDayFocused) {
       
   675 		setFocusToProperDay();
       
   676 	}
       
   677 	
       
   678 	// Block the signals generated by model, this is being done as
       
   679 	// we want to avoid the overload of view listening to signals
       
   680 	mModel->blockSignals(true);
       
   681 	
   433 	mIsNonActiveDayFocused = false;
   682 	mIsNonActiveDayFocused = false;
   434 	QDateTime currDate = mView->getCurrentDay();
   683 	
   435 	QDateTime currDateTime = CalenDateUtils::beginningOfDay( currDate );
       
   436 	int rowsInFutMonth = mView->rowsInFutMonth();
   684 	int rowsInFutMonth = mView->rowsInFutMonth();
   437 	int rowsInPrevMonth = mView->rowsInPrevMonth();
   685 	int rowsInPrevMonth = mView->rowsInPrevMonth();
   438 	// remove the cells in the previous month
   686 	// remove the cells in the previous month
   439 	int countToBeDeleted = rowsInPrevMonth * KCalenDaysInWeek;
   687 	int countToBeDeleted = rowsInPrevMonth * KCalenDaysInWeek;
   440 		
   688 		
   441 	// Get the updated dates from the view
   689 	// Get the updated dates from the view
   442 	mView->updateModelWithFutureMonth();
   690 	mView->updateModelWithFutureMonth();
   443 	QList<CalenMonthData > monthDataList = mView->monthDataList();
   691 	QList<CalenMonthData > monthDataList = mView->monthDataList();
   444 	mMonthDataArray = monthDataList;
   692 	mMonthDataArray = monthDataList;
   445 	// Get the updated rows to be inserted
   693 	
   446 	rowsInPrevMonth = mView->rowsInPrevMonth();
   694 	// Get the model count before we add any rows into the mode
   447 	rowsInFutMonth = mView->rowsInFutMonth();
       
   448 	int countToBeAdded = rowsInFutMonth * KCalenDaysInWeek;
       
   449 	int lastVisibleIndex = monthDataList.count() - countToBeAdded;
       
   450 	
       
   451 	int rowCount = mModel->rowCount();
   695 	int rowCount = mModel->rowCount();
   452 	mModel->insertRows(rowCount, countToBeAdded);
   696 	// Append the required rows
   453 	
   697 	handleAppendingRows(monthDataList);
   454 	// Get the default text color to be set
   698 	
   455 	QColor textColor = HbColorScheme::color("qtc_cal_month_notactive_dates");
   699 	// Since, we have finished setData, Now unblock the signals
   456 	for (int i = 0; i < countToBeAdded; i++) {
   700 	mModel->blockSignals(false);
   457 		QMap<int, QVariant> data;
   701 	
   458 		QModelIndex currentIndex = mModel->index(rowCount + i, 0);
   702 	// Since till now, we had blocked signals being generated frm the mode
   459 				
   703 	// view will be unaware of the items that we added. Hence, inform the view
   460 		QDateTime dateTime = monthDataList[lastVisibleIndex + i].Day();
   704 	// explicitly in one shot
   461 		HbExtendedLocale locale = HbExtendedLocale::system();
   705 	QModelIndex leftIndex = mModel->index(rowCount-1, 0);
   462 		// Get the localised string for the day
   706 	QModelIndex rightIndex = mModel->index(mModel->rowCount()-1, 0);
   463 		QString date = locale.toString(dateTime.date().day());
   707 	dataChanged(leftIndex, rightIndex);
   464 		data.insert(CalendarNamespace::CalendarMonthDayRole, date);
   708 		
   465 		
       
   466 		// Create the variant list to contain the date to depict a grid item
       
   467 		QVariantList itemData;
       
   468 		
       
   469 		// NOTE: Add the data in the order mentioned in the 
       
   470 		// CalendarNamespace::DataRole enum. Dont change the order.
       
   471 		itemData << date;
       
   472 		
       
   473 		// Disable the focus role
       
   474 		itemData << QString("");
       
   475 		
       
   476 		// Check for current day
       
   477 		if (currDateTime == CalenDateUtils::beginningOfDay( dateTime )) {
       
   478 			// Set the underline icon
       
   479 			itemData << true;
       
   480 		} else {
       
   481 			itemData << false;
       
   482 		}
       
   483 		
       
   484 		// Update the event indicators
       
   485 		if (monthDataList[lastVisibleIndex + i].HasEvents()) {
       
   486 			// Set the underline icon
       
   487 			itemData << QString("qtg_graf_cal_event_ind");
       
   488 		} else {
       
   489 			itemData << QString("");
       
   490 		}
       
   491 		
       
   492 		// Add default text color
       
   493 		itemData << textColor;
       
   494 				
       
   495 		// Set the data to model
       
   496 		mModel->itemFromIndex(currentIndex)->setData(itemData);
       
   497 	}
       
   498 	
       
   499 	// Update the mCurrentRow
   709 	// Update the mCurrentRow
   500 	mCurrentRow -= (countToBeDeleted);
   710 	mCurrentRow -= (countToBeDeleted);
   501 	for (int i = 0; i < countToBeDeleted; i++) {
   711 	for (int i = 0; i < countToBeDeleted; i++) {
   502 		mModel->removeRow(0);
   712 		mModel->removeRow(0);
   503 	}
   713 	}
       
   714 	
   504 	mIsAtomicScroll = true;
   715 	mIsAtomicScroll = true;
       
   716 	
       
   717 	rowsInFutMonth = mView->rowsInFutMonth();
       
   718 	rowsInPrevMonth = mView->rowsInPrevMonth();
   505 	
   719 	
   506 	// Calculate the proper index to be scrolled to
   720 	// Calculate the proper index to be scrolled to
   507 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
   721 	int itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
   508 	QModelIndex indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   722 	QModelIndex indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   509 	scrollTo(indexToBeScrolled);
   723 	scrollTo(indexToBeScrolled);
       
   724 	
       
   725 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
   726 									   KCalenDaysInWeek) - 1;
       
   727 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
   728 	mIsAtomicScroll = true;
       
   729 	scrollTo(indexToBeScrolled);
       
   730 	
       
   731 	// Update the sart position of the content widget
       
   732     mStartPos = mContentWidget->pos();
       
   733 }
       
   734 
       
   735 /*!
       
   736 	Helper function that appends the required rows to the model
       
   737  */
       
   738 void CalenMonthGrid::handleAppendingRows(QList<CalenMonthData > &monthDataList)
       
   739 {
       
   740 	QDateTime currDate = mView->getCurrentDay();
       
   741 	QDateTime currDateTime = CalenDateUtils::beginningOfDay( currDate );
       
   742 	int rowsInFutMonth = mView->rowsInFutMonth();
       
   743 	int countToBeAdded = rowsInFutMonth * KCalenDaysInWeek;
       
   744 	int lastVisibleIndex = monthDataList.count() - countToBeAdded;
       
   745 	
       
   746 	int rowCount = mModel->rowCount();
       
   747 	mModel->insertRows(rowCount, countToBeAdded);
       
   748 	
       
   749 	for (int i = 0; i < countToBeAdded; i++) {
       
   750 		QModelIndex currentIndex = mModel->index(rowCount + i, 0);
       
   751 				
       
   752 		QDateTime dateTime = monthDataList[lastVisibleIndex + i].Day();
       
   753 		
       
   754 		// Create the variant list to contain the date to depict a grid item
       
   755 		QVariantList itemData;
       
   756 		
       
   757 		// NOTE: Add the data in the order mentioned in the 
       
   758 		// CalendarNamespace::DataRole enum. Dont change the order.
       
   759 		itemData << mLocalisedDates.at(dateTime.date().day()-1);;
       
   760 		
       
   761 		// Disable the focus role
       
   762 		itemData << false;
       
   763 		
       
   764 		// Check for current day
       
   765 		if (currDateTime == CalenDateUtils::beginningOfDay( dateTime )) {
       
   766 			// Set the underline icon attribute
       
   767 			itemData << true;
       
   768 		} else {
       
   769 			itemData << false;
       
   770 		}
       
   771 		
       
   772 		// Update the event indicators
       
   773 		if (monthDataList[lastVisibleIndex + i].HasEvents()) {
       
   774 			// Set the event indicator attribute
       
   775 			itemData << true;
       
   776 		} else {
       
   777 			itemData << false;
       
   778 		}
       
   779 		
       
   780 		// Add default text color
       
   781 		itemData << false;
       
   782 				
       
   783 		// Set the data to model
       
   784 		mModel->itemFromIndex(currentIndex)->setData(itemData);
       
   785 	}
   510 }
   786 }
   511 
   787 
   512 /*!
   788 /*!
   513  Slot to handle when a particular grid item is tapped
   789  Slot to handle when a particular grid item is tapped
   514  */
   790  */
   519 		return;
   795 		return;
   520 	}
   796 	}
   521 	mIsNonActiveDayFocused = false;
   797 	mIsNonActiveDayFocused = false;
   522 	// Check if the same item has been tapped twice
   798 	// Check if the same item has been tapped twice
   523 	if (mCurrentRow == index.row()) {
   799 	if (mCurrentRow == index.row()) {
   524 		// Launch the agenda view
   800 		// Launch the Day view
   525 		mView->launchDayView();
   801 		mView->launchDayView();
   526 	} else {
   802 	} else {
   527 		// Reset the focus attribute to this item		
   803 		// Reset the focus attribute to this item		
   528 		QModelIndex itemIndex = mModel->index(mCurrentRow,0);
   804 		QModelIndex itemIndex = mModel->index(mCurrentRow,0);
       
   805 		if(itemIndex.row() < 0 || itemIndex.row() >= mModel->rowCount() ||
       
   806 				itemIndex.column() < 0 || itemIndex.column() > mModel->columnCount()) {
       
   807 			return;
       
   808 		}
   529 		QVariant itemData = itemIndex.data(Qt::UserRole + 1);
   809 		QVariant itemData = itemIndex.data(Qt::UserRole + 1);
   530 		QVariantList list = itemData.toList();
   810 		QVariantList list = itemData.toList();
   531 		list.replace(CalendarNamespace::CalendarMonthFocusRole, QString(""));
   811 		list.replace(CalendarNamespace::CalendarMonthFocusRole, false);
   532 		mModel->itemFromIndex(itemIndex)->setData(list);
   812 		mModel->itemFromIndex(itemIndex)->setData(list);
   533 		
   813 		
   534 		// Inform view to update the context and preview panes
   814 		// Inform view to update the context and preview panes
   535 		mCurrentRow = index.row();
   815 		mCurrentRow = index.row();
   536 		itemIndex = mModel->index(mCurrentRow,0);
   816 		itemIndex = mModel->index(mCurrentRow,0);
   537 		itemData = itemIndex.data(Qt::UserRole + 1);
   817 		itemData = itemIndex.data(Qt::UserRole + 1);
   538 		list = itemData.toList();
   818 		list = itemData.toList();
   539 		list.replace(CalendarNamespace::CalendarMonthFocusRole, 
   819 		list.replace(CalendarNamespace::CalendarMonthFocusRole, 
   540 						 QString("qtg_fr_cal_focused_day_ind"));
   820 								 true);
   541 		mModel->itemFromIndex(itemIndex)->setData(list);
   821 		mModel->itemFromIndex(itemIndex)->setData(list);
   542 		// Check if inactive date is tapped
   822 		// Check if inactive date is tapped
   543 		QDateTime activeMonth = mView->getActiveDay();
   823 		QDateTime activeMonth = mView->getActiveDay();
   544 		int month = activeMonth.date().month();
   824 		int month = activeMonth.date().month();
   545 		if(month != mMonthDataArray[mCurrentRow].Day().date().month()){
   825 		if(month != mMonthDataArray[mCurrentRow].Day().date().month()){
   546 			// Set the flag
   826 			// Set the flag
   547 			mIsNonActiveDayFocused = true;
   827 			mIsNonActiveDayFocused = true;
   548 			mNonActiveFocusedDay = mMonthDataArray[mCurrentRow].Day();
   828 			mNonActiveFocusedDay = mMonthDataArray[mCurrentRow].Day();
   549 			
   829 			
   550 			// Get the current active month
   830 			// Add one month to active month
   551 			QDateTime activeMonth = mView->getActiveDay();
       
   552 			// Add one month to it
       
   553 			activeMonth = activeMonth.addMonths(1);
   831 			activeMonth = activeMonth.addMonths(1);
   554 			if (activeMonth.date().month() == 
   832 			if (activeMonth.date().month() == 
   555 				mNonActiveFocusedDay.date().month()) {
   833 				mNonActiveFocusedDay.date().month()) {
       
   834 				mDirection = up;
   556 				// up gesture
   835 				// up gesture
   557 				upGesture(SCROLL_SPEEED);
   836 				upGesture();
       
   837 				setActiveDates(activeMonth.date());
   558 			} else {
   838 			} else {
       
   839 				mDirection = down;
   559 				// down gesture
   840 				// down gesture
   560 				downGesture(SCROLL_SPEEED);
   841 				downGesture();
       
   842 				setActiveDates(activeMonth.addMonths(-2).date());
   561 			}
   843 			}
   562 		} 
   844 		} 
   563 		mView->setContextForActiveDay(index.row());
   845 		mView->setContextForActiveDay(index.row());
   564 	}
   846 	}
   565 }
   847 }
   586 	}
   868 	}
   587 	// Reset the focus attribute to earlier current item
   869 	// Reset the focus attribute to earlier current item
   588 	QModelIndex index = mModel->index(mCurrentRow,0);
   870 	QModelIndex index = mModel->index(mCurrentRow,0);
   589 	QVariant itemData = index.data(Qt::UserRole + 1);
   871 	QVariant itemData = index.data(Qt::UserRole + 1);
   590 	QVariantList list = itemData.toList();
   872 	QVariantList list = itemData.toList();
   591 	list.replace(CalendarNamespace::CalendarMonthFocusRole, QString(""));
   873 	list.replace(CalendarNamespace::CalendarMonthFocusRole, false);
   592 	mModel->itemFromIndex(index)->setData(list);
   874 	mModel->itemFromIndex(index)->setData(list);
   593 	
   875 	
   594 	// Search for this date in the model
   876 	// Search for this date in the model
   595 	for (int i = indexStart; i <= indexEnd; i++) {
   877 	for (int i = indexStart; i <= indexEnd; i++) {
   596 		if (monthDataList[i].Day().date() == dateToBeFocussed.date()) {
   878 		if (monthDataList[i].Day().date() == dateToBeFocussed.date()) {
   597 			index = mModel->index(i,0);
   879 			index = mModel->index(i,0);
   598 			itemData = index.data(Qt::UserRole + 1);
   880 			itemData = index.data(Qt::UserRole + 1);
   599 			list = itemData.toList();
   881 			list = itemData.toList();
   600 			list.replace(CalendarNamespace::CalendarMonthFocusRole,
   882 			list.replace(CalendarNamespace::CalendarMonthFocusRole,
   601 							 QString("qtg_fr_cal_focused_day_ind"));
   883 										 true);
   602 			mModel->itemFromIndex(index)->setData(list);
   884 			mModel->itemFromIndex(index)->setData(list);
   603 			mCurrentRow = i;
   885 			mCurrentRow = i;
   604 			mView->setContextForActiveDay(i);
   886 			mView->setContextForActiveDay(i);
   605 			break;
   887 			break;
   606 		}
   888 		}
   633 													activeDate.daysInMonth());
   915 													activeDate.daysInMonth());
   634 	// Number of days frm start of the grid to end of the month
   916 	// Number of days frm start of the grid to end of the month
   635 	end = firstDateInGrid.daysTo(endOfActiveMonth);
   917 	end = firstDateInGrid.daysTo(endOfActiveMonth);
   636 	
   918 	
   637 	// Set the active text color
   919 	// Set the active text color
   638 	QColor textColor = HbColorScheme::color("qtc_cal_month_active_dates");
   920 	for (int i = start; i < end; i++) {	
   639 	if (textColor.isValid()) {
   921 		QModelIndex index = mModel->index(i,0);
   640 		for (int i = start; i < end; i++) {	
   922 		QVariant itemData = index.data(Qt::UserRole + 1);
   641 			QModelIndex index = mModel->index(i,0);
   923 		QVariantList list = itemData.toList();
   642 			QVariant itemData = index.data(Qt::UserRole + 1);
   924 		list.replace(CalendarNamespace::CalendarMonthTextColorRole, true);
   643 			QVariantList list = itemData.toList();
   925 		mModel->itemFromIndex(index)->setData(list);
   644 			list.replace(CalendarNamespace::CalendarMonthTextColorRole, textColor);
       
   645 			mModel->itemFromIndex(index)->setData(list);
       
   646 		}
       
   647 	}
   926 	}
   648 	
   927 	
   649 	// Now set the inactive text color to those which were active before the swipe
   928 	// Now set the inactive text color to those which were active before the swipe
   650 	if (mDirection == invalid) {
   929 	if (mDirection == invalid) {
   651 		// no need to do anything as other dates will be in inactive dates color
   930 		// no need to do anything as other dates will be in inactive dates color
   681 		// Number of days frm start of the grid to end of the month
   960 		// Number of days frm start of the grid to end of the month
   682 		end = firstDateInGrid.daysTo(endOfActiveMonth);
   961 		end = firstDateInGrid.daysTo(endOfActiveMonth);
   683 	}
   962 	}
   684 	
   963 	
   685 	// Set the inactive text color
   964 	// Set the inactive text color
   686 	textColor = HbColorScheme::color("qtc_cal_month_notactive_dates");
   965 	for (int i = start; i < end; i++) {		
   687 	if (textColor.isValid()) {
   966 		QModelIndex index = mModel->index(i,0);
   688 		for (int i = start; i < end; i++) {		
   967 		QVariant itemData = index.data(Qt::UserRole + 1);
   689 			QModelIndex index = mModel->index(i,0);
   968 		QVariantList list = itemData.toList();
   690 			QVariant itemData = index.data(Qt::UserRole + 1);
   969 		list.replace(CalendarNamespace::CalendarMonthTextColorRole, false);
   691 			QVariantList list = itemData.toList();
   970 		mModel->itemFromIndex(index)->setData(list);
   692 			list.replace(CalendarNamespace::CalendarMonthTextColorRole, textColor);
       
   693 			mModel->itemFromIndex(index)->setData(list);
       
   694 		}
       
   695 	}
   971 	}
   696 }
   972 }
   697 
   973 
   698 /*!
   974 /*!
   699  To get current foucsed index of monthGrid
   975  To get current foucsed index of monthGrid
   717 void CalenMonthGrid::orientationChanged(Qt::Orientation newOrientation)
   993 void CalenMonthGrid::orientationChanged(Qt::Orientation newOrientation)
   718 {
   994 {
   719     Q_UNUSED(newOrientation)
   995     Q_UNUSED(newOrientation)
   720 	// We are overriding this function to avoid the default behavior of
   996 	// We are overriding this function to avoid the default behavior of
   721 	// hbgridview on orientation change as it swaps the row and column counts
   997 	// hbgridview on orientation change as it swaps the row and column counts
       
   998 	// Calculate the proper index to be scrolled to
       
   999 	int rowsInPrevMonth;
       
  1000     int itemToBeScrolled;
       
  1001     QModelIndex indexToBeScrolled;
       
  1002 	if (newOrientation == Qt::Horizontal) {
       
  1003 		rowsInPrevMonth = mView->rowsInPrevMonth();
       
  1004 		itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
  1005 		indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
  1006 		mIsAtomicScroll = true;
       
  1007 		scrollTo(indexToBeScrolled);
       
  1008 	} else {
       
  1009 		rowsInPrevMonth = mView->rowsInPrevMonth();
       
  1010 		itemToBeScrolled = rowsInPrevMonth * KCalenDaysInWeek;
       
  1011 		indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
  1012 		mIsAtomicScroll = true;
       
  1013 		scrollTo(indexToBeScrolled);
       
  1014 		
       
  1015 		itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
       
  1016 				KCalenDaysInWeek) - 1;
       
  1017 		indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
       
  1018 		mIsAtomicScroll = true;
       
  1019 		scrollTo(indexToBeScrolled);
       
  1020 	}
   722 }
  1021 }
   723 
  1022 
   724 /*!
  1023 /*!
   725  Paint function to draw grid lines
  1024  Paint function to draw grid lines
   726  */
  1025  */
   728                           const QStyleOptionGraphicsItem* option,
  1027                           const QStyleOptionGraphicsItem* option,
   729                           QWidget* widget)
  1028                           QWidget* widget)
   730 {
  1029 {
   731 	Q_UNUSED(option);
  1030 	Q_UNUSED(option);
   732 	Q_UNUSED(widget);
  1031 	Q_UNUSED(widget);
       
  1032 	painter->setRenderHint(QPainter::NonCosmeticDefaultPen);
   733 	
  1033 	
   734 	// Set the required attributes to the pen
  1034 	// Set the required attributes to the pen
   735 	QPen pen;
  1035 	QPen pen;
       
  1036 	HbDeviceProfile deviceProf;
       
  1037 	qreal unitValue = deviceProf.unitValue();
       
  1038 	qreal widthInPixels = GRIDLINE_WIDTH * unitValue;
   736 	pen.setStyle(Qt::SolidLine);
  1039 	pen.setStyle(Qt::SolidLine);
   737 	pen.setWidth(GRIDLINE_WIDTH);
  1040 	pen.setWidth(widthInPixels);
   738 	if (mGridLineColor.isValid()) {
  1041 	if (mGridLineColor.isValid()) {
   739 		pen.setBrush(mGridLineColor);
  1042 		pen.setBrush(mGridLineColor);
   740 	} else {
  1043 	} else {
   741 		pen.setBrush(mGridBorderColor);
  1044 		pen.setBrush(mGridBorderColor);
   742 	}
  1045 	}
   743 	// Set the pen to the painter
  1046 	//store the old pen first
       
  1047     QPen oldPen = painter->pen();
       
  1048 	// Set the new pen to the painter
   744 	painter->setPen(pen);
  1049 	painter->setPen(pen);
   745 	
  1050 	
   746 	// Get the sizes of content widget
  1051 	// Get the sizes of content widget
   747 	qreal contentHeight = mContentWidget->size().height();
  1052 	qreal contentHeight = mContentWidget->size().height();
   748 	qreal contentWidth = mContentWidget->size().width();
  1053 	qreal contentWidth = mContentWidget->size().width();
   749 	
  1054 	qreal rowWidth = 0.0;
   750 	// Get the num of rows
  1055 	int numOfRows = 0;
   751 	int numOfRows = mModel->rowCount() / KCalenDaysInWeek;
       
   752 	// Draw horizontal lines
       
   753 	qreal rowWidth = contentHeight / numOfRows;
       
   754 	
       
   755 	QPointF startPoint = mContentWidget->pos();
  1056 	QPointF startPoint = mContentWidget->pos();
       
  1057 	
       
  1058 	// NOTE!!!: There is a filcker when we blindly draw equally spaced lines
       
  1059 	// on complete content widget when scrolling is finished. This happens only
       
  1060 	// when content widget size is changed due to the change in total number
       
  1061 	// of rows when we append or prepend rows. Hence, to avoid this, we draw
       
  1062 	// lines on complete content widget only when it is scrolling.
       
  1063 	// That means, as soon as scrolling is finished, we will end up drawing 
       
  1064 	// only 6 lines that are visible to the user.
       
  1065 	if (mDirection == invalid) {
       
  1066 		// Start point is left most point on the screen
       
  1067 		startPoint = QPointF(0,0);
       
  1068 		rowWidth = size().height() / KNumOfVisibleRows;
       
  1069 		numOfRows = KNumOfVisibleRows;
       
  1070 	} else {
       
  1071 		// Get the num of rows
       
  1072 		numOfRows = mModel->rowCount() / KCalenDaysInWeek;
       
  1073 		// Draw horizontal lines
       
  1074 		rowWidth = contentHeight / numOfRows;
       
  1075 	}
       
  1076 	
   756 	QPointF endPoint(startPoint.x() + contentWidth, 
  1077 	QPointF endPoint(startPoint.x() + contentWidth, 
   757 	                 startPoint.y());
  1078 	                 startPoint.y());
   758 	
  1079 	
   759 	// Create the list of points for which lines have to be drawn
  1080 	// Create the list of points for which lines have to be drawn
   760 	// List should have even number of points so that it draws all the lines
  1081 	// List should have even number of points so that it draws all the lines
   778 		pointList.append(QPointF(endPoint.x() + (i * colWidth), endPoint.y()));
  1099 		pointList.append(QPointF(endPoint.x() + (i * colWidth), endPoint.y()));
   779 	}
  1100 	}
   780 	
  1101 	
   781 	// Draw the lines for the points in the vector list
  1102 	// Draw the lines for the points in the vector list
   782 	painter->drawLines(pointList);
  1103 	painter->drawLines(pointList);
       
  1104 	// Set the old pen back
       
  1105 	painter->setPen(oldPen);
   783 }
  1106 }
   784 
  1107 
   785 // End of File
  1108 // End of File