calendarui/views/src/calenmonthgrid.cpp
changeset 32 ea672fcb0ea0
parent 26 a949c2543c15
child 37 360d55486d7f
equal deleted inserted replaced
26:a949c2543c15 32:ea672fcb0ea0
    18 // System includes
    18 // System includes
    19 #include <hbgridview.h>
    19 #include <hbgridview.h>
    20 #include <hbabstractviewitem.h>
    20 #include <hbabstractviewitem.h>
    21 #include <hbstyleloader.h>
    21 #include <hbstyleloader.h>
    22 #include <hbcolorscheme.h>
    22 #include <hbcolorscheme.h>
       
    23 #include <hbpangesture.h>
       
    24 #include <hbswipegesture.h>
    23 
    25 
    24 // User includes
    26 // User includes
    25 #include "calenmonthgrid.h"
    27 #include "calenmonthgrid.h"
    26 #include "calengriditemprototype.h"
    28 #include "calengriditemprototype.h"
    27 #include "calenmonthdata.h"
    29 #include "calenmonthdata.h"
    28 #include "calenmonthview.h"
    30 #include "calenmonthview.h"
    29 #include "calendateutils.h"
    31 #include "calendateutils.h"
    30 #include "calencommon.h"
    32 #include "calencommon.h"
    31 
    33 
    32 // Constants
    34 // Constants
    33 #define SCROLL_SPEEED 2000 
    35 #define SCROLL_SPEEED 3000 
    34 #define GRIDLINE_WIDTH 0.075 //units
    36 #define GRIDLINE_WIDTH 0.075 //units
    35 
    37 
    36 /*!
    38 /*!
    37  \class CalenMonthGrid
    39  \class CalenMonthGrid
    38 
    40 
    47 	mModel(0),
    49 	mModel(0),
    48 	mDirection(invalid),
    50 	mDirection(invalid),
    49 	mIsPanGesture(false),
    51 	mIsPanGesture(false),
    50 	mIsAtomicScroll(true),
    52 	mIsAtomicScroll(true),
    51 	mView(NULL),
    53 	mView(NULL),
    52 	mCurrentRow(0),
    54 	mCurrentRow(-100),
    53 	mIsNonActiveDayFocused(false),
    55 	mIsNonActiveDayFocused(false),
    54 	mIgnoreItemActivated(false),
    56 	mIgnoreItemActivated(false),
    55 	mGridBorderColor(Qt::gray)
    57 	mGridBorderColor(Qt::gray)
    56 {
    58 {
    57 	setScrollDirections(Qt::Vertical);
    59 	setScrollDirections(Qt::Vertical);
    62 	setSelectionMode(HbGridView::NoSelection);
    64 	setSelectionMode(HbGridView::NoSelection);
    63 	setUniformItemSizes(true);
    65 	setUniformItemSizes(true);
    64 	setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
    66 	setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
    65 	setClampingStyle(HbScrollArea::StrictClamping);
    67 	setClampingStyle(HbScrollArea::StrictClamping);
    66 	setEnabledAnimations(HbAbstractItemView::None);
    68 	setEnabledAnimations(HbAbstractItemView::None);
       
    69 	setFrictionEnabled(false);
    67 	resetTransform();
    70 	resetTransform();
    68 	
    71 	
    69 	// Get the content widget of the scroll area to draw the grid lines
    72 	// Get the content widget of the scroll area to draw the grid lines
    70 	mContentWidget = contentWidget();
    73 	mContentWidget = contentWidget();
    71 	
    74 	
    82 	}
    85 	}
    83 	
    86 	
    84 	// Connect to scrolling finished signal
    87 	// Connect to scrolling finished signal
    85 	connect(this, SIGNAL(scrollingEnded()), this,
    88 	connect(this, SIGNAL(scrollingEnded()), this,
    86 			SLOT(scrollingFinished()));
    89 			SLOT(scrollingFinished()));
    87 	
       
    88 	// Connect to item activated signal
       
    89 	connect(this, SIGNAL(activated(const QModelIndex &)), this,
       
    90 				SLOT(itemActivated(const QModelIndex &)));
       
    91 }
    90 }
    92 
    91 
    93 /*!
    92 /*!
    94  Destructor
    93  Destructor
    95  */
    94  */
   268 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
   267 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
   269 								   KCalenDaysInWeek) - 1;
   268 								   KCalenDaysInWeek) - 1;
   270 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   269 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   271 	mIsAtomicScroll = true;
   270 	mIsAtomicScroll = true;
   272 	scrollTo(indexToBeScrolled);
   271 	scrollTo(indexToBeScrolled);
       
   272 	
       
   273 	// Update the sart position of the content widget
       
   274 	mStartPos = mContentWidget->pos();
       
   275 	
       
   276 	// Now connect to the signal which gets emitted when any item on the grid 
       
   277 	// is tapped.
       
   278 	connect(this, SIGNAL(activated(const QModelIndex &)), this,
       
   279 						SLOT(itemActivated(const QModelIndex &)));
   273 }
   280 }
   274 
   281 
   275 /*!
   282 /*!
   276 	Updates the view with just event indicators
   283 	Updates the view with just event indicators
   277  */
   284  */
   290 		}
   297 		}
   291 	}
   298 	}
   292 }
   299 }
   293 
   300 
   294 /*!
   301 /*!
   295  Listens for down gesture
   302  Scrolls the content dowmwards
   296  */
   303  */
   297 void CalenMonthGrid::downGesture (int value)
   304 void CalenMonthGrid::downGesture()
   298 {
   305 {
   299 	Q_UNUSED(value)	
   306     mDirection = down;
   300 	mDirection = down;
   307     mIsAtomicScroll = false;
   301 	mIsAtomicScroll = false;
   308     setAttribute(Hb::InteractionDisabled);
   302 	setAttribute(Hb::InteractionDisabled);
   309     QPointF targetPos(0.0, 0.0);
   303 	
   310     scrollContentsTo(targetPos,500);
   304 	// pass it to parent
   311 }
   305 	HbScrollArea::downGesture(value);
   312 
   306 }
   313 /*!
   307 
   314  Scrolls the content upwards
   308 /*!
   315  */
   309  Listens for Up gesture
   316 void CalenMonthGrid::upGesture()
   310  */
   317 {
   311 void CalenMonthGrid::upGesture (int value)
   318     mDirection = up;
   312 {
   319     mIsAtomicScroll = false;
   313 	Q_UNUSED(value)	
   320     setAttribute(Hb::InteractionDisabled);
   314 	mDirection = up;
   321     QPointF targetPos(0.0, mStartPos.y() - size().height());
   315 	mIsAtomicScroll = false;
   322     scrollContentsTo(-targetPos,500);
   316 	setAttribute(Hb::InteractionDisabled);
       
   317 	
       
   318 	// pass it to parent
       
   319 	HbScrollArea::upGesture(value);
       
   320 	
   323 	
   321 }
   324 }
   322 
   325 
   323 /*!
   326 /*!
   324  Function to listen mouse press events
   327  Function to listen mouse press events
   325  */
   328  */
   326 void CalenMonthGrid::mousePressEvent(QGraphicsSceneMouseEvent* event)
   329 void CalenMonthGrid::mousePressEvent(QGraphicsSceneMouseEvent* event)
   327 {
   330 {
   328 	mPressedPos = event->pos();
       
   329 	// Pass it to parent
   331 	// Pass it to parent
   330 	HbGridView::mousePressEvent(event);
   332 	HbGridView::mousePressEvent(event);
   331 }
   333 }
   332 
   334 
   333 /*!
   335 /*!
   334  Function to listen mouse release events
   336  Function to listen mouse release events
   335  */
   337  */
   336 void CalenMonthGrid::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
   338 void CalenMonthGrid::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
   337 {
   339 {
   338 	int posDiff = mPressedPos.y() - event->pos().y();
   340 	// Pass it grid view if pan gesture is not in progress else pass it to
   339 	if (posDiff < -50) {
   341 	// scrollarea. Problem here is, if we pass to gridview when panning, then 
   340 		mDirection = down;
   342 	// its emitting item activated signal simply becasue of which focus item
   341 	} else if (posDiff > 50){
   343 	// is getting changed when you finish the pan / shake
   342 		mDirection = up;
       
   343 	}
       
   344 	// Pass it to parent
       
   345 	HbGridView::mouseReleaseEvent(event);
       
   346 }
       
   347 
       
   348 /*!
       
   349  Listens for pan gesture
       
   350  */
       
   351 void  CalenMonthGrid::panGesture(const QPointF &  delta)
       
   352 {
       
   353 	setAttribute(Hb::InteractionDisabled);
       
   354 	mIsAtomicScroll = false;
       
   355 	if (!mIsPanGesture) {
   344 	if (!mIsPanGesture) {
   356 		mIsPanGesture = true;
   345 		HbGridView::mouseReleaseEvent(event);
   357 		mIgnoreItemActivated = true;
   346 	} else {
   358 		mStartPos = mContentWidget->pos();
   347 		HbScrollArea::mouseReleaseEvent(event);
   359 		// Get to know the direction of the gesture
   348 	}
   360 		if (delta.y() > 0) {
   349 }
   361 			mDirection = down;
   350 
   362 		} else {
   351 /*!
   363 			mDirection = up;
   352     Function to list for all the gesture events
   364 		}
   353  */
   365 	}
   354 void CalenMonthGrid::gestureEvent(QGestureEvent *event)
   366 	
   355 {
   367 	// Call the parent class to perform the pan gesture
   356    if(HbPanGesture *gesture = qobject_cast<HbPanGesture *>(event->gesture(Qt::PanGesture))) {
   368 	// When scrolling finished, month grid will adjust to show the proper month
   357         if (gesture->state() == Qt::GestureStarted) {
   369 	HbScrollArea::panGesture(delta);
   358             setAttribute(Hb::InteractionDisabled);
       
   359             mIsAtomicScroll = false;
       
   360             if (!mIsPanGesture) {
       
   361                 mIsPanGesture = true;
       
   362                 mIgnoreItemActivated = true;
       
   363                 mStartPos = mContentWidget->pos();
       
   364                 // Get to know the direction of the gesture
       
   365                 QPointF velocity = gesture->velocity();
       
   366                 if (velocity.y() > 0) {
       
   367                     mDirection = down;
       
   368                 } else {
       
   369                     mDirection = up;
       
   370                 }
       
   371             }
       
   372         } else if(gesture->state() == Qt::GestureFinished) {
       
   373 		// TODO: Need to handle here to avoid followOn animation
       
   374         }
       
   375     } else if(HbSwipeGesture *gesture = qobject_cast<HbSwipeGesture *>(event->gesture(Qt::SwipeGesture))) {
       
   376         if (gesture->state() == Qt::GestureStarted) {
       
   377             setAttribute(Hb::InteractionDisabled);
       
   378             mIsAtomicScroll = false;
       
   379             if (gesture->swipeAngle() > 250 && gesture->swipeAngle() < 290 && 
       
   380                     gesture->verticalDirection() == QSwipeGesture::Down) {
       
   381                 mDirection = down;
       
   382             } else if (gesture->swipeAngle() > 70 && gesture->swipeAngle() < 110 && 
       
   383                     gesture->verticalDirection() == QSwipeGesture::Up) {
       
   384                 mDirection = up;
       
   385             }
       
   386         }
       
   387         gesture->setSpeed(SCROLL_SPEEED);
       
   388     }
       
   389    
       
   390     // Call the parent class to perform the pan gesture
       
   391     // When scrolling finished, month grid will adjust to show the proper month
       
   392     HbScrollArea::gestureEvent(event);
   370 }
   393 }
   371 
   394 
   372 /*!
   395 /*!
   373  Gets called when scrolling finishes to update the model
   396  Gets called when scrolling finishes to update the model
   374  */
   397  */
   375 void CalenMonthGrid::scrollingFinished()
   398 void CalenMonthGrid::scrollingFinished()
   376 {
   399 {
   377 	
       
   378 	if (mIsPanGesture) {
   400 	if (mIsPanGesture) {
   379 		handlePanGestureFinished();
   401 		handlePanGestureFinished();
   380 	} else if(!mIsAtomicScroll) {
   402 	} else if(!mIsAtomicScroll) {
   381 		QDateTime activeDate = mView->getActiveDay();
   403 		QDateTime activeDate = mView->getActiveDay();
   382 		if(mDirection == down) { // down gesture
   404 		if(mDirection == down) { // down gesture
   424 		// more than or equal to half of the future month
   446 		// more than or equal to half of the future month
   425 		if (date.date().day() > (activeMonth.date().daysInMonth()) / 2 ||
   447 		if (date.date().day() > (activeMonth.date().daysInMonth()) / 2 ||
   426 				date.addDays(KNumOfVisibleRows*KCalenDaysInWeek).date().day() >=
   448 				date.addDays(KNumOfVisibleRows*KCalenDaysInWeek).date().day() >=
   427 				(prevMonth.date().daysInMonth()) / 2) {
   449 				(prevMonth.date().daysInMonth()) / 2) {
   428 			// up gesture to bring the next month
   450 			// up gesture to bring the next month
   429 			upGesture(SCROLL_SPEEED);
   451 			upGesture();
   430 		} else {
   452 		} else {
   431 			// we should again show the current month by scrolling downwards
   453 			// we should again show the current month by scrolling downwards
   432 			mDirection = down;
   454 			mDirection = down;
   433 			mIsAtomicScroll = true;
   455 			mIsAtomicScroll = true;
       
   456 			setAttribute(Hb::InteractionDisabled);
   434 			scrollContentsTo(-mStartPos,500);
   457 			scrollContentsTo(-mStartPos,500);
   435 		}
   458 		}
   436 	} else if (month == prevMonth.date().month()) {
   459 	} else if (month == prevMonth.date().month()) {
   437 		// first visible item belongs to previous month
   460 		// first visible item belongs to previous month
   438 		// Check if the date is more than half of the previous month
   461 		// Check if the date is more than half of the previous month
   439 		if (date.date().day() > (prevMonth.date().daysInMonth()) / 2) {
   462 		if (date.date().day() > (prevMonth.date().daysInMonth()) / 2) {
   440 			// we should again show the current month by scrolling upwards
   463 			// we should again show the current month by scrolling upwards
   441 			mDirection = up;
   464 			mDirection = up;
   442 			mIsAtomicScroll = true;
   465 			mIsAtomicScroll = true;
       
   466 			setAttribute(Hb::InteractionDisabled);
   443 			scrollContentsTo(-mStartPos,500);
   467 			scrollContentsTo(-mStartPos,500);
   444 		} else {
   468 		} else {
   445 			// down gesture to show the previous month
   469 			// down gesture to show the previous month
   446 			downGesture(SCROLL_SPEEED);
   470 			downGesture();
   447 		}
   471 		}
       
   472 	} else if (month == prevMonth.addMonths(-1).date().month()) {
       
   473 		// first visible date belong to previous to previous month
       
   474 		// hence, scroll down to bring the previous month
       
   475 		downGesture();
   448 	} else if (month == nextMonth.date().month()) {
   476 	} else if (month == nextMonth.date().month()) {
   449 		// first visible item belongs to next month
   477 		// first visible item belongs to next month
   450 		// Check if the date is more than half of the next month
   478 		// Check if the date is more than half of the next month
   451 		if (date.date().day() > (nextMonth.date().daysInMonth()) / 2) {
   479 		if (date.date().day() > (nextMonth.date().daysInMonth()) / 2) {
   452 			// up gesture to bring the next month
   480 			// up gesture to bring the next month
   453 			upGesture(SCROLL_SPEEED);
   481 			upGesture();
   454 		} else {
   482 		} else {
   455 			// we should again show the current month by scrolling upwards
   483 			// we should again show the current month by scrolling upwards
   456 			mDirection = invalid;
   484 			mDirection = invalid;
       
   485 			setAttribute(Hb::InteractionDisabled);
   457 			scrollContentsTo(-mStartPos,500);
   486 			scrollContentsTo(-mStartPos,500);
   458 		}
   487 		}
       
   488 	} else if (month == nextMonth.addMonths(1).date().month()) {
       
   489 		// first visible date belongs to next to next month
       
   490 		// hence, scroll up to show the next month
       
   491 		upGesture();
   459 	}
   492 	}
   460 }
   493 }
   461 
   494 
   462 /*!
   495 /*!
   463  Called when down gesture is performed. Adds the new previous month details
   496  Called when down gesture is performed. Adds the new previous month details
   519 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   552 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   520 	mIsAtomicScroll = true;
   553 	mIsAtomicScroll = true;
   521 	scrollTo(indexToBeScrolled);
   554 	scrollTo(indexToBeScrolled);
   522 	// Update the mCurrentRow
   555 	// Update the mCurrentRow
   523 	mCurrentRow += countToBeAdded;
   556 	mCurrentRow += countToBeAdded;
       
   557 	
       
   558 	// Update the sart position of the content widget
       
   559 	mStartPos = mContentWidget->pos();
   524 }
   560 }
   525 
   561 
   526 /*!
   562 /*!
   527 	Helper function that prepends the required rows to the model
   563 	Helper function that prepends the required rows to the model
   528  */
   564  */
   639 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
   675 	itemToBeScrolled = ((rowsInPrevMonth + KNumOfVisibleRows) * 
   640 									   KCalenDaysInWeek) - 1;
   676 									   KCalenDaysInWeek) - 1;
   641 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   677 	indexToBeScrolled = mModel->index(itemToBeScrolled, 0);
   642 	mIsAtomicScroll = true;
   678 	mIsAtomicScroll = true;
   643 	scrollTo(indexToBeScrolled);
   679 	scrollTo(indexToBeScrolled);
       
   680 	
       
   681 	// Update the sart position of the content widget
       
   682     mStartPos = mContentWidget->pos();
   644 }
   683 }
   645 
   684 
   646 /*!
   685 /*!
   647 	Helper function that appends the required rows to the model
   686 	Helper function that appends the required rows to the model
   648  */
   687  */
   711 		// Launch the agenda view
   750 		// Launch the agenda view
   712 		mView->launchDayView();
   751 		mView->launchDayView();
   713 	} else {
   752 	} else {
   714 		// Reset the focus attribute to this item		
   753 		// Reset the focus attribute to this item		
   715 		QModelIndex itemIndex = mModel->index(mCurrentRow,0);
   754 		QModelIndex itemIndex = mModel->index(mCurrentRow,0);
       
   755 		if(itemIndex.row() < 0 || itemIndex.row() >= mModel->rowCount() ||
       
   756 				itemIndex.column() < 0 || itemIndex.column() > mModel->columnCount()) {
       
   757 			return;
       
   758 		}
   716 		QVariant itemData = itemIndex.data(Qt::UserRole + 1);
   759 		QVariant itemData = itemIndex.data(Qt::UserRole + 1);
   717 		QVariantList list = itemData.toList();
   760 		QVariantList list = itemData.toList();
   718 		list.replace(CalendarNamespace::CalendarMonthFocusRole, false);
   761 		list.replace(CalendarNamespace::CalendarMonthFocusRole, false);
   719 		mModel->itemFromIndex(itemIndex)->setData(list);
   762 		mModel->itemFromIndex(itemIndex)->setData(list);
   720 		
   763 		
   738 			activeMonth = activeMonth.addMonths(1);
   781 			activeMonth = activeMonth.addMonths(1);
   739 			if (activeMonth.date().month() == 
   782 			if (activeMonth.date().month() == 
   740 				mNonActiveFocusedDay.date().month()) {
   783 				mNonActiveFocusedDay.date().month()) {
   741 				mDirection = up;
   784 				mDirection = up;
   742 				// up gesture
   785 				// up gesture
   743 				upGesture(SCROLL_SPEEED);
   786 				upGesture();
   744 				setActiveDates(activeMonth.date());
   787 				setActiveDates(activeMonth.date());
   745 			} else {
   788 			} else {
   746 				mDirection = down;
   789 				mDirection = down;
   747 				// down gesture
   790 				// down gesture
   748 				downGesture(SCROLL_SPEEED);
   791 				downGesture();
   749 				setActiveDates(activeMonth.addMonths(-2).date());
   792 				setActiveDates(activeMonth.addMonths(-2).date());
   750 			}
   793 			}
   751 		} 
   794 		} 
   752 		mView->setContextForActiveDay(index.row());
   795 		mView->setContextForActiveDay(index.row());
   753 	}
   796 	}