KDELibs4Support

kdatewidget.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001 Waldo Bastian ([email protected])
3  Copyright 2007, 2010 John Layt <[email protected]>
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 <QDate>
23 #include <QLayout>
24 #include <QLineEdit>
25 #include <QDoubleSpinBox>
26 
27 #include <kcombobox.h>
28 
29 #include "kcalendarsystem.h"
30 #include "klocalizeddate.h"
31 
32 class KDateWidgetSpinBox : public QSpinBox
33 {
34 public:
35  KDateWidgetSpinBox(int min, int max, QWidget *parent) : QSpinBox(parent)
36  {
37  setRange(qMin(min, max), qMax(min, max));
38  setSingleStep(1);
40  }
41 };
42 
43 class Q_DECL_HIDDEN KDateWidget::KDateWidgetPrivate
44 {
45 public:
46  KDateWidgetSpinBox *m_day;
47  KComboBox *m_month;
48  KDateWidgetSpinBox *m_year;
49  KLocalizedDate m_date;
50  // Need to keep a QDate copy as the "const QDate &date() const;" method returns a reference
51  // and returning m_date.date() creates a temporary leading to crashes. Doh!
52  QDate m_refDate;
53 };
54 
55 KDateWidget::KDateWidget(QWidget *parent) : QWidget(parent), d(new KDateWidgetPrivate)
56 {
57  initWidget(QDate::currentDate());
58 }
59 
61  : QWidget(parent), d(new KDateWidgetPrivate)
62 {
63  initWidget(date);
64 }
65 
66 void KDateWidget::initWidget(const QDate &date)
67 {
68  QHBoxLayout *layout = new QHBoxLayout(this);
69  layout->setContentsMargins(0, 0, 0, 0);
70  const int spacingHint = style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
71  layout->setSpacing(spacingHint);
72 
73  d->m_day = new KDateWidgetSpinBox(1, 31, this);
74  d->m_month = new KComboBox(this);
75  d->m_year = new KDateWidgetSpinBox(calendar()->year(calendar()->earliestValidDate()),
76  calendar()->year(calendar()->latestValidDate()), this);
77 
78  layout->addWidget(d->m_day);
79  layout->addWidget(d->m_month);
80  layout->addWidget(d->m_year);
81 
82  connect(d->m_day, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
83  connect(d->m_month, SIGNAL(activated(int)), this, SLOT(slotDateChanged()));
84  connect(d->m_year, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
85 
86  setFocusProxy(d->m_day);
88 
89  if (calendar()->isValid(date)) {
90  setDate(date);
91  } else {
93  }
94 }
95 
97 {
98  delete d;
99 }
100 
101 bool KDateWidget::setDate(const QDate &date)
102 {
103  if (calendar()->isValid(date)) {
104  bool dayBlocked = d->m_day->blockSignals(true);
105  bool monthBlocked = d->m_month->blockSignals(true);
106  bool yearBlocked = d->m_year->blockSignals(true);
107 
108  d->m_date.setDate(date);
109  d->m_refDate = date;
110 
111  d->m_day->setMaximum(d->m_date.daysInMonth());
112  d->m_day->setValue(d->m_date.day());
113 
114  d->m_month->clear();
115  d->m_month->setMaxVisibleItems(d->m_date.monthsInYear());
116  for (int m = 1; m <= d->m_date.monthsInYear(); ++m) {
117  d->m_month->addItem(calendar()->monthName(m, d->m_date.year()));
118  }
119  d->m_month->setCurrentIndex(d->m_date.month() - 1);
120 
121  d->m_year->setValue(d->m_date.year());
122 
123  d->m_day->blockSignals(dayBlocked);
124  d->m_month->blockSignals(monthBlocked);
125  d->m_year->blockSignals(yearBlocked);
126 
127  emit changed(d->m_refDate);
128  return true;
129  }
130  return false;
131 }
132 
133 const QDate &KDateWidget::date() const
134 {
135  return d->m_refDate;
136 }
137 
138 void KDateWidget::slotDateChanged()
139 {
141  int y, m, day;
142 
143  y = d->m_year->value();
144  y = qMin(qMax(y, calendar()->year(calendar()->earliestValidDate())),
145  calendar()->year(calendar()->latestValidDate()));
146 
147  date.setDate(y, 1, 1);
148  m = d->m_month->currentIndex() + 1;
149  m = qMin(qMax(m, 1), date.monthsInYear());
150 
151  date.setDate(y, m, 1);
152  day = d->m_day->value();
153  day = qMin(qMax(day, 1), date.daysInMonth());
154 
155  date.setDate(y, m, day);
156  setDate(date.date());
157 }
158 
160 {
161  return d->m_date.calendar();
162 }
163 
165 {
166  QDate oldDate = date();
167  d->m_date = KLocalizedDate(oldDate, newCalendar);
168  return setDate(oldDate);
169 }
170 
172 {
173  d->m_date.setCalendarSystem(newCalendarSystem);
174  return true;
175 }
176 
QLayout * layout() const const
StrongFocus
void setContentsMargins(int left, int top, int right, int bottom)
const KCalendarSystem * calendar() const
Returns the currently selected calendar system.
void setFocusPolicy(Qt::FocusPolicy policy)
QStyle * style() const const
virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const =0
int y() const const
QDate date() const
Returns the currently set date as a QDate.
const QDate & date() const
Returns the currently selected date.
AlignRight
void setRange(int minimum, int maximum)
KCalendarSystem abstract base class, provides support for local Calendar Systems in KDE...
bool setCalendar(KCalendarSystem *calendar=nullptr)
Changes the calendar system to use.
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
CalendarSystem
Definition: klocale.h:646
bool setDate(const QDate &date)
Set the date using a QDate.
A date selection widget.
Definition: kdatewidget.h:44
QLineEdit * lineEdit() const const
void setFocusProxy(QWidget *w)
A class representing a date localized using the local calendar system, language and formats...
int monthsInYear() const
Returns number of months in the year.
KDateWidget(QWidget *parent=nullptr)
Constructs a date selection widget.
Definition: kdatewidget.cpp:55
void setSingleStep(int val)
int daysInMonth() const
Returns the number of days in the month.
void setAlignment(Qt::Alignment flag)
bool setDate(const QDate &date)
Changes the selected date to date.
PM_DefaultLayoutSpacing
QDate currentDate()
void changed(const QDate &date)
Emitted whenever the date of the widget is changed, either with setDate() or via user selection...
bool setCalendarSystem(KLocale::CalendarSystem calendarSystem)
virtual ~KDateWidget()
Destructs the date selection widget.
Definition: kdatewidget.cpp:96
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSpacing(int spacing)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 4 2020 22:57:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.