src/hbwidgets/widgets/hbdatetimepicker_p.cpp
changeset 23 e6ad4ef83b23
parent 21 4633027730f5
child 28 b7da29130b0e
equal deleted inserted replaced
21:4633027730f5 23:e6ad4ef83b23
   125                     const SectionNode sn = { hour, i - add, countRepeat(format, i, 2) };
   125                     const SectionNode sn = { hour, i - add, countRepeat(format, i, 2) };
   126                     newSectionNodes.append(sn);
   126                     newSectionNodes.append(sn);
   127                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   127                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   128                     i += sn.count - 1;
   128                     i += sn.count - 1;
   129                     index = i + 1;
   129                     index = i + 1;
       
   130                     if(newDisplay & hour) {
       
   131                         return false; // error in format
       
   132                     }
   130                     newDisplay |= hour;
   133                     newDisplay |= hour;
   131                 }
   134                 }
   132                 break;
   135                 break;
   133             case 'm':
   136             case 'm':
   134                 {
   137                 {
   135                     const SectionNode sn = { MinuteSection, i - add, countRepeat(format, i, 2) };
   138                     const SectionNode sn = { MinuteSection, i - add, countRepeat(format, i, 2) };
   136                     newSectionNodes.append(sn);
   139                     newSectionNodes.append(sn);
   137                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   140                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   138                     i += sn.count - 1;
   141                     i += sn.count - 1;
   139                     index = i + 1;
   142                     index = i + 1;
       
   143                     if(newDisplay & MinuteSection) {
       
   144                         return false; // error in format
       
   145                     }
   140                     newDisplay |= MinuteSection;
   146                     newDisplay |= MinuteSection;
   141                 }
   147                 }
   142                 break;
   148                 break;
   143             case 's':
   149             case 's':
   144                 {
   150                 {
   145                     const SectionNode sn = { SecondSection, i - add, countRepeat(format, i, 2) };
   151                     const SectionNode sn = { SecondSection, i - add, countRepeat(format, i, 2) };
   146                     newSectionNodes.append(sn);
   152                     newSectionNodes.append(sn);
   147                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   153                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   148                     i += sn.count - 1;
   154                     i += sn.count - 1;
   149                     index = i + 1;
   155                     index = i + 1;
       
   156                     if(newDisplay & SecondSection) {
       
   157                         return false; // error in format
       
   158                     }
   150                     newDisplay |= SecondSection;
   159                     newDisplay |= SecondSection;
   151                 }
   160                 }
   152                 break;
   161                 break;
   153 
   162 
   154             case 'z':
   163             case 'z':
   156                     const SectionNode sn = { MSecSection, i - add, countRepeat(format, i, 3) < 3 ? 1 : 3 };
   165                     const SectionNode sn = { MSecSection, i - add, countRepeat(format, i, 3) < 3 ? 1 : 3 };
   157                     newSectionNodes.append(sn);
   166                     newSectionNodes.append(sn);
   158                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   167                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   159                     i += sn.count - 1;
   168                     i += sn.count - 1;
   160                     index = i + 1;
   169                     index = i + 1;
       
   170                     if(newDisplay & MSecSection) {
       
   171                         return false; // error in format
       
   172                     }
   161                     newDisplay |= MSecSection;
   173                     newDisplay |= MSecSection;
   162                 }
   174                 }
   163                 break;
   175                 break;
   164             case 'A':
   176             case 'A':
   165             case 'a':
   177             case 'a':
   166                 {
   178                 {
   167                     const bool cap = (sect == 'A');
   179                     const bool cap = (sect == 'A');
   168                     const SectionNode sn = { AmPmSection, i - add, (cap ? 1 : 0) };
   180                     const SectionNode sn = { AmPmSection, i - add, (cap ? 1 : 0) };
   169                     newSectionNodes.append(sn);
   181                     newSectionNodes.append(sn);
   170                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   182                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
       
   183                     if(newDisplay & AmPmSection) {
       
   184                         return false; // error in format
       
   185                     }
   171                     newDisplay |= AmPmSection;
   186                     newDisplay |= AmPmSection;
   172                     if (i + 1 < format.size()
   187                     if (i + 1 < format.size()
   173                         && format.at(i+1) == (cap ? QLatin1Char('P') : QLatin1Char('p'))) {
   188                         && format.at(i+1) == (cap ? QLatin1Char('P') : QLatin1Char('p'))) {
   174                             ++i;
   189                             ++i;
   175                     }
   190                     }
   184                             i - add, repeat == 4 ? 4 : 2 };
   199                             i - add, repeat == 4 ? 4 : 2 };
   185                         newSectionNodes.append(sn);
   200                         newSectionNodes.append(sn);
   186                         appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   201                         appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   187                         i += sn.count - 1;
   202                         i += sn.count - 1;
   188                         index = i + 1;
   203                         index = i + 1;
       
   204                         if(newDisplay & sn.type) {
       
   205                             return false; //error in format
       
   206                         }
   189                         newDisplay |= sn.type;
   207                         newDisplay |= sn.type;
   190                     }
   208                     }
   191                 }
   209                 }
   192                 break;
   210                 break;
   193             case 'M':
   211             case 'M':
   195                     const SectionNode sn = { MonthSection, i - add, countRepeat(format, i, 4) };
   213                     const SectionNode sn = { MonthSection, i - add, countRepeat(format, i, 4) };
   196                     newSectionNodes.append(sn);
   214                     newSectionNodes.append(sn);
   197                     newSeparators.append(unquote(format.mid(index, i - index)));
   215                     newSeparators.append(unquote(format.mid(index, i - index)));
   198                     i += sn.count - 1;
   216                     i += sn.count - 1;
   199                     index = i + 1;
   217                     index = i + 1;
       
   218                     if(newDisplay & MonthSection) {
       
   219                         return false; // error in format
       
   220                     }
   200                     newDisplay |= MonthSection;
   221                     newDisplay |= MonthSection;
   201                 }
   222                 }
   202                 break;
   223                 break;
   203             case 'd':
   224             case 'd':
   204                 {
   225                 {
   206                     const SectionNode sn = { repeat >= 3 ? DayOfWeekSection : DaySection, i - add, repeat };
   227                     const SectionNode sn = { repeat >= 3 ? DayOfWeekSection : DaySection, i - add, repeat };
   207                     newSectionNodes.append(sn);
   228                     newSectionNodes.append(sn);
   208                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   229                     appendSeparator(&newSeparators, format, index, i - index, lastQuote);
   209                     i += sn.count - 1;
   230                     i += sn.count - 1;
   210                     index = i + 1;
   231                     index = i + 1;
       
   232                     if(newDisplay & (DayOfWeekSection | DaySection)) {
       
   233                         return false; // error in format, dd and ddd not yet supported in same format
       
   234                     }
   211                     newDisplay |= sn.type;
   235                     newDisplay |= sn.type;
   212                 }
   236                 }
   213                 break;
   237                 break;
   214 
   238 
   215             default:
   239             default:
   348 
   372 
   349     mDateTimeMode = dateTimeMode;
   373     mDateTimeMode = dateTimeMode;
   350     mIntervals[QDateTimeEdit::MinuteSection]=1;
   374     mIntervals[QDateTimeEdit::MinuteSection]=1;
   351 
   375 
   352     //read the format from locale
   376     //read the format from locale
   353     mFormat = localeDateTimeFormat(dateTimeMode);
   377     QString newFormat = localeDateTimeFormat(dateTimeMode);
   354 
   378 
   355     //parse the format and set the sections in order
   379     //parse the format and set the sections in order
   356     parseDisplayFormat(mFormat);
   380     if(isFormatValid(newFormat)) {
       
   381         mFormat=newFormat;
       
   382         processDisplaySections();
       
   383     }
   357 
   384 
   358     //create primitives
   385     //create primitives
   359     createPrimitives();
   386     createPrimitives();
   360 
   387 
   361     //recreate and rearrange depending on the format
   388     //recreate and rearrange depending on the format
   380 bool HbDateTimePickerPrivate::isFormatValid(const QString &newDisplayFormat)
   407 bool HbDateTimePickerPrivate::isFormatValid(const QString &newDisplayFormat)
   381 {
   408 {
   382     if(newDisplayFormat == mFormat) {
   409     if(newDisplayFormat == mFormat) {
   383         return false;
   410         return false;
   384     }
   411     }
   385     return true;
   412 
       
   413     return mParser.parseFormat(newDisplayFormat);
   386 }
   414 }
   387 
   415 
   388 /* 
   416 /* 
   389    this will reset the display sections and re add them in order
   417    this will reset the display sections and re add them in order
   390    mentioned in the display format passed. this also sets the mIs24HourFormat var.
   418    mentioned in the display format passed. this also sets the mIs24HourFormat var.
   391 */
   419 */
   392 void HbDateTimePickerPrivate::parseDisplayFormat(const QString &format)
   420 void HbDateTimePickerPrivate::processDisplaySections()
   393 {
   421 {
   394     if(mParser.parseFormat(format)) {
       
   395         for(int i=0;i<mParser.mSectionNodes.count();++i) {
   422         for(int i=0;i<mParser.mSectionNodes.count();++i) {
   396             switch(mParser.mSectionNodes[i].type) {
   423             switch(mParser.mSectionNodes[i].type) {
   397                 case HbDateTimeParser::DaySection:
   424                 case HbDateTimeParser::DaySection:
   398                 case HbDateTimeParser::DayOfWeekSection:
   425                 case HbDateTimeParser::DayOfWeekSection:
   399                     mDayFormat = QString(mParser.mSectionNodes[i].count,'d');
   426                     mDayFormat = QString(mParser.mSectionNodes[i].count,'d');
   434                 default:
   461                 default:
   435                     break;
   462                     break;
   436                     /*case HbDateTimeParser::DayOfWeekSection: not supported */
   463                     /*case HbDateTimeParser::DayOfWeekSection: not supported */
   437             }
   464             }
   438         }
   465         }
   439     }
       
   440 }
   466 }
   441 
   467 
   442 /*
   468 /*
   443    this is called whenever the setDisplayFormat changes.
   469    this is called whenever the setDisplayFormat changes.
   444    function deletes all tumbleviews which currently exist and
   470    function deletes all tumbleviews which currently exist and
   447 void HbDateTimePickerPrivate::rearrangeTumbleViews()
   473 void HbDateTimePickerPrivate::rearrangeTumbleViews()
   448 {                  
   474 {                  
   449     Q_Q(HbDateTimePicker);
   475     Q_Q(HbDateTimePicker);
   450 
   476 
   451     if(!(mParser.mDisplaySections & (HbDateTimeParser::YearSection|
   477     if(!(mParser.mDisplaySections & (HbDateTimeParser::YearSection|
   452 				     HbDateTimeParser::YearSection2Digits))) {
   478                      HbDateTimeParser::YearSection2Digits))) {
   453       deleteAndNull(mYearPicker);
   479       deleteAndNull(mYearPicker);
   454       mYearModel = 0;
   480       mYearModel = 0;
   455     }
   481     }
   456     if(!(mParser.mDisplaySections & HbDateTimeParser::MonthSection)) {
   482     if(!(mParser.mDisplaySections & HbDateTimeParser::MonthSection)) {
   457       deleteAndNull(mMonthPicker);
   483       deleteAndNull(mMonthPicker);
   460     if(!(mParser.mDisplaySections & HbDateTimeParser::DaySection)) {
   486     if(!(mParser.mDisplaySections & HbDateTimeParser::DaySection)) {
   461       deleteAndNull(mDayPicker);
   487       deleteAndNull(mDayPicker);
   462       mDayModel = 0;
   488       mDayModel = 0;
   463     }
   489     }
   464     if(!(mParser.mDisplaySections & (HbDateTimeParser::Hour12Section|
   490     if(!(mParser.mDisplaySections & (HbDateTimeParser::Hour12Section|
   465 				     HbDateTimeParser::Hour24Section))) {
   491                      HbDateTimeParser::Hour24Section))) {
   466       deleteAndNull(mHourPicker);
   492       deleteAndNull(mHourPicker);
   467       mHourModel = 0;
   493       mHourModel = 0;
   468     }
   494     }
   469     if(!(mParser.mDisplaySections & HbDateTimeParser::MinuteSection)) {
   495     if(!(mParser.mDisplaySections & HbDateTimeParser::MinuteSection)) {
   470       deleteAndNull(mMinutePicker);
   496       deleteAndNull(mMinutePicker);
  1063     if(newIndex > (end-start)) {
  1089     if(newIndex > (end-start)) {
  1064         newIndex = end-start;
  1090         newIndex = end-start;
  1065     }
  1091     }
  1066 
  1092 
  1067     resizeModel(mMinuteModel, 
  1093     resizeModel(mMinuteModel, 
  1068 		mMinuteOffset,mMinuteModel->index(mMinuteModel->rowCount() - 1).data().toInt(),
  1094         mMinuteOffset,mMinuteModel->index(mMinuteModel->rowCount() - 1).data().toInt(),
  1069 		start,end,
  1095         start,end,
  1070 		&HbDateTimePickerPrivate::localeMinute, 
  1096         &HbDateTimePickerPrivate::localeMinute, 
  1071 		mIntervals[QDateTimeEdit::MinuteSection]);
  1097         mIntervals[QDateTimeEdit::MinuteSection]);
  1072     mMinuteOffset = start;
  1098     mMinuteOffset = start;
  1073 
  1099 
  1074     //Select the nearest value when the range is set.
  1100     //Select the nearest value when the range is set.
  1075     int index = 0;
  1101     int index = 0;
  1076 
  1102 
  1224             qDebug() << "resizeModel: oldStart=" << oldStart
  1250             qDebug() << "resizeModel: oldStart=" << oldStart
  1225                      << " oldEnd=" << oldEnd << " newStart=" 
  1251                      << " oldEnd=" << oldEnd << " newStart=" 
  1226                      << newStart << " newEnd=" << newEnd
  1252                      << newStart << " newEnd=" << newEnd
  1227                      << " interval=" << interval;
  1253                      << " interval=" << interval;
  1228 #endif
  1254 #endif
  1229 	    bool b1=false,b2=false;
  1255         bool b1=false,b2=false;
  1230             int oldinterval=model->rowCount()>1 ? (model->index(1,0).data().toInt(&b1)-model->index(0,0).data().toInt(&b2)):0; 
  1256             int oldinterval=model->rowCount()>1 ? (model->index(1,0).data().toInt(&b1)-model->index(0,0).data().toInt(&b2)):0; 
  1231 #ifdef HBDATETIMEPICKER_DEBUG
  1257 #ifdef HBDATETIMEPICKER_DEBUG
  1232             qDebug() << "resizeModel:sameoldInterval=" << oldinterval;
  1258             qDebug() << "resizeModel:sameoldInterval=" << oldinterval;
  1233 #endif
  1259 #endif
  1234             if(b1 && b2 && (oldinterval == interval) && (newStart == oldStart)) {
  1260             if(b1 && b2 && (oldinterval == interval) && (newStart == oldStart)) {
  1258 #endif
  1284 #endif
  1259                     model->removeRows((model->rowCount()-count),count);
  1285                     model->removeRows((model->rowCount()-count),count);
  1260                 }
  1286                 }
  1261                 return;//optimizing inserts when interval is set
  1287                 return;//optimizing inserts when interval is set
  1262             } else {
  1288             } else {
  1263 	        if(b1 && b2) {		
  1289             if(b1 && b2) {      
  1264 		    model->removeRows(0,model->rowCount());
  1290             model->removeRows(0,model->rowCount());
  1265                     oldStart = oldEnd = 0;
  1291                     oldStart = oldEnd = 0;
  1266 	        }
  1292             }
  1267             }
  1293             }
  1268 
  1294 
  1269         if((model->rowCount() == 0) && (newEnd-newStart>=0)) {
  1295         if((model->rowCount() == 0) && (newEnd-newStart>=0)) {
  1270 #ifdef HBDATETIMEPICKER_DEBUG
  1296 #ifdef HBDATETIMEPICKER_DEBUG
  1271             qDebug() << "resizeModel:default addition inserting(0," << (newEnd-newStart+1)/interval << ")";
  1297             qDebug() << "resizeModel:default addition inserting(0," << (newEnd-newStart+1)/interval << ")";
  1625     bool *bOk = false;
  1651     bool *bOk = false;
  1626 #ifdef HBDATETIMEPICKER_DEBUG
  1652 #ifdef HBDATETIMEPICKER_DEBUG
  1627     qDebug() << "_q_minutesChanged:" << index;
  1653     qDebug() << "_q_minutesChanged:" << index;
  1628     qDebug() << mLocale.toInt(mMinuteModel->index(mMinuteOffset+index,0).data().toString(),bOk, 10);
  1654     qDebug() << mLocale.toInt(mMinuteModel->index(mMinuteOffset+index,0).data().toString(),bOk, 10);
  1629 #endif
  1655 #endif
  1630 	QTime newTime(mDateTime.time().hour(),mLocale.toInt(mMinuteModel->index(index,0).data().toString(),bOk, 10),mDateTime.time().second());
  1656     QTime newTime(mDateTime.time().hour(),mLocale.toInt(mMinuteModel->index(index,0).data().toString(),bOk, 10),mDateTime.time().second());
  1631     if(newTime.isValid()) {
  1657     if(newTime.isValid()) {
  1632         mDateTime.setTime(newTime);
  1658         mDateTime.setTime(newTime);
  1633     }
  1659     }
  1634     else {
  1660     else {
  1635 #ifdef HBDATETIMEPICKER_DEBUG
  1661 #ifdef HBDATETIMEPICKER_DEBUG