KProperty

dateedit.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 "dateedit.h"
24
25#include "KPropertyUtils.h"
26
27#include <QLocale>
28
29class Q_DECL_HIDDEN KPropertyDateEditor::Private
30{
31public:
32 Private()
33 {
34 }
35};
36
37KPropertyDateEditor::KPropertyDateEditor(const KProperty* prop, QWidget* parent)
38 : QDateEdit(parent), d(new Private)
39{
40 setFrame(false);
41 setCalendarPopup(true);
42
43 if (prop->hasOptions()) {
44 const QDate minDate = prop->option("min", minimumDate()).toDate();
45 const QDate maxDate = prop->option("max", maximumDate()).toDate();
46 if (minDate.isValid() && maxDate.isValid() && minDate <= maxDate) {
47 setDateRange(minDate, maxDate);
48 }
49 const QString minValueText(prop->option("minValueText").toString());
50 if (!minValueText.isEmpty()) {
51 setSpecialValueText(minValueText);
52 }
53 }
54 connect(this, SIGNAL(dateChanged(QDate)), this, SLOT(onDateChanged()));
55}
56
57KPropertyDateEditor::~KPropertyDateEditor()
58{
59 delete d;
60}
61
62QVariant KPropertyDateEditor::value() const
63{
64 return QVariant(date());
65}
66
67void KPropertyDateEditor::setValue(const QVariant& value)
68{
69 blockSignals(true);
70 setDate(value.toDate());
71 blockSignals(false);
72}
73
74void KPropertyDateEditor::paintEvent(QPaintEvent* event)
75{
77 KPropertyWidgetsFactory::paintTopGridLine(this);
78}
79
80
81void KPropertyDateEditor::onDateChanged()
82{
83 emit commitData(this);
84}
85
86
87//! @todo Port to KLocale, be inspired by KexiDateTableEdit (with Kexi*Formatter)
88KPropertyDateDelegate::KPropertyDateDelegate()
89{
90}
91
92QString KPropertyDateDelegate::valueToString(const QVariant& value, const QLocale &locale) const
93{
94 const QString defaultDateFormat = locale.dateFormat(QLocale::ShortFormat);
95 return value.toDate().toString(defaultDateFormat);
96}
97
98QWidget* KPropertyDateDelegate::createEditor(int type, QWidget* parent,
99 const QStyleOptionViewItem& option, const QModelIndex& index) const
100{
101 Q_UNUSED(type);
102 Q_UNUSED(option);
103
104 KProperty *prop = KPropertyUtils::propertyForIndex(index);
105 if (!prop) {
106 return nullptr;
107 }
108 return new KPropertyDateEditor(prop, parent);
109}
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
bool isValid(int year, int month, int day)
QString toString(QStringView format, QCalendar cal) const const
virtual bool event(QEvent *event) override
QString dateFormat(FormatType format) const const
bool blockSignals(bool block)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QDate toDate() const const
QString toString() 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.