[Kde-bindings] KDE/kdelibs/kdeui/widgets
John Layt
john at layt.net
Fri Dec 3 22:46:12 UTC 2010
SVN commit 1203421 by jlayt:
Update KDateWidget to use KLocalizedDate internally to simplify code.
Add api to setCalendar using new KLoclae::CalendarSystem enum
Also a couple of bug fixes I noticed.
CCMAIL: kde-bindings at kde.org
M +47 -56 kdatewidget.cpp
M +13 -0 kdatewidget.h
--- trunk/KDE/kdelibs/kdeui/widgets/kdatewidget.cpp #1203420:1203421
@@ -1,6 +1,6 @@
/* This file is part of the KDE libraries
Copyright (C) 2001 Waldo Bastian (bastian at kde.org)
- Copyright (c) 2007 John Layt <john at layt.net>
+ Copyright 2007, 2010 John Layt <john at layt.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
@@ -27,9 +27,9 @@
#include <kcombobox.h>
#include "kcalendarsystem.h"
+#include "klocalizeddate.h"
#include "kdialog.h"
#include "kglobal.h"
-#include "klocale.h"
class KDateWidgetSpinBox : public QSpinBox
@@ -49,57 +49,35 @@
KDateWidgetSpinBox *m_day;
KComboBox *m_month;
KDateWidgetSpinBox *m_year;
- QDate m_dat;
- KCalendarSystem *m_cal;
+ KLocalizedDate m_date;
+ // Need to keep a QDate copy as the "const QDate &date() const;" method returns a reference
+ // and returning m_date.date() creates a temporary leading to crashes. Doh!
+ QDate m_refDate;
};
KDateWidget::KDateWidget( QWidget *parent ) : QWidget( parent ), d( new KDateWidgetPrivate )
{
- init( QDate() );
- setDate( QDate::currentDate() );
+ init( QDate::currentDate() );
}
KDateWidget::KDateWidget( const QDate &date, QWidget *parent )
: QWidget( parent ), d( new KDateWidgetPrivate )
{
init( date );
- if ( ! setDate( date ) ) {
- setDate( QDate::currentDate() );
}
-}
void KDateWidget::init( const QDate &date )
{
- //set calendar system to default, i.e. global
- setCalendar();
- //make sure date is valid in calendar system
- QDate initDate;
- if ( calendar()->isValid( date ) ) {
- initDate = date;
- } else {
- initDate = QDate::currentDate();
- }
-
QHBoxLayout *layout = new QHBoxLayout( this );
layout->setMargin( 0 );
layout->setSpacing( KDialog::spacingHint() );
- // set the maximum day value in the day field, so that the day can
- // be editted when the KDateWidget is constructed with an empty date
+
d->m_day = new KDateWidgetSpinBox( 1, 31, this );
d->m_month = new KComboBox( this );
- d->m_month->setMaxVisibleItems( 12 );
-
- for ( int i = 1; ; ++i ) {
- const QString str = calendar()->monthName( i, calendar()->year( initDate ) );
- if ( str.isEmpty() ) {
- break;
- }
- d->m_month->addItem( str );
- }
-
d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
calendar()->year( calendar()->latestValidDate() ), this );
+
layout->addWidget( d->m_day );
layout->addWidget( d->m_month );
layout->addWidget( d->m_year );
@@ -111,8 +89,12 @@
setFocusProxy(d->m_day);
setFocusPolicy(Qt::StrongFocus);
- d->m_dat = initDate;
+ if ( calendar()->isValid( date ) ) {
+ setDate( date );
+ } else {
+ setDate( QDate::currentDate() );
}
+}
KDateWidget::~KDateWidget()
{
@@ -126,17 +108,25 @@
bool monthBlocked = d->m_month->blockSignals( true );
bool yearBlocked = d->m_year->blockSignals( true );
- d->m_day->setMaximum( calendar()->daysInMonth( date ) );
- d->m_day->setValue( calendar()->day( date ) );
- d->m_month->setCurrentIndex( calendar()->month( date ) - 1 );
- d->m_year->setValue( calendar()->year( date ) );
+ d->m_date.setDate( date );
+ d->m_refDate = date;
+ d->m_day->setMaximum( d->m_date.daysInMonth() );
+ d->m_day->setValue( d->m_date.day() );
+
+ d->m_month->setMaxVisibleItems( d->m_date.monthsInYear() );
+ for ( int m = 1; m <= d->m_date.monthsInYear(); ++m ) {
+ d->m_month->addItem( calendar()->monthName( m, d->m_date.year() ) );
+ }
+ d->m_month->setCurrentIndex( d->m_date.month() - 1 );
+
+ d->m_year->setValue( d->m_date.year() );
+
d->m_day->blockSignals( dayBlocked );
d->m_month->blockSignals( monthBlocked );
d->m_year->blockSignals( yearBlocked );
- d->m_dat = date;
- emit changed( d->m_dat );
+ emit changed( d->m_refDate );
return true;
}
return false;
@@ -144,50 +134,51 @@
const QDate& KDateWidget::date() const
{
- return d->m_dat;
+ return d->m_refDate;
}
void KDateWidget::slotDateChanged( )
{
- QDate date;
+ KLocalizedDate date;
int y, m, day;
y = d->m_year->value();
y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
calendar()->year( calendar()->latestValidDate() ) );
- calendar()->setYMD( date, y, 1, 1 );
+ date.setDate( y, 1, 1 );
m = d->m_month->currentIndex() + 1;
- m = qMin( qMax( m, 1 ), calendar()->monthsInYear( date ) );
+ m = qMin( qMax( m, 1 ), date.monthsInYear() );
- calendar()->setYMD( date, y, m, 1 );
+ date.setDate( y, m, 1 );
day = d->m_day->value();
- day = qMin( qMax( day, 1 ), calendar()->daysInMonth( date ) );
+ day = qMin( qMax( day, 1 ), date.daysInMonth() );
- calendar()->setYMD( date, y, m, day );
- setDate( date );
+ date.setDate( y, m, day );
+ setDate( date.date() );
}
const KCalendarSystem *KDateWidget::calendar() const
{
- if ( d->m_cal ) {
- return d->m_cal;
+ return d->m_date.calendar();
}
- return KGlobal::locale()->calendar();
+bool KDateWidget::setCalendar( KCalendarSystem *newCalendar )
+{
+ QDate oldDate = date();
+ d->m_date = KLocalizedDate( oldDate, newCalendar );
+ return setDate( oldDate );
}
-bool KDateWidget::setCalendar( KCalendarSystem *calendar )
+bool KDateWidget::setCalendar( const QString &newCalendarType )
{
- d->m_cal = calendar;
- return true;
+ return setCalendarSystem( KCalendarSystem::calendarSystemForCalendarType( newCalendarType ) );
}
-bool KDateWidget::setCalendar( const QString &calendarType )
+bool KDateWidget::setCalendarSystem( KLocale::CalendarSystem newCalendarSystem )
{
- d->m_cal = KCalendarSystem::create( calendarType );
- return d->m_cal;
+ d->m_date.setCalendarSystem( newCalendarSystem );
+ return true;
}
-
#include "kdatewidget.moc"
--- trunk/KDE/kdelibs/kdeui/widgets/kdatewidget.h #1203420:1203421
@@ -24,6 +24,8 @@
#include <QtGui/QWidget>
+#include "klocale.h"
+
class KCalendarSystem;
class QDate;
@@ -61,6 +63,7 @@
*/
virtual ~KDateWidget();
+ // KDE5 remove const &
/**
* Returns the currently selected date.
*/
@@ -98,6 +101,16 @@
*/
bool setCalendar( const QString &calendarType );
+ /**
+ * @since 4.6
+ *
+ * Changes the calendar system to use. Will always use global locale.
+ *
+ * @param calendarSystem the calendar system to use
+ * @return @c true if the calendar system was successfully set, @c false otherwise
+ */
+ bool setCalendarSystem( KLocale::CalendarSystem calendarSystem );
+
Q_SIGNALS:
/**
* Emitted whenever the date of the widget
More information about the Kde-bindings
mailing list