• Skip to content
  • Skip to link menu
KDE 4.2 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::currentDate() );
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     d->m_month->setMaxVisibleItems( 12 );
00092 
00093     for ( int i = 1; ; ++i ) {
00094         const QString str = calendar()->monthName( i, calendar()->year( initDate ) );
00095         if ( str.isEmpty() ) {
00096             break;
00097         }
00098         d->m_month->addItem( str );
00099     }
00100 
00101     d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
00102                                         calendar()->year( calendar()->latestValidDate() ), this );
00103     layout->addWidget( d->m_day );
00104     layout->addWidget( d->m_month );
00105     layout->addWidget( d->m_year );
00106 
00107     connect( d->m_day, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00108     connect( d->m_month, SIGNAL( activated( int ) ), this, SLOT( slotDateChanged() ) );
00109     connect( d->m_year, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00110 
00111     d->m_dat = initDate;
00112 }
00113 
00114 KDateWidget::~KDateWidget()
00115 {
00116     delete d;
00117 }
00118 
00119 bool KDateWidget::setDate( const QDate &date )
00120 {
00121     if ( calendar()->isValid( date ) ) {
00122         bool dayBlocked = d->m_day->blockSignals( true );
00123         bool monthBlocked = d->m_month->blockSignals( true );
00124         bool yearBlocked = d->m_year->blockSignals( true );
00125 
00126         d->m_day->setMaximum( calendar()->daysInMonth( date ) );
00127         d->m_day->setValue( calendar()->day( date ) );
00128         d->m_month->setCurrentIndex( calendar()->month( date ) - 1 );
00129         d->m_year->setValue( calendar()->year( date ) );
00130 
00131         d->m_day->blockSignals( dayBlocked );
00132         d->m_month->blockSignals( monthBlocked );
00133         d->m_year->blockSignals( yearBlocked );
00134 
00135         d->m_dat = date;
00136         emit changed( d->m_dat );
00137         return true;
00138     }
00139     return false;
00140 }
00141 
00142 const QDate& KDateWidget::date() const
00143 {
00144     return d->m_dat;
00145 }
00146 
00147 void KDateWidget::slotDateChanged( )
00148 {
00149     QDate date;
00150     int y, m, day;
00151 
00152     y = d->m_year->value();
00153     y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
00154               calendar()->year( calendar()->latestValidDate() ) );
00155 
00156     calendar()->setYMD( date, y, 1, 1 );
00157     m = d->m_month->currentIndex() + 1;
00158     m = qMin( qMax( m, 1 ), calendar()->monthsInYear( date ) );
00159 
00160     calendar()->setYMD( date, y, m, 1 );
00161     day = d->m_day->value();
00162     day = qMin( qMax( day, 1 ), calendar()->daysInMonth( date ) );
00163 
00164     calendar()->setYMD( date, y, m, day );
00165     setDate( date );
00166 }
00167 
00168 const KCalendarSystem *KDateWidget::calendar() const
00169 {
00170     if ( d->m_cal ) {
00171         return d->m_cal;
00172     }
00173 
00174     return  KGlobal::locale()->calendar();
00175 }
00176 
00177 bool KDateWidget::setCalendar( KCalendarSystem *calendar )
00178 {
00179     d->m_cal = calendar;
00180     return true;
00181 }
00182 
00183 bool KDateWidget::setCalendar( const QString &calendarType )
00184 {
00185     d->m_cal = KCalendarSystem::create( calendarType );
00186     return d->m_cal;
00187 }
00188 
00189 
00190 #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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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