• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kdatewidget.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 2001 Waldo Bastian (bastian@kde.org)
00003     Copyright (c) 2007 John Layt <john@layt.net>
00004  
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008  
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013  
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kdatewidget.h"
00021 
00022 #include <QtCore/QDate>
00023 #include <QtGui/QLayout>
00024 #include <QtGui/QLineEdit>
00025 #include <QtGui/QDoubleSpinBox>
00026 
00027 #include <kcombobox.h>
00028 
00029 #include "kcalendarsystem.h"
00030 #include "kdialog.h"
00031 #include "kglobal.h"
00032 #include "klocale.h"
00033 
00034 
00035 class KDateWidgetSpinBox : public QSpinBox
00036 {
00037 public:
00038     KDateWidgetSpinBox( int min, int max, QWidget *parent ) : QSpinBox( parent )
00039     {
00040         setRange( qMin( min, max ), qMax( min, max ) );
00041         setSingleStep( 1 );
00042         lineEdit()->setAlignment( Qt::AlignRight );
00043     }
00044 };
00045 
00046 class KDateWidget::KDateWidgetPrivate
00047 {
00048 public:
00049     KDateWidgetSpinBox *m_day;
00050     KComboBox *m_month;
00051     KDateWidgetSpinBox *m_year;
00052     QDate m_dat;
00053     KCalendarSystem *m_cal;
00054 };
00055 
00056 
00057 KDateWidget::KDateWidget( QWidget *parent ) : QWidget( parent ), d( new KDateWidgetPrivate )
00058 {
00059     init( QDate() );
00060     setDate( QDate() );
00061 }
00062 
00063 KDateWidget::KDateWidget( const QDate &date, QWidget *parent )
00064             : QWidget( parent ), d( new KDateWidgetPrivate )
00065 {
00066     init( date );
00067     if ( ! setDate( date ) ) {
00068         setDate( QDate::currentDate() );
00069     }
00070 }
00071 
00072 void KDateWidget::init( const QDate &date )
00073 {
00074     //set calendar system to default, i.e. global
00075     setCalendar();
00076     //make sure date is valid in calendar system
00077     QDate initDate;
00078     if ( calendar()->isValid( date ) ) {
00079         initDate = date;
00080     } else {
00081         initDate = QDate::currentDate();
00082     }
00083 
00084     QHBoxLayout *layout = new QHBoxLayout( this );
00085     layout->setMargin( 0 );
00086     layout->setSpacing( KDialog::spacingHint() );
00087     // set the maximum day value in the day field, so that the day can
00088     // be editted when the KDateWidget is constructed with an empty date
00089     d->m_day = new KDateWidgetSpinBox( 1, 31, this );
00090     d->m_month = new KComboBox( this );
00091 
00092     for ( int i = 1; ; ++i ) {
00093         const QString str = calendar()->monthName( i, calendar()->year( initDate ) );
00094         if ( str.isEmpty() ) {
00095             break;
00096         }
00097         d->m_month->addItem( str );
00098     }
00099 
00100     d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
00101                                         calendar()->year( calendar()->latestValidDate() ), this );
00102     layout->addWidget( d->m_day );
00103     layout->addWidget( d->m_month );
00104     layout->addWidget( d->m_year );
00105 
00106     connect( d->m_day, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00107     connect( d->m_month, SIGNAL( activated( int ) ), this, SLOT( slotDateChanged() ) );
00108     connect( d->m_year, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00109 }
00110 
00111 KDateWidget::~KDateWidget()
00112 {
00113     delete d;
00114 }
00115 
00116 bool KDateWidget::setDate( const QDate &date )
00117 {
00118     if ( calendar()->isValid( date ) ) {
00119         bool dayBlocked = d->m_day->blockSignals( true );
00120         bool monthBlocked = d->m_month->blockSignals( true );
00121         bool yearBlocked = d->m_year->blockSignals( true );
00122 
00123         d->m_day->setMaximum( calendar()->daysInMonth( date ) );
00124         d->m_day->setValue( calendar()->day( date ) );
00125         d->m_month->setCurrentIndex( calendar()->month( date ) - 1 );
00126         d->m_year->setValue( calendar()->year( date ) );
00127 
00128         d->m_day->blockSignals( dayBlocked );
00129         d->m_month->blockSignals( monthBlocked );
00130         d->m_year->blockSignals( yearBlocked );
00131 
00132         d->m_dat = date;
00133         emit changed( d->m_dat );
00134         return true;
00135     }
00136     return false;
00137 }
00138 
00139 const QDate& KDateWidget::date() const
00140 {
00141     return d->m_dat;
00142 }
00143 
00144 void KDateWidget::slotDateChanged( )
00145 {
00146     QDate date;
00147     int y, m, day;
00148 
00149     y = d->m_year->value();
00150     y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
00151               calendar()->year( calendar()->latestValidDate() ) );
00152 
00153     calendar()->setYMD( date, y, 1, 1 );
00154     m = d->m_month->currentIndex() + 1;
00155     m = qMin( qMax( m, 1 ), calendar()->monthsInYear( date ) );
00156 
00157     calendar()->setYMD( date, y, m, 1 );
00158     day = d->m_day->value();
00159     day = qMin( qMax( day, 1 ), calendar()->daysInMonth( date ) );
00160 
00161     calendar()->setYMD( date, y, m, day );
00162     setDate( date );
00163 }
00164 
00165 const KCalendarSystem *KDateWidget::calendar() const
00166 {
00167     if ( d->m_cal ) {
00168         return d->m_cal;
00169     }
00170 
00171     return  KGlobal::locale()->calendar();
00172 }
00173 
00174 bool KDateWidget::setCalendar( KCalendarSystem *calendar )
00175 {
00176     d->m_cal = calendar;
00177     return true;
00178 }
00179 
00180 bool KDateWidget::setCalendar( const QString &calendarType )
00181 {
00182     d->m_cal = KCalendarSystem::create( calendarType );
00183     return d->m_cal;
00184 }
00185 
00186 
00187 #include "kdatewidget.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal