KProperty

timeedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4 Copyright (C) 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
5 Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21*/
22
23#include "timeedit.h"
24#include "KPropertyUtils.h"
25
26#include <QLocale>
27
28class Q_DECL_HIDDEN KPropertyTimeEditor::Private
29{
30public:
31 Private() {
32 }
33};
34
35KPropertyTimeEditor::KPropertyTimeEditor(const KProperty* prop, QWidget* parent)
36 : QTimeEdit(parent), d(new Private)
37{
38 setFrame(false);
39 setContentsMargins(0,1,0,0);
40
41 if (prop->hasOptions()) {
42 const QTime minTime = prop->option("min", minimumTime()).toTime();
43 const QTime maxTime = prop->option("max", maximumTime()).toTime();
44 if (minTime.isValid() && maxTime.isValid() && minTime <= maxTime) {
45 setTimeRange(minTime, maxTime);
46 }
47 const QString minValueText(prop->option("minValueText").toString());
48 if (!minValueText.isEmpty()) {
49 setSpecialValueText(minValueText);
50 }
51 }
52
53 connect(this, SIGNAL(timeChanged(QTime)), this, SLOT(onTimeChanged()));
54}
55
56KPropertyTimeEditor::~KPropertyTimeEditor()
57{
58 delete d;
59}
60
61QVariant KPropertyTimeEditor::value() const
62{
63 return QVariant(time());
64}
65
66void KPropertyTimeEditor::setValue(const QVariant& value)
67{
68 blockSignals(true);
69 setTime(value.toTime());
70 blockSignals(false);
71}
72
73void KPropertyTimeEditor::paintEvent(QPaintEvent* event)
74{
76 KPropertyWidgetsFactory::paintTopGridLine(this);
77}
78
79
80void KPropertyTimeEditor::onTimeChanged()
81{
82 emit commitData(this);
83}
84
85
86//! @todo Port to KLocale, be inspired by KexiTimeTableEdit (with Kexi*Formatter)
87KPropertyTimeDelegate::KPropertyTimeDelegate()
88{
89}
90
91QString KPropertyTimeDelegate::valueToString(const QVariant& value, const QLocale &locale) const
92{
93 if (locale.language() == QLocale::C) {
94 if (value.isNull()) {
95 return QString();
96 }
97 return value.toTime().toString(Qt::ISODate);
98 }
99 const QString defaultTimeFormat = locale.timeFormat(QLocale::ShortFormat);
100 return value.toTime().toString(defaultTimeFormat);
101}
102
103QWidget* KPropertyTimeDelegate::createEditor(int type, QWidget* parent,
104 const QStyleOptionViewItem& option, const QModelIndex& index) const
105{
106 Q_UNUSED(type);
107 Q_UNUSED(option);
108
109 KProperty* prop = KPropertyUtils::propertyForIndex(index);
110 if (!prop) {
111 return nullptr;
112 }
113 return new KPropertyTimeEditor(prop, parent);
114}
The base class representing a single property.
Definition KProperty.h:96
QVariant option(const char *name, const QVariant &defaultValue=QVariant()) const
Returns value of given option Option is set if returned value is not null. If there is no option for ...
bool hasOptions() const
Returns true if at least one option is specified for this property If there are no options defined tr...
virtual void paintEvent(QPaintEvent *event) override
virtual bool event(QEvent *event) override
Language language() const const
QString timeFormat(FormatType format) const const
bool blockSignals(bool block)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isValid(int h, int m, int s, int ms)
QString toString(QStringView format) const const
bool isNull() const const
QString toString() const const
QTime toTime() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:57:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.