• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • 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 2007, 2010 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 "klocalizeddate.h"
00031 #include "kdialog.h"
00032 #include "kglobal.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     KLocalizedDate m_date;
00053     // Need to keep a QDate copy as the "const QDate &date() const;" method returns a reference
00054     // and returning m_date.date() creates a temporary leading to crashes.  Doh!
00055     QDate m_refDate;
00056 };
00057 
00058 
00059 KDateWidget::KDateWidget( QWidget *parent ) : QWidget( parent ), d( new KDateWidgetPrivate )
00060 {
00061     init( QDate::currentDate() );
00062 }
00063 
00064 KDateWidget::KDateWidget( const QDate &date, QWidget *parent )
00065             : QWidget( parent ), d( new KDateWidgetPrivate )
00066 {
00067     init( date );
00068 }
00069 
00070 void KDateWidget::init( const QDate &date )
00071 {
00072     QHBoxLayout *layout = new QHBoxLayout( this );
00073     layout->setMargin( 0 );
00074     layout->setSpacing( KDialog::spacingHint() );
00075 
00076     d->m_day = new KDateWidgetSpinBox( 1, 31, this );
00077     d->m_month = new KComboBox( this );
00078     d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
00079                                         calendar()->year( calendar()->latestValidDate() ), this );
00080 
00081     layout->addWidget( d->m_day );
00082     layout->addWidget( d->m_month );
00083     layout->addWidget( d->m_year );
00084 
00085     connect( d->m_day, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00086     connect( d->m_month, SIGNAL( activated( int ) ), this, SLOT( slotDateChanged() ) );
00087     connect( d->m_year, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00088 
00089     setFocusProxy(d->m_day);
00090     setFocusPolicy(Qt::StrongFocus);
00091 
00092     if ( calendar()->isValid( date ) ) {
00093         setDate( date );
00094     } else {
00095         setDate( QDate::currentDate() );
00096     }
00097 }
00098 
00099 KDateWidget::~KDateWidget()
00100 {
00101     delete d;
00102 }
00103 
00104 bool KDateWidget::setDate( const QDate &date )
00105 {
00106     if ( calendar()->isValid( date ) ) {
00107         bool dayBlocked = d->m_day->blockSignals( true );
00108         bool monthBlocked = d->m_month->blockSignals( true );
00109         bool yearBlocked = d->m_year->blockSignals( true );
00110 
00111         d->m_date.setDate( date );
00112         d->m_refDate = date;
00113 
00114         d->m_day->setMaximum( d->m_date.daysInMonth() );
00115         d->m_day->setValue( d->m_date.day() );
00116 
00117         d->m_month->setMaxVisibleItems( d->m_date.monthsInYear() );
00118         for ( int m = 1; m <= d->m_date.monthsInYear(); ++m ) {
00119             d->m_month->addItem( calendar()->monthName( m, d->m_date.year() ) );
00120         }
00121         d->m_month->setCurrentIndex( d->m_date.month() - 1 );
00122 
00123         d->m_year->setValue( d->m_date.year() );
00124 
00125         d->m_day->blockSignals( dayBlocked );
00126         d->m_month->blockSignals( monthBlocked );
00127         d->m_year->blockSignals( yearBlocked );
00128 
00129         emit changed( d->m_refDate );
00130         return true;
00131     }
00132     return false;
00133 }
00134 
00135 const QDate& KDateWidget::date() const
00136 {
00137     return d->m_refDate;
00138 }
00139 
00140 void KDateWidget::slotDateChanged( )
00141 {
00142     KLocalizedDate date;
00143     int y, m, day;
00144 
00145     y = d->m_year->value();
00146     y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
00147               calendar()->year( calendar()->latestValidDate() ) );
00148 
00149     date.setDate( y, 1, 1 );
00150     m = d->m_month->currentIndex() + 1;
00151     m = qMin( qMax( m, 1 ), date.monthsInYear() );
00152 
00153     date.setDate( y, m, 1 );
00154     day = d->m_day->value();
00155     day = qMin( qMax( day, 1 ), date.daysInMonth() );
00156 
00157     date.setDate( y, m, day );
00158     setDate( date.date() );
00159 }
00160 
00161 const KCalendarSystem *KDateWidget::calendar() const
00162 {
00163     return  d->m_date.calendar();
00164 }
00165 
00166 bool KDateWidget::setCalendar( KCalendarSystem *newCalendar )
00167 {
00168     QDate oldDate = date();
00169     d->m_date = KLocalizedDate( oldDate, newCalendar );
00170     return setDate( oldDate );
00171 }
00172 
00173 bool KDateWidget::setCalendar( const QString &newCalendarType )
00174 {
00175     return setCalendarSystem( KCalendarSystem::calendarSystemForCalendarType( newCalendarType ) );
00176 }
00177 
00178 bool KDateWidget::setCalendarSystem( KLocale::CalendarSystem newCalendarSystem )
00179 {
00180     d->m_date.setCalendarSystem( newCalendarSystem );
00181     return true;
00182 }
00183 
00184 #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
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
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