• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kdatewidget.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2001 Waldo Bastian (bastian@kde.org)
3 Copyright 2007, 2010 John Layt <john@layt.net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kdatewidget.h"
21
22#include <QtCore/QDate>
23#include <QtGui/QLayout>
24#include <QtGui/QLineEdit>
25#include <QtGui/QDoubleSpinBox>
26
27#include <kcombobox.h>
28
29#include "kcalendarsystem.h"
30#include "klocalizeddate.h"
31#include "kdialog.h"
32#include "kglobal.h"
33
34
35class KDateWidgetSpinBox : public QSpinBox
36{
37public:
38 KDateWidgetSpinBox( int min, int max, QWidget *parent ) : QSpinBox( parent )
39 {
40 setRange( qMin( min, max ), qMax( min, max ) );
41 setSingleStep( 1 );
42 lineEdit()->setAlignment( Qt::AlignRight );
43 }
44};
45
46class KDateWidget::KDateWidgetPrivate
47{
48public:
49 KDateWidgetSpinBox *m_day;
50 KComboBox *m_month;
51 KDateWidgetSpinBox *m_year;
52 KLocalizedDate m_date;
53 // Need to keep a QDate copy as the "const QDate &date() const;" method returns a reference
54 // and returning m_date.date() creates a temporary leading to crashes. Doh!
55 QDate m_refDate;
56};
57
58
59KDateWidget::KDateWidget( QWidget *parent ) : QWidget( parent ), d( new KDateWidgetPrivate )
60{
61 init( QDate::currentDate() );
62}
63
64KDateWidget::KDateWidget( const QDate &date, QWidget *parent )
65 : QWidget( parent ), d( new KDateWidgetPrivate )
66{
67 init( date );
68}
69
70void KDateWidget::init( const QDate &date )
71{
72 QHBoxLayout *layout = new QHBoxLayout( this );
73 layout->setMargin( 0 );
74 layout->setSpacing( KDialog::spacingHint() );
75
76 d->m_day = new KDateWidgetSpinBox( 1, 31, this );
77 d->m_month = new KComboBox( this );
78 d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
79 calendar()->year( calendar()->latestValidDate() ), this );
80
81 layout->addWidget( d->m_day );
82 layout->addWidget( d->m_month );
83 layout->addWidget( d->m_year );
84
85 connect( d->m_day, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()) );
86 connect( d->m_month, SIGNAL(activated(int)), this, SLOT(slotDateChanged()) );
87 connect( d->m_year, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()) );
88
89 setFocusProxy(d->m_day);
90 setFocusPolicy(Qt::StrongFocus);
91
92 if ( calendar()->isValid( date ) ) {
93 setDate( date );
94 } else {
95 setDate( QDate::currentDate() );
96 }
97}
98
99KDateWidget::~KDateWidget()
100{
101 delete d;
102}
103
104bool KDateWidget::setDate( const QDate &date )
105{
106 if ( calendar()->isValid( date ) ) {
107 bool dayBlocked = d->m_day->blockSignals( true );
108 bool monthBlocked = d->m_month->blockSignals( true );
109 bool yearBlocked = d->m_year->blockSignals( true );
110
111 d->m_date.setDate( date );
112 d->m_refDate = date;
113
114 d->m_day->setMaximum( d->m_date.daysInMonth() );
115 d->m_day->setValue( d->m_date.day() );
116
117 d->m_month->clear();
118 d->m_month->setMaxVisibleItems( d->m_date.monthsInYear() );
119 for ( int m = 1; m <= d->m_date.monthsInYear(); ++m ) {
120 d->m_month->addItem( calendar()->monthName( m, d->m_date.year() ) );
121 }
122 d->m_month->setCurrentIndex( d->m_date.month() - 1 );
123
124 d->m_year->setValue( d->m_date.year() );
125
126 d->m_day->blockSignals( dayBlocked );
127 d->m_month->blockSignals( monthBlocked );
128 d->m_year->blockSignals( yearBlocked );
129
130 emit changed( d->m_refDate );
131 return true;
132 }
133 return false;
134}
135
136const QDate& KDateWidget::date() const
137{
138 return d->m_refDate;
139}
140
141void KDateWidget::slotDateChanged( )
142{
143 KLocalizedDate date;
144 int y, m, day;
145
146 y = d->m_year->value();
147 y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
148 calendar()->year( calendar()->latestValidDate() ) );
149
150 date.setDate( y, 1, 1 );
151 m = d->m_month->currentIndex() + 1;
152 m = qMin( qMax( m, 1 ), date.monthsInYear() );
153
154 date.setDate( y, m, 1 );
155 day = d->m_day->value();
156 day = qMin( qMax( day, 1 ), date.daysInMonth() );
157
158 date.setDate( y, m, day );
159 setDate( date.date() );
160}
161
162const KCalendarSystem *KDateWidget::calendar() const
163{
164 return d->m_date.calendar();
165}
166
167bool KDateWidget::setCalendar( KCalendarSystem *newCalendar )
168{
169 QDate oldDate = date();
170 d->m_date = KLocalizedDate( oldDate, newCalendar );
171 return setDate( oldDate );
172}
173
174bool KDateWidget::setCalendar( const QString &newCalendarType )
175{
176 return setCalendarSystem( KCalendarSystem::calendarSystem( newCalendarType ) );
177}
178
179bool KDateWidget::setCalendarSystem( KLocale::CalendarSystem newCalendarSystem )
180{
181 d->m_date.setCalendarSystem( newCalendarSystem );
182 return true;
183}
184
185#include "kdatewidget.moc"
KCalendarSystem
KCalendarSystem::calendarSystem
KLocale::CalendarSystem calendarSystem() const
KComboBox
An enhanced combo box.
Definition: kcombobox.h:149
KDateWidget::slotDateChanged
void slotDateChanged()
Definition: kdatewidget.cpp:141
KDateWidget::changed
void changed(const QDate &date)
Emitted whenever the date of the widget is changed, either with setDate() or via user selection.
KDateWidget::init
void init(const QDate &date)
Definition: kdatewidget.cpp:70
KDateWidget::calendar
const KCalendarSystem * calendar() const
Returns the currently selected calendar system.
Definition: kdatewidget.cpp:162
KDateWidget::setDate
bool setDate(const QDate &date)
Changes the selected date to date.
Definition: kdatewidget.cpp:104
KDateWidget::setCalendarSystem
bool setCalendarSystem(KLocale::CalendarSystem calendarSystem)
Definition: kdatewidget.cpp:179
KDateWidget::KDateWidget
KDateWidget(QWidget *parent=0)
Constructs a date selection widget.
Definition: kdatewidget.cpp:59
KDateWidget::setCalendar
bool setCalendar(KCalendarSystem *calendar=0)
Changes the calendar system to use.
Definition: kdatewidget.cpp:167
KDateWidget::date
QDate date
Definition: kdatewidget.h:47
KDateWidget::~KDateWidget
virtual ~KDateWidget()
Destructs the date selection widget.
Definition: kdatewidget.cpp:99
KDialog::spacingHint
static int spacingHint()
Returns the number of pixels that should be used between widgets inside a dialog according to the KDE...
Definition: kdialog.cpp:432
KLocale::CalendarSystem
CalendarSystem
KLocalizedDate
KLocalizedDate::date
QDate date() const
KLocalizedDate::daysInMonth
int daysInMonth() const
KLocalizedDate::monthsInYear
int monthsInYear() const
KLocalizedDate::setDate
bool setDate(const QDate &date)
QWidget
kcalendarsystem.h
kcombobox.h
kdatewidget.h
kdialog.h
kglobal.h
klocalizeddate.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • 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
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal