File indexing completed on 2024-03-24 04:43:37
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 "datetimeedit.h" 0024 #include "KPropertyWidgetsFactory.h" 0025 #include "KPropertyUtils.h" 0026 0027 #include <QLocale> 0028 0029 class Q_DECL_HIDDEN KPropertyDateTimeEditor::Private 0030 { 0031 public: 0032 Private() 0033 { 0034 } 0035 }; 0036 0037 KPropertyDateTimeEditor::KPropertyDateTimeEditor(const KProperty* prop, QWidget* parent) 0038 : QDateTimeEdit(parent), d(new Private) 0039 { 0040 setFrame(false); 0041 setCalendarPopup(true); 0042 0043 if (prop->hasOptions()) { 0044 const QDateTime minDateTime = prop->option("min", minimumDateTime()).toDateTime(); 0045 const QDateTime maxDateTime = prop->option("max", maximumDateTime()).toDateTime(); 0046 if (minDateTime.isValid() && maxDateTime.isValid() && minDateTime <= maxDateTime) { 0047 setDateTimeRange(minDateTime, maxDateTime); 0048 } 0049 const QString minValueText(prop->option("minValueText").toString()); 0050 if (!minValueText.isEmpty()) { 0051 setSpecialValueText(minValueText); 0052 } 0053 } 0054 connect(this, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(onDateTimeChanged())); 0055 } 0056 0057 KPropertyDateTimeEditor::~KPropertyDateTimeEditor() 0058 { 0059 delete d; 0060 } 0061 0062 QVariant KPropertyDateTimeEditor::value() const 0063 { 0064 return QVariant(dateTime()); 0065 } 0066 0067 void KPropertyDateTimeEditor::setValue(const QVariant& value) 0068 { 0069 blockSignals(true); 0070 setDateTime(value.toDateTime()); 0071 blockSignals(false); 0072 } 0073 0074 void KPropertyDateTimeEditor::paintEvent(QPaintEvent* event) 0075 { 0076 QDateTimeEdit::paintEvent(event); 0077 KPropertyWidgetsFactory::paintTopGridLine(this); 0078 } 0079 0080 0081 void KPropertyDateTimeEditor::onDateTimeChanged() 0082 { 0083 emit commitData(this); 0084 } 0085 0086 0087 //! @todo Port to KLocale, be inspired by KexiDateTimeTableEdit (with Kexi*Formatter) 0088 KPropertyDateTimeDelegate::KPropertyDateTimeDelegate() 0089 { 0090 } 0091 0092 QString KPropertyDateTimeDelegate::valueToString(const QVariant& value, const QLocale &locale) const 0093 { 0094 const QString defaultDateTimeFormat = locale.dateTimeFormat(QLocale::ShortFormat); 0095 return value.toDateTime().toString(defaultDateTimeFormat); 0096 } 0097 0098 QWidget* KPropertyDateTimeDelegate::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 KPropertyDateTimeEditor(prop, parent); 0109 }