KProperty

datetimeedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4  Copyright (C) 2012 Friedrich W. H. Kossebau <[email protected]>
5  Copyright (C) 2015 Jarosław Staniek <[email protected]>
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 "datetimeedit.h"
24 #include "KPropertyWidgetsFactory.h"
25 #include "KPropertyUtils.h"
26 
27 #include <QLocale>
28 
29 class Q_DECL_HIDDEN KPropertyDateTimeEditor::Private
30 {
31 public:
32  Private()
33  {
34  }
35 };
36 
37 KPropertyDateTimeEditor::KPropertyDateTimeEditor(const KProperty* prop, QWidget* parent)
38  : QDateTimeEdit(parent), d(new Private)
39 {
40  setFrame(false);
41  setCalendarPopup(true);
42 
43  if (prop->hasOptions()) {
44  const QDateTime minDateTime = prop->option("min", minimumDateTime()).toDateTime();
45  const QDateTime maxDateTime = prop->option("max", maximumDateTime()).toDateTime();
46  if (minDateTime.isValid() && maxDateTime.isValid() && minDateTime <= maxDateTime) {
47  setDateTimeRange(minDateTime, maxDateTime);
48  }
49  const QString minValueText(prop->option("minValueText").toString());
50  if (!minValueText.isEmpty()) {
51  setSpecialValueText(minValueText);
52  }
53  }
54  connect(this, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(onDateTimeChanged()));
55 }
56 
57 KPropertyDateTimeEditor::~KPropertyDateTimeEditor()
58 {
59  delete d;
60 }
61 
62 QVariant KPropertyDateTimeEditor::value() const
63 {
64  return QVariant(dateTime());
65 }
66 
67 void KPropertyDateTimeEditor::setValue(const QVariant& value)
68 {
69  blockSignals(true);
70  setDateTime(value.toDateTime());
71  blockSignals(false);
72 }
73 
74 void KPropertyDateTimeEditor::paintEvent(QPaintEvent* event)
75 {
77  KPropertyWidgetsFactory::paintTopGridLine(this);
78 }
79 
80 
81 void KPropertyDateTimeEditor::onDateTimeChanged()
82 {
83  emit commitData(this);
84 }
85 
86 
88 KPropertyDateTimeDelegate::KPropertyDateTimeDelegate()
89 {
90 }
91 
92 QString KPropertyDateTimeDelegate::valueToString(const QVariant& value, const QLocale &locale) const
93 {
94  const QString defaultDateTimeFormat = locale.dateTimeFormat(QLocale::ShortFormat);
95  return value.toDateTime().toString(defaultDateTimeFormat);
96 }
97 
98 QWidget* KPropertyDateTimeDelegate::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 KPropertyDateTimeEditor(prop, parent);
109 }
QString toString(Qt::DateFormat format) const const
QDateTime toDateTime() const const
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 ...
Definition: KProperty.cpp:564
virtual void paintEvent(QPaintEvent *event) override
bool hasOptions() const
Returns true if at least one option is specified for this property If there are no options defined tr...
Definition: KProperty.cpp:570
QLocale locale() const const
bool blockSignals(bool block)
bool isValid() const const
The base class representing a single property.
Definition: KProperty.h:95
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString dateTimeFormat(QLocale::FormatType format) const const
QString toString() const const
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.