[Kde-bindings] [kdelibs] kdeui: kdeui: Add isNull() to new date/time widgets
John Layt
jlayt at kde.org
Mon Jun 20 23:07:54 UTC 2011
Git commit d81cd465c787f629f402b2c14ddb4c0bd3a83b2e by John Layt.
Committed on 11/06/2011 at 09:54.
Pushed by jlayt into branch 'master'.
kdeui: Add isNull() to new date/time widgets
Realised that a client wouldn't be able to tell the difference between
the user entering an invalid/incomplete date and not entering a date.
Also const fixes and some other small changes from review, apidox to
follow.
CCMAIL: kde-bindings at kde.org
M +17 -0 kdeui/tests/kdatecomboboxtest.cpp
M +1 -0 kdeui/tests/kdatecomboboxtest.h
M +14 -1 kdeui/tests/kdatetimeedittest.cpp
M +1 -0 kdeui/tests/kdatetimeedittest.h
M +16 -0 kdeui/tests/ktimecomboboxtest.cpp
M +1 -0 kdeui/tests/ktimecomboboxtest.h
M +36 -19 kdeui/widgets/kdatecombobox.cpp
M +14 -3 kdeui/widgets/kdatecombobox.h
M +51 -34 kdeui/widgets/kdatetimeedit.cpp
M +42 -10 kdeui/widgets/kdatetimeedit.h
M +65 -36 kdeui/widgets/ktimecombobox.cpp
M +13 -2 kdeui/widgets/ktimecombobox.h
http://commits.kde.org/kdelibs/d81cd465c787f629f402b2c14ddb4c0bd3a83b2e
diff --git a/kdeui/tests/kdatecomboboxtest.cpp b/kdeui/tests/kdatecomboboxtest.cpp
index 639be27..a820fcd 100644
--- a/kdeui/tests/kdatecomboboxtest.cpp
+++ b/kdeui/tests/kdatecomboboxtest.cpp
@@ -25,6 +25,7 @@
#include "kdebug.h"
#include "kdatecombobox.h"
#include "kcalendarsystem.h"
+#include "klineedit.h"
QTEST_KDEMAIN(KDateComboBoxTest, GUI)
@@ -37,11 +38,27 @@ void KDateComboBoxTest::testDefaults()
QCOMPARE(m_combo->minimumDate(), KGlobal::locale()->calendar()->earliestValidDate());
QCOMPARE(m_combo->maximumDate(), KGlobal::locale()->calendar()->latestValidDate());
QCOMPARE(m_combo->isValid(), true);
+ QCOMPARE(m_combo->isNull(), false);
QCOMPARE(m_combo->options(), KDateComboBox::EditDate | KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::DateKeywords);
QCOMPARE(m_combo->displayFormat(), KLocale::ShortDate);
delete m_combo;
}
+void KDateComboBoxTest::testValidNull()
+{
+ m_combo = new KDateComboBox(0);
+ QCOMPARE(m_combo->isValid(), true);
+ QCOMPARE(m_combo->isNull(), false);
+ m_combo->setDate(QDate());
+ QCOMPARE(m_combo->isValid(), false);
+ QCOMPARE(m_combo->isNull(), true);
+ m_combo->setDate(QDate(2000, 1, 1));
+ m_combo->lineEdit()->setText("invalid");
+ QCOMPARE(m_combo->isValid(), false);
+ QCOMPARE(m_combo->isNull(), false);
+ delete m_combo;
+}
+
void KDateComboBoxTest::testDateRange()
{
m_combo = new KDateComboBox(0);
diff --git a/kdeui/tests/kdatecomboboxtest.h b/kdeui/tests/kdatecomboboxtest.h
index 2be5c41..483b0bc 100644
--- a/kdeui/tests/kdatecomboboxtest.h
+++ b/kdeui/tests/kdatecomboboxtest.h
@@ -30,6 +30,7 @@ class KDateComboBoxTest : public QWidget
private Q_SLOTS:
void testDefaults();
+ void testValidNull();
void testDateRange();
void testDateList();
void testOptions();
diff --git a/kdeui/tests/kdatetimeedittest.cpp b/kdeui/tests/kdatetimeedittest.cpp
index b572f05..2741cc7 100644
--- a/kdeui/tests/kdatetimeedittest.cpp
+++ b/kdeui/tests/kdatetimeedittest.cpp
@@ -34,11 +34,13 @@ QTEST_KDEMAIN(KDateTimeEditTest, GUI)
void KDateTimeEditTest::testDefaults()
{
m_edit = new KDateTimeEdit(0);
+ QCOMPARE(m_edit->dateTime(), KDateTime(QDate::currentDate(), QTime(0, 0, 0)));
QCOMPARE(m_edit->date(), QDate::currentDate());
- QCOMPARE(m_edit->time(), QTime(0, 0, 0, 0));
+ QCOMPARE(m_edit->time(), QTime(0, 0, 0));
QCOMPARE(m_edit->minimumDateTime(), KDateTime(KGlobal::locale()->calendar()->earliestValidDate(), QTime(0, 0, 0)));
QCOMPARE(m_edit->maximumDateTime(), KDateTime(KGlobal::locale()->calendar()->latestValidDate(), QTime(23, 59, 59, 999)));
QCOMPARE(m_edit->isValid(), true);
+ QCOMPARE(m_edit->isNull(), false);
QCOMPARE(m_edit->options(), KDateTimeEdit::ShowDate | KDateTimeEdit::EditDate | KDateTimeEdit::SelectDate | KDateTimeEdit::DatePicker | KDateTimeEdit::DateKeywords | KDateTimeEdit::ShowTime | KDateTimeEdit::EditTime | KDateTimeEdit::SelectTime);
QCOMPARE(m_edit->dateDisplayFormat(), KLocale::ShortDate);
QCOMPARE(m_edit->timeListInterval(), 15);
@@ -46,6 +48,17 @@ void KDateTimeEditTest::testDefaults()
delete m_edit;
}
+void KDateTimeEditTest::testValidNull()
+{
+ m_edit = new KDateTimeEdit(0);
+ QCOMPARE(m_edit->isValid(), true);
+ QCOMPARE(m_edit->isNull(), false);
+ m_edit->setDateTime(KDateTime());
+ QCOMPARE(m_edit->isValid(), false);
+ QCOMPARE(m_edit->isNull(), true);
+ delete m_edit;
+}
+
void KDateTimeEditTest::testDateTimeRange()
{
m_edit = new KDateTimeEdit(0);
diff --git a/kdeui/tests/kdatetimeedittest.h b/kdeui/tests/kdatetimeedittest.h
index a80ce2c..06d78b9 100644
--- a/kdeui/tests/kdatetimeedittest.h
+++ b/kdeui/tests/kdatetimeedittest.h
@@ -30,6 +30,7 @@ class KDateTimeEditTest : public QWidget
private Q_SLOTS:
void testDefaults();
+ void testValidNull();
void testDateTimeRange();
void testOptions();
void testDateDisplayFormat();
diff --git a/kdeui/tests/ktimecomboboxtest.cpp b/kdeui/tests/ktimecomboboxtest.cpp
index 2e9cbf6..053375a 100644
--- a/kdeui/tests/ktimecomboboxtest.cpp
+++ b/kdeui/tests/ktimecomboboxtest.cpp
@@ -23,6 +23,7 @@
#include "qtest_kde.h"
#include "kdebug.h"
+#include "klineedit.h"
#include <ktimecombobox.h>
QTEST_KDEMAIN(KTimeComboBoxTest, GUI)
@@ -42,6 +43,21 @@ void KTimeComboBoxTest::testDefaults()
delete m_combo;
}
+void KTimeComboBoxTest::testValidNull()
+{
+ m_combo = new KTimeComboBox(0);
+ QCOMPARE(m_combo->isValid(), true);
+ QCOMPARE(m_combo->isNull(), false);
+ m_combo->setTime(QTime());
+ QCOMPARE(m_combo->isValid(), false);
+ QCOMPARE(m_combo->isNull(), true);
+ m_combo->setTime(QTime(0, 0, 0));
+ m_combo->lineEdit()->setText("99:99");
+ QCOMPARE(m_combo->isValid(), false);
+ QCOMPARE(m_combo->isNull(), false);
+ delete m_combo;
+}
+
void KTimeComboBoxTest::testTimeRange()
{
m_combo = new KTimeComboBox();
diff --git a/kdeui/tests/ktimecomboboxtest.h b/kdeui/tests/ktimecomboboxtest.h
index bfb7291..7babca9 100644
--- a/kdeui/tests/ktimecomboboxtest.h
+++ b/kdeui/tests/ktimecomboboxtest.h
@@ -30,6 +30,7 @@ class KTimeComboBoxTest : public QWidget
private Q_SLOTS:
void testDefaults();
+ void testValidNull();
void testTimeRange();
void testTimeListInterval();
void testTimeList();
diff --git a/kdeui/widgets/kdatecombobox.cpp b/kdeui/widgets/kdatecombobox.cpp
index 42ca362..64308e8 100644
--- a/kdeui/widgets/kdatecombobox.cpp
+++ b/kdeui/widgets/kdatecombobox.cpp
@@ -298,14 +298,16 @@ void KDateComboBox::assignDate(const QDate &date)
d->m_date = date;
}
-KLocale::CalendarSystem KDateComboBox::calendarSystem()
+KLocale::CalendarSystem KDateComboBox::calendarSystem() const
{
return d->m_date.calendarSystem();
}
void KDateComboBox::setCalendarSystem(KLocale::CalendarSystem calendarSystem)
{
- assignCalendarSystem(calendarSystem);
+ if (calendarSystem != d->m_date.calendarSystem()) {
+ assignCalendarSystem(calendarSystem);
+ }
}
void KDateComboBox::assignCalendarSystem(KLocale::CalendarSystem calendarSystem)
@@ -325,11 +327,17 @@ void KDateComboBox::setCalendar(KCalendarSystem *calendar)
bool KDateComboBox::isValid() const
{
+ d->parseDate();
return d->m_date.isValid() &&
d->m_date >= d->m_minDate &&
d->m_date <= d->m_maxDate;
}
+bool KDateComboBox::isNull() const
+{
+ return lineEdit()->text().isEmpty();
+}
+
KDateComboBox::Options KDateComboBox::options() const
{
return d->m_options;
@@ -337,9 +345,11 @@ KDateComboBox::Options KDateComboBox::options() const
void KDateComboBox::setOptions(Options options)
{
- d->m_options = options;
- d->initDateWidget();
- d->updateDateWidget();
+ if (options != d->m_options) {
+ d->m_options = options;
+ d->initDateWidget();
+ d->updateDateWidget();
+ }
}
QDate KDateComboBox::minimumDate() const
@@ -374,17 +384,20 @@ void KDateComboBox::resetMaximumDate()
void KDateComboBox::setDateRange(const QDate &minDate,
const QDate &maxDate,
- const QString &minErrorMsg,
- const QString &maxErrorMsg)
+ const QString &minWarnMsg,
+ const QString &maxWarnMsg)
{
if (!minDate.isValid() || !maxDate.isValid() || minDate > maxDate) {
return;
- }
+ }
- d->m_minDate = minDate;
- d->m_maxDate = maxDate;
- d->m_minWarnMsg = minErrorMsg;
- d->m_maxWarnMsg = maxErrorMsg;
+ 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;
+ }
}
void KDateComboBox::resetDateRange()
@@ -392,16 +405,18 @@ void KDateComboBox::resetDateRange()
setDateRange(d->defaultMinDate(), d->defaultMaxDate(), QString(), QString());
}
-KLocale::DateFormat KDateComboBox::displayFormat()
+KLocale::DateFormat KDateComboBox::displayFormat() const
{
return d->m_displayFormat;
}
void KDateComboBox::setDisplayFormat(KLocale::DateFormat format)
{
- d->m_displayFormat = format;
- d->initDateWidget();
- d->updateDateWidget();
+ if (format != d->m_displayFormat) {
+ d->m_displayFormat = format;
+ d->initDateWidget();
+ d->updateDateWidget();
+ }
}
QMap<QDate, QString> KDateComboBox::dateMap() const
@@ -411,9 +426,11 @@ QMap<QDate, QString> KDateComboBox::dateMap() const
void KDateComboBox::setDateMap(QMap<QDate, QString> dateMap)
{
- d->m_dateMap.clear();
- d->m_dateMap = dateMap;
- d->initDateWidget();
+ if (dateMap != d->m_dateMap) {
+ d->m_dateMap.clear();
+ d->m_dateMap = dateMap;
+ d->initDateWidget();
+ }
}
bool KDateComboBox::eventFilter(QObject *object, QEvent *event)
diff --git a/kdeui/widgets/kdatecombobox.h b/kdeui/widgets/kdatecombobox.h
index ab2e910..63bf52f 100644
--- a/kdeui/widgets/kdatecombobox.h
+++ b/kdeui/widgets/kdatecombobox.h
@@ -85,7 +85,7 @@ public:
* @see setCalendarSystem()
* @return the Calendar System currently used
*/
- KLocale::CalendarSystem calendarSystem();
+ KLocale::CalendarSystem calendarSystem() const;
/**
* Returns a pointer to the Calendar System object used by this widget
@@ -105,9 +105,20 @@ public:
/**
* Return if the current user input is valid
*
+ * If the user input is null then it is not valid
+ *
+ * @see isNull()
* @return if the current user input is valid
*/
- bool isValid()const;
+ bool isValid() const;
+
+ /**
+ * Return if the current user input is null
+ *
+ * @see isValid()
+ * @return if the current user input is null
+ */
+ bool isNull() const;
/**
* Return the currently set widget options
@@ -123,7 +134,7 @@ public:
*
* @return the currently set date format
*/
- KLocale::DateFormat displayFormat();
+ KLocale::DateFormat displayFormat() const;
/**
* Return the current minimum date
diff --git a/kdeui/widgets/kdatetimeedit.cpp b/kdeui/widgets/kdatetimeedit.cpp
index 6f0ff75..a1d3cec 100644
--- a/kdeui/widgets/kdatetimeedit.cpp
+++ b/kdeui/widgets/kdatetimeedit.cpp
@@ -122,16 +122,16 @@ void KDateTimeEditPrivate::initDateWidget()
ui.m_dateCombo->blockSignals(true);
ui.m_dateCombo->setVisible((m_options &KDateTimeEdit::ShowDate) == KDateTimeEdit::ShowDate);
KDateComboBox::Options options;
- if ((m_options &KDateTimeEdit::EditDate) == KDateTimeEdit::EditDate) {
+ if ((m_options & KDateTimeEdit::EditDate) == KDateTimeEdit::EditDate) {
options = options | KDateComboBox::EditDate;
}
- if ((m_options &KDateTimeEdit::SelectDate) == KDateTimeEdit::SelectDate) {
+ if ((m_options & KDateTimeEdit::SelectDate) == KDateTimeEdit::SelectDate) {
options = options | KDateComboBox::SelectDate;
}
- if ((m_options &KDateTimeEdit::DatePicker) == KDateTimeEdit::DatePicker) {
+ if ((m_options & KDateTimeEdit::DatePicker) == KDateTimeEdit::DatePicker) {
options = options | KDateComboBox::DatePicker;
}
- if ((m_options &KDateTimeEdit::DateKeywords) == KDateTimeEdit::DateKeywords) {
+ if ((m_options & KDateTimeEdit::DateKeywords) == KDateTimeEdit::DateKeywords) {
options = options | KDateComboBox::DateKeywords;
}
ui.m_dateCombo->setOptions(options);
@@ -293,7 +293,7 @@ KDateTime KDateTimeEdit::dateTime() const
return d->m_dateTime;
}
-KLocale::CalendarSystem KDateTimeEdit::calendarSystem()
+KLocale::CalendarSystem KDateTimeEdit::calendarSystem() const
{
return d-> ui.m_dateCombo->calendarSystem();
}
@@ -320,20 +320,37 @@ bool KDateTimeEdit::isValid() const
d->m_dateTime <= d->m_maxDateTime;
}
+bool KDateTimeEdit::isNull() const
+{
+ return isNullDate() && isNullTime();
+}
+
bool KDateTimeEdit::isValidDate() const
{
return d->ui.m_dateCombo->isValid();
}
+bool KDateTimeEdit::isNullDate() const
+{
+ return d->ui.m_dateCombo->isNull();
+}
+
bool KDateTimeEdit::isValidTime() const
{
return d->ui.m_timeCombo->isValid();
}
+bool KDateTimeEdit::isNullTime() const
+{
+ return d->ui.m_timeCombo->isNull();
+}
+
void KDateTimeEdit::setOptions(Options options)
{
- d->m_options = options;
- d->initWidgets();
+ if (options != d->m_options) {
+ d->m_options = options;
+ d->initWidgets();
+ }
}
KDateTimeEdit::Options KDateTimeEdit::options() const
@@ -343,14 +360,12 @@ KDateTimeEdit::Options KDateTimeEdit::options() const
void KDateTimeEdit::setDateTime(const KDateTime &dateTime)
{
- if (dateTime == d->m_dateTime) {
- return;
+ if (dateTime != d->m_dateTime) {
+ assignDateTime(dateTime);
+ emit dateTimeChanged(d->m_dateTime);
+ emit dateChanged(d->m_dateTime.date());
+ emit timeChanged(d->m_dateTime.time());
}
-
- assignDateTime(dateTime);
- emit dateTimeChanged(d->m_dateTime);
- emit dateChanged(d->m_dateTime.date());
- emit timeChanged(d->m_dateTime.time());
}
void KDateTimeEdit::assignDateTime(const KDateTime &dateTime)
@@ -362,13 +377,11 @@ void KDateTimeEdit::assignDateTime(const KDateTime &dateTime)
void KDateTimeEdit::setDate(const QDate &date)
{
- if (date == d->m_dateTime.date()) {
- return;
+ if (date != d->m_dateTime.date()) {
+ assignDate(date);
+ emit dateTimeChanged(d->m_dateTime);
+ emit dateChanged(d->m_dateTime.date());
}
-
- assignDate(date);
- emit dateTimeChanged(d->m_dateTime);
- emit dateChanged(d->m_dateTime.date());
}
void KDateTimeEdit::assignDate(const QDate &date)
@@ -402,13 +415,11 @@ void KDateTimeEdit::setCalendar(KCalendarSystem *calendar)
void KDateTimeEdit::setTime(const QTime &time)
{
- if (time == d->m_dateTime.time()) {
- return;
+ if (time != d->m_dateTime.time()) {
+ assignTime(time);
+ emit dateTimeChanged(d->m_dateTime);
+ emit timeChanged(d->m_dateTime.time());
}
-
- assignTime(time);
- emit dateTimeChanged(d->m_dateTime);
- emit timeChanged(d->m_dateTime.time());
}
void KDateTimeEdit::assignTime(const QTime &time)
@@ -474,10 +485,12 @@ void KDateTimeEdit::setDateTimeRange(const KDateTime &minDateTime,
minDateTime <= maxDateTime &&
d->calendar()->isValid(minDateTime.date()) &&
d->calendar()->isValid(maxDateTime.date())) {
+
d->m_minDateTime = minDateTime;
d->m_minWarnMsg = minErrorMsg;
d->m_maxDateTime = maxDateTime;
d->m_maxWarnMsg = maxErrorMsg;
+
}
}
@@ -488,11 +501,13 @@ void KDateTimeEdit::resetDateTimeRange()
void KDateTimeEdit::setCalendarSystemsList(QList<KLocale::CalendarSystem> calendars)
{
- d->m_calendarSystems = calendars;
- d->updateCalendarWidget();
+ if (calendars != d->m_calendarSystems) {
+ d->m_calendarSystems = calendars;
+ d->updateCalendarWidget();
+ }
}
-QList<KLocale::CalendarSystem> KDateTimeEdit::calendarSystemsList()
+QList<KLocale::CalendarSystem> KDateTimeEdit::calendarSystemsList() const
{
return d->m_calendarSystems;
}
@@ -502,7 +517,7 @@ void KDateTimeEdit::setDateDisplayFormat(KLocale::DateFormat format)
d->ui.m_dateCombo->setDisplayFormat(format);
}
-KLocale::DateFormat KDateTimeEdit::dateDisplayFormat()
+KLocale::DateFormat KDateTimeEdit::dateDisplayFormat() const
{
return d->ui.m_dateCombo->displayFormat();
}
@@ -522,7 +537,7 @@ void KDateTimeEdit::setTimeDisplayFormat(KLocale::TimeFormatOptions formatOption
d->ui.m_timeCombo->setDisplayFormat(formatOptions);
}
-KLocale::TimeFormatOptions KDateTimeEdit::timeDisplayFormat()
+KLocale::TimeFormatOptions KDateTimeEdit::timeDisplayFormat() const
{
return d->ui.m_timeCombo->displayFormat();
}
@@ -551,11 +566,13 @@ QList<QTime> KDateTimeEdit::timeList() const
void KDateTimeEdit::setTimeZones(const KTimeZones::ZoneMap &zones)
{
- d->m_zones = zones;
- d->updateTimeSpecWidget();
+ if (zones != d->m_zones) {
+ d->m_zones = zones;
+ d->updateTimeSpecWidget();
+ }
}
-KTimeZones::ZoneMap KDateTimeEdit::timeZones()
+KTimeZones::ZoneMap KDateTimeEdit::timeZones() const
{
return d->m_zones;
}
diff --git a/kdeui/widgets/kdatetimeedit.h b/kdeui/widgets/kdatetimeedit.h
index de9344c..9a76da1 100644
--- a/kdeui/widgets/kdatetimeedit.h
+++ b/kdeui/widgets/kdatetimeedit.h
@@ -98,7 +98,7 @@ public:
* @see setCalendarSystem()
* @return the Calendar System currently used
*/
- KLocale::CalendarSystem calendarSystem();
+ KLocale::CalendarSystem calendarSystem() const;
/**
* Returns a pointer to the Calendar System object used by this widget
@@ -141,7 +141,7 @@ public:
*
* @param calendars the list of calendar systems to display
*/
- QList<KLocale::CalendarSystem> calendarSystemsList();
+ QList<KLocale::CalendarSystem> calendarSystemsList() const;
/**
* Return the current minimum date and time
@@ -164,7 +164,7 @@ public:
*
* @return the currently set date format
*/
- KLocale::DateFormat dateDisplayFormat();
+ KLocale::DateFormat dateDisplayFormat() const;
/**
* Return the map of dates listed in the drop-down and their displayed
@@ -182,7 +182,7 @@ public:
*
* @return the currently set time format
*/
- KLocale::TimeFormatOptions timeDisplayFormat();
+ KLocale::TimeFormatOptions timeDisplayFormat() const;
/**
* Return the time list interval able to be selected
@@ -206,28 +206,60 @@ public:
*
* @param zones the time zones to display
*/
- KTimeZones::ZoneMap timeZones();
+ KTimeZones::ZoneMap timeZones() const;
/**
* Return if the current user input is valid
*
+ * If the user input is null then it is not valid
+ *
+ * @see isNull()
* @return if the current user input is valid
*/
- bool isValid()const;
+ bool isValid() const;
+
+ /**
+ * Return if the current user input is null
+ *
+ * @see isValid()
+ * @return if the current user input is null
+ */
+ bool isNull() const;
/**
* Return if the current user input date is valid
*
- * @return if the current user input is valid
+ * If the user input date is null then it is not valid
+ *
+ * @see isNullDate()
+ * @return if the current user input date is valid
*/
- bool isValidDate()const;
+ bool isValidDate() const;
/**
+ * Return if the current user input date is null
+ *
+ * @see isValidDate()
+ * @return if the current user input date is null
+ */
+ bool isNullDate() const;
+ /**
* Return if the current user input time is valid
*
- * @return if the current user input is valid
+ * If the user input time is null then it is not valid
+ *
+ * @see isNullTime()
+ * @return if the current user input time is valid
+ */
+ bool isValidTime() const;
+
+ /**
+ * Return if the current user input time is null
+ *
+ * @see isValidTime()
+ * @return if the current user input time is null
*/
- bool isValidTime()const;
+ bool isNullTime() const;
Q_SIGNALS:
diff --git a/kdeui/widgets/ktimecombobox.cpp b/kdeui/widgets/ktimecombobox.cpp
index 7154aec..2da5ab1 100644
--- a/kdeui/widgets/ktimecombobox.cpp
+++ b/kdeui/widgets/ktimecombobox.cpp
@@ -39,7 +39,7 @@ public:
QTime defaultMinTime();
QTime defaultMaxTime();
- QString timeFormatToInputMask(const QString &format);
+ QString timeFormatToInputMask(const QString &format, bool nullMask = false);
QTime nearestIntervalTime(const QTime &time);
QString formatTime(const QTime &time);
QString convertDigits(const QString &digits);
@@ -62,6 +62,7 @@ public:
QTime m_maxTime;
QString m_minWarnMsg;
QString m_maxWarnMsg;
+ QString m_nullString;
bool m_warningShown;
KLocale::TimeFormatOptions m_displayFormat;
int m_timeListInterval;
@@ -94,14 +95,19 @@ QTime KTimeComboBoxPrivate::defaultMaxTime()
return QTime(23, 59, 59, 999);
}
-QString KTimeComboBoxPrivate::timeFormatToInputMask(const QString &format)
+QString KTimeComboBoxPrivate::timeFormatToInputMask(const QString &format, bool nullMask)
{
//TODO not sure this will always work, does it support DigitSets, am/pm is dodgy?
QString mask = formatTime(QTime(12,34,56,789));
+ QString null = mask;
mask.replace(convertDigits(QLatin1String("12")), QLatin1String("09"));
+ null.replace(convertDigits(QLatin1String("12")), QLatin1String(""));
mask.replace(convertDigits(QLatin1String("34")), QLatin1String("99"));
+ null.replace(convertDigits(QLatin1String("34")), QLatin1String(""));
mask.replace(convertDigits(QLatin1String("56")), QLatin1String("99"));
+ null.replace(convertDigits(QLatin1String("56")), QLatin1String(""));
mask.replace(convertDigits(QLatin1String("789")), QLatin1String("900"));
+ null.replace(convertDigits(QLatin1String("789")), QLatin1String(""));
if (format.contains(QLatin1String("%p")) ||
format.contains(QLatin1String("%P"))) {
QString am = KGlobal::locale()->dayPeriodText(QTime(0, 0, 0));
@@ -112,8 +118,14 @@ QString KTimeComboBoxPrivate::timeFormatToInputMask(const QString &format)
ampmMask.append(QLatin1Char('a'));
}
mask.replace(pm, ampmMask);
+ null.replace(pm, QLatin1String(""));
+ }
+
+ if (nullMask) {
+ return null;
+ } else {
+ return mask;
}
- return mask;
}
QTime KTimeComboBoxPrivate::nearestIntervalTime(const QTime &time)
@@ -148,6 +160,7 @@ void KTimeComboBoxPrivate::initTimeWidget()
// Set the input mask from the current format
q->lineEdit()->setInputMask(timeFormatToInputMask(KGlobal::locale()->timeFormat()));
+ m_nullString = timeFormatToInputMask(KGlobal::locale()->timeFormat(), true);
// If EditTime then set the line edit
q->lineEdit()->setReadOnly((m_options &KTimeComboBox::EditTime) != KTimeComboBox::EditTime);
@@ -320,6 +333,11 @@ bool KTimeComboBox::isValid() const
d->m_time <= d->m_maxTime;
}
+bool KTimeComboBox::isNull() const
+{
+ return lineEdit()->text() == d->m_nullString;
+}
+
KTimeComboBox::Options KTimeComboBox::options() const
{
return d->m_options;
@@ -327,9 +345,11 @@ KTimeComboBox::Options KTimeComboBox::options() const
void KTimeComboBox::setOptions(Options options)
{
- d->m_options = options;
- d->initTimeWidget();
- d->updateTimeWidget();
+ if (options != d->m_options) {
+ d->m_options = options;
+ d->initTimeWidget();
+ d->updateTimeWidget();
+ }
}
QTime KTimeComboBox::minimumTime() const
@@ -369,12 +389,15 @@ void KTimeComboBox::setTimeRange(const QTime &minTime, const QTime &maxTime,
return;
}
- d->m_minTime = minTime;
- d->m_maxTime = maxTime;
- d->m_minWarnMsg = minWarnMsg;
- d->m_maxWarnMsg = maxWarnMsg;
- d->initTimeWidget();
- d->updateTimeWidget();
+ if (minTime != d->m_minTime || maxTime != d->m_maxTime ||
+ minWarnMsg != d->m_minWarnMsg || maxWarnMsg != d->m_maxWarnMsg) {
+ d->m_minTime = minTime;
+ d->m_maxTime = maxTime;
+ d->m_minWarnMsg = minWarnMsg;
+ d->m_maxWarnMsg = maxWarnMsg;
+ d->initTimeWidget();
+ d->updateTimeWidget();
+ }
}
void KTimeComboBox::resetTimeRange()
@@ -382,16 +405,18 @@ void KTimeComboBox::resetTimeRange()
setTimeRange(d->defaultMinTime(), d->defaultMaxTime(), QString(), QString());
}
-KLocale::TimeFormatOptions KTimeComboBox::displayFormat()
+KLocale::TimeFormatOptions KTimeComboBox::displayFormat() const
{
return d->m_displayFormat;
}
void KTimeComboBox::setDisplayFormat(KLocale::TimeFormatOptions format)
{
- d->m_displayFormat = format;
- d->initTimeWidget();
- d->updateTimeWidget();
+ if (format != d->m_displayFormat) {
+ d->m_displayFormat = format;
+ d->initTimeWidget();
+ d->updateTimeWidget();
+ }
}
int KTimeComboBox::timeListInterval() const
@@ -401,19 +426,21 @@ int KTimeComboBox::timeListInterval() const
void KTimeComboBox::setTimeListInterval(int minutes)
{
- //Must be able to exactly divide the valid time period
- int lowMins = (d->m_minTime.hour() * 60) + d->m_minTime.minute();
- int hiMins = (d->m_maxTime.hour() * 60) + d->m_maxTime.minute();
- if (d->m_minTime.minute() == 0 && d->m_maxTime.minute() == 59) {
- ++hiMins;
- }
- if ((hiMins - lowMins) % minutes == 0) {
- d->m_timeListInterval = minutes;
- d->m_timeList.clear();
- } else {
- return;
+ if (minutes != d->m_timeListInterval) {
+ //Must be able to exactly divide the valid time period
+ int lowMins = (d->m_minTime.hour() * 60) + d->m_minTime.minute();
+ int hiMins = (d->m_maxTime.hour() * 60) + d->m_maxTime.minute();
+ if (d->m_minTime.minute() == 0 && d->m_maxTime.minute() == 59) {
+ ++hiMins;
+ }
+ if ((hiMins - lowMins) % minutes == 0) {
+ d->m_timeListInterval = minutes;
+ d->m_timeList.clear();
+ } else {
+ return;
+ }
+ d->initTimeWidget();
}
- d->initTimeWidget();
}
QList<QTime> KTimeComboBox::timeList() const
@@ -430,16 +457,18 @@ QList<QTime> KTimeComboBox::timeList() const
void KTimeComboBox::setTimeList(QList<QTime> timeList,
const QString &minWarnMsg, const QString &maxWarnMsg)
{
- d->m_timeList.clear();
- foreach (const QTime &time, timeList) {
- if (time.isValid() && !d->m_timeList.contains(time)) {
- d->m_timeList.append(time);
+ if (timeList != d->m_timeList) {
+ d->m_timeList.clear();
+ foreach (const QTime &time, timeList) {
+ if (time.isValid() && !d->m_timeList.contains(time)) {
+ d->m_timeList.append(time);
+ }
}
+ qSort(d->m_timeList);
+ // Does the updateTimeWidget call for us
+ setTimeRange(d->m_timeList.first(), d->m_timeList.last(),
+ minWarnMsg, maxWarnMsg);
}
- qSort(d->m_timeList);
- // Does the updateTimeWidget call for us
- setTimeRange(d->m_timeList.first(), d->m_timeList.last(),
- minWarnMsg, maxWarnMsg);
}
bool KTimeComboBox::eventFilter(QObject *object, QEvent *event)
diff --git a/kdeui/widgets/ktimecombobox.h b/kdeui/widgets/ktimecombobox.h
index a75aadc..cee6901 100644
--- a/kdeui/widgets/ktimecombobox.h
+++ b/kdeui/widgets/ktimecombobox.h
@@ -75,9 +75,20 @@ public:
/**
* Return if the current user input is valid
*
+ * If the user input is null then it is not valid
+ *
+ * @see isNull()
* @return if the current user input is valid
*/
- bool isValid()const;
+ bool isValid() const;
+
+ /**
+ * Return if the current user input is null
+ *
+ * @see isValid()
+ * @return if the current user input is null
+ */
+ bool isNull() const;
/**
* Return the currently set widget options
@@ -93,7 +104,7 @@ public:
*
* @return the currently set time format
*/
- KLocale::TimeFormatOptions displayFormat();
+ KLocale::TimeFormatOptions displayFormat() const;
/**
* Return the current minimum time
More information about the Kde-bindings
mailing list