[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH] kdelibs/kdeui/kdatepicker.cpp
From:       John Layt <johnlayt () yahoo ! com ! au>
Date:       2007-11-08 0:01:23
Message-ID: 200711080001.23711.johnlayt () yahoo ! com ! au
[Download RAW message or body]

Have this one floating around from way back...


Problem: In a KDatePicker (e.g. run kdeui/tests/kdatepicktest), select year 
9999 or -4713 (i.e. max/min valid years), and it will infinite loop inside 
fillWeeksCombo().

Solution: Add checks to ensure loop start, end, and incremented dates are 
valid.


Problem: In fillWeeksCombo() if first week of year is week 53 of previous year 
(e.g. 2010, 9999), then all the weeks in the combo get incorrectly marked 
with '*' as being in a different year.

Solution: Re-initiaise the year variable each time through the loop.


OK to commit?

Cheers!

John.

--


["kdatepicker-fillweekscombo.diff" (text/x-diff)]

Index: widgets/kdatepicker.cpp
===================================================================
--- widgets/kdatepicker.cpp	(revision 733956)
+++ widgets/kdatepicker.cpp	(working copy)
@@ -157,25 +157,37 @@
 
     int thisYear = q->calendar()->year( q->date() );
 
+    // If the date set is in the same year as the latest valid date, then the last \
day of +    // that year may not be a valid date, so start from the latest valid date \
                instead.
     // Note we need this roundabout way to calculate lastDayOfYear instead of simply \
taking  // 1st day of next year - 1 day, as next year may not be a valid date
-    // JPL still leaves open what to do if we start or end part way through year.
     QDate lastDayOfYear;
-    q->calendar()->setYMD( lastDayOfYear, thisYear, q->calendar()->monthsInYear( \
                q->date() ), 1 );
-    q->calendar()->setYMD( lastDayOfYear, thisYear, q->calendar()->monthsInYear( \
                q->date() ),
-                           q->calendar()->daysInMonth( lastDayOfYear ) );
+    if ( ! q->calendar()->setYMD( lastDayOfYear, thisYear,
+                                  q->calendar()->monthsInYear( q->date() ), 1 ) ||
+         ! q->calendar()->setYMD( lastDayOfYear, thisYear,
+                                  q->calendar()->monthsInYear( q->date() ),
+                                  q->calendar()->daysInMonth( lastDayOfYear ) ) ) {
+        lastDayOfYear = q->calendar()->latestValidDate();
+    }
 
-    // JPL this won't always be a valid date!
+    // If the date set is in the same year as the earliest valid date, then the 1st \
of +    // that year may not be a valid date, so start from the first valid date \
instead +    // In particular covers the case of Gregorian where 1/1/-4713 is not a \
valid QDate  QDate day;
-    q->calendar()->setYMD( day, thisYear, 1, 1 );
+    if ( ! q->calendar()->setYMD( day, thisYear, 1, 1 ) ) {
+        day = q->calendar()->earliestValidDate();
+    }
 
     selectWeek->clear();
 
-    for ( ; day <= lastDayOfYear ; day = q->calendar()->addDays( day, \
                q->calendar()->daysInWeek( day ) ) ) {
-        QString week = i18n( "Week %1", q->calendar()->weekNumber( day, &thisYear ) \
); +    for ( ; q->calendar()->isValid( day ) && day <= lastDayOfYear;
+            day = q->calendar()->addDays( day, q->calendar()->daysInWeek( day ) ) ) \
{  
+        int weekYear = thisYear;
+        QString week = i18n( "Week %1", q->calendar()->weekNumber( day, &weekYear ) \
); +
         // show that this is a week from a different year
-        if ( q->calendar()->year( day ) != thisYear ) {
+        if ( weekYear != thisYear ) {
             week += '*';
         }
 
@@ -360,8 +372,13 @@
 
     // calculate the item num in the week combo box; normalize selected day so as if \
1.1. is the first day of the week  QDate firstDay;
-    // JPL may not always be valid!
-    calendar()->setYMD( firstDay, calendar()->year( date_ ), 1, 1 );
+    // If the date set is in the same year as the earliest valid date, then the 1st \
of +    // that year may not be a valid date, so start from the first valid date \
instead +    // In particular covers the case of Gregorian where 1/1/-4713 is not a \
valid QDate +    QDate day;
+    if ( ! calendar()->setYMD( firstDay, calendar()->year( date_ ), 1, 1 ) ) {
+        firstDay = calendar()->earliestValidDate();
+    }
     d->selectWeek->setCurrentIndex( ( calendar()->dayOfYear( date_ ) + \
calendar()->dayOfWeek( firstDay ) - 2 ) /  calendar()->daysInWeek( date_ ) );
     d->selectYear->setText( calendar()->yearString( date_, \
KCalendarSystem::LongFormat ) );


Send instant messages to your online friends http://au.messenger.yahoo.com 

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic