src/hbwidgets/widgets/hbdatetimepicker_p.cpp
changeset 1 f7ac710697a9
parent 0 16d8024aca5e
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
   315     ,mIs24HourFormat(false)
   315     ,mIs24HourFormat(false)
   316     ,mIsTwoDigitYearFormat(false)
   316     ,mIsTwoDigitYearFormat(false)
   317     ,mBackground(0)
   317     ,mBackground(0)
   318     ,mFrame(0)
   318     ,mFrame(0)
   319     ,mContent(0)
   319     ,mContent(0)
       
   320     ,mIntervals()
       
   321     ,mHighlight(0)
   320 {
   322 {
   321     mMinimumDate = HBDATETIMEPICKER_DATETIME_MIN;
   323     mMinimumDate = HBDATETIMEPICKER_DATETIME_MIN;
   322     mMaximumDate = HBDATETIMEPICKER_DATETIME_MAX;
   324     mMaximumDate = HBDATETIMEPICKER_DATETIME_MAX;
   323     mDateTime = mMinimumDate;
   325     mDateTime = mMinimumDate;
   324 }
   326 }
   325 
   327 
   326 HbDateTimePickerPrivate::~HbDateTimePickerPrivate()
   328 HbDateTimePickerPrivate::~HbDateTimePickerPrivate()
   327 {
   329 {
   328     QGraphicsLayoutItem *item;
       
   329     foreach(item,mDividers)
       
   330     {
       
   331         mLayout->removeItem(item);
       
   332         delete item;
       
   333     }
       
   334 }
   330 }
   335 
   331 
   336 /*
   332 /*
   337    called only once. while the widget is constructing.
   333    called only once. while the widget is constructing.
   338 */
   334 */
   342     Q_Q(HbDateTimePicker);
   338     Q_Q(HbDateTimePicker);
   343 
   339 
   344     //create base content widget which contains the tumble views
   340     //create base content widget which contains the tumble views
   345     mContent=new HbWidget(q);
   341     mContent=new HbWidget(q);
   346     mLayout = new QGraphicsLinearLayout(Qt::Horizontal);
   342     mLayout = new QGraphicsLinearLayout(Qt::Horizontal);
   347     mLayout->setSpacing(0);
   343     mLayout->setSpacing(1);
   348     mLayout->setContentsMargins(0,0,0,0);
   344     mLayout->setContentsMargins(0,0,0,0);
   349     mContent->setLayout(mLayout);
   345     mContent->setLayout(mLayout);
   350     q->style()->setItemName(mContent,"content");
   346     q->style()->setItemName(mContent,"content");
   351 
   347 
   352     mDateTimeMode = dateTimeMode;
   348     mDateTimeMode = dateTimeMode;
   355     mFormat = localeDateTimeFormat(dateTimeMode);
   351     mFormat = localeDateTimeFormat(dateTimeMode);
   356 
   352 
   357     //parse the format and set the sections in order
   353     //parse the format and set the sections in order
   358     parseDisplayFormat(mFormat);
   354     parseDisplayFormat(mFormat);
   359 
   355 
   360     mDividers.clear();
   356     //create primitives
   361 
       
   362     //create the dividers used in rearrangeTumbleViews
       
   363     createPrimitives();
   357     createPrimitives();
   364 
   358 
   365     //recreate and rearrange depending on the format
   359     //recreate and rearrange depending on the format
   366     rearrangeTumbleViews();
   360     rearrangeTumbleViews();
   367 }
   361 }
   473     mDayOffset = -1;
   467     mDayOffset = -1;
   474     mHourOffset = -1;
   468     mHourOffset = -1;
   475     mMinuteOffset = -1;
   469     mMinuteOffset = -1;
   476     mSecondOffset = -1;
   470     mSecondOffset = -1;
   477 
   471 
   478     createDividers();
   472     QPointer<HbTumbleView> lastAdded;
   479 
       
   480     //divider stuff
       
   481     //TODO: improve the divider addition and removal
       
   482     foreach(QGraphicsItem *item, mDividers) {
       
   483         HbFrameItem *fame = qgraphicsitem_cast<HbFrameItem *>(item);
       
   484         Q_ASSERT(fame); // WRONG USE OF PRIMITIVES - Please fix it
       
   485         mLayout->removeItem(fame);
       
   486         fame->setVisible(false);
       
   487     }
       
   488 
       
   489 
       
   490     //TODO: improve the divider addition and removal
       
   491     bool hasSeparator = mParser.mSectionNodes.count() > 1;
       
   492 
   473 
   493     for(int i=0;i<mParser.mSectionNodes.count();i++) {
   474     for(int i=0;i<mParser.mSectionNodes.count();i++) {
   494 
       
   495         if(hasSeparator && (mLayout->count()>0)) {
       
   496             //TODO: improve the divider addition and removal
       
   497             HbFrameItem *f=static_cast<HbFrameItem*>(mDividers.at(i - 1));
       
   498             if(f) {
       
   499                 f->setVisible(true);
       
   500             }
       
   501             mLayout->addItem(mDividers.at(i - 1));
       
   502         }
       
   503 
       
   504         switch(mParser.mSectionNodes[i].type) {
   475         switch(mParser.mSectionNodes[i].type) {
   505             case HbDateTimeParser::AmPmSection:
   476             case HbDateTimeParser::AmPmSection:
   506                 mAmPmPicker = new HbTumbleView(q);
   477                 mAmPmPicker = new HbTumbleView(q);
   507                 mAmPmModel = new QStringListModel(q);
   478                 mAmPmModel = new QStringListModel(q);
   508                 mAmPmPicker->setModel(mAmPmModel);
   479                 mAmPmPicker->setModel(mAmPmModel);
       
   480                 //mAmPmPicker->setLoopingEnabled(true);
   509                 mLayout->addItem(mAmPmPicker);
   481                 mLayout->addItem(mAmPmPicker);
   510                 break;
   482                 mAmPmPicker->primitive("highlight")->hide();
       
   483                 mAmPmPicker->primitive("separator")->show();
       
   484                 lastAdded = mAmPmPicker;
       
   485                 break;
       
   486 
   511             case HbDateTimeParser::DaySection:
   487             case HbDateTimeParser::DaySection:
   512             case HbDateTimeParser::DayOfWeekSection:
   488             case HbDateTimeParser::DayOfWeekSection:
   513                 mDayPicker = new HbTumbleView(q);
   489                 mDayPicker = new HbTumbleView(q);
   514                 mDayModel = new QStringListModel(q);
   490                 mDayModel = new QStringListModel(q);
   515                 mDayPicker->setModel(mDayModel);
   491                 mDayPicker->setModel(mDayModel);
       
   492                 //mDayPicker->setLoopingEnabled(true);
   516                 mLayout->addItem(mDayPicker);
   493                 mLayout->addItem(mDayPicker);
   517                 break;
   494                 mDayPicker->primitive("highlight")->hide();
       
   495                 mDayPicker->primitive("separator")->show();
       
   496                 lastAdded = mDayPicker;
       
   497                 break;
       
   498 
   518             case HbDateTimeParser::MonthSection:
   499             case HbDateTimeParser::MonthSection:
   519                 mMonthPicker = new HbTumbleView(q);
   500                 mMonthPicker = new HbTumbleView(q);
   520                 mMonthModel = new QStringListModel(q);
   501                 mMonthModel = new QStringListModel(q);
   521                 mMonthPicker->setModel(mMonthModel);
   502                 mMonthPicker->setModel(mMonthModel);
       
   503                 //mMonthPicker->setLoopingEnabled(true);
   522                 mLayout->addItem(mMonthPicker);
   504                 mLayout->addItem(mMonthPicker);
   523                 break;
   505                 mMonthPicker->primitive("highlight")->hide();
       
   506                 mMonthPicker->primitive("separator")->show();
       
   507                 lastAdded = mMonthPicker;
       
   508                 break;
       
   509 
   524             case HbDateTimeParser::YearSection:
   510             case HbDateTimeParser::YearSection:
   525             case HbDateTimeParser::YearSection2Digits:
   511             case HbDateTimeParser::YearSection2Digits:
   526                 mYearPicker = new HbTumbleView(q);
   512                 mYearPicker = new HbTumbleView(q);
   527                 mYearModel = new QStringListModel(q);
   513                 mYearModel = new QStringListModel(q);
   528                 mYearPicker->setModel(mYearModel);
   514                 mYearPicker->setModel(mYearModel);
       
   515                 //mYearPicker->setLoopingEnabled(true);
   529                 mLayout->addItem(mYearPicker);
   516                 mLayout->addItem(mYearPicker);
   530                 break;
   517                 mYearPicker->primitive("highlight")->hide();
       
   518                 mYearPicker->primitive("separator")->show();
       
   519                 lastAdded = mYearPicker;
       
   520                 break;
       
   521 
   531             case HbDateTimeParser::SecondSection:
   522             case HbDateTimeParser::SecondSection:
   532                 mSecondPicker = new HbTumbleView(q);
   523                 mSecondPicker = new HbTumbleView(q);
   533                 mSecondModel = new QStringListModel(q);
   524                 mSecondModel = new QStringListModel(q);
   534                 mSecondPicker->setModel(mSecondModel);
   525                 mSecondPicker->setModel(mSecondModel);
       
   526                 //mSecondPicker->setLoopingEnabled(false);
   535                 mLayout->addItem(mSecondPicker);
   527                 mLayout->addItem(mSecondPicker);
   536                 break;
   528                 mSecondPicker->primitive("highlight")->hide();
       
   529                 mSecondPicker->primitive("separator")->show();
       
   530                 lastAdded = mSecondPicker;
       
   531                 break;
       
   532 
   537             case HbDateTimeParser::MinuteSection:
   533             case HbDateTimeParser::MinuteSection:
   538                 mMinutePicker = new HbTumbleView(q);
   534                 mMinutePicker = new HbTumbleView(q);
   539                 mMinuteModel = new QStringListModel(q);
   535                 mMinuteModel = new QStringListModel(q);
   540                 mMinutePicker->setModel(mMinuteModel);
   536                 mMinutePicker->setModel(mMinuteModel);
       
   537                 //mMinutePicker->setLoopingEnabled(false);
   541                 mLayout->addItem(mMinutePicker);
   538                 mLayout->addItem(mMinutePicker);
   542                 break;
   539                 mMinutePicker->primitive("highlight")->hide();
       
   540                 mMinutePicker->primitive("separator")->show();
       
   541                 lastAdded = mMinutePicker;
       
   542                 break;
       
   543 
   543             case HbDateTimeParser::Hour12Section:
   544             case HbDateTimeParser::Hour12Section:
   544             case HbDateTimeParser::Hour24Section:
   545             case HbDateTimeParser::Hour24Section:
   545                 mHourPicker = new HbTumbleView(q);
   546                 mHourPicker = new HbTumbleView(q);
   546                 mHourModel = new QStringListModel(q);
   547                 mHourModel = new QStringListModel(q);
   547                 mHourPicker->setModel(mHourModel);
   548                 mHourPicker->setModel(mHourModel);
       
   549                 //mHourPicker->setLoopingEnabled(true);
   548                 mLayout->addItem(mHourPicker);
   550                 mLayout->addItem(mHourPicker);
   549                 break;
   551                 mHourPicker->primitive("highlight")->hide();
   550             default:break;
   552                 mHourPicker->primitive("separator")->show();
   551         }
   553                 lastAdded = mHourPicker;
   552     }
   554                 break;
       
   555 
       
   556             default:
       
   557                 break;
       
   558         }
       
   559     }
       
   560 
       
   561     //For the last added tumble view, hide the separator.
       
   562     if(lastAdded){
       
   563         lastAdded->primitive("separator")->hide();
       
   564     }
       
   565 
   553     setRanges();
   566     setRanges();
   554     makeConnections();
   567     makeConnections();
   555     syncVisualDate();
   568     syncVisualDate();
   556  //TODO:what to do with current date, should reset ?
   569     //TODO:what to do with current date, should reset ?
   557 }
   570 }
   558 
   571 
   559 void HbDateTimePickerPrivate::makeConnections()
   572 void HbDateTimePickerPrivate::makeConnections()
   560 {
   573 {
   561     Q_Q(HbDateTimePicker);
   574     Q_Q(HbDateTimePicker);
   815             mSecondPicker->setSelected(newDateTime.time().second()-mSecondOffset);
   828             mSecondPicker->setSelected(newDateTime.time().second()-mSecondOffset);
   816 #ifdef HBDATETIMEPICKER_DEBUG
   829 #ifdef HBDATETIMEPICKER_DEBUG
   817             qDebug() << "setDateTime after: secondOffset=" << mSecondOffset << " time=" << newDateTime.time();
   830             qDebug() << "setDateTime after: secondOffset=" << mSecondOffset << " time=" << newDateTime.time();
   818 #endif
   831 #endif
   819         }
   832         }
       
   833         if(mAmPmPicker){
       
   834             if(newDate.time().hour() >= 12){
       
   835                 mAmPmPicker->setSelected(1);
       
   836             }
       
   837             else{
       
   838                 mAmPmPicker->setSelected(0);
       
   839             }
       
   840         }
       
   841 
   820         mDateTime = newDateTime;
   842         mDateTime = newDateTime;
   821 
   843 
   822     }
   844     }
   823 }
   845 }
   824 void HbDateTimePickerPrivate::setMinimumDateTime(const QDateTime &newMinDateTime)
   846 void HbDateTimePickerPrivate::setMinimumDateTime(const QDateTime &newMinDateTime)
   992     }
  1014     }
   993 
  1015 
   994     resizeModel(mMinuteModel,
  1016     resizeModel(mMinuteModel,
   995             mMinuteOffset,mMinuteOffset+mMinuteModel->rowCount()-1,
  1017             mMinuteOffset,mMinuteOffset+mMinuteModel->rowCount()-1,
   996             start,end,
  1018             start,end,
   997             &HbDateTimePickerPrivate::localeMinute);
  1019             &HbDateTimePickerPrivate::localeMinute, mIntervals[QDateTimeEdit::MinuteSection]);
   998     mMinuteOffset = start;
  1020     mMinuteOffset = start;
   999 
  1021 
  1000     mMinutePicker->setSelected(newIndex);
  1022     mMinutePicker->setSelected(newIndex);
  1001 
  1023 
  1002     //check if minute is valid
  1024     //check if minute is valid
  1119    model classes with one interface/virtual fuction specialization.
  1141    model classes with one interface/virtual fuction specialization.
  1120 */
  1142 */
  1121 void HbDateTimePickerPrivate::resizeModel(QStringListModel *model,
  1143 void HbDateTimePickerPrivate::resizeModel(QStringListModel *model,
  1122             int oldStart, int oldEnd,
  1144             int oldStart, int oldEnd,
  1123             int newStart, int newEnd,
  1145             int newStart, int newEnd,
  1124             QString (HbDateTimePickerPrivate::*localeFunc)(int))
  1146             QString (HbDateTimePickerPrivate::*localeFunc)(int), int interval)
  1125 {
  1147 {
  1126     //if row count is zero, then insert from newEnd to newStart
  1148     if(interval > 1){
       
  1149         model->removeRows(0, model->rowCount());
       
  1150     }
       
  1151 
  1127     if((model->rowCount() == 0) && (newEnd-newStart>=0)) {
  1152     if((model->rowCount() == 0) && (newEnd-newStart>=0)) {
  1128         //initialize condition
  1153         //initialize condition
  1129         model->insertRows(0,newEnd-newStart+1);
  1154 
  1130         for(int i=0;i<=newEnd-newStart;i++) {
  1155         for(int i=0;i<=newEnd-newStart;i++) {
  1131             QModelIndex index=model->index(i,0);
  1156             //model->setData(index,(this->*localeFunc)(i+newStart));//TODO:add a readable typedef
  1132             if(index.isValid()) {
  1157             QString text;
  1133                 //model->setData(index,(this->*localeFunc)(i+newStart));//TODO:add a readable typedef
  1158 
  1134                 QString text = (this->*localeFunc)(i+newStart);
  1159             if(interval > 1){
       
  1160                 if(((newStart + interval) * i) <= newEnd){
       
  1161                     model->insertRow(i);
       
  1162                     text = (this->*localeFunc)(!((newStart + interval)*i) ? newStart : (newStart + interval)*i);
       
  1163                 }
       
  1164                 else{
       
  1165                     break;
       
  1166                 }
       
  1167             }
       
  1168             else{
       
  1169                 model->insertRow(i);
       
  1170                 text = (this->*localeFunc)(i+newStart);
       
  1171             }
       
  1172 
  1135 #ifdef HB_TEXT_MEASUREMENT_UTILITY
  1173 #ifdef HB_TEXT_MEASUREMENT_UTILITY
  1136                 if ( localeFunc == &HbDateTimePickerPrivate::localeMonth &&
  1174             if ( localeFunc == &HbDateTimePickerPrivate::localeMonth &&
  1137                     HbFeatureManager::instance()->featureStatus( HbFeatureManager::TextMeasurement ) ) {
  1175                 HbFeatureManager::instance()->featureStatus( HbFeatureManager::TextMeasurement ) ) {
  1138                     text.append(QChar(LOC_TEST_START));
  1176                     text.append(QChar(LOC_TEST_START));
  1139                     text.append("qtl_datetimepicker_popup_month_sec");
  1177                     text.append("qtl_datetimepicker_popup_month_sec");
  1140                     text.append(QChar(LOC_TEST_END));
  1178                     text.append(QChar(LOC_TEST_END));
  1141                 }
  1179             }
  1142 #endif
  1180 #endif
       
  1181             QModelIndex index=model->index(i,0);
       
  1182             if(index.isValid()) {
  1143                 model->setData(index,text);//TODO:add a readable typedef
  1183                 model->setData(index,text);//TODO:add a readable typedef
  1144             }
  1184             }
       
  1185 
  1145         }
  1186         }
  1146         return;
  1187         return;
  1147     }
  1188     }
  1148 
  1189 
  1149     if(newStart < oldStart) {
  1190     if(newStart < oldStart) {
  1184     }
  1225     }
  1185     if(!mFrame) {
  1226     if(!mFrame) {
  1186         mFrame = q->style()->createPrimitive(HbStyle::P_DateTimePicker_frame,q);
  1227         mFrame = q->style()->createPrimitive(HbStyle::P_DateTimePicker_frame,q);
  1187         q->style()->setItemName(mFrame,"frame");
  1228         q->style()->setItemName(mFrame,"frame");
  1188     }
  1229     }
  1189     createDividers();
  1230 
  1190 }
  1231     if(!mHighlight){
  1191 
  1232         mHighlight = q->style()->createPrimitive(HbStyle::P_TumbleView_highlight,q);
  1192 void HbDateTimePickerPrivate::createDividers()
  1233         q->style()->setItemName(mHighlight,"highlight");
  1193 {
  1234     }
  1194     Q_Q(HbDateTimePicker);
       
  1195     
       
  1196     if( mParser.mSectionNodes.count() == mDividers.count() ){
       
  1197         return;
       
  1198     }
       
  1199 
       
  1200     if( mParser.mSectionNodes.count() < mDividers.count() ){
       
  1201         for( int i = mParser.mSectionNodes.count() - 1; i > mDividers.count(); i--)
       
  1202         {
       
  1203             QPointer<QGraphicsWidget> item = mDividers.at(i);
       
  1204             mDividers.removeAt(i);
       
  1205             delete item;
       
  1206         }
       
  1207 
       
  1208         return;
       
  1209     }
       
  1210     else if( mParser.mSectionNodes.count() > mDividers.count() ){
       
  1211 
       
  1212         for(int i = mDividers.count();i < mParser.mSectionNodes.count(); i++) { //TODO: optimally create when required
       
  1213             QGraphicsItem *item=q->style()->createPrimitive(HbStyle::P_DateTimePicker_separator, mContent);
       
  1214             Q_ASSERT(item->isWidget());
       
  1215             q->style()->setItemName(item,"separator");
       
  1216             mDividers.append(static_cast<QGraphicsWidget *>(item));
       
  1217         }
       
  1218     }
       
  1219 }
       
  1220 
       
  1221 void HbDateTimePickerPrivate::updateDividers()
       
  1222 {
       
  1223     /*Q_Q(HbDateTimePicker);
       
  1224     HbStyleOption option;
       
  1225     q->initStyleOption(&option);
       
  1226     for(int i=0;i<qMin(mDividers.count(),mDividerIndex);i++) {
       
  1227         q->style()->updatePrimitive((QGraphicsItem*)mDividers.at(i),HbStyle::P_DateTimePicker_separator,&option);
       
  1228     }*/
       
  1229 
       
  1230     //TODO: improve the divider addition and removal
       
  1231 
       
  1232     //using the style update primitive crashes. need to investigate why
       
  1233 
       
  1234 }
  1235 }
  1235 
  1236 
  1236 void HbDateTimePickerPrivate::_q_dayChanged(int index)
  1237 void HbDateTimePickerPrivate::_q_dayChanged(int index)
  1237 {
  1238 {
  1238 #ifdef HBDATETIMEPICKER_DEBUG
  1239 #ifdef HBDATETIMEPICKER_DEBUG
  1482 void HbDateTimePickerPrivate::_q_minutesChanged(int index)
  1483 void HbDateTimePickerPrivate::_q_minutesChanged(int index)
  1483 {
  1484 {
  1484 #ifdef HBDATETIMEPICKER_DEBUG
  1485 #ifdef HBDATETIMEPICKER_DEBUG
  1485     qDebug() << "_q_minutesChanged:" << index;
  1486     qDebug() << "_q_minutesChanged:" << index;
  1486 #endif
  1487 #endif
  1487     QTime newTime(mDateTime.time().hour(),mMinuteOffset+index,mDateTime.time().second());
  1488 	QTime newTime(mDateTime.time().hour(),mLocale.toInt(mMinuteModel->index(mMinuteOffset+index,0).data().toString()),mDateTime.time().second());
  1488     if(newTime.isValid()) {
  1489     if(newTime.isValid()) {
  1489         mDateTime.setTime(newTime);
  1490         mDateTime.setTime(newTime);
  1490     }
  1491     }
  1491     else {
  1492     else {
  1492 #ifdef HBDATETIMEPICKER_DEBUG
  1493 #ifdef HBDATETIMEPICKER_DEBUG