File indexing completed on 2024-04-21 04:41:02

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 "timeedit.h"
0024 #include "KPropertyUtils.h"
0025 
0026 #include <QLocale>
0027 
0028 class Q_DECL_HIDDEN KPropertyTimeEditor::Private
0029 {
0030 public:
0031     Private() {
0032     }
0033 };
0034 
0035 KPropertyTimeEditor::KPropertyTimeEditor(const KProperty* prop, QWidget* parent)
0036   : QTimeEdit(parent), d(new Private)
0037 {
0038     setFrame(false);
0039     setContentsMargins(0,1,0,0);
0040 
0041     if (prop->hasOptions()) {
0042         const QTime minTime = prop->option("min", minimumTime()).toTime();
0043         const QTime maxTime = prop->option("max", maximumTime()).toTime();
0044         if (minTime.isValid() && maxTime.isValid() && minTime <= maxTime) {
0045             setTimeRange(minTime, maxTime);
0046         }
0047         const QString minValueText(prop->option("minValueText").toString());
0048         if (!minValueText.isEmpty()) {
0049             setSpecialValueText(minValueText);
0050         }
0051     }
0052 
0053     connect(this, SIGNAL(timeChanged(QTime)), this, SLOT(onTimeChanged()));
0054 }
0055 
0056 KPropertyTimeEditor::~KPropertyTimeEditor()
0057 {
0058     delete d;
0059 }
0060 
0061 QVariant KPropertyTimeEditor::value() const
0062 {
0063     return QVariant(time());
0064 }
0065 
0066 void KPropertyTimeEditor::setValue(const QVariant& value)
0067 {
0068     blockSignals(true);
0069     setTime(value.toTime());
0070     blockSignals(false);
0071 }
0072 
0073 void KPropertyTimeEditor::paintEvent(QPaintEvent* event)
0074 {
0075     QTimeEdit::paintEvent(event);
0076     KPropertyWidgetsFactory::paintTopGridLine(this);
0077 }
0078 
0079 
0080 void KPropertyTimeEditor::onTimeChanged()
0081 {
0082     emit commitData(this);
0083 }
0084 
0085 
0086 //! @todo Port to KLocale, be inspired by KexiTimeTableEdit (with Kexi*Formatter)
0087 KPropertyTimeDelegate::KPropertyTimeDelegate()
0088 {
0089 }
0090 
0091 QString KPropertyTimeDelegate::valueToString(const QVariant& value, const QLocale &locale) const
0092 {
0093     if (locale.language() == QLocale::C) {
0094         if (value.isNull()) {
0095             return QString();
0096         }
0097         return value.toTime().toString(Qt::ISODate);
0098     }
0099     const QString defaultTimeFormat = locale.timeFormat(QLocale::ShortFormat);
0100     return value.toTime().toString(defaultTimeFormat);
0101 }
0102 
0103 QWidget* KPropertyTimeDelegate::createEditor(int type, QWidget* parent,
0104     const QStyleOptionViewItem& option, const QModelIndex& index) const
0105 {
0106     Q_UNUSED(type);
0107     Q_UNUSED(option);
0108 
0109     KProperty* prop = KPropertyUtils::propertyForIndex(index);
0110     if (!prop) {
0111         return nullptr;
0112     }
0113     return new KPropertyTimeEditor(prop, parent);
0114 }