]> Git trees. - libqmvoc.git/commitdiff
Fixing crash condition in ExtDateWidget. If initialized without a date
authorJason Harris <kstars@30doradus.org>
Tue, 1 Jun 2004 01:33:18 +0000 (01:33 +0000)
committerJason Harris <kstars@30doradus.org>
Tue, 1 Jun 2004 01:33:18 +0000 (01:33 +0000)
argument, it was initialized to an invalid date, which caused a crash in the
function ExtDate::daysInMonth().  Two fixes make the widget much more robust:

+ ExtDate::daysInMonth() and ExtDate::leapYear() now check the validity of the
date first, and return sensible default values if the date is invalid.

+ the default ctor ExtDateWidget() now initializes to the current date, rather
than an invalid date.

This fixes the crash in the ScriptBuilder tool.

CCMAIL: kstars-devel@kde.org

svn path=/trunk/kdeedu/libkdeedu/; revision=316636

extdate/extdatetime.cpp
extdate/extdatewidget.cpp
extdate/extdatewidget.h
extdate/testwidget.cpp
extdate/testwidget.h

index 686fd3bc6596ef03ce3a04a063b2ebaea6c613d3..bf7d2cdba4816314effaa8717aa893407b705aa6 100644 (file)
@@ -290,12 +290,19 @@ int ExtDate::dayOfYear() const
 
 int ExtDate::daysInMonth() const
 {
-       int     a_month = month();
-       return (a_month == 2) ? (leapYear(year()) ? 29 : 28) : m_monthLength[a_month-1] ;
+       if ( isValid() ) {
+         int m = month();
+         int d = m_monthLength[m-1];
+         if (m==2 && leapYear(year())) d++;
+         return d;
+       } else {
+         return 31;
+       }
 }
 
 int ExtDate::daysInYear() const
 {
+       if ( ! isValid() ) return 365;
        return (leapYear(year()) ? 366 : 365);
 }
 
index 61627f2ff36d993537f8998725f7973e1fc6cd39..7a869dd9e8c0ed9d5c789f8894226d1200ba9c18 100644 (file)
@@ -56,7 +56,7 @@ public:
 ExtDateWidget::ExtDateWidget( QWidget *parent, const char *name )
   : QWidget( parent, name )
 {
-  init(ExtDate());
+  init(ExtDate::currentDate());
   setDate(ExtDate());
 }
 
index 851a372619c75f05c52a5f66246caad6826620cc..e23b28f84990cddc396cb8d13805da7d9c6f6c9c 100644 (file)
@@ -39,7 +39,7 @@ class ExtDateWidget : public QWidget
 
 public:
   /**
-   * Constructs a date selection widget.
+   * Constructs a date selection widget, initialized to the current CPU date.
    */
   ExtDateWidget( QWidget *parent=0, const char *name=0 );
 
index 8ecc7bb6c68a7da358eb709e93175fa45e84ced0..5806c348f1bcbafee044e6a6c92b5bd4e20b560d 100644 (file)
@@ -1,9 +1,11 @@
 #include <kdatepicker.h>
+#include <kdatewidget.h>
 #include <klineedit.h>
 #include <qlayout.h>
 #include <qlabel.h>
 
 #include "extdatepicker.h"
+#include "extdatewidget.h"
 #include "testwidget.h"
 
 TestWidget::TestWidget( QWidget *p=0, const char *name=0 ) : KMainWindow( p, name ) {
@@ -20,12 +22,17 @@ TestWidget::TestWidget( QWidget *p=0, const char *name=0 ) : KMainWindow( p, nam
        edpEdit = new KLineEdit(w);
        edpEdit->setReadOnly( TRUE );
 
+       kdw = new KDateWidget( QDate::currentDate(), w );
+       edw = new ExtDateWidget( ExtDate::currentDate(), w );
+
        glay->addWidget( kdpLabel, 0, 0 );
        glay->addWidget( edpLabel, 0, 1 );
        glay->addWidget( kdp, 1, 0 );
        glay->addWidget( edp, 1, 1 );
        glay->addWidget( kdpEdit, 2, 0 );
        glay->addWidget( edpEdit, 2, 1 );
+       glay->addWidget( kdw, 3, 0 );
+       glay->addWidget( edw, 3, 1 );
 
        setCentralWidget(w);
 
index 0b97f5af8e84ced83a786d037cc2cc7135692b6d..4da7611c1cb01d691c0eca93eb55dd37b707156d 100644 (file)
@@ -5,7 +5,9 @@
 
 class KDatePicker;
 class KLineEdit;
+class KDateWidget;
 class ExtDatePicker;
+class ExtDateWidget;
 class QGridLayout;
 class QDate;
 class ExtDate;
@@ -24,6 +26,8 @@ class TestWidget : public KMainWindow {
                QGridLayout *glay;
                KDatePicker *kdp;
                ExtDatePicker *edp;
+               ExtDateWidget *edw;
+               KDateWidget *kdw;
                KLineEdit *kdpEdit, *edpEdit;
 };