File indexing completed on 2024-04-14 04:36:11

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
0004    Copyright (C) 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
0005    Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "dateedit.h"
0024 
0025 #include "KPropertyUtils.h"
0026 
0027 #include <QLocale>
0028 
0029 class Q_DECL_HIDDEN KPropertyDateEditor::Private
0030 {
0031 public:
0032     Private()
0033     {
0034     }
0035 };
0036 
0037 KPropertyDateEditor::KPropertyDateEditor(const KProperty* prop, QWidget* parent)
0038   : QDateEdit(parent), d(new Private)
0039 {
0040     setFrame(false);
0041     setCalendarPopup(true);
0042 
0043     if (prop->hasOptions()) {
0044         const QDate minDate = prop->option("min", minimumDate()).toDate();
0045         const QDate maxDate = prop->option("max", maximumDate()).toDate();
0046         if (minDate.isValid() && maxDate.isValid() && minDate <= maxDate) {
0047             setDateRange(minDate, maxDate);
0048         }
0049         const QString minValueText(prop->option("minValueText").toString());
0050         if (!minValueText.isEmpty()) {
0051             setSpecialValueText(minValueText);
0052         }
0053     }
0054     connect(this, SIGNAL(dateChanged(QDate)), this, SLOT(onDateChanged()));
0055 }
0056 
0057 KPropertyDateEditor::~KPropertyDateEditor()
0058 {
0059     delete d;
0060 }
0061 
0062 QVariant KPropertyDateEditor::value() const
0063 {
0064     return QVariant(date());
0065 }
0066 
0067 void KPropertyDateEditor::setValue(const QVariant& value)
0068 {
0069     blockSignals(true);
0070     setDate(value.toDate());
0071     blockSignals(false);
0072 }
0073 
0074 void KPropertyDateEditor::paintEvent(QPaintEvent* event)
0075 {
0076     QDateEdit::paintEvent(event);
0077     KPropertyWidgetsFactory::paintTopGridLine(this);
0078 }
0079 
0080 
0081 void KPropertyDateEditor::onDateChanged()
0082 {
0083     emit commitData(this);
0084 }
0085 
0086 
0087 //! @todo Port to KLocale, be inspired by KexiDateTableEdit (with Kexi*Formatter)
0088 KPropertyDateDelegate::KPropertyDateDelegate()
0089 {
0090 }
0091 
0092 QString KPropertyDateDelegate::valueToString(const QVariant& value, const QLocale &locale) const
0093 {
0094     const QString defaultDateFormat = locale.dateFormat(QLocale::ShortFormat);
0095     return value.toDate().toString(defaultDateFormat);
0096 }
0097 
0098 QWidget* KPropertyDateDelegate::createEditor(int type, QWidget* parent,
0099     const QStyleOptionViewItem& option, const QModelIndex& index) const
0100 {
0101     Q_UNUSED(type);
0102     Q_UNUSED(option);
0103 
0104     KProperty *prop = KPropertyUtils::propertyForIndex(index);
0105     if (!prop) {
0106         return nullptr;
0107     }
0108     return new KPropertyDateEditor(prop, parent);
0109 }