Package: kwidgetsaddons / 5.28.0-3

Fix-KDateComboBox-checks-for-valid-entered-dates.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
From: David Jarvie <djarvie@kde.org>
Date: Sat, 31 Dec 2016 00:01:12 +0000
Subject: Fix KDateComboBox checks for valid entered dates

This fixes wrong or missing checks to determine whether an entered
date is valid, which fixes the following bugs:
- When an up/down arrow or page up/down key was pressed to change the
  date, and the minimum and maximum dates were not set, it was always
  considered invalid and the date was not changed.
- When the DateKeywords option was set, and the minimum and maximum
  dates were not set, the only date which was displayed in the menu was
  "No Date".
- setMinimumDate() and resetMinimumDate() did nothing if no maximum
  date was currently set.
- setMaximumDate() and resetMaximumDate() did nothing if no minimum
  date was currently set.
- resetDateRange() did nothing.

REVIEW: 129709
---
 autotests/kdatecomboboxtest.cpp | 22 +++++++++++
 src/kdatecombobox.cpp           | 81 +++++++++++++++++++++++++++--------------
 src/kdatecombobox.h             |  6 ++-
 3 files changed, 81 insertions(+), 28 deletions(-)

diff --git a/autotests/kdatecomboboxtest.cpp b/autotests/kdatecomboboxtest.cpp
index c15525a..07bb16c 100644
--- a/autotests/kdatecomboboxtest.cpp
+++ b/autotests/kdatecomboboxtest.cpp
@@ -95,6 +95,28 @@ void KDateComboBoxTest::testDateRange()
     QCOMPARE(m_combo->minimumDate(), QDate(2000, 1, 1));
     QCOMPARE(m_combo->maximumDate(), QDate(2003, 1, 1));
 
+    m_combo->resetDateRange();
+    QVERIFY(!m_combo->minimumDate().isValid());
+    QVERIFY(!m_combo->maximumDate().isValid());
+
+    // Check functioning when the minimum or maximum date is not already set
+
+    m_combo->setMinimumDate(QDate(2000, 1, 1));
+    QCOMPARE(m_combo->minimumDate(), QDate(2000, 1, 1));
+    QVERIFY(!m_combo->maximumDate().isValid());
+
+    m_combo->resetMinimumDate();
+    QVERIFY(!m_combo->minimumDate().isValid());
+    QVERIFY(!m_combo->maximumDate().isValid());
+
+    m_combo->setMaximumDate(QDate(2003, 1, 1));
+    QVERIFY(!m_combo->minimumDate().isValid());
+    QCOMPARE(m_combo->maximumDate(), QDate(2003, 1, 1));
+
+    m_combo->resetMaximumDate();
+    QVERIFY(!m_combo->minimumDate().isValid());
+    QVERIFY(!m_combo->maximumDate().isValid());
+
     delete m_combo;
 }
 
diff --git a/src/kdatecombobox.cpp b/src/kdatecombobox.cpp
index ad1d085..2df1e9d 100644
--- a/src/kdatecombobox.cpp
+++ b/src/kdatecombobox.cpp
@@ -51,6 +51,11 @@ public:
     void addMenuAction(const QString &text, const QDate &date);
     void enableMenuDates();
     void updateDateWidget();
+    void setDateRange(const QDate &minDate,
+                      const QDate &maxDate,
+                      const QString &minWarnMsg,
+                      const QString &maxWarnMsg);
+    bool isInDateRange(const QDate &date) const;
 
 // Q_PRIVATE_SLOTs
     void clickDate();
@@ -134,7 +139,7 @@ void KDateComboBoxPrivate::initDateWidget()
     // If EditTime then set the line edit
     q->lineEdit()->setReadOnly((m_options & KDateComboBox::EditDate) != KDateComboBox::EditDate);
 
-    // If SelectTime then make list items visible
+    // If SelectDate then make list items visible
     if ((m_options & KDateComboBox::SelectDate) == KDateComboBox::SelectDate ||
             (m_options & KDateComboBox::DatePicker) == KDateComboBox::DatePicker ||
             (m_options & KDateComboBox::DatePicker) == KDateComboBox::DateKeywords) {
@@ -205,7 +210,7 @@ void KDateComboBoxPrivate::enableMenuDates()
     // Hide menu dates if they are outside the date range
     for (int i = 0; i < m_actions.count(); ++i) {
         QDate date = m_actions[i]->data().toDate();
-        m_actions[i]->setVisible(!date.isValid() || (date >= m_minDate && date <= m_maxDate));
+        m_actions[i]->setVisible(!date.isValid() || isInDateRange(date));
     }
 }
 
@@ -222,16 +227,48 @@ void KDateComboBoxPrivate::updateDateWidget()
     q->blockSignals(false);
 }
 
+void KDateComboBoxPrivate::setDateRange(const QDate &minDate,
+                                        const QDate &maxDate,
+                                        const QString &minWarnMsg,
+                                        const QString &maxWarnMsg)
+{
+    if (minDate.isValid() && maxDate.isValid() && minDate > maxDate) {
+        return;
+    }
+
+    if (minDate != m_minDate || maxDate != m_maxDate ||
+            minWarnMsg != m_minWarnMsg || maxWarnMsg != m_maxWarnMsg) {
+        m_minDate = minDate;
+        m_maxDate = maxDate;
+        m_minWarnMsg = minWarnMsg;
+        m_maxWarnMsg = maxWarnMsg;
+    }
+    enableMenuDates();
+}
+
+bool KDateComboBoxPrivate::isInDateRange(const QDate &date) const
+{
+    return date.isValid() &&
+           (!m_minDate.isValid() || date >= m_minDate) &&
+           (!m_maxDate.isValid() || date <= m_maxDate);
+}
+
 void KDateComboBoxPrivate::selectDate(QAction *action)
 {
     if (action->objectName() != QLatin1String("DatePicker")) {
-        enterDate(action->data().toDate());
+        QDate date = action->data().toDate();
+        if (isInDateRange(date)) {
+            enterDate(date);
+        }
     }
 }
 
 void KDateComboBoxPrivate::clickDate()
 {
-    enterDate(m_datePicker->date());
+    QDate date = m_datePicker->date();
+    if (isInDateRange(date)) {
+        enterDate(date);
+    }
 }
 
 void KDateComboBoxPrivate::editDate(const QString &text)
@@ -341,9 +378,7 @@ void KDateComboBox::assignDate(const QDate &date)
 bool KDateComboBox::isValid() const
 {
     d->parseDate();
-    return d->m_date.isValid() &&
-           (!d->m_minDate.isValid() || d->m_date >= d->m_minDate) &&
-           (!d->m_maxDate.isValid() || d->m_date <= d->m_maxDate);
+    return d->isInDateRange(d->m_date);
 }
 
 bool KDateComboBox::isNull() const
@@ -372,13 +407,14 @@ QDate KDateComboBox::minimumDate() const
 
 void KDateComboBox::setMinimumDate(const QDate &minDate, const QString &minWarnMsg)
 {
-    setDateRange(minDate, d->m_maxDate, minWarnMsg, d->m_maxWarnMsg);
+    if (minDate.isValid()) {
+        d->setDateRange(minDate, d->m_maxDate, minWarnMsg, d->m_maxWarnMsg);
+    }
 }
 
 void KDateComboBox::resetMinimumDate()
 {
-    //setDateRange(d->m_minDate, d->defaultMaxDate(), d->m_minWarnMsg, QString());
-    setDateRange(QDate(), d->m_maxDate, QString(), d->m_maxWarnMsg);
+    d->setDateRange(QDate(), d->m_maxDate, QString(), d->m_maxWarnMsg);
 }
 
 QDate KDateComboBox::maximumDate() const
@@ -388,13 +424,14 @@ QDate KDateComboBox::maximumDate() const
 
 void KDateComboBox::setMaximumDate(const QDate &maxDate, const QString &maxWarnMsg)
 {
-    setDateRange(d->m_minDate, maxDate, d->m_minWarnMsg, maxWarnMsg);
+    if (maxDate.isValid()) {
+        d->setDateRange(d->m_minDate, maxDate, d->m_minWarnMsg, maxWarnMsg);
+    }
 }
 
 void KDateComboBox::resetMaximumDate()
 {
-    //setDateRange(d->m_minDate, d->defaultMaxDate(), d->m_minWarnMsg, QString());
-    setDateRange(d->m_minDate, QDate(), d->m_minWarnMsg, QString());
+    d->setDateRange(d->m_minDate, QDate(), d->m_minWarnMsg, QString());
 }
 
 void KDateComboBox::setDateRange(const QDate &minDate,
@@ -402,24 +439,14 @@ void KDateComboBox::setDateRange(const QDate &minDate,
                                  const QString &minWarnMsg,
                                  const QString &maxWarnMsg)
 {
-    if (!minDate.isValid() || !maxDate.isValid() || minDate > maxDate) {
-        return;
-    }
-
-    if (minDate != d->m_minDate || maxDate != d->m_maxDate ||
-            minWarnMsg != d->m_minWarnMsg || maxWarnMsg != d->m_maxWarnMsg) {
-        d->m_minDate = minDate;
-        d->m_maxDate = maxDate;
-        d->m_minWarnMsg = minWarnMsg;
-        d->m_maxWarnMsg = maxWarnMsg;
+    if (minDate.isValid() && maxDate.isValid()) {
+        d->setDateRange(minDate, maxDate, minWarnMsg, maxWarnMsg);
     }
-    d->enableMenuDates();
 }
 
 void KDateComboBox::resetDateRange()
 {
-    //setDateRange(d->defaultMinDate(), d->defaultMaxDate(), QString(), QString());
-    setDateRange(QDate(), QDate(), QString(), QString());
+    d->setDateRange(QDate(), QDate(), QString(), QString());
 }
 
 QLocale::FormatType KDateComboBox::displayFormat() const
@@ -475,7 +502,7 @@ void KDateComboBox::keyPressEvent(QKeyEvent *keyEvent)
         QComboBox::keyPressEvent(keyEvent);
         return;
     }
-    if (temp.isValid() && temp >= d->m_minDate && temp <= d->m_maxDate) {
+    if (d->isInDateRange(temp)) {
         d->enterDate(temp);
     }
 }
diff --git a/src/kdatecombobox.h b/src/kdatecombobox.h
index d9a20ca..458c701 100644
--- a/src/kdatecombobox.h
+++ b/src/kdatecombobox.h
@@ -227,7 +227,9 @@ public Q_SLOTS:
     void setMinimumDate(const QDate &minTime, const QString &minWarnMsg = QString());
 
     /**
-     * Reset the minimum date to the default
+     * Reset the minimum date to the default.
+     *
+     * The default is to have no minimum date.
      */
     void resetMinimumDate();
 
@@ -248,6 +250,8 @@ public Q_SLOTS:
 
     /**
      * Reset the maximum date to the default
+     *
+     * The default is to have no maximum date.
      */
     void resetMaximumDate();